package com.njcn.harmonic.service.distribution.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 com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.constant.BizParamConstant;
import com.njcn.common.pojo.dto.SimpleDTO;
import com.njcn.device.pms.api.DistributionMonitorClient;
import com.njcn.device.pms.api.MonitorClient;
import com.njcn.device.pms.api.PmsGeneralDeviceInfoClient;
import com.njcn.device.pms.api.PwMonitorClient;
import com.njcn.device.pms.pojo.dto.PmsGeneralDeviceDTO;
import com.njcn.device.pms.pojo.dto.PmsMonitorDTO;
import com.njcn.device.pms.pojo.dto.PwPmsMonitorDTO;
import com.njcn.device.pms.pojo.param.PmsDeviceInfoParam;
import com.njcn.device.pms.pojo.param.PmsMonitorParam;
import com.njcn.device.pms.pojo.param.PwPmsMonitorParam;
import com.njcn.harmonic.mapper.distribution.PwRStatOrgMapper;
import com.njcn.harmonic.mapper.majornetwork.*;
import com.njcn.harmonic.pojo.param.RStatOrgParam;
import com.njcn.harmonic.pojo.param.StatSubstationBizBaseParam;
import com.njcn.harmonic.pojo.po.RStatOrgQ;
import com.njcn.harmonic.pojo.vo.*;
import com.njcn.harmonic.service.distribution.PwRStatOrgService;
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.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 配网区域分类统计月表 服务实现类
 * </p>
 *
 * @author rui.wu
 * @since 2022-11-08
 */
@Service
@RequiredArgsConstructor
public class PwRStatOrgServiceImpl extends ServiceImpl<PwRStatOrgMapper, RStatOrgQ> implements PwRStatOrgService {

    private final DicDataFeignClient dicDataFeignClient;
    private final PwRStatOrgMapper pwRStatOrgMapper;
    private final RStatOrgYMapper rStatOrgYMapper;
    private final RStatOrgQMapper rStatOrgQMapper;
    private final RStatOrgMMapper rStatOrgMMapper;
    private final PwMonitorClient pwMonitorClient;
    private final RMpPassRateYMapper rMpPassRateYMapper;
    private final RMpPassRateQMapper rMpPassRateQMapper;
    private final RMpPassRateMMapper rMpPassRateMMapper;
    private final RMpSurplusHarmonicDetailMMapper rMpSurplusHarmonicDetailMMapper;
    private final DeptFeignClient deptFeignClient;
    private final RStatSubstationMMapper rStatSubstationMMapper;
    private final RStatSubstationQMapper rStatSubstationQMapper;
    private final RStatSubstationYMapper rStatSubstationYMapper;
    private final MonitorClient monitorClient;
    private final PmsGeneralDeviceInfoClient pmsGeneralDeviceInfoClient;

    private final DistributionMonitorClient distributionMonitorClient;

    @Override
    public List<RStatOrgVO.PwRStatOrgVO> getRStatHarmonicAll(StatSubstationBizBaseParam param) {
        //获取当前部门下所有部门信息
        List<DeptDTO> deptList = deptFeignClient.getDeptDescendantIndexes(param.getId(), WebUtil.filterDeptType()).getData();
        List<DeptDTO> deptDTOList = DeptUtil.getDeptSubsetVOList(deptList, param.getId());


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

        //获取配网信息
        DictData distributionData = dicDataFeignClient.getDicDataByCode(DicDataEnum.DISTRIBUTION_POINT.getCode()).getData();
        List<RStatOrgVO.PwRStatOrgVO> info = new ArrayList<>();
        List<RStatOrgVO.PwRStatOrgVO> newList = new ArrayList<>();
        switch (String.valueOf(param.getType())) {
            //查询区域超标统计-年数据
            case BizParamConstant.STAT_BIZ_YEAR:
                info = pwRStatOrgMapper.selectListY(param, deptIds, distributionData.getId());
                break;
            //查询区域超标统计-季数据
            case BizParamConstant.STAT_BIZ_QUARTER:
                info = pwRStatOrgMapper.selectListQ(param, deptIds, distributionData.getId());
                break;
            //查询各区域超标统计-月数据
            case BizParamConstant.STAT_BIZ_MONTH:
                info = pwRStatOrgMapper.selectListM(param, deptIds, distributionData.getId());
                break;
            default:
                break;
        }
        if (CollectionUtil.isNotEmpty(info)) {
            Map<String, List<RStatOrgVO.PwRStatOrgVO>> collect = info.stream().collect(
                    Collectors.groupingBy(RStatOrgVO.PwRStatOrgVO::getOrgNo));
            collect.forEach((key, value) -> {
                if (deptMap.containsKey(key)) {
                    for (RStatOrgVO.PwRStatOrgVO org : value) {
                        DeptDTO deptDTO = deptMap.get(key);
                        org.setOrgName(deptDTO.getName());
                        newList.add(org);
                    }
                }
            });
            return newList;
        }
        return Lists.newArrayList();
    }

