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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.device.biz.pojo.po.Overlimit;
import com.njcn.device.biz.utils.COverlimitUtil;
import com.njcn.device.pms.enums.PmsDeviceResponseEnum;
import com.njcn.device.pms.mapper.majornetwork.*;
import com.njcn.device.pms.pojo.dto.PmsDistributionMonitorDTO;
import com.njcn.device.pms.pojo.dto.PmsMonitorBaseDTO;
import com.njcn.device.pms.pojo.dto.PwPmsMonitorDTO;
import com.njcn.device.pms.pojo.param.DistributionMonitorParam;
import com.njcn.device.pms.pojo.param.MonitorStatus;
import com.njcn.device.pms.pojo.param.PmsDeviceInfoParam;
import com.njcn.device.pms.pojo.param.TaiZhangDelParam;
import com.njcn.device.pms.pojo.param.pwUser.PwUserMonitorParam;
import com.njcn.device.pms.pojo.po.*;
import com.njcn.device.pms.pojo.vo.DoubleUserVO;
import com.njcn.device.pms.pojo.vo.PmsSummaryStatisticsVO;
import com.njcn.device.pms.pojo.vo.pwUser.PwUserMonitorDataVO;
import com.njcn.device.pms.service.majornetwork.IDistributionMonitorService;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.system.enums.DicDataTypeEnum;
import com.njcn.system.pojo.constant.DicDataConstant;
import com.njcn.system.pojo.po.DictData;
import com.njcn.user.api.DeptFeignClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hongawen
 * @since 2022-10-14
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class DistributionMonitorServiceImpl extends MppServiceImpl<DistributionMonitorMapper, DistributionMonitor> implements IDistributionMonitorService {

    private final DicDataFeignClient dicDataFeignClient;

    private final MonitorMapper monitorMapper;

    private final PowerDistributionareaMapper powerDistributionareaMapper;

    private final PowerClientMapper powerClientMapper;

    private final PowerGenerationUserMapper powerGenerationUserMapper;

    private final DeptFeignClient deptFeignClient;

    private final DistributionMonitorMapper distributionMonitorMapper;

    private final OverlimitMapper overlimitMapper;


    @Override
    public List<PmsMonitorBaseDTO> getMonitorByCondition(List<String> deptIdList, PmsDeviceInfoParam pmsDeviceInfoParam) {
//        return this.baseMapper.getIdByOrgId(deptIdList, pmsDeviceInfoParam);
        return this.baseMapper.getIdByOrgIdNew(deptIdList, pmsDeviceInfoParam);
    }

    @Override
    public PmsSummaryStatisticsVO.SummaryVO getMonitorAndDevice(List<String> deptIdList) {
        return this.baseMapper.getMonitorAndDevice(deptIdList);
    }

    @Override
    public List<DoubleUserVO> getDoubleUserByDept(String orgId) {
        return this.baseMapper.getDoubleUserByDept(orgId);
    }

    @Override
    public Boolean addDistributionMonitor(DistributionMonitorParam distributionMonitorParam) {
        DictData dictData = dicDataFeignClient.getDicDataById(distributionMonitorParam.getMonitorSort()).getData();
        if (Objects.isNull(dictData)) {
            throw new BusinessException(PmsDeviceResponseEnum.NO_LINE_SORT);
        }

        //判断配网表里是否已经存在监测点
        LambdaQueryWrapper<DistributionMonitor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DistributionMonitor::getStatus, DataStateEnum.ENABLE.getCode())
                .eq(DistributionMonitor::getMonitorId, distributionMonitorParam.getMonitorId());
        int count = this.count(lambdaQueryWrapper);
        if (count > 0) {
            throw new BusinessException(PmsDeviceResponseEnum.DIS_ADD_REPEAT);
        }

        DistributionMonitor distributionMonitor = new DistributionMonitor();
        distributionMonitor.setMonitorSort(dictData.getId());
        switch (dictData.getCode()) {
            case DicDataConstant.ONE_LINE:
                //一类监测点
                Monitor monitor = monitorMapper.selectById(distributionMonitorParam.getMonitorId());
                if (Objects.isNull(monitor)) {
                    throw new BusinessException(PmsDeviceResponseEnum.ONE_MONITOR_NOT_FIND);
                }
                distributionMonitor.setMonitorId(monitor.getId());
                distributionMonitor.setCreatedDate(LocalDateTime.now());
                distributionMonitor.setIfPowerUser(0);
                distributionMonitor.setOrgId(monitor.getOrgId());
                distributionMonitor.setMonitorState(monitor.getMonitorState());
                distributionMonitor.setVoltageLevel(monitor.getVoltageLevel());
                distributionMonitor.setStatus(DataStateEnum.ENABLE.getCode());
                distributionMonitor.setTerminalId(monitor.getTerminalId());
                distributionMonitor.setTerminalWiringMethod(monitor.getTerminalWiringMethod());
                distributionMonitor.setLineId(monitor.getLineId());
                distributionMonitor.setPowerStationId(monitor.getPowerrId());
                distributionMonitor.setStatisticalInterval(monitor.getStatisticalInterval());
                distributionMonitor.setCt1(monitor.getCt1());
                distributionMonitor.setCt2(monitor.getCt2());
                distributionMonitor.setPt1(monitor.getPt1());
                distributionMonitor.setPt2(monitor.getPt2());
                this.save(distributionMonitor);
                break;
            case DicDataConstant.TWO_LINE:
                //二类监测点(台区)
                PowerDistributionarea powerDistributionarea = powerDistributionareaMapper.selectById(distributionMonitorParam.getMonitorId());
                if (Objects.isNull(powerDistributionarea)) {
                    throw new BusinessException(PmsDeviceResponseEnum.TWO_MONITOR_NOT_FIND);
                }
                PowerDistributionarea powerDistributionareaTem = powerDistributionareaMapper.selectById(powerDistributionarea.getId());
                distributionMonitor.setMonitorId(powerDistributionarea.getId());
                distributionMonitor.setOrgId(powerDistributionarea.getOrgId());
                distributionMonitor.setCreatedDate(LocalDateTime.now());
                distributionMonitor.setIfPowerUser(0);
                distributionMonitor.setMonitorState(distributionMonitorParam.getMonitorState());
                distributionMonitor.setVoltageLevel(powerDistributionareaTem.getVoltageLevel());
                distributionMonitor.setStatus(DataStateEnum.ENABLE.getCode());

                distributionMonitor.setTerminalId(distributionMonitorParam.getTerminalId());
                distributionMonitor.setStatisticalInterval(15);
                distributionMonitor.setPowerStationId(powerDistributionareaTem.getPowerStationId());
                distributionMonitor.setLineId(powerDistributionareaTem.getLineId());
                distributionMonitor.setTerminalWiringMethod(distributionMonitorParam.getTerminalWiringMethod());
                distributionMonitor.setCt1(distributionMonitorParam.getCt1());
                distributionMonitor.setCt2(distributionMonitorParam.getCt2());
                distributionMonitor.setPt1(distributionMonitorParam.getPt1());
                distributionMonitor.setPt2(distributionMonitorParam.getPt2());
                distributionMonitor.setStatisticalInterval(distributionMonitorParam.getStatisticalInterval());
                this.save(distributionMonitor);

                //只有二三类需要填写限值，一类默认新建主网监测点时候已经新建限值
                overLimitAdd(distributionMonitor.getVoltageLevel(), distributionMonitor.getMonitorId(),false);
                break;
            case DicDataConstant.THREE_LINE:
                //三类监测点
                if (distributionMonitorParam.getIfPowerUser() == 1) {
                    //发电
                    PowerGenerationUser powerGenerationUser = powerGenerationUserMapper.selectById(distributionMonitorParam.getMonitorId());
                    if (Objects.isNull(powerGenerationUser)) {
                        throw new BusinessException(PmsDeviceResponseEnum.THREE_MONITOR_NOT_FIND);
                    }
                    distributionMonitor.setIfPowerUser(1);
                    distributionMonitor.setVoltageLevel(powerGenerationUser.getVoltageLevel());
                    distributionMonitor.setOrgId(powerGenerationUser.getOrgId());
                    distributionMonitor.setLineId(powerGenerationUser.getLineId());
                    distributionMonitor.setPowerStationId(powerGenerationUser.getPowerStationId());
                } else {
                    //用电
                    PowerClient powerClient = powerClientMapper.selectById(distributionMonitorParam.getMonitorId());
                    if (Objects.isNull(powerClient)) {
                        throw new BusinessException(PmsDeviceResponseEnum.THREE_MONITOR_NOT_FIND);
                    }
                    distributionMonitor.setIfPowerUser(0);
                    distributionMonitor.setVoltageLevel(powerClient.getVoltageLevel());
                    distributionMonitor.setOrgId(powerClient.getOrgId());
                    distributionMonitor.setLineId(powerClient.getLineId());
                    distributionMonitor.setPowerStationId(powerClient.getPowerStationId());
                }
                distributionMonitor.setMonitorId(distributionMonitorParam.getMonitorId());
                distributionMonitor.setCreatedDate(LocalDateTime.now());
                distributionMonitor.setMonitorState(distributionMonitorParam.getMonitorState());
                distributionMonitor.setStatus(DataStateEnum.ENABLE.getCode());
                distributionMonitor.setTerminalId(distributionMonitorParam.getTerminalId());
                distributionMonitor.setTerminalWiringMethod(distributionMonitorParam.getTerminalWiringMethod());
                distributionMonitor.setCt1(distributionMonitorParam.getCt1());
                distributionMonitor.setCt2(distributionMonitorParam.getCt2());
                distributionMonitor.setPt1(distributionMonitorParam.getPt1());
                distributionMonitor.setPt2(distributionMonitorParam.getPt2());
                distributionMonitor.setStatisticalInterval(distributionMonitorParam.getStatisticalInterval());
                this.save(distributionMonitor);
                overLimitAdd(distributionMonitor.getVoltageLevel(), distributionMonitor.getMonitorId(),false);

                break;
            default:
                throw new BusinessException(PmsDeviceResponseEnum.NO_LINE_SORT);
        }
        return true;
    }

    @Override
    public Boolean updateDistributionMonitor(DistributionMonitorParam distributionMonitorParam) {
        DictData dictData = dicDataFeignClient.getDicDataById(distributionMonitorParam.getMonitorSort()).getData();
        if (Objects.isNull(dictData)) {
            throw new BusinessException(PmsDeviceResponseEnum.NO_LINE_SORT);
        }
        LambdaUpdateWrapper<DistributionMonitor> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(DistributionMonitor::getMonitorSort, dictData.getId());

        String voltageLevel;
        switch (dictData.getCode()) {
            case DicDataConstant.TWO_LINE:
                //二类监测点(台区)
                PowerDistributionarea powerDistributionarea = powerDistributionareaMapper.selectById(distributionMonitorParam.getMonitorId());
                if (Objects.isNull(powerDistributionarea)) {
                    throw new BusinessException(PmsDeviceResponseEnum.TWO_MONITOR_NOT_FIND);
                }
                voltageLevel = powerDistributionarea.getVoltageLevel();
                lambdaUpdateWrapper.eq(DistributionMonitor::getMonitorId, powerDistributionarea.getId());
                break;
            case DicDataConstant.THREE_LINE:
                //三类监测点
                if (distributionMonitorParam.getIfPowerUser() == 1) {
                    //发电
                    PowerGenerationUser powerGenerationUser = powerGenerationUserMapper.selectById(distributionMonitorParam.getMonitorId());
                    if (Objects.isNull(powerGenerationUser)) {
                        throw new BusinessException(PmsDeviceResponseEnum.THREE_MONITOR_NOT_FIND);
                    }
                    voltageLevel = powerGenerationUser.getVoltageLevel();
                    lambdaUpdateWrapper.eq(DistributionMonitor::getMonitorId, powerGenerationUser.getId());
                } else {
                    //用电
                    PowerClient powerClient = powerClientMapper.selectById(distributionMonitorParam.getMonitorId());
                    if (Objects.isNull(powerClient)) {
                        throw new BusinessException(PmsDeviceResponseEnum.THREE_MONITOR_NOT_FIND);
                    }
                    voltageLevel = powerClient.getVoltageLevel();
                    lambdaUpdateWrapper.eq(DistributionMonitor::getMonitorId, powerClient.getId());
                }
                break;
            default:
                throw new BusinessException(PmsDeviceResponseEnum.NO_LINE_SORT);
        }
        lambdaUpdateWrapper.set(DistributionMonitor::getMonitorState, distributionMonitorParam.getMonitorState())
                .set(DistributionMonitor::getTerminalId, distributionMonitorParam.getTerminalId())
                .set(DistributionMonitor::getTerminalWiringMethod, distributionMonitorParam.getTerminalWiringMethod())
                .set(DistributionMonitor::getCt1, distributionMonitorParam.getCt1())
                .set(DistributionMonitor::getCt2, distributionMonitorParam.getCt2())
                .set(DistributionMonitor::getPt1, distributionMonitorParam.getPt1())
                .set(DistributionMonitor::getPt2, distributionMonitorParam.getPt2())
                .set(DistributionMonitor::getStatisticalInterval, distributionMonitorParam.getStatisticalInterval());

        this.update(lambdaUpdateWrapper);
        overLimitAdd(voltageLevel,distributionMonitorParam.getMonitorId(),true);
        return true;
    }

    @Override
    public List<DistributionMonitor> distributionMonitorList(List<String> disMonitorIds) {
        List<DistributionMonitor> distributionMonitorList = new ArrayList<>();
        LambdaQueryWrapper<DistributionMonitor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DistributionMonitor::getStatus, DataStateEnum.ENABLE.getCode());
        if (CollUtil.isEmpty(disMonitorIds)) {
            distributionMonitorList = this.list(lambdaQueryWrapper);
        } else {
            lambdaQueryWrapper.in(DistributionMonitor::getMonitorId, disMonitorIds);
            distributionMonitorList = this.list(lambdaQueryWrapper);
        }
        return distributionMonitorList;
    }

    @Override
    public DistributionMonitor getDistributionMonitorById(String monitorSort, String id) {
        DictData dictData = dicDataFeignClient.getDicDataById(monitorSort).getData();
        if (Objects.isNull(dictData)) {
            throw new BusinessException(PmsDeviceResponseEnum.NO_LINE_SORT);
        }
        LambdaUpdateWrapper<DistributionMonitor> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(DistributionMonitor::getMonitorSort, monitorSort).eq(DistributionMonitor::getMonitorId, id);
        DistributionMonitor distributionMonitor = this.getOne(lambdaUpdateWrapper);
        return distributionMonitor;
    }

    /**
     * 删除配网监测点
     *
     * @author cdf
     * @date 2022/12/5
     */
    @Override
    public Boolean delDistributionMonitor(TaiZhangDelParam taiZhangDelParam) {
        LambdaQueryWrapper<DistributionMonitor> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        String code = "";

        if (taiZhangDelParam.getType().equals(1)) {
            code = DicDataEnum.ONE_LINE.getCode();
        } else if (taiZhangDelParam.getType().equals(2)) {
            code = DicDataEnum.TWO_LINE.getCode();
        } else {
            code = DicDataEnum.THREE_LINE.getCode();
        }

        DictData dictData = dicDataFeignClient.getDicDataByCode(code).getData();
        if (Objects.isNull(dictData)) {
            throw new BusinessException(PmsDeviceResponseEnum.NO_LINE_SORT);
        }
        lambdaQueryWrapper.eq(DistributionMonitor::getMonitorSort, dictData.getId());
        lambdaQueryWrapper.in(DistributionMonitor::getMonitorId, taiZhangDelParam.getIds());
        return this.remove(lambdaQueryWrapper);
    }

    /**
     * @param orgId
     * @param monitorSort
     * @Description: 按部门获取旗下配网所有监测点(分一类 ， 二类 ， 三类)
     * @Param: [orgId, monitorSort]
     * @return: java.util.List<com.njcn.device.pms.pojo.po.DistributionMonitor>
     * @Author: clam
     * @Date: 2022/12/14
     */
    @Override
    public List<DistributionMonitor> distributionMonitorListByCondition(String orgId, String monitorSort) {
        List<String> deptIds = deptFeignClient.getDepSonSelfCodetByDeptId(orgId).getData();

        List<DistributionMonitor> distributionMonitorList = distributionMonitorMapper.distributionMonitorListByCondition(deptIds, monitorSort);
        return distributionMonitorList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateStatus(MonitorStatus.PwMonitorStatus monitorParam) {

        switch (monitorParam.getType()) {
            case 1:
                //一类监测点 先修改一类监测点，在修改配网监测点
                LambdaUpdateWrapper<Monitor> update = new LambdaUpdateWrapper<>();
                update.set(Monitor::getStatus, monitorParam.getStatus())
                        .in(Monitor::getId, monitorParam.getId());
                monitorMapper.update(null, update);

                this.update(new LambdaUpdateWrapper<DistributionMonitor>().set(DistributionMonitor::getStatus, monitorParam.getStatus())
                        .in(DistributionMonitor::getMonitorId, monitorParam.getId()));
                break;
            case 2:
                //二类监测点(台区)
                LambdaUpdateWrapper<PowerDistributionarea> updateArea = new LambdaUpdateWrapper<>();
                updateArea.set(PowerDistributionarea::getStatus, monitorParam.getStatus())
                        .in(PowerDistributionarea::getId, monitorParam.getId());
                powerDistributionareaMapper.update(null, updateArea);

                this.update(new LambdaUpdateWrapper<DistributionMonitor>().set(DistributionMonitor::getStatus, monitorParam.getStatus())
                        .in(DistributionMonitor::getMonitorId, monitorParam.getId()));
                break;
            case 3:
                //三类监测点
                if (monitorParam.getSmallType() == 1) {
                    //发电
                    LambdaUpdateWrapper<PowerGenerationUser> updateUser = new LambdaUpdateWrapper<>();
                    updateUser.set(PowerGenerationUser::getStatus, monitorParam.getStatus())
                            .in(PowerGenerationUser::getId, monitorParam.getId());
                    powerGenerationUserMapper.update(null, updateUser);

                } else {
                    //用电
                    LambdaUpdateWrapper<PowerClient> updateClient = new LambdaUpdateWrapper<>();
                    updateClient.set(PowerClient::getStatus, monitorParam.getStatus())
                            .in(PowerClient::getId, monitorParam.getId());
                    powerClientMapper.update(null, updateClient);

                }
                this.update(new LambdaUpdateWrapper<DistributionMonitor>().set(DistributionMonitor::getStatus, monitorParam.getStatus())
                        .in(DistributionMonitor::getMonitorId, monitorParam.getId()));
                break;
            default:
                throw new BusinessException(PmsDeviceResponseEnum.NO_LINE_SORT);
        }
        return true;
    }


    /**
     * 获取配网监测点详情
     *
     * @author cdf
     * @date 2023/2/24
     */
    @Override
    public PmsDistributionMonitorDTO distributionMonitorDetail(String monitorId, String monitorSort, Integer ifPowerUser) {
        DictData dictData = dicDataFeignClient.getDicDataById(monitorSort).getData();
        if (Objects.isNull(dictData)) {
            throw new BusinessException(PmsDeviceResponseEnum.NO_LINE_SORT);
        }

        PmsDistributionMonitorDTO pmsDistributionMonitorDTO = new PmsDistributionMonitorDTO();

        switch (dictData.getCode()) {
            case DicDataConstant.ONE_LINE:
                LambdaQueryWrapper<Monitor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(Monitor::getId, monitorId).eq(Monitor::getStatus, DataStateEnum.ENABLE.getCode());
                Monitor monitor = monitorMapper.selectOne(lambdaQueryWrapper);
                pmsDistributionMonitorDTO.setId(monitor.getId());
                pmsDistributionMonitorDTO.setName(monitor.getName());
                pmsDistributionMonitorDTO.setOrgName(monitor.getOrgName());
                pmsDistributionMonitorDTO.setPowerStationName(monitor.getPowerrName());
                pmsDistributionMonitorDTO.setLineName(monitor.getLineName());
                pmsDistributionMonitorDTO.setMonitorSort(1);
                break;
            case DicDataConstant.TWO_LINE:
                LambdaQueryWrapper<PowerDistributionarea> powerDistributionareaLambdaQueryWrapper = new LambdaQueryWrapper<>();
                powerDistributionareaLambdaQueryWrapper.eq(PowerDistributionarea::getId, monitorId).eq(PowerDistributionarea::getStatus, DataStateEnum.ENABLE.getCode());
                PowerDistributionarea powerDistributionarea = powerDistributionareaMapper.selectOne(powerDistributionareaLambdaQueryWrapper);
                pmsDistributionMonitorDTO.setId(powerDistributionarea.getId());
                pmsDistributionMonitorDTO.setName(powerDistributionarea.getName());
                pmsDistributionMonitorDTO.setOrgName(powerDistributionarea.getOrgName());
                pmsDistributionMonitorDTO.setPowerStationName(powerDistributionarea.getPowerrName());
                pmsDistributionMonitorDTO.setLineName(powerDistributionarea.getLineName());
                pmsDistributionMonitorDTO.setMonitorSort(2);
                break;
            case DicDataConstant.THREE_LINE:
                pmsDistributionMonitorDTO.setMonitorSort(3);
                if (ifPowerUser == 1) {
                    LambdaQueryWrapper<PowerGenerationUser> powerGenerationUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
                    powerGenerationUserLambdaQueryWrapper.eq(PowerGenerationUser::getId, monitorId).eq(PowerGenerationUser::getStatus, DataStateEnum.ENABLE.getCode());
                    PowerGenerationUser powerGenerationUser = powerGenerationUserMapper.selectOne(powerGenerationUserLambdaQueryWrapper);
                    if (Objects.nonNull(powerGenerationUser)) {
                        pmsDistributionMonitorDTO.setId(powerGenerationUser.getId());
                        pmsDistributionMonitorDTO.setName(powerGenerationUser.getName());
                        pmsDistributionMonitorDTO.setOrgName(powerGenerationUser.getOrgName());
                        PowerDistributionarea taiqu = powerDistributionareaMapper.selectById(powerGenerationUser.getPlatformId());
                        if (Objects.nonNull(taiqu)) {
                            pmsDistributionMonitorDTO.setPlatformName(taiqu.getName());
                        }
                    }
                } else {
                    LambdaQueryWrapper<PowerClient> powerClientLambdaQueryWrapper = new LambdaQueryWrapper<>();
                    powerClientLambdaQueryWrapper.eq(PowerClient::getId, monitorId).eq(PowerClient::getStatus, DataStateEnum.ENABLE.getCode());
                    PowerClient powerClient = powerClientMapper.selectOne(powerClientLambdaQueryWrapper);
                    if (Objects.nonNull(powerClient)) {
                        pmsDistributionMonitorDTO.setId(powerClient.getId());
                        pmsDistributionMonitorDTO.setName(powerClient.getName());
                        pmsDistributionMonitorDTO.setOrgName(powerClient.getOrgName());
                        PowerDistributionarea taiqu = powerDistributionareaMapper.selectById(powerClient.getPlatformId());
                        if (Objects.nonNull(taiqu)) {
                            pmsDistributionMonitorDTO.setPlatformName(taiqu.getName());
                        }
                    }
                }
                break;
            default:
                break;
        }
        return pmsDistributionMonitorDTO;
    }

    @Override
    public List<PwPmsMonitorDTO> pwMonitorAreaDetailInfo(List<String> monitorIds) {
        return distributionMonitorMapper.pwMonitorAreaDetailInfo(monitorIds);
    }

    @Override
    public List<String> getBaseDisMonitorIds(List<String> orgIds,List<String> monitorSortIds) {
        return distributionMonitorMapper.getBaseDisMonitorIds(orgIds,monitorSortIds);
    }

    @Override
    public List<PwUserMonitorDataVO> getPwUserIds(PwUserMonitorParam pwUserMonitorParam) {
        List<String> deptIds = deptFeignClient.getDepSonSelfCodetByDeptId(pwUserMonitorParam.getOrgId()).getData();
        DictData dictData = dicDataFeignClient.getDicDataByCodeAndType(DicDataEnum.RUN.getCode(), DicDataTypeEnum.LINE_STATE.getCode()).getData();
        pwUserMonitorParam.setMonitorState(dictData.getId());
        return this.distributionMonitorMapper.getDistributionList(deptIds,pwUserMonitorParam);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean savePmsDistributionUser() {
        DictData data = dicDataFeignClient.getDicDataByCode(DicDataEnum.THREE_LINE.getCode()).getData();
        if(ObjectUtil.isNotNull(data)){
            this.baseMapper.deletePmsDistributionMonitor(data.getId(),"1");
            this.baseMapper.insertPmsDistributionUser(data.getId());
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean savePmsDistributionArea() {
        DictData data = dicDataFeignClient.getDicDataByCode(DicDataEnum.TWO_LINE.getCode()).getData();
        if(ObjectUtil.isNotNull(data)){
            this.baseMapper.deletePmsDistributionMonitor(data.getId(),null);
            this.baseMapper.insertPmsDistributionArea(data.getId());
        }
        return true;
    }

    private void overLimitAdd(String voltageLevel, String id,Boolean update) {
        DictData voltageDic = dicDataFeignClient.getDicDataById(voltageLevel).getData();
        float voltageLevelValue = Float.parseFloat(voltageDic.getValue());
        float capacity = COverlimitUtil.getDlCapByVoltageLevel(voltageLevelValue);
        Overlimit overlimit = COverlimitUtil.globalAssemble(voltageLevelValue, capacity, capacity, capacity, 1, 1);
        overlimit.setId(id);
        if(update){
            overlimitMapper.deleteById(id);
        }
        overlimitMapper.insert(overlimit);
    }
}
