package com.pqs9900.serviceImpl.terminal;

import cn.hutool.core.collection.CollectionUtil;
import com.njcn.mapper.configuration.DicDataMapper;
import com.njcn.pojo.configuration.DicData;
import com.njcn.shiro.token.TokenManager;
import com.pqs9900.mapper.terminal.FlowStrategyMapper;
import com.pqs9900.pojo.terminal.FlowStrategyBindDTO;
import com.pqs9900.pojo.terminal.FlowStrategyVO;
import com.pqs9900.service.terminal.FlowStrategyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Description 流量策略管理
 * @Author cdf
 * @Date 2020/8/10 15:01
 **/
@Service
public class FlowStrategyServiceImpl implements FlowStrategyService {

    @Autowired
    private FlowStrategyMapper flowStrategyMapper;

    @Autowired
    private DicDataMapper dicDataMapper;






    @Override
    public List<DicData> getTartgetTypeForDic(){
        String str = "装置功能";
        return dicDataMapper.getTartgetTypeForDic(str);
    }

    @Override
    @Transactional
    public String addStrategy(FlowStrategyVO flowStrategyVO) throws Exception {
      Integer typeof =  flowStrategyVO.getType();
      if(typeof == 0){
          //判断是否有同名策略
          List<FlowStrategyVO> islist = flowStrategyMapper.getStrategyList(null,0,null);
          if(!CollectionUtil.isEmpty(islist)){
              return "默认类型策略唯一";
          }
      }
            //判断是否存在重名
            List<FlowStrategyVO> namelist = flowStrategyMapper.getStrategyList(flowStrategyVO.getName(),null,null);
            if(!CollectionUtil.isEmpty(namelist)){
                return "已存在同名策略";
            }else {
                List<FlowStrategyBindDTO> comm = new ArrayList<>();
                String fsIndex = UUID.randomUUID().toString();
                String comIndex = UUID.randomUUID().toString();
                String warnIndex = UUID.randomUUID().toString();
                String alertIndex = UUID.randomUUID().toString();
                flowStrategyVO.setFsIndex(fsIndex);
                flowStrategyVO.setComPerIndex(comIndex);
                flowStrategyVO.setWarnPerIndex(warnIndex);
                flowStrategyVO.setAlertPerIndex(alertIndex);
                flowStrategyVO.setUpdateTime(new Date());
                flowStrategyVO.setUpdateUser(TokenManager.getUserId());
                flowStrategyMapper.addStrategy(flowStrategyVO);
                Integer com = flowStrategyVO.getComPer();
                Integer warn = flowStrategyVO.getWarnPer();
                Integer alert = flowStrategyVO.getAlertPer();

                flowStrategyMapper.addFsdic(comIndex,com,0);
                flowStrategyMapper.addFsdic(warnIndex,warn,1);
                flowStrategyMapper.addFsdic(alertIndex,alert,2);

                List<String> comlist = flowStrategyVO.getComList();
                if(!CollectionUtil.isEmpty(comlist)) {
                    for (String str : comlist) {
                        FlowStrategyBindDTO flowStrategyBindDTO = new FlowStrategyBindDTO();
                        flowStrategyBindDTO.setFsdIndex(comIndex);
                        flowStrategyBindDTO.setFucIndex(str);
                        comm.add(flowStrategyBindDTO);
                    }
                }
                List<String> warnlist = flowStrategyVO.getWarnList();
                if(!CollectionUtil.isEmpty(warnlist)) {
                    for (String str1 : warnlist) {
                        FlowStrategyBindDTO flowStrategyBindDTO1 = new FlowStrategyBindDTO();
                        flowStrategyBindDTO1.setFsdIndex(warnIndex);
                        flowStrategyBindDTO1.setFucIndex(str1);
                        comm.add(flowStrategyBindDTO1);
                    }
                }
                List<String> alertlist = flowStrategyVO.getAlertList();
                if(!CollectionUtil.isEmpty(alertlist)) {
                    for (String str2 : alertlist) {
                        FlowStrategyBindDTO flowStrategyBindDTO2 = new FlowStrategyBindDTO();
                        flowStrategyBindDTO2.setFsdIndex(alertIndex);
                        flowStrategyBindDTO2.setFucIndex(str2);
                        comm.add(flowStrategyBindDTO2);
                    }
                }
                if(!CollectionUtil.isEmpty(comm)){
                    flowStrategyMapper.addFun(comm);
                }
                return"新增成功";
            }
    }

