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

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.common.pojo.enums.common.ServerEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.common.pojo.param.StatisticsBizBaseParam;
import com.njcn.common.utils.EnumUtils;
import com.njcn.device.biz.pojo.dto.StatisticsMonitor;
import com.njcn.device.biz.pojo.dto.*;
import com.njcn.device.biz.pojo.param.DeptGetLineParam;

import com.njcn.device.biz.pojo.param.SubstationParam;
import com.njcn.device.biz.pojo.po.Overlimit;
import com.njcn.device.biz.utils.COverlimitUtil;
import com.njcn.device.pms.mapper.majornetwork.*;
import com.njcn.device.pms.pojo.dto.PmsMonitorBaseDTO;
import com.njcn.device.pms.pojo.dto.PmsStatationStatInfoDTO;
import com.njcn.device.pms.pojo.param.PmsStatationStatInfoParam;
import com.njcn.device.pms.pojo.po.*;
import com.njcn.device.pms.service.ledgerManger.DeptLineService;
import com.njcn.device.pms.service.ledgerManger.CommTerminalService;
import com.njcn.device.pms.service.majornetwork.IPowerDistributionareaService;
import com.njcn.device.pq.pojo.po.LineBak;
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.user.pojo.po.Dept;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * pqs
 *
 * @author cdf
 * @date 2023/5/10
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class CommTerminalServiceImpl implements CommTerminalService {

    private final String commTerminal = "commTerminal#";

    private final DeptLineService deptLineService;

    private final DeptFeignClient deptFeignClient;

    private final MonitorMapper monitorMapper;

    private final StatationStatMapper statationStatMapper;

    private final PmsGeneratrixWireMapper pmsGeneratrixWireMapper;

    private final TerminalMapper terminalMapper;

    private final DistributionMonitorMapper distributionMonitorMapper;

    private final DicDataFeignClient dicDataFeignClient;

    private final OverlimitMapper overlimitMapper;

    private final PowerDistributionareaMapper powerDistributionareaMapper;

    private final PowerClientMapper powerClientMapper;

    private final PowerGenerationUserMapper powerGenerationUserMapper;
    private final IPowerDistributionareaService iPowerDistributionareaService;

    private final LineBakMapper lineBakMapper;

    /**
     * 基础获取单位信息
     *
     * @author cdf
     * @date 2023/5/10
     */
    @Override
    public List<DeptGetBase> getDeptChildrenByParent(DeptGetLineParam deptGetLineParam) {
        /*List<DeptGetBase> redisResult = (List<DeptGetBase>) redisUtil.getObjectByKey(commTerminal + deptGetLineParam.getDeptId());
        if (CollectionUtil.isNotEmpty(redisResult)) {
            return redisResult;
        }*/
        List<DeptGetBase> result = new ArrayList<>();
        List<DeptDTO> deptDTOList = deptFeignClient.getDeptDescendantIndexes(deptGetLineParam.getDeptId(), Stream.of(0, 1).collect(Collectors.toList())).getData();
        deptDTOList.forEach(it -> {
            DeptGetBase deptGetBase = new DeptGetBase();
            deptGetBase.setUnitId(it.getCode());
            deptGetBase.setUnitName(it.getName());
            List<String> deptChildren = deptDTOList.stream().filter(deptDTO -> deptDTO.getPids().contains(it.getId())).map(DeptDTO::getCode).collect(Collectors.toList());
            deptChildren.add(it.getCode());
            deptGetBase.setUnitChildrenList(deptChildren);
            deptGetBase.setDeptLevel(it.getPids().split(StrUtil.COMMA).length);
            result.add(deptGetBase);
        });
        //redisUtil.saveByKey(commTerminal + deptGetLineParam.getDeptId(),result);
        return result;
    }


    @Override
    public List<DeptGetChildrenMoreDTO> deptGetLine(DeptGetLineParam deptGetLineParam) {
        if (Objects.isNull(deptGetLineParam.getSystemType())) {
            deptGetLineParam.setSystemType(2);
        }
        List<DeptGetChildrenMoreDTO> result = new ArrayList<>();
        List<DeptGetBase> temDept = getDeptChildrenByParent(deptGetLineParam);
        //电压等级
        // List<DictData> voltageDic = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.DEV_VOLTAGE.getCode()).getData();
        //Map<String, DictData> mapVoltage = voltageDic.stream().collect(Collectors.toMap(DictData::getId, Function.identity()));
        //接线方式
        List<DictData> wireList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.DEV_CONNECT.getCode()).getData();
        Map<String,DictData> wireMap = wireList.stream().collect(Collectors.toMap(DictData::getId,Function.identity()));

        //监测点状态
        DictData dictData = dicDataFeignClient.getDicDataByNameAndTypeName(DicDataTypeEnum.LINE_STATE.getName(), DicDataEnum.RUN.getName()).getData();

        Map<String, List<Monitor>> finalMap;
        if (deptGetLineParam.getSystemType() == 0 || deptGetLineParam.getSystemType() == 2) {
            List<Monitor> monitorList = monitorMapper.selectList(new LambdaQueryWrapper<Monitor>()
                    .eq(Monitor::getStatus, DataStateEnum.ENABLE.getCode())
                    .eq(Objects.nonNull(deptGetLineParam.getIsUpToGrid()), Monitor::getIsUpToGrid, deptGetLineParam.getIsUpToGrid())
                    .eq(deptGetLineParam.getMonitorStateRunning(), Monitor::getMonitorState, dictData.getId()));
            finalMap = monitorList.stream().collect(Collectors.groupingBy(Monitor::getOrgId));
        }else {
            finalMap = new HashMap<>();
        }
        //TODO 配网数据量巨大，暂时注释
       /* Map<String, List<PmsMonitorBaseDTO>> mapPms = new HashMap<>();
        if (deptGetLineParam.getSystemType() == 1 || deptGetLineParam.getSystemType() == 2) {
            List<PmsMonitorBaseDTO> pmsMonitorBaseDTOList = distributionMonitorMapper.getDisMonitorAllList(null, 0);
            mapPms = pmsMonitorBaseDTOList.stream().collect(Collectors.groupingBy(PmsMonitorBaseDTO::getOrgId));
        }
        Map<String, List<PmsMonitorBaseDTO>> finalMapPms = mapPms;*/

        temDept.forEach(item -> {
            DeptGetChildrenMoreDTO deptGetChildrenMoreDTO = new DeptGetChildrenMoreDTO();
            deptGetChildrenMoreDTO.setUnitId(item.getUnitId());
            deptGetChildrenMoreDTO.setUnitName(item.getUnitName());
            deptGetChildrenMoreDTO.setUnitChildrenList(item.getUnitChildrenList());
            deptGetChildrenMoreDTO.setDeptLevel(item.getDeptLevel());
            List<String> deptIds = item.getUnitChildrenList();
            if (CollectionUtil.isNotEmpty(deptIds)) {
                List<LineDevGetDTO> lineList = new ArrayList<>();
                List<LineDevGetDTO> lineListPw = new ArrayList<>();
                deptIds.forEach(i -> {
                    if (deptGetLineParam.getSystemType() == 0 || deptGetLineParam.getSystemType() == 2) {
                        if (finalMap.containsKey(i)) {
                            finalMap.get(i).forEach(it -> {
                                LineDevGetDTO lineDevGetDTO = new LineDevGetDTO();
                                lineDevGetDTO.setUnitId(it.getOrgId());
                                lineDevGetDTO.setUnitName(it.getOrgName());
                                lineDevGetDTO.setPointId(it.getId());
                                lineDevGetDTO.setInterval(it.getStatisticalInterval());
                                lineDevGetDTO.setDevId(it.getTerminalId());
                                lineDevGetDTO.setType(1);
                                lineDevGetDTO.setLineType(1);
                                lineDevGetDTO.setLineTag(it.getMonitorTag());
                                lineDevGetDTO.setObjType(it.getObjType());
                                lineDevGetDTO.setPointName(it.getName());
                                //预处理模块需要的id修改value为id
                                lineDevGetDTO.setVoltageLevel(it.getVoltageLevel());
                                lineDevGetDTO.setIsUpToGrid(it.getIsUpToGrid());
                                lineDevGetDTO.setIsUploadHead(it.getIsUploadHead());
                                lineDevGetDTO.setMonitorUploadStatus(it.getMonitorUploadStatus());
                                lineDevGetDTO.setWiringMethod(wireMap.get(it.getTerminalWiringMethod()).getValue());
                                lineList.add(lineDevGetDTO);
                            });
                        }
                    }

                 /*   if (deptGetLineParam.getSystemType() == 1 || deptGetLineParam.getSystemType() == 2) {
                        if (finalMapPms.containsKey(i)) {
                            finalMapPms.get(i).forEach(it -> {
                                LineDevGetDTO lineDevGetDTOPms = new LineDevGetDTO();
                                lineDevGetDTOPms.setPointId(it.getMonitorId());
                                lineDevGetDTOPms.setInterval(10);
                                lineDevGetDTOPms.setDevId(it.getTerminalId());
                                lineDevGetDTOPms.setType(1);
                                lineDevGetDTOPms.setLineType(it.getLineType());
                                lineDevGetDTOPms.setVoltageLevel(mapVoltage.get(it.getVoltageLevel()).getValue());
                                lineListPw.add(lineDevGetDTOPms);
                            });
                        }
                    }*/
                });
                deptGetChildrenMoreDTO.setLineBaseList(lineList);
                deptGetChildrenMoreDTO.setPwMonitorIds(lineListPw);
            }
            result.add(deptGetChildrenMoreDTO);
        });
        return result;
    }


    @Override
    public List<DeptGetSubStationDTO> deptSubStation(DeptGetLineParam deptGetLineParam) {
        List<DeptGetSubStationDTO> result = new ArrayList<>();
        List<DeptGetBase> temDept = getDeptChildrenByParent(deptGetLineParam);
        List<StatationStat> statationStatList = statationStatMapper.selectList(new LambdaQueryWrapper<StatationStat>().eq(StatationStat::getStatus, 1));
        Map<String, List<StatationStat>> map = statationStatList.stream().collect(Collectors.groupingBy(StatationStat::getOrgId));

        //获取监测点信息
        List<PmsStatationStatInfoDTO> statationStatInfo = statationStatMapper.getStatationStatInfo(new PmsStatationStatInfoParam());
        Map<String, List<PmsStatationStatInfoDTO>> statationAndMonitorMap = statationStatInfo.stream().collect(Collectors.groupingBy(PmsStatationStatInfoDTO::getOrgId));

        //List<PmsMonitorBaseDTO> pmsMonitorBaseDTOList = distributionMonitorMapper.getDisMonitorAllList(null, 0);
        //Map<String, List<PmsMonitorBaseDTO>> mapPms = pmsMonitorBaseDTOList.stream().collect(Collectors.groupingBy(PmsMonitorBaseDTO::getOrgId));


        temDept.forEach(item -> {
            DeptGetSubStationDTO deptGetSubStationDTO = new DeptGetSubStationDTO();
            deptGetSubStationDTO.setUnitId(item.getUnitId());
            deptGetSubStationDTO.setUnitName(item.getUnitName());
            deptGetSubStationDTO.setUnitChildrenList(item.getUnitChildrenList());
            deptGetSubStationDTO.setDeptLevel(item.getDeptLevel());
            List<String> deptIds = item.getUnitChildrenList();
            if (CollectionUtil.isNotEmpty(deptIds)) {
                List<String> lineList = new ArrayList<>();
                List<String> lineListPw = new ArrayList<>();
                List<String> statationList = new ArrayList<>();
                deptIds.forEach(i -> {
                    if (map.containsKey(i)) {
                        lineList.addAll(map.get(i).stream().map(StatationStat::getPowerId).distinct().collect(Collectors.toList()));

                    }
                    if (statationAndMonitorMap.containsKey(i)) {
                        statationList.addAll(statationAndMonitorMap.get(i).stream().filter(x -> x.getPowerId() != null).map(PmsStatationStatInfoDTO::getMonitorId).collect(Collectors.toList()));
                    }
                    /*if (mapPms.containsKey(i)) {
                        lineListPw.addAll(mapPms.get(i).stream().map(PmsMonitorBaseDTO::getPowerrId).distinct().collect(Collectors.toList()));
                    }*/
                });
                deptGetSubStationDTO.setStationIds(lineList.stream().distinct().collect(Collectors.toList()));
                deptGetSubStationDTO.setStationAndMonitorIDs(statationList.stream().distinct().collect(Collectors.toList()));
                deptGetSubStationDTO.setPwStationIds(lineListPw.stream().distinct().collect(Collectors.toList()));

            }
            result.add(deptGetSubStationDTO);
        });
        return result;
    }

    @Override
    public List<DeptGetBusBarDTO> deptBusBar(DeptGetLineParam deptGetLineParam) {
        List<DeptGetBusBarDTO> result = new ArrayList<>();
        List<DeptGetBase> temDept = getDeptChildrenByParent(deptGetLineParam);

        List<GeneratrixWire> generatrixWireList = pmsGeneratrixWireMapper.getGeneratrixWireList();
        Map<String, List<GeneratrixWire>> map = generatrixWireList.stream().collect(Collectors.groupingBy(GeneratrixWire::getOrgId));

        //获取主网，线路和监测点关系个数
        List<GeneratrixWire> wireAndMonitor = pmsGeneratrixWireMapper.getWireAndMonitor();
        Map<String, List<GeneratrixWire>> wireAndMonitorMap = wireAndMonitor.stream().collect(Collectors.groupingBy(GeneratrixWire::getOrgId));

        /*List<PmsMonitorBaseDTO> pmsMonitorBaseDTOList = distributionMonitorMapper.getDisMonitorAllList(null, 0);
        Map<String, List<PmsMonitorBaseDTO>> mapPms = pmsMonitorBaseDTOList.stream().collect(Collectors.groupingBy(PmsMonitorBaseDTO::getOrgId));
*/

        temDept.forEach(item -> {
            DeptGetBusBarDTO deptGetBusBarDTO = new DeptGetBusBarDTO();
            deptGetBusBarDTO.setUnitId(item.getUnitId());
            deptGetBusBarDTO.setUnitName(item.getUnitName());
            deptGetBusBarDTO.setUnitChildrenList(item.getUnitChildrenList());
            List<String> deptIds = item.getUnitChildrenList();
            if (CollectionUtil.isNotEmpty(deptIds)) {
                List<String> lineList = new ArrayList<>();
                List<String> lineListPw = new ArrayList<>();
                List<String> wireAndMonitorList = new ArrayList<>();
                deptIds.forEach(i -> {
                    if (map.containsKey(i)) {
                        lineList.addAll(map.get(i).stream().map(GeneratrixWire::getId).collect(Collectors.toList()));
                    }
                    if (wireAndMonitorMap.containsKey(i)) {
                        wireAndMonitorList.addAll(wireAndMonitorMap.get(i).stream().map(GeneratrixWire::getId).collect(Collectors.toList()));
                    }
                   /* if (mapPms.containsKey(i)) {
                        lineListPw.addAll(mapPms.get(i).stream().map(PmsMonitorBaseDTO::getLineId).collect(Collectors.toList()));
                    }*/
                });
                deptGetBusBarDTO.setBusBarIds(lineList);
                deptGetBusBarDTO.setBusBarAndMonitorIDs(wireAndMonitorList);
                deptGetBusBarDTO.setPwBusBarIds(lineListPw);
            }
            result.add(deptGetBusBarDTO);
        });
        return result;
    }

    @Override
    public List<DeptGetDeviceDTO> deptGetDevice(DeptGetLineParam deptGetLineParam, Integer type) {
        List<DeptGetDeviceDTO> result = new ArrayList<>();
        List<DeptGetBase> temDept = getDeptChildrenByParent(deptGetLineParam);

        List<Monitor> monitorList = monitorMapper.selectList(new LambdaQueryWrapper<Monitor>().select(Monitor::getId, Monitor::getTerminalId, Monitor::getOrgId).eq(Monitor::getStatus, 1));
        List<String> terminalIds = monitorList.stream().map(Monitor::getTerminalId).distinct().collect(Collectors.toList());
        LambdaQueryWrapper<PmsTerminal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(PmsTerminal::getId, terminalIds);
        List<PmsTerminal> pmsTerminals = terminalMapper.selectList(lambdaQueryWrapper);
        Map<String, List<PmsTerminal>> map = pmsTerminals.stream().collect(Collectors.groupingBy(PmsTerminal::getOrgId));

      /*  List<PmsMonitorBaseDTO> pmsMonitorBaseDTOList = distributionMonitorMapper.getDisMonitorAllList(null, 0);
        List<String> pwTerminalIds = pmsMonitorBaseDTOList.stream().map(PmsMonitorBaseDTO::getTerminalId).distinct().collect(Collectors.toList());
        List<PmsTerminal> pwPmsTerminals = terminalMapper.selectList(new LambdaQueryWrapper<PmsTerminal>().in(CollUtil.isNotEmpty(pwTerminalIds),PmsTerminal::getId, pwTerminalIds));
        Map<String, List<PmsTerminal>> mapPms = pwPmsTerminals.stream().collect(Collectors.groupingBy(PmsTerminal::getOrgId));
*/
        Map<String, List<Monitor>> monitorMap = new HashMap<>();
        Map<String, List<PmsMonitorBaseDTO>> pwMonitorMap = new HashMap<>();
        if (type == 1) {
            //获取装置接入的监测点
            monitorMap = monitorList.stream().collect(Collectors.groupingBy(Monitor::getTerminalId));
            //pwMonitorMap = pmsMonitorBaseDTOList.stream().filter(x-> ObjectUtil.isNotNull(x.getTerminalId())).collect(Collectors.groupingBy(PmsMonitorBaseDTO::getTerminalId));


        }

        Map<String, List<Monitor>> finalMonitorMap = monitorMap;
        Map<String, List<PmsMonitorBaseDTO>> finalPwMonitorMap = pwMonitorMap;
        temDept.forEach(item -> {
            DeptGetDeviceDTO deptGetDeviceDTO = new DeptGetDeviceDTO();
            deptGetDeviceDTO.setUnitId(item.getUnitId());
            deptGetDeviceDTO.setUnitName(item.getUnitName());
            deptGetDeviceDTO.setUnitChildrenList(item.getUnitChildrenList());
            List<String> deptIds = item.getUnitChildrenList();
            if (CollectionUtil.isNotEmpty(deptIds)) {
                List<LineDevGetDTO> devGetDTOList = new ArrayList<>();
                List<LineDevGetDTO> devGetDTOListPw = new ArrayList<>();
                deptIds.forEach(i -> {
                    if (map.containsKey(i)) {
                        map.get(i).forEach(dev -> {
                            LineDevGetDTO lineDevGetDTO = new LineDevGetDTO();
                            lineDevGetDTO.setDevId(dev.getId());
                            lineDevGetDTO.setType(0);
                            lineDevGetDTO.setUpdateTime(dev.getUpdateTime());
                            lineDevGetDTO.setComFlag(dev.getComFlag());
                            if (type == 1) {
                                if (finalMonitorMap.containsKey(dev.getId())) {
                                    lineDevGetDTO.setMonitorIds(finalMonitorMap.get(dev.getId()).stream().map(Monitor::getId).distinct().collect(Collectors.toList()));
                                }
                            }
                            devGetDTOList.add(lineDevGetDTO);
                        });
                    }

                   /* if (mapPms.containsKey(i)) {
                        mapPms.get(i).forEach(dev -> {
                            LineDevGetDTO lineDevGetDTO = new LineDevGetDTO();
                            lineDevGetDTO.setDevId(dev.getId());
                            lineDevGetDTO.setType(1);
                            lineDevGetDTO.setUpdateTime(dev.getUpdateTime());
                            lineDevGetDTO.setComFlag(dev.getComFlag());
                            if(type == 1) {
                                if (finalPwMonitorMap.containsKey(dev.getId())) {
                                    lineDevGetDTO.setMonitorIds(finalMonitorMap.get(dev.getId()).stream().map(Monitor::getId).distinct().collect(Collectors.toList()));
                                }
                            }
                            devGetDTOListPw.add(lineDevGetDTO);
                        });
                    }*/
                });
                deptGetDeviceDTO.setDeviceList(devGetDTOList);
                deptGetDeviceDTO.setPwDeviceList(devGetDTOListPw);
            }
            result.add(deptGetDeviceDTO);
        });
        return result;
    }

    @Override
    public LineDevGetBandDTO substationGetLine(String substationId) {
        LineDevGetBandDTO result = new LineDevGetBandDTO();
        List<Monitor> monitorList = monitorMapper.selectList(new LambdaQueryWrapper<Monitor>().eq(Monitor::getStatus, 1).eq(Monitor::getPowerrId, substationId));
        List<PmsMonitorBaseDTO> pmsMonitorBaseDTOList = distributionMonitorMapper.getDisMonitorAllList(substationId, 1);
        assLineDev(result, monitorList, pmsMonitorBaseDTOList);
        return result;
    }

    @Override
    public LineDevGetBandDTO busBarGetLine(String busBarId) {
        LineDevGetBandDTO result = new LineDevGetBandDTO();
        List<PmsMonitorBaseDTO> pmsMonitorBaseDTOList = distributionMonitorMapper.getDisMonitorAllList(busBarId, 1);
        List<Monitor> monitorList = monitorMapper.selectList(new LambdaQueryWrapper<Monitor>().eq(Monitor::getStatus, 1).eq(Monitor::getLineId, busBarId));
        assLineDev(result, monitorList, pmsMonitorBaseDTOList);
        return result;
    }


    /**
     * 组装信息
     */
    private void assLineDev(LineDevGetBandDTO result, List<Monitor> monitorList, List<PmsMonitorBaseDTO> pmsMonitorBaseDTOList) {
        List<DictData> voltageDic = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.DEV_VOLTAGE.getCode()).getData();
        Map<String, DictData> mapVoltage = voltageDic.stream().collect(Collectors.toMap(DictData::getId, Function.identity()));

        if (CollectionUtil.isNotEmpty(monitorList)) {
            List<LineDevGetDTO> temZw = new ArrayList<>();

            monitorList.forEach(item -> {
                LineDevGetDTO lineDevGetDTO = new LineDevGetDTO();
                lineDevGetDTO.setPointId(item.getId());
                lineDevGetDTO.setType(1);
                lineDevGetDTO.setDevId(item.getTerminalId());
                lineDevGetDTO.setVoltageLevel(mapVoltage.get(item.getVoltageLevel()).getValue());
                temZw.add(lineDevGetDTO);
            });
            result.setZwList(temZw);
        }

        if (CollectionUtil.isNotEmpty(pmsMonitorBaseDTOList)) {
            List<LineDevGetDTO> temPw = new ArrayList<>();
            pmsMonitorBaseDTOList.forEach(item -> {
                LineDevGetDTO lineDevGetDTO = new LineDevGetDTO();
                lineDevGetDTO.setPointId(item.getMonitorId());
                lineDevGetDTO.setType(1);
                lineDevGetDTO.setDevId(item.getTerminalId());
                lineDevGetDTO.setVoltageLevel(mapVoltage.get(item.getVoltageLevel()).getValue());
                temPw.add(lineDevGetDTO);
            });
            result.setPwList(temPw);
        }
    }










    /*-------------------------------------------------------------------------------------*/

    /**
     * @author cdf
     * @date 2023/5/10
     */
    @Override
    public List<DeptGetChildrenDTO> deptGetLineList(DeptGetLineParam deptGetLineParam) {
        List<DeptGetChildrenDTO> all = new ArrayList<>();
        List<DeptDTO> deptDTOList = deptFeignClient.getDeptDescendantIndexes(deptGetLineParam.getDeptId(), Stream.of(0, 1).collect(Collectors.toList())).getData();
        getAllChildren(deptGetLineParam.getDeptId(), deptDTOList, all);
        Map<String, List<String>> map = deptLineService.getLineByDeptRelation(filterDataType(deptGetLineParam.getServerName()));
        all.forEach(item -> {
            List<String> itemDeptChildren = item.getDeptChildren();
            if (CollectionUtil.isNotEmpty(itemDeptChildren)) {
                List<String> lineIds = new ArrayList<>();
                itemDeptChildren.forEach(i -> {
                    if (map.containsKey(i)) {
                        lineIds.addAll(map.get(i));
                    }
                });
                item.setLineIds(lineIds);
            }
        });
        return all;
    }

    /**
     * 筛选数据类型
     */
    private Integer filterDataType(String serverName) {
        ServerEnum serverEnum = EnumUtils.getServerEnumByName(serverName);
        switch (serverEnum) {
            case EVENT:
                return 0;
            case HARMONIC:
                return 1;
            default:
                throw new BusinessException(CommonResponseEnum.INVALID_PARAMETER);
        }

    }


    /**
     * 获取指定部门所有子孙部门id
     *
     * @author cdf
     * @date 2023/4/24
     */
    private void getAllChildren(String deptId, List<DeptDTO> deptAllList, List<DeptGetChildrenDTO> res) {
        deptAllList.stream().filter(item -> item.getPid().equals(deptId)).forEach(it -> {
            List<String> deptChildren = deptAllList.stream().filter(deptDTO -> deptDTO.getPids().contains(it.getId())).map(DeptDTO::getId).collect(Collectors.toList());
            deptChildren.add(it.getId());
            DeptGetChildrenDTO generalDeviceDTO = new DeptGetChildrenDTO();
            generalDeviceDTO.setDeptId(it.getId());
            generalDeviceDTO.setDeptName(it.getName());
            generalDeviceDTO.setDeptChildren(deptChildren);
            res.add(generalDeviceDTO);
            getAllChildren(it.getId(), deptAllList, res);
        });
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updatePmsOverLimit() {
        List<DictData> dictDataList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.DEV_VOLTAGE.getCode()).getData();
        Map<String, DictData> map = dictDataList.stream().collect(Collectors.toMap(DictData::getId, Function.identity()));

        List<Monitor> monitorList = monitorMapper.selectList(new LambdaQueryWrapper<>());

        int count = 0;
        for (Monitor monitor : monitorList) {
            Float voltage = Float.parseFloat(map.get(monitor.getVoltageLevel()).getValue());
            Overlimit overlimit = COverlimitUtil.globalAssemble(voltage, monitor.getUserAgreementCapacity(), monitor.getPowerSupplyEqCapacity(), monitor.getMinShortCircuitCapacity(), 1, 0);
            overlimit.setId(monitor.getId());
            overlimitMapper.deleteById(monitor.getId());
            overlimitMapper.insert(overlimit);
            count++;

        }
        //二类监测点
        List<PowerDistributionarea> powerDistributionareaList = powerDistributionareaMapper.selectList(new LambdaQueryWrapper<>());
        int countArea = 0;
        for (PowerDistributionarea powerDistributionarea : powerDistributionareaList) {
            Float voltage = Float.parseFloat(map.get(powerDistributionarea.getVoltageLevel()).getValue());
            Overlimit overlimit = COverlimitUtil.globalAssemble(voltage, null, null, null, 1, 1);
            overlimit.setId(powerDistributionarea.getId());
            overlimitMapper.deleteById(powerDistributionarea.getId());
            overlimitMapper.insert(overlimit);
            countArea++;
        }
        //三类监测
        List<PowerClient> powerClientList = powerClientMapper.selectList(new LambdaQueryWrapper<>());
        int countClient = 0;
        for (PowerClient powerClient : powerClientList) {
            Float voltage = Float.parseFloat(map.get(powerClient.getVoltageLevel()).getValue());
            Overlimit overlimit = COverlimitUtil.globalAssemble(voltage, null, null, null, 1, 1);
            overlimit.setId(powerClient.getId());
            overlimitMapper.deleteById(powerClient.getId());
            overlimitMapper.insert(overlimit);
            countClient++;
        }

        List<PowerGenerationUser> powerGenerationUserList = powerGenerationUserMapper.selectList(new LambdaQueryWrapper<>());

        for (PowerGenerationUser powerGenerationUser : powerGenerationUserList) {
            Float voltage = Float.parseFloat(map.get(powerGenerationUser.getVoltageLevel()).getValue());
            Overlimit overlimit = COverlimitUtil.globalAssemble(voltage, null, null, null, 1, 1);
            overlimit.setId(powerGenerationUser.getId());
            overlimitMapper.deleteById(powerGenerationUser.getId());
            overlimitMapper.insert(overlimit);
            countClient++;
        }
        return "主网监测点限值更新" + count + "个,台区限值更新" + countArea + "个,发电用电用户" + countClient + "个";
    }

    @Override
    public List<String> getOneMonitorIds() {
        DictData dictData = dicDataFeignClient.getDicDataByNameAndTypeName(DicDataTypeEnum.LINE_STATE.getName(), DicDataEnum.RUN.getName()).getData();
        if (Objects.isNull(dictData)) {
            throw new BusinessException("监测点状态字典为空");
        }
        LambdaQueryWrapper<Monitor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(Monitor::getId).eq(Monitor::getMonitorState, dictData.getId());
        List<Monitor> monitorList = monitorMapper.selectList(lambdaQueryWrapper);
        return monitorList.stream().map(Monitor::getId).collect(Collectors.toList());
    }

    @Override
    public List<String> getRunMonitorList() {
        List<String> result = new ArrayList<>();
        DictData dictData = dicDataFeignClient.getDicDataByNameAndTypeName(DicDataTypeEnum.LINE_STATE.getName(), DicDataEnum.RUN.getName()).getData();
        if (Objects.isNull(dictData)) {
            throw new BusinessException("监测点状态字典为空");
        }
        LambdaQueryWrapper<Monitor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(Monitor::getId).eq(Monitor::getMonitorState, dictData.getId());
        List<Monitor> monitorList = monitorMapper.selectList(lambdaQueryWrapper);
        result.addAll(monitorList.stream().map(Monitor::getId).collect(Collectors.toList()));
        //加入无线用户测点
        DictData three = dicDataFeignClient.getDicDataByCodeAndType(DicDataEnum.THREE_LINE.getCode(),DicDataTypeEnum.LINE_SORT.getCode()).getData();
        List<DistributionMonitor> distributionMonitorList = distributionMonitorMapper.selectList(new LambdaQueryWrapper<DistributionMonitor>().eq(DistributionMonitor::getMonitorSort,three.getId()).eq(DistributionMonitor::getStatus,DataStateEnum.ENABLE.getCode()));

        if(CollectionUtil.isNotEmpty(distributionMonitorList)){
            result.addAll(distributionMonitorList.stream().map(DistributionMonitor::getMonitorId).collect(Collectors.toList()));
        }
        return result;
    }

    /**
     * 获取单个监测点信息
     *
     * @author cdf
     * @date 2023/10/12
     */
    @Override
    public LineDevGetDTO getMonitorDetail(String monitorId) {
        LineDevGetDTO lineDevGetDTO = new LineDevGetDTO();

        Monitor monitor = monitorMapper.selectById(monitorId);
        if (Objects.nonNull(monitor)) {
            lineDevGetDTO.setPointName(monitor.getName());
            lineDevGetDTO.setInterval(monitor.getStatisticalInterval());
            lineDevGetDTO.setDevId(monitor.getTerminalId());
            lineDevGetDTO.setVoltageLevel(monitor.getVoltageLevel());
            lineDevGetDTO.setType(0);
            LineBak lineBak = lineBakMapper.selectOne(new LambdaQueryWrapper<LineBak>()
                    .eq(LineBak::getId, monitor.getId())
            );
            if (ObjUtil.isNotNull(lineBak)) {
                lineDevGetDTO.setOracleLineId(lineBak.getLineId());
            }
            return lineDevGetDTO;
        }

        LambdaQueryWrapper<DistributionMonitor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DistributionMonitor::getMonitorId, monitorId);
        DistributionMonitor distributionMonitor = distributionMonitorMapper.selectOne(lambdaQueryWrapper);
        if (Objects.nonNull(distributionMonitor)) {
            DictData dictDataSort = dicDataFeignClient.getDicDataById(distributionMonitor.getMonitorSort()).getData();

            if (DicDataEnum.TWO_LINE.getCode().equals(dictDataSort.getCode())) {
                //二类
                PowerDistributionarea powerDistributionarea = powerDistributionareaMapper.selectById(monitorId);
                if (Objects.nonNull(powerDistributionarea)) {
                    lineDevGetDTO.setPointName(powerDistributionarea.getName());
                    lineDevGetDTO.setInterval(distributionMonitor.getStatisticalInterval());
                    lineDevGetDTO.setDevId(distributionMonitor.getTerminalId());
                    lineDevGetDTO.setVoltageLevel(powerDistributionarea.getVoltageLevel());
                    lineDevGetDTO.setType(1);
                    lineDevGetDTO.setLineType(2);
                    return lineDevGetDTO;
                }
            } else if (DicDataEnum.THREE_LINE.getCode().equals(dictDataSort.getCode())) {
                if (distributionMonitor.getIfPowerUser() == 1) {
                    //发电用户
                    PowerGenerationUser powerGenerationUser = powerGenerationUserMapper.selectById(monitorId);
                    lineDevGetDTO.setPointName(powerGenerationUser.getName());
                    lineDevGetDTO.setInterval(distributionMonitor.getStatisticalInterval());
                    lineDevGetDTO.setDevId(distributionMonitor.getTerminalId());
                    lineDevGetDTO.setVoltageLevel(powerGenerationUser.getVoltageLevel());
                } else {
                    PowerClient powerClient = powerClientMapper.selectById(monitorId);
                    lineDevGetDTO.setPointName(powerClient.getName());
                    lineDevGetDTO.setInterval(distributionMonitor.getStatisticalInterval());
                    lineDevGetDTO.setDevId(distributionMonitor.getTerminalId());
                    lineDevGetDTO.setVoltageLevel(powerClient.getVoltageLevel());
                }
                lineDevGetDTO.setType(1);
                lineDevGetDTO.setLineType(3);
                return lineDevGetDTO;
            }

        }
        return null;
    }

    @Override
    public List<StatisticsMonitor> getStatisticsMonitor(StatisticsBizBaseParam param) {
        List<StatisticsMonitor> info = new ArrayList<>();
        //部门信息
        List<DeptDTO> deptInfos = deptFeignClient.getDeptDescendantIndexes(param.getId(), Stream.of(0, 1).collect(Collectors.toList())).getData();
        List<DictData> voltageList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.DEV_VOLTAGE.getCode()).getData();
        List<Monitor> monitors = monitorMapper.selectMonitorList(param);

        List<PowerDistributionarea> distributionList = iPowerDistributionareaService.list(new LambdaQueryWrapper<PowerDistributionarea>()
                .select(PowerDistributionarea::getId,
                        PowerDistributionarea::getOrgId,
                        PowerDistributionarea::getVoltageLevel,
                        PowerDistributionarea::getApLoadType,
                        PowerDistributionarea::getPhotovoltaicCapacity
                )
                .eq(PowerDistributionarea::getStatus, 1)
                .ge(StrUtil.isNotBlank(param.getStartTime()), PowerDistributionarea::getCreateTime, DateUtil.beginOfDay(DateUtil.parse(param.getStartTime())))
                .le(StrUtil.isNotBlank(param.getEndTime()), PowerDistributionarea::getCreateTime, DateUtil.endOfDay(DateUtil.parse(param.getEndTime())))
        );
        List<DictData> dictDatas = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.MONITORING_LABELS.getCode()).getData();
        Map<String, String> map = new HashMap<>();
        for (DictData dictDatum : dictDatas) {
            if (DicDataEnum.ONSHORE_WIND.getCode().equals(dictDatum.getCode())) {
                map.put("fd", dictDatum.getId());
            }
            if (DicDataEnum.POWER_STATION.getCode().equals(dictDatum.getCode())) {
                map.put("gfdz", dictDatum.getId());
            }
            if (DicDataEnum.ELECTRIFIED_RAILWAYS.getCode().equals(dictDatum.getCode())) {
                map.put("dq", dictDatum.getId());
            }
            if (DicDataEnum.SMELT_LOAD.getCode().equals(dictDatum.getCode())) {
                map.put("yl", dictDatum.getId());
            }
            if (DicDataEnum.DISTRIBUTED_PHOTOVOLTAICS.getCode().equals(dictDatum.getCode())) {
                map.put("gf", dictDatum.getId());
            }
        }
        // 过滤出传入id的子单位id
        List<DeptDTO> directDeptInfos = deptInfos.stream()
                .filter(deptDTO -> deptDTO.getPid().equals(param.getId()))
                .collect(Collectors.toList());
        List<Monitor> monitorsNew;
        List<PowerDistributionarea> distributionNew;
        if (CollectionUtil.isEmpty(directDeptInfos)) {
            //没有直接子部门，获取当前部门所有信息
            List<DeptDTO> dept = deptInfos.stream()
                    .filter(deptDTO -> deptDTO.getId().equals(param.getId()))
                    .collect(Collectors.toList());

            monitorsNew = monitors.stream().filter(x -> x.getOrgId().equals(dept.get(0).getCode())).collect(Collectors.toList());
            distributionNew = distributionList.stream().filter(x -> x.getOrgId().equals(dept.get(0).getCode())).collect(Collectors.toList());
            statisticsMonitorAdd(Arrays.asList(dept.get(0).getCode()), voltageList, monitorsNew, distributionNew, map, info);
        } else {
            // 遍历子单位id
            for (DeptDTO deptDTO : directDeptInfos) {
                // 与全部单位id比较，取出上级为子单位id的集合
                List<DeptDTO> descendantDeptDTO = deptInfos.stream()
                        .filter(d -> d.getPids().contains(deptDTO.getId()))
                        .collect(Collectors.toList());
                //形成需要查询监测点的部门索引
                List<String> deptIdList = descendantDeptDTO.stream()
                        .map(DeptDTO::getCode)
                        .distinct()
                        .collect(Collectors.toList());
                deptIdList.add(deptDTO.getCode());
                monitorsNew = monitors.stream().filter(x -> deptIdList.contains(x.getOrgId())).collect(Collectors.toList());
                distributionNew = distributionList.stream().filter(x -> deptIdList.contains(x.getOrgId())).collect(Collectors.toList());
                statisticsMonitorAdd(deptIdList, voltageList, monitorsNew, distributionNew, map, info);
            }
        }
        return info;
    }

    public List<StatisticsMonitor> statisticsMonitorAdd(List<String> deptIdList, List<DictData> voltageList, List<Monitor> monitorsNew, List<PowerDistributionarea> list, Map<String, String> map, List<StatisticsMonitor> info) {

        Map<String, List<Monitor>> monitorMap = monitorsNew.stream().collect(Collectors.groupingBy(x -> x.getOrgId() + "_" + x.getVoltageLevel()));
        Map<String, List<PowerDistributionarea>> distributionMap = list.stream().collect(Collectors.groupingBy(x -> x.getOrgId() + "_" + x.getVoltageLevel()));

        for (String s : deptIdList) {
            for (DictData dictData : voltageList) {
                StatisticsMonitor monitor = new StatisticsMonitor();
                monitor.setOrgId(s);
                monitor.setVoltageLevel(dictData.getId());
                monitor.setGeneratrixSum(0);
                String key = s + "_" + dictData.getId();
                if (monitorMap.containsKey(key)) {
                    List<Monitor> monitors = monitorMap.get(s + "_" + dictData.getId());
                    monitor.setLineNum(Math.toIntExact(monitors.stream().map(Monitor::getLineId).distinct().count()));
                    monitor.setPhotovoltaicLineNum(Math.toIntExact(monitors.stream().filter(x -> x.getMonitorTag().equals(map.get("gf"))).map(Monitor::getLineId).distinct().count()));
                    monitor.setWindPowerNum(Math.toIntExact(monitors.stream().filter(x -> x.getMonitorTag().equals(map.get("fd"))).map(Monitor::getId).distinct().count()));
                    monitor.setPvNum(Math.toIntExact(monitors.stream().filter(x -> x.getMonitorTag().equals(map.get("gfdz"))).map(Monitor::getId).distinct().count()));
                    monitor.setSmeltingLoadNum(Math.toIntExact(monitors.stream().filter(x -> x.getMonitorTag().equals(map.get("yl"))).map(Monitor::getId).distinct().count()));
                    monitor.setElectricTrackNum(Math.toIntExact(monitors.stream().filter(x -> x.getMonitorTag().equals(map.get("dq"))).map(Monitor::getId).distinct().count()));
                    monitor.setWindPowerCapacity(monitors.stream().filter(x -> x.getMonitorTag().equals(map.get("fd"))).mapToDouble(Monitor::getStandShortCapacity).sum());
                    monitor.setPvCapacity(monitors.stream().filter(x -> x.getMonitorTag().equals(map.get("fd"))).mapToDouble(Monitor::getStandShortCapacity).sum());
                    monitor.setSmeltingLoadCapacity(monitors.stream().filter(x -> x.getMonitorTag().equals(map.get("fd"))).mapToDouble(Monitor::getStandShortCapacity).sum());
                    monitor.setElectricTrackCapacity(monitors.stream().filter(x -> x.getMonitorTag().equals(map.get("fd"))).mapToDouble(Monitor::getStandShortCapacity).sum());

                } else {
                    monitor.setLineNum(0);
                    monitor.setPhotovoltaicLineNum(0);
                    monitor.setWindPowerNum(0);
                    monitor.setPvNum(0);
                    monitor.setSmeltingLoadNum(0);
                    monitor.setElectricTrackNum(0);
                    monitor.setWindPowerCapacity(0.0);
                    monitor.setPvCapacity(00.0);
                    monitor.setSmeltingLoadCapacity(0.0);
                    monitor.setElectricTrackCapacity(0.0);

                }
                if (distributionMap.containsKey(key)) {
                    List<PowerDistributionarea> distributionAreaList = distributionMap.get(key);
                    int size = distributionAreaList.size();
                    monitor.setPlatformNum(size);
                    monitor.setPhotovoltaicCourtsNum(distributionAreaList.stream().filter(x -> x.getApLoadType().equals(map.get("gf"))).map(PowerDistributionarea::getId).distinct().collect(Collectors.toList()).size());
                    //获取分布式光伏总容量
                    double sum = distributionAreaList.stream().mapToDouble(PowerDistributionarea::getPhotovoltaicCapacity).sum();
                    double v = sum / size * 0.25;
                    monitor.setExceedCourtscapNum(distributionAreaList.stream().filter(x -> x.getPhotovoltaicCapacity() > v).distinct().collect(Collectors.toList()).size());
                } else {
                    monitor.setPlatformNum(0);
                    monitor.setPhotovoltaicCourtsNum(0);
                    monitor.setExceedCourtscapNum(0);
                }
                info.add(monitor);
            }
        }
        return info;
    }

    @Override
    public Map<String, String> getCustomDetailByLineId(String id) {
        return monitorMapper.getCustomDetailByLineId(id);
    }

    @Override
    public List<BusBarAndHisMonitorDTO> getBusBarAndHisMonitor() {
        List<BusBarAndHisMonitorDTO> result = new ArrayList<>();
        DictData dictData = dicDataFeignClient.getDicDataByNameAndTypeName(DicDataTypeEnum.LINE_STATE.getName(), DicDataEnum.RUN.getName()).getData();

        LambdaQueryWrapper<Monitor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(Monitor::getId, Monitor::getLineId).eq(Monitor::getMonitorState, dictData.getId())
                .eq(Monitor::getStatus, DataStateEnum.ENABLE.getCode());
        List<Monitor> monitorList = monitorMapper.selectList(lambdaQueryWrapper);

        Map<String, List<Monitor>> busBarMap = monitorList.stream().collect(Collectors.groupingBy(Monitor::getLineId));
        busBarMap.forEach((key, val) -> {
            BusBarAndHisMonitorDTO busBarAndHisMonitorDTO = new BusBarAndHisMonitorDTO();
            busBarAndHisMonitorDTO.setBusBarId(key);
            busBarAndHisMonitorDTO.setMonitorIds(val.stream().map(Monitor::getId).collect(Collectors.toList()));
            result.add(busBarAndHisMonitorDTO);
        });
        return result;
    }

    @Override
    public List<CommMonitorInfoDTO> tagOrIdGetMonitorList(List<String> tagList, List<String> monitorIds, List<String> deptIds) {
        List<CommMonitorInfoDTO> result = new ArrayList<>();

        DictData dictData = dicDataFeignClient.getDicDataByCodeAndType(DicDataEnum.RUN.getCode(), DicDataTypeEnum.LINE_STATE.getCode()).getData();

        List<Dept> deptList = deptFeignClient.getDeptInfoListByIds(deptIds).getData();
        List<Monitor> monitorList = monitorMapper.selectList(new LambdaQueryWrapper<Monitor>()

                .eq(Monitor::getStatus, DataStateEnum.ENABLE.getCode()).in(Monitor::getOrgId, deptList.stream().map(Dept::getCode).collect(Collectors.toList()))
                .eq(Monitor::getMonitorState, dictData.getId())
                .in(Monitor::getMonitorTag, tagList)
                .or(CollectionUtil.isNotEmpty(monitorIds), item -> item.in(CollectionUtil.isNotEmpty(monitorIds), Monitor::getId, monitorIds)));
        monitorList.forEach(item -> result.add(CommMonitorInfoDTO.builder().id(item.getId()).name(item.getName()).powerStationName(item.getPowerrName()).objTagId(item.getMonitorTag()).build()));
        return result;
    }

    @Override
    public List<DeptGetSubStationDTO.Info> deptSubStationInfo(DeptGetLineParam deptGetLineParam) {
        List<DeptGetSubStationDTO.Info> result = new ArrayList<>();
        List<DeptGetBase> temDept = getDeptChildrenByParent(deptGetLineParam);

        //获取监测点信息
        List<PmsStatationStatInfoDTO> statationStatInfo = statationStatMapper.getStatationStatInfo(new PmsStatationStatInfoParam());
        Map<String, List<PmsStatationStatInfoDTO>> statationAndMonitorMap = statationStatInfo.stream().collect(Collectors.groupingBy(PmsStatationStatInfoDTO::getOrgId));

//        List<PmsMonitorBaseDTO> pmsMonitorBaseDTOList = distributionMonitorMapper.getDisMonitorAllList(null, 0);
//        Map<String, List<PmsMonitorBaseDTO>> mapPms = pmsMonitorBaseDTOList.stream().collect(Collectors.groupingBy(PmsMonitorBaseDTO::getOrgId));

        temDept.forEach(item -> {
            DeptGetSubStationDTO.Info deptGetSubStationDTO = new DeptGetSubStationDTO.Info();
            deptGetSubStationDTO.setUnitId(item.getUnitId());
            deptGetSubStationDTO.setUnitName(item.getUnitName());
            deptGetSubStationDTO.setUnitChildrenList(item.getUnitChildrenList());
            deptGetSubStationDTO.setDeptLevel(item.getDeptLevel());
            List<String> deptIds = item.getUnitChildrenList();
            if (CollectionUtil.isNotEmpty(deptIds)) {
                List<SubGetBase> subList = new ArrayList<>();
                List<SubGetBase> subListPw = new ArrayList<>();
                SubGetBase subGetBase;
                for (String deptId : deptIds) {
                    if (statationAndMonitorMap.containsKey(deptId)) {
                        //获取部门下变电信息
                        List<PmsStatationStatInfoDTO> sub = statationAndMonitorMap.get(deptId);
                        Map<String, List<PmsStatationStatInfoDTO>> subMap = sub.stream()
                                .collect(Collectors.groupingBy(x -> x.getPowerId() + "_" + x.getPowerName() + "_" + x.getPowerVoltageLevel()));
                        for (Map.Entry<String, List<PmsStatationStatInfoDTO>> stringListEntry : subMap.entrySet()) {
                            String[] split = stringListEntry.getKey().split("_");
                            subGetBase = new SubGetBase();
                            subGetBase.setId(split[0]);
                            subGetBase.setName(split[1]);
                            subGetBase.setVoltageLevel(split[2]);
                            List<String> monitorIds = stringListEntry.getValue().stream().map(PmsStatationStatInfoDTO::getMonitorId).distinct().collect(Collectors.toList());
                            subGetBase.setUnitChildrenList(monitorIds);
                            subList.add(subGetBase);
                        }
                    }

//                    if (mapPms.containsKey(deptId)) {
//                        //获取部门下变电信息
//                        List<PmsMonitorBaseDTO> sub = mapPms.get(deptId);
//                        Map<String, List<PmsMonitorBaseDTO>> subMap = sub.stream()
//                                .collect(Collectors.groupingBy(x->x.getPowerrId()+"_"+x.getVoltageLevel()));
//                        for (Map.Entry<String, List<PmsMonitorBaseDTO>> stringListEntry : subMap.entrySet()) {
//                            String[] split = stringListEntry.getKey().split("_");
//                            subGetBase=new SubGetBase();
//                            subGetBase.setId(split[0]);
//                            subGetBase.setVoltageLevel(split[1]);
//                            List<String> monitorIds = stringListEntry.getValue().stream().map(PmsMonitorBaseDTO::getMonitorId).distinct().collect(Collectors.toList());
//                            subGetBase.setUnitChildrenList(monitorIds);
//                            subListPw.add(subGetBase);
//                        }
//                    }
                }
                deptGetSubStationDTO.setStationIds(subList);
            }
            result.add(deptGetSubStationDTO);
        });
        return result;
    }

    @Override
    public List<SubGetBase> tagOrIdGetSub(SubstationParam param) {
        return statationStatMapper.selectStatationStatList(param);
    }

}
