package com.yunfeiyun.agmp.fms.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yunfeiyun.agmp.common.enums.SysWarnEnum;
import com.yunfeiyun.agmp.fms.domain.SysWarn;
import com.yunfeiyun.agmp.fms.domain.reqvo.SysWarnReqVo;
import com.yunfeiyun.agmp.fms.domain.resvo.SysWarnResVo;
import com.yunfeiyun.agmp.fms.service.ISysWarnService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.yunfeiyun.agmp.common.annotation.Log;
import com.yunfeiyun.agmp.common.core.controller.BaseController;
import com.yunfeiyun.agmp.common.core.domain.AjaxResult;
import com.yunfeiyun.agmp.common.enums.BusinessType;
import com.yunfeiyun.agmp.common.core.page.TableDataInfo;

/**
 * 系统预警Controller
 * 
 * @author 杨晓辉
 * @date 2023-07-05
 */
@RestController
@RequestMapping("/sys/warn")
public class SysWarnController extends BaseController
{
    @Autowired
    private ISysWarnService sysWarnService;

    /**
     * 查询系统预警列表
     */
    @PreAuthorize("@ss.hasPermi('sys:warn:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysWarnReqVo reqVo)
    {
        startPage();
        reqVo.setUserId(getUserId());
        List<SysWarnResVo> list = sysWarnService.selectSysWarnList(reqVo);
        return getDataTable(list);
    }

    /**
     * 查询系统预警列表
     */
    @PreAuthorize("@ss.hasPermi('sys:warn:count:list')")
    @GetMapping("/count/list")
    public TableDataInfo countList(SysWarnReqVo reqVo)
    {
        List<SysWarnResVo> list = sysWarnService.selectCountOfSysWarn(reqVo);
        return getDataTable(list);
    }

    /**
     * 查询系统预警列表
     */
    @PreAuthorize("@ss.hasPermi('sys:warn:count:info')")
    @GetMapping("/count/info")
    public AjaxResult countInfo(SysWarnReqVo reqVo)
    {
        List<SysWarnResVo> list = sysWarnService.selectCountOfSysWarn(reqVo);
        Map<String, Object> result = new HashMap<>();
        result.put("count1", "0");
        result.put("count2", "0");
        result.put("count3", "0");
        result.put("count4", "0");
        result.put("count5", "0");
        for (SysWarnResVo sysWarnResVo : list) {
            if (SysWarnEnum.SysWarnClass.PRODUCTIVE_PROCESS.getCode().equals(sysWarnResVo.getWarnClass())) {
                result.put("count1", sysWarnResVo.getCount());
            } else if (SysWarnEnum.SysWarnClass.QUALITY_INSPECTION_PROCESS.getCode().equals(sysWarnResVo.getWarnClass())) {
                result.put("count2", sysWarnResVo.getCount());
            } else if (SysWarnEnum.SysWarnClass.SALES_PROCESS.getCode().equals(sysWarnResVo.getWarnClass())) {
                result.put("count3", sysWarnResVo.getCount());
            } else if (SysWarnEnum.SysWarnClass.TRANSPORTATION_PROCESS.getCode().equals(sysWarnResVo.getWarnClass())) {
                result.put("count4", sysWarnResVo.getCount());
            } else if (SysWarnEnum.SysWarnClass.SCAN_CODE_PROCESS.getCode().equals(sysWarnResVo.getWarnClass())) {
                result.put("count5", sysWarnResVo.getCount());
            }
        }

        return success(result);
    }

    /**
     * 已读
     */
    @PreAuthorize("@ss.hasPermi('sys:warn:read')")
    @Log(title = "系统预警", businessType = BusinessType.INSERT)
    @PostMapping("/read")
    public AjaxResult read(@RequestBody SysWarn sysWarn)
    {
        return toAjax(sysWarnService.read(sysWarn));
    }

    /**
     * 获取系统预警详细信息
     */
    @PreAuthorize("@ss.hasPermi('sys:warn:info')")
    @GetMapping(value = "/info")
    public AjaxResult getInfo(@RequestParam("warnId") String warnId)
    {
        return success(sysWarnService.selectSysWarnByWarnId(warnId));
    }

    /**
     * 新增系统预警
     */
    @PreAuthorize("@ss.hasPermi('sys:warn:add')")
    @Log(title = "系统预警", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SysWarn sysWarn)
    {
        return toAjax(sysWarnService.insertSysWarn(sysWarn));
    }

    /**
     * 修改系统预警
     */
    @PreAuthorize("@ss.hasPermi('sys:warn:edit')")
    @Log(title = "系统预警", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody SysWarn sysWarn)
    {
        return toAjax(sysWarnService.updateSysWarn(sysWarn));
    }

    /**
     * 删除系统预警
     */
    @PreAuthorize("@ss.hasPermi('sys:warn:delete')")
    @Log(title = "系统预警", businessType = BusinessType.DELETE)
	@DeleteMapping("/delete")
    public AjaxResult remove(@RequestParam("warnIds") String[] warnIds)
    {
        return toAjax(sysWarnService.deleteSysWarnByWarnIds(warnIds));
    }
}
