package com.njcn.harmonic.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.NumberUtil;
import com.njcn.device.api.GeneralDeviceInfoClient;
import com.njcn.device.enums.LineBaseEnum;
import com.njcn.device.pojo.dto.GeneralDeviceDTO;
import com.njcn.device.pojo.param.DeviceInfoParam;
import com.njcn.harmonic.constant.Param;
import com.njcn.harmonic.mapper.SteadyQualifyMapper;
import com.njcn.harmonic.pojo.dto.SteadyQualifyDTO;
import com.njcn.harmonic.pojo.vo.SteadyQualifyCensusVO;
import com.njcn.harmonic.pojo.vo.SteadyQualifyVO;
import com.njcn.harmonic.service.SteadyQualifyService;
import com.njcn.influxdb.utils.InfluxDbUtils;
import lombok.AllArgsConstructor;
import org.influxdb.dto.QueryResult;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: chenchao
 * @date: 2022/04/01 16:10
 * @Description: <描述>
 */
@Service
@AllArgsConstructor
public class SteadyQualifyServiceImpl implements SteadyQualifyService {

    private final GeneralDeviceInfoClient generalDeviceInfoClient;

    private final SteadyQualifyMapper steadyQualifyMapper;

    private final InfluxDbUtils influxDbUtils;

    @Override
    public List<SteadyQualifyVO> getSteadyQualifyData(DeviceInfoParam.BusinessParam steadyParam) {
        List<SteadyQualifyVO> steadyQualifyList = new ArrayList<>();

        //按照条件获取实际运行终端综合信息
        List<GeneralDeviceDTO> deviceDataList = generalDeviceInfoClient.getPracticalRunDeviceInfo(steadyParam).getData();
        //组装监测点Top层集合
        for (GeneralDeviceDTO  generalDeviceDTO: deviceDataList) {
            List<String> lineIndexes = generalDeviceDTO.getLineIndexes();
            if (CollectionUtils.isEmpty(lineIndexes)) {
                continue;
            }
            SteadyQualifyVO steadyQualifyVO = new SteadyQualifyVO();
            steadyQualifyVO.setId(generalDeviceDTO.getIndex());
            steadyQualifyVO.setName(generalDeviceDTO.getName());
            // List<SteadyQualifyVO> lineData = steadyQualifyMapper.getSteadyQualifyData(lineIndexes);
            List<SteadyQualifyDTO> qualifiesRate = getQualifiesRate(lineIndexes, steadyParam.getSearchBeginTime(), steadyParam.getSearchEndTime());
            SteadyQualifyVO dataMoreMonitorMoreDay = getDataMoreMonitorMoreDay(qualifiesRate);
            steadyQualifyVO.setFlicker(dataMoreMonitorMoreDay.getFlicker());
            steadyQualifyVO.setFreqOffset(dataMoreMonitorMoreDay.getFreqOffset());
            steadyQualifyVO.setVoltageOffset(dataMoreMonitorMoreDay.getVoltageOffset());
            steadyQualifyVO.setVoltageUnbalance(dataMoreMonitorMoreDay.getVoltageUnbalance());
            steadyQualifyVO.setNegativeCurrent(dataMoreMonitorMoreDay.getNegativeCurrent());
            steadyQualifyVO.setHarmonicVoltage(dataMoreMonitorMoreDay.getHarmonicVoltage());
            steadyQualifyVO.setHarmonicCurrent(dataMoreMonitorMoreDay.getHarmonicCurrent());
            steadyQualifyVO.setInterHarmonic(dataMoreMonitorMoreDay.getInterHarmonic());
            //组装父级数据树
            List<SteadyQualifyVO> treeList = getTreeData(lineIndexes, steadyParam);
            steadyQualifyVO.setChildren(treeList);
            steadyQualifyList.add(steadyQualifyVO);
        }
        return steadyQualifyList;
    }

