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.DateUtil;
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.plugins.pagination.Page;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.device.pms.enums.PmsDeviceResponseEnum;
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.PmsBaseParam;
import com.njcn.device.pms.pojo.param.StatationStatParam;
import com.njcn.device.pms.pojo.param.PmsStatationStatInfoParam;
import com.njcn.device.pms.pojo.param.TerminalQueryParam;
import com.njcn.device.pms.pojo.po.*;
import com.njcn.device.pms.pojo.vo.StatationStatVO;
import com.njcn.device.pms.service.majornetwork.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.event.pojo.vo.DeptLevelVO;
import com.njcn.event.utils.DeptUtil;
import com.njcn.influx.pojo.po.DataHarmPowerP;
import com.njcn.influx.pojo.po.DataV;
import com.njcn.influx.service.DataHarmPowerPService;
import com.njcn.influx.service.IDataVService;
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 com.njcn.web.factory.PageFactory;
import com.njcn.web.pojo.param.BaseParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.security.Key;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hongawen
 * @since 2022-10-14
 */
@Service
@RequiredArgsConstructor
public class StatationStatServiceImpl extends ServiceImpl<StatationStatMapper, StatationStat> implements IStatationStatService {

    private final StatationStatMapper statationStatMapper;

    private final DeptFeignClient deptFeignClient;

    private final TerminalMapper terminalMapper;

    private final PmsGeneratrixWireMapper pmsGeneratrixWireMapper;

    private final IMonitorService iMonitorService;

    private final PowerDistributionareaMapper powerDistributionareaMapper;

    private final DicDataFeignClient dicDataFeignClient;
    private final DistributionMonitorMapper distributionMonitorMapper;
    private final IDataVService iDataVService;
    private final DataHarmPowerPService dataHarmPowerPService;

    private final PmsMidLedgerMapper pmsMidLedgerMapper;


    /**
     * 获取指定的变电站信息
     *
     * @param pmsStatationStatInfoParam 变电站详情数据入参
     * @return 指定的变电站信息
     */
    @Override
    public List<PmsStatationStatInfoDTO> getStatationStatInfo(PmsStatationStatInfoParam pmsStatationStatInfoParam) {
        return statationStatMapper.getStatationStatInfo(pmsStatationStatInfoParam);
    }

    @Override
    public boolean addStatationStat(StatationStatParam statationStatParam) {

        StatationStat statationStatValid = this.getById(statationStatParam.getPowerId());
        if (Objects.nonNull(statationStatValid)) {
            throw new BusinessException(PmsDeviceResponseEnum.STATION_REPEAT);
        }

        //校验同一单位下不能出现同名电站
        LambdaQueryWrapper<StatationStat> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StatationStat::getOrgId, statationStatParam.getOrgId())
                .eq(StatationStat::getPowerName, statationStatParam.getPowerName());
        int count = this.count(lambdaQueryWrapper);
        if (count > 0) {
            throw new BusinessException(PmsDeviceResponseEnum.DEPT_STATION_REPEAT);
        }

        StatationStat statationStat = new StatationStat();
        BeanUtils.copyProperties(statationStatParam, statationStat);
        statationStat.setStatus(DataStateEnum.ENABLE.getCode());
        return this.save(statationStat);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateStatationStat(StatationStatParam statationStatParam) {
        //校验同一单位下不能出现同名电站
        LambdaQueryWrapper<StatationStat> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StatationStat::getOrgId, statationStatParam.getOrgId())
                .eq(StatationStat::getPowerName, statationStatParam.getPowerName())
                .ne(StatationStat::getPowerId, statationStatParam.getPowerId());
        int count = this.count(lambdaQueryWrapper);
        if (count > 0) {
            throw new BusinessException(PmsDeviceResponseEnum.DEPT_STATION_REPEAT);
        }

        StatationStat res = this.getById(statationStatParam.getPowerId());

