package com.njcn.executor.handler;

import com.njcn.common.pojo.constant.PatternRegex;
import com.njcn.device.api.LineFeignClient;
import com.njcn.device.pojo.dto.PollutionParamDTO;
import com.njcn.device.pojo.po.Overlimit;
import com.njcn.executor.pojo.vo.*;
import com.njcn.harmonic.constant.Param;
import com.njcn.influxdb.utils.InfluxDbUtils;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.influxdb.InfluxDB;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Point;
import org.influxdb.dto.QueryResult;
import org.influxdb.impl.InfluxDBResultMapper;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 类的介绍：
 *
 * @author xuyang
 * @version 1.0.0
 * @createTime 2022/5/17 14:34
 */
@Slf4j
@Component
@AllArgsConstructor
public class LimitRateJob {

    private final String DATABASE = "pqsbase";

    private final InfluxDbUtils influxDbUtils;

    private final LineFeignClient lineFeignClient;

    @XxlJob("limitRateJobHandler")
    public void getLimitRate() throws ParseException {
        List<String> list = new ArrayList<>(),paramList = new ArrayList<>();;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)-1, 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.set(calendar2.get(Calendar.YEAR), calendar2.get(Calendar.MONTH), calendar2.get(Calendar.DAY_OF_MONTH)-1, 23, 59, 59);
        calendar2.set(Calendar.MILLISECOND, 0);
        String startTime = format.format(calendar.getTime());
        String endTime = format.format(calendar2.getTime());
        String command = XxlJobHelper.getJobParam();
        if (!StringUtils.isEmpty(command)){
            paramList = Arrays.asList(command.split(","));
            startTime = paramList.get(0);
            endTime = paramList.get(1);
            list = paramList.subList(2,paramList.size());
            if (CollectionUtils.isEmpty(list)){
                log.info("补招未传入监测点，默认使用系统查询的监测点");
                list = lineFeignClient.getLineList().getData();
            }
            boolean s1 = Pattern.matches(PatternRegex.TIME_FORMAT,startTime);
            boolean e1 = Pattern.matches(PatternRegex.TIME_FORMAT,endTime);
            if (!s1 || !e1){
                log.error("补招时间格式错误");
                return;
            } else {
                startTime = startTime + " 00:00:00";
                endTime = endTime + " 23:59:59";
            }
        } else {
            list = lineFeignClient.getLineList().getData();
        }
        long diff,diffDays,a,b = 0;
        Date d1 = format.parse(startTime);
        Date d2 = format.parse(endTime);
        diff = d2.getTime() - d1.getTime();
        diffDays = diff / (24 * 60 * 60 * 1000-1000);
        int days = (int) diffDays;
        if (!CollectionUtils.isEmpty(list)){
            Map<String,List<Overlimit>> overLimitMap = new HashMap<>();
            PollutionParamDTO pollutionParamDTO = new PollutionParamDTO();
            pollutionParamDTO.setLineList(list);
            List<Overlimit> overLimitList = lineFeignClient.getOverLimitByList(pollutionParamDTO).getData();
            if (!CollectionUtils.isEmpty(overLimitList)){
                overLimitMap = overLimitList.stream().collect(Collectors.groupingBy(Overlimit::getId));
            }
            for (int i = 1; i <= days; i++) {
                a = d1.getTime() + (long)(i-1)*(24 * 60 * 60) * 1000;
                b = d1.getTime() + (long)i*(24 * 60 * 60) * 1000-1000;
                startTime = format.format(a);
                endTime = format.format(b);
                Map<String,List<DataV>> map1 = getAllTime(list,startTime,endTime);
                Map<String,List<DataPlt>> map2 = getFlickerAllTime(list,startTime,endTime);
                Map<String,List<DataHarmRateV>> map3 = getDataHarmRateV(list,startTime,endTime);
                Map<String,List<DataI>> map4 = getDataI(list,startTime,endTime);
                Map<String,List<DataInHarmV>> map5 = getDataInHarmV(list,startTime,endTime);
                Map<String,List<DataV>> map6 = getDataV(list,startTime,endTime);
                Map<String,List<DataV>> map7 = getDataVUnbalance(list,startTime,endTime);
                Map<String,List<DataI>> map8 = getDataINeg(list,startTime,endTime);
                Map<String,List<DataV>> map9 = getDataVFreq(list,startTime,endTime);
                Map<String,List<DataV>> map10 = getDataVDev(list,startTime,endTime);
                Map<String,List<DataPlt>> map11 = getDataPlt(list,startTime,endTime);
                List<LimitRate> result = getData(overLimitMap,map1,map2,map3,map4,map5,map6,map7,map8,map9,map10,map11);
                //入库
                if (!CollectionUtils.isEmpty(result)){
                    insert(result,a);
                }
            }
        }
    }


    /**
     * 组装A、B、C、T数据
     */
    private List<LimitRate> getData(Map<String,List<Overlimit>> limitMap, Map<String,List<DataV>> map1, Map<String,List<DataPlt>> map2, Map<String,List<DataHarmRateV>> map3,Map<String,List<DataI>> map4,Map<String,List<DataInHarmV>> map5,Map<String,List<DataV>> map6,Map<String,List<DataV>> map7,Map<String,List<DataI>> map8,Map<String,List<DataV>> map9,Map<String,List<DataV>> map10,Map<String,List<DataPlt>> map11){
        List<LimitRate> result = new ArrayList<>();
        for (String item:limitMap.keySet()){
            Map<String,List<DataHarmRateV>> map111 = new HashMap<>();
            Map<String,List<DataI>> map22 = new HashMap<>();
            Map<String,List<DataInHarmV>> map33 = new HashMap<>();
            Map<String,List<DataV>> map44 = new HashMap<>();
            Map<String,List<DataV>> map88 = new HashMap<>();
            Map<String,List<DataPlt>> map99 = new HashMap<>();
            Map<Instant, List<DataHarmRateV>> map1111 = new HashMap<>();
            Map<Instant,List<DataI>> map222 = new HashMap<>();
            Map<Instant,List<DataInHarmV>> map333 = new HashMap<>();
            Map<Instant,List<DataV>> map444 = new HashMap<>();
            Map<Instant,List<DataV>> map555 = new HashMap<>();
            Map<Instant,List<DataI>> map666 = new HashMap<>();
            Map<Instant,List<DataV>> map777 = new HashMap<>();
            Map<Instant,List<DataV>> map888 = new HashMap<>();
            Map<Instant,List<DataPlt>> map999 = new HashMap<>();
            Integer allTime = 0;
            Integer flickerAllTime = 0;
            Overlimit overlimit = limitMap.get(item).get(0);
            if (!CollectionUtils.isEmpty(map1.get(item))){
                allTime = map1.get(item).get(0).getAllTime();
            }
            if (!CollectionUtils.isEmpty(map2.get(item))){
                flickerAllTime = map2.get(item).get(0).getFlickerAllTime();
            }
            List<DataHarmRateV> list1 = map3.get(item);
            List<DataI> list2 = map4.get(item);
            List<DataInHarmV> list3 = map5.get(item);
            List<DataV> list4 = map6.get(item);
            List<DataV> list5 = map7.get(item);
            List<DataI> list6 = map8.get(item);
            List<DataV> list7 = map9.get(item);
            List<DataV> list8 = map10.get(item);
            List<DataPlt> list9 = map11.get(item);
            if (!CollectionUtils.isEmpty(list1)){
                map111 = list1.stream().collect(Collectors.groupingBy(DataHarmRateV::getPhaseType));
                map1111 = list1.stream().collect(Collectors.groupingBy(DataHarmRateV::getTime));
            }
            if (!CollectionUtils.isEmpty(list2)){
                map22 = list2.stream().collect(Collectors.groupingBy(DataI::getPhaseType));
                map222 = list2.stream().collect(Collectors.groupingBy(DataI::getTime));
            }
            if (!CollectionUtils.isEmpty(list3)){
                map33 = list3.stream().collect(Collectors.groupingBy(DataInHarmV::getPhaseType));
                map333 = list3.stream().collect(Collectors.groupingBy(DataInHarmV::getTime));
            }
            if (!CollectionUtils.isEmpty(list4)){
                map44 = list4.stream().collect(Collectors.groupingBy(DataV::getPhaseType));
                map444 = list4.stream().collect(Collectors.groupingBy(DataV::getTime));
            }
            if (!CollectionUtils.isEmpty(list5)){
                map555 = list5.stream().collect(Collectors.groupingBy(DataV::getTime));
            }
            if (!CollectionUtils.isEmpty(list6)){
                map666 = list6.stream().collect(Collectors.groupingBy(DataI::getTime));
            }
            if (!CollectionUtils.isEmpty(list7)){
                map777 = list7.stream().collect(Collectors.groupingBy(DataV::getTime));
            }
            if (!CollectionUtils.isEmpty(list8)){
                map88 = list8.stream().collect(Collectors.groupingBy(DataV::getPhaseType));
                map888 = list8.stream().collect(Collectors.groupingBy(DataV::getTime));
            }
            if (!CollectionUtils.isEmpty(list9)){
                map99 = list9.stream().collect(Collectors.groupingBy(DataPlt::getPhaseType));
                map999 = list9.stream().collect(Collectors.groupingBy(DataPlt::getTime));
            }
            LimitRate a = limitRateData(overlimit,allTime,flickerAllTime,"A",item,map111.get("A"),map22.get("A"),map33.get("A"),map44.get("A"),map88.get("A"),map99.get("A"));
            LimitRate b = limitRateData(overlimit,allTime,flickerAllTime,"B",item,map111.get("B"),map22.get("B"),map33.get("B"),map44.get("B"),map88.get("B"),map99.get("B"));
            LimitRate c = limitRateData(overlimit,allTime,flickerAllTime,"C",item,map111.get("C"),map22.get("C"),map33.get("C"),map44.get("C"),map88.get("C"),map99.get("C"));
            LimitRate t = limitRateDataT(overlimit,allTime,flickerAllTime,item,map1111,map222,map333,map444,map555,map666,map777,map888,map999);
            result.add(a);
            result.add(b);
            result.add(c);
            result.add(t);
        };
        return result;
    }


    /**
     * A、B、C三项指标统计
     */
    public LimitRate limitRateData(Overlimit overlimit, Integer allTime, Integer flickerAllTime, String phasicType, String lineId, List<DataHarmRateV> list1, List<DataI> list2, List<DataInHarmV> list3, List<DataV> list4, List<DataV> list8, List<DataPlt> list9){
        LimitRate limitRate = new LimitRate();
        int v2 = 0,v3 = 0,v4 = 0,v5 = 0,v6 = 0,v7 = 0,v8 = 0,v9 = 0,v10 = 0,v11 = 0,v12 = 0,v13 = 0,v14 = 0,v15 = 0,v16 = 0,v17 = 0,v18 = 0,v19 = 0,v20 = 0,v21 = 0,v22 = 0,v23 = 0,v24 = 0,v25 = 0;
        int i2 = 0,i3 = 0,i4 = 0,i5 = 0,i6 = 0,i7 = 0,i8 = 0,i9 = 0,i10 = 0,i11 = 0,i12 = 0,i13 = 0,i14 = 0,i15 = 0,i16 = 0,i17 = 0,i18 = 0,i19 = 0,i20 = 0,i21 = 0,i22 = 0,i23 = 0,i24 = 0,i25 = 0;
        int uHarm1 = 0,uHarm2 = 0,uHarm3 = 0,uHarm4 = 0,uHarm5 = 0,uHarm6 = 0,uHarm7 = 0,uHarm8 = 0,uHarm9 = 0,uHarm10 = 0,uHarm11 = 0,uHarm12 = 0,uHarm13 = 0,uHarm14 = 0,uHarm15 = 0,uHarm16 = 0;
        int thd = 0,uDev = 0,flicker = 0;
        //2-25次谐波电压含有率
        if (!CollectionUtils.isEmpty(list1)){
            for (DataHarmRateV item : list1) {
                if (item.getV2() > overlimit.getUharm2()){
                    v2++;
                }
                if (item.getV3() > overlimit.getUharm3()){
                    v3++;
                }
                if (item.getV4() > overlimit.getUharm4()){
                    v4++;
                }
                if (item.getV5() > overlimit.getUharm5()){
                    v5++;
                }
                if (item.getV6() > overlimit.getUharm6()){
                    v6++;
                }
                if (item.getV7() > overlimit.getUharm7()){
                    v7++;
                }
                if (item.getV8() > overlimit.getUharm8()){
                    v8++;
                }
                if (item.getV9() > overlimit.getUharm9()){
                    v9++;
                }
                if (item.getV10() > overlimit.getUharm10()){
                    v10++;
                }
                if (item.getV11() > overlimit.getUharm11()){
                    v11++;
                }
                if (item.getV12() > overlimit.getUharm12()){
                    v12++;
                }
                if (item.getV13() > overlimit.getUharm13()){
                    v13++;
                }
                if (item.getV14() > overlimit.getUharm14()){
                    v14++;
                }
                if (item.getV15() > overlimit.getUharm15()){
                    v15++;
                }
                if (item.getV16() > overlimit.getUharm16()){
                    v16++;
                }
                if (item.getV17() > overlimit.getUharm17()){
                    v17++;
                }
                if (item.getV18() > overlimit.getUharm18()){
                    v18++;
                }
                if (item.getV19() > overlimit.getUharm19()){
                    v19++;
                }
                if (item.getV20() > overlimit.getUharm20()){
                    v20++;
                }
                if (item.getV21() > overlimit.getUharm21()){
                    v21++;
                }
                if (item.getV22() > overlimit.getUharm22()){
                    v22++;
                }
                if (item.getV23() > overlimit.getUharm23()){
                    v23++;
                }
                if (item.getV24() > overlimit.getUharm24()){
                    v24++;
                }
                if (item.getV25() > overlimit.getUharm25()){
                    v25++;
                }
            }
        }
        //2-25次谐波电流
        if (!CollectionUtils.isEmpty(list2)){
            for (DataI item : list2) {
                if (item.getI2() > overlimit.getIharm2()){
                    i2++;
                }
                if (item.getI3() > overlimit.getIharm3()){
                    i3++;
                }
                if (item.getI4() > overlimit.getIharm4()){
                    i4++;
                }
                if (item.getI5() > overlimit.getIharm5()){
                    i5++;
                }
                if (item.getI6() > overlimit.getIharm6()){
                    i6++;
                }
                if (item.getI7() > overlimit.getIharm7()){
                    i7++;
                }
                if (item.getI8() > overlimit.getIharm8()){
                    i8++;
                }
                if (item.getI9() > overlimit.getIharm9()){
                    i9++;
                }
                if (item.getI10() > overlimit.getIharm10()){
                    i10++;
                }
                if (item.getI11() > overlimit.getIharm11()){
                    i11++;
                }
                if (item.getI12() > overlimit.getIharm12()){
                    i12++;
                }
                if (item.getI13() > overlimit.getIharm13()){
                    i13++;
                }
                if (item.getI14() > overlimit.getIharm14()){
                    i14++;
                }
                if (item.getI15() > overlimit.getIharm15()){
                    i15++;
                }
                if (item.getI16() > overlimit.getIharm16()){
                    i16++;
                }
                if (item.getI17() > overlimit.getIharm17()){
                    i17++;
                }
                if (item.getI18() > overlimit.getIharm18()){
                    i18++;
                }
                if (item.getI19() > overlimit.getIharm19()){
                    i19++;
                }
                if (item.getI20() > overlimit.getIharm20()){
                    i20++;
                }
                if (item.getI21() > overlimit.getIharm21()){
                    i21++;
                }
                if (item.getI22() > overlimit.getIharm22()){
                    i22++;
                }
                if (item.getI23() > overlimit.getIharm23()){
                    i23++;
                }
                if (item.getI24() > overlimit.getIharm24()){
                    i24++;
                }
                if (item.getI25() > overlimit.getIharm25()){
                    i25++;
                }
            }
        }
        //0.5-15.5次间谐波电压含有率
        if (!CollectionUtils.isEmpty(list3)){
            for (DataInHarmV item : list3) {
                if (item.getV1() > overlimit.getInuharm1()){
                    uHarm1++;
                }
                if (item.getV2() > overlimit.getInuharm2()){
                    uHarm2++;
                }
                if (item.getV3() > overlimit.getInuharm3()){
                    uHarm3++;
                }
                if (item.getV4() > overlimit.getInuharm4()){
                    uHarm4++;
                }
                if (item.getV5() > overlimit.getInuharm5()){
                    uHarm5++;
                }
                if (item.getV6() > overlimit.getInuharm6()){
                    uHarm6++;
                }
                if (item.getV7() > overlimit.getInuharm7()){
                    uHarm7++;
                }
                if (item.getV8() > overlimit.getInuharm8()){
                    uHarm8++;
                }
                if (item.getV9() > overlimit.getInuharm9()){
                    uHarm9++;
                }
                if (item.getV10() > overlimit.getInuharm10()){
                    uHarm10++;
                }
                if (item.getV11() > overlimit.getInuharm11()){
                    uHarm11++;
                }
                if (item.getV12() > overlimit.getInuharm12()){
                    uHarm12++;
                }
                if (item.getV13() > overlimit.getInuharm13()){
                    uHarm13++;
                }
                if (item.getV14() > overlimit.getInuharm14()){
                    uHarm14++;
                }
                if (item.getV15() > overlimit.getInuharm15()){
                    uHarm15++;
                }
                if (item.getV16() > overlimit.getInuharm16()){
                    uHarm16++;
                }
            }
        }
        //电压总谐波畸变率
        if (!CollectionUtils.isEmpty(list4)){
            for (DataV item : list4) {
                if (item.getVThd() > overlimit.getUaberrance()){
                    thd++;
                }
            }
        }
        //电压上偏差、电压下偏差
        if (!CollectionUtils.isEmpty(list8)){
            Map<String,List<DataV>> devMap = list8.stream().collect(Collectors.groupingBy(DataV::getValueType));
            List<DataV> maxList = devMap.get("MAX");
            for (DataV item : maxList) {
                if (item.getVuDev() > overlimit.getVoltageDev()){
                    uDev++;
                }
            }
            List<DataV> minList = devMap.get("MIN");
            for (DataV item : minList) {
                if (item.getVlDev() < overlimit.getUvoltageDev()){
                    uDev++;
                }
            }
        }
        //长时间闪变
        if (!CollectionUtils.isEmpty(list9)){
            for (DataPlt item : list9) {
                if (item.getPlt() > overlimit.getFlicker()){
                    flicker++;
                }
            }
        }
        limitRate.setLineId(lineId);
        limitRate.setPhasicType(phasicType);
        limitRate.setAllTime(allTime);
        limitRate.setFlickerAllTime(flickerAllTime);
        limitRate.setUAberranceOverTime(0);
        limitRate.setINegOverTime(0);
        limitRate.setFreqDevOverTime(0);
        limitRate.setUHarm2OverTime(v2);
        limitRate.setUHarm3OverTime(v3);
        limitRate.setUHarm4OverTime(v4);
        limitRate.setUHarm5OverTime(v5);
        limitRate.setUHarm6OverTime(v6);
        limitRate.setUHarm7OverTime(v7);
        limitRate.setUHarm8OverTime(v8);
        limitRate.setUHarm9OverTime(v9);
        limitRate.setUHarm10OverTime(v10);
        limitRate.setUHarm11OverTime(v11);
        limitRate.setUHarm12OverTime(v12);
        limitRate.setUHarm13OverTime(v13);
        limitRate.setUHarm14OverTime(v14);
        limitRate.setUHarm15OverTime(v15);
        limitRate.setUHarm16OverTime(v16);
        limitRate.setUHarm17OverTime(v17);
        limitRate.setUHarm18OverTime(v18);
        limitRate.setUHarm19OverTime(v19);
        limitRate.setUHarm20OverTime(v20);
        limitRate.setUHarm21OverTime(v21);
        limitRate.setUHarm22OverTime(v22);
        limitRate.setUHarm23OverTime(v23);
        limitRate.setUHarm24OverTime(v24);
        limitRate.setUHarm25OverTime(v25);
        limitRate.setIHarm2OverTime(i2);
        limitRate.setIHarm3OverTime(i3);
        limitRate.setIHarm4OverTime(i4);
        limitRate.setIHarm5OverTime(i5);
        limitRate.setIHarm6OverTime(i6);
        limitRate.setIHarm7OverTime(i7);
        limitRate.setIHarm8OverTime(i8);
        limitRate.setIHarm9OverTime(i9);
        limitRate.setIHarm10OverTime(i10);
        limitRate.setIHarm11OverTime(i11);
        limitRate.setIHarm12OverTime(i12);
        limitRate.setIHarm13OverTime(i13);
        limitRate.setIHarm14OverTime(i14);
        limitRate.setIHarm15OverTime(i15);
        limitRate.setIHarm16OverTime(i16);
        limitRate.setIHarm17OverTime(i17);
        limitRate.setIHarm18OverTime(i18);
        limitRate.setIHarm19OverTime(i19);
        limitRate.setIHarm20OverTime(i20);
        limitRate.setIHarm21OverTime(i21);
        limitRate.setIHarm22OverTime(i22);
        limitRate.setIHarm23OverTime(i23);
        limitRate.setIHarm24OverTime(i24);
        limitRate.setIHarm25OverTime(i25);
        limitRate.setInuHarm1OverTime(uHarm1);
        limitRate.setInuHarm2OverTime(uHarm2);
        limitRate.setInuHarm3OverTime(uHarm3);
        limitRate.setInuHarm4OverTime(uHarm4);
        limitRate.setInuHarm5OverTime(uHarm5);
        limitRate.setInuHarm6OverTime(uHarm6);
        limitRate.setInuHarm7OverTime(uHarm7);
        limitRate.setInuHarm8OverTime(uHarm8);
        limitRate.setInuHarm9OverTime(uHarm9);
        limitRate.setInuHarm10OverTime(uHarm10);
        limitRate.setInuHarm11OverTime(uHarm11);
        limitRate.setInuHarm12OverTime(uHarm12);
        limitRate.setInuHarm13OverTime(uHarm13);
        limitRate.setInuHarm14OverTime(uHarm14);
        limitRate.setInuHarm15OverTime(uHarm15);
        limitRate.setInuHarm16OverTime(uHarm16);
        limitRate.setUBalanceOverTime(thd);
        limitRate.setVoltageDevOverTime(uDev);
        limitRate.setFlickerOverTime(flicker);
        return limitRate;
    }

    /**
     * T三项指标统计
     */
    public LimitRate limitRateDataT(Overlimit overlimit, Integer allTime, Integer flickerAllTime, String lineId, Map<Instant,List<DataHarmRateV>> map1,Map<Instant,List<DataI>> map2, Map<Instant,List<DataInHarmV>> map3,Map<Instant,List<DataV>> map4,Map<Instant,List<DataV>> map5,Map<Instant,List<DataI>> map6,Map<Instant,List<DataV>> map7, Map<Instant,List<DataV>> map8,Map<Instant,List<DataPlt>> map9){
        LimitRate limitRate = new LimitRate();
        int v2 = 0,v3 = 0,v4 = 0,v5 = 0,v6 = 0,v7 = 0,v8 = 0,v9 = 0,v10 = 0,v11 = 0,v12 = 0,v13 = 0,v14 = 0,v15 = 0,v16 = 0,v17 = 0,v18 = 0,v19 = 0,v20 = 0,v21 = 0,v22 = 0,v23 = 0,v24 = 0,v25 = 0;
        int i2 = 0,i3 = 0,i4 = 0,i5 = 0,i6 = 0,i7 = 0,i8 = 0,i9 = 0,i10 = 0,i11 = 0,i12 = 0,i13 = 0,i14 = 0,i15 = 0,i16 = 0,i17 = 0,i18 = 0,i19 = 0,i20 = 0,i21 = 0,i22 = 0,i23 = 0,i24 = 0,i25 = 0;
        int uHarm1 = 0,uHarm2 = 0,uHarm3 = 0,uHarm4 = 0,uHarm5 = 0,uHarm6 = 0,uHarm7 = 0,uHarm8 = 0,uHarm9 = 0,uHarm10 = 0,uHarm11 = 0,uHarm12 = 0,uHarm13 = 0,uHarm14 = 0,uHarm15 = 0,uHarm16 = 0;
        int thd = 0,uDev = 0,flicker = 0,freqDev = 0,uAberrance = 0,iNeg = 0;
        //2-25次谐波电压含有率
        for (Instant time:map1.keySet()) {
            List<DataHarmRateV> list = map1.get(time);
            List<DataHarmRateV> l2 = list.stream().filter(s->s.getV2()>overlimit.getUharm2()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l2)){
                v2++;
            }
            List<DataHarmRateV> l3 = list.stream().filter(s->s.getV3()>overlimit.getUharm3()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l3)){
                v3++;
            }
            List<DataHarmRateV> l4 = list.stream().filter(s->s.getV4()>overlimit.getUharm4()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l4)){
                v4++;
            }
            List<DataHarmRateV> l5 = list.stream().filter(s->s.getV5()>overlimit.getUharm5()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l5)){
                v5++;
            }
            List<DataHarmRateV> l6 = list.stream().filter(s->s.getV6()>overlimit.getUharm6()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l6)){
                v6++;
            }
            List<DataHarmRateV> l7 = list.stream().filter(s->s.getV7()>overlimit.getUharm7()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l7)){
                v7++;
            }
            List<DataHarmRateV> l8 = list.stream().filter(s->s.getV8()>overlimit.getUharm8()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l8)){
                v8++;
            }
            List<DataHarmRateV> l9 = list.stream().filter(s->s.getV9()>overlimit.getUharm9()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l9)){
                v9++;
            }
            List<DataHarmRateV> l10 = list.stream().filter(s->s.getV10()>overlimit.getUharm10()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l10)){
                v10++;
            }
            List<DataHarmRateV> l11 = list.stream().filter(s->s.getV11()>overlimit.getUharm11()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l11)){
                v11++;
            }
            List<DataHarmRateV> l12 = list.stream().filter(s->s.getV12()>overlimit.getUharm12()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l12)){
                v12++;
            }
            List<DataHarmRateV> l13 = list.stream().filter(s->s.getV13()>overlimit.getUharm13()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l13)){
                v13++;
            }
            List<DataHarmRateV> l14 = list.stream().filter(s->s.getV14()>overlimit.getUharm14()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l14)){
                v14++;
            }
            List<DataHarmRateV> l15 = list.stream().filter(s->s.getV15()>overlimit.getUharm15()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l15)){
                v15++;
            }
            List<DataHarmRateV> l16 = list.stream().filter(s->s.getV16()>overlimit.getUharm16()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l16)){
                v16++;
            }
            List<DataHarmRateV> l17 = list.stream().filter(s->s.getV17()>overlimit.getUharm17()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l17)){
                v17++;
            }
            List<DataHarmRateV> l18 = list.stream().filter(s->s.getV18()>overlimit.getUharm18()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l18)){
                v18++;
            }
            List<DataHarmRateV> l19 = list.stream().filter(s->s.getV19()>overlimit.getUharm19()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l19)){
                v19++;
            }
            List<DataHarmRateV> l20 = list.stream().filter(s->s.getV20()>overlimit.getUharm20()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l20)){
                v20++;
            }
            List<DataHarmRateV> l21 = list.stream().filter(s->s.getV21()>overlimit.getUharm21()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l21)){
                v21++;
            }
            List<DataHarmRateV> l22 = list.stream().filter(s->s.getV22()>overlimit.getUharm22()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l22)){
                v22++;
            }
            List<DataHarmRateV> l23 = list.stream().filter(s->s.getV23()>overlimit.getUharm23()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l23)){
                v23++;
            }
            List<DataHarmRateV> l24 = list.stream().filter(s->s.getV24()>overlimit.getUharm24()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l24)){
                v24++;
            }
            List<DataHarmRateV> l25 = list.stream().filter(s->s.getV25()>overlimit.getUharm25()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l25)){
                v25++;
            }
        }
        //2-25次谐波电流
        for (Instant time:map2.keySet()) {
            List<DataI> list = map2.get(time);
            List<DataI> l2 = list.stream().filter(s->s.getI2()>overlimit.getIharm2()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l2)){
                i2++;
            }
            List<DataI> l3 = list.stream().filter(s->s.getI3()>overlimit.getIharm3()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l3)){
                i3++;
            }
            List<DataI> l4 = list.stream().filter(s->s.getI4()>overlimit.getIharm4()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l4)){
                i4++;
            }
            List<DataI> l5 = list.stream().filter(s->s.getI5()>overlimit.getIharm5()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l5)){
                i5++;
            }
            List<DataI> l6 = list.stream().filter(s->s.getI6()>overlimit.getIharm6()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l6)){
                i6++;
            }
            List<DataI> l7 = list.stream().filter(s->s.getI7()>overlimit.getIharm7()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l7)){
                i7++;
            }
            List<DataI> l8 = list.stream().filter(s->s.getI8()>overlimit.getIharm8()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l8)){
                i8++;
            }
            List<DataI> l9 = list.stream().filter(s->s.getI9()>overlimit.getIharm9()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l9)){
                i9++;
            }
            List<DataI> l10 = list.stream().filter(s->s.getI10()>overlimit.getIharm10()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l10)){
                i10++;
            }
            List<DataI> l11 = list.stream().filter(s->s.getI11()>overlimit.getIharm11()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l11)){
                i11++;
            }
            List<DataI> l12 = list.stream().filter(s->s.getI12()>overlimit.getIharm12()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l12)){
                i12++;
            }
            List<DataI> l13 = list.stream().filter(s->s.getI13()>overlimit.getIharm13()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l13)){
                i13++;
            }
            List<DataI> l14 = list.stream().filter(s->s.getI14()>overlimit.getIharm14()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l14)){
                i14++;
            }
            List<DataI> l15 = list.stream().filter(s->s.getI15()>overlimit.getIharm15()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l15)){
                i15++;
            }
            List<DataI> l16 = list.stream().filter(s->s.getI16()>overlimit.getIharm16()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l16)){
                i16++;
            }
            List<DataI> l17 = list.stream().filter(s->s.getI17()>overlimit.getIharm17()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l17)){
                i17++;
            }
            List<DataI> l18 = list.stream().filter(s->s.getI18()>overlimit.getIharm18()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l18)){
                i18++;
            }
            List<DataI> l19 = list.stream().filter(s->s.getI19()>overlimit.getIharm19()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l19)){
                i19++;
            }
            List<DataI> l20 = list.stream().filter(s->s.getI20()>overlimit.getIharm20()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l20)){
                i20++;
            }
            List<DataI> l21 = list.stream().filter(s->s.getI21()>overlimit.getIharm21()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l21)){
                i21++;
            }
            List<DataI> l22 = list.stream().filter(s->s.getI22()>overlimit.getIharm22()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l22)){
                i22++;
            }
            List<DataI> l23 = list.stream().filter(s->s.getI23()>overlimit.getIharm23()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l23)){
                i23++;
            }
            List<DataI> l24 = list.stream().filter(s->s.getI24()>overlimit.getIharm24()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l24)){
                i24++;
            }
            List<DataI> l25 = list.stream().filter(s->s.getI25()>overlimit.getIharm25()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l25)){
                i25++;
            }
        }
        //0.5-15.5次间谐波电压含有率
        for (Instant time:map3.keySet()) {
            List<DataInHarmV> list3 = map3.get(time);
            List<DataInHarmV> l2 = list3.stream().filter(s->s.getV1()>overlimit.getInuharm1()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l2)){
                uHarm1++;
            }
            List<DataInHarmV> l3 = list3.stream().filter(s->s.getV2()>overlimit.getInuharm2()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l3)){
                uHarm2++;
            }
            List<DataInHarmV> l4 = list3.stream().filter(s->s.getV3()>overlimit.getInuharm3()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l4)){
                uHarm3++;
            }
            List<DataInHarmV> l5 = list3.stream().filter(s->s.getV4()>overlimit.getInuharm4()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l5)){
                uHarm4++;
            }
            List<DataInHarmV> l6 = list3.stream().filter(s->s.getV5()>overlimit.getInuharm5()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l6)){
                uHarm5++;
            }
            List<DataInHarmV> l7 = list3.stream().filter(s->s.getV6()>overlimit.getInuharm6()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l7)){
                uHarm6++;
            }
            List<DataInHarmV> l8 = list3.stream().filter(s->s.getV7()>overlimit.getInuharm7()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l8)){
                uHarm7++;
            }
            List<DataInHarmV> l9 = list3.stream().filter(s->s.getV8()>overlimit.getInuharm8()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l9)){
                uHarm8++;
            }
            List<DataInHarmV> l10 = list3.stream().filter(s->s.getV9()>overlimit.getInuharm9()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l10)){
                uHarm9++;
            }
            List<DataInHarmV> l11 = list3.stream().filter(s->s.getV10()>overlimit.getInuharm10()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l11)){
                uHarm10++;
            }
            List<DataInHarmV> l12 = list3.stream().filter(s->s.getV11()>overlimit.getInuharm11()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l12)){
                uHarm11++;
            }
            List<DataInHarmV> l13 = list3.stream().filter(s->s.getV12()>overlimit.getInuharm12()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l13)){
                uHarm12++;
            }
            List<DataInHarmV> l14 = list3.stream().filter(s->s.getV13()>overlimit.getInuharm13()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l14)){
                uHarm13++;
            }
            List<DataInHarmV> l15 = list3.stream().filter(s->s.getV14()>overlimit.getInuharm14()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l15)){
                uHarm14++;
            }
            List<DataInHarmV> l16 = list3.stream().filter(s->s.getV15()>overlimit.getInuharm15()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l16)){
                uHarm15++;
            }
            List<DataInHarmV> l17 = list3.stream().filter(s->s.getV16()>overlimit.getInuharm16()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l17)){
                uHarm16++;
            }
        }
        //电压总谐波畸变率
        for (Instant time:map4.keySet()) {
            List<DataV> list = map4.get(time);
            List<DataV> l = list.stream().filter(s->s.getVThd()>overlimit.getUaberrance()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l)){
                thd++;
            }
        }
        //三相电压不平衡度
        for (Instant time:map5.keySet()) {
            List<DataV> list = map5.get(time);
            List<DataV> l = list.stream().filter(s->s.getVUnbalance()>overlimit.getUbalance()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l)){
                uAberrance++;
            }
        }
        //负序电流
        for (Instant time:map6.keySet()) {
            List<DataI> list = map6.get(time);
            List<DataI> l = list.stream().filter(s->s.getINeg()>overlimit.getINeg()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l)){
                iNeg++;
            }
        }
        //频率偏差
        for (Instant time:map7.keySet()) {
            List<DataV> list = map7.get(time);
            List<DataV> l = list.stream().filter(s->s.getFreqDev()>overlimit.getFreqDev()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l)){
                freqDev++;
            }
        }
        //电压上偏差、电压下偏差
        for (Instant time:map8.keySet()) {
            List<DataV> list = map8.get(time);
            Map<String,List<DataV>> devMap = list.stream().collect(Collectors.groupingBy(DataV::getValueType));
            List<DataV> maxList = devMap.get("MAX");
            List<DataV> minList = devMap.get("MIN");
            List<DataV> l1 = maxList.stream().filter(s->s.getVuDev()>overlimit.getVoltageDev()).collect(Collectors.toList());
            List<DataV> l2 = minList.stream().filter(s->s.getVlDev()>overlimit.getUvoltageDev()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l1) || !CollectionUtils.isEmpty(l2)){
                uDev++;
            }
        }
        //长时间闪变
        for (Instant time:map9.keySet()) {
            List<DataPlt> list = map9.get(time);
            List<DataPlt> l = list.stream().filter(s->s.getPlt()>overlimit.getFlicker()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l)){
                flicker++;
            }
        }
        limitRate.setLineId(lineId);
        limitRate.setPhasicType("T");
        limitRate.setAllTime(allTime);
        limitRate.setFlickerAllTime(flickerAllTime);
        limitRate.setUAberranceOverTime(uAberrance);
        limitRate.setINegOverTime(iNeg);
        limitRate.setFreqDevOverTime(freqDev);
        limitRate.setUHarm2OverTime(v2);
        limitRate.setUHarm3OverTime(v3);
        limitRate.setUHarm4OverTime(v4);
        limitRate.setUHarm5OverTime(v5);
        limitRate.setUHarm6OverTime(v6);
        limitRate.setUHarm7OverTime(v7);
        limitRate.setUHarm8OverTime(v8);
        limitRate.setUHarm9OverTime(v9);
        limitRate.setUHarm10OverTime(v10);
        limitRate.setUHarm11OverTime(v11);
        limitRate.setUHarm12OverTime(v12);
        limitRate.setUHarm13OverTime(v13);
        limitRate.setUHarm14OverTime(v14);
        limitRate.setUHarm15OverTime(v15);
        limitRate.setUHarm16OverTime(v16);
        limitRate.setUHarm17OverTime(v17);
        limitRate.setUHarm18OverTime(v18);
        limitRate.setUHarm19OverTime(v19);
        limitRate.setUHarm20OverTime(v20);
        limitRate.setUHarm21OverTime(v21);
        limitRate.setUHarm22OverTime(v22);
        limitRate.setUHarm23OverTime(v23);
        limitRate.setUHarm24OverTime(v24);
        limitRate.setUHarm25OverTime(v25);
        limitRate.setIHarm2OverTime(i2);
        limitRate.setIHarm3OverTime(i3);
        limitRate.setIHarm4OverTime(i4);
        limitRate.setIHarm5OverTime(i5);
        limitRate.setIHarm6OverTime(i6);
        limitRate.setIHarm7OverTime(i7);
        limitRate.setIHarm8OverTime(i8);
        limitRate.setIHarm9OverTime(i9);
        limitRate.setIHarm10OverTime(i10);
        limitRate.setIHarm11OverTime(i11);
        limitRate.setIHarm12OverTime(i12);
        limitRate.setIHarm13OverTime(i13);
        limitRate.setIHarm14OverTime(i14);
        limitRate.setIHarm15OverTime(i15);
        limitRate.setIHarm16OverTime(i16);
        limitRate.setIHarm17OverTime(i17);
        limitRate.setIHarm18OverTime(i18);
        limitRate.setIHarm19OverTime(i19);
        limitRate.setIHarm20OverTime(i20);
        limitRate.setIHarm21OverTime(i21);
        limitRate.setIHarm22OverTime(i22);
        limitRate.setIHarm23OverTime(i23);
        limitRate.setIHarm24OverTime(i24);
        limitRate.setIHarm25OverTime(i25);
        limitRate.setInuHarm1OverTime(uHarm1);
        limitRate.setInuHarm2OverTime(uHarm2);
        limitRate.setInuHarm3OverTime(uHarm3);
        limitRate.setInuHarm4OverTime(uHarm4);
        limitRate.setInuHarm5OverTime(uHarm5);
        limitRate.setInuHarm6OverTime(uHarm6);
        limitRate.setInuHarm7OverTime(uHarm7);
        limitRate.setInuHarm8OverTime(uHarm8);
        limitRate.setInuHarm9OverTime(uHarm9);
        limitRate.setInuHarm10OverTime(uHarm10);
        limitRate.setInuHarm11OverTime(uHarm11);
        limitRate.setInuHarm12OverTime(uHarm12);
        limitRate.setInuHarm13OverTime(uHarm13);
        limitRate.setInuHarm14OverTime(uHarm14);
        limitRate.setInuHarm15OverTime(uHarm15);
        limitRate.setInuHarm16OverTime(uHarm16);
        limitRate.setUBalanceOverTime(thd);
        limitRate.setVoltageDevOverTime(uDev);
        limitRate.setFlickerOverTime(flicker);
        return limitRate;
    }

    /**
     * 功能描述:获取influxDB -> data_v -> 总计算次数(用data_v中phasic_type=A,quality_flag=0来参与统计)
     * @author xy
     * @param line          监测点集合
     * @param startTime     开始时间
     * @param endTime       结束时间
     * @date 2022/5/23 10:51
     * @return
     */
    public Map<String,List<DataV>> getAllTime(List<String> line, String startTime, String endTime){
        Map<String,List<DataV>> map = new HashMap<>();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("(");
        for (int i = 0; i < line.size(); i++) {
            if (line.size() - i != 1) {
                stringBuilder.append("line_id='").append(line.get(i)).append("' or ");
            } else {
                stringBuilder.append("line_id='").append(line.get(i)).append("') and ");
            }
        }
        stringBuilder.append(Param.TIME + " >= '").append(startTime).append("' and ").append(Param.TIME).append(" <= '").append(endTime).append("' and phasic_type = 'A' and value_type = 'AVG' and quality_flag = '0' group by line_id tz('Asia/Shanghai')");
        String sql = "SELECT count(freq) AS all_time FROM data_v where " + stringBuilder;
        QueryResult sqlResult = influxDbUtils.query(sql);
        InfluxDBResultMapper resultMapper = new InfluxDBResultMapper();
        List<DataV> list = resultMapper.toPOJO(sqlResult, DataV.class);
        if (!CollectionUtils.isEmpty(list)){
            map = list.stream().collect(Collectors.groupingBy(DataV::getLineId));
        }
        return map;
    }

    /**
     * 功能描述:获取influxDB ->data_plt -> 闪变总次数(用data_plt中phasic_type=A,quality_flag=0来参与统计)
     * @author xy
     * @param line          监测点集合
     * @param startTime     开始时间
     * @param endTime       结束时间
     * @date 2022/5/23 10:51
     * @return
     */
    public Map<String,List<DataPlt>> getFlickerAllTime(List<String> line, String startTime, String endTime){
        Map<String,List<DataPlt>> map = new HashMap<>();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("(");
        for (int i = 0; i < line.size(); i++) {
            if (line.size() - i != 1) {
                stringBuilder.append("line_id='").append(line.get(i)).append("' or ");
            } else {
                stringBuilder.append("line_id='").append(line.get(i)).append("') and ");
            }
        }
        stringBuilder.append(Param.TIME + " >= '").append(startTime).append("' and ").append(Param.TIME).append(" <= '").append(endTime).append("' and phasic_type = 'A' and quality_flag = '0' group by line_id tz('Asia/Shanghai')");
        String sql = "SELECT count(plt) AS flicker_all_time FROM data_plt where " + stringBuilder;
        QueryResult sqlResult = influxDbUtils.query(sql);
        InfluxDBResultMapper resultMapper = new InfluxDBResultMapper();
        List<DataPlt> list = resultMapper.toPOJO(sqlResult, DataPlt.class);
        if (!CollectionUtils.isEmpty(list)){
            map = list.stream().collect(Collectors.groupingBy(DataPlt::getLineId));
        }
        return map;
    }

    /**
     * 功能描述:获取influxDB -> data_harmrate_v -> 2-25次谐波电压含有率 -> 日95%概率值
     * @author xy
     * @param line          监测点集合
     * @param startTime     开始时间
     * @param endTime       结束时间
     * @date 2022/5/23 10:51
     * @return
     */
    public Map<String,List<DataHarmRateV>> getDataHarmRateV(List<String> line, String startTime, String endTime){
        Map<String,List<DataHarmRateV>> map = new HashMap<>();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("(");
        for (int i = 0; i < line.size(); i++) {
            if (line.size() - i != 1) {
                stringBuilder.append("line_id='").append(line.get(i)).append("' or ");
            } else {
                stringBuilder.append("line_id='").append(line.get(i)).append("') and ");
            }
        }
        stringBuilder.append(Param.TIME + " >= '").append(startTime).append("' and ").append(Param.TIME).append(" <= '").append(endTime).append("' and (phasic_type = 'A' or phasic_type = 'B' or phasic_type = 'C') and value_type = 'CP95' and quality_flag = '0' tz('Asia/Shanghai')");
        String sql = "SELECT * FROM data_harmrate_v WHERE " + stringBuilder;
        QueryResult sqlResult = influxDbUtils.query(sql);
        InfluxDBResultMapper resultMapper = new InfluxDBResultMapper();
        List<DataHarmRateV> list = resultMapper.toPOJO(sqlResult, DataHarmRateV.class);
        if (!CollectionUtils.isEmpty(list)){
            map = list.stream().collect(Collectors.groupingBy(DataHarmRateV::getLineId));
        }
        return map;
    }

    /**
     * 功能描述:获取influxDB -> data_i -> 2-25次谐波电流 -> 日95%概率值
     * @author xy
     * @param line          监测点集合
     * @param startTime     开始时间
     * @param endTime       结束时间
     * @date 2022/5/23 10:51
     * @return
     */
    public Map<String,List<DataI>> getDataI(List<String> line, String startTime, String endTime){
        Map<String,List<DataI>> map = new HashMap<>();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("(");
        for (int i = 0; i < line.size(); i++) {
            if (line.size() - i != 1) {
                stringBuilder.append("line_id='").append(line.get(i)).append("' or ");
            } else {
                stringBuilder.append("line_id='").append(line.get(i)).append("') and ");
            }
        }
        stringBuilder.append(Param.TIME + " >= '").append(startTime).append("' and ").append(Param.TIME).append(" <= '").append(endTime).append("' and (phasic_type = 'A' or phasic_type = 'B' or phasic_type = 'C') and value_type = 'CP95' and quality_flag = '0' tz('Asia/Shanghai')");
        String sql = "SELECT * FROM data_i WHERE " + stringBuilder;
        QueryResult sqlResult = influxDbUtils.query(sql);
        InfluxDBResultMapper resultMapper = new InfluxDBResultMapper();
        List<DataI> list = resultMapper.toPOJO(sqlResult, DataI.class);
        if (!CollectionUtils.isEmpty(list)){
            map = list.stream().collect(Collectors.groupingBy(DataI::getLineId));
        }
        return map;
    }

    /**
     * 功能描述:获取influxDB -> data_inharm_v -> 0.5-15.5次间谐波电压含有率 -> 日95%概率值
     * @author xy
     * @param line          监测点集合
     * @param startTime     开始时间
     * @param endTime       结束时间
     * @date 2022/5/23 10:51
     * @return
     */
    public Map<String,List<DataInHarmV>> getDataInHarmV(List<String> line, String startTime, String endTime){
        Map<String,List<DataInHarmV>> map = new HashMap<>();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("(");
        for (int i = 0; i < line.size(); i++) {
            if (line.size() - i != 1) {
                stringBuilder.append("line_id='").append(line.get(i)).append("' or ");
            } else {
                stringBuilder.append("line_id='").append(line.get(i)).append("') and ");
            }
        }
        stringBuilder.append(Param.TIME + " >= '").append(startTime).append("' and ").append(Param.TIME).append(" <= '").append(endTime).append("' and (phasic_type = 'A' or phasic_type = 'B' or phasic_type = 'C') and value_type = 'CP95' and quality_flag = '0' tz('Asia/Shanghai')");
        String sql = "SELECT * FROM data_inharm_v WHERE " + stringBuilder;
        QueryResult sqlResult = influxDbUtils.query(sql);
        InfluxDBResultMapper resultMapper = new InfluxDBResultMapper();
        List<DataInHarmV> list = resultMapper.toPOJO(sqlResult, DataInHarmV.class);
        if (!CollectionUtils.isEmpty(list)){
            map = list.stream().collect(Collectors.groupingBy(DataInHarmV::getLineId));
        }
        return map;
    }

    /**
     * 功能描述:获取influxDB -> data_v -> 电压总谐波畸变率 -> 日95%概率值
     * @author xy
     * @param line          监测点集合
     * @param startTime     开始时间
     * @param endTime       结束时间
     * @date 2022/5/23 10:51
     * @return
     */
    public Map<String,List<DataV>> getDataV(List<String> line, String startTime, String endTime){
        Map<String,List<DataV>> map = new HashMap<>();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("(");
        for (int i = 0; i < line.size(); i++) {
            if (line.size() - i != 1) {
                stringBuilder.append("line_id='").append(line.get(i)).append("' or ");
            } else {
                stringBuilder.append("line_id='").append(line.get(i)).append("') and ");
            }
        }
        stringBuilder.append(Param.TIME + " >= '").append(startTime).append("' and ").append(Param.TIME).append(" <= '").append(endTime).append("' and (phasic_type = 'A' or phasic_type = 'B' or phasic_type = 'C') and value_type = 'CP95' and quality_flag = '0' tz('Asia/Shanghai')");
        String sql = "SELECT * FROM data_v WHERE " + stringBuilder;
        QueryResult sqlResult = influxDbUtils.query(sql);
        InfluxDBResultMapper resultMapper = new InfluxDBResultMapper();
        List<DataV> list = resultMapper.toPOJO(sqlResult, DataV.class);
        if (!CollectionUtils.isEmpty(list)){
            map = list.stream().collect(Collectors.groupingBy(DataV::getLineId));
        }
        return map;
    }


    /**
     * 功能描述:获取influxDB -> data_v -> 负序电压不平衡度 -> 最大值 && 日95%概率值
     * @author xy
     * @param line          监测点集合
     * @param startTime     开始时间
     * @param endTime       结束时间
     * @date 2022/5/23 10:51
     * @return
     */
    public Map<String,List<DataV>> getDataVUnbalance(List<String> line, String startTime, String endTime){
        Map<String,List<DataV>> map = new HashMap<>();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("(");
        for (int i = 0; i < line.size(); i++) {
            if (line.size() - i != 1) {
                stringBuilder.append("line_id='").append(line.get(i)).append("' or ");
            } else {
                stringBuilder.append("line_id='").append(line.get(i)).append("') and ");
            }
        }
        stringBuilder.append(Param.TIME + " >= '").append(startTime).append("' and ").append(Param.TIME).append(" <= '").append(endTime).append("' and phasic_type = 'T' and (value_type = 'CP95' or value_type = 'MAX') and quality_flag = '0' tz('Asia/Shanghai')");
        String sql = "SELECT * FROM data_v WHERE " + stringBuilder;
        QueryResult sqlResult = influxDbUtils.query(sql);
        InfluxDBResultMapper resultMapper = new InfluxDBResultMapper();
        List<DataV> list = resultMapper.toPOJO(sqlResult, DataV.class);
        if (!CollectionUtils.isEmpty(list)){
            map = list.stream().collect(Collectors.groupingBy(DataV::getLineId));
        }
        return map;
    }

    /**
     * 功能描述:获取influxDB -> data_i -> 负序电流 -> 最大值 && 日95%概率值
     * @author xy
     * @param line          监测点集合
     * @param startTime     开始时间
     * @param endTime       结束时间
     * @date 2022/5/23 10:51
     * @return
     */
    public Map<String,List<DataI>> getDataINeg(List<String> line, String startTime, String endTime){
        Map<String,List<DataI>> map = new HashMap<>();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("(");
        for (int i = 0; i < line.size(); i++) {
            if (line.size() - i != 1) {
                stringBuilder.append("line_id='").append(line.get(i)).append("' or ");
            } else {
                stringBuilder.append("line_id='").append(line.get(i)).append("') and ");
            }
        }
        stringBuilder.append(Param.TIME + " >= '").append(startTime).append("' and ").append(Param.TIME).append(" <= '").append(endTime).append("' and phasic_type = 'T' and (value_type = 'CP95' or value_type = 'MAX') and quality_flag = '0' tz('Asia/Shanghai')");
        String sql = "SELECT * FROM data_i WHERE " + stringBuilder;
        QueryResult sqlResult = influxDbUtils.query(sql);
        InfluxDBResultMapper resultMapper = new InfluxDBResultMapper();
        List<DataI> list = resultMapper.toPOJO(sqlResult, DataI.class);
        if (!CollectionUtils.isEmpty(list)){
            map = list.stream().collect(Collectors.groupingBy(DataI::getLineId));
        }
        return map;
    }

    /**
     * 功能描述:获取influxDB -> data_v -> 频率偏差 -> 最大值 && 最小值
     * @author xy
     * @param line          监测点集合
     * @param startTime     开始时间
     * @param endTime       结束时间
     * @date 2022/5/23 10:51
     * @return
     */
    public Map<String,List<DataV>> getDataVFreq(List<String> line, String startTime, String endTime){
        Map<String,List<DataV>> map = new HashMap<>();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("(");
        for (int i = 0; i < line.size(); i++) {
            if (line.size() - i != 1) {
                stringBuilder.append("line_id='").append(line.get(i)).append("' or ");
            } else {
                stringBuilder.append("line_id='").append(line.get(i)).append("') and ");
            }
        }
        stringBuilder.append(Param.TIME + " >= '").append(startTime).append("' and ").append(Param.TIME).append(" <= '").append(endTime).append("' and phasic_type = 'T' and (value_type = 'MIN' or value_type = 'MAX') and quality_flag = '0' tz('Asia/Shanghai')");
        String sql = "SELECT * FROM data_v WHERE " + stringBuilder;
        QueryResult sqlResult = influxDbUtils.query(sql);
        InfluxDBResultMapper resultMapper = new InfluxDBResultMapper();
        List<DataV> list = resultMapper.toPOJO(sqlResult, DataV.class);
        if (!CollectionUtils.isEmpty(list)){
            map = list.stream().collect(Collectors.groupingBy(DataV::getLineId));
        }
        return map;
    }

    /**
     * 功能描述:获取influxDB -> data_v -> 电压偏差 -> 最大值 && 最小值
     * @author xy
     * @param line          监测点集合
     * @param startTime     开始时间
     * @param endTime       结束时间
     * @date 2022/5/23 10:51
     * @return
     */
    public Map<String,List<DataV>> getDataVDev(List<String> line, String startTime, String endTime){
        Map<String,List<DataV>> map = new HashMap<>();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("(");
        for (int i = 0; i < line.size(); i++) {
            if (line.size() - i != 1) {
                stringBuilder.append("line_id='").append(line.get(i)).append("' or ");
            } else {
                stringBuilder.append("line_id='").append(line.get(i)).append("') and ");
            }
        }
        stringBuilder.append(Param.TIME + " >= '").append(startTime).append("' and ").append(Param.TIME).append(" <= '").append(endTime).append("' and (phasic_type = 'A' or phasic_type = 'B' or phasic_type = 'C') and (value_type = 'MIN' or value_type = 'MAX') and quality_flag = '0' tz('Asia/Shanghai')");
        String sql = "SELECT * FROM data_v WHERE " + stringBuilder;
        QueryResult sqlResult = influxDbUtils.query(sql);
        InfluxDBResultMapper resultMapper = new InfluxDBResultMapper();
        List<DataV> list = resultMapper.toPOJO(sqlResult, DataV.class);
        if (!CollectionUtils.isEmpty(list)){
            map = list.stream().collect(Collectors.groupingBy(DataV::getLineId));
        }
        return map;
    }

    /**
     * 功能描述:获取influxDB -> data_plt -> 长时间闪变 -> 最大值
     * @author xy
     * @param line          监测点集合
     * @param startTime     开始时间
     * @param endTime       结束时间
     * @date 2022/5/23 10:51
     * @return
     */
    public Map<String,List<DataPlt>> getDataPlt(List<String> line, String startTime, String endTime){
        Map<String,List<DataPlt>> map = new HashMap<>();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("(");
        for (int i = 0; i < line.size(); i++) {
            if (line.size() - i != 1) {
                stringBuilder.append("line_id='").append(line.get(i)).append("' or ");
            } else {
                stringBuilder.append("line_id='").append(line.get(i)).append("') and ");
            }
        }
        stringBuilder.append(Param.TIME + " >= '").append(startTime).append("' and ").append(Param.TIME).append(" <= '").append(endTime).append("' and (phasic_type = 'A' or phasic_type = 'B' or phasic_type = 'C') and quality_flag = '0' tz('Asia/Shanghai')");
        String sql = "SELECT * FROM data_plt WHERE " + stringBuilder;
        QueryResult sqlResult = influxDbUtils.query(sql);
        InfluxDBResultMapper resultMapper = new InfluxDBResultMapper();
        List<DataPlt> list = resultMapper.toPOJO(sqlResult, DataPlt.class);
        if (!CollectionUtils.isEmpty(list)){
            map = list.stream().collect(Collectors.groupingBy(DataPlt::getLineId));
        }
        return map;
    }

    private void insert(List<LimitRate> list, long time){
        List<String> records = new ArrayList<>();
        list.forEach(item->{
            Map<String, String> tags = new HashMap<>();
            Map<String, Object> fields = new HashMap<>();
            tags.put("line_id",item.getLineId());
            tags.put("phasic_type",item.getPhasicType());
            fields.put("all_time",item.getAllTime());
            fields.put("flicker_all_time",item.getFlickerAllTime());
            fields.put("flicker_overtime",item.getFlickerOverTime());
            fields.put("freq_dev_overtime",item.getFreqDevOverTime());
            fields.put("ubalance_overtime",item.getUBalanceOverTime());
            fields.put("uaberrance_overtime",item.getUAberranceOverTime());
            fields.put("i_neg_overtime",item.getINegOverTime());
            fields.put("voltage_dev_overtime",item.getVoltageDevOverTime());
            fields.put("uharm_2_overtime",item.getUHarm2OverTime());
            fields.put("uharm_3_overtime",item.getUHarm3OverTime());
            fields.put("uharm_4_overtime",item.getUHarm4OverTime());
            fields.put("uharm_5_overtime",item.getUHarm5OverTime());
            fields.put("uharm_6_overtime",item.getUHarm6OverTime());
            fields.put("uharm_7_overtime",item.getUHarm7OverTime());
            fields.put("uharm_8_overtime",item.getUHarm8OverTime());
            fields.put("uharm_9_overtime",item.getUHarm9OverTime());
            fields.put("uharm_10_overtime",item.getUHarm10OverTime());
            fields.put("uharm_11_overtime",item.getUHarm11OverTime());
            fields.put("uharm_12_overtime",item.getUHarm12OverTime());
            fields.put("uharm_13_overtime",item.getUHarm13OverTime());
            fields.put("uharm_14_overtime",item.getUHarm14OverTime());
            fields.put("uharm_15_overtime",item.getUHarm15OverTime());
            fields.put("uharm_16_overtime",item.getUHarm16OverTime());
            fields.put("uharm_17_overtime",item.getUHarm17OverTime());
            fields.put("uharm_18_overtime",item.getUHarm18OverTime());
            fields.put("uharm_19_overtime",item.getUHarm19OverTime());
            fields.put("uharm_20_overtime",item.getUHarm20OverTime());
            fields.put("uharm_21_overtime",item.getUHarm21OverTime());
            fields.put("uharm_22_overtime",item.getUHarm22OverTime());
            fields.put("uharm_23_overtime",item.getUHarm23OverTime());
            fields.put("uharm_24_overtime",item.getUHarm24OverTime());
            fields.put("uharm_25_overtime",item.getUHarm25OverTime());
            fields.put("iharm_2_overtime",item.getIHarm2OverTime());
            fields.put("iharm_3_overtime",item.getIHarm3OverTime());
            fields.put("iharm_4_overtime",item.getIHarm4OverTime());
            fields.put("iharm_5_overtime",item.getIHarm5OverTime());
            fields.put("iharm_6_overtime",item.getIHarm6OverTime());
            fields.put("iharm_7_overtime",item.getIHarm7OverTime());
            fields.put("iharm_8_overtime",item.getIHarm8OverTime());
            fields.put("iharm_9_overtime",item.getIHarm9OverTime());
            fields.put("iharm_10_overtime",item.getIHarm10OverTime());
            fields.put("iharm_11_overtime",item.getIHarm11OverTime());
            fields.put("iharm_12_overtime",item.getIHarm12OverTime());
            fields.put("iharm_13_overtime",item.getIHarm13OverTime());
            fields.put("iharm_14_overtime",item.getIHarm14OverTime());
            fields.put("iharm_15_overtime",item.getIHarm15OverTime());
            fields.put("iharm_16_overtime",item.getIHarm16OverTime());
            fields.put("iharm_17_overtime",item.getIHarm17OverTime());
            fields.put("iharm_18_overtime",item.getIHarm18OverTime());
            fields.put("iharm_19_overtime",item.getIHarm19OverTime());
            fields.put("iharm_20_overtime",item.getIHarm20OverTime());
            fields.put("iharm_21_overtime",item.getIHarm21OverTime());
            fields.put("iharm_22_overtime",item.getIHarm22OverTime());
            fields.put("iharm_23_overtime",item.getIHarm23OverTime());
            fields.put("iharm_24_overtime",item.getIHarm24OverTime());
            fields.put("iharm_25_overtime",item.getIHarm25OverTime());
            fields.put("inuharm_1_overtime",item.getInuHarm1OverTime());
            fields.put("inuharm_2_overtime",item.getInuHarm2OverTime());
            fields.put("inuharm_3_overtime",item.getInuHarm3OverTime());
            fields.put("inuharm_4_overtime",item.getInuHarm4OverTime());
            fields.put("inuharm_5_overtime",item.getInuHarm5OverTime());
            fields.put("inuharm_6_overtime",item.getInuHarm6OverTime());
            fields.put("inuharm_7_overtime",item.getInuHarm7OverTime());
            fields.put("inuharm_8_overtime",item.getInuHarm8OverTime());
            fields.put("inuharm_9_overtime",item.getInuHarm9OverTime());
            fields.put("inuharm_10_overtime",item.getInuHarm10OverTime());
            fields.put("inuharm_11_overtime",item.getInuHarm11OverTime());
            fields.put("inuharm_12_overtime",item.getInuHarm12OverTime());
            fields.put("inuharm_13_overtime",item.getInuHarm13OverTime());
            fields.put("inuharm_14_overtime",item.getInuHarm14OverTime());
            fields.put("inuharm_15_overtime",item.getInuHarm15OverTime());
            fields.put("inuharm_16_overtime",item.getInuHarm16OverTime());
            Point point = influxDbUtils.pointBuilder("limit_rate", time, TimeUnit.MILLISECONDS, tags, fields);
            BatchPoints batchPoints = BatchPoints.database(DATABASE).tag("line_id", item.getLineId()).tag("phasic_type",item.getPhasicType()).retentionPolicy("").consistency(InfluxDB.ConsistencyLevel.ALL).build();
            batchPoints.point(point);
            records.add(batchPoints.lineProtocol());
        });
        influxDbUtils.batchInsert(DATABASE,"", InfluxDB.ConsistencyLevel.ALL, records);
    }
}