    @Override
    public SteadyQualifyCensusVO getSteadyQualifyCensus(DeviceInfoParam.BusinessParam steadyCensusParam) {
        SteadyQualifyCensusVO steadyQualifyCensusVO = new SteadyQualifyCensusVO();
        List<String> type = new ArrayList<>();
        //按照条件获取实际运行终端综合信息
        List<GeneralDeviceDTO> deviceDataList = generalDeviceInfoClient.getPracticalRunDeviceInfo(steadyCensusParam).getData();
        List<Double> harmonicVoltage = new ArrayList<>(), voltageOffset = new ArrayList<>(),
                VoltageUnbalance = new ArrayList<>(), interHarmonic = new ArrayList<>(),
                harmonicCurrent = new ArrayList<>(), negativeCurrent = new ArrayList<>(),
                freqOffset = new ArrayList<>(), flicker = new ArrayList<>();
        if (!CollectionUtils.isEmpty(deviceDataList)) {
            for (GeneralDeviceDTO generalDeviceDTO: deviceDataList){
                List<String> lineIndexes = generalDeviceDTO.getLineIndexes();
                if (CollectionUtils.isEmpty(lineIndexes)) {
                    continue;
                }
                type.add(generalDeviceDTO.getName());
                List<SteadyQualifyDTO> qualifiesRate = getQualifiesRate(lineIndexes, steadyCensusParam.getSearchBeginTime(), steadyCensusParam.getSearchEndTime());
                SteadyQualifyVO dataMoreMonitorMoreDay = getDataMoreMonitorMoreDay(qualifiesRate);
                harmonicVoltage.add(dataMoreMonitorMoreDay.getHarmonicVoltage());
                voltageOffset.add(dataMoreMonitorMoreDay.getVoltageOffset());
                VoltageUnbalance.add(dataMoreMonitorMoreDay.getVoltageUnbalance());
                interHarmonic.add(dataMoreMonitorMoreDay.getInterHarmonic());
                harmonicCurrent.add(dataMoreMonitorMoreDay.getHarmonicCurrent());
                negativeCurrent.add(dataMoreMonitorMoreDay.getNegativeCurrent());
                freqOffset.add(dataMoreMonitorMoreDay.getFreqOffset());
                flicker.add(dataMoreMonitorMoreDay.getFlicker());
            }
        }
        steadyQualifyCensusVO.setHarmonicVoltage(harmonicVoltage);
        steadyQualifyCensusVO.setVoltageOffset(voltageOffset);
        steadyQualifyCensusVO.setVoltageUnbalance(VoltageUnbalance);
        steadyQualifyCensusVO.setInterHarmonic(interHarmonic);
        steadyQualifyCensusVO.setHarmonicCurrent(harmonicCurrent);
        steadyQualifyCensusVO.setNegativeCurrent(negativeCurrent);
        steadyQualifyCensusVO.setFreqOffset(freqOffset);
        steadyQualifyCensusVO.setFlicker(flicker);
        steadyQualifyCensusVO.setType(type);

        return steadyQualifyCensusVO;
    }

    /**
     * 获取父级每层数据
     */
    private List<SteadyQualifyVO> getTreeData(List<String> lineIndexes, DeviceInfoParam.BusinessParam steadyParam) {
        //监测点集合
        List<SteadyQualifyVO> monitorList = steadyQualifyMapper.getSteadyQualifyData(lineIndexes);
        //母线集合
        List<SteadyQualifyVO> busBarList = steadyQualifyMapper.getLineInfoByList(monitorList.stream().map(SteadyQualifyVO::getPid).distinct().collect(Collectors.toList()));
        //终端集合
        List<SteadyQualifyVO> deviceList = steadyQualifyMapper.getLineInfoByList(busBarList.stream().map(SteadyQualifyVO::getPid).distinct().collect(Collectors.toList()));
        //变电站集合
        List<SteadyQualifyVO> substationList = steadyQualifyMapper.getLineInfoByList(deviceList.stream().map(SteadyQualifyVO::getPid).distinct().collect(Collectors.toList()));
        //供电公司集合
        List<SteadyQualifyVO> powerCompanyList = steadyQualifyMapper.getLineInfoByList(substationList.stream().map(SteadyQualifyVO::getPid).distinct().collect(Collectors.toList()));

        List<SteadyQualifyDTO> qualifiesRate = getQualifiesRate(lineIndexes, steadyParam.getSearchBeginTime(), steadyParam.getSearchEndTime());
        monitorList.forEach(steadyQualifyVO -> {
            SteadyQualifyVO dataSingleMonitorMoreDay = getDataSingleMonitorMoreDay(qualifiesRate, steadyQualifyVO.getId());
            steadyQualifyVO.setFlicker(dataSingleMonitorMoreDay.getFlicker());
            steadyQualifyVO.setFreqOffset(dataSingleMonitorMoreDay.getFreqOffset());
            steadyQualifyVO.setVoltageOffset(dataSingleMonitorMoreDay.getVoltageOffset());
            steadyQualifyVO.setVoltageUnbalance(dataSingleMonitorMoreDay.getVoltageUnbalance());
            steadyQualifyVO.setNegativeCurrent(dataSingleMonitorMoreDay.getNegativeCurrent());
            steadyQualifyVO.setHarmonicVoltage(dataSingleMonitorMoreDay.getHarmonicVoltage());
            steadyQualifyVO.setHarmonicCurrent(dataSingleMonitorMoreDay.getHarmonicCurrent());
            steadyQualifyVO.setInterHarmonic(dataSingleMonitorMoreDay.getInterHarmonic());
        });
        setChildesList(substationList,monitorList);
        setFatherDistortion(substationList);

        powerCompanyList.stream().peek(item -> item.setChildren(getChildCategoryList(item, substationList))).collect(Collectors.toList());
        setFatherDistortion(powerCompanyList);

        return powerCompanyList;
    }

