package com.njcn.harmonic.service.majornetwork.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.njcn.common.pojo.constant.BizParamConstant;
import com.njcn.common.pojo.dto.SimpleDTO;
import com.njcn.common.pojo.param.StatisticsBizBaseParam;
import com.njcn.device.pms.api.MonitorClient;
import com.njcn.device.pms.api.PmsGeneralDeviceInfoClient;
import com.njcn.device.pms.pojo.dto.PmsGeneralDeviceDTO;
import com.njcn.device.pms.pojo.dto.PmsMonitorDTO;
import com.njcn.device.pms.pojo.param.PmsDeviceInfoParam;
import com.njcn.device.pms.pojo.param.PmsMonitorParam;
import com.njcn.harmonic.mapper.majornetwork.RStatOrgMMapper;
import com.njcn.harmonic.mapper.majornetwork.RStatOrgQMapper;
import com.njcn.harmonic.mapper.majornetwork.RStatOrgYMapper;
import com.njcn.harmonic.mapper.majornetwork.RStatSubstationMMapper;
import com.njcn.harmonic.mapper.majornetwork.RStatSubstationQMapper;
import com.njcn.harmonic.mapper.majornetwork.RStatSubstationYMapper;
import com.njcn.harmonic.pojo.param.RStatOrgParam;
import com.njcn.harmonic.pojo.param.StatSubstationBizBaseParam;
import com.njcn.harmonic.pojo.po.RStatOrgY;
import com.njcn.harmonic.pojo.vo.*;
import com.njcn.harmonic.service.majornetwork.RStatOrgYService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.harmonic.utils.DeptUtil;
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.dto.DeptDTO;
import com.njcn.web.utils.WebUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 区域分类统计年表 服务实现类
 * </p>
 *
 * @author rui.wu
 * @since 2022-10-17
 */
@Service
@RequiredArgsConstructor
public class RStatOrgYServiceImpl extends ServiceImpl<RStatOrgYMapper, RStatOrgY> implements RStatOrgYService {

    private final DeptFeignClient deptFeignClient;
    private final DicDataFeignClient dicDataFeignClient;
    private final RStatOrgYMapper rStatOrgYMapper;
    private final RStatOrgQMapper rStatOrgQMapper;
    private final RStatOrgMMapper rStatOrgMMapper;
    private final RStatSubstationMMapper rStatSubstationMMapper;
    private final RStatSubstationQMapper rStatSubstationQMapper;
    private final RStatSubstationYMapper rStatSubstationYMapper;
    private final MonitorClient monitorClient;
    private final PmsGeneralDeviceInfoClient pmsGeneralDeviceInfoClient;

