package com.ruoyi.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.system.domain.ZsOperationWarnconfig;
import com.ruoyi.system.task.MonitorTaskScheduler;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.ZsOperationWarnresultVo;
import com.ruoyi.system.domain.bo.ZsOperationWarnresultBo;
import com.ruoyi.system.service.IZsOperationWarnresultService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 预警结果
 *
 * @author 王强
 * @date 2025-05-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/operationWarnresult")
@Slf4j
public class ZsOperationWarnresultController extends BaseController {

    private final IZsOperationWarnresultService iZsOperationWarnresultService;

    /**
     * 查询预警结果列表
     */
    @SaCheckPermission("system:operationWarnresult:list")
    @GetMapping("/list")
    public TableDataInfo<ZsOperationWarnresultVo> list(ZsOperationWarnresultBo bo, PageQuery pageQuery) {
        return iZsOperationWarnresultService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出预警结果列表
     */
    @SaCheckPermission("system:operationWarnresult:export")
    @Log(title = "预警结果", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ZsOperationWarnresultBo bo, HttpServletResponse response) {
        List<ZsOperationWarnresultVo> list = iZsOperationWarnresultService.queryList(bo);
        ExcelUtil.exportExcel(list, "预警结果", ZsOperationWarnresultVo.class, response);
    }

    /**
     * 获取预警结果详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:operationWarnresult:query")
    @GetMapping("/{id}")
    public R<ZsOperationWarnresultVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iZsOperationWarnresultService.queryById(id));
    }

    /**
     * 新增预警结果
     */
    @SaCheckPermission("system:operationWarnresult:add")
    @Log(title = "预警结果", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ZsOperationWarnresultBo bo) {
        return toAjax(iZsOperationWarnresultService.insertByBo(bo));
    }

    /**
     * 修改预警结果
     */
    @SaCheckPermission("system:operationWarnresult:edit")
    @Log(title = "预警结果", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ZsOperationWarnresultBo bo) {
        return toAjax(iZsOperationWarnresultService.updateByBo(bo));
    }

    /**
     * 删除预警结果
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:operationWarnresult:remove")
    @Log(title = "预警结果", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iZsOperationWarnresultService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    //立即调用预警监控
    @SaIgnore
    @PostMapping("/receiveLatestGoodsInfo")
    public Map receiveLatestGoodsInfo(@RequestBody Map map) {
        try {
            return iZsOperationWarnresultService.receiveLatestGoodsInfo(map);
        } catch (Exception e) {
            Map reMap = new HashMap();
            reMap.put("success",false);
            reMap.put("message","失败");
            log.error("最新商品信息接收失败！=：",e);
            return reMap;
        }
    }

}
