package com.njcn.prepare.harmonic.service.mysql.Impl.line;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.device.biz.pojo.po.Overlimit;
import com.njcn.device.pq.api.GeneralDeviceInfoClient;
import com.njcn.device.pq.api.LineFeignClient;
import com.njcn.device.pq.api.OverLimitClient;
import com.njcn.harmonic.pojo.po.day.*;
import com.njcn.influx.deprecated.InfluxDBPublicParam;
import com.njcn.influx.pojo.po.DataPlt;
import com.njcn.influx.pojo.po.DataV;
import com.njcn.influx.utils.InfluxDbUtils;
import com.njcn.prepare.harmonic.enums.PrepareResponseEnum;
import com.njcn.prepare.harmonic.mapper.mysql.day.*;
import com.njcn.prepare.harmonic.pojo.bo.CalculatedParam;
import com.njcn.prepare.harmonic.pojo.mysql.po.line.MpSurplusAbnormalD;
import com.njcn.prepare.harmonic.service.mysql.line.HarmonicMetricService;
import com.njcn.prepare.harmonic.service.mysql.line.NormalLimitService;
import com.njcn.user.api.DeptFeignClient;
import com.njcn.user.pojo.po.Dept;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.influxdb.dto.QueryResult;
import org.influxdb.impl.InfluxDBResultMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 稳态指标评估
 * @author xiaoyao
 * @version 1.0.0
 * @createTime 2022/11/3 9:08
 */
@Slf4j
@Service
@AllArgsConstructor
public class HarmonicMetricServiceImpl extends MppServiceImpl<RStatAssesDMapper, RStatAssesDPO> implements HarmonicMetricService {

    private final InfluxDbUtils influxDbUtils;
    private final LineFeignClient lineFeignClient;
    private final GeneralDeviceInfoClient generalDeviceInfoClient;
    private final DeptFeignClient deptFeignClient;
    private final OverLimitClient overLimitClient;
    private final RStatDataHarmRateVDMapper rStatDataHarmRateVDMapper;
    private final RStatDataInHarmVDMapper rStatDataInHarmVDMapper;
    private final RStatDataVDMapper rStatDataVDMapper;
    private final RStatDataIDMapper rStatDataIDMapper;
    private final RStatDataPltDMapper rStatDataPltDMapper;
    private final NormalLimitService normalLimitService;




    @Override
    public void newMetricData(CalculatedParam calculatedParam) {
        List<RStatAssesDPO> list = new ArrayList<>();
        List<String> lineIdList;
        if (CollUtil.isEmpty(calculatedParam.getIdList())){
            List<Overlimit> overLimitList = getAllLineData();
            lineIdList = overLimitList.stream().map(Overlimit::getId).collect(Collectors.toList());
        }else {
            lineIdList = new ArrayList<>(calculatedParam.getIdList());
        }
        Date dateOut = DateUtil.parse(calculatedParam.getDataDate());
        for (String lineId : lineIdList){
            Map<String, Object> fields = getMetricData(lineId,calculatedParam.getDataDate());
            if(CollUtil.isNotEmpty(fields)) {
                RStatAssesDPO rStatAssesDpo = new RStatAssesDPO();
                rStatAssesDpo.setTime(dateOut);
                rStatAssesDpo.setLineId(lineId);
                rStatAssesDpo.setVuDev(Double.parseDouble(fields.get("vu_dev").toString()));
                rStatAssesDpo.setFreqDev(Double.parseDouble(fields.get("freq_dev").toString()));
                rStatAssesDpo.setDataPlt(Double.parseDouble(fields.get("data_plt").toString()));
                rStatAssesDpo.setVUnbalanceCp95(Double.parseDouble(fields.get("v_unbalance_cp95").toString()));
                rStatAssesDpo.setVThdCp95(Double.parseDouble(fields.get("v_thd_cp95").toString()));
                list.add(rStatAssesDpo);
            }
        }
        if(CollUtil.isNotEmpty(list)){
            this.saveOrUpdateBatchByMultiId(list,50);
        }
    }

