package com.njcn.device.pms.service.majornetwork.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.njcn.device.pms.mapper.majornetwork.SpecialAnalysisMapper;
import com.njcn.device.pms.pojo.param.SpecialAnalysisParam;
import com.njcn.device.pms.pojo.vo.SpecialAnalysisMonitorVO;
import com.njcn.device.pms.service.majornetwork.SpecialAnalysisService;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.system.enums.DicDataTypeEnum;
import com.njcn.system.pojo.po.DictData;
import com.njcn.user.api.DeptFeignClient;
import com.njcn.user.pojo.po.Dept;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <专项分析>
 *
 * @author wr
 * @createTime: 2022-12-01
 */
@Service
@RequiredArgsConstructor
public class SpecialAnalysisServiceImpl implements SpecialAnalysisService {

    private final DeptFeignClient deptFeignClient;
    private final SpecialAnalysisMapper specialAnalysisMapper;
    private final DicDataFeignClient dicDataFeignClient;
    private static final String XIONG_AN_CITY="雄安新区";

    @Override
    public SpecialAnalysisMonitorVO getMonitorList(SpecialAnalysisParam param) {
        //初始化对象
        SpecialAnalysisMonitorVO vo = new SpecialAnalysisMonitorVO();

        //获取电压等级的字典
        List<DictData> voltageLevelList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.DEV_VOLTAGE.getCode()).getData();
        //将电压信息转成map，key：id  value：name
        Map<String, String> voltageLevelMap = voltageLevelList.stream().collect(Collectors.toMap(DictData::getId, DictData::getName));
        DictData dictData = dicDataFeignClient.getDicDataByNameAndTypeName(DicDataTypeEnum.LINE_STATE.getName(), DicDataEnum.RUN.getName()).getData();
        //专项分析-只是统计当前所在部门及下部门直接挂钩的监测点
        List<Dept> deptList = deptFeignClient.getSpecialDeptList().getData();
        List<String> deptIds = deptList.stream().map(Dept::getCode).collect(Collectors.toList());

        DictData data;
        switch (param.getType()) {
            //光伏电站
            case 1:
                data = dicDataFeignClient.getDicDataByCodeAndType(DicDataEnum.POWER_STATION.getCode(),DicDataTypeEnum.MONITORING_LABELS.getCode()).getData();
                break;
            //冶炼负荷
            case 2:
                data = dicDataFeignClient.getDicDataByCode(DicDataEnum.SMELT_LOAD.getCode()).getData();
                break;
            //风电场
            default:
                data = dicDataFeignClient.getDicDataByCode(DicDataEnum.WIND_FARM.getCode()).getData();
                break;
        }
        //1.获取原始数据(参数1，部门id。参数2，监测点标签字典id集合)
        List<SpecialAnalysisMonitorVO.SpecialAnalysisVO> infoList = specialAnalysisMapper.selectList(deptIds, data.getId(),dictData.getId());

