package com.pqs9300.controller.strategy;

import com.njcn.pojo.commons.HttpResult;
import com.njcn.utils.PubUtils;
import com.njcn.utils.XssFilterUtil;
import com.pqs9300.pojo.strategy.ParamDataVO;
import com.pqs9300.pojo.strategy.ThsAlarmFormAss;
import com.pqs9300.pojo.strategy.ThsAlarmFormDTO;
import com.pqs9300.pojo.strategy.DicStrategyRVO;
import com.pqs9300.pojo.transaction.OverRunLog;
import com.pqs9300.service.strategy.WarnStrategyService;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Description 预警告警策略控制器
 * @Author cdf
 * @Date 2020/6/17 10:28
 **/
@Controller
@RequestMapping("strategy")
public class WarningStrategyController {

    @Resource
    private WarnStrategyService warnStrategyService;

    /**
     * @Author chendaofei
     * @Description 新增预警策略
     * @Date 15:27 2020/6/17
     **/
    @PostMapping("addEarlyWarn")
    @ResponseBody
    public HttpResult addEarlyWarn(@RequestBody ThsAlarmFormDTO thsAlarmFormDTO) {
        HttpResult httpResult = new HttpResult();
        try {
            if (PubUtils.isBlank(thsAlarmFormDTO.getWfName()) || thsAlarmFormDTO.getGrade() == null) {
                httpResult.setCode(500);
                httpResult.setMessage("参数不能为空");
                return httpResult;
            }
            String wname = XssFilterUtil.dealString(thsAlarmFormDTO.getWfName());
            thsAlarmFormDTO.setWfName(wname);
            String res = warnStrategyService.addEarlyWarn(thsAlarmFormDTO);
            if ("新增成功".equals(res)) {
                httpResult.setCode(200);
                httpResult.setMessage(res);
            } else {
                httpResult.setCode(500);
                httpResult.setMessage(res);
            }
            return httpResult;
        }catch (Exception e){
            httpResult.setCode(500);
            httpResult.setMessage("新增数据失败");
            return httpResult;
        }
    }

    /**
     * @Author chendaofei
     * @Description 删除预警策略
     * @Date 15:27 2020/6/17
     **/
    @GetMapping("delEarlyWarn")
    @ResponseBody
    public HttpResult delEarlyWarn(String wfIndex) {
        HttpResult httpResult = new HttpResult();
        if (PubUtils.isBlank(wfIndex)) {
            httpResult.setCode(500);
            httpResult.setMessage("参数为空");
            return httpResult;
        }
        try {
            String res = warnStrategyService.delEarlyWarn(wfIndex);
            if ("删除成功".equals(res)) {
                httpResult.setCode(200);
                httpResult.setMessage(res);
            } else {
                httpResult.setCode(500);
                httpResult.setMessage(res);
            }
            return httpResult;
        }catch (Exception e){
            httpResult.setCode(500);
            httpResult.setMessage("删除数据失败");
            return httpResult;
        }
    }

    /**
     * @Author chendaofei
     * @Description 判断是否存在绑定事件
     * @Date 15:27 2020/6/17
     **/
    @GetMapping("isExitBindEvent")
    @ResponseBody
    public HttpResult isExitBindEvent(String wfIndex){
        HttpResult httpResult = new HttpResult();
        if (PubUtils.isBlank(wfIndex)) {
            httpResult.setCode(500);
            httpResult.setMessage("参数为空");
            return httpResult;
        }
        int res = warnStrategyService.isExitBindEvent(wfIndex);
        if(res>0){
            httpResult.setCode(200);
            httpResult.setMessage("存在绑定事件");
        }else {
            httpResult.setCode(500);
            httpResult.setMessage("不存在存在绑定事件");
        }
        return httpResult;
    }

    /**
     * @Author chendaofei
     * @Description 修改预警策略
     * @Date 15:27 2020/6/17
     **/
    @PostMapping("updateEarlyWarn")
    @ResponseBody
    public HttpResult updateEarlyWarn(@RequestBody ThsAlarmFormDTO thsAlarmFormDTO) {
        HttpResult httpResult = new HttpResult();
        try {
            if (PubUtils.isBlank(thsAlarmFormDTO.getWfIndex()) || PubUtils.isBlank(thsAlarmFormDTO.getWfName()) || thsAlarmFormDTO.getGrade() == null) {
                httpResult.setCode(500);
                httpResult.setMessage("参数为空");
                return httpResult;
            }
            String wname = XssFilterUtil.dealString(thsAlarmFormDTO.getWfName());
            thsAlarmFormDTO.setWfName(wname);
            String res = warnStrategyService.updateEarlyWarn(thsAlarmFormDTO);
            if ("已存在此策略名".equals(res)) {
                httpResult.setCode(500);
                httpResult.setMessage(res);
            } else if ("修改成功".equals(res)) {
                httpResult.setCode(200);
                httpResult.setMessage(res);
            } else {
                httpResult.setCode(500);
                httpResult.setMessage(res);
            }
            return httpResult;
        }catch (Exception e){
            httpResult.setCode(500);
            httpResult.setMessage("修改失败");
            return httpResult;
        }
    }