    /**
     * 计算父级合格率
     */
    private void setFatherDistortion(List<SteadyQualifyVO> list) {
        list.forEach(steadyQualifyVO -> {
            List<SteadyQualifyVO> children = steadyQualifyVO.getChildren();
            List<SteadyQualifyVO> flicker = children.stream().filter(child -> !Objects.equals(child.getFlicker(), 3.14159)).collect(Collectors.toList());
            List<SteadyQualifyVO> freqOffset = children.stream().filter(child -> !Objects.equals(child.getFreqOffset(), 3.14159)).collect(Collectors.toList());
            List<SteadyQualifyVO> voltageOffset = children.stream().filter(child -> !Objects.equals(child.getVoltageOffset(), 3.14159)).collect(Collectors.toList());
            List<SteadyQualifyVO> voltageUnbalance = children.stream().filter(child -> !Objects.equals(child.getVoltageUnbalance(), 3.14159)).collect(Collectors.toList());
            List<SteadyQualifyVO> negativeCurrent = children.stream().filter(child -> !Objects.equals(child.getNegativeCurrent(), 3.14159)).collect(Collectors.toList());
            List<SteadyQualifyVO> harmonicVoltage = children.stream().filter(child -> !Objects.equals(child.getHarmonicVoltage(), 3.14159)).collect(Collectors.toList());
            List<SteadyQualifyVO> harmonicCurrent = children.stream().filter(child -> !Objects.equals(child.getHarmonicCurrent(), 3.14159)).collect(Collectors.toList());
            List<SteadyQualifyVO> InterHarmonic = children.stream().filter(child -> !Objects.equals(child.getInterHarmonic(), 3.14159)).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(flicker)) {
                steadyQualifyVO.setFlicker(NumberUtil.round(flicker.stream().mapToDouble(SteadyQualifyVO::getFlicker).average().orElse(100), 2).doubleValue());
            }
            if (!CollectionUtils.isEmpty(freqOffset)) {
                steadyQualifyVO.setFreqOffset(NumberUtil.round(freqOffset.stream().mapToDouble(SteadyQualifyVO::getFreqOffset).average().orElse(100), 2).doubleValue());
            }
            if (!CollectionUtils.isEmpty(voltageOffset)) {
                steadyQualifyVO.setVoltageOffset(NumberUtil.round(voltageOffset.stream().mapToDouble(SteadyQualifyVO::getVoltageOffset).average().orElse(100), 2).doubleValue());
            }
            if (!CollectionUtils.isEmpty(voltageUnbalance)) {
                steadyQualifyVO.setVoltageUnbalance(NumberUtil.round(voltageUnbalance.stream().mapToDouble(SteadyQualifyVO::getVoltageUnbalance).average().orElse(100), 2).doubleValue());
            }
            if (!CollectionUtils.isEmpty(negativeCurrent)) {
                steadyQualifyVO.setNegativeCurrent(NumberUtil.round(negativeCurrent.stream().mapToDouble(SteadyQualifyVO::getNegativeCurrent).average().orElse(100), 2).doubleValue());
            }
            if (!CollectionUtils.isEmpty(harmonicVoltage)) {
                steadyQualifyVO.setHarmonicVoltage(NumberUtil.round(harmonicVoltage.stream().mapToDouble(SteadyQualifyVO::getHarmonicVoltage).average().orElse(100), 2).doubleValue());
            }
            if (!CollectionUtils.isEmpty(harmonicCurrent)) {
                steadyQualifyVO.setHarmonicCurrent(NumberUtil.round(harmonicCurrent.stream().mapToDouble(SteadyQualifyVO::getHarmonicCurrent).average().orElse(100), 2).doubleValue());
            }
            if (!CollectionUtils.isEmpty(InterHarmonic)) {
                steadyQualifyVO.setInterHarmonic(NumberUtil.round(InterHarmonic.stream().mapToDouble(SteadyQualifyVO::getInterHarmonic).average().orElse(100), 2).doubleValue());
            }
        });
    }

    /**
     * 组装树层级
     */
    private List<SteadyQualifyVO> getChildCategoryList(SteadyQualifyVO item, List<SteadyQualifyVO> child) {
        return child.stream().filter(allItem -> allItem.getPid().equals(item.getId())).collect(Collectors.toList());
    }
    private void setChildesList(List<SteadyQualifyVO> item, List<SteadyQualifyVO> childes) {
        Map<String, List<SteadyQualifyVO>> groupLine;
        groupLine = childes.stream().collect(Collectors.groupingBy(steadyQualifyVO -> {
            String[] pid = steadyQualifyVO.getPidS().split(",");
            return pid[LineBaseEnum.SUB_LEVEL.getCode()];
        }));
        item.stream().peek(steadyQualifyVO -> {
            if (!CollectionUtils.isEmpty(groupLine.get(steadyQualifyVO.getId()))) {
                steadyQualifyVO.setChildren(groupLine.get(steadyQualifyVO.getId()));
            }
        }).collect(Collectors.toList());
    }

    /**
     * 计算监测点日统计合格率
     */
    public SteadyQualifyVO getDataSingleMonitorSingeDay(List<SteadyQualifyDTO> qualifiesRate, String lineId, String time) {
        SteadyQualifyVO steadyQualifyVO = new SteadyQualifyVO();
        List<SteadyQualifyDTO> steadyQualifyDTOList = qualifiesRate.stream().filter(
                temp -> temp.getMYINDEX().equalsIgnoreCase(lineId) && temp.getTime().equalsIgnoreCase(time)
        ).collect(Collectors.toList());
        if (!CollectionUtil.isEmpty(steadyQualifyDTOList)) {
            SteadyQualifyDTO steadyQualifyDTO = steadyQualifyDTOList.get(0);
            //闪变合格率
            steadyQualifyVO.setFlicker(calculate(steadyQualifyDTO.getFlicker_AllTime(), steadyQualifyDTO.getFlicker_OverTime()));
            //频率偏差合格率
            steadyQualifyVO.setFreqOffset(calculate(steadyQualifyDTO.getAllTime(), steadyQualifyDTO.getFreq_Dev_OverTime()));
            //计算电压偏差
            steadyQualifyVO.setVoltageOffset(calculate(steadyQualifyDTO.getAllTime(), steadyQualifyDTO.getVoltage_Dev_OverTime()));
            //相电压不平衡度
            steadyQualifyVO.setVoltageUnbalance(calculate(steadyQualifyDTO.getAllTime(), steadyQualifyDTO.getUBalance_OverTime()));
            //负序电流
            steadyQualifyVO.setNegativeCurrent(calculate(steadyQualifyDTO.getAllTime(), steadyQualifyDTO.getI_Neg_OverTime()));
            //谐波电压
            steadyQualifyVO.setHarmonicVoltage(calculate(steadyQualifyDTO.getAllTime(), NumberUtil.max(steadyQualifyDTO.getUAberrance_OverTime(), steadyQualifyDTO.getUHarm_2_OverTime(), steadyQualifyDTO.getUHarm_3_OverTime(), steadyQualifyDTO.getUHarm_4_OverTime(), steadyQualifyDTO.getUHarm_5_OverTime(), steadyQualifyDTO.getUHarm_6_OverTime(), steadyQualifyDTO.getUHarm_7_OverTime(), steadyQualifyDTO.getUHarm_8_OverTime(), steadyQualifyDTO.getUHarm_9_OverTime(), steadyQualifyDTO.getUHarm_10_OverTime(), steadyQualifyDTO.getUHarm_11_OverTime(), steadyQualifyDTO.getUHarm_12_OverTime(), steadyQualifyDTO.getUHarm_13_OverTime(), steadyQualifyDTO.getUHarm_14_OverTime(), steadyQualifyDTO.getUHarm_15_OverTime(), steadyQualifyDTO.getUHarm_16_OverTime(), steadyQualifyDTO.getUHarm_17_OverTime(), steadyQualifyDTO.getUHarm_18_OverTime(), steadyQualifyDTO.getUHarm_19_OverTime(), steadyQualifyDTO.getUHarm_20_OverTime(), steadyQualifyDTO.getUHarm_21_OverTime(), steadyQualifyDTO.getUHarm_22_OverTime(), steadyQualifyDTO.getUHarm_23_OverTime(), steadyQualifyDTO.getUHarm_24_OverTime(), steadyQualifyDTO.getUHarm_25_OverTime())));
            //谐波电流
            steadyQualifyVO.setHarmonicCurrent(calculate(steadyQualifyDTO.getAllTime(), NumberUtil.max(steadyQualifyDTO.getIHarm_2_OverTime(), steadyQualifyDTO.getIHarm_3_OverTime(), steadyQualifyDTO.getIHarm_4_OverTime(), steadyQualifyDTO.getIHarm_5_OverTime(), steadyQualifyDTO.getIHarm_6_OverTime(), steadyQualifyDTO.getIHarm_7_OverTime(), steadyQualifyDTO.getIHarm_8_OverTime(), steadyQualifyDTO.getIHarm_9_OverTime(), steadyQualifyDTO.getIHarm_10_OverTime(), steadyQualifyDTO.getIHarm_11_OverTime(), steadyQualifyDTO.getIHarm_12_OverTime(), steadyQualifyDTO.getIHarm_13_OverTime(), steadyQualifyDTO.getIHarm_14_OverTime(), steadyQualifyDTO.getIHarm_15_OverTime(), steadyQualifyDTO.getIHarm_16_OverTime(), steadyQualifyDTO.getIHarm_17_OverTime(), steadyQualifyDTO.getIHarm_18_OverTime(), steadyQualifyDTO.getIHarm_19_OverTime(), steadyQualifyDTO.getIHarm_20_OverTime(), steadyQualifyDTO.getIHarm_21_OverTime(), steadyQualifyDTO.getIHarm_22_OverTime(), steadyQualifyDTO.getIHarm_23_OverTime(), steadyQualifyDTO.getIHarm_24_OverTime(), steadyQualifyDTO.getIHarm_25_OverTime())));
            //间谐波电压含有率
            steadyQualifyVO.setInterHarmonic(calculate(steadyQualifyDTO.getAllTime(), NumberUtil.max(steadyQualifyDTO.getInUHARM_1_OverTime(), steadyQualifyDTO.getInUHARM_2_OverTime(), steadyQualifyDTO.getInUHARM_3_OverTime(), steadyQualifyDTO.getInUHARM_4_OverTime(), steadyQualifyDTO.getInUHARM_5_OverTime(), steadyQualifyDTO.getInUHARM_6_OverTime(), steadyQualifyDTO.getInUHARM_7_OverTime(), steadyQualifyDTO.getInUHARM_8_OverTime(), steadyQualifyDTO.getInUHARM_9_OverTime(), steadyQualifyDTO.getInUHARM_10_OverTime(), steadyQualifyDTO.getInUHARM_11_OverTime(), steadyQualifyDTO.getInUHARM_12_OverTime(), steadyQualifyDTO.getInUHARM_13_OverTime(), steadyQualifyDTO.getInUHARM_14_OverTime(), steadyQualifyDTO.getInUHARM_15_OverTime(), steadyQualifyDTO.getInUHARM_16_OverTime())));
        }
        return steadyQualifyVO;
    }

    /**
     * 监测点时间范围统计合格率
     */
    public SteadyQualifyVO getDataSingleMonitorMoreDay(List<SteadyQualifyDTO> qualifiesRate, String lineId) {
        SteadyQualifyVO steadyQualifyVO = new SteadyQualifyVO();
        //从数据里筛选出指定监测点的数据
        List<SteadyQualifyDTO> steadyQualifyDTOList = qualifiesRate.stream().filter(
                temp -> temp.getMYINDEX().equalsIgnoreCase(lineId)).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(steadyQualifyDTOList)) {
            List<SteadyQualifyVO> steadyQualifyVOS = new ArrayList<>();
            //获取出时间和id，调用第一个方法
            for (SteadyQualifyDTO steadyQualifyDTO : steadyQualifyDTOList) {
                steadyQualifyVOS.add(getDataSingleMonitorSingeDay(qualifiesRate, lineId, steadyQualifyDTO.getTime()));
            }
            //求各项平均值
            steadyQualifyVO.setFlicker(NumberUtil.round(steadyQualifyVOS.stream().mapToDouble(SteadyQualifyVO::getFlicker).average().orElse(100), 2).doubleValue());
            steadyQualifyVO.setFreqOffset(NumberUtil.round(steadyQualifyVOS.stream().mapToDouble(SteadyQualifyVO::getFreqOffset).average().orElse(100), 2).doubleValue());
            steadyQualifyVO.setVoltageOffset(NumberUtil.round(steadyQualifyVOS.stream().mapToDouble(SteadyQualifyVO::getVoltageOffset).average().orElse(100), 2).doubleValue());
            steadyQualifyVO.setVoltageUnbalance(NumberUtil.round(steadyQualifyVOS.stream().mapToDouble(SteadyQualifyVO::getVoltageUnbalance).average().orElse(100), 2).doubleValue());
            steadyQualifyVO.setNegativeCurrent(NumberUtil.round(steadyQualifyVOS.stream().mapToDouble(SteadyQualifyVO::getNegativeCurrent).average().orElse(100), 2).doubleValue());
            steadyQualifyVO.setHarmonicVoltage(NumberUtil.round(steadyQualifyVOS.stream().mapToDouble(SteadyQualifyVO::getHarmonicVoltage).average().orElse(100), 2).doubleValue());
            steadyQualifyVO.setHarmonicCurrent(NumberUtil.round(steadyQualifyVOS.stream().mapToDouble(SteadyQualifyVO::getHarmonicCurrent).average().orElse(100), 2).doubleValue());
            steadyQualifyVO.setInterHarmonic(NumberUtil.round(steadyQualifyVOS.stream().mapToDouble(SteadyQualifyVO::getInterHarmonic).average().orElse(100), 2).doubleValue());
        }
        return steadyQualifyVO;
    }

    /**
     * 计算区域监测点时间范围统计合格率
     */
    public SteadyQualifyVO getDataMoreMonitorMoreDay(List<SteadyQualifyDTO> qualifiesRate) {
        SteadyQualifyVO steadyQualifyVO = new SteadyQualifyVO();
        List<SteadyQualifyVO> steadyResults = new ArrayList<>();
        //以时间分组的各项数据
        Map<String, List<SteadyQualifyDTO>> groupByTimeData = qualifiesRate.stream().collect(Collectors.groupingBy(SteadyQualifyDTO::getTime));
        if (CollectionUtil.isNotEmpty(groupByTimeData)) {
            Set<String> times = groupByTimeData.keySet();
            for (String time : times) {
                List<String> ids = groupByTimeData.get(time).stream().map(SteadyQualifyDTO::getMYINDEX).collect(Collectors.toList());
                steadyResults.add(getDataMoreMonitorSingleDay(qualifiesRate, ids, time));
            }
        }
        if (CollectionUtil.isNotEmpty(steadyResults)) {
            //求各项平均值
            steadyQualifyVO.setFlicker(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyQualifyVO::getFlicker).average().orElse(100), 2).doubleValue());
            steadyQualifyVO.setFreqOffset(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyQualifyVO::getFreqOffset).average().orElse(100), 2).doubleValue());
            steadyQualifyVO.setVoltageOffset(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyQualifyVO::getVoltageOffset).average().orElse(100), 2).doubleValue());
            steadyQualifyVO.setVoltageUnbalance(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyQualifyVO::getVoltageUnbalance).average().orElse(100), 2).doubleValue());
            steadyQualifyVO.setNegativeCurrent(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyQualifyVO::getNegativeCurrent).average().orElse(100), 2).doubleValue());
            steadyQualifyVO.setHarmonicVoltage(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyQualifyVO::getHarmonicVoltage).average().orElse(100), 2).doubleValue());
            steadyQualifyVO.setHarmonicCurrent(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyQualifyVO::getHarmonicCurrent).average().orElse(100), 2).doubleValue());
            steadyQualifyVO.setInterHarmonic(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyQualifyVO::getInterHarmonic).average().orElse(100), 2).doubleValue());
        }
        return steadyQualifyVO;
    }

    /**
     * 计算区域监测点日统计合格率
     */
    public SteadyQualifyVO getDataMoreMonitorSingleDay(List<SteadyQualifyDTO> qualifiesRate, List<String> lineIds, String time) {
        SteadyQualifyVO steadyQualifyVO = new SteadyQualifyVO();
        List<SteadyQualifyVO> steadyResults = new ArrayList<>();
        for (String lineId : lineIds) {
            steadyResults.add(getDataSingleMonitorSingeDay(qualifiesRate, lineId, time));
        }
        if (CollectionUtil.isNotEmpty(steadyResults)) {
            //求各项平均值
            steadyQualifyVO.setFlicker(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyQualifyVO::getFlicker).average().orElse(100), 2).doubleValue());
            steadyQualifyVO.setFreqOffset(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyQualifyVO::getFreqOffset).average().orElse(100), 2).doubleValue());
            steadyQualifyVO.setVoltageOffset(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyQualifyVO::getVoltageOffset).average().orElse(100), 2).doubleValue());
            steadyQualifyVO.setVoltageUnbalance(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyQualifyVO::getVoltageUnbalance).average().orElse(100), 2).doubleValue());
            steadyQualifyVO.setNegativeCurrent(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyQualifyVO::getNegativeCurrent).average().orElse(100), 2).doubleValue());
            steadyQualifyVO.setHarmonicVoltage(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyQualifyVO::getHarmonicVoltage).average().orElse(100), 2).doubleValue());
            steadyQualifyVO.setHarmonicCurrent(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyQualifyVO::getHarmonicCurrent).average().orElse(100), 2).doubleValue());
            steadyQualifyVO.setInterHarmonic(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyQualifyVO::getInterHarmonic).average().orElse(100), 2).doubleValue());
        }
        return steadyQualifyVO;
    }


    /**
     * influxDB查询稳态监测点相关信息
     * @param lineIndexes
     * @param startTime
     * @param endTime
     */
    private List<SteadyQualifyDTO> getQualifiesRate(List<String> lineIndexes, String startTime, String endTime) {
        List<SteadyQualifyDTO> qualifyDTOList = new ArrayList<>();
        //组装sql语句
        // StringBuilder string = new StringBuilder();
        // string.append(Param.QualityFlag + "='1' and (" + Param.PHASIC_TYPE + "='" + Param.PHASIC_TYPEA + "' or " + Param.PHASIC_TYPE + "='" + Param.PHASIC_TYPEB + "' or " + Param.PHASIC_TYPE + "='" + Param.PHASIC_TYPEC + "') and "+ Param.VALUETYPE + "='AVG' and ");
        StringBuilder timeId = new StringBuilder();
        timeId.append(Param.TIME + " >= '" + startTime + Param.START_TIME + "' and " + Param.TIME + " <= '" + endTime + Param.END_TIME + "' and ");
        for (int i = 0; i < lineIndexes.size(); i++) {
            if (lineIndexes.size() - i != 1) {
                timeId.append(Param.MYINDEX + "='").append(lineIndexes.get(i)).append("' or ");
            } else {
                timeId.append(Param.MYINDEX + "='").append(lineIndexes.get(i)).append("' ");
            }
        }
        String a = "SELECT * FROM LIMIT_RATE WHERE time >= '2021-01-01 00:00:00' and time <= '2022-04-30 23:59:59' and MYINDEX='bffe97ff541333448358b7b00ed97cdb' or MYINDEX='479bf4e228b5579629454ee99de0b950' or MYINDEX='1e3b8531483b2a8cbee6747f1f641cf9'";
        //sql语句
        String sql = "SELECT * FROM LIMIT_RATE WHERE " + timeId;
        //结果集
        QueryResult result = influxDbUtils.query(sql);
        //处理结果集
        if (!CollectionUtils.isEmpty(result.getResults().get(0).getSeries())) {
            List<List<Object>> values = result.getResults().get(0).getSeries().get(0).getValues();
            values.forEach(list ->{
                SteadyQualifyDTO steadyQualifyDTO = new SteadyQualifyDTO();
                steadyQualifyDTO.setTime(list.get(0).toString());
                steadyQualifyDTO.setMYINDEX(list.get(46).toString());
                steadyQualifyDTO.setPhasic_Type(list.get(47).toString());
                steadyQualifyDTO.setAllTime(Double.parseDouble(list.get(1).toString()));
                steadyQualifyDTO.setFlicker_AllTime(Double.parseDouble(list.get(2).toString()));
                steadyQualifyDTO.setFlicker_OverTime(Double.parseDouble(list.get(3).toString()));
                steadyQualifyDTO.setFreq_Dev_OverTime(Double.parseDouble(list.get(4).toString()));
                steadyQualifyDTO.setIHarm_10_OverTime(Double.parseDouble(list.get(5).toString()));
                steadyQualifyDTO.setIHarm_11_OverTime(Double.parseDouble(list.get(6).toString()));
                steadyQualifyDTO.setIHarm_12_OverTime(Double.parseDouble(list.get(7).toString()));
                steadyQualifyDTO.setIHarm_13_OverTime(Double.parseDouble(list.get(8).toString()));
                steadyQualifyDTO.setIHarm_14_OverTime(Double.parseDouble(list.get(9).toString()));
                steadyQualifyDTO.setIHarm_15_OverTime(Double.parseDouble(list.get(10).toString()));
                steadyQualifyDTO.setIHarm_16_OverTime(Double.parseDouble(list.get(11).toString()));
                steadyQualifyDTO.setIHarm_17_OverTime(Double.parseDouble(list.get(12).toString()));
                steadyQualifyDTO.setIHarm_18_OverTime(Double.parseDouble(list.get(13).toString()));
                steadyQualifyDTO.setIHarm_19_OverTime(Double.parseDouble(list.get(14).toString()));
                steadyQualifyDTO.setIHarm_20_OverTime(Double.parseDouble(list.get(15).toString()));
                steadyQualifyDTO.setIHarm_21_OverTime(Double.parseDouble(list.get(16).toString()));
                steadyQualifyDTO.setIHarm_22_OverTime(Double.parseDouble(list.get(17).toString()));
                steadyQualifyDTO.setIHarm_23_OverTime(Double.parseDouble(list.get(18).toString()));
                steadyQualifyDTO.setIHarm_24_OverTime(Double.parseDouble(list.get(19).toString()));
                steadyQualifyDTO.setIHarm_25_OverTime(Double.parseDouble(list.get(20).toString()));
                steadyQualifyDTO.setIHarm_2_OverTime(Double.parseDouble(list.get(21).toString()));
                steadyQualifyDTO.setIHarm_3_OverTime(Double.parseDouble(list.get(22).toString()));
                steadyQualifyDTO.setIHarm_4_OverTime(Double.parseDouble(list.get(23).toString()));
                steadyQualifyDTO.setIHarm_5_OverTime(Double.parseDouble(list.get(24).toString()));
                steadyQualifyDTO.setIHarm_6_OverTime(Double.parseDouble(list.get(25).toString()));
                steadyQualifyDTO.setIHarm_7_OverTime(Double.parseDouble(list.get(26).toString()));
                steadyQualifyDTO.setIHarm_8_OverTime(Double.parseDouble(list.get(27).toString()));
                steadyQualifyDTO.setIHarm_9_OverTime(Double.parseDouble(list.get(28).toString()));
                steadyQualifyDTO.setI_Neg_OverTime(Double.parseDouble(list.get(29).toString()));
                steadyQualifyDTO.setInUHARM_10_OverTime(Double.parseDouble(list.get(30).toString()));
                steadyQualifyDTO.setInUHARM_11_OverTime(Double.parseDouble(list.get(31).toString()));
                steadyQualifyDTO.setInUHARM_12_OverTime(Double.parseDouble(list.get(32).toString()));
                steadyQualifyDTO.setInUHARM_13_OverTime(Double.parseDouble(list.get(33).toString()));
                steadyQualifyDTO.setInUHARM_14_OverTime(Double.parseDouble(list.get(34).toString()));
                steadyQualifyDTO.setInUHARM_15_OverTime(Double.parseDouble(list.get(35).toString()));
                steadyQualifyDTO.setInUHARM_16_OverTime(Double.parseDouble(list.get(36).toString()));
                steadyQualifyDTO.setInUHARM_1_OverTime(Double.parseDouble(list.get(37).toString()));
                steadyQualifyDTO.setInUHARM_2_OverTime(Double.parseDouble(list.get(38).toString()));
                steadyQualifyDTO.setInUHARM_3_OverTime(Double.parseDouble(list.get(39).toString()));
                steadyQualifyDTO.setInUHARM_4_OverTime(Double.parseDouble(list.get(40).toString()));
                steadyQualifyDTO.setInUHARM_5_OverTime(Double.parseDouble(list.get(41).toString()));
                steadyQualifyDTO.setInUHARM_6_OverTime(Double.parseDouble(list.get(42).toString()));
                steadyQualifyDTO.setInUHARM_7_OverTime(Double.parseDouble(list.get(43).toString()));
                steadyQualifyDTO.setInUHARM_8_OverTime(Double.parseDouble(list.get(44).toString()));
                steadyQualifyDTO.setInUHARM_9_OverTime(Double.parseDouble(list.get(45).toString()));
                steadyQualifyDTO.setUAberrance_OverTime(Double.parseDouble(list.get(48).toString()));
                steadyQualifyDTO.setUBalance_OverTime(Double.parseDouble(list.get(49).toString()));
                steadyQualifyDTO.setUHarm_10_OverTime(Double.parseDouble(list.get(50).toString()));
                steadyQualifyDTO.setUHarm_11_OverTime(Double.parseDouble(list.get(51).toString()));
                steadyQualifyDTO.setUHarm_12_OverTime(Double.parseDouble(list.get(52).toString()));
                steadyQualifyDTO.setUHarm_13_OverTime(Double.parseDouble(list.get(53).toString()));
                steadyQualifyDTO.setUHarm_14_OverTime(Double.parseDouble(list.get(54).toString()));
                steadyQualifyDTO.setUHarm_15_OverTime(Double.parseDouble(list.get(55).toString()));
                steadyQualifyDTO.setUHarm_16_OverTime(Double.parseDouble(list.get(56).toString()));
                steadyQualifyDTO.setUHarm_17_OverTime(Double.parseDouble(list.get(57).toString()));
                steadyQualifyDTO.setUHarm_18_OverTime(Double.parseDouble(list.get(58).toString()));
                steadyQualifyDTO.setUHarm_19_OverTime(Double.parseDouble(list.get(59).toString()));
                steadyQualifyDTO.setUHarm_20_OverTime(Double.parseDouble(list.get(60).toString()));
                steadyQualifyDTO.setUHarm_21_OverTime(Double.parseDouble(list.get(61).toString()));
                steadyQualifyDTO.setUHarm_22_OverTime(Double.parseDouble(list.get(62).toString()));
                steadyQualifyDTO.setUHarm_23_OverTime(Double.parseDouble(list.get(63).toString()));
                steadyQualifyDTO.setUHarm_24_OverTime(Double.parseDouble(list.get(64).toString()));
                steadyQualifyDTO.setUHarm_25_OverTime(Double.parseDouble(list.get(65).toString()));
                steadyQualifyDTO.setUHarm_2_OverTime(Double.parseDouble(list.get(66).toString()));
                steadyQualifyDTO.setUHarm_3_OverTime(Double.parseDouble(list.get(67).toString()));
                steadyQualifyDTO.setUHarm_4_OverTime(Double.parseDouble(list.get(68).toString()));
                steadyQualifyDTO.setUHarm_5_OverTime(Double.parseDouble(list.get(69).toString()));
                steadyQualifyDTO.setUHarm_6_OverTime(Double.parseDouble(list.get(70).toString()));
                steadyQualifyDTO.setUHarm_7_OverTime(Double.parseDouble(list.get(71).toString()));
                steadyQualifyDTO.setUHarm_8_OverTime(Double.parseDouble(list.get(72).toString()));
                steadyQualifyDTO.setUHarm_9_OverTime(Double.parseDouble(list.get(73).toString()));
                steadyQualifyDTO.setVoltage_Dev_OverTime(Double.parseDouble(list.get(74).toString()));

                // steadyQualifyDTO.setTime(time);
                // if (!CollectionUtils.isEmpty(valueList)){
                //     for (List<Object> value : valueList) {
                //         String time = value.get(0).toString();
                //         double v = Double.parseDouble(value.get(1).toString());
                //         value.get(2);
                //         value.get(3);
                //         //谐波畸变率 保留两位小数
                //         Double distortion = value.get(1) == null ? null : BigDecimal.valueOf(Double.parseDouble(value.get(1).toString())).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                //
                //
                //     }
                // }
                qualifyDTOList.add(steadyQualifyDTO);
            });
        }

        return qualifyDTOList;
    }

    private static double calculate(Double allTime, Double overTime) {
        if (allTime >= overTime){
            return NumberUtil.round((allTime - overTime) / allTime * 100, 2).doubleValue();
        }
        return 0.0;
    }

}