    @Override
    public List<RStatOrgVO> getRStatHarmonicYAll(RStatOrgParam.Info param) {
        //获取部门信息
        List<DeptDTO> deptList = deptFeignClient.getDeptDescendantIndexes(param.getId(), WebUtil.filterDeptType()).getData();
        List<DeptDTO> deptDTOList = DeptUtil.getDeptSubsetVOList(deptList, param.getId());
        List<String> deptIds = deptDTOList.stream().map(DeptDTO::getCode).collect(Collectors.toList());

        Map<String, DeptDTO> deptMap = deptDTOList.stream().collect(Collectors.toMap(DeptDTO::getCode, Function.identity()));

        //获取主网id信息
        DictData mainnetData = dicDataFeignClient.getDicDataByCode(DicDataEnum.MAINNET_POINT.getCode()).getData();

        //数据库查询
        List<RStatOrgY> list = rStatOrgYMapper.selectList(new LambdaQueryWrapper<RStatOrgY>()
                .eq(RStatOrgY::getDataType, mainnetData.getId())
                .in(RStatOrgY::getOrgNo, deptIds)
                .in(CollUtil.isNotEmpty(param.getMeasurementTypeClass()),RStatOrgY::getMeasurementTypeClass, param.getMeasurementTypeClass())
                .ge(StrUtil.isNotBlank(param.getStartTime()), RStatOrgY::getDataDate, param.getStartTime())
                .le(StrUtil.isNotBlank(param.getEndTime()), RStatOrgY::getDataDate, param.getEndTime()));
        Map<String, List<RStatOrgY>> collect = list.stream().collect(Collectors.groupingBy(RStatOrgY::getOrgNo));

        List<RStatOrgVO> newList = new ArrayList<>();
        collect.forEach((key, value) -> {
            if (deptMap.containsKey(key)) {
                for (RStatOrgY rStatOrgY : value) {
                    DeptDTO deptDTO = deptMap.get(key);
                    RStatOrgVO rStatOrgVO = new RStatOrgVO();
                    rStatOrgVO.setOrgName(deptDTO.getName());
                    rStatOrgVO.setAverageOverDay(rStatOrgY.getAverageOverDay());
                    rStatOrgVO.setMeasurementTypeClass(rStatOrgY.getMeasurementTypeClass());
                    rStatOrgVO.setEffectiveMeasurementAverage(rStatOrgY.getEffectiveMeasurementAverage());
                    rStatOrgVO.setEffectiveMeasurementAccrued(rStatOrgY.getEffectiveMeasurementAccrued());
                    rStatOrgVO.setOverLimitMeasurementAverage(rStatOrgY.getOverLimitMeasurementAverage());
                    rStatOrgVO.setOverLimitMeasurementAccrued(rStatOrgY.getOverLimitMeasurementAccrued());
                    DecimalFormat df = new DecimalFormat("###.00");
                    if(rStatOrgY.getOverLimitMeasurementAverage()!=null&&rStatOrgY.getEffectiveMeasurementAverage()!=null&&
                            rStatOrgY.getOverLimitMeasurementAverage()!=0&&rStatOrgY.getEffectiveMeasurementAverage()!=0){
                        Float harmonicMeasurementRatioAverage = Float.parseFloat(
                                df.format(rStatOrgY.getOverLimitMeasurementAverage()*100.0 / rStatOrgY.getEffectiveMeasurementAverage() ));
                        rStatOrgVO.setHarmonicMeasurementRatioAverage(harmonicMeasurementRatioAverage);
                    }else{
                        rStatOrgVO.setHarmonicMeasurementRatioAverage(0.0f);
                    }
                    if(rStatOrgY.getOverLimitMeasurementAccrued()!=null&&rStatOrgY.getEffectiveMeasurementAccrued()!=null&&
                            rStatOrgY.getOverLimitMeasurementAccrued()!=0&&rStatOrgY.getEffectiveMeasurementAccrued()!=0){
                        Float harmonicMeasurementRatioAccrued = Float.parseFloat(
                                df.format(rStatOrgY.getOverLimitMeasurementAccrued()*100.0 / rStatOrgY.getEffectiveMeasurementAccrued()));
                        rStatOrgVO.setHarmonicMeasurementRatioAccrued(harmonicMeasurementRatioAccrued);
                    }else{
                        rStatOrgVO.setHarmonicMeasurementRatioAccrued(0.0f);
                    }
                    newList.add(rStatOrgVO);
                }
            }
        });
        return newList;
    }