    /**
     * @Author chendaofei
     * @Description 控制是否启用策略
     * @Date 15:27 2020/6/17
     **/
    @PostMapping("statusController")
    @ResponseBody
    public HttpResult statusController(String wfIndex, Integer state) {
        HttpResult httpResult = new HttpResult();
        if (PubUtils.isBlank(wfIndex) || state == null) {
            httpResult.setCode(500);
            httpResult.setMessage("参数为空");
            return httpResult;
        }
        Integer res = warnStrategyService.statusController(wfIndex, state);
        if (res == 1) {
            httpResult.setCode(200);
            httpResult.setMessage("转换成功");
        } else {
            httpResult.setCode(500);
            httpResult.setMessage("转换失败");
        }
        return httpResult;
    }

    /**
     * @Author chendaofei
     * @Description 获取预警策略列表
     * @Date 15:27 2020/6/17
     **/
    @PostMapping("getEarlyWarnList")
    @ResponseBody
    public HttpResult getEarlyWarnList(@RequestParam(value = "grade") Integer grade) {
        List<ThsAlarmFormDTO> list = warnStrategyService.getEarlyWarnList(grade);
        return PubUtils.assignmentResult(list, 200, "获取成功");
    }

    /**
     * @Author chendaofei
     * @Description 修改预警策略指标类型与或
     * @Date 15:27 2020/6/17
     **/
    @PostMapping("updateEarlyWarnAndOr")
    @ResponseBody
    public HttpResult updateEarlyWarnAndOr(String wfIndex, Integer operation) {
        HttpResult httpResult = new HttpResult();
        if (PubUtils.isBlank(wfIndex) || operation == null) {
            httpResult.setCode(500);
            httpResult.setMessage("参数不可为空");
            return httpResult;
        }
        Integer res = warnStrategyService.updateEarlyWarnAndOr(wfIndex, operation);
        if (res == 1) {
            httpResult.setCode(200);
            httpResult.setMessage("修改成功");
        } else {
            httpResult.setCode(500);
            httpResult.setMessage("修改失败");
        }
        return httpResult;
    }

    /**
     * @Author chendaofei
     * @Description 通过id获取预警策略
     * @Date 15:27 2020/6/17
     **/
    @GetMapping("getEarlyWarnById")
    @ResponseBody
    public HttpResult getEarlyWarnById(String wfIndex) {
        if (PubUtils.isBlank(wfIndex)) {
            return PubUtils.assignmentResult(null, 500, "参数为空");
        }
        try {
            ThsAlarmFormDTO thsAlarmFormDTO = warnStrategyService.getEarlyWarnById(wfIndex);
            return PubUtils.assignmentResult(thsAlarmFormDTO, 200, "获取成功");
        }catch (Exception e){
            return PubUtils.assignmentResult(null, 500, "获取数据失败");
        }
    }



    /*------------------操作THS_WarnFormAss表--------------------------*/

    /**
     * 获取字典表的指标类型数据
     */
    @PostMapping("targetType")
    @ResponseBody
    public HttpResult targetType(@RequestParam("wfIndex") String wfIndex, @RequestParam("type") Integer type) {
        HttpResult result;
        List<DicStrategyRVO> targetTypes;
        if (PubUtils.isBlank(wfIndex) || type == null) {
            return PubUtils.assignmentResult(null, 500, "参数不可为空");
        }
        try {
            targetTypes = warnStrategyService.getEarlyWarnAss(wfIndex, type);
            if (CollectionUtils.isEmpty(targetTypes)) {
                result = PubUtils.assignmentResult(null, 500, "字典表没有类型数据");
            } else {
                result = PubUtils.assignmentResult(targetTypes, 200, "获取字典表类型成功");
            }
        } catch (Exception e) {
            result = PubUtils.assignmentResult(null, 500, "获取字典表类型失败");
        }
        return result;
    }

    /**
     * @Author chendaofei
     * @Description 新增预警绑定关系
     * @Date 8:48 2020/6/23
     **/
    @PostMapping("addEarlyWarnAss")
    @ResponseBody
    public HttpResult addEarlyWarnAss(@RequestBody ParamDataVO paramDataVO) {
        HttpResult httpResult = new HttpResult();
        try {
            List<ThsAlarmFormAss> list = paramDataVO.getList();
            String wfIndex = paramDataVO.getWfIndex();
            Integer state = paramDataVO.getState();

            if (PubUtils.isBlank(wfIndex)) {
                httpResult.setCode(500);
                httpResult.setMessage("参数不可为空");
                return httpResult;
            }
            String res = warnStrategyService.addEarlyWarnAss(wfIndex, state, list);
            if ("配置成功".equals(res)) {
                httpResult.setCode(200);
                httpResult.setMessage(res);
            } else {
                httpResult.setCode(500);
                httpResult.setMessage(res);
            }
            return httpResult;
        }catch (Exception e){
            httpResult.setCode(500);
            httpResult.setMessage("配置失败,请联系管理员");
            return httpResult;
        }
    }


    /**
     * @Author chendaofei
     * @Description 获取预警策略绑定的监测点
     * @Date 15:27 2020/6/17
     **/
    @PostMapping("getStrategyLine")
    @ResponseBody
    public HttpResult getStrategyLine(String wfIndex, Integer type) {
        if (PubUtils.isBlank(wfIndex) || type == null) {
            return PubUtils.assignmentResult(null, 500, "参数不可为空");
        }
        List<ThsAlarmFormAss> list = new ArrayList<>();
        list = warnStrategyService.getStrategyLine(wfIndex, type);
        return PubUtils.assignmentResult(list, 200, "获取成功");
    }

}