        //获取部门区域
        if (CollectionUtil.isNotEmpty(infoList)) {
            infoList.stream().forEach(info -> {
                if (voltageLevelMap.containsKey(info.getVoltageLevel())) {
                    String name = voltageLevelMap.get(info.getVoltageLevel());
                    info.setVoltageLevel(name);
                }
                String replace = info.getOrgName().replace("供电公司本部","");
                if(XIONG_AN_CITY.equals(replace)){
                    info.setOrgName(XIONG_AN_CITY);
                }else{
                    info.setOrgName(replace+"市");
                }

            });
            Map<String, List<SpecialAnalysisMonitorVO.SpecialAnalysisVO>> listMap =
                    infoList.stream().collect(Collectors.groupingBy(r->
                                r.getOrgName()
                            ));

            vo.setSpecialAnalysisVOList(listMap);

            //2.根据电压等级分组显示数据
            Map<String, Long> map = infoList.stream().collect(Collectors.groupingBy(su -> su.getVoltageLevel(), Collectors.counting()));
            List<Map<String, String>> maps = new ArrayList<>();
            Map<String, String> newMap;
            for (String key : map.keySet()) {
                newMap = new HashMap<>();
                newMap.put("name", key);
                newMap.put("value", map.get(key).toString());
                maps.add(newMap);
            }
            vo.setVoltageLevelMap(maps);
        } else {
            vo.setSpecialAnalysisVOList(new HashMap<>());
            vo.setVoltageLevelMap(new ArrayList<>());
        }
        return vo;
    }

    @Override
    public SpecialAnalysisMonitorVO.CompareDetailVO getYoYList(SpecialAnalysisParam.CompareDateParam param) {
        //获取部门信息
        List<Dept> deptList = deptFeignClient.getSpecialDeptList().getData();
        List<String> deptIds = deptList.stream().map(Dept::getCode).collect(Collectors.toList());
        return specialAnalysisMapper.selectYoYList(deptIds, param);
    }

    @Override
    public Map<String, List<SpecialAnalysisMonitorVO.SpecialAnalysisRailwayVO>> getDisplayRailway() {
        // 获取部门信息
        List<Dept> deptList = deptFeignClient.getSpecialDeptList().getData();
        List<String> deptIds = deptList.stream().map(Dept::getCode).collect(Collectors.toList());

        //获取电压等级的字典
        List<DictData> voltageLevelList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.DEV_VOLTAGE.getCode()).getData();
        //将电压信息转成map，key：id  value：name
        Map<String, String> voltageLevelMap = voltageLevelList.stream().collect(Collectors.toMap(DictData::getId, DictData::getName));

        List<SpecialAnalysisMonitorVO.SpecialAnalysisRailwayVO> specialAnalysisRailwayVOS = specialAnalysisMapper.selectRailwayList(deptIds);
        if (CollUtil.isNotEmpty(specialAnalysisRailwayVOS)) {
            specialAnalysisRailwayVOS.stream().forEach(info -> {
                if (voltageLevelMap.containsKey(info.getVoltageLevel())) {
                    String name = voltageLevelMap.get(info.getVoltageLevel());
                    info.setVoltageLevel(name);
                }
            });
            return specialAnalysisRailwayVOS.stream().collect(
                    Collectors.groupingBy(SpecialAnalysisMonitorVO.SpecialAnalysisRailwayVO::getRailwayLineName));
        }
        return new HashMap<>();
    }

    @Override
    public List<SpecialAnalysisMonitorVO.CompareVO> getMapDisplayRailway(SpecialAnalysisParam.CompareParam param) {
        // 获取部门信息
        List<Dept> deptList = deptFeignClient.getSpecialDeptList().getData();
        List<String> deptIds = deptList.stream().map(Dept::getCode).collect(Collectors.toList());
        return specialAnalysisMapper.selectMapDisplayRailway(deptIds, param);
    }

    @Override
    public List<SpecialAnalysisMonitorVO.WiringTypeDetail> getWiringTypeRailway(SpecialAnalysisParam.CompareDateParam param) {
        // 获取部门信息
        List<Dept> deptList = deptFeignClient.getSpecialDeptList().getData();
        List<String> deptIds = deptList.stream().map(Dept::getCode).collect(Collectors.toList());
        //子节点获取监测点类别(监测标签)
        List<DictData> lineData = dicDataFeignClient.getDicDataByTypeCode(
                DicDataTypeEnum.CONNET_GROUP_WAY.getCode()).getData();


        List<SpecialAnalysisMonitorVO.WiringTypeDetail> wiringTypeRailway = specialAnalysisMapper.selectWiringTypeRailway(deptIds, param);
        Map<String, SpecialAnalysisMonitorVO.WiringTypeDetail> detailMap = wiringTypeRailway.stream()
                .collect(Collectors.toMap(SpecialAnalysisMonitorVO.WiringTypeDetail::getConnetGroupWay, Function.identity()));
        //处理主节点不存在的集合
        List<DictData> notMeasurementList = lineData.stream().filter(r -> !detailMap.containsKey(r.getId()))
                .collect(Collectors.toList());
        SpecialAnalysisMonitorVO.WiringTypeDetail wiringTypeDetail;
        for (DictData notData : notMeasurementList) {
            wiringTypeDetail = new SpecialAnalysisMonitorVO.WiringTypeDetail();
            wiringTypeDetail.setConnetGroupWay(notData.getId());
            wiringTypeDetail.setNum(0);
            wiringTypeDetail.setRateAvg(0);
            wiringTypeDetail.setRate95(0);
            wiringTypeDetail.setRate99(0);
            wiringTypeRailway.add(wiringTypeDetail);
        }
        if (CollUtil.isNotEmpty(wiringTypeRailway)) {
            return wiringTypeRailway;

        }
        return new ArrayList<>();
    }


}
