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

import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.common.utils.NjcnBeanUtil;
import com.njcn.device.pq.api.LineFeignClient;
import com.njcn.device.biz.pojo.po.Overlimit;
import com.njcn.harmonic.pojo.po.LimitRate;
import com.njcn.harmonic.pojo.po.LimitRatePO;
import com.njcn.harmonic.pojo.po.day.RStatLimitRateDPO;
import com.njcn.influxdb.param.InfluxDBPublicParam;
import com.njcn.influxdb.utils.InfluxDbUtils;
import com.njcn.prepare.harmonic.mapper.mysql.line.LimitRatePOMapper;
import com.njcn.prepare.harmonic.pojo.influxdb.po.*;
import com.njcn.prepare.harmonic.service.mysql.day.IRStatLimitRateDService;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
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.beans.BeanUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

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

/**
 * Description:
 * 接口文档访问地址：http://serverIP:port/swagger-ui.html
 * Date: 2022/10/18  10:00【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */

@Slf4j
@Component
@AllArgsConstructor
public class LimitRateService  extends MppServiceImpl<LimitRatePOMapper, LimitRatePO> {

    private final InfluxDbUtils influxDbUtils;

    private final LineFeignClient lineFeignClient;

    private final IRStatLimitRateDService statLimitRateDService;

    /**
     * @Description: 处理 limitrate批处理逻辑
     * @Param: [lineList：监测点index集合, startTime统计开始时间, endTime统计结束时间]
     * @return: void
     * @Author: clam
     * @Date: 2022/10/18
     */
    @Transactional(
            rollbackFor = {Exception.class}
    )
    @SneakyThrows
    @Async("asyncExecutor")
    public void limitRateJobHandler(List<String> lineList, String startTime, String endTime)  {
        List<LimitRatePO> limitRates = new ArrayList<> ();
        List<LimitRate> limitRateList = new ArrayList<> ();
        Instant instant = new SimpleDateFormat ("yyyy-MM-dd").parse (startTime).toInstant ( );
        Long time= instant.toEpochMilli();
        for (int i = 0; i <lineList.size () ; i++) {
            String lineIndex =lineList.get (i);
            Overlimit overlimit = lineFeignClient.getOverLimitData(lineIndex).getData ();
            if(!Optional.ofNullable (overlimit).isPresent ()){
                continue;
            }
//            influxDbUtils.setDbName ("pqsbase");
            List<DataVPO> map1 = getAllTime(lineIndex,startTime,endTime);
            List<DataPltPO> map2 = getFlickerAllTime(lineIndex,startTime,endTime);
            List<DataHarmRateVPO> map3 = getDataHarmRateV(lineIndex,startTime,endTime);
            List<DataIPO> map4 = getDataI(lineIndex,startTime,endTime);
            List<DataInHarmVPO> map5 = getDataInHarmV(lineIndex,startTime,endTime);
            List<DataVPO> map6 = getDataV(lineIndex,startTime,endTime);
            List<DataVPO> map7 = getDataVUnbalance(lineIndex,startTime,endTime);
            List<DataIPO> map8 = getDataINeg(lineIndex,startTime,endTime);
            List<DataVPO> map9 = getDataVFreq(lineIndex,startTime,endTime);
            List<DataVPO> map10 = getDataVDev(lineIndex,startTime,endTime);
            List<DataPltPO> map11 = getDataPlt(lineIndex,startTime,endTime);

            List<LimitRate> result = getData(overlimit,map1,map2,map3,map4,map5,map6,map7,map8,map9,map10,map11);
            limitRateList.addAll (result);
            List<LimitRatePO> collect = result.stream ( ).map (t -> {
                LimitRatePO limitRatePO = new LimitRatePO ( );
                NjcnBeanUtil.copyPropertiesIgnoreCase (t,limitRatePO);
                limitRatePO.setTime (instant);
                return limitRatePO;
            }).collect (Collectors.toList ( ));
            limitRates.addAll (collect);
        }
        if (!CollectionUtils.isEmpty(limitRateList)){
            /*插入influxdb*/
            insert(limitRateList,time);
            /*插入mysql*/
            this.saveOrUpdateBatchByMultiId (limitRates,50);
        }
    }