    @Override
    public void rMpSurplusAbnormalD(CalculatedParam calculatedParam) {
        List<String> lineIds;
        List<MpSurplusAbnormalD> all = new ArrayList<>();

        if (CollectionUtils.isEmpty(calculatedParam.getIdList())) {
            Dept dept = deptFeignClient.getRootDept().getData();
            lineIds = generalDeviceInfoClient.deptGetRunLine(dept.getId()).getData();
        } else {
            //不为空则为补招
            lineIds = calculatedParam.getIdList();
        }
        if (CollUtil.isNotEmpty(lineIds)) {
            List<Map<String, Object>> overLimitMap = overLimitClient.getLimitMapsByLineIds(lineIds).getData();
            Map<String, Map<String, Object>> ovMap = overLimitMap.stream().collect(Collectors.toMap(item -> item.get("id").toString(), Function.identity()));

            for (String lineIndex : lineIds) {
                boolean flag = false;
                //获取当前监测点的限值详情
                if (!ovMap.containsKey(lineIndex)) {
                    log.error(LocalDateTime.now() + "--监测点<" + lineIndex + ">不存在限值！");
                    throw new BusinessException(PrepareResponseEnum.LIMIT_RATE_MISSING);
                }
                Map<String, Object> overLimit = ovMap.get(lineIndex);

                //谐波电压
                LambdaQueryWrapper<RStatDataHarmrateVDPO> lambdaQueryV = new LambdaQueryWrapper<>();
                lambdaQueryV.eq(RStatDataHarmrateVDPO::getLineId, lineIndex).eq(RStatDataHarmrateVDPO::getTime, calculatedParam.getDataDate() + InfluxDBPublicParam.START_TIME);
                List<Map<String, Object>> harmVMap = rStatDataHarmRateVDMapper.selectMaps(lambdaQueryV);
                Map<String, List<Map<String, Object>>> tenMap = harmVMap.stream().collect(Collectors.groupingBy(item -> item.get("phaseType").toString()));

                //谐波电流幅值
                LambdaQueryWrapper<RStatDataIDPO> lambdaQueryI = new LambdaQueryWrapper<>();
                lambdaQueryI.eq(RStatDataIDPO::getLineId, lineIndex).eq(RStatDataIDPO::getTime, calculatedParam.getDataDate() + InfluxDBPublicParam.START_TIME);
                List<Map<String, Object>> harmIMap = rStatDataIDMapper.selectMaps(lambdaQueryI);
                Map<String, List<Map<String, Object>>> iMap = harmIMap.stream().collect(Collectors.groupingBy(item -> item.get("phaseType").toString()));

                //间谐波电压
                LambdaQueryWrapper<RStatDataInharmVDPO> lambdaQueryInV = new LambdaQueryWrapper<>();
                lambdaQueryInV.eq(RStatDataInharmVDPO::getLineId, lineIndex).eq(RStatDataInharmVDPO::getTime, calculatedParam.getDataDate() + InfluxDBPublicParam.START_TIME);
                List<Map<String, Object>> harmInVMap = rStatDataInHarmVDMapper.selectMaps(lambdaQueryInV);
                Map<String, List<Map<String, Object>>> inMap = harmInVMap.stream().collect(Collectors.groupingBy(item -> item.get("phaseType").toString()));

                //三相电压
                LambdaQueryWrapper<RStatDataVDPO> lambdaQueryDataV = new LambdaQueryWrapper<>();
                lambdaQueryDataV.eq(RStatDataVDPO::getLineId, lineIndex).eq(RStatDataVDPO::getTime, calculatedParam.getDataDate() + InfluxDBPublicParam.START_TIME);
                List<RStatDataVDPO> rStatDataVDPOList = rStatDataVDMapper.selectList(lambdaQueryDataV);
                Map<String, List<RStatDataVDPO>> dataVMap = rStatDataVDPOList.stream().collect(Collectors.groupingBy(RStatDataVDPO::getPhaseType));

                //电流
                LambdaQueryWrapper<RStatDataIDPO> lambdaQueryDataI = new LambdaQueryWrapper<>();
                lambdaQueryDataI.eq(RStatDataIDPO::getLineId, lineIndex).eq(RStatDataIDPO::getTime, calculatedParam.getDataDate() + InfluxDBPublicParam.START_TIME);
                List<RStatDataIDPO> rStatDataIDPOList = rStatDataIDMapper.selectList(lambdaQueryDataI);
                Map<String, List<RStatDataIDPO>> dataIMap = rStatDataIDPOList.stream().collect(Collectors.groupingBy(RStatDataIDPO::getPhaseType));

                //长时闪变
                LambdaQueryWrapper<RStatDataPltDPO> lambdaQueryDataPlt = new LambdaQueryWrapper<>();
                lambdaQueryDataPlt.eq(RStatDataPltDPO::getLineId, lineIndex).eq(RStatDataPltDPO::getTime, calculatedParam.getDataDate() + InfluxDBPublicParam.START_TIME);
                List<RStatDataPltDPO> rStatDataPltDPOList = rStatDataPltDMapper.selectList(lambdaQueryDataPlt);
                Map<String, List<RStatDataPltDPO>> dataPltMap = rStatDataPltDPOList.stream().collect(Collectors.groupingBy(RStatDataPltDPO::getPhaseType));

                String phasic = "";
                for (int phase = 1; phase < 5; phase++) {
                    switch (phase) {
                        case 1:
                            phasic = "A";
                            break;
                        case 2:
                            phasic = "B";
                            break;
                        case 3:
                            phasic = "C";
                            break;
                        case 4:
                            phasic = "T";
                            break;
                    }

                    MpSurplusAbnormalD mpSurplusAbnormalCp95 = new MpSurplusAbnormalD();
                    mpSurplusAbnormalCp95.setPhasicType(phasic);
                    mpSurplusAbnormalCp95.setValueType("CP95");
                    mpSurplusAbnormalCp95.setLineId(lineIndex);
                    mpSurplusAbnormalCp95.setTimeId(LocalDateTime.parse(calculatedParam.getDataDate() + InfluxDBPublicParam.START_TIME, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

                    MpSurplusAbnormalD mpSurplusAbnormalAvg = new MpSurplusAbnormalD();
                    mpSurplusAbnormalAvg.setPhasicType(phasic);
                    mpSurplusAbnormalAvg.setValueType("AVG");
                    mpSurplusAbnormalAvg.setLineId(lineIndex);
                    mpSurplusAbnormalAvg.setTimeId(LocalDateTime.parse(calculatedParam.getDataDate() + InfluxDBPublicParam.START_TIME, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

                    MpSurplusAbnormalD mpSurplusAbnormalMax = new MpSurplusAbnormalD();
                    mpSurplusAbnormalMax.setPhasicType(phasic);
                    mpSurplusAbnormalMax.setValueType("MAX");
                    mpSurplusAbnormalMax.setLineId(lineIndex);
                    mpSurplusAbnormalMax.setTimeId(LocalDateTime.parse(calculatedParam.getDataDate() + InfluxDBPublicParam.START_TIME, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

                    MpSurplusAbnormalD mpSurplusAbnormalMin = new MpSurplusAbnormalD();
                    mpSurplusAbnormalMin.setPhasicType(phasic);
                    mpSurplusAbnormalMin.setValueType("MIN");
                    mpSurplusAbnormalMin.setLineId(lineIndex);
                    mpSurplusAbnormalMin.setTimeId(LocalDateTime.parse(calculatedParam.getDataDate() + InfluxDBPublicParam.START_TIME, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

                    if (dataVMap.containsKey(phasic)) {
                        List<RStatDataVDPO> mapList = dataVMap.get(phasic);
                        Map<String, RStatDataVDPO> vMap = mapList.stream().collect(Collectors.toMap(RStatDataVDPO::getValueType, Function.identity()));
                        //三项电压不平衡度
                        if (vMap.get("CP95").getVUnbalance() > (Double) overLimit.get("ubalance")) {
                            mpSurplusAbnormalCp95.setUbalance(vMap.get("CP95").getVUnbalance().floatValue());
                            mpSurplusAbnormalAvg.setUbalance(vMap.get("AVG").getVUnbalance().floatValue());
                            mpSurplusAbnormalMin.setUbalance(vMap.get("MIN").getVUnbalance().floatValue());
                            mpSurplusAbnormalMax.setUbalance(vMap.get("MAX").getVUnbalance().floatValue());
                            flag = true;
                        }
                        //电压畸变
                        if (vMap.get("CP95").getVThd() > (Double) overLimit.get("uaberrance")) {
                            mpSurplusAbnormalCp95.setUaberrance(vMap.get("CP95").getVThd().floatValue());
                            mpSurplusAbnormalAvg.setUaberrance(vMap.get("AVG").getVThd().floatValue());
                            mpSurplusAbnormalMin.setUaberrance(vMap.get("MIN").getVThd().floatValue());
                            mpSurplusAbnormalMax.setUaberrance(vMap.get("MAX").getVThd().floatValue());
                            flag = true;
                        }
                        //频率偏差
                        if (Math.abs(vMap.get("MAX").getFreqDev()) > (Double) overLimit.get("freq_dev") || Math.abs(vMap.get("MIN").getFreqDev()) > (Double) overLimit.get("freq_dev")) {
                            mpSurplusAbnormalCp95.setFreq(vMap.get("CP95").getFreqDev().floatValue());
                            mpSurplusAbnormalAvg.setFreq(vMap.get("AVG").getFreqDev().floatValue());
                            mpSurplusAbnormalMin.setFreq(vMap.get("MIN").getFreqDev().floatValue());
                            mpSurplusAbnormalMax.setFreq(vMap.get("MAX").getFreqDev().floatValue());
                            flag = true;
                        }
                        //电压上偏差
                        if (Math.abs(vMap.get("MAX").getVlDev()) > (Double) overLimit.get("voltage_dev")) {
                            mpSurplusAbnormalCp95.setVoltageDev(vMap.get("CP95").getVlDev().floatValue());
                            mpSurplusAbnormalAvg.setVoltageDev(vMap.get("AVG").getVlDev().floatValue());
                            mpSurplusAbnormalMin.setVoltageDev(vMap.get("MIN").getVlDev().floatValue());
                            mpSurplusAbnormalMax.setVoltageDev(vMap.get("MAX").getVlDev().floatValue());
                            flag = true;
                        }

                    }
                    //负序电流
                    if (dataIMap.containsKey(phasic)) {
                        List<RStatDataIDPO> mapList = dataIMap.get(phasic);
                        Map<String, RStatDataIDPO> inegMap = mapList.stream().collect(Collectors.toMap(RStatDataIDPO::getValueType, Function.identity()));
                        if (inegMap.get("CP95").getINeg() > (Double) overLimit.get("i_neg")) {
                            mpSurplusAbnormalCp95.setINeg(inegMap.get("CP95").getINeg().floatValue());
                            mpSurplusAbnormalAvg.setINeg(inegMap.get("AVG").getINeg().floatValue());
                            mpSurplusAbnormalMin.setINeg(inegMap.get("MIN").getINeg().floatValue());
                            mpSurplusAbnormalMax.setINeg(inegMap.get("MAX").getINeg().floatValue());
                            flag = true;
                        }
                    }
                    //长时闪变
                    if (dataPltMap.containsKey(phasic)) {
                        List<RStatDataPltDPO> mapList = dataPltMap.get(phasic);
                        Map<String, RStatDataPltDPO> pltMap = mapList.stream().collect(Collectors.toMap(RStatDataPltDPO::getValueType, Function.identity()));
                        RStatDataPltDPO maxObj = pltMap.get("MAX");
                        Double flickerLimit = (Double) overLimit.get("flicker");
                        if (maxObj != null && flickerLimit != null && (maxObj).getPlt() > flickerLimit) {
                            if (pltMap.get("CP95") != null) {
                                mpSurplusAbnormalCp95.setFlicker(pltMap.get("CP95").getPlt().floatValue());
                            }
                            if (pltMap.get("AVG") != null) {
                                mpSurplusAbnormalAvg.setFlicker(pltMap.get("AVG").getPlt().floatValue());
                            }
                            if (pltMap.get("MIN") != null) {
                                mpSurplusAbnormalMin.setFlicker(pltMap.get("MIN").getPlt().floatValue());
                            }
                            mpSurplusAbnormalMax.setFlicker((maxObj).getPlt().floatValue());
                            flag = true;
                        }
//                        if (pltMap.get("MAX").getPlt() > (Double) overLimit.get("flicker")) {
//                            mpSurplusAbnormalCp95.setFlicker(pltMap.get("CP95").getPlt().floatValue());
//                            mpSurplusAbnormalAvg.setFlicker(pltMap.get("AVG").getPlt().floatValue());
//                            mpSurplusAbnormalMin.setFlicker(pltMap.get("MIN").getPlt().floatValue());
//                            mpSurplusAbnormalMax.setFlicker(pltMap.get("MAX").getPlt().floatValue());
//                            flag = true;
//                        }
                    }
                    //谐波电压
                    dealData(tenMap,phasic,overLimit,flag,mpSurplusAbnormalCp95,mpSurplusAbnormalAvg,mpSurplusAbnormalMax,mpSurplusAbnormalMin,2,26,1,"uharm_","v_",all);
                    //谐波电流
                    dealData(iMap,phasic,overLimit,flag,mpSurplusAbnormalCp95,mpSurplusAbnormalAvg,mpSurplusAbnormalMax,mpSurplusAbnormalMin,2,26,2,"iharm_","i_",all);
                    //间谐波电压
                    dealData(inMap,phasic,overLimit,flag,mpSurplusAbnormalCp95,mpSurplusAbnormalAvg,mpSurplusAbnormalMax,mpSurplusAbnormalMin,1,17,3,"inuharm_","v_",all);
                    all.add(mpSurplusAbnormalCp95);
                    all.add(mpSurplusAbnormalAvg);
                    all.add(mpSurplusAbnormalMax);
                    all.add(mpSurplusAbnormalMin);
                }
            }
        }
        if (CollUtil.isNotEmpty(all)) {
            //入库操作
            normalLimitService.saveOrUpdateBatchByMultiId(all);
        }
    }

    /**
     *
     * @author cdf
     * @date 2023/8/23
     */
    private void dealData(Map<String, List<Map<String, Object>>> inMap,
                          String phasic,Map<String, Object> overLimit,
                          Boolean flag,MpSurplusAbnormalD mpSurplusAbnormalCp95,MpSurplusAbnormalD mpSurplusAbnormalAvg,MpSurplusAbnormalD mpSurplusAbnormalMax,MpSurplusAbnormalD mpSurplusAbnormalMin
            ,int start,int count,int type,String overTag,String temTag, List<MpSurplusAbnormalD> all){
        if (inMap.containsKey(phasic)) {
            List<Map<String, Object>> mapList = inMap.get(phasic);
            Map<String, Map<String, Object>> harmMap = mapList.stream().collect(Collectors.toMap(item -> item.get("value_type").toString(), Function.identity()));
            Map<String, Object> mCP95 = harmMap.get("CP95");
            Map<String, Object> mAVG = harmMap.get("AVG");
            Map<String, Object> mMAX = harmMap.get("MAX");
            Map<String, Object> mMIN = harmMap.get("MIN");
            Map<String, Object> moreValueCp95 = new HashMap<>();
            Map<String, Object> moreValueAvg = new HashMap<>();
            Map<String, Object> moreValueMax = new HashMap<>();
            Map<String, Object> moreValueMin = new HashMap<>();
            for (int i = start; i < count; i++) {
                BigDecimal hv = (BigDecimal) mCP95.get(temTag + i);
                String t = overTag + i;
                BigDecimal over = BigDecimal.valueOf((Double) overLimit.get(t));
                if (hv.compareTo(over) > 0) {
                    moreValueCp95.put(temTag + i, hv.floatValue());
                    moreValueAvg.put(temTag + i, mAVG.get(temTag + i));
                    moreValueMax.put(temTag + i, mMAX.get(temTag + i));
                    moreValueMin.put(temTag + i, mMIN.get(temTag + i));
                }
            }

            if (!moreValueCp95.isEmpty()) {
                flag = true;
                assPojo(moreValueCp95, mpSurplusAbnormalCp95, type);
                assPojo(moreValueAvg, mpSurplusAbnormalAvg, type);
                assPojo(moreValueMax, mpSurplusAbnormalMax, type);
                assPojo(moreValueMin, mpSurplusAbnormalMin, type);

            }
        }
    }


    /**
     * type 1.谐波电压含有率  2.谐波电流幅值  其他.间谐波电流
     *
     * @author cdf
     * @date 2023/8/22
     */
    private void assPojo(Map<String, Object> map, MpSurplusAbnormalD mpSurplusAbnormalD, Integer type) {
        if (type == 1) {
            //谐波电压
            map.forEach((key, val) -> {
                switch (key) {
                    case "v_2":
                        mpSurplusAbnormalD.setUharm2(Float.parseFloat(val.toString()));
                        break;
                    case "v_3":
                        mpSurplusAbnormalD.setUharm3(Float.parseFloat(val.toString()));
                        break;
                    case "v_4":
                        mpSurplusAbnormalD.setUharm4(Float.parseFloat(val.toString()));
                        break;
                    case "v_5":
                        mpSurplusAbnormalD.setUharm5(Float.parseFloat(val.toString()));
                        break;
                    case "v_6":
                        mpSurplusAbnormalD.setUharm6(Float.parseFloat(val.toString()));
                        break;
                    case "v_7":
                        mpSurplusAbnormalD.setUharm7(Float.parseFloat(val.toString()));
                        break;
                    case "v_8":
                        mpSurplusAbnormalD.setUharm8(Float.parseFloat(val.toString()));
                        break;
                    case "v_9":
                        mpSurplusAbnormalD.setUharm9(Float.parseFloat(val.toString()));
                        break;
                    case "v_10":
                        mpSurplusAbnormalD.setUharm10(Float.parseFloat(val.toString()));
                        break;
                    case "v_11":
                        mpSurplusAbnormalD.setUharm11(Float.parseFloat(val.toString()));
                        break;
                    case "v_12":
                        mpSurplusAbnormalD.setUharm12(Float.parseFloat(val.toString()));
                        break;
                    case "v_13":
                        mpSurplusAbnormalD.setUharm13(Float.parseFloat(val.toString()));
                        break;
                    case "v_14":
                        mpSurplusAbnormalD.setUharm14(Float.parseFloat(val.toString()));
                        break;
                    case "v_15":
                        mpSurplusAbnormalD.setUharm15(Float.parseFloat(val.toString()));
                        break;
                    case "v_16":
                        mpSurplusAbnormalD.setUharm16(Float.parseFloat(val.toString()));
                        break;
                    case "v_17":
                        mpSurplusAbnormalD.setUharm17(Float.parseFloat(val.toString()));
                        break;
                    case "v_18":
                        mpSurplusAbnormalD.setUharm18(Float.parseFloat(val.toString()));
                        break;
                    case "v_19":
                        mpSurplusAbnormalD.setUharm19(Float.parseFloat(val.toString()));
                        break;
                    case "v_20":
                        mpSurplusAbnormalD.setUharm20(Float.parseFloat(val.toString()));
                        break;
                    case "v_21":
                        mpSurplusAbnormalD.setUharm21(Float.parseFloat(val.toString()));
                        break;
                    case "v_22":
                        mpSurplusAbnormalD.setUharm22(Float.parseFloat(val.toString()));
                        break;
                    case "v_23":
                        mpSurplusAbnormalD.setUharm23(Float.parseFloat(val.toString()));
                        break;
                    case "v_24":
                        mpSurplusAbnormalD.setUharm24(Float.parseFloat(val.toString()));
                        break;
                    case "v_25":
                        mpSurplusAbnormalD.setUharm25(Float.parseFloat(val.toString()));
                        break;
                    case "time":
                        break;
                    default:
                        throw new IllegalStateException("Unexpected value: " + key);
                }

            });
        } else if (type == 2) {
            //谐波电流
            map.forEach((key, val) -> {
                switch (key) {
                    case "i_2":
                        mpSurplusAbnormalD.setIharm2(Float.parseFloat(val.toString()));
                        break;
                    case "i_3":
                        mpSurplusAbnormalD.setIharm3(Float.parseFloat(val.toString()));
                        break;
                    case "i_4":
                        mpSurplusAbnormalD.setIharm4(Float.parseFloat(val.toString()));
                        break;
                    case "i_5":
                        mpSurplusAbnormalD.setIharm5(Float.parseFloat(val.toString()));
                        break;
                    case "i_6":
                        mpSurplusAbnormalD.setIharm6(Float.parseFloat(val.toString()));
                        break;
                    case "i_7":
                        mpSurplusAbnormalD.setIharm7(Float.parseFloat(val.toString()));
                        break;
                    case "i_8":
                        mpSurplusAbnormalD.setIharm8(Float.parseFloat(val.toString()));
                        break;
                    case "i_9":
                        mpSurplusAbnormalD.setIharm9(Float.parseFloat(val.toString()));
                        break;
                    case "i_10":
                        mpSurplusAbnormalD.setIharm10(Float.parseFloat(val.toString()));
                        break;
                    case "i_11":
                        mpSurplusAbnormalD.setIharm11(Float.parseFloat(val.toString()));
                        break;
                    case "i_12":
                        mpSurplusAbnormalD.setIharm12(Float.parseFloat(val.toString()));
                        break;
                    case "i_13":
                        mpSurplusAbnormalD.setIharm13(Float.parseFloat(val.toString()));
                        break;
                    case "i_14":
                        mpSurplusAbnormalD.setIharm14(Float.parseFloat(val.toString()));
                        break;
                    case "i_15":
                        mpSurplusAbnormalD.setIharm15(Float.parseFloat(val.toString()));
                        break;
                    case "i_16":
                        mpSurplusAbnormalD.setIharm16(Float.parseFloat(val.toString()));
                        break;
                    case "i_17":
                        mpSurplusAbnormalD.setIharm17(Float.parseFloat(val.toString()));
                        break;
                    case "i_18":
                        mpSurplusAbnormalD.setIharm18(Float.parseFloat(val.toString()));
                        break;
                    case "i_19":
                        mpSurplusAbnormalD.setIharm19(Float.parseFloat(val.toString()));
                        break;
                    case "i_20":
                        mpSurplusAbnormalD.setIharm20(Float.parseFloat(val.toString()));
                        break;
                    case "i_21":
                        mpSurplusAbnormalD.setIharm21(Float.parseFloat(val.toString()));
                        break;
                    case "i_22":
                        mpSurplusAbnormalD.setIharm22(Float.parseFloat(val.toString()));
                        break;
                    case "i_23":
                        mpSurplusAbnormalD.setIharm23(Float.parseFloat(val.toString()));
                        break;
                    case "i_24":
                        mpSurplusAbnormalD.setIharm24(Float.parseFloat(val.toString()));
                        break;
                    case "i_25":
                        mpSurplusAbnormalD.setIharm25(Float.parseFloat(val.toString()));
                        break;
                    case "time":
                        break;
                    default:
                        throw new IllegalStateException("Unexpected value: " + key);
                }
            });
        } else {
            //间谐波电压
            map.forEach((key, val) -> {
                switch (key) {
                    case "v_1":
                        mpSurplusAbnormalD.setInuharm1(Float.parseFloat(val.toString()));
                        break;
                    case "v_2":
                        mpSurplusAbnormalD.setInuharm2(Float.parseFloat(val.toString()));
                        break;
                    case "v_3":
                        mpSurplusAbnormalD.setInuharm3(Float.parseFloat(val.toString()));
                        break;
                    case "v_4":
                        mpSurplusAbnormalD.setInuharm4(Float.parseFloat(val.toString()));
                        break;
                    case "v_5":
                        mpSurplusAbnormalD.setInuharm5(Float.parseFloat(val.toString()));
                        break;
                    case "v_6":
                        mpSurplusAbnormalD.setInuharm6(Float.parseFloat(val.toString()));
                        break;
                    case "v_7":
                        mpSurplusAbnormalD.setInuharm7(Float.parseFloat(val.toString()));
                        break;
                    case "v_8":
                        mpSurplusAbnormalD.setInuharm8(Float.parseFloat(val.toString()));
                        break;
                    case "v_9":
                        mpSurplusAbnormalD.setInuharm9(Float.parseFloat(val.toString()));
                        break;
                    case "v_10":
                        mpSurplusAbnormalD.setInuharm10(Float.parseFloat(val.toString()));
                        break;
                    case "v_11":
                        mpSurplusAbnormalD.setInuharm11(Float.parseFloat(val.toString()));
                        break;
                    case "v_12":
                        mpSurplusAbnormalD.setInuharm12(Float.parseFloat(val.toString()));
                        break;
                    case "v_13":
                        mpSurplusAbnormalD.setInuharm13(Float.parseFloat(val.toString()));
                        break;
                    case "v_14":
                        mpSurplusAbnormalD.setInuharm14(Float.parseFloat(val.toString()));
                        break;
                    case "v_15":
                        mpSurplusAbnormalD.setInuharm15(Float.parseFloat(val.toString()));
                        break;
                    case "v_16":
                        mpSurplusAbnormalD.setInuharm16(Float.parseFloat(val.toString()));
                        break;
                    case "time":
                        break;
                    default:
                        throw new IllegalStateException("Unexpected value: " + key);
                }

            });
        }
    }

    private Map<String, Object> getMetricData(String lineId,String date){
        String processParam = " and line_id = '"+lineId+"' and time >= '"+date+" 00:00:00' and time <= '"+date+" 23:59:59' tz('Asia/Shanghai')";
        QueryResult vuDevResult = influxDbUtils.query("select abs(vu_dev) as vu_dev from data_v where phasic_type != 'T' and value_type = 'AVG'"+processParam);
        QueryResult vThdResult = influxDbUtils.query("select mean(v_thd) from data_v where phasic_type != 'T' and value_type = 'CP95'"+processParam);
        QueryResult freqDevResult = influxDbUtils.query("select abs(freq_dev) as freq_dev from data_v where phasic_type = 'T' and value_type = 'AVG'"+processParam);
        QueryResult vUnResult = influxDbUtils.query("select mean(v_unbalance) from data_v where phasic_type = 'T' and value_type = 'CP95'"+processParam);
        QueryResult pltResult = influxDbUtils.query("select mean(plt) from data_plt where phasic_type != 'T'"+processParam);
        InfluxDBResultMapper resultMapper = new InfluxDBResultMapper();
        List<DataV> vuDev = resultMapper.toPOJO(vuDevResult, DataV.class);
        List<DataV> vThd = resultMapper.toPOJO(vThdResult, DataV.class);
        List<DataV> freqDev = resultMapper.toPOJO(freqDevResult, DataV.class);
        List<DataV> vUn = resultMapper.toPOJO(vUnResult, DataV.class);
        List<DataPlt> plt = resultMapper.toPOJO(pltResult, DataPlt.class);
        Map<String, Object> outMap = new HashMap<>();
        if(CollUtil.isNotEmpty(vuDev) || CollUtil.isNotEmpty(vThd) || CollUtil.isNotEmpty(freqDev) || CollUtil.isNotEmpty(vUn) || CollUtil.isNotEmpty(plt)){
            outMap.put("vu_dev",(vuDev.size()>0)?vuDev.stream().collect(Collectors.averagingDouble(DataV::getVuDev)):0.0);
            outMap.put("v_thd_cp95",(vThd.size()>0)?vThd.get(0).getMean():0.0);
            outMap.put("freq_dev",(freqDev.size()>0)?freqDev.stream().collect(Collectors.averagingDouble(DataV::getFreqDev)):0.0);
            outMap.put("v_unbalance_cp95",(vUn.size()>0)?vUn.get(0).getMean():0.0);
            outMap.put("data_plt",(plt.size()>0)?plt.get(0).getMean():0.0);
        }
        return outMap;
    }

    private List<Overlimit> getAllLineData() {
        return lineFeignClient.getAllLineOverLimit("harmonic-boot","").getData();
    }
}