    @Override
    public List<RStatSubstationOrg2VO.MonitorType> getRStatSubstationOrg(StatSubstationBizBaseParam param) {
        // 获取当前用户的部门的子部门信息
        List<DeptDTO> deptList = deptFeignClient.getDeptDescendantIndexes(param.getId(), WebUtil.filterDeptType()).getData();
        List<DeptDTO> data = DeptUtil.getDeptSubsetVOList(deptList, param.getId());
        if(CollUtil.isEmpty(param.getIds())){
            //监测点对象标签
            List<DictData> dataList = dicDataFeignClient.getDicDataByTypeCode(
                    DicDataTypeEnum.MONITORING_LABELS.getCode()).getData();
            param.setIds(dataList.stream().map(DictData::getId).collect(Collectors.toList()));
        }
        List<RStatSubstationOrg2VO.MonitorType> info = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(data)) {
            // 根据暂态指标枚举查询暂态指标
            List<DictData> eventStatis = dicDataFeignClient.getDicDataByTypeCode(
                    DicDataTypeEnum.STEADY_STATIS.getCode()).getData();
            List<RStatSubstationOrgVO> temp = new ArrayList<>();
            List<RStatSubstationOrgVO> tb;
            List<RStatSubstationOrgVO> hb;
            // 过滤出部门id
            List<String> deptIds = data.stream().map(DeptDTO::getCode).collect(Collectors.toList());

            //获取主网id信息
            DictData mainnetData = dicDataFeignClient.getDicDataByCode(DicDataEnum.MAINNET_POINT.getCode()).getData();
            String startTime = param.getStartTime();
            String endTime = param.getEndTime();
            //同比（上一年）
            String tStarTime = DateUtil.offset(DateUtil.parse(startTime), DateField.YEAR, -1).toString();
            String tEndTime = DateUtil.offset(DateUtil.parse(endTime), DateField.YEAR, -1).toString();
            //环比（上个月）
            String hStarTime = DateUtil.offset(DateUtil.parse(startTime), DateField.MONTH, -1).toString();
            String hEndTime = DateUtil.offset(DateUtil.parse(endTime), DateField.MONTH, -1).toString();
            param.getStartTime();
            // 类型（1年 2季度 3月份 4日
            switch (param.getType().toString()) {
                case BizParamConstant.STAT_BIZ_YEAR:
                    // 获取年区域暂态指标分类统计表
                    temp = rStatOrgYMapper.getYearRStatHarmonicOrgInfo(param, deptIds, mainnetData.getId());
                    param.setStartTime(tStarTime);
                    param.setEndTime(tEndTime);
                     tb = rStatOrgYMapper.getYearRStatHarmonicOrgInfo(param, deptIds, mainnetData.getId());
                    param.setStartTime(hStarTime);
                    param.setEndTime(hEndTime);
                     hb = rStatOrgYMapper.getYearRStatHarmonicOrgInfo(param, deptIds, mainnetData.getId());
                    assemble(temp,tb,hb);
                    break;
                case BizParamConstant.STAT_BIZ_QUARTER:
                    // 获取季区域暂态指标分类统计表
                    temp = rStatOrgQMapper.getQuarterRStatHarmonicOrgInfo(param, deptIds, mainnetData.getId());
                    param.setStartTime(tStarTime);
                    param.setEndTime(tEndTime);
                    tb = rStatOrgQMapper.getQuarterRStatHarmonicOrgInfo(param, deptIds, mainnetData.getId());
                    param.setStartTime(hStarTime);
                    param.setEndTime(hEndTime);
                    hb = rStatOrgQMapper.getQuarterRStatHarmonicOrgInfo(param, deptIds, mainnetData.getId());
                    assemble(temp,tb,hb);
                    break;
                case BizParamConstant.STAT_BIZ_MONTH:
                    // 获取月区域暂态指标分类统计表
                    temp = rStatOrgMMapper.getMonthRStatHarmonicOrgInfo(param, deptIds, mainnetData.getId());
                    param.setStartTime(tStarTime);
                    param.setEndTime(tEndTime);
                    tb = rStatOrgMMapper.getMonthRStatHarmonicOrgInfo(param, deptIds, mainnetData.getId());
                    param.setStartTime(hStarTime);
                    param.setEndTime(hEndTime);
                    hb = rStatOrgMMapper.getMonthRStatHarmonicOrgInfo(param, deptIds, mainnetData.getId());
                    assemble(temp,tb,hb);
                    break;
                default:
                    break;
            }
            if (CollUtil.isNotEmpty(temp)) {
                Map<String, List<RStatSubstationOrgVO>> map = temp
                        .stream().collect(Collectors.groupingBy(x->x.getOrgNo()+"_"+x.getMeasurementTypeClass()));
                map.forEach((key, value) -> {
                    String[] split = key.split("_");
                    RStatSubstationOrg2VO.MonitorType vo = new RStatSubstationOrg2VO.MonitorType();
                    vo.setOrgNo(split[0]);
                    vo.setDataDate(value.get(0).getDataDate());
                    vo.setMonitorType(split[1]);
                    vo.setEffectiveMeasurementAverage(value.get(0).getEffectiveMeasurementAverage());
                    vo.setEffectiveMeasurementAccrued(value.get(0).getEffectiveMeasurementAccrued());
                    vo.setHarmonicMeasurementAverage(value.get(0).getHarmonicMeasurementAverage());
                    vo.setHarmonicMeasurementAccrued(value.get(0).getHarmonicMeasurementAccrued());
                    vo.setHarmonicMeasurementRatioAverage(value.get(0).getHarmonicMeasurementRatioAverage());
                    vo.setHarmonicMeasurementRatioAccrued(value.get(0).getHarmonicMeasurementRatioAccrued());
                    //遍历赋值
                    for (RStatSubstationOrgVO orgVO : value) {
                        for (DictData eventStati : eventStatis) {
                            if (eventStati.getId().equals(orgVO.getHarmonicType())) {
                                if (eventStati.getCode().equals(RStatSubstationOrg2VO.FREQUENCY_DEV)) {
                                    vo.setTAverage(orgVO.getOverLimitMeasurementAverage());
                                    vo.setTAccrued(orgVO.getOverLimitMeasurementAccrued());
                                    vo.setOAverage(orgVO.getOverLimitMeasurementRatioAverage());
                                    vo.setOAccrued(orgVO.getOverLimitMeasurementRatioAccrued());
                                    vo.setChainAverage(orgVO.getChainAverage());
                                    vo.setChainAccrued(orgVO.getChainAccrued());
                                    vo.setSameAverage(orgVO.getSameAverage());
                                    vo.setSameAccrued(orgVO.getSameAccrued());
                                    vo.setAverageOverDay(orgVO.getAverageOverDay());
                                }
                                if (eventStati.getCode().equals(RStatSubstationOrg2VO.VOLTAGE_DEV)) {
                                    vo.setTAverage1(orgVO.getOverLimitMeasurementAverage());
                                    vo.setTAccrued1(orgVO.getOverLimitMeasurementAccrued());
                                    vo.setOAverage1(orgVO.getOverLimitMeasurementRatioAverage());
                                    vo.setOAccrued1(orgVO.getOverLimitMeasurementRatioAccrued());
                                    vo.setChainAverage1(orgVO.getChainAverage());
                                    vo.setChainAccrued1(orgVO.getChainAccrued());
                                    vo.setSameAverage1(orgVO.getSameAverage());
                                    vo.setSameAccrued1(orgVO.getSameAccrued());
                                    vo.setAverageOverDay1(orgVO.getAverageOverDay());
                                }
                                if (eventStati.getCode().equals(RStatSubstationOrg2VO.FLICKER)) {
                                    vo.setTAverage2(orgVO.getOverLimitMeasurementAverage());
                                    vo.setTAccrued2(orgVO.getOverLimitMeasurementAccrued());
                                    vo.setOAverage2(orgVO.getOverLimitMeasurementRatioAverage());
                                    vo.setOAccrued2(orgVO.getOverLimitMeasurementRatioAccrued());
                                    vo.setChainAverage2(orgVO.getChainAverage());
                                    vo.setChainAccrued2(orgVO.getChainAccrued());
                                    vo.setSameAverage2(orgVO.getSameAverage());
                                    vo.setSameAccrued2(orgVO.getSameAccrued());
                                    vo.setAverageOverDay2(orgVO.getAverageOverDay());
                                }
                                if (eventStati.getCode().equals(RStatSubstationOrg2VO.HARMONIC_VOLTAGE)) {
                                    vo.setTAverage3(orgVO.getOverLimitMeasurementAverage());
                                    vo.setTAccrued3(orgVO.getOverLimitMeasurementAccrued());
                                    vo.setOAverage3(orgVO.getOverLimitMeasurementRatioAverage());
                                    vo.setOAccrued3(orgVO.getOverLimitMeasurementRatioAccrued());
                                    vo.setChainAverage3(orgVO.getChainAverage());
                                    vo.setChainAccrued3(orgVO.getChainAccrued());
                                    vo.setSameAverage3(orgVO.getSameAverage());
                                    vo.setSameAccrued3(orgVO.getSameAccrued());
                                    vo.setAverageOverDay3(orgVO.getAverageOverDay());
                                }
                                if (eventStati.getCode().equals(RStatSubstationOrg2VO.HARMONIC_CURRENT)) {
                                    vo.setTAverage4(orgVO.getOverLimitMeasurementAverage());
                                    vo.setTAccrued4(orgVO.getOverLimitMeasurementAccrued());
                                    vo.setOAverage4(orgVO.getOverLimitMeasurementRatioAverage());
                                    vo.setOAccrued4(orgVO.getOverLimitMeasurementRatioAccrued());
                                    vo.setChainAverage4(orgVO.getChainAverage());
                                    vo.setChainAccrued4(orgVO.getChainAccrued());
                                    vo.setSameAverage4(orgVO.getSameAverage());
                                    vo.setSameAccrued4(orgVO.getSameAccrued());
                                    vo.setAverageOverDay4(orgVO.getAverageOverDay());
                                }
                                if (eventStati.getCode().equals(RStatSubstationOrg2VO.INTERHARMONIC_VOLTAGE)) {
                                    vo.setTAverage5(orgVO.getOverLimitMeasurementAverage());
                                    vo.setTAccrued5(orgVO.getOverLimitMeasurementAccrued());
                                    vo.setOAverage5(orgVO.getOverLimitMeasurementRatioAverage());
                                    vo.setOAccrued5(orgVO.getOverLimitMeasurementRatioAccrued());
                                    vo.setChainAverage5(orgVO.getChainAverage());
                                    vo.setChainAccrued5(orgVO.getChainAccrued());
                                    vo.setSameAverage5(orgVO.getSameAverage());
                                    vo.setSameAccrued5(orgVO.getSameAccrued());
                                    vo.setAverageOverDay5(orgVO.getAverageOverDay());
                                }
                                if (eventStati.getCode().equals(RStatSubstationOrg2VO.PHASE_VOLTAGE)) {
                                    vo.setTAverage6(orgVO.getOverLimitMeasurementAverage());
                                    vo.setTAccrued6(orgVO.getOverLimitMeasurementAccrued());
                                    vo.setOAverage6(orgVO.getOverLimitMeasurementRatioAverage());
                                    vo.setOAccrued6(orgVO.getOverLimitMeasurementRatioAccrued());
                                    vo.setChainAverage6(orgVO.getChainAverage());
                                    vo.setChainAccrued6(orgVO.getChainAccrued());
                                    vo.setSameAverage6(orgVO.getSameAverage());
                                    vo.setSameAccrued6(orgVO.getSameAccrued());
                                    vo.setAverageOverDay6(orgVO.getAverageOverDay());
                                }
                                if (eventStati.getCode().equals(RStatSubstationOrg2VO.NEG_CURRENT)) {
                                    vo.setTAverage7(orgVO.getOverLimitMeasurementAverage());
                                    vo.setTAccrued7(orgVO.getOverLimitMeasurementAccrued());
                                    vo.setOAverage7(orgVO.getOverLimitMeasurementRatioAverage());
                                    vo.setOAccrued7(orgVO.getOverLimitMeasurementRatioAccrued());
                                    vo.setChainAverage7(orgVO.getChainAverage());
                                    vo.setChainAccrued7(orgVO.getChainAccrued());
                                    vo.setSameAverage7(orgVO.getSameAverage());
                                    vo.setSameAccrued7(orgVO.getSameAccrued());
                                    vo.setAverageOverDay7(orgVO.getAverageOverDay());
                                }
                                if (eventStati.getCode().equals(RStatSubstationOrg2VO.TOTAL_INDICATOR)) {
                                    vo.setTAverage8(orgVO.getOverLimitMeasurementAverage());
                                    vo.setTAccrued8(orgVO.getOverLimitMeasurementAccrued());
                                    vo.setOAverage8(orgVO.getOverLimitMeasurementRatioAverage());
                                    vo.setOAccrued8(orgVO.getOverLimitMeasurementRatioAccrued());
                                    vo.setChainAverage8(orgVO.getChainAverage());
                                    vo.setChainAccrued8(orgVO.getChainAccrued());
                                    vo.setSameAverage8(orgVO.getSameAverage());
                                    vo.setSameAccrued8(orgVO.getSameAccrued());
                                    vo.setAverageOverDay8(orgVO.getAverageOverDay());
                                }

                            }

                        }
                    }
                    info.add(vo);
                });
                // 匹配单位名称
                for (DeptDTO dto : data) {
                    for (RStatSubstationOrg2VO vo : info) {
                        if (dto.getCode().equals(vo.getOrgNo())) {
                            vo.setOrgName(dto.getName());
                        }
                    }
                }
            }

        }
        info.sort(Comparator.comparing(x->x.getOrgName()));
        return info;
    }

    @Override
    public List<RSubstationIcon2VO.RSubstationInfoVO> getRStatSubstation(StatSubstationBizBaseParam param) {
        //远程调用监测点信息接口
        PmsDeviceInfoParam pms = new PmsDeviceInfoParam();
        pms.setDeptIndex(param.getId());
        pms.setStatisticalType(new SimpleDTO());
        List<PmsGeneralDeviceDTO> data = pmsGeneralDeviceInfoClient.getPmsDeviceInfoWithInOrg(pms).getData();
        //生成变电站id集合
        List<String> pAddIds = new ArrayList<>();
        data.stream().forEach(e -> pAddIds.addAll(e.getPowerrIdList()));
        List<RSubstationIcon2VO> rSubstationIconVO = new ArrayList<>();
        //根据监测点id获取变电站信息
        PmsMonitorParam dto = new PmsMonitorParam();
        List<String> mAddIds = new ArrayList<>();
        data.stream().forEach(e -> mAddIds.addAll(e.getMonitorIdList()));
        dto.setMonitorIds(mAddIds);
        //变电站id
        dto.setPowerrIds(param.getIds());
        //根据变电站分组
        List<PmsMonitorDTO> pmsMonitorDTOS = monitorClient.getMonitorInfoListByCond(dto).getData();

        //用于临时存储TestValue的getPowerrId(解决去重失效问题)
        List<String> infoIds = new ArrayList<>();
        Map<String, PmsMonitorDTO> pmsMonitorDTOMap = pmsMonitorDTOS.stream().filter(
                v -> {
                    if (infoIds.contains(v.getPowerrId())) {
                        return false;
                    } else {
                        infoIds.add(v.getPowerrId());
                        return true;
                    }
                }).collect(Collectors.toMap(PmsMonitorDTO::getPowerrId, Function.identity()));

        if (CollUtil.isNotEmpty(pmsMonitorDTOS)) {
            //获取最新过滤的变电站id
            List<String> ids = pmsMonitorDTOS.stream().map(PmsMonitorDTO::getPowerrId).distinct().collect(Collectors.toList());
            //根据id进行筛选
            StatSubstationBizBaseParam baseParam = BeanUtil.copyProperties(param, StatSubstationBizBaseParam.class);
            baseParam.setIds(ids);
            String string = param.getType().toString();
            switch (string) {
                //查询变电站稳态指标平均超标天数-年数据
                case BizParamConstant.STAT_BIZ_YEAR:
                    rSubstationIconVO = rStatSubstationYMapper.getStatSubstationIcon2Y(baseParam);
                    break;
                //查询变电站稳态指标平均超标天数-季数据
                case BizParamConstant.STAT_BIZ_QUARTER:
                    rSubstationIconVO = rStatSubstationQMapper.getStatSubstationIcon2Q(baseParam);
                    break;
                //查询变电站稳态指标平均超标天数-月数据
                case BizParamConstant.STAT_BIZ_MONTH:
                    rSubstationIconVO = rStatSubstationMMapper.getStatSubstationIcon2M(baseParam);
                    break;
                default:
                    break;
            }
            List<RSubstationIcon2VO.RSubstationInfoVO> rSubstationInfoVOS = BeanUtil.copyToList(rSubstationIconVO, RSubstationIcon2VO.RSubstationInfoVO.class);
            if (CollectionUtil.isNotEmpty(rSubstationInfoVOS)) {
                Map<String, RSubstationIcon2VO.RSubstationInfoVO> detailVOMap = rSubstationInfoVOS.stream()
                        .collect(Collectors.toMap(RSubstationIcon2VO.RSubstationInfoVO::getSubstationId, Function.identity()));
                detailVOMap.forEach((Key, value) -> {
                    if (pmsMonitorDTOMap.containsKey(Key)) {
                        PmsMonitorDTO dto1 = pmsMonitorDTOMap.get(Key);
                        value.setCityName(dto1.getOrgName());
                        value.setSubstationName(dto1.getPowerrName());
                    }
                });
                rSubstationInfoVOS.sort(Comparator.comparing(x->x.getCityName()+";"+x.getSubstationName()));
            }
            return rSubstationInfoVOS;
        }
        return new ArrayList<>();
    }

    @Override
    public List<RStatOrgIndexVO.RStatOrgIndexInfoVO> getRStatOrgIndex(StatisticsBizBaseParam param) {
        // 获取当前用户的部门的子部门信息
        List<DeptDTO> deptList = deptFeignClient.getDeptDescendantIndexes(param.getId(), WebUtil.filterDeptType()).getData();
        List<DeptDTO> data = DeptUtil.getDeptSubsetVOList(deptList, param.getId());

        List<RStatOrgIndexVO.RStatOrgIndexInfoVO> info = new ArrayList<>();
        //获取主网id信息
        DictData mainnetData = dicDataFeignClient.getDicDataByCode(DicDataEnum.MAINNET_POINT.getCode()).getData();

        if (CollectionUtil.isNotEmpty(data)) {
            // 过滤出部门id
            List<String> deptIds = data.stream().map(DeptDTO::getCode).collect(Collectors.toList());
            StatSubstationBizBaseParam baseParam = BeanUtil.copyProperties(param, StatSubstationBizBaseParam.class);
            baseParam.setIds(deptIds);
            // 类型（1年 2季度 3月份 4日
            switch (param.getType().toString()) {
                case BizParamConstant.STAT_BIZ_YEAR:
                    // 获取年区域稳态指标合格率统计表
                    info = rStatOrgYMapper.getYearRStatOrgIndex(baseParam, mainnetData.getId());
                    break;
                case BizParamConstant.STAT_BIZ_QUARTER:
                    // 获取季区域稳态指标合格率统计表
                    info = rStatOrgQMapper.getQuarterRStatOrgIndex(baseParam, mainnetData.getId());
                    break;
                case BizParamConstant.STAT_BIZ_MONTH:
                    // 获取月区域稳态指标合格率统计表
                    info = rStatOrgMMapper.getMonthRStatOrgIndex(baseParam, mainnetData.getId());
                    break;
                default:
                    break;
            }
        }
        //匹配名称
        for (DeptDTO dto : data) {
            for (RStatOrgIndexVO vo : info) {
                if (dto.getCode().equals(vo.getOrgNo())) {
                    vo.setOrgName(dto.getName());
                    vo.setSort(dto.getSort());
                }
            }
        }
        if(CollUtil.isNotEmpty(info)){
             info.sort(Comparator.comparing(RStatOrgIndexVO::getSort).reversed());
        }
        return info;
    }

    private List<RStatSubstationOrgVO> assemble(List<RStatSubstationOrgVO> temp,List<RStatSubstationOrgVO> hb,List<RStatSubstationOrgVO> tb){
        Map<String, RStatSubstationOrgVO> map = temp.stream().collect(Collectors.toMap(x -> x.getOrgNo() + ";" + x.getMeasurementTypeClass() + ";" + x.getHarmonicType(), Function.identity()));
        Map<String, RStatSubstationOrgVO> tbMap = hb.stream().collect(Collectors.toMap(x -> x.getOrgNo() + ";" + x.getMeasurementTypeClass() + ";" + x.getHarmonicType(), Function.identity()));
        Map<String, RStatSubstationOrgVO> hbMap = tb.stream().collect(Collectors.toMap(x -> x.getOrgNo() + ";" + x.getMeasurementTypeClass() + ";" + x.getHarmonicType(), Function.identity()));
        map.forEach((key,value)->{
            RStatSubstationOrgVO rStatSubstationOrgVO;
            if(tbMap.containsKey(key)){
                rStatSubstationOrgVO = tbMap.get(key);
                value.setChainAverage(rStatSubstationOrgVO.getOverLimitMeasurementAverage());
                value.setChainAccrued(rStatSubstationOrgVO.getOverLimitMeasurementRatioAccrued());
            }
            if(hbMap.containsKey(key)){
                rStatSubstationOrgVO = hbMap.get(key);
                value.setSameAverage(rStatSubstationOrgVO.getOverLimitMeasurementAverage());
                value.setSameAccrued(rStatSubstationOrgVO.getOverLimitMeasurementRatioAccrued());
            }

        });
        return temp;
    }

}
