package com.pqs9300.controller.strategy;

import com.njcn.pojo.advanced.PicDataObj;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.utils.PubUtils;
import com.njcn.utils.XssFilterUtil;
import com.pqs9300.pojo.strategy.*;
import com.pqs9300.service.strategy.AlarmStrategyService;
import org.slf4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.ws.rs.POST;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description 告警告警策略控制器
 * @Author cdf
 * @Date 2020/6/17 10:28
 **/
@Controller
@RequestMapping("alarmstrategy")
public class AlarmStrategyController {
    @Resource
    private AlarmStrategyService alarmStrategyService;

    /**
     * @Author chendaofei
     * @Description 新增告警策略
     * @Date 15:27 2020/6/17
     **/
    @PostMapping("addAlarm")
    @ResponseBody
    public HttpResult addAlarm(@RequestBody ThsAlarmDTO thsAlarmDTO){
        try {
            if (PubUtils.isBlank(thsAlarmDTO.getAlarmName()) || thsAlarmDTO.getGrade() == null) {
                return PubUtils.assignmentResult(null, 500, "参数不能为空");
            }
            String wname = XssFilterUtil.dealString(thsAlarmDTO.getAlarmName());
            thsAlarmDTO.setAlarmName(wname);
            String res = alarmStrategyService.addAlarm(thsAlarmDTO);
            if ("新增成功".equals(res)) {
                return PubUtils.assignmentResult(null, 200, res);
            } else {
                return PubUtils.assignmentResult(null, 500, res);
            }
        }catch (Exception e){
            return PubUtils.assignmentResult(null, 500, "新增出错");
        }
    }

    /**
     * @Author chendaofei
     * @Description 删除告警策略
     * @Date 15:27 2020/6/17
     **/
    @GetMapping("delAlarm")
    @ResponseBody
    public HttpResult delAlarm(String alarmIndex){
        HttpResult httpResult =new HttpResult();
        if(PubUtils.isBlank(alarmIndex)){
            httpResult.setCode(500);
            httpResult.setMessage("参数为空");
            return httpResult;
        }
        try {
            String res = alarmStrategyService.delAlarm(alarmIndex);
            if ("删除成功".equals(res)) {
                httpResult.setCode(200);
                httpResult.setMessage(res);
            } else {
                httpResult.setCode(500);
                httpResult.setMessage(res);
            }
            return httpResult;
        }catch (Exception e){
            return PubUtils.assignmentResult(null, 500, "删除数据出错");
        }
    }