    @Transactional(rollbackFor = {Exception.class})
    @SneakyThrows
    @Async("asyncExecutor")
    public void limitRate(List<String> lineList, String startTime, String endTime)  {
        log.info(LocalDateTime.now()+"r_stat_limit_rate_d开始执行=======》");
        List<RStatLimitRateDPO> list = new ArrayList<>();
        List<LimitRate> limitRateList = new ArrayList<> ();
        Date date = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = simpleDateFormat.parse(startTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        for (int i = 0; i <lineList.size () ; i++) {
            String lineIndex =lineList.get (i);
            Overlimit overlimit = lineFeignClient.getOverLimitData(lineIndex).getData ();
            if(!Optional.ofNullable (overlimit).isPresent ()){
                continue;
            }
            List<DataVPO> map1 = getAllTime(lineIndex,startTime,endTime);
            List<DataPltPO> map2 = getFlickerAllTime(lineIndex,startTime,endTime);
            List<DataHarmRateVPO> map3 = getDataHarmRateV(lineIndex,startTime,endTime);
            List<DataIPO> map4 = getDataI(lineIndex,startTime,endTime);
            List<DataInHarmVPO> map5 = getDataInHarmV(lineIndex,startTime,endTime);
            List<DataVPO> map6 = getDataV(lineIndex,startTime,endTime);
            List<DataVPO> map7 = getDataVUnbalance(lineIndex,startTime,endTime);
            List<DataIPO> map8 = getDataINeg(lineIndex,startTime,endTime);
            List<DataVPO> map9 = getDataVFreq(lineIndex,startTime,endTime);
            List<DataVPO> map10 = getDataVDev(lineIndex,startTime,endTime);
            List<DataPltPO> map11 = getDataPlt(lineIndex,startTime,endTime);

            List<LimitRate> result = getData(overlimit,map1,map2,map3,map4,map5,map6,map7,map8,map9,map10,map11);
            limitRateList.addAll (result);
            Date finalDate = date;
            List<RStatLimitRateDPO> collect = result.stream ( ).map (t -> {
                RStatLimitRateDPO po = new RStatLimitRateDPO ( );
                NjcnBeanUtil.copyPropertiesIgnoreCase (t,po);
                po.setTime(finalDate);
                return po;
            }).collect (Collectors.toList ( ));
            list.addAll(collect);
        }
        if (!CollectionUtils.isEmpty(limitRateList)){
            /*插入mysql*/
            statLimitRateDService.insert(list);
        }

        log.info(LocalDateTime.now()+"r_stat_limit_rate_d执行结束《=======");
    }



    /**
     * 功能描述:获取influxDB -> data_v -> 总计算次数(用data_v中phasic_type=A,quality_flag=0来参与统计)
     *
     * @param lineIndex 监测点index
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     * @date 2022/5/23 10:51
     */
    public List<DataVPO> getAllTime(String lineIndex, String startTime, String endTime) {
        StringBuilder stringBuilder = new StringBuilder ( );
        stringBuilder.append ("line_id='").append (lineIndex).append ("' and ").append (InfluxDBPublicParam.TIME + " >= '").append (startTime).
                append ("' and ").append (InfluxDBPublicParam.TIME).
                append (" <= '").append (endTime).
                append ("' and phasic_type = 'A' and value_type = 'AVG' and quality_flag = '0' ").
                append (InfluxDBPublicParam.TIME_ZONE);
        String sql = "SELECT count(freq) AS all_time FROM " + InfluxDBPublicParam.DATA_V + " where " + stringBuilder;
        QueryResult sqlResult = influxDbUtils.query (sql);
        InfluxDBResultMapper resultMapper = new InfluxDBResultMapper ( );
        List<DataVPO> list = resultMapper.toPOJO (sqlResult, DataVPO.class);

        return list;
    }


    /**
     * @Description: getFlickerAllTime
     * @Param: [lineIndex, startTime, endTime]
     * @return: java.util.Map<java.lang.String, java.util.List < com.njcn.prepare.harmonic.preprocessing.pojo.vo.DataPltPO>>
     * @Author: clam
     * @Date: 2022/10/18
     */
    public List<DataPltPO> getFlickerAllTime(String lineIndex, String startTime, String endTime) {
        StringBuilder stringBuilder = new StringBuilder ( );

        stringBuilder.append ("line_id='").append (lineIndex).append ("' and ").append (InfluxDBPublicParam.TIME + " >= '").
                append (startTime).append ("' and ").append (InfluxDBPublicParam.TIME).
                append (" <= '").append (endTime).
                append ("' and phasic_type = 'A' and quality_flag = '0' ").
                append (InfluxDBPublicParam.TIME_ZONE);
        String sql = "SELECT count(plt) AS flicker_all_time FROM " + InfluxDBPublicParam.DATA_PLT + " where " + stringBuilder;
        QueryResult sqlResult = influxDbUtils.query (sql);
        InfluxDBResultMapper resultMapper = new InfluxDBResultMapper ( );
        List<DataPltPO> list = resultMapper.toPOJO (sqlResult, DataPltPO.class);

        return list;
    }

    /**
     * 功能描述:获取influxDB -> data_harmrate_v -> 2-25次谐波电压含有率 -> 日95%概率值
     *
     * @param lineIndex 监测点index
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     * @date 2022/5/23 10:51
     */
    public List<DataHarmRateVPO> getDataHarmRateV(String lineIndex, String startTime, String endTime) {
        StringBuilder stringBuilder = new StringBuilder ( );
        stringBuilder.append ("line_id='").append (lineIndex).append ("' and ").append (InfluxDBPublicParam.TIME + " >= '").append (startTime).append ("' and ").
                append (InfluxDBPublicParam.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' ").
                append (InfluxDBPublicParam.TIME_ZONE);
        String sql = "SELECT * FROM " + InfluxDBPublicParam.DATA_HARM_RATE_V + " WHERE " + stringBuilder;
        QueryResult sqlResult = influxDbUtils.query (sql);
        InfluxDBResultMapper resultMapper = new InfluxDBResultMapper ( );
        List<DataHarmRateVPO> list = resultMapper.toPOJO (sqlResult, DataHarmRateVPO.class);

        return list;
    }

    /**
     * 功能描述:获取influxDB -> data_i -> 2-25次谐波电流 -> 日95%概率值
     *
     * @param lineIndex 监测点Index
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @Date: 2022/10/18
     */
    public List<DataIPO> getDataI(String lineIndex, String startTime, String endTime) {
        StringBuilder stringBuilder = new StringBuilder ( );
        stringBuilder.append ("line_id='").append (lineIndex).append ("' and ").append (InfluxDBPublicParam.TIME + " >= '").append (startTime).
                append ("' and ").append (InfluxDBPublicParam.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' ").
                append (InfluxDBPublicParam.TIME_ZONE);
        String sql = "SELECT * FROM " + InfluxDBPublicParam.DATA_I + " WHERE " + stringBuilder;
        QueryResult sqlResult = influxDbUtils.query (sql);
        InfluxDBResultMapper resultMapper = new InfluxDBResultMapper ( );
        List<DataIPO> list = resultMapper.toPOJO (sqlResult, DataIPO.class);

        return list;
    }

    /**
     * 功能描述:获取influxDB -> data_inharm_v -> 0.5-15.5次间谐波电压含有率 -> 日95%概率值
     *
     * @param lineIndex 监测点index
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     * @date 2022/5/23 10:51
     */
    public List<DataInHarmVPO> getDataInHarmV(String lineIndex, String startTime, String endTime) {
        StringBuilder stringBuilder = new StringBuilder ( );
        stringBuilder.append ("line_id='").append (lineIndex).append ("' and ").append (InfluxDBPublicParam.TIME + " >= '").append (startTime).
                append ("' and ").append (InfluxDBPublicParam.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' ").
                append (InfluxDBPublicParam.TIME_ZONE);
        String sql = "SELECT * FROM " + InfluxDBPublicParam.DATA_IN_HARM_V + " WHERE " + stringBuilder;
        QueryResult sqlResult = influxDbUtils.query (sql);
        InfluxDBResultMapper resultMapper = new InfluxDBResultMapper ( );
        List<DataInHarmVPO> list = resultMapper.toPOJO (sqlResult, DataInHarmVPO.class);

        return list;
    }

    /**
     * 功能描述:获取influxDB -> data_v -> 电压总谐波畸变率 -> 日95%概率值
     *
     * @param lineIndex 监测点Index
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     * @date 2022/5/23 10:51
     */
    public List<DataVPO> getDataV(String lineIndex, String startTime, String endTime) {
        StringBuilder stringBuilder = new StringBuilder ( );
        stringBuilder.append ("line_id='").append (lineIndex).append ("' and ").append (InfluxDBPublicParam.TIME + " >= '").append (startTime).
                append ("' and ").append (InfluxDBPublicParam.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' ").
                append (InfluxDBPublicParam.TIME_ZONE);
        String sql = "SELECT * FROM " + InfluxDBPublicParam.DATA_V + " WHERE " + stringBuilder;
        QueryResult sqlResult = influxDbUtils.query (sql);
        InfluxDBResultMapper resultMapper = new InfluxDBResultMapper ( );
        List<DataVPO> list = resultMapper.toPOJO (sqlResult, DataVPO.class);

        return list;
    }

    /**
     * 功能描述:获取influxDB -> data_v -> 负序电压不平衡度 -> 最大值 && 日95%概率值
     *
     * @param lineIndex 监测点lineIndex
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     * @date 2022/5/23 10:51
     */
    public List<DataVPO> getDataVUnbalance(String lineIndex, String startTime, String endTime) {

        StringBuilder stringBuilder = new StringBuilder ( );
        stringBuilder.append ("line_id='").append (lineIndex).append ("' and ").append (InfluxDBPublicParam.TIME + " >= '").append (startTime).
                append ("' and ").append (InfluxDBPublicParam.TIME).append (" <= '").append (endTime).
                append ("' and phasic_type = 'T' and (value_type = 'CP95' or value_type = 'MAX') and quality_flag = '0' ").
                append (InfluxDBPublicParam.TIME_ZONE);
        String sql = "SELECT * FROM " + InfluxDBPublicParam.DATA_V + " WHERE " + stringBuilder;
        QueryResult sqlResult = influxDbUtils.query (sql);
        InfluxDBResultMapper resultMapper = new InfluxDBResultMapper ( );
        List<DataVPO> list = resultMapper.toPOJO (sqlResult, DataVPO.class);

        return list;
    }

    /**
     * 功能描述:获取influxDB -> data_i -> 负序电流 -> 最大值 && 日95%概率值
     *
     * @param lineIndex 监测点lineIndex
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     * @date 2022/5/23 10:51
     */
    public List<DataIPO> getDataINeg(String lineIndex, String startTime, String endTime) {
        Map<String, List<DataIPO>> map = new HashMap<> ( );
        StringBuilder stringBuilder = new StringBuilder ( );
        stringBuilder.append ("line_id='").append (lineIndex).append ("' and ").append (InfluxDBPublicParam.TIME + " >= '").
                append (startTime).append ("' and ").append (InfluxDBPublicParam.TIME).append (" <= '").append (endTime).
                append ("' and phasic_type = 'T' and (value_type = 'CP95' or value_type = 'MAX') and quality_flag = '0' ").
                append (InfluxDBPublicParam.TIME_ZONE);
        String sql = "SELECT * FROM " + InfluxDBPublicParam.DATA_I + " WHERE " + stringBuilder;
        QueryResult sqlResult = influxDbUtils.query (sql);
        InfluxDBResultMapper resultMapper = new InfluxDBResultMapper ( );
        List<DataIPO> list = resultMapper.toPOJO (sqlResult, DataIPO.class);

        return list;
    }

    /**
     * 功能描述:获取influxDB -> data_v -> 频率偏差 -> 最大值 && 最小值
     *
     * @param lineIndex 监测点lineIndex
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     * @date 2022/5/23 10:51
     */
    public List<DataVPO> getDataVFreq(String lineIndex, String startTime, String endTime) {
        StringBuilder stringBuilder = new StringBuilder ( );
        stringBuilder.append ("line_id='").append (lineIndex).append ("' and ").append (InfluxDBPublicParam.TIME + " >= '").append (startTime).append ("' and ").
                append (InfluxDBPublicParam.TIME).append (" <= '").
                append (endTime).
                append ("' and phasic_type = 'T' and (value_type = 'MIN' or value_type = 'MAX') and quality_flag = '0' ").
                append (InfluxDBPublicParam.TIME_ZONE);
        String sql = "SELECT * FROM " + InfluxDBPublicParam.DATA_V + " WHERE " + stringBuilder;
        QueryResult sqlResult = influxDbUtils.query (sql);
        InfluxDBResultMapper resultMapper = new InfluxDBResultMapper ( );
        List<DataVPO> list = resultMapper.toPOJO (sqlResult, DataVPO.class);

        return list;
    }

    /**
     * 功能描述:获取influxDB -> data_v -> 电压偏差 -> 最大值
     *
     * @param lineIndex 监测点lineIndex
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     * @date 2022/5/23 10:51
     */
    public List<DataVPO> getDataVDev(String lineIndex, String startTime, String endTime) {
        StringBuilder stringBuilder = new StringBuilder ( );
        stringBuilder.append ("line_id='").append (lineIndex).append ("' and ").append (InfluxDBPublicParam.TIME + " >= '").
                append (startTime).append ("' and ").append (InfluxDBPublicParam.TIME).append (" <= '").append (endTime).
                append ("' and (phasic_type = 'A' or phasic_type = 'B' or phasic_type = 'C') and value_type = 'MAX' and quality_flag = '0' ").
                append (InfluxDBPublicParam.TIME_ZONE);
        String sql = "SELECT * FROM " + InfluxDBPublicParam.DATA_V + " WHERE " + stringBuilder;
        QueryResult sqlResult = influxDbUtils.query (sql);
        InfluxDBResultMapper resultMapper = new InfluxDBResultMapper ( );
        List<DataVPO> list = resultMapper.toPOJO (sqlResult, DataVPO.class);

        return list;
    }

    /**
     * 功能描述:获取influxDB -> data_plt -> 长时间闪变 -> 最大值
     *
     * @param lineIndex 监测点lineIndex
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     * @date 2022/5/23 10:51
     */
    public List<DataPltPO> getDataPlt(String lineIndex, String startTime, String endTime) {
        StringBuilder stringBuilder = new StringBuilder ( );
        stringBuilder.append ("line_id='").append (lineIndex).append ("' and ").append (InfluxDBPublicParam.TIME + " >= '").append (startTime).
                append ("' and ").append (InfluxDBPublicParam.TIME).append (" <= '").append (endTime).
                append ("' and (phasic_type = 'A' or phasic_type = 'B' or phasic_type = 'C') and quality_flag = '0' ").
                append (InfluxDBPublicParam.TIME_ZONE);
        String sql = "SELECT * FROM " + InfluxDBPublicParam.DATA_PLT + " WHERE " + stringBuilder;
        QueryResult sqlResult = influxDbUtils.query (sql);
        InfluxDBResultMapper resultMapper = new InfluxDBResultMapper ( );
        List<DataPltPO> list = resultMapper.toPOJO (sqlResult, DataPltPO.class);

        return list;
    }

    /**
    * @Description: getData
    * @Param: [overlimit, allTimeList, flickerAllTimeList, dataHarmRateVPOList, dataIPOList, dataInHarmVPOList, dataVPOList, dataVPOUnbalanceList, dataIPONegList, dataVPOFreqList, dataVPODevList, dataPltPOList]
    * @return: java.util.List<com.njcn.harmonic.pojo.po.LimitRate>
    * @Author: clam
    * @Date: 2022/10/18
    */
    private List<LimitRate> getData(Overlimit overlimit, List<DataVPO> allTimeList,
                                    List<DataPltPO> flickerAllTimeList, List<DataHarmRateVPO> dataHarmRateVPOList,
                                    List<DataIPO> dataIPOList, List<DataInHarmVPO> dataInHarmVPOList,
                                    List<DataVPO> dataVPOList, List<DataVPO> dataVPOUnbalanceList, List<DataIPO> dataIPONegList,
                                    List<DataVPO> dataVPOFreqList, List<DataVPO> dataVPODevList, List<DataPltPO> dataPltPOList) {
        List<LimitRate> result = new ArrayList<> ( );

        Map<String, List<DataHarmRateVPO>> harmRateVByPhaseType = new HashMap<> ( );
        Map<String, List<DataIPO>> dataIByPhaseType = new HashMap<> ( );
        Map<String, List<DataInHarmVPO>> dataInHarmVPhaseType = new HashMap<> ( );
        Map<String, List<DataVPO>> dataVPhaseType = new HashMap<> ( );
        Map<String, List<DataVPO>> dataVDevPhaseType = new HashMap<> ( );
        Map<String, List<DataPltPO>> dataPltPhaseType = new HashMap<> ( );
        Map<Instant, List<DataHarmRateVPO>> harmRateVByTime = new HashMap<> ( );
        Map<Instant, List<DataIPO>> dataIByTime = new HashMap<> ( );
        Map<Instant, List<DataInHarmVPO>> dataInHarmVByTime = new HashMap<> ( );
        Map<Instant, List<DataVPO>> dataVByTime = new HashMap<> ( );
        Map<Instant, List<DataVPO>> dataVUnbalanceByTime = new HashMap<> ( );
        Map<Instant, List<DataIPO>> dataINegByTime = new HashMap<> ( );
        Map<Instant, List<DataVPO>> dataVFreqByTime = new HashMap<> ( );
        Map<Instant, List<DataVPO>> dataVDevByTime = new HashMap<> ( );
        Map<Instant, List<DataPltPO>> dataPltByTime = new HashMap<> ( );
        Integer allTime = 0;
        Integer flickerAllTime = 0;
        if (!CollectionUtils.isEmpty (allTimeList)) {
            allTime = allTimeList.get (0).getAllTime ( );
        }
        if (!CollectionUtils.isEmpty (flickerAllTimeList)) {
            flickerAllTime = flickerAllTimeList.get (0).getFlickerAllTime ();        }



        if (!CollectionUtils.isEmpty (dataHarmRateVPOList)) {
            harmRateVByPhaseType = dataHarmRateVPOList.stream ( ).collect (Collectors.groupingBy (DataHarmRateVPO::getPhaseType));
            harmRateVByTime = dataHarmRateVPOList.stream ( ).collect (Collectors.groupingBy (DataHarmRateVPO::getTime));
        }
        if (!CollectionUtils.isEmpty (dataIPOList)) {
            dataIByPhaseType = dataIPOList.stream ( ).collect (Collectors.groupingBy (DataIPO::getPhaseType));
            dataIByTime = dataIPOList.stream ( ).collect (Collectors.groupingBy (DataIPO::getTime));
        }
        if (!CollectionUtils.isEmpty (dataInHarmVPOList)) {
            dataInHarmVPhaseType = dataInHarmVPOList.stream ( ).collect (Collectors.groupingBy (DataInHarmVPO::getPhaseType));
            dataInHarmVByTime = dataInHarmVPOList.stream ( ).collect (Collectors.groupingBy (DataInHarmVPO::getTime));
        }
        if (!CollectionUtils.isEmpty (dataVPOList)) {
            dataVPhaseType = dataVPOList.stream ( ).collect (Collectors.groupingBy (DataVPO::getPhaseType));
            dataVByTime = dataVPOList.stream ( ).collect (Collectors.groupingBy (DataVPO::getTime));
        }
        if (!CollectionUtils.isEmpty (dataVPOUnbalanceList)) {
            dataVUnbalanceByTime = dataVPOUnbalanceList.stream ( ).collect (Collectors.groupingBy (DataVPO::getTime));
        }
        if (!CollectionUtils.isEmpty (dataIPONegList)) {
            dataINegByTime = dataIPONegList.stream ( ).collect (Collectors.groupingBy (DataIPO::getTime));
        }
        if (!CollectionUtils.isEmpty (dataVPOFreqList)) {
            dataVFreqByTime = dataVPOFreqList.stream ( ).collect (Collectors.groupingBy (DataVPO::getTime));
        }
        if (!CollectionUtils.isEmpty (dataVPODevList)) {
            dataVDevPhaseType = dataVPODevList.stream ( ).collect (Collectors.groupingBy (DataVPO::getPhaseType));
            dataVDevByTime = dataVPODevList.stream ( ).collect (Collectors.groupingBy (DataVPO::getTime));
        }
        if (!CollectionUtils.isEmpty (dataPltPOList)) {
            dataPltPhaseType = dataPltPOList.stream ( ).collect (Collectors.groupingBy (DataPltPO::getPhaseType));
            dataPltByTime = dataPltPOList.stream ( ).collect (Collectors.groupingBy (DataPltPO::getTime));
        }
        LimitRate a = limitRateData (overlimit, allTime, flickerAllTime, "A", overlimit.getId (), harmRateVByPhaseType.get ("A"), dataIByPhaseType.get ("A"), dataInHarmVPhaseType.get ("A"), dataVPhaseType.get ("A"), dataVDevPhaseType.get ("A"), dataPltPhaseType.get ("A"));
        LimitRate b = limitRateData (overlimit, allTime, flickerAllTime, "B", overlimit.getId (), harmRateVByPhaseType.get ("B"), dataIByPhaseType.get ("B"), dataInHarmVPhaseType.get ("B"), dataVPhaseType.get ("B"), dataVDevPhaseType.get ("B"), dataPltPhaseType.get ("B"));
        LimitRate c = limitRateData (overlimit, allTime, flickerAllTime, "C", overlimit.getId (), harmRateVByPhaseType.get ("C"), dataIByPhaseType.get ("C"), dataInHarmVPhaseType.get ("C"), dataVPhaseType.get ("C"), dataVDevPhaseType.get ("C"), dataPltPhaseType.get ("C"));
        LimitRate t = limitRateDataT (overlimit, allTime, flickerAllTime, overlimit.getId (), harmRateVByTime, dataIByTime, dataInHarmVByTime, dataVByTime, dataVUnbalanceByTime, dataINegByTime, dataVFreqByTime, dataVDevByTime, dataPltByTime);
        result.add (a);
        result.add (b);
        result.add (c);
        result.add (t);

        return result;
    }


    /**
    * @Description: limitRateData
    * @Param: [overlimit, allTime, flickerAllTime, phasicType, lineId, dataHarmRateVPOList, dataIPOList, dataInHarmVPOList, dataVPOList, dataVUnbalanceList, dataINegList]
    * @return: com.njcn.harmonic.pojo.po.LimitRate
    * @Author: clam
    * @Date: 2022/10/18
    */
    public LimitRate limitRateData(Overlimit overlimit, Integer allTime, Integer
            flickerAllTime, String phasicType, String lineId,
                                   List<DataHarmRateVPO> dataHarmRateVPOList,
                                   List<DataIPO> dataIPOList, List<DataInHarmVPO> dataInHarmVPOList,
                                   List<DataVPO> dataVPOList, List<DataVPO> dataVPODevList, List<DataPltPO> dataPltPOList) {
        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 (dataHarmRateVPOList)) {
            for (DataHarmRateVPO item : dataHarmRateVPOList) {
                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 (dataIPOList)) {
            for (DataIPO item : dataIPOList) {
                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 (dataInHarmVPOList)) {
            for (DataInHarmVPO item : dataInHarmVPOList) {
                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 (dataVPOList)) {
            for (DataVPO item : dataVPOList) {
                if (item.getVThd ( ) > overlimit.getUaberrance ( )) {
                    thd++;
                }
            }
        }
        //电压上偏差、电压下偏差(根据vl_dev的正负判断是用哪个值判断越限)
        if (!CollectionUtils.isEmpty (dataVPODevList)) {
            for (DataVPO item : dataVPODevList) {
                if (item.getVlDev() >= 0){
                    if (item.getVlDev() > overlimit.getVoltageDev()){
                        uDev++;
                    }
                } else {
                    if (Math.abs(item.getVlDev()) > Math.abs(overlimit.getUvoltageDev())){
                        uDev++;
                    }
                }
            }
        }
        //长时间闪变
        if (!CollectionUtils.isEmpty (dataPltPOList)) {
            for (DataPltPO item : dataPltPOList) {
                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;
    }


    public LimitRate limitRateDataT(Overlimit overlimit, Integer allTime, Integer flickerAllTime, String lineId, Map<Instant,List<DataHarmRateVPO>> map1, Map<Instant,List<DataIPO>> map2, Map<Instant,List<DataInHarmVPO>> map3, Map<Instant,List<DataVPO>> map4, Map<Instant,List<DataVPO>> map5, Map<Instant,List<DataIPO>> map6, Map<Instant,List<DataVPO>> map7, Map<Instant,List<DataVPO>> map8, Map<Instant,List<DataPltPO>> 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<DataHarmRateVPO> list = map1.get(time);
            List<DataHarmRateVPO> l2 = list.stream().filter(s->s.getV2()>overlimit.getUharm2()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l2)){
                v2++;
            }
            List<DataHarmRateVPO> l3 = list.stream().filter(s->s.getV3()>overlimit.getUharm3()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l3)){
                v3++;
            }
            List<DataHarmRateVPO> l4 = list.stream().filter(s->s.getV4()>overlimit.getUharm4()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l4)){
                v4++;
            }
            List<DataHarmRateVPO> l5 = list.stream().filter(s->s.getV5()>overlimit.getUharm5()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l5)){
                v5++;
            }
            List<DataHarmRateVPO> l6 = list.stream().filter(s->s.getV6()>overlimit.getUharm6()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l6)){
                v6++;
            }
            List<DataHarmRateVPO> l7 = list.stream().filter(s->s.getV7()>overlimit.getUharm7()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l7)){
                v7++;
            }
            List<DataHarmRateVPO> l8 = list.stream().filter(s->s.getV8()>overlimit.getUharm8()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l8)){
                v8++;
            }
            List<DataHarmRateVPO> l9 = list.stream().filter(s->s.getV9()>overlimit.getUharm9()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l9)){
                v9++;
            }
            List<DataHarmRateVPO> l10 = list.stream().filter(s->s.getV10()>overlimit.getUharm10()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l10)){
                v10++;
            }
            List<DataHarmRateVPO> l11 = list.stream().filter(s->s.getV11()>overlimit.getUharm11()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l11)){
                v11++;
            }
            List<DataHarmRateVPO> l12 = list.stream().filter(s->s.getV12()>overlimit.getUharm12()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l12)){
                v12++;
            }
            List<DataHarmRateVPO> l13 = list.stream().filter(s->s.getV13()>overlimit.getUharm13()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l13)){
                v13++;
            }
            List<DataHarmRateVPO> l14 = list.stream().filter(s->s.getV14()>overlimit.getUharm14()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l14)){
                v14++;
            }
            List<DataHarmRateVPO> l15 = list.stream().filter(s->s.getV15()>overlimit.getUharm15()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l15)){
                v15++;
            }
            List<DataHarmRateVPO> l16 = list.stream().filter(s->s.getV16()>overlimit.getUharm16()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l16)){
                v16++;
            }
            List<DataHarmRateVPO> l17 = list.stream().filter(s->s.getV17()>overlimit.getUharm17()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l17)){
                v17++;
            }
            List<DataHarmRateVPO> l18 = list.stream().filter(s->s.getV18()>overlimit.getUharm18()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l18)){
                v18++;
            }
            List<DataHarmRateVPO> l19 = list.stream().filter(s->s.getV19()>overlimit.getUharm19()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l19)){
                v19++;
            }
            List<DataHarmRateVPO> l20 = list.stream().filter(s->s.getV20()>overlimit.getUharm20()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l20)){
                v20++;
            }
            List<DataHarmRateVPO> l21 = list.stream().filter(s->s.getV21()>overlimit.getUharm21()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l21)){
                v21++;
            }
            List<DataHarmRateVPO> l22 = list.stream().filter(s->s.getV22()>overlimit.getUharm22()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l22)){
                v22++;
            }
            List<DataHarmRateVPO> l23 = list.stream().filter(s->s.getV23()>overlimit.getUharm23()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l23)){
                v23++;
            }
            List<DataHarmRateVPO> l24 = list.stream().filter(s->s.getV24()>overlimit.getUharm24()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l24)){
                v24++;
            }
            List<DataHarmRateVPO> 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<DataIPO> list = map2.get(time);
            List<DataIPO> l2 = list.stream().filter(s->s.getI2()>overlimit.getIharm2()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l2)){
                i2++;
            }
            List<DataIPO> l3 = list.stream().filter(s->s.getI3()>overlimit.getIharm3()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l3)){
                i3++;
            }
            List<DataIPO> l4 = list.stream().filter(s->s.getI4()>overlimit.getIharm4()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l4)){
                i4++;
            }
            List<DataIPO> l5 = list.stream().filter(s->s.getI5()>overlimit.getIharm5()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l5)){
                i5++;
            }
            List<DataIPO> l6 = list.stream().filter(s->s.getI6()>overlimit.getIharm6()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l6)){
                i6++;
            }
            List<DataIPO> l7 = list.stream().filter(s->s.getI7()>overlimit.getIharm7()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l7)){
                i7++;
            }
            List<DataIPO> l8 = list.stream().filter(s->s.getI8()>overlimit.getIharm8()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l8)){
                i8++;
            }
            List<DataIPO> l9 = list.stream().filter(s->s.getI9()>overlimit.getIharm9()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l9)){
                i9++;
            }
            List<DataIPO> l10 = list.stream().filter(s->s.getI10()>overlimit.getIharm10()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l10)){
                i10++;
            }
            List<DataIPO> l11 = list.stream().filter(s->s.getI11()>overlimit.getIharm11()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l11)){
                i11++;
            }
            List<DataIPO> l12 = list.stream().filter(s->s.getI12()>overlimit.getIharm12()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l12)){
                i12++;
            }
            List<DataIPO> l13 = list.stream().filter(s->s.getI13()>overlimit.getIharm13()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l13)){
                i13++;
            }
            List<DataIPO> l14 = list.stream().filter(s->s.getI14()>overlimit.getIharm14()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l14)){
                i14++;
            }
            List<DataIPO> l15 = list.stream().filter(s->s.getI15()>overlimit.getIharm15()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l15)){
                i15++;
            }
            List<DataIPO> l16 = list.stream().filter(s->s.getI16()>overlimit.getIharm16()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l16)){
                i16++;
            }
            List<DataIPO> l17 = list.stream().filter(s->s.getI17()>overlimit.getIharm17()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l17)){
                i17++;
            }
            List<DataIPO> l18 = list.stream().filter(s->s.getI18()>overlimit.getIharm18()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l18)){
                i18++;
            }
            List<DataIPO> l19 = list.stream().filter(s->s.getI19()>overlimit.getIharm19()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l19)){
                i19++;
            }
            List<DataIPO> l20 = list.stream().filter(s->s.getI20()>overlimit.getIharm20()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l20)){
                i20++;
            }
            List<DataIPO> l21 = list.stream().filter(s->s.getI21()>overlimit.getIharm21()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l21)){
                i21++;
            }
            List<DataIPO> l22 = list.stream().filter(s->s.getI22()>overlimit.getIharm22()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l22)){
                i22++;
            }
            List<DataIPO> l23 = list.stream().filter(s->s.getI23()>overlimit.getIharm23()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l23)){
                i23++;
            }
            List<DataIPO> l24 = list.stream().filter(s->s.getI24()>overlimit.getIharm24()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l24)){
                i24++;
            }
            List<DataIPO> 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<DataInHarmVPO> list3 = map3.get(time);
            List<DataInHarmVPO> l2 = list3.stream().filter(s->s.getV1()>overlimit.getInuharm1()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l2)){
                uHarm1++;
            }
            List<DataInHarmVPO> l3 = list3.stream().filter(s->s.getV2()>overlimit.getInuharm2()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l3)){
                uHarm2++;
            }
            List<DataInHarmVPO> l4 = list3.stream().filter(s->s.getV3()>overlimit.getInuharm3()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l4)){
                uHarm3++;
            }
            List<DataInHarmVPO> l5 = list3.stream().filter(s->s.getV4()>overlimit.getInuharm4()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l5)){
                uHarm4++;
            }
            List<DataInHarmVPO> l6 = list3.stream().filter(s->s.getV5()>overlimit.getInuharm5()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l6)){
                uHarm5++;
            }
            List<DataInHarmVPO> l7 = list3.stream().filter(s->s.getV6()>overlimit.getInuharm6()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l7)){
                uHarm6++;
            }
            List<DataInHarmVPO> l8 = list3.stream().filter(s->s.getV7()>overlimit.getInuharm7()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l8)){
                uHarm7++;
            }
            List<DataInHarmVPO> l9 = list3.stream().filter(s->s.getV8()>overlimit.getInuharm8()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l9)){
                uHarm8++;
            }
            List<DataInHarmVPO> l10 = list3.stream().filter(s->s.getV9()>overlimit.getInuharm9()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l10)){
                uHarm9++;
            }
            List<DataInHarmVPO> l11 = list3.stream().filter(s->s.getV10()>overlimit.getInuharm10()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l11)){
                uHarm10++;
            }
            List<DataInHarmVPO> l12 = list3.stream().filter(s->s.getV11()>overlimit.getInuharm11()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l12)){
                uHarm11++;
            }
            List<DataInHarmVPO> l13 = list3.stream().filter(s->s.getV12()>overlimit.getInuharm12()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l13)){
                uHarm12++;
            }
            List<DataInHarmVPO> l14 = list3.stream().filter(s->s.getV13()>overlimit.getInuharm13()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l14)){
                uHarm13++;
            }
            List<DataInHarmVPO> l15 = list3.stream().filter(s->s.getV14()>overlimit.getInuharm14()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l15)){
                uHarm14++;
            }
            List<DataInHarmVPO> l16 = list3.stream().filter(s->s.getV15()>overlimit.getInuharm15()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l16)){
                uHarm15++;
            }
            List<DataInHarmVPO> l17 = list3.stream().filter(s->s.getV16()>overlimit.getInuharm16()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l17)){
                uHarm16++;
            }
        }
        //电压总谐波畸变率
        for (Instant time:map4.keySet()) {
            List<DataVPO> list = map4.get(time);
            List<DataVPO> l = list.stream().filter(s->s.getVThd()>overlimit.getUaberrance()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l)){
                thd++;
            }
        }
        //三相电压不平衡度
        for (Instant time:map5.keySet()) {
            List<DataVPO> list = map5.get(time);
            List<DataVPO> l = list.stream().filter(s->s.getVUnbalance()>overlimit.getUbalance()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l)){
                uAberrance++;
            }
        }
        //负序电流
        for (Instant time:map6.keySet()) {
            List<DataIPO> list = map6.get(time);
            List<DataIPO> l = list.stream().filter(s->s.getINeg()>overlimit.getINeg()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l)){
                iNeg++;
            }
        }
        //频率偏差
        for (Instant time:map7.keySet()) {
            List<DataVPO> list = map7.get(time);
            List<DataVPO> l = list.stream().filter(s->s.getFreqDev()>overlimit.getFreqDev()).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l)){
                freqDev++;
            }
        }
        //电压上偏差、电压下偏差
        for (Instant time:map8.keySet()) {
            List<DataVPO> list = map8.get(time);
            List<DataVPO> l1 = list.stream().filter(s->s.getVlDev()>=0&&s.getVlDev()>overlimit.getVoltageDev()).collect(Collectors.toList());
            List<DataVPO> l2 = list.stream().filter(s->s.getVlDev()<0&&Math.abs(s.getVlDev())>Math.abs(overlimit.getUvoltageDev())).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(l1) || !CollectionUtils.isEmpty(l2)){
                uDev++;
            }
        }
        //长时间闪变
        for (Instant time:map9.keySet()) {
            List<DataPltPO> list = map9.get(time);
            List<DataPltPO> 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;
    }


    /**
    * @Description: 插入influxdb
    * @Param: [list, time]
    * @return: void
    * @Author: clam
    * @Date: 2022/10/21
    */
    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(InfluxDBPublicParam.LIMIT_RATE, time, TimeUnit.MILLISECONDS, tags, fields);
            BatchPoints batchPoints = BatchPoints.database(influxDbUtils.getDbName ()).tag(InfluxDBPublicParam.LINE_ID, item.getLineId()).tag(InfluxDBPublicParam.PHASIC_TYPE,item.getPhasicType()).retentionPolicy("").consistency(InfluxDB.ConsistencyLevel.ALL).build();
            batchPoints.point(point);
            records.add(batchPoints.lineProtocol());
        });
        influxDbUtils.batchInsert(influxDbUtils.getDbName (),"", InfluxDB.ConsistencyLevel.ALL, records);
    }

    public static void main(String[] args) {
        LimitRate limitRate = new LimitRate();
        limitRate.setAllTime (0);
        limitRate.setFlickerAllTime (0);
        LimitRatePO limitRatePO = new LimitRatePO ();
        BeanUtils.copyProperties (limitRate, limitRatePO);
        System.out.println ("limitRatePO:"+ limitRatePO.getAllTime ());
    }



}
