package com.njcn.harmonic.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
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.SteadyExceedRateMapper;
import com.njcn.harmonic.pojo.po.LimitRatePO;
import com.njcn.harmonic.pojo.vo.SteadyExceedRateCensusVO;
import com.njcn.harmonic.pojo.vo.SteadyExceedRateVO;
import com.njcn.harmonic.service.SteadyExceedRateService;
import com.njcn.influxdb.utils.InfluxDbUtils;
import lombok.AllArgsConstructor;
import org.influxdb.dto.QueryResult;
import org.influxdb.impl.InfluxDBResultMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @version 1.0.0
 * @author: chenchao
 * @date: 2022/04/12 14:03
 */
@Service
@AllArgsConstructor
public class SteadyExceedRateServiceImpl implements SteadyExceedRateService {

    private final GeneralDeviceInfoClient generalDeviceInfoClient;

    private final SteadyExceedRateMapper steadyExceedRateMapper;

    private final InfluxDbUtils influxDbUtils;

    @Override
    public List<SteadyExceedRateVO> getSteadyExceedRateData(DeviceInfoParam.BusinessParam steadyExceedParam) {
        List<SteadyExceedRateVO> steadyExceedRateVOS = new ArrayList<>();
        //按照条件获取实际运行终端综合信息
        List<GeneralDeviceDTO> deviceDataList = generalDeviceInfoClient.getPracticalRunDeviceInfo(steadyExceedParam).getData();
        for (GeneralDeviceDTO generalDeviceDTO: deviceDataList){
            List<String> lineIndexes = generalDeviceDTO.getLineIndexes();
            if (CollectionUtils.isEmpty(lineIndexes)) {
                continue;
            }
            SteadyExceedRateVO steadyExceedRateVO = new SteadyExceedRateVO();
            steadyExceedRateVO.setId(generalDeviceDTO.getIndex());
            steadyExceedRateVO.setName(generalDeviceDTO.getName());
            List<LimitRatePO> qualifiesRate = getQualifiesRate(lineIndexes, steadyExceedParam.getSearchBeginTime(), steadyExceedParam.getSearchEndTime());
            SteadyExceedRateVO dataMoreMonitorMoreDay = getDataMoreMonitorMoreDay(qualifiesRate);
            steadyExceedRateVO.setSteadyExceedRate(dataMoreMonitorMoreDay.getSteadyExceedRate());

            //组装父级数据树
            List<SteadyExceedRateVO> treeList = getTreeData(lineIndexes, steadyExceedParam);
            steadyExceedRateVO.setChildren(treeList);

            steadyExceedRateVOS.add(steadyExceedRateVO);
        }
        return steadyExceedRateVOS;
    }