    /**
     * @Author chendaofei
     * @Description 判断是否存在绑定事件
     * @Date 15:27 2020/6/17
     **/
    @GetMapping("isExitBindEvent")
    @ResponseBody
    public HttpResult isExitBindEvent(String alarmIndex){
        HttpResult httpResult = new HttpResult();
        if (PubUtils.isBlank(alarmIndex)) {
            httpResult.setCode(500);
            httpResult.setMessage("参数为空");
            return httpResult;
        }
        int res = alarmStrategyService.isExitBindEvent(alarmIndex);
        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("updateAlarm")
    @ResponseBody
    public HttpResult updateAlarm(@RequestBody ThsAlarmDTO thsAlarmDTO){
        if(PubUtils.isBlank(thsAlarmDTO.getAlarmIndex())|| PubUtils.isBlank(thsAlarmDTO.getAlarmName())|| thsAlarmDTO.getGrade()==null){
            return PubUtils.assignmentResult(null,500,"参数不能为空");
        }
        try {
            String wname = XssFilterUtil.dealString(thsAlarmDTO.getAlarmName());
            thsAlarmDTO.setAlarmName(wname);
            String res = alarmStrategyService.updateAlarm(thsAlarmDTO);
            if ("修改成功".equals(res)) {
                return PubUtils.assignmentResult(null, 200, res);
            } else {
                return PubUtils.assignmentResult(null, 500, res);
            }
        }catch (Exception e){
            return PubUtils.assignmentResult(null, 500, "修改数据失败");
        }
    }

    /**
     * @Author chendaofei
     * @Description 控制是否启用策略
     * @Date 15:27 2020/6/17
     **/
    @PostMapping("statusController")
    @ResponseBody
    public HttpResult statusController(String alarmIndex,Integer state){
        HttpResult httpResult = new HttpResult();
        try {
            Integer res = alarmStrategyService.statusController(alarmIndex, state);
            if (res == 1) {
                httpResult.setCode(200);
                httpResult.setMessage("转换成功");
            } else {
                httpResult.setCode(500);
                httpResult.setMessage("转换失败");
            }
            return httpResult;
        }catch (Exception e){
            httpResult.setCode(500);
            httpResult.setMessage("异常，请联系管理员");
            return httpResult;
        }
    }

    /**
     * @Author chendaofei
     * @Description 获取告警列表
     * @Date 15:27 2020/6/17
     **/
    @PostMapping("getAlarmList")
    @ResponseBody
    public HttpResult updateAlarm(Integer grade){
        List<ThsAlarmDTO> list = alarmStrategyService.getAlarmList(grade);
        return PubUtils.assignmentResult(list,200,"获取成功");
    }

    /**
     * @Author chendaofei
     * @Description 修改告警策略指标类型与或
     * @Date 15:27 2020/6/17
     **/
    @PostMapping("updateAlarmAndOr")
    @ResponseBody
    public HttpResult updateAlarmAndOr(String alarmIndex,Integer operation){
        HttpResult httpResult = new HttpResult();
        if(PubUtils.isBlank(alarmIndex)||operation==null){
            httpResult.setCode(500);
            httpResult.setMessage("参数不可为空");
            return httpResult;
        }else {
            Integer res = alarmStrategyService.updateAlarmAndOr(alarmIndex,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("getAlarmById")
    @ResponseBody
    public HttpResult getAlarmById(String alarmIndex){
        if(PubUtils.isBlank(alarmIndex)){
            return PubUtils.assignmentResult(null,500,"参数不可为空");
        }
        try {
            ThsAlarmDTO thsAlarmDTO = alarmStrategyService.getAlarmById(alarmIndex);
            return PubUtils.assignmentResult(thsAlarmDTO,200,"获取成功");
        }catch (Exception e){
            return PubUtils.assignmentResult(null,500,"获取失败");
        }
    }



    /*------------------操作THS_FormAss表--------------------------*/
    /**
     * 获取字典表的指标类型数据
     */
    @PostMapping("targetType")
    @ResponseBody
    public HttpResult targetType(@RequestParam("alarmIndex") String alarmIndex,@RequestParam("type") Integer type) {
        HttpResult result;
        List<DicStrategyRVO> targetTypes;
        if(PubUtils.isBlank(alarmIndex)|| type ==null){
            return PubUtils.assignmentResult(null, 500, "参数不可为空");
        }
        try {
            targetTypes = alarmStrategyService.getAlarmAss(alarmIndex,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("addAlarmAss")
    @ResponseBody
    public HttpResult addAlarmAss(@RequestBody ParamDataVO paramDataVO){
        HttpResult httpResult = new HttpResult();
        try {
        List<ThsAlarmFormAss> list = paramDataVO.getList();
        String alarmIndex = paramDataVO.getWfIndex();
        Integer state = paramDataVO.getState();
        if (PubUtils.isBlank(alarmIndex)|| state==null) {
            httpResult.setCode(500);
            httpResult.setMessage("参数不可为空");
            return httpResult;
        }
        String res = alarmStrategyService.addAlarmAss(alarmIndex,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 alarmIndex, Integer type){
        if(PubUtils.isBlank(alarmIndex)|| type ==null){
            return PubUtils.assignmentResult(null, 500, "参数不可为空");
        }else {
            List<ThsAlarmAss>  list= new ArrayList<>();
            list = alarmStrategyService.getStrategyLine(alarmIndex,type);
            return PubUtils.assignmentResult(list,200,"获取成功");
        }

    }

}