    @Override
    @Transactional
    public String updateStrategy(FlowStrategyVO flowStrategyVO)throws Exception {
        Integer typeof = flowStrategyVO.getType();
        if(typeof == 0){
            //判断是否有同名策略
            List<FlowStrategyVO> uplist = flowStrategyMapper.getStrategyList(null,typeof,flowStrategyVO.getFsIndex());
            if(!CollectionUtil.isEmpty(uplist)){
                return "只能有一条通用策略";
            }
        }
            //判断是否存在重名
            List<FlowStrategyVO> namelist = flowStrategyMapper.getStrategyList(flowStrategyVO.getName(),null,flowStrategyVO.getFsIndex());
            if(!CollectionUtil.isEmpty(namelist)){
                return "已存在同名策略";
            }else {

                String fsIndex = flowStrategyVO.getFsIndex();
                String comIndex = flowStrategyVO.getComPerIndex();
                String warnIndex = flowStrategyVO.getWarnPerIndex();
                String alertIndex = flowStrategyVO.getAlertPerIndex();
                flowStrategyVO.setFsIndex(fsIndex);
                flowStrategyVO.setUpdateTime(new Date());
                flowStrategyVO.setUpdateUser(TokenManager.getUserId());
                flowStrategyMapper.updateStrategy(flowStrategyVO);

                flowStrategyMapper.updateFsdic(comIndex,flowStrategyVO.getComPer(),0);
                flowStrategyMapper.updateFsdic(warnIndex,flowStrategyVO.getWarnPer(),1);
                flowStrategyMapper.updateFsdic(alertIndex,flowStrategyVO.getAlertPer(),2);

                flowStrategyMapper.delFun(comIndex);
                flowStrategyMapper.delFun(warnIndex);
                flowStrategyMapper.delFun(alertIndex);


                return"操作成功";
            }
    }

    @Override
    public String addFunTran(FlowStrategyVO flowStrategyVO){
        String comIndex = flowStrategyVO.getComPerIndex();
        String warnIndex = flowStrategyVO.getWarnPerIndex();
        String alertIndex = flowStrategyVO.getAlertPerIndex();
        List<FlowStrategyBindDTO> comm = new ArrayList<>();
        List<String> comlist = flowStrategyVO.getComList();
        if(!CollectionUtil.isEmpty(comlist)) {
            for (String str : comlist) {
                FlowStrategyBindDTO flowStrategyBindDTO = new FlowStrategyBindDTO();
                flowStrategyBindDTO.setFsdIndex(comIndex);
                flowStrategyBindDTO.setFucIndex(str);
                comm.add(flowStrategyBindDTO);
            }
        }
        List<String> warnlist = flowStrategyVO.getWarnList();
        if(!CollectionUtil.isEmpty(warnlist)) {
            for (String str1 : warnlist) {
                FlowStrategyBindDTO flowStrategyBindDTO1 = new FlowStrategyBindDTO();
                flowStrategyBindDTO1.setFsdIndex(warnIndex);
                flowStrategyBindDTO1.setFucIndex(str1);
                comm.add(flowStrategyBindDTO1);
            }
        }
        List<String> alertlist = flowStrategyVO.getAlertList();
        if(!CollectionUtil.isEmpty(alertlist)) {
            for (String str2 : alertlist) {
                FlowStrategyBindDTO flowStrategyBindDTO2 = new FlowStrategyBindDTO();
                flowStrategyBindDTO2.setFsdIndex(alertIndex);
                flowStrategyBindDTO2.setFucIndex(str2);
                comm.add(flowStrategyBindDTO2);
            }
        }
        if(!CollectionUtil.isEmpty(comm)){
            flowStrategyMapper.addFun(comm);
        }else {
            return "操作异常";
        }
        return "操作成功";
    }


    @Override
    public Integer delStrategy(String fsIndex) {
        return flowStrategyMapper.delStrategy(fsIndex);
    }

    @Override
    public List<FlowStrategyVO> getStrategyList() {
        List<FlowStrategyVO> list = flowStrategyMapper.getStrategyList(null,null,null);
        return list;
    }

    @Override
    public FlowStrategyVO getStrategy(String fsIndex) {
        FlowStrategyVO flowStrategyVO = null;
        flowStrategyVO = flowStrategyMapper.getStrategy(fsIndex);
        if(flowStrategyVO!=null){
           List<String> com = flowStrategyMapper.getFun(flowStrategyVO.getComPerIndex());
           List<String> warn = flowStrategyMapper.getFun(flowStrategyVO.getWarnPerIndex());
           List<String> alert = flowStrategyMapper.getFun(flowStrategyVO.getAlertPerIndex());
           if(!CollectionUtil.isEmpty(com)&&!CollectionUtil.isEmpty(warn)&&!CollectionUtil.isEmpty(alert)){
               flowStrategyVO.setComList(com);
               flowStrategyVO.setWarnList(warn);
               flowStrategyVO.setAlertList(alert);
           }
        }
        return flowStrategyVO;
    }

    /**
     * @Author chendaofei
     * @Description
     * @Date 16:57 2020/8/20
     **/
    @Override
    public List<String> getFun(String fsdIndex){
        return flowStrategyMapper.getFun(fsdIndex);
    }



}