    @Override
    public SteadyExceedRateCensusVO getSteadyExceedRateCensus(DeviceInfoParam.ConditionBusinessParam steadyExceedCensusParam) {
        SteadyExceedRateCensusVO steadyExceedRateCensusVO = new SteadyExceedRateCensusVO();
        List<String> type = new ArrayList<>(), time = new ArrayList<>();
        List<List<Double>> steadyExceedList = new ArrayList<>();
        List<List<String>> tempIndex = new ArrayList<>();

        //按照条件获取实际运行终端综合信息
        List<GeneralDeviceDTO> deviceDataList = generalDeviceInfoClient.getPracticalRunDeviceInfo(steadyExceedCensusParam).getData();
        if (!CollectionUtils.isEmpty(deviceDataList)) {
            for (GeneralDeviceDTO generalDeviceDTO: deviceDataList) {
                type.add(generalDeviceDTO.getName());

                List<String> lineIndexes = generalDeviceDTO.getLineIndexes();
                tempIndex.add(lineIndexes);
            }

            int timed = 0;
            switch (steadyExceedCensusParam.getTimeFlag()) {
                case 0:
                    time = getSteadyTimesByMouth(steadyExceedCensusParam.getSearchBeginTime(), steadyExceedCensusParam.getSearchEndTime());
                    timed = getSteadyByMouth(steadyExceedCensusParam.getSearchBeginTime(), steadyExceedCensusParam.getSearchEndTime());
                    for (int i =0; i<timed; i++) {
                        List<Double> steadyExceedRate = new ArrayList<>();
                        for (List<String> lineIndexes: tempIndex) {
                            if (!CollectionUtils.isEmpty(lineIndexes)) {
                                String beginTime = time.get(i)+"-01";
                                DateTime date = DateUtil.endOfMonth(new DateTime(beginTime));
                                String endTime = date.toString(DatePattern.NORM_DATE_PATTERN);

                                List<LimitRatePO> qualifiesRate = getQualifiesRate(lineIndexes, beginTime, endTime);
                                SteadyExceedRateVO dataMoreMonitorMoreDay = getDataMoreMonitorMoreDay(qualifiesRate);
                                steadyExceedRate.add(dataMoreMonitorMoreDay.getSteadyExceedRate());
                            } else {
                                steadyExceedRate.add(3.14159);
                            }
                        }
                        steadyExceedList.add(steadyExceedRate);
                    }
                    break;
                case 1:
                    time = getSteadyTimesByDay(steadyExceedCensusParam.getSearchBeginTime(), steadyExceedCensusParam.getSearchEndTime());
                    timed = getSteadyByDay(steadyExceedCensusParam.getSearchBeginTime(), steadyExceedCensusParam.getSearchEndTime());
                    for (int i =0; i<timed; i++) {
                        List<Double> steadyExceedRate = new ArrayList<>();
                        for (List<String> lineIndexes: tempIndex) {
                            if (!CollectionUtils.isEmpty(lineIndexes)) {
                                List<LimitRatePO> qualifiesRate = getQualifiesRate(lineIndexes, steadyExceedCensusParam.getSearchBeginTime(), steadyExceedCensusParam.getSearchEndTime());
                                SteadyExceedRateVO dataMoreMonitorSingleDay = getDataMoreMonitorSingleDay(qualifiesRate, lineIndexes, time.get(i));
                                steadyExceedRate.add(dataMoreMonitorSingleDay.getSteadyExceedRate());
                            } else {
                                steadyExceedRate.add(3.14159);
                            }
                        }
                        steadyExceedList.add(steadyExceedRate);
                    }
                    break;
                case 2:
                    time = getSteadyTimesByDay(steadyExceedCensusParam.getSearchBeginTime(), steadyExceedCensusParam.getSearchEndTime());
                    List<Double> steadyExceedRate = new ArrayList<>();
                    for (List<String> lineIndexes: tempIndex) {
                        if (!CollectionUtils.isEmpty(lineIndexes)) {
                            List<LimitRatePO> qualifiesRate = getQualifiesRate(lineIndexes, steadyExceedCensusParam.getSearchBeginTime(), steadyExceedCensusParam.getSearchEndTime());
                            SteadyExceedRateVO dataMoreMonitorSingleDay = getDataMoreMonitorSingleDay(qualifiesRate, lineIndexes, steadyExceedCensusParam.getSearchBeginTime());
                            steadyExceedRate.add(dataMoreMonitorSingleDay.getSteadyExceedRate());
                        } else {
                            steadyExceedRate.add(3.14159);
                        }
                    }
                    steadyExceedList.add(steadyExceedRate);
                    break;
            }
        }
        steadyExceedRateCensusVO.setType(type);
        steadyExceedRateCensusVO.setTime(time);
        steadyExceedRateCensusVO.setSteadyExceedRate(steadyExceedList);
        return steadyExceedRateCensusVO;
    }



