package com.pqs9300.serviceimpl.strategy;

import cn.hutool.core.collection.CollectionUtil;
import com.njcn.pojo.user.Depts;
import com.njcn.shiro.token.TokenManager;
import com.njcn.utils.UserUtil;
import com.pqs9300.mapper.strategy.AlarmStrategyMapper;
import com.pqs9300.pojo.strategy.*;
import com.pqs9300.service.strategy.AlarmStrategyService;
import com.pqs9300.service.strategy.WarnStrategyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @Description
 * @Author cdf
 * @Date 2020/6/17 15:36
 **/
@Service
public class AlarmStrategyServiceImpl implements AlarmStrategyService {
    private static final Logger logger = LoggerFactory.getLogger(AlarmStrategyServiceImpl.class);
    @Autowired
    private UserUtil userUtil;
    @Resource
    private AlarmStrategyMapper alarmStrategyMapper;

    /**
     * @Author chendaofei
     * @Description 新增告警策略
     * @Date 15:27 2020/6/17
     **/
    @Override
    public String addAlarm(ThsAlarmDTO thsAlarmDTO) {
        int res1 = alarmStrategyMapper.isSameName(thsAlarmDTO);
        if (res1 > 0) {
            return "已存在此策略名";
        } else {
            String strUserGuid = TokenManager.getToken().getUserIndex();
            String index = java.util.UUID.randomUUID().toString();
            thsAlarmDTO.setAlarmIndex(index);
            thsAlarmDTO.setOperation(0);
            thsAlarmDTO.setUserIndex(strUserGuid);
            thsAlarmDTO.setUpdateTime(new Date());
            Integer res = alarmStrategyMapper.addAlarm(thsAlarmDTO);
            if (res == 1) {
                return "新增成功";
            } else {
                return "新增失败";
            }
        }

    }

    /**
     * @Author chendaofei
     * @Description 修改告警策略
     * @Date 15:27 2020/6/17
     **/
    @Override
    public String updateAlarm(ThsAlarmDTO thsAlarmDTO) {
        int res1 = alarmStrategyMapper.isSameName(thsAlarmDTO);
        if (res1 > 0) {
            return "已存在此策略名";
        } else {
            String strUserGuid = TokenManager.getToken().getUserIndex();
            thsAlarmDTO.setUserIndex(strUserGuid);
            thsAlarmDTO.setUpdateTime(new Date());
            Integer res = alarmStrategyMapper.updateAlarm(thsAlarmDTO);
            if (res == 1) {
                return "修改成功";
            } else {
                return "修改失败";
            }
        }

    }

    /**
     * @Author chendaofei
     * @Description 控制是否启用策略
     * @Date 15:27 2020/6/17
     **/
    @Override
    public Integer statusController(String alarmIndex, Integer state) {
        return alarmStrategyMapper.statusController(alarmIndex, state);
    }

    /**
     * @Author chendaofei
     * @Description 修改告警策略指标类型与或
     * @Date 15:27 2020/6/17
     **/
    @Override
    public Integer updateAlarmAndOr(String alarmIndex, Integer operation) {
        return alarmStrategyMapper.updateAlarmAndOr(alarmIndex, operation);
    }

    /**
     * @Author chendaofei
     * @Description 删除告警策略
     * @Date 15:27 2020/6/17
     **/
    @Override
    public String delAlarm(String alarmIndex) {
        alarmStrategyMapper.delAlarmAss(alarmIndex);
        Integer res = alarmStrategyMapper.delAlarm(alarmIndex);
        if (res == 1) {
            return "删除成功";
        } else {
            return "删除失败";
        }
    }

    /**
     * @Author chendaofei
     * @Description 删除预警策略
     * @Date 15:27 2020/6/17
     **/
    @Override
    public Integer isExitBindEvent(String alarmIndex) {
        return alarmStrategyMapper.isExitBindEvent(alarmIndex);
    }

    /**
     * @Author chendaofei
     * @Description 获取告警策略
     * @Date 15:27 2020/6/17
     **/
    @Override
    public List<ThsAlarmDTO> getAlarmList(Integer grade) {
        //获取当前用户部门,根据用户部门遍历出所有子部门
        Depts depts = userUtil.getCurrentDept();
        List<Depts> deptsListDown = userUtil.getDeptsByNode(depts.getDeptsIndex());
        List<Depts> deptsListUp = userUtil.getDeptsByNodeUp(depts.getDeptsIndex());
        //首先查出upUser为空的，当前用户权限部门下的所有事件
        Set<String> deptsIndexs = new HashSet<>();
        deptsIndexs.add(depts.getDeptsIndex());
        for (Depts i : deptsListDown) {
            deptsIndexs.add(i.getDeptsIndex());
        }
        for (Depts i : deptsListUp) {
            deptsIndexs.add(i.getDeptsIndex());
        }
        List<String> list = new ArrayList<>(deptsIndexs);
        return alarmStrategyMapper.getAlarmList(grade, null,list);
    }

