package com.pqs9300.serviceimpl.strategy;

import cn.hutool.core.collection.CollectionUtil;
import com.njcn.enums.DicDataTypeEnum;
import com.njcn.pojo.user.Depts;
import com.njcn.shiro.token.TokenManager;
import com.njcn.utils.UserUtil;
import com.pqs9300.mapper.strategy.WarningStrategyMapper;
import com.pqs9300.pojo.strategy.ThsAlarmFormDTO;
import com.pqs9300.pojo.strategy.DicStrategyRVO;
import com.pqs9300.pojo.strategy.ThsAlarmFormAss;
import com.pqs9300.service.strategy.WarnStrategyService;
import org.apache.poi.ss.formula.functions.T;
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.*;
import java.util.stream.Collectors;

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

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

    }

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

    }

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

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

    /**
     * @Author chendaofei
     * @Description 删除预警策略
     * @Date 15:27 2020/6/17
     **/
    @Override
    public String delEarlyWarn(String wfIndex) {
        warningStrategyMapper.delEarlyWarnAss(wfIndex);
        Integer res = warningStrategyMapper.delEarlyWarn(wfIndex);
        if (res == 1) {
            return "删除成功";
        } else {
            return "删除失败";
        }
    }

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

    /**
     * @Author chendaofei
     * @Description 获取预警策略
     * @Date 15:27 2020/6/17
     **/
    @Override
    public List<ThsAlarmFormDTO> getEarlyWarnList(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 warningStrategyMapper.getEarlyWarnList(grade, null,list);
    }

    /**
     * @Author chendaofei
     * @Description 通过id获取预警策略
     * @Date 15:27 2020/6/17
     **/
    @Override
    public ThsAlarmFormDTO getEarlyWarnById(String wfIndex) {
        return warningStrategyMapper.getEarlyWarnById(wfIndex);
    }



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

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

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


    /**
     * @Author chendaofei
     * @Description 获取预警策略列表
     * @Date 15:27 2020/6/17
     **/
    @Override
    public List<ThsAlarmFormAss> getStrategyLine(String wfIndex, Integer type) {
        return warningStrategyMapper.getEarlyWarnAss(wfIndex, type);
    }


    /**
     * @Author chendaofei
     * @Description 获取预警策略关系
     * @Date 15:27 2020/6/17
     **/
    @Override
    public List<DicStrategyRVO> getEarlyWarnAss(String wfIndex, Integer type) {
        String strType = null;
        if (type == 0) {
            strType = DicDataTypeEnum.payloadEnum.getName();
        } else {
            strType = "指标类型";
        }
        List<DicStrategyRVO> list = warningStrategyMapper.getTartgetTypeForDic(strType);
        if (list != null && list.size() != 0) {
            List<ThsAlarmFormAss> asslist = warningStrategyMapper.getEarlyWarnAss(wfIndex, 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).getWfassIndex())) {
                            list.get(i).setIsCheck(1);
                            break;
                        } else {
                            list.get(i).setIsCheck(0);
                        }
                    }
                }
            } else {
                for (DicStrategyRVO dicStrategyRVO : list) {
                    dicStrategyRVO.setIsCheck(0);
                }
            }
        }
        return list;
    }

}