    /** 根据时间范围返回相应单位的时间整数
     */
    private int getSteadyByMouth(String searchBeginTime, String searchEndTime) {
        int beginMouthTime = Integer.parseInt(searchBeginTime.substring(5, 7));
        int endMouthTime = Integer.parseInt(searchEndTime.substring(5, 7));
        int mouth = endMouthTime - beginMouthTime + 1;
        return mouth;
    }
    private int getSteadyByDay(String searchBeginTime, String searchEndTime) {
        int beginTime = Integer.parseInt(searchBeginTime.substring(8, 10));
        int endTime = Integer.parseInt(searchEndTime.substring(8, 10));
        int day = endTime - beginTime + 1;
        return day;
    }
    /** 根据时间范围返回相应单位的时间格式字符串
     */
    private List<String> getSteadyTimesByMouth(String searchBeginTime, String searchEndTime) {
        List<String> string = new ArrayList<>();
        int beginMouthTime = Integer.parseInt(searchBeginTime.substring(5, 7));
        int endMouthTime = Integer.parseInt(searchEndTime.substring(5, 7));
        for (int i =0; i<endMouthTime - beginMouthTime + 1; i++) {
            int a =0;
            if (beginMouthTime<9) {
                a = beginMouthTime + i;
                string.add(searchBeginTime.substring(0, 5)+"0"+a);
            } else {
                string.add(searchBeginTime.substring(0, 5)+a);
            }
        }
        return string;
    }
    private List<String> getSteadyTimesByDay(String searchBeginTime, String searchEndTime) {
        List<String> string = new ArrayList<>();
        int beginTime = Integer.parseInt(searchBeginTime.substring(8, 10));
        int endTime = Integer.parseInt(searchEndTime.substring(8, 10));
        int a =0;
        for (int i =0; i<endTime - beginTime + 1; i++) {
            if (a<9) {
                a = beginTime + i;
                string.add(searchBeginTime.substring(0, 8)+"0"+a);
            } else {
                a = beginTime + i;
                string.add(searchBeginTime.substring(0, 8)+a);
            }
        }
        return string;
    }


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

        List<LimitRatePO> qualifiesRate = getQualifiesRate(lineIndexes, steadyExceedParam.getSearchBeginTime(), steadyExceedParam.getSearchEndTime());
        monitorList.forEach(steadyExceedRateVO -> {
            SteadyExceedRateVO dataSingleMonitorMoreDay = getDataSingleMonitorMoreDay(qualifiesRate, steadyExceedRateVO.getId());
            steadyExceedRateVO.setSteadyExceedRate(dataSingleMonitorMoreDay.getSteadyExceedRate());
        });
        setChildesList(substationList,monitorList);
        setFatherDistortion(substationList);
        setSubstationData(substationList);
        powerCompanyList.stream().peek(item -> item.setChildren(getChildCategoryList(item, substationList))).collect(Collectors.toList());
        setFatherDistortion(powerCompanyList);