        StatationStat statationStat = new StatationStat();
        BeanUtils.copyProperties(statationStatParam, statationStat);
        this.updateById(statationStat);

        if(!res.getPowerName().equals(statationStatParam.getPowerName())) {
            //修改单位名称时候要确保其他台账中的电站名同时修改
            LambdaQueryWrapper<Monitor> monitorLambdaQueryWrapper = new LambdaQueryWrapper<>();
            monitorLambdaQueryWrapper.eq(Monitor::getStatus, DataStateEnum.ENABLE.getCode()).eq(Monitor::getPowerrId, statationStat.getPowerId());

            //主网监测点
            List<Monitor> monitorList = iMonitorService.list(monitorLambdaQueryWrapper);
            if (CollectionUtil.isNotEmpty(monitorList)) {
                List<Monitor> monitorListP = monitorList.stream().map(item -> {
                    Monitor monitor = new Monitor();
                    monitor.setPowerrName(statationStat.getPowerName());
                    monitor.setId(item.getId());
                    return monitor;
                }).collect(Collectors.toList());
                iMonitorService.updateBatchById(monitorListP);
            }

            LambdaUpdateWrapper<GeneratrixWire> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.set(GeneratrixWire::getStationName,statationStatParam.getPowerName())
                    .eq(GeneratrixWire::getStationId,statationStatParam.getPowerId());
            pmsGeneratrixWireMapper.update(null,lambdaUpdateWrapper);
            //台区
            LambdaQueryWrapper<PowerDistributionarea> powerDistributionareaLambdaQueryWrapper = new LambdaQueryWrapper<>();
            powerDistributionareaLambdaQueryWrapper.eq(PowerDistributionarea::getStatus, DataStateEnum.ENABLE.getCode()).eq(PowerDistributionarea::getPowerStationId, statationStat.getPowerId());

            List<PowerDistributionarea> powerDistributionareaList = powerDistributionareaMapper.selectList(powerDistributionareaLambdaQueryWrapper);
            if (CollectionUtil.isNotEmpty(powerDistributionareaList)) {
                powerDistributionareaList.forEach(item -> {
                    PowerDistributionarea powerDistributionarea = new PowerDistributionarea();
                    powerDistributionarea.setPowerrName(statationStat.getPowerName());
                    powerDistributionarea.setId(item.getId());
                    powerDistributionareaMapper.updateById(powerDistributionarea);
                });
            }
            //装置
            //台区
            LambdaQueryWrapper<PmsTerminal> pmsTerminalLambdaQueryWrapper = new LambdaQueryWrapper<>();
            pmsTerminalLambdaQueryWrapper.eq(PmsTerminal::getStatus, DataStateEnum.ENABLE.getCode()).eq(PmsTerminal::getPowerStationId, statationStat.getPowerId());

            List<PmsTerminal> pmsTerminalList = terminalMapper.selectList(pmsTerminalLambdaQueryWrapper);
            if (CollectionUtil.isNotEmpty(pmsTerminalList)) {
                pmsTerminalList.forEach(item -> {
                    PmsTerminal pmsTerminalP = new PmsTerminal();
                    pmsTerminalP.setPowerrName(statationStat.getPowerName());
                    pmsTerminalP.setId(item.getId());
                    terminalMapper.updateById(pmsTerminalP);
                });
            }
        }
        return true;
    }


    @Override
    public boolean delStatationStat(List<String> statationStatIds) {
        //删除前需要判断是否存在绑定关系

        //装置是否存在绑定
        LambdaQueryWrapper<PmsTerminal> terminalLambdaQueryWrapper = new LambdaQueryWrapper<>();
        terminalLambdaQueryWrapper.eq(PmsTerminal::getStatus, DataStateEnum.ENABLE.getCode()).in(PmsTerminal::getPowerStationId, statationStatIds);
        int terCount = terminalMapper.selectCount(terminalLambdaQueryWrapper);
        if (terCount > 0) {
            throw new BusinessException(PmsDeviceResponseEnum.TERMINAL_BIND_FIND);
        }
        //线路
        LambdaQueryWrapper<GeneratrixWire> wireLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wireLambdaQueryWrapper.eq(GeneratrixWire::getStatus, DataStateEnum.ENABLE.getCode()).in(GeneratrixWire::getStationId, statationStatIds);
        int wCount = pmsGeneratrixWireMapper.selectCount(wireLambdaQueryWrapper);
        if (wCount > 0) {
            throw new BusinessException(PmsDeviceResponseEnum.WIRE_BIND_FIND);
        }

        return this.removeByIds(statationStatIds);
    }

    @Override
    public StatationStat getStatationStatById(String statationStatId) {
        return this.getById(statationStatId);
    }

    @Override
    public List<StatationStat> getStatationStatList(PmsBaseParam pmsBaseParam) {
        LambdaQueryWrapper<StatationStat> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(StatationStat::getCreateTime);
        if (StrUtil.isNotBlank(pmsBaseParam.getDeptId())) {
            Dept dept = deptFeignClient.getDeptById(pmsBaseParam.getDeptId()).getData();
            lambdaQueryWrapper.eq(StatationStat::getOrgId, dept.getCode());
        }

        if (StrUtil.isNotBlank(pmsBaseParam.getDeptCode())) {
            lambdaQueryWrapper.eq(StatationStat::getOrgId, pmsBaseParam.getDeptCode());
        }

        return this.list(lambdaQueryWrapper);
    }

    @Override
    public Page<StatationStatVO> getStatationStatPageList(TerminalQueryParam baseParam) {
        List<String> orgIds = new ArrayList<>();
        if(StrUtil.isNotBlank(baseParam.getOrgId())){
            orgIds.addAll(deptFeignClient.getDepSonSelfCodetByDeptId(baseParam.getOrgId()).getData());
        }
        return this.baseMapper.getStatationStatPageList(new Page<>(PageFactory.getPageNum(baseParam), PageFactory.getPageSize(baseParam)), baseParam,orgIds);
    }

    /**
     * 获取变电站信息
     *
     * @param param 条件参数
     * @return com.njcn.common.pojo.response.HttpResult<java.util.List < com.njcn.device.pms.pojo.po.PmsStatationStat>>
     * @author yzh
     * @date 2022/11/21
     */
    @Override
    public List<StatationStat> getPowerInfo(PmsStatationStatInfoParam param) {
        LambdaQueryWrapper<StatationStat> lqw = new LambdaQueryWrapper<>();
        lqw.in(CollUtil.isNotEmpty(param.getOrgIds()) , StatationStat::getOrgId, param.getOrgIds());
        lqw.in(CollUtil.isNotEmpty(param.getPowerIds()), StatationStat::getPowerId, param.getPowerIds());
        lqw.like(StrUtil.isNotBlank(param.getPowerName()), StatationStat::getPowerName, param.getPowerName());
        lqw.in(CollUtil.isNotEmpty(param.getPowerVoltageLevel()), StatationStat::getVoltageLevel, param.getPowerVoltageLevel());
        return this.list(lqw);
    }

    @Override
    public List<Integer> statisticsMotion(String orgId) {
        List<Integer> num = new ArrayList<>();
        num.add(0);
        num.add(0);
        num.add(0);
        List<DeptDTO> deptInfos = deptFeignClient.getDeptDescendantIndexes(orgId, Stream.of(0, 1).collect(Collectors.toList())).getData();
        List<String> orgIDs = deptInfos.stream().map(DeptDTO::getCode).distinct().collect(Collectors.toList());
        //获取电压指定电压id
        Map<String, String> voltageIDs = voltageLevel();
        //查询变电站信息
        List<StatationStat> list = this.list(new QueryWrapper<StatationStat>()
                .select(" Voltage_Level,COUNT(Power_Id) as Should_Be_Num")
                .eq("Status", 1)
                .in("Org_Id", orgIDs)
                .in("Voltage_Level", voltageIDs.keySet())
                .groupBy("Voltage_Level")
        );
        Map<String, Integer> infoMap = list.stream().collect(Collectors.toMap(StatationStat::getVoltageLevel, StatationStat::getShouldBeNum));
        voltageIDs.forEach((key, value) -> {
            if (infoMap.containsKey(key)) {
                Integer integer = infoMap.get(key);
                if (value.equals(DicDataEnum.DY_500KV.getCode())) {
                    num.set(0, num.get(0) + integer);
                }
                if (value.equals(DicDataEnum.DY_DC_500kV.getCode())) {
                    num.set(0, num.get(0) + integer);
                }
                if (value.equals(DicDataEnum.DY_220KV.getCode())) {
                    num.set(1, integer);
                }
                if (value.equals(DicDataEnum.DY_110KV.getCode())) {
                    num.set(2, integer);
                }
            }

        });
        return num;
    }

    @Override
    public List<List<StatationStatVO.StatationParam>> orgStatisticsMotion(String orgId) {
        List<List<StatationStatVO.StatationParam>> info = new LinkedList<>();
        List<StatationStatVO.StatationParam> kv500 = new LinkedList<>();
        List<StatationStatVO.StatationParam> kv220 = new LinkedList<>();
        List<StatationStatVO.StatationParam> kv110 = new LinkedList<>();
        //查询当前部门下子节点信息
        List<DeptDTO> deptInfos = deptFeignClient.getDeptDescendantIndexes(orgId, Stream.of(0, 1).collect(Collectors.toList())).getData();
        List<DeptLevelVO> deptLevelVOList = DeptUtil.getDeptLevelVOList(deptInfos, orgId);
        List<String> orgIDs = deptInfos.stream().map(DeptDTO::getCode).collect(Collectors.toList());

        //获取电压指定电压id
        Map<String, String> voltageIDs = voltageLevel();

        //查询变电站信息
        List<StatationStat> list = this.list(new QueryWrapper<StatationStat>()
                .select(" Org_Id,Voltage_Level,COUNT(Power_Id) as Should_Be_Num")
                .eq("Status", 1)
                .in("Org_Id", orgIDs)
                .in("Voltage_Level", voltageIDs.keySet())
                .groupBy("Org_Id,Voltage_Level")
        );

        for (DeptLevelVO deptLevelVO : deptLevelVOList) {
            List<String> ids = deptLevelVO.getDeptList().stream().map(DeptLevelVO::getCode).collect(Collectors.toList());
            ids.add(deptLevelVO.getCode());

            List<StatationStat> contain = list.stream().filter(x -> ids.contains(x.getOrgId())).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(contain)) {

                Map<String, Long> infoMap = contain.stream().collect(Collectors.groupingBy(StatationStat::getVoltageLevel, Collectors.summingLong(StatationStat::getShouldBeNum)));
                StatationStatVO.StatationParam kv500Param = new StatationStatVO.StatationParam();
                StatationStatVO.StatationParam kv220Param = new StatationStatVO.StatationParam();
                StatationStatVO.StatationParam kv110Param = new StatationStatVO.StatationParam();

                kv500Param.setOrgName(deptLevelVO.getName());
                kv500Param.setNum(0L);
                kv220Param.setOrgName(deptLevelVO.getName());
                kv220Param.setNum(0L);
                kv110Param.setOrgName(deptLevelVO.getName());
                kv110Param.setNum(0L);

                voltageIDs.forEach((key, value) -> {
                    if (infoMap.containsKey(key)) {
                        Long integer = infoMap.get(key);
                        if (value.equals(DicDataEnum.DY_500KV.getCode())) {
                            kv500Param.setNum(kv500Param.getNum() + integer);
                        }
                        if (value.equals(DicDataEnum.DY_DC_500kV.getCode())) {
                            kv500Param.setNum(kv500Param.getNum() + integer);
                        }
                        if (value.equals(DicDataEnum.DY_220KV.getCode())) {
                            kv220Param.setNum(integer);
                        }
                        if (value.equals(DicDataEnum.DY_110KV.getCode())) {
                            kv110Param.setNum(integer);
                        }

                    }
                });
                kv500.add(kv500Param);
                kv220.add(kv220Param);
                kv110.add(kv110Param);
            } else {
                StatationStatVO.StatationParam param = new StatationStatVO.StatationParam();
                param.setOrgName(deptLevelVO.getName());
                param.setNum(0L);
                kv500.add(param);
                kv220.add(param);
                kv110.add(param);
            }

        }
        info.add(kv500);
        info.add(kv220);
        info.add(kv110);
        return info;
    }

    @Override
    public List<StatationStat> getStatationStatList(String orgID) {
        if (StrUtil.isBlank(orgID)) {
            throw new BusinessException(PmsDeviceResponseEnum.NO_ID_ORG);
        }
        List<DeptDTO> deptInfos = deptFeignClient.getDeptDescendantIndexes(orgID, Stream.of(0, 1).collect(Collectors.toList())).getData();
        List<String> orgIDs = deptInfos.stream().map(DeptDTO::getCode).collect(Collectors.toList());

        LambdaQueryWrapper<StatationStat> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(StatationStat::getOrgId, orgIDs);
        lambdaQueryWrapper.orderByDesc(StatationStat::getCreateTime);
        return this.list(lambdaQueryWrapper);
    }

    @Override
    public List<StatationStatVO.Curve> getStatationStatCurve(String subID, String voltageLevel, Integer type) {
        List<StatationStatVO.Curve> curves = new ArrayList<>();
        if (StrUtil.isBlank(subID)) {
            throw new BusinessException(PmsDeviceResponseEnum.NO_ID_STATION);
        }
        //获取主网信息,没有则获取配网信息
        List<Monitor> list = iMonitorService.list(new LambdaQueryWrapper<Monitor>()
                .eq(Monitor::getPowerrId, subID)
                .eq(Monitor::getVoltageLevel, voltageLevel)
        );
        if (CollUtil.isNotEmpty(list)) {
            String lineId = list.get(0).getLineId();
            addCurves(lineId, type, curves);

        } else {
            List<PmsMonitorBaseDTO> disMonitorAllList = distributionMonitorMapper.getDisMonitorAllList(subID, 1);
            List<PmsMonitorBaseDTO> disMonitors = disMonitorAllList.stream().filter(x -> x.equals(voltageLevel)).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(disMonitors)) {
                String monitorId = disMonitors.get(0).getMonitorId();
                addCurves(monitorId, type, curves);
            }
        }
        if(CollUtil.isNotEmpty(curves)){
            curves.sort(Comparator.comparing(r -> r.getTime()));
        }
        return curves;
    }

    @Override
    public List<PmsMidLedger> getStationByDeptCode(String deptCode) {
        LambdaQueryWrapper<PmsMidLedger> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PmsMidLedger::getSectionId,deptCode).eq(PmsMidLedger::getLevel,0)
                .eq(PmsMidLedger::getState,DataStateEnum.ENABLE.getCode()).last("and id not in (select Mid_Station_Id from pms_statation_stat where Mid_Station_Id is not null)");
        List<PmsMidLedger> pmsMidLedgerList =pmsMidLedgerMapper.selectList(lambdaQueryWrapper);
        return pmsMidLedgerList;
    }

    @Override
    public Boolean batchSync() {
        LambdaQueryWrapper<PmsMidLedger> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PmsMidLedger::getLevel,0).eq(PmsMidLedger::getState,DataStateEnum.ENABLE.getCode());
        List<PmsMidLedger> pmsMidLedgerList = pmsMidLedgerMapper.selectList(lambdaQueryWrapper);

        List<StatationStat> statationStatList = statationStatMapper.selectList(new LambdaQueryWrapper<>());

        for(StatationStat statationStat :statationStatList){

            for(PmsMidLedger pmsMidLedger :pmsMidLedgerList){
                if(statationStat.getOrgId().equals(pmsMidLedger.getSectionId())){
                    if(pmsMidLedger.getName().equals(statationStat.getPowerName())){
                        statationStat.setMidStationId(pmsMidLedger.getId());
                        break;
                    }
                }
            }
        }

        List<StatationStat> poList = statationStatList.stream().filter(item->Objects.nonNull(item.getMidStationId())).collect(Collectors.toList());

        for(StatationStat statationStat : poList){
            LambdaUpdateWrapper<StatationStat> lambdaUpdateWrapper = new LambdaUpdateWrapper();
            lambdaUpdateWrapper.set(StatationStat::getMidStationId,statationStat.getMidStationId()).eq(StatationStat::getPowerId,statationStat.getPowerId());
            this.update(lambdaUpdateWrapper);
        }
        System.out.println("5555");


        return true;
    }

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

        voltageLevelMap.forEach((key, value) -> {
            if (key.equals(DicDataEnum.DY_500KV.getCode())) {
                voltageIDs.put(value, key);
            }
            if (key.equals(DicDataEnum.DY_DC_500kV.getCode())) {
                voltageIDs.put(value, key);
            }
            if (key.equals(DicDataEnum.DY_220KV.getCode())) {
                voltageIDs.put(value, key);
            }
            if (key.equals(DicDataEnum.DY_110KV.getCode())) {
                voltageIDs.put(value, key);
            }
        });
        return voltageIDs;
    }

    /**
     * 对象添加
     *
     * @param LineID 监测点id ff2d9674c1f1ecce7f33a5bf17fc4f2d
     * @param type   0=谐波 1=总有功
     * @param curves 初始集合
     * @return
     */
    private List<StatationStatVO.Curve> addCurves(String LineID, Integer type, List<StatationStatVO.Curve> curves) {
        if (type == 0) {
            List<DataV> dataV = iDataVService.getHarmonicDataV(LineID,
                    DateUtil.beginOfDay(DateUtil.date()).toString()
                    , DateUtil.endOfDay(DateUtil.date()).toString());
            if (CollUtil.isNotEmpty(dataV)) {
                Map<Instant, Double> collect = dataV.stream().collect(Collectors.toMap(DataV::getTime, DataV::getVThd, (c1, c2) -> c1 > c2 ? c1 : c2));
                collect.forEach((key, value) -> {
                    StatationStatVO.Curve curve = new StatationStatVO.Curve();
                    curve.setTime(Date.from(key));
                    curve.setNum(BigDecimal.valueOf(value).setScale(3, RoundingMode.HALF_UP));
                    curves.add(curve);
                });
            }
        } else {
            List<DataHarmPowerP> dataHarmPowerP = dataHarmPowerPService.getHarmonicPowerP(LineID,
                    DateUtil.beginOfDay(DateUtil.date()).toString()
                    , DateUtil.endOfDay(DateUtil.date()).toString());
            if (CollUtil.isNotEmpty(dataHarmPowerP)) {
                Map<Instant, Double> dataHarmPowerPMap = dataHarmPowerP.stream().collect(Collectors.toMap(DataHarmPowerP::getTime, DataHarmPowerP::getP, (c1, c2) -> c1 > c2 ? c1 : c2));
                dataHarmPowerPMap.forEach((key, value) -> {
                    StatationStatVO.Curve curve = new StatationStatVO.Curve();
                    curve.setTime(Date.from(key));
                    curve.setNum(BigDecimal.valueOf(value).setScale(3, RoundingMode.HALF_UP));
                    curves.add(curve);
                });
            }
        }
        return curves;
    }
}