    @Override
    public List<PwRStatOrgVO> getRStatOrgIndex(StatSubstationBizBaseParam 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());

        //获取配网信息
        DictData distributionData = dicDataFeignClient.getDicDataByCode(DicDataEnum.DISTRIBUTION_POINT.getCode()).getData();
        List<PwRStatOrgVO> info = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(deptDTOList)) {
            // 过滤出部门id
            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.getPwYearRStatOrgIndex(baseParam, distributionData.getId());
                    break;
                case BizParamConstant.STAT_BIZ_QUARTER:
                    // 获取季区域稳态指标合格率统计表
                    info = rStatOrgQMapper.getPwQuarterRStatOrgIndex(baseParam, distributionData.getId());
                    break;
                case BizParamConstant.STAT_BIZ_MONTH:
                    // 获取月区域稳态指标合格率统计表
                    info = rStatOrgMMapper.getPwMonthRStatOrgIndex(baseParam, distributionData.getId());
                    break;
                default:
                    break;
            }
        }
        //匹配名称
        for (DeptDTO dto : deptDTOList) {
            for (PwRStatOrgVO vo : info) {
                if (dto.getCode().equals(vo.getOrgNo())) {
                    vo.setOrgName(dto.getName());
                }
            }
        }
        return info;
    }

    @Override
    public Page<PwRStatOrgVO.PwRStatOrgDetailVO> getRMpPassRateInfo(RStatOrgParam.PwRStatOrgParam param) {
        Page<PwRStatOrgVO.PwRStatOrgDetailVO> pageResult = new Page<>(param.getPageNum(),param.getPageSize());
        //初始化对象
        List<RStatOrgIndexVO> info = new ArrayList<>();
        // 获取当前用户的部门的子部门信息
        PwPmsMonitorParam pwPmsMonitorParam = new PwPmsMonitorParam();
        //部门id
        pwPmsMonitorParam.setOrgId(param.getId());
        //变电站
        pwPmsMonitorParam.setPowerId(param.getPowerrIds());
        //电压等级
        pwPmsMonitorParam.setVoltageLevels(param.getVoltageLevels());
        //监测点类别
        pwPmsMonitorParam.setMonitorSort(param.getMonitorSort());
        //监测点名称
        pwPmsMonitorParam.setMonitorName(param.getMonitorName());
        List<String> monitorIds = pmsGeneralDeviceInfoClient.getPwPmsMonitorIds(pwPmsMonitorParam).getData();

        if (CollectionUtil.isNotEmpty(monitorIds)) {



            //获取最新过滤的监测点id
            StatSubstationBizBaseParam baseParam = BeanUtil.copyProperties(param, StatSubstationBizBaseParam.class);
            baseParam.setIds(monitorIds);
            // 类型（1年 2季度 3月份
            Page<RStatOrgIndexVO> temPage = new Page<>();
            switch (param.getType().toString()) {
                case BizParamConstant.STAT_BIZ_YEAR:
                    // 获取年稳态指标合格率明细表
                    temPage = rMpPassRateYMapper.getPageYearPassRate(new Page<>(param.getPageNum(),param.getPageSize()),baseParam);
                    break;
                case BizParamConstant.STAT_BIZ_QUARTER:
                    // 获取季稳态指标合格率明细表
                    temPage = rMpPassRateQMapper.getPwQuarterPassRate(new Page<>(param.getPageNum(),param.getPageSize()),baseParam);
                    break;
                case BizParamConstant.STAT_BIZ_MONTH:
                    // 获取月稳态指标合格率明细表
                    temPage = rMpPassRateMMapper.getPwMonthPassRate(new Page<>(param.getPageNum(),param.getPageSize()),baseParam);
                    break;
                default:
                    break;

            }
            info = temPage.getRecords();
            if (CollectionUtil.isNotEmpty(info)) {
                List<String> ids = info.stream().map(RStatOrgIndexVO::getMeasurementPointId).distinct().collect(Collectors.toList());
                List<PwPmsMonitorDTO> pwPmsMonitorDTOList = distributionMonitorClient.pwMonitorAreaDetailInfo(ids).getData();
                Map<String, PwPmsMonitorDTO> monitorDTOMap = pwPmsMonitorDTOList.stream().collect(
                        Collectors.toMap(PwPmsMonitorDTO::getMonitorId, Function.identity(), (key1, key2) -> key2));

                //初始化
                List<PwRStatOrgVO.PwRStatOrgDetailVO> temp = new ArrayList<>();
                //便利对象
                Map<String, RStatOrgIndexVO> collect = info.stream()
                        .collect(Collectors.toMap(RStatOrgIndexVO::getMeasurementPointId, Function.identity()));
                collect.forEach((Key, value) -> {
                    if (monitorDTOMap.containsKey(Key)) {
                        //根据监测点信息，进行属性赋值
                        PwPmsMonitorDTO pwPmsMonitorDTO = monitorDTOMap.get(Key);
                        PwRStatOrgVO.PwRStatOrgDetailVO pwRStatOrgVO = new PwRStatOrgVO.PwRStatOrgDetailVO();
                        pwRStatOrgVO.setMeasurementPointId(pwPmsMonitorDTO.getMonitorId());
                        pwRStatOrgVO.setMeasurementPointName(pwPmsMonitorDTO.getMonitorName());
                        pwRStatOrgVO.setMonitorVoltageName(pwPmsMonitorDTO.getVoltageLevel());
                        pwRStatOrgVO.setPowerName(pwPmsMonitorDTO.getPowerName());
                        pwRStatOrgVO.setOrgNo(pwPmsMonitorDTO.getOrgId());
                        pwRStatOrgVO.setOrgName(pwPmsMonitorDTO.getOrgName());
                        pwRStatOrgVO.setMonitorSort(pwPmsMonitorDTO.getMonitorSort());
                        pwRStatOrgVO.setFreqPassRate(value.getFreqPassRate());
                        pwRStatOrgVO.setVDevPassRate(value.getVDevPassRate());
                        pwRStatOrgVO.setFlickerPassRate(value.getFlickerPassRate());
                        temp.add(pwRStatOrgVO);
                    }
                });
                return pageResult.setRecords(temp);
            }
        }
        return pageResult;
    }

    @Override
    public List<RStatSubstationOrg2VO.PwRStatSubstationOrgVO> getRStatSubstationOrg(StatSubstationBizBaseParam param) {
        //获取当前部门下所有部门信息
        List<DeptDTO> deptList = deptFeignClient.getDeptDescendantIndexes(param.getId(), WebUtil.filterDeptType()).getData();
        List<DeptDTO> deptDTOList = 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()));
        }
        Map<String, DeptDTO> deptMap = deptDTOList.stream().collect(Collectors.toMap(DeptDTO::getCode, Function.identity()));
        List<String> deptIds = deptDTOList.stream().map(DeptDTO::getCode).collect(Collectors.toList());
        //初始化
        List<RStatSubstationOrg2VO.PwRStatSubstationOrgVO> info = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(deptDTOList)) {
            // 根据暂态指标查询暂态指标
            List<DictData> steadyStatis = dicDataFeignClient.getDicDataByTypeCode(
                    DicDataTypeEnum.STEADY_STATIS.getCode()).getData();
            List<RStatSubstationOrgVO> temp = new ArrayList<>();
            List<RStatSubstationOrgVO> tb;
            List<RStatSubstationOrgVO> hb;
            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();
            //获取配网信息
            DictData distributionData = dicDataFeignClient.getDicDataByCode(DicDataEnum.DISTRIBUTION_POINT.getCode()).getData();
            //选择数据时间范围
            switch (param.getType().toString()) {
                case BizParamConstant.STAT_BIZ_YEAR:
                    // 获取年区域暂态指标分类统计表
                    temp = rStatOrgYMapper.getYearRStatHarmonicOrgInfo(param, deptIds, distributionData.getId());
                    param.setStartTime(tStarTime);
                    param.setEndTime(tEndTime);
                    tb = rStatOrgYMapper.getYearRStatHarmonicOrgInfo(param, deptIds, distributionData.getId());
                    param.setStartTime(hStarTime);
                    param.setEndTime(hEndTime);
                    hb = rStatOrgYMapper.getYearRStatHarmonicOrgInfo(param, deptIds, distributionData.getId());
                    assemble(temp,tb,hb);
                    break;
                case BizParamConstant.STAT_BIZ_QUARTER:
                    // 获取季区域暂态指标分类统计表
                    temp = rStatOrgQMapper.getQuarterRStatHarmonicOrgInfo(param, deptIds, distributionData.getId());
                    param.setStartTime(tStarTime);
                    param.setEndTime(tEndTime);
                    tb = rStatOrgQMapper.getQuarterRStatHarmonicOrgInfo(param, deptIds, distributionData.getId());
                    param.setStartTime(hStarTime);
                    param.setEndTime(hEndTime);
                    hb = rStatOrgQMapper.getQuarterRStatHarmonicOrgInfo(param, deptIds, distributionData.getId());
                    assemble(temp,tb,hb);
                    break;
                case BizParamConstant.STAT_BIZ_MONTH:
                    // 获取月区域暂态指标分类统计表
                    temp = rStatOrgMMapper.getMonthRStatHarmonicOrgInfo(param, deptIds, distributionData.getId());
                    param.setStartTime(tStarTime);
                    param.setEndTime(tEndTime);
                    tb = rStatOrgMMapper.getMonthRStatHarmonicOrgInfo(param, deptIds, distributionData.getId());
                    param.setStartTime(hStarTime);
                    param.setEndTime(hEndTime);
                    hb = rStatOrgMMapper.getMonthRStatHarmonicOrgInfo(param, deptIds, distributionData.getId());
                    assemble(temp,tb,hb);
                    break;
                default:
                    break;
            }
            Map<String, List<RStatSubstationOrgVO>> map = temp
                    .stream().collect(Collectors.groupingBy(x->x.getOrgNo()+"_"+x.getMeasurementTypeClass()));
            map.forEach((key, value) -> {
                String[] split = key.split("_");
                RStatSubstationOrg2VO.PwRStatSubstationOrgVO vo = new RStatSubstationOrg2VO.PwRStatSubstationOrgVO();
                //获取部门信息
                if (deptMap.containsKey(key)) {
                    DeptDTO deptDTO = deptMap.get(split[0]);
                    vo.setOrgName(deptDTO.getName());
                }
                vo.setOrgNo(value.get(0).getOrgNo());
                vo.setMonitorSort(split[1]);
                vo.setDataDate(value.get(0).getDataDate());
                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 : steadyStatis) {
                        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);
            });
            info.sort(Comparator.comparing(x->x.getOrgName()));
            return info;
        }
        return Lists.newArrayList();
    }

    @Override
    public Page<RMpPartHarmonicDetailVO.PwRMpPartHarmonicDetailDVO> getRMpPartHarmonicDetail(RStatOrgParam.PwRStatOrgParam param) {
        Page<RMpPartHarmonicDetailVO.PwRMpPartHarmonicDetailDVO> pageResult = new Page<>(param.getPageNum(),param.getPageSize());

        PwPmsMonitorParam pwPmsMonitorParam = new PwPmsMonitorParam();
        //部门id
        pwPmsMonitorParam.setOrgId(param.getId());
        //电压等级
        pwPmsMonitorParam.setVoltageLevels(param.getVoltageLevels());
        //监测点类别
        pwPmsMonitorParam.setMonitorSort(param.getMonitorSort());
        //监测点名称
        pwPmsMonitorParam.setMonitorName(param.getMonitorName());
        //获取配网监测点信息
        List<String> monitorIds = pmsGeneralDeviceInfoClient.getPwPmsMonitorIds(pwPmsMonitorParam).getData();

        if (CollectionUtil.isNotEmpty(monitorIds)) {
            //获取监测点id
            //筛选出数据库对应监测点
            StatSubstationBizBaseParam baseParam = BeanUtil.copyProperties(param, StatSubstationBizBaseParam.class);
            baseParam.setIds(monitorIds);
            Page<RMpPartHarmonicDetailVO> rMpPartHarmonicDetail = rMpSurplusHarmonicDetailMMapper.getPwRMpPartHarmonicDetail(new Page<>(param.getPageNum(),param.getPageSize()),baseParam);
            List<RMpPartHarmonicDetailVO> tmpList = rMpPartHarmonicDetail.getRecords();
            if(CollUtil.isEmpty(tmpList)){
                return pageResult;
            }
            List<RMpPartHarmonicDetailVO.PwRMpPartHarmonicDetailDVO> newList = BeanUtil.copyToList(
                    tmpList, RMpPartHarmonicDetailVO.PwRMpPartHarmonicDetailDVO.class);

            List<String> ids = tmpList.stream().map(RMpPartHarmonicDetailVO::getMeasurementPointId).distinct().collect(Collectors.toList());
            List<PwPmsMonitorDTO> pwPmsMonitorDTOList = distributionMonitorClient.pwMonitorAreaDetailInfo(ids).getData();
            Map<String,PwPmsMonitorDTO> monitorDTOMap = pwPmsMonitorDTOList.stream().collect(Collectors.toMap(PwPmsMonitorDTO::getMonitorId,Function.identity()));

            if (CollectionUtil.isNotEmpty(newList)) {
                newList = newList.stream().peek(item->{
                    if (monitorDTOMap.containsKey(item.getMeasurementPointId())) {
                        PwPmsMonitorDTO dto1 = monitorDTOMap.get(item.getMeasurementPointId());
                        item.setOrgName(dto1.getOrgName());
                        item.setPowerrName(dto1.getPowerName());
                        item.setMeasurementPointName(dto1.getMonitorName());
                        item.setMonitorSort(dto1.getMonitorSort());
                        item.setMonitorVoltageName(dto1.getVoltageLevel());
                    }
                }).collect(Collectors.toList());
                pageResult.setRecords(newList);
            }
        }
        return pageResult;
    }

    @Override
    public List<RSubstationIcon2VO.RSubstationInfoVO> getRStatSubstation(StatSubstationBizBaseParam param) {
        //远程调用监测点信息接口
        PmsDeviceInfoParam pms = new PmsDeviceInfoParam();
        pms.setDeptIndex(param.getId());
        pms.setStatisticalType(new SimpleDTO());
        List<PmsGeneralDeviceDTO> data = pmsGeneralDeviceInfoClient.getPwPmsDeviceInfoWithOrgId(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).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());
                    }
                });
            }
            return rSubstationInfoVOS;
        }
        return new ArrayList<>();
    }
    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;
    }
}