        return powerCompanyList;
    }

    //查变电站信息
    private void setSubstationData(List<SteadyExceedRateVO> substationList) {
        List<String> ids = new ArrayList<>();
        substationList.forEach(steadyExceedRateVO -> {
            ids.add(steadyExceedRateVO.getId());
        });
        List<SteadyExceedRateVO> substation = steadyExceedRateMapper.getSubstationList(ids);
        substationList.stream().map(list1 -> substation.stream().filter(list2 -> Objects.equals(list1.getId(), list2.getId())).findAny().map(m -> {
            list1.setVoltageLevel(m.getVoltageLevel());
            return list1;
        })).collect(Collectors.toList());
    }
    /**
     * 计算父级合格率
     */
    private void setFatherDistortion(List<SteadyExceedRateVO> list) {
        list.forEach(steadyExceedRateVO -> {
            List<SteadyExceedRateVO> children = steadyExceedRateVO.getChildren();
            List<SteadyExceedRateVO> flicker = children.stream().filter(child -> !Objects.equals(child.getSteadyExceedRate(), 3.14159)).collect(Collectors.toList());

            if (!CollectionUtils.isEmpty(flicker)) {
                steadyExceedRateVO.setSteadyExceedRate(NumberUtil.round(flicker.stream().mapToDouble(SteadyExceedRateVO::getSteadyExceedRate).average().orElse(1.2345), 2).doubleValue());
            }
        });
    }
    /**
     * 组装树层级
     */
    private List<SteadyExceedRateVO> getChildCategoryList(SteadyExceedRateVO item, List<SteadyExceedRateVO> child) {
        return child.stream().filter(allItem -> allItem.getPid().equals(item.getId())).collect(Collectors.toList());
    }
    private void setChildesList(List<SteadyExceedRateVO> item, List<SteadyExceedRateVO> childes) {
        Map<String, List<SteadyExceedRateVO>> groupLine;
        groupLine = childes.stream().collect(Collectors.groupingBy(steadyExceedRateVO -> {
            String[] pid = steadyExceedRateVO.getPidS().split(",");
            return pid[LineBaseEnum.SUB_LEVEL.getCode()];
        }));
        item.stream().peek(steadyExceedRateVO -> {
            if (!CollectionUtils.isEmpty(groupLine.get(steadyExceedRateVO.getId()))) {
                steadyExceedRateVO.setChildren(groupLine.get(steadyExceedRateVO.getId()));
            }
        }).collect(Collectors.toList());
    }

    /**
     * 计算一个监测点日统计占比率
     */
    public SteadyExceedRateVO getDataSingleMonitorSingeDay(List<LimitRatePO> qualifiesRate, String lineId, String time) {
        SteadyExceedRateVO steadyExceedRateVO = new SteadyExceedRateVO();
        LimitRatePO limitRatePO;
        List<LimitRatePO> limitRatePOList = qualifiesRate.stream().filter(
                temp -> temp.getLineId().equalsIgnoreCase(lineId) && temp.getDayStr().equalsIgnoreCase(time)
        ).collect(Collectors.toList());
        if (!CollectionUtil.isEmpty(limitRatePOList)) {
            limitRatePO = limitRatePOList.get(0);
            if ((limitRatePO.getFlickerOverTime()+ limitRatePO.getFreqDevOverTime()+ limitRatePO.getINegOverTime()+ limitRatePO.getUAberranceOverTime()+ limitRatePO.getUBalanceOverTime()+ limitRatePO.getVoltageDevOverTime()+ limitRatePO.getIHarm2OverTime()+ limitRatePO.getIHarm3OverTime()+ limitRatePO.getIHarm4OverTime()+ limitRatePO.getIHarm5OverTime()+ limitRatePO.getIHarm6OverTime()+ limitRatePO.getIHarm7OverTime()+ limitRatePO.getIHarm8OverTime()+ limitRatePO.getIHarm9OverTime()+ limitRatePO.getIHarm10OverTime()+ limitRatePO.getIHarm11OverTime()+ limitRatePO.getIHarm12OverTime()+ limitRatePO.getIHarm13OverTime()+ limitRatePO.getIHarm14OverTime()+ limitRatePO.getIHarm15OverTime()+ limitRatePO.getIHarm16OverTime()+ limitRatePO.getIHarm17OverTime()+ limitRatePO.getIHarm18OverTime()+ limitRatePO.getIHarm19OverTime()+ limitRatePO.getIHarm20OverTime()+ limitRatePO.getIHarm21OverTime()+ limitRatePO.getIHarm22OverTime()+ limitRatePO.getIHarm23OverTime()+ limitRatePO.getIHarm24OverTime()+ limitRatePO.getIHarm25OverTime()+ limitRatePO.getUHarm2OverTime()+ limitRatePO.getUHarm3OverTime()+ limitRatePO.getUHarm4OverTime()+ limitRatePO.getUHarm5OverTime()+ limitRatePO.getUHarm6OverTime()+ limitRatePO.getUHarm7OverTime()+ limitRatePO.getUHarm8OverTime()+ limitRatePO.getUHarm9OverTime()+ limitRatePO.getUHarm10OverTime()+ limitRatePO.getUHarm11OverTime()+ limitRatePO.getUHarm12OverTime()+ limitRatePO.getUHarm13OverTime()+ limitRatePO.getUHarm14OverTime()+ limitRatePO.getUHarm15OverTime()+ limitRatePO.getUHarm16OverTime()+ limitRatePO.getUHarm17OverTime()+ limitRatePO.getUHarm18OverTime()+ limitRatePO.getUHarm19OverTime()+ limitRatePO.getUHarm20OverTime()+ limitRatePO.getUHarm21OverTime()+ limitRatePO.getUHarm22OverTime()+ limitRatePO.getUHarm23OverTime()+ limitRatePO.getUHarm24OverTime()+ limitRatePO.getUHarm25OverTime()+ limitRatePO.getInUHARM1OverTime()+ limitRatePO.getInUHARM2OverTime()+ limitRatePO.getInUHARM3OverTime()+ limitRatePO.getInUHARM4OverTime()+ limitRatePO.getInUHARM5OverTime()+ limitRatePO.getInUHARM6OverTime()+ limitRatePO.getInUHARM7OverTime()+ limitRatePO.getInUHARM8OverTime()+ limitRatePO.getInUHARM9OverTime()+ limitRatePO.getInUHARM10OverTime()+ limitRatePO.getInUHARM11OverTime()+ limitRatePO.getInUHARM12OverTime()+ limitRatePO.getInUHARM13OverTime()+ limitRatePO.getInUHARM14OverTime()+ limitRatePO.getInUHARM15OverTime()+ limitRatePO.getInUHARM16OverTime()) > 0){
                steadyExceedRateVO.setSteadyExceedRate(100.00);
            } else { steadyExceedRateVO.setSteadyExceedRate(0.00); }

        }
        return steadyExceedRateVO;
    }
    /**
     * 计算一个监测点月统计占比率
     */
    public SteadyExceedRateVO getDataSingleMonitorMouth(List<LimitRatePO> qualifiesRate, String lineId, String time) {
        SteadyExceedRateVO steadyExceedRateVO = new SteadyExceedRateVO();
        LimitRatePO limitRatePO;
        List<LimitRatePO> limitRatePOList = qualifiesRate.stream().filter(
                temp -> temp.getLineId().equalsIgnoreCase(lineId) && temp.getMouthStr().equalsIgnoreCase(time)
        ).collect(Collectors.toList());
        if (!CollectionUtil.isEmpty(limitRatePOList)) {
            limitRatePO = limitRatePOList.get(0);
            if ((limitRatePO.getFlickerOverTime()+ limitRatePO.getFreqDevOverTime()+ limitRatePO.getINegOverTime()+ limitRatePO.getUAberranceOverTime()+ limitRatePO.getUBalanceOverTime()+ limitRatePO.getVoltageDevOverTime()+ limitRatePO.getIHarm2OverTime()+ limitRatePO.getIHarm3OverTime()+ limitRatePO.getIHarm4OverTime()+ limitRatePO.getIHarm5OverTime()+ limitRatePO.getIHarm6OverTime()+ limitRatePO.getIHarm7OverTime()+ limitRatePO.getIHarm8OverTime()+ limitRatePO.getIHarm9OverTime()+ limitRatePO.getIHarm10OverTime()+ limitRatePO.getIHarm11OverTime()+ limitRatePO.getIHarm12OverTime()+ limitRatePO.getIHarm13OverTime()+ limitRatePO.getIHarm14OverTime()+ limitRatePO.getIHarm15OverTime()+ limitRatePO.getIHarm16OverTime()+ limitRatePO.getIHarm17OverTime()+ limitRatePO.getIHarm18OverTime()+ limitRatePO.getIHarm19OverTime()+ limitRatePO.getIHarm20OverTime()+ limitRatePO.getIHarm21OverTime()+ limitRatePO.getIHarm22OverTime()+ limitRatePO.getIHarm23OverTime()+ limitRatePO.getIHarm24OverTime()+ limitRatePO.getIHarm25OverTime()+ limitRatePO.getUHarm2OverTime()+ limitRatePO.getUHarm3OverTime()+ limitRatePO.getUHarm4OverTime()+ limitRatePO.getUHarm5OverTime()+ limitRatePO.getUHarm6OverTime()+ limitRatePO.getUHarm7OverTime()+ limitRatePO.getUHarm8OverTime()+ limitRatePO.getUHarm9OverTime()+ limitRatePO.getUHarm10OverTime()+ limitRatePO.getUHarm11OverTime()+ limitRatePO.getUHarm12OverTime()+ limitRatePO.getUHarm13OverTime()+ limitRatePO.getUHarm14OverTime()+ limitRatePO.getUHarm15OverTime()+ limitRatePO.getUHarm16OverTime()+ limitRatePO.getUHarm17OverTime()+ limitRatePO.getUHarm18OverTime()+ limitRatePO.getUHarm19OverTime()+ limitRatePO.getUHarm20OverTime()+ limitRatePO.getUHarm21OverTime()+ limitRatePO.getUHarm22OverTime()+ limitRatePO.getUHarm23OverTime()+ limitRatePO.getUHarm24OverTime()+ limitRatePO.getUHarm25OverTime()+ limitRatePO.getInUHARM1OverTime()+ limitRatePO.getInUHARM2OverTime()+ limitRatePO.getInUHARM3OverTime()+ limitRatePO.getInUHARM4OverTime()+ limitRatePO.getInUHARM5OverTime()+ limitRatePO.getInUHARM6OverTime()+ limitRatePO.getInUHARM7OverTime()+ limitRatePO.getInUHARM8OverTime()+ limitRatePO.getInUHARM9OverTime()+ limitRatePO.getInUHARM10OverTime()+ limitRatePO.getInUHARM11OverTime()+ limitRatePO.getInUHARM12OverTime()+ limitRatePO.getInUHARM13OverTime()+ limitRatePO.getInUHARM14OverTime()+ limitRatePO.getInUHARM15OverTime()+ limitRatePO.getInUHARM16OverTime()) > 0){
                steadyExceedRateVO.setSteadyExceedRate(100.00);
            } else { steadyExceedRateVO.setSteadyExceedRate(0.00); }

        }
        return steadyExceedRateVO;
    }

    /**
     * 监测点时间范围统计占比率
     */
    public SteadyExceedRateVO getDataSingleMonitorMoreDay(List<LimitRatePO> qualifiesRate, String lineId) {
        SteadyExceedRateVO steadyExceedRateVO = new SteadyExceedRateVO();
        List<LimitRatePO> limitRatePOList;
        List<SteadyExceedRateVO> steadyExceedRateVOS = new ArrayList<>();
        //从数据里筛选出指定监测点的数据
        limitRatePOList = qualifiesRate.stream().filter(
                temp -> temp.getLineId().equalsIgnoreCase(lineId)).collect(Collectors.toList());
        System.out.println("该监测点当天的信息为>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+limitRatePOList);
        if (CollectionUtil.isNotEmpty(limitRatePOList)) {
            Map<String, List<SteadyExceedRateVO>> timeMap;
            //获取出时间和id，获取日统计占比率
            for (LimitRatePO po: limitRatePOList) {
                SteadyExceedRateVO dataSingleMonitorSingeDay = getDataSingleMonitorSingeDay(qualifiesRate, lineId, po.getDayStr());

                Double rate = dataSingleMonitorSingeDay.getSteadyExceedRate();
                steadyExceedRateVO.setSteadyExceedRate(rate);
                steadyExceedRateVOS.add(steadyExceedRateVO);
            }
            System.out.println("steadyExceedRateVOS>>>>>>>>>>>>>>>>>>>>>>>>>"+steadyExceedRateVOS);
            //求时间范围的占比率平均值
            steadyExceedRateVO.setSteadyExceedRate(NumberUtil.round(steadyExceedRateVOS.stream().mapToDouble(SteadyExceedRateVO::getSteadyExceedRate).average().orElse(0), 2).doubleValue());

        }
        return steadyExceedRateVO;
    }

    /**
     * 计算区域监测点时间范围统计占比率
     */
    public SteadyExceedRateVO getDataMoreMonitorMoreDay(List<LimitRatePO> qualifiesRate) {
        SteadyExceedRateVO steadyExceedRateVO = new SteadyExceedRateVO();
        List<SteadyExceedRateVO> steadyResults = new ArrayList<>();
        //以时间分组的各项数据
        Map<String, List<LimitRatePO>> groupByTimeData = qualifiesRate.stream().collect(Collectors.groupingBy(LimitRatePO::getDayStr));
        if (CollectionUtil.isNotEmpty(groupByTimeData)) {
            Set<String> times = groupByTimeData.keySet();
            for (String time : times) {
                List<String> ids = groupByTimeData.get(time).stream().map(LimitRatePO::getLineId).collect(Collectors.toList());
                steadyResults.add(getDataMoreMonitorSingleDay(qualifiesRate, ids, time));
            }
        }
        if (CollectionUtil.isNotEmpty(steadyResults)) {
            //求区域监测点多天的平均值
            steadyExceedRateVO.setSteadyExceedRate(
            NumberUtil.round(steadyResults.stream().mapToDouble(SteadyExceedRateVO::getSteadyExceedRate).average().orElse(0), 2).doubleValue()
            );
        }
        return steadyExceedRateVO;
    }

    /**
     * 计算区域监测点日统计占比率
     */
    public SteadyExceedRateVO getDataMoreMonitorSingleDay(List<LimitRatePO> qualifiesRate, List<String> lineIds, String time) {
        SteadyExceedRateVO steadyExceedRateVO = new SteadyExceedRateVO();
        List<SteadyExceedRateVO> steadyResults = new ArrayList<>();
        for (String lineId : lineIds) {
            steadyResults.add(getDataSingleMonitorSingeDay(qualifiesRate, lineId, time));
        }
        if (CollectionUtil.isNotEmpty(steadyResults)) {
            //求多个监测点的平均值
            steadyExceedRateVO.setSteadyExceedRate(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyExceedRateVO::getSteadyExceedRate).average().orElse(0), 2).doubleValue());
            steadyExceedRateVO.setTime(time);

        }
        return steadyExceedRateVO;
    }

    /**
     * 计算区域监测点月统计占比率
     */
    public SteadyExceedRateVO getDataMoreMonitorMouth(List<LimitRatePO> qualifiesRate, List<String> lineIds, String time) {
        SteadyExceedRateVO steadyExceedRateVO = new SteadyExceedRateVO();
        List<SteadyExceedRateVO> steadyResults = new ArrayList<>();
        for (String lineId : lineIds) {
            steadyResults.add(getDataSingleMonitorMouth(qualifiesRate, lineId, time));
        }
        if (CollectionUtil.isNotEmpty(steadyResults)) {
            //求多个监测点的平均值
            steadyExceedRateVO.setSteadyExceedRate(NumberUtil.round(steadyResults.stream().mapToDouble(SteadyExceedRateVO::getSteadyExceedRate).average().orElse(0), 2).doubleValue());
            steadyExceedRateVO.setTime(time);
        }
        return steadyExceedRateVO;
    }

    /**
     * influxDB查询稳态监测点相关信息
     * @param lineIndexes
     * @param startTime
     * @param endTime
     */
    private List<LimitRatePO> getQualifiesRate(List<String> lineIndexes, String startTime, String endTime) {
        List<LimitRatePO> limitRatePOS = 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.PHASIC_TYPE  + "='" + Param.PHASIC_TYPET + "' and " + 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='71b900811d9a95811913776d6e725255' or MYINDEX='479bf4e228b5579629454ee99de0b950' or MYINDEX='1e3b8531483b2a8cbee6747f1f641cf9'";
        //sql语句
        String sql = "SELECT * FROM LIMIT_RATE WHERE " + timeId;
        //结果集
        QueryResult result = influxDbUtils.query(sql);
        //结果集映射到对象中
        InfluxDBResultMapper resultMapper = new InfluxDBResultMapper();
        limitRatePOS = resultMapper.toPOJO(result, LimitRatePO.class);
        //将时间处理为年月日的字符串
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("yyyy-MM");
        limitRatePOS.forEach(item->{
            Instant instant = item.getTime();
            ZoneId zoneId = ZoneId.systemDefault();
            LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
            String dateStr = dateTimeFormatter.format(localDateTime);
            String monthStr = monthFormatter.format(localDateTime);
            item.setDayStr(dateStr);
            item.setMouthStr(monthStr);
        });


        return limitRatePOS;
    }

}