    /**
     * @Author chendaofei
     * @Description 通过id获取告警策略
     * @Date 15:27 2020/6/17
     **/
    @Override
    public ThsAlarmDTO getAlarmById(String alarmIndex) {
        return alarmStrategyMapper.getAlarmById(alarmIndex);
    }



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

    /**
     * @Author chendaofei
     * @Description 新增告警策略关系表
     * @Date 15:27 2020/6/17
     **/
    @Override
    public String addAlarmAss(String alarmIndex, Integer state, List<ThsAlarmFormAss> list) {
        Boolean flagLine = false;
        int result = alarmStrategyMapper.updateAlarmAndOr(alarmIndex, state);
        if (result != 1) {
            return "配置出错";
        }
        if (!CollectionUtil.isEmpty(list)) {
            ThsAlarmDTO thsAlarmDTO = alarmStrategyMapper.getAlarmById(alarmIndex);
            Integer grade = thsAlarmDTO.getGrade();
            //list不为空
            for (ThsAlarmFormAss thsAlarmFormAss : list) {
                if (thsAlarmFormAss.getType() == 2) {
                    flagLine = true;
                    break;
                }
            }
            if (flagLine) {
                //如果存在监测点则判断是否已经存在此监测点配置
                int res = alarmStrategyMapper.isExitLine(list, grade, alarmIndex, 2);
                if (res > 0) {
                    return "存在相同监测点配置";
                }
            } else {
                //不存在监测点则判断是否存在负荷指标类型相同的
                List<ThsAlarmDTO> all = alarmStrategyMapper.getAlarmList(grade, alarmIndex,null);
                for (ThsAlarmDTO warnItem : all) {
                    int same = 0;
                    String wfIndex2 = warnItem.getAlarmIndex();
                    List<ThsAlarmAss> assList = alarmStrategyMapper.getAlarmAss(wfIndex2, 2);
                    if (assList.size() == 0) {
                        //没有监测点时判断是否类型重复
                        List<ThsAlarmAss> assList0 = alarmStrategyMapper.getAlarmAss(wfIndex2, null);

                        if (!CollectionUtil.isEmpty(assList0)) {
                            for (ThsAlarmAss tf : assList0) {
                                for (ThsAlarmFormAss tf1 : list) {
                                    if (tf1.getWfassIndex().equals(tf.getAlarmassIndex()) && tf1.getType().equals(tf.getType())) {
                                        same++;
                                        break;
                                    }
                                }
                            }
                        }
                        if (same == assList0.size() && same != 0 && same == list.size()) {
                            return "已存在相同策略";
                        }
                    }
                }
            }
            alarmStrategyMapper.delAlarmAss(alarmIndex);
            alarmStrategyMapper.addAlarmAss(list);
        } else {
            alarmStrategyMapper.delAlarmAss(alarmIndex);
        }
        return "配置成功";
    }

    /**
     * @Author chendaofei
     * @Description 获取告警策略列表
     * @Date 15:27 2020/6/17
     **/
    @Override
    public List<ThsAlarmAss> getStrategyLine(String alarmIndex, Integer type) {
        return alarmStrategyMapper.getAlarmAss(alarmIndex, type);
    }

    /**
     * @Author chendaofei
     * @Description 获取告警策略关系
     * @Date 15:27 2020/6/17
     **/
    @Override
    public List<DicStrategyRVO> getAlarmAss(String alarmIndex, Integer type) {
        String strType = null;
        if (type == 0) {
            strType = "干扰源类型";
        } else {
            strType = "指标类型";
        }
        List<DicStrategyRVO> list = alarmStrategyMapper.getTartgetTypeForDic(strType);
        if (list != null && list.size() != 0) {
            List<ThsAlarmAss> asslist = alarmStrategyMapper.getAlarmAss(alarmIndex, type);
            if (asslist != null && asslist.size() != 0) {
                for (int i = 0; i < list.size(); i++) {
                    for (int j = 0; j < asslist.size(); j++) {
                        if (list.get(i).getDicIndex().equals(asslist.get(j).getAlarmassIndex())) {
                            list.get(i).setIsCheck(1);
                            break;
                        } else {
                            list.get(i).setIsCheck(0);
                        }
                    }

                }
            } else {
                for (DicStrategyRVO dicStrategyRVO : list) {
                    dicStrategyRVO.setIsCheck(0);
                }
            }
        }
        return list;
    }

}
