package com.njcn.harmonic.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.njcn.common.config.GeneralInfo;
import com.njcn.device.biz.commApi.CommLineClient;
import com.njcn.device.biz.pojo.dto.LineALLInfoDTO;
import com.njcn.device.pq.api.GeneralDeviceInfoClient;
import com.njcn.device.pq.api.LineFeignClient;
import com.njcn.device.pq.api.UserLedgerFeignClient;
import com.njcn.device.pq.pojo.dto.GeneralDeviceDTO;
import com.njcn.device.pq.pojo.dto.OverLimitLineDTO;
import com.njcn.device.pq.pojo.dto.PollutionParamDTO;
import com.njcn.device.pq.pojo.dto.WarningSubstationDTO;
import com.njcn.event.api.EventDetailFeignClient;
import com.njcn.event.pojo.vo.GeneralVO;
import com.njcn.harmonic.constant.Param;
import com.njcn.harmonic.mapper.RStatLimitTargetMapper;
import com.njcn.harmonic.pojo.excel.area.AreaExcel;
import com.njcn.harmonic.pojo.excel.monitor.ITimes;
import com.njcn.harmonic.pojo.excel.monitor.MonitorExcel;
import com.njcn.harmonic.pojo.excel.monitor.OverLimitDays;
import com.njcn.harmonic.pojo.excel.monitor.UTimes;
import com.njcn.harmonic.pojo.excel.substation.SubstationExcel;
import com.njcn.harmonic.pojo.po.day.RStatLimitRateDPO;
import com.njcn.harmonic.pojo.po.day.RStatLimitTargetDPO;
import com.njcn.harmonic.pojo.vo.MonitorOverLimitVO;
import com.njcn.harmonic.pojo.vo.OverAreaLimitVO;
import com.njcn.harmonic.pojo.vo.OverAreaVO;
import com.njcn.harmonic.pojo.vo.WarningSubstationVO;
import com.njcn.harmonic.mapper.RStatLimitRateDMapper;
import com.njcn.harmonic.service.IAnalyzeService;
import com.njcn.poi.excel.ExcelUtil;
import com.njcn.supervision.pojo.param.user.UserReportParam;
import com.njcn.supervision.pojo.vo.user.UserLedgerVO;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.system.enums.DicDataTypeEnum;
import com.njcn.system.pojo.po.DictData;
import com.njcn.web.utils.RequestUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.File;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 类的介绍：
 *
 * @author xuyang
 * @version 1.0.0
 * @createTime 2022/4/29 14:48
 */
@Slf4j
@Service
@AllArgsConstructor
public class AnalyzeServiceImpl implements IAnalyzeService {

    private final GeneralDeviceInfoClient generalDeviceInfoClient;

    private final GeneralInfo generalInfo;

    private final HarmonicServiceImpl harmonicService;

    private final LineFeignClient lineFeignClient;

    private final RStatLimitRateDMapper rateDMapper;

    private final RStatLimitTargetMapper targetMapper;
    private final EventDetailFeignClient eventDetailFeignClient;
    private final DicDataFeignClient dicDataFeignClient;
    private final UserLedgerFeignClient userLedgerFeignClient;
    private final CommLineClient commLineClient;
    @Override
    public Page<OverAreaLimitVO> getAreaData(OverAreaVO param) {
        Page<OverAreaLimitVO> page = new Page<>();
        page.setSize(param.getPageSize());
        page.setCurrent(param.getPageNum());
        List<OverAreaLimitVO> list = new ArrayList<>();
        param.setServerName(generalInfo.getMicroServiceName());
        List<GeneralDeviceDTO> deptList = generalDeviceInfoClient.getPracticalRunDeviceInfo(param).getData();
        if (!CollectionUtils.isEmpty(deptList)) {
            page.setTotal(deptList.size());
            int pages = (int) Math.ceil(deptList.size() * 1.0 / param.getPageSize());
            page.setPages(pages);
            List<List<GeneralDeviceDTO>> pageList = Lists.partition(deptList, param.getPageSize());
            List<GeneralDeviceDTO> temList = pageList.get(param.getPageNum() - 1);
            temList.forEach(item -> {
                OverAreaLimitVO overAreaLimitVO = new OverAreaLimitVO();
                if (!CollectionUtils.isEmpty(item.getLineIndexes())) {
                    List<RStatLimitTargetDPO> limitTargetList = harmonicService.getLimitTarget(item.getLineIndexes(), param.getSearchBeginTime(), param.getSearchEndTime());
                    overAreaLimitVO = handleAreaData(limitTargetList, item.getLineIndexes(), param);
                }
                overAreaLimitVO.setName(item.getName());
                list.add(overAreaLimitVO);
            });
            if (!CollectionUtils.isEmpty(list)) {
                List<OverAreaLimitVO> recordList = new ArrayList<>();
                //默认 根据在线监测点个数 倒叙排序
                recordList = list.stream().sorted(Comparator.comparing(OverAreaLimitVO::getOnlineMonitorNumber).reversed().thenComparing(OverAreaLimitVO::getName)).collect(Collectors.toList());
                page.setRecords(recordList);
                return page;
            }
        }
        return page;
    }

    @Override
    @SuppressWarnings("unchecked")
    public Page<WarningSubstationVO> getWarningSubstation(OverAreaVO param) {
        //获取暂态统计指标
        List<DictData> eventStatis = dicDataFeignClient.getDicDataByTypeCode(
                DicDataTypeEnum.EVENT_STATIS.getCode()).getData();

        Page<WarningSubstationVO> page = new Page<>();
        page.setSize(param.getPageSize());
        page.setCurrent(param.getPageNum());
        List<String> line = new ArrayList<>(), subInfo = new ArrayList<>();
        List<WarningSubstationDTO> substationDTOList = new ArrayList<>();
        Map<String, List<RStatLimitRateDPO>> map = new HashMap<>();
        Map<String, GeneralVO> map2 = new HashMap<>();
        param.setServerName(generalInfo.getMicroServiceName());
        List<WarningSubstationVO> list = new ArrayList<>();
        List<GeneralDeviceDTO> subList = generalDeviceInfoClient.getPracticalRunDeviceInfoAsSubstation(param).getData();
        if (!CollectionUtils.isEmpty(subList)) {
            page.setTotal(subList.size());
            int pages = (int) Math.ceil(subList.size() * 1.0 / param.getPageSize());
            page.setPages(pages);
            List<List<GeneralDeviceDTO>> pageList = Lists.partition(subList, param.getPageSize());
            List<GeneralDeviceDTO> temList = pageList.get(param.getPageNum() - 1);
            temList.forEach(item -> {
                line.addAll(item.getLineIndexes());
                subInfo.add(item.getIndex());
            });
            if (!CollectionUtils.isEmpty(line)) {
                substationDTOList = lineFeignClient.getWarningSub(subInfo).getData();
                List<RStatLimitRateDPO> limitRateList = (List<RStatLimitRateDPO>) getAllData(line, param.getSearchBeginTime(), param.getSearchEndTime(), Param.LIMIT_RATE);
                if (!CollectionUtils.isEmpty(limitRateList)) {
                    map = limitRateList.stream().collect(Collectors.groupingBy(RStatLimitRateDPO::getLineId));
                }
                List<GeneralVO> eventDetailList = getEventDetail(line, param.getSearchBeginTime(), param.getSearchEndTime(), eventStatis);
                if (!CollectionUtils.isEmpty(eventDetailList)) {
                    map2 = eventDetailList.stream().collect(Collectors.toMap(GeneralVO::getId, Function.identity()));
                }
            }
            for (GeneralDeviceDTO item : temList) {
                int onlineMonitorCount = 0, alertMonitorCount = 0, alertCount = 0, freqCount = 0, voltageCount = 0, ubalanceCount = 0, flickerCount = 0, iNegCount = 0, uharmCount = 0, iharmCount = 0, inuharmCount = 0;
                int freqCounts = 0, voltageCounts = 0, uharmCounts = 0, iharmCounts = 0, ubalanceCounts = 0, flickerCounts = 0, inuharmCounts = 0, iNegCounts = 0;
                int eventUp = 0, eventDown = 0, eventInterrupt = 0, eventUps = 0, eventDowns = 0, eventInterrupts = 0;
                WarningSubstationVO warningSubstationVO = new WarningSubstationVO();
                //电站id
                warningSubstationVO.setId(item.getIndex());
                //电站名称
                warningSubstationVO.setPlantName(item.getName());
                List<String> lineList = item.getLineIndexes();
                if (!CollectionUtils.isEmpty(lineList)) {
                    for (String item2 : lineList) {
                        //谐波统计
                        if (!CollectionUtils.isEmpty(map.get(item2))) {
                            RStatLimitRateDPO limitRate = map.get(item2).get(0);
                            if (!Objects.isNull(limitRate)) {
                                onlineMonitorCount++;
                                if (limitRate.getFreqDevOvertime() > 0 || limitRate.getVoltageDevOvertime() > 0 || limitRate.getUbalanceOvertime() > 0 || limitRate.getFlickerOvertime() > 0 || limitRate.getINegOvertime() > 0 || limitRate.getUharm2Overtime() > 0 || limitRate.getIharm2Overtime() > 0 || limitRate.getInuharm1Overtime() > 0) {
                                    alertMonitorCount++;
                                }
                                if (limitRate.getFreqDevOvertime() > 0) {
                                    freqCount++;
                                    freqCounts = freqCounts + limitRate.getFreqDevOvertime();
                                }
                                if (limitRate.getVoltageDevOvertime() > 0) {
                                    voltageCount++;
                                    voltageCounts = voltageCounts + limitRate.getVoltageDevOvertime();
                                }
                                if (limitRate.getUharm2Overtime() > 0) {
                                    uharmCount++;
                                    uharmCounts = uharmCounts + limitRate.getUharm2Overtime();
                                }
                                if (limitRate.getIharm2Overtime() > 0) {
                                    iharmCount++;
                                    iharmCounts = iharmCounts + limitRate.getIharm2Overtime();
                                }
                                if (limitRate.getUbalanceOvertime() > 0) {
                                    ubalanceCount++;
                                    ubalanceCounts = ubalanceCounts + limitRate.getUbalanceOvertime();
                                }
                                if (limitRate.getFlickerOvertime() > 0) {
                                    flickerCount++;
                                    flickerCounts = flickerCounts + limitRate.getFlickerOvertime();
                                }
                                if (limitRate.getInuharm1Overtime() > 0) {
                                    inuharmCount++;
                                    inuharmCounts = inuharmCounts + limitRate.getInuharm1Overtime();
                                }
                                if (limitRate.getINegOvertime() > 0) {
                                    iNegCount++;
                                    iNegCounts = iNegCounts + limitRate.getINegOvertime();
                                }
                                alertCount = limitRate.getFreqDevOvertime() + limitRate.getVoltageDevOvertime() + limitRate.getUbalanceOvertime() + limitRate.getFlickerOvertime() + limitRate.getINegOvertime() + limitRate.getUharm2Overtime() + limitRate.getIharm2Overtime() + limitRate.getInuharm1Overtime();
                            }
                        }
                        //暂降统计
                        if (ObjectUtil.isNotNull(map2.get(item2))) {
                            GeneralVO eventDetailList = map2.get(item2);
                            if (ObjectUtil.isNotNull(eventDetailList)) {
                                //电压暂降
                                eventDowns += eventDetailList.getSagsCount();
                                //电压暂升
                                eventUps += eventDetailList.getUpCount();
                                //电压暂升
                                eventInterrupts += eventDetailList.getBreakCount();
                            }
                        }
                        if (eventDowns > 0) {
                            eventDown++;
                        }
                        if (eventUps > 0) {
                            eventUp++;
                        }
                        if (eventInterrupts > 0) {
                            eventInterrupt++;
                        }
                    }
                }
                //在线监测点数量(个)
                warningSubstationVO.setOnlineMonitorCounts(onlineMonitorCount);
                if (onlineMonitorCount != 0) {
                    //告警监测点数量(个)
                    warningSubstationVO.setAlertMonitorCounts(alertMonitorCount);
                    //告警次数
                    warningSubstationVO.setAlertCounts(alertCount);
                    //告警频次(次/点)
                    warningSubstationVO.setAlertAlarmFrequency(alertMonitorCount == 0 ? 0 : BigDecimal.valueOf(alertCount * 1.0 / alertMonitorCount).setScale(2, RoundingMode.HALF_UP).doubleValue());
                    //频率偏差
                    warningSubstationVO.setFrequencyDeviation(freqCount == 0 ? 0 : BigDecimal.valueOf(freqCounts * 1.0 / freqCount).setScale(2, RoundingMode.HALF_UP).doubleValue());
                    //电压偏差
                    warningSubstationVO.setVoltageDeviation(voltageCount == 0 ? 0 : BigDecimal.valueOf(voltageCounts * 1.0 / voltageCount).setScale(2, RoundingMode.HALF_UP).doubleValue());
                    //谐波电压
                    warningSubstationVO.setHarmonicVoltage(uharmCount == 0 ? 0 : BigDecimal.valueOf(uharmCounts * 1.0 / uharmCount).setScale(2, RoundingMode.HALF_UP).doubleValue());
                    //谐波电流
                    warningSubstationVO.setHarmonicCurrent(iharmCount == 0 ? 0 : BigDecimal.valueOf(iharmCounts * 1.0 / iharmCount).setScale(2, RoundingMode.HALF_UP).doubleValue());
                    //三相电压不平衡
                    warningSubstationVO.setThreePhaseVoltageUnbalance(ubalanceCount == 0 ? 0 : BigDecimal.valueOf(ubalanceCounts * 1.0 / ubalanceCount).setScale(2, RoundingMode.HALF_UP).doubleValue());
                    //闪变
                    warningSubstationVO.setFlicker(flickerCount == 0 ? 0 : BigDecimal.valueOf(flickerCounts * 1.0 / flickerCount).setScale(2, RoundingMode.HALF_UP).doubleValue());
                    //间谐波电压
                    warningSubstationVO.setInterHarmonic(inuharmCount == 0 ? 0 : BigDecimal.valueOf(inuharmCounts * 1.0 / inuharmCount).setScale(2, RoundingMode.HALF_UP).doubleValue());
                    //负序电流
                    warningSubstationVO.setNegative(iNegCount == 0 ? 0 : BigDecimal.valueOf(iNegCounts * 1.0 / iNegCount).setScale(2, RoundingMode.HALF_UP).doubleValue());
                    //电压暂降
                    warningSubstationVO.setVoltageDip(eventDown == 0 ? 0 : BigDecimal.valueOf(eventDowns * 1.0 / eventDown).setScale(2, RoundingMode.HALF_UP).doubleValue());
                    //电压暂升
                    warningSubstationVO.setVoltageSwell(eventUp == 0 ? 0 : BigDecimal.valueOf(eventUps * 1.0 / eventUp).setScale(2, RoundingMode.HALF_UP).doubleValue());
                    //短时中断
                    warningSubstationVO.setShortInterruption(eventInterrupt == 0 ? 0 : BigDecimal.valueOf(eventInterrupts * 1.0 / eventInterrupt).setScale(2, RoundingMode.HALF_UP).doubleValue());
                }
                list.add(warningSubstationVO);
            }
        }
        if (!CollectionUtils.isEmpty(list)) {
            for (WarningSubstationVO item1 : list) {
                for (WarningSubstationDTO item2 : substationDTOList) {
                    if (Objects.equals(item1.getId(), item2.getId())) {
                        item1.setProvinceCompany(item2.getProvince());
                        item1.setCityCompany(item2.getCity());
                        item1.setPlantVoltageLevel(item2.getScale());
                    }
                }
            }
            List<WarningSubstationVO> recordList = new ArrayList<>();
            //默认 在线监测点数量 倒叙排序
            recordList = list.stream().sorted(Comparator.comparing(WarningSubstationVO::getOnlineMonitorCounts).reversed().thenComparing(WarningSubstationVO::getPlantName)).collect(Collectors.toList());
            page.setRecords(recordList);
            return page;
        }
        return page;
    }

    @Override
    public Page<MonitorOverLimitVO> monitorOverLimitVO(OverAreaVO param) {
        Page<MonitorOverLimitVO> page = new Page<>();
        page.setSize(param.getPageSize());
        page.setCurrent(param.getPageNum());
        List<MonitorOverLimitVO> result = new ArrayList<>();
        List<String> lineList = new ArrayList<>();
        List<GeneralDeviceDTO> deptList = generalDeviceInfoClient.getPracticalRunDeviceInfo(param).getData();
        if (!CollectionUtils.isEmpty(deptList)) {
            deptList.forEach(item -> {
                lineList.addAll(item.getLineIndexes());
            });
        }
        List<String> filterLineList = new ArrayList<>();

        if(CollectionUtil.isNotEmpty(lineList)){
            //根据searchvalue过滤
            String searchvalue=param.getSearchValue();
            List<LineALLInfoDTO> data = commLineClient.getLineAllDetailList(lineList).getData();
            filterLineList= data.stream()
                    .filter(dto -> {
                        LineALLInfoDTO.LineLineDTO lineDTO = dto.getLineLineDTO();
                        String linename = lineDTO != null ? lineDTO.getLinename() : null;
                        String objName2 = lineDTO != null ? lineDTO.getObjName2() : null;

                        LineALLInfoDTO.LineSubStationDTO subStationDTO = dto.getLineSubStationDTO();
                        String subStationName = subStationDTO != null ? subStationDTO.getSubStationName() : null;

                        // 大小写敏感的模糊匹配（相当于 MySQL 的 LIKE '%keyword%'）
                        return (linename != null && linename.contains(searchvalue))
                                || (objName2 != null && objName2.contains(searchvalue))
                                || (subStationName != null && subStationName.contains(searchvalue));
                    }).map(dto -> dto.getLineLineDTO() != null ? dto.getLineLineDTO().getLineId() : null)

                    .collect(Collectors.toList());
        }
        if (CollectionUtil.isNotEmpty(filterLineList)) {
            page = targetMapper.getSumLimitTargetPage(page, filterLineList, DateUtil.beginOfDay(DateUtil.parse(param.getSearchBeginTime())),
                    DateUtil.endOfDay(DateUtil.parse(param.getSearchEndTime())));
            List<MonitorOverLimitVO> pageRecords = page.getRecords();
            if (CollectionUtils.isEmpty(pageRecords)) {
                return page;
            }
            Map<String, List<MonitorOverLimitVO>> monMap = pageRecords.stream().collect(Collectors.groupingBy(MonitorOverLimitVO::getId));
            ArrayList<String> list = new ArrayList<>();
            list.addAll(monMap.keySet());
            PollutionParamDTO pollutionParamDTO = new PollutionParamDTO();
            pollutionParamDTO.setLineList(list);
            List<OverLimitLineDTO> overLimitLineList = lineFeignClient.getOverLimitLineInfo(pollutionParamDTO).getData();
            List<RStatLimitTargetDPO> rStatLimitTargetDPOS = targetMapper.selectList(new QueryWrapper<RStatLimitTargetDPO>()
                    .in("r_stat_limit_target_d.my_index", list).between("r_stat_limit_target_d.time_id", DateUtil.beginOfDay(DateUtil.parse(param.getSearchBeginTime())),
                            DateUtil.endOfDay(DateUtil.parse(param.getSearchEndTime()))));
            Map<String, List<RStatLimitTargetDPO>> rsMap = rStatLimitTargetDPOS.stream().collect(Collectors.groupingBy(RStatLimitTargetDPO::getLineId));


            List<UserLedgerVO> userLedgerVOList = userLedgerFeignClient.selectUserList(new UserReportParam()).getData();
            Map<String, UserLedgerVO> userLedgerVOMap = userLedgerVOList.stream().collect(Collectors.toMap(UserLedgerVO::getId, Function.identity()));

            for (OverLimitLineDTO vo : overLimitLineList) {
                MonitorOverLimitVO monitorOverLimitVO = new MonitorOverLimitVO();
                BeanUtil.copyProperties(vo, monitorOverLimitVO);
                MonitorOverLimitVO source = monMap.get(vo.getId()).get(0);
                UserLedgerVO userLedgerVO = userLedgerVOMap.getOrDefault(vo.getObjId(), null);
                monitorOverLimitVO.setLineObjectName(Objects.nonNull(userLedgerVO) ? userLedgerVO.getProjectName() : "/");
                monitorOverLimitVO.setOverDay(source.getOverDay());
                monitorOverLimitVO.setVolDevOverDay(source.getVolDevOverDay());
                monitorOverLimitVO.setFreqOverDay(source.getFreqOverDay());
                monitorOverLimitVO.setFlickerOverDay(source.getFlickerOverDay());
                monitorOverLimitVO.setNegativeOverDay(source.getNegativeOverDay());
                monitorOverLimitVO.setThreeUnbalance(source.getThreeUnbalance());
                monitorOverLimitVO.setVolDisOverDay(source.getVolDisOverDay());
                monitorOverLimitVO.setOverVolThreeTimes(source.getOverVolThreeTimes());
                monitorOverLimitVO.setOverVolFiveTimes(source.getOverVolFiveTimes());
                monitorOverLimitVO.setOverVolSevenTimes(source.getOverVolSevenTimes());
                monitorOverLimitVO.setOverVolElevenTimes(source.getOverVolElevenTimes());
                monitorOverLimitVO.setOverVolThirteenTimes(source.getOverVolThirteenTimes());
                monitorOverLimitVO.setOverVolTwentyThreeTimes(source.getOverVolTwentyThreeTimes());
                monitorOverLimitVO.setOverVolTwentyFiveTimes(source.getOverVolTwentyFiveTimes());
                monitorOverLimitVO.setOverVolOtherTimes(source.getOverVolOtherTimes());
                monitorOverLimitVO.setOverCurThreeTimes(source.getOverCurThreeTimes());
                monitorOverLimitVO.setOverCurFiveTimes(source.getOverCurFiveTimes());
                monitorOverLimitVO.setOverCurSevenTimes(source.getOverCurSevenTimes());
                monitorOverLimitVO.setOverCurElevenTimes(source.getOverCurElevenTimes());
                monitorOverLimitVO.setOverCurThirteenTimes(source.getOverCurThirteenTimes());
                monitorOverLimitVO.setOverCurTwentyThreeTimes(source.getOverCurTwentyThreeTimes());
                monitorOverLimitVO.setOverCurTwentyFiveTimes(source.getOverCurTwentyFiveTimes());
                monitorOverLimitVO.setOverCurOtherTimes(source.getOverCurOtherTimes());
                List<RStatLimitTargetDPO> limitRateDPOS = rsMap.get(vo.getId());
                // 谐波电压含有率超标天数
                long volContainOverDay = limitRateDPOS.stream().filter(t -> filterNotQualifiedUharmData(t)).count();
                // 谐波电流超标天数
                long harmCurOverDay = limitRateDPOS.stream().filter(t -> filterNotQualifiedIharmData(t)).count();
                // 间谐波电压含有率超标天数
                long intHarmOverDay = limitRateDPOS.stream().filter(t -> filterNotQualifiedInuharmData(t)).count();
                // 谐波电压超标天数
                long harmVolOverDay = limitRateDPOS.stream().filter(t -> filterNotQualifiedHarmVolData(t)).count();
                monitorOverLimitVO.setHarmVolOverDay((int) harmVolOverDay);
                monitorOverLimitVO.setVolContainOverDay((int) volContainOverDay);
                monitorOverLimitVO.setHarmCurOverDay((int) harmCurOverDay);
                monitorOverLimitVO.setIntHarmOverDay((int) intHarmOverDay);
                result.add(monitorOverLimitVO);
            }

        }
        if (!CollectionUtils.isEmpty(result)) {
            //默认 根据在线监测点个数 倒叙排序
            List<MonitorOverLimitVO> recordList = result.stream().sorted(Comparator.comparing(MonitorOverLimitVO::getOverDay).reversed()).collect(Collectors.toList());
            page.setRecords(recordList);
            return page;
        }
        return page;
    }

    private boolean filterNotQualifiedHarmVolData(RStatLimitTargetDPO t) {
        // 电压畸变率+谐波电压含有率（2-25次）
        int i = t.getUaberranceOvertime() + t.getUharm2Overtime() + t.getUharm3Overtime() + t.getUharm4Overtime() + t.getUharm5Overtime() + t.getUharm6Overtime() + t.getUharm7Overtime() + t.getUharm8Overtime() + t.getUharm9Overtime() + t.getUharm10Overtime() + t.getUharm11Overtime() + t.getUharm12Overtime() + t.getUharm13Overtime() + t.getUharm14Overtime() + t.getUharm15Overtime() + t.getUharm16Overtime() + t.getUharm17Overtime() + t.getUharm18Overtime() + t.getUharm19Overtime() + t.getUharm20Overtime() + t.getUharm21Overtime() + t.getUharm22Overtime() + t.getUharm23Overtime() + t.getUharm24Overtime() + t.getUharm25Overtime();
        if (i > 0) {
            return true;
        }
        return false;
    }

    private boolean filterNotQualifiedInuharmData(RStatLimitTargetDPO t) {
        int i = t.getInuharm1Overtime() + t.getInuharm2Overtime() + t.getInuharm3Overtime() + t.getInuharm4Overtime() + t.getInuharm5Overtime() + t.getInuharm6Overtime() + t.getInuharm7Overtime() + t.getInuharm8Overtime() + t.getInuharm9Overtime() + t.getInuharm10Overtime() + t.getInuharm11Overtime() + t.getInuharm12Overtime() + t.getInuharm13Overtime() + t.getInuharm14Overtime() + t.getInuharm15Overtime() + t.getInuharm16Overtime();
        if (i > 0) {
            return true;
        }
        return false;
    }

    private boolean filterNotQualifiedIharmData(RStatLimitTargetDPO t) {
        int i = t.getIharm2Overtime() + t.getIharm3Overtime() + t.getIharm4Overtime() + t.getIharm5Overtime() + t.getIharm6Overtime() + t.getIharm7Overtime() + t.getIharm8Overtime() + t.getIharm9Overtime() + t.getIharm10Overtime() + t.getIharm11Overtime() + t.getIharm12Overtime() + t.getIharm13Overtime() + t.getIharm14Overtime() + t.getIharm15Overtime() + t.getIharm16Overtime() + t.getIharm17Overtime() + t.getIharm18Overtime() + t.getIharm19Overtime() + t.getIharm20Overtime() + t.getIharm21Overtime() + t.getIharm22Overtime() + t.getIharm23Overtime() + t.getIharm24Overtime() + t.getIharm25Overtime();
        if (i > 0) {
            return true;
        }
        return false;
    }

    private boolean filterNotQualifiedUharmData(RStatLimitTargetDPO t) {
        if (t.getUharm2Overtime() != 0 || t.getUharm3Overtime() != 0 || t.getUharm4Overtime() != 0 || t.getUharm5Overtime() != 0 || t.getUharm6Overtime() != 0 || t.getUharm7Overtime() != 0 || t.getUharm8Overtime() != 0 || t.getUharm9Overtime() != 0 || t.getUharm10Overtime() != 0 || t.getUharm11Overtime() != 0 || t.getUharm12Overtime() != 0 || t.getUharm13Overtime() != 0 || t.getUharm14Overtime() != 0 || t.getUharm15Overtime() != 0 || t.getUharm16Overtime() != 0 || t.getUharm17Overtime() != 0 || t.getUharm18Overtime() != 0 || t.getUharm19Overtime() != 0 || t.getUharm20Overtime() != 0 || t.getUharm21Overtime() != 0 || t.getUharm22Overtime() != 0 || t.getUharm23Overtime() != 0 || t.getUharm24Overtime() != 0 || t.getUharm25Overtime() != 0) {
            return true;
        }
        return false;
    }

    @Override
    public String exportArea(OverAreaVO param, String methodDescribe) {
        List<AreaExcel> result = new ArrayList<>();
        String fileName = methodDescribe + CharUtil.UNDERLINE + param.getSearchBeginTime() + CharUtil.DASHED + param.getSearchEndTime() + CharUtil.UNDERLINE + param.getStatisticalType().getName() + ".xlsx";
        String targetDir = generalInfo.getBusinessTempPath() + File.separator + RequestUtil.getUserIndex();
        log.error("文件路径" + targetDir);
        log.error("文件名" + fileName);
        File excel = new File(targetDir, fileName);
        List<OverAreaLimitVO> list = getAreaData(param).getRecords();
        if (!CollectionUtil.isEmpty(list)) {
            list.forEach(item -> {
                AreaExcel areaExcel = new AreaExcel();
                BeanUtil.copyProperties(item, areaExcel);
                result.add(areaExcel);
            });
        }
        ExcelUtil.exportExcelWithTargetFile(excel, param.getStatisticalType().getName(), methodDescribe, AreaExcel.class, result);
        return FileUtil.getAbsolutePath(excel);
    }

    @Override
    public String exportSubstation(OverAreaVO param, String methodDescribe) {
        List<SubstationExcel> result = new ArrayList<>();
        String fileName = methodDescribe + CharUtil.UNDERLINE + param.getSearchBeginTime() + CharUtil.DASHED + param.getSearchEndTime() + CharUtil.UNDERLINE + param.getStatisticalType().getName() + ".xlsx";
        String targetDir = generalInfo.getBusinessTempPath() + File.separator + RequestUtil.getUserIndex();
        log.error("文件路径" + targetDir);
        log.error("文件名" + fileName);
        File excel = new File(targetDir, fileName);
        List<WarningSubstationVO> list = getWarningSubstation(param).getRecords();
        if (!CollectionUtil.isEmpty(list)) {
            list.forEach(item -> {
                SubstationExcel substationExcel = new SubstationExcel();
                BeanUtil.copyProperties(item, substationExcel);
                result.add(substationExcel);
            });
        }
        ExcelUtil.exportExcelWithTargetFile(excel, param.getStatisticalType().getName(), methodDescribe, SubstationExcel.class, result);
        return FileUtil.getAbsolutePath(excel);
    }

    @Override
    public String exportMonitor(OverAreaVO param, String methodDescribe) {
        List<MonitorExcel> result = new ArrayList<>();
        String fileName = methodDescribe + CharUtil.UNDERLINE + param.getSearchBeginTime() + CharUtil.DASHED + param.getSearchEndTime() + CharUtil.UNDERLINE + param.getStatisticalType().getName() + ".xlsx";
        String targetDir = generalInfo.getBusinessTempPath() + File.separator + RequestUtil.getUserIndex();
        log.error("文件路径" + targetDir);
        log.error("文件名" + fileName);
        File excel = new File(targetDir, fileName);
        List<MonitorOverLimitVO> list = monitorOverLimitVO(param).getRecords();
        if (!CollectionUtil.isEmpty(list)) {
            list.forEach(item -> {
                MonitorExcel monitorExcel = new MonitorExcel();
                BeanUtil.copyProperties(item, monitorExcel);

                OverLimitDays overLimitDays = new OverLimitDays();
                List<UTimes> list1 = new ArrayList<>();
                UTimes uTimes = new UTimes();
                BeanUtil.copyProperties(item, uTimes);
                list1.add(uTimes);
                overLimitDays.setUTimes(list1);

                List<ITimes> list2 = new ArrayList<>();
                ITimes iTimes = new ITimes();
                BeanUtil.copyProperties(item, iTimes);
                list2.add(iTimes);
                overLimitDays.setITimes(list2);

                List<OverLimitDays> list3 = new ArrayList<>();
                BeanUtil.copyProperties(item, overLimitDays);
                list3.add(overLimitDays);
                monitorExcel.setOverLimitDays(list3);
                result.add(monitorExcel);
            });
        }
        ExcelUtil.exportExcelWithTargetFile(excel, param.getStatisticalType().getName(), methodDescribe, MonitorExcel.class, result);
        return FileUtil.getAbsolutePath(excel);
    }


    /**
     * 功能描述: 处理区域(变电站)在线监测点数、超标监测点数、平均超标天数、占比
     *
     * @param list 集合
     *             type 类型
     * @return
     * @author xy
     * @date 2022/2/25 15:05
     */
    @SuppressWarnings("unchecked")
    public OverAreaLimitVO handleAreaData(List<RStatLimitTargetDPO> list, List<String> line, OverAreaVO param) {
        int onlineCount = 0, overLineCount = 0, freqCount = 0, voltageCount = 0, ubalanceCount = 0, flickerCount = 0, iNegCount = 0, uharmCount = 0, iharmCount = 0, inuharmCount = 0;
        OverAreaLimitVO overAreaLimitVO = new OverAreaLimitVO();

        Set<String> freqList = new HashSet<>();
        Set<String> voltageMonitorList = new HashSet<>();
        Set<String> threePhaseVoltageMonitorList = new HashSet<>();
        Set<String> flickerMonitorList = new HashSet<>();
        Set<String> harmonicVoltageMonitorList = new HashSet<>();
        Set<String> harmonicCurrentMonitorList = new HashSet<>();
        Set<String> harmonicInHarmonciVMonitorList = new HashSet<>();

        int threeV = 0, fiveV = 0, sevenV = 0, elevenV = 0, otherV = 0;
        Set<String> threeVList = new HashSet<>(), fiveVList = new HashSet<>(), sevenVList = new HashSet<>(), elevenVList = new HashSet<>(), otherVList = new HashSet<>();
        int threeI = 0, fiveI = 0, sevenI = 0, elevenI = 0,otherI = 0;
        Set<String> threeIList = new HashSet<>(), fiveIList = new HashSet<>(), sevenIList = new HashSet<>(), elevenIList = new HashSet<>(), otherIList = new HashSet<>();


        if (!CollectionUtils.isEmpty(list)) {
            List<RStatLimitTargetDPO> data = (List<RStatLimitTargetDPO>) getAllData(line, param.getSearchBeginTime(), param.getSearchEndTime(), Param.LIMIT_TARGET);
            if (!CollectionUtils.isEmpty(data)) {
                onlineCount = data.size();
                //在线监测点个数
                overAreaLimitVO.setOnlineMonitorNumber(onlineCount);

                for (RStatLimitTargetDPO item : data) {
                    if (item.getFreqDevOvertime() > 0 || item.getVoltageDevOvertime() > 0 || item.getUbalanceOvertime() > 0 || item.getFlickerOvertime() > 0 || item.getINegOvertime() > 0 || item.getUharm2Overtime() > 0 || item.getIharm2Overtime() > 0 || item.getInuharm1Overtime() > 0) {
                        overLineCount++;
                    }
                    if (item.getFreqDevOvertime() > 0) {
                        freqCount++;
                    }
                    if (item.getVoltageDevOvertime() > 0) {
                        voltageCount++;
                    }
                    if (item.getUbalanceOvertime() > 0) {
                        ubalanceCount++;
                    }
                    if (item.getFlickerOvertime() > 0) {
                        flickerCount++;
                    }
                    if (item.getINegOvertime() > 0) {
                        iNegCount++;
                    }
                    if (vHarmFlag(item)) {
                        uharmCount++;
                    }

                    if (iHarmFlag(item)) {
                        iharmCount++;
                    }
                    if (inHarmFlag(item)) {
                        inuharmCount++;
                    }



                    //区域报告
                    if (Objects.nonNull(param.getAreaReportFlag()) && param.getAreaReportFlag() == 1) {
                        if (item.getFreqDevOvertime() > 0) {
                            freqList.add(item.getLineId());
                        }
                        if (item.getVoltageDevOvertime() > 0) {
                            voltageMonitorList.add(item.getLineId());
                        }
                        if (item.getUbalanceOvertime() > 0) {
                            threePhaseVoltageMonitorList.add(item.getLineId());
                        }
                        if (item.getFlickerOvertime() > 0) {
                            flickerMonitorList.add(item.getLineId());
                        }
                        if (vHarmFlag(item)) {
                            harmonicVoltageMonitorList.add(item.getLineId());
                        }

                        if (iHarmFlag(item)) {
                            harmonicCurrentMonitorList.add(item.getLineId());
                        }


                        if (inHarmFlag(item)) {
                            harmonicInHarmonciVMonitorList.add(item.getLineId());
                        }

                        if (item.getUharm3Overtime() > 0) {
                            threeV++;
                            threeVList.add(item.getLineId());
                        }
                        if (item.getUharm5Overtime() > 0) {
                            fiveV++;
                            fiveVList.add(item.getLineId());
                        }
                        if (item.getUharm7Overtime() > 0) {
                            sevenV++;
                            sevenVList.add(item.getLineId());
                        }
                        if (item.getUharm11Overtime() > 0) {
                            elevenV++;
                            elevenVList.add(item.getLineId());
                        }
                        if (vOtherHarmFlag(item)) {
                            otherV++;
                            otherVList.add(item.getLineId());
                        }


                        if (item.getIharm3Overtime() > 0) {
                            threeI++;
                            threeIList.add(item.getLineId());
                        }
                        if (item.getIharm5Overtime() > 0) {
                            fiveI++;
                            fiveIList.add(item.getLineId());
                        }
                        if (item.getIharm7Overtime() > 0) {
                            sevenI++;
                            sevenIList.add(item.getLineId());
                        }
                        if (item.getIharm11Overtime() > 0) {
                            elevenI++;
                            elevenIList.add(item.getLineId());
                        }
                        if (iOtherHarmFlag(item)) {
                            otherI++;
                            otherIList.add(item.getLineId());
                        }

                    }
                }


                //超标监测点个数
                overAreaLimitVO.setOverLimitMonitorNumber(overLineCount);
                //超标监测点数占比
                overAreaLimitVO.setOverBiLi(BigDecimal.valueOf(overLineCount * 1.0 / data.size() * 100).setScale(2, RoundingMode.HALF_UP).doubleValue());

                //频率偏差
                overAreaLimitVO.setFrequencyMonitorNumber(freqCount);
                overAreaLimitVO.setFrequencyBiLi(BigDecimal.valueOf(freqCount * 1.0 / data.size() * 100).setScale(2, RoundingMode.HALF_UP).doubleValue());
                overAreaLimitVO.setFreqMonitorList(new ArrayList<>(freqList));
                //电压偏差
                overAreaLimitVO.setVoltageMonitorNumber(voltageCount);
                overAreaLimitVO.setVoltageBiLi(BigDecimal.valueOf(voltageCount * 1.0 / data.size() * 100).setScale(2, RoundingMode.HALF_UP).doubleValue());
                overAreaLimitVO.setVoltageMonitorList(new ArrayList<>(voltageMonitorList));
                //三相电压不平衡度
                overAreaLimitVO.setThreePhaseVoltageMonitorNumber(ubalanceCount);
                overAreaLimitVO.setThreePhaseVoltageBiLi(BigDecimal.valueOf(ubalanceCount * 1.0 / data.size() * 100).setScale(2, RoundingMode.HALF_UP).doubleValue());
                overAreaLimitVO.setThreePhaseVoltageMonitorList(new ArrayList<>(threePhaseVoltageMonitorList));
                //闪变
                overAreaLimitVO.setFlickerMonitorNumber(flickerCount);
                overAreaLimitVO.setFlickerBiLi(BigDecimal.valueOf(flickerCount * 1.0 / data.size() * 100).setScale(2, RoundingMode.HALF_UP).doubleValue());
                overAreaLimitVO.setFlickerMonitorList(new ArrayList<>(flickerMonitorList));
                //负序电流
                overAreaLimitVO.setNegativeMonitorNumber(iNegCount);
                overAreaLimitVO.setNegativeBiLi(BigDecimal.valueOf(iNegCount * 1.0 / data.size() * 100).setScale(2, RoundingMode.HALF_UP).doubleValue());
                //谐波电压
                overAreaLimitVO.setHarmonicVoltageMonitorNumber(uharmCount);
                overAreaLimitVO.setHarmonicVoltageBiLi(BigDecimal.valueOf(uharmCount * 1.0 / data.size() * 100).setScale(2, RoundingMode.HALF_UP).doubleValue());
                overAreaLimitVO.setHarmonicVoltageMonitorList(new ArrayList<>(harmonicVoltageMonitorList));

                //谐波电流
                overAreaLimitVO.setHarmonicCurrentMonitorNumber(iharmCount);
                overAreaLimitVO.setHarmonicCurrentBiLi(BigDecimal.valueOf(iharmCount * 1.0 / data.size() * 100).setScale(2, RoundingMode.HALF_UP).doubleValue());
                overAreaLimitVO.setHarmonicCurrentMonitorList(new ArrayList<>(harmonicCurrentMonitorList));
                //间谐波电压
                overAreaLimitVO.setInterHarmonicMonitorNumber(inuharmCount);
                overAreaLimitVO.setInterHarmonicBiLi(BigDecimal.valueOf(inuharmCount * 1.0 / data.size() * 100).setScale(2, RoundingMode.HALF_UP).doubleValue());
                overAreaLimitVO.setInHarmonicVMonitorList(new ArrayList<>(harmonicInHarmonciVMonitorList));

            }


            int freqOverDay = 0, voltageOverDay = 0, ubalanceOverDay = 0, flickerOverDay = 0, iNegOverDay = 0, uharmOverDay = 0, iharmOverDay = 0, inuharmOverDay = 0;

            int threeOverV = 0, fiveOverV = 0, sevenOverV = 0, elevenOverV = 0, otherOverV = 0;
            int threeOverI = 0, fiveOverI = 0, sevenOverI = 0, elevenOverI = 0, otherOverI = 0;


            Map<LocalDate, List<RStatLimitTargetDPO>> map = list.stream().collect(Collectors.groupingBy(RStatLimitTargetDPO::getTime));

            List<Double> freqOverOneDayList = new ArrayList<>(), voltageOverOneDayList = new ArrayList<>(), ubalanceOverOneDayList = new ArrayList<>(),
                    flickerOverOneDayList = new ArrayList<>(), iNegOverOneDayList = new ArrayList<>(), uharmOverOneDayList = new ArrayList<>(),
                    iharmOverOneDayList = new ArrayList<>(), inuharmOverOneDayList = new ArrayList<>();


            List<Double> threeOverOneDayList = new ArrayList<>(), fiveOverOneDayList = new ArrayList<>(), sevenOverOneDayList = new ArrayList<>(), elevenOverOneDayList = new ArrayList<>(), otherOverOneDayList = new ArrayList<>();
            List<Double> threeOverOneDayIList = new ArrayList<>(), fiveOverOneDayIList = new ArrayList<>(), sevenOverOneDayIList = new ArrayList<>(), elevenOverOneDayIList = new ArrayList<>(), otherOverOneDayIList = new ArrayList<>();


            for (LocalDate key : map.keySet()) {
                int freqOverOneDay = 0, voltageOverOneDay = 0, ubalanceOverOneDay = 0, flickerOverOneDay = 0, iNegOverOneDay = 0, uharmOverOneDay = 0, iharmOverOneDay = 0, inuharmOverOneDay = 0;

                int threeOneV = 0, fiveOneV = 0, sevenOneV = 0, elevenOneV = 0, otherOneV = 0;
                int threeOneI = 0, fiveOneI = 0, sevenOneI = 0, elevenOneI = 0, otherOneI = 0;


                List<RStatLimitTargetDPO> l = map.get(key);
                for (RStatLimitTargetDPO pojo : l) {
                    if (pojo.getFreqDevOvertime() > 0) {
                        freqOverDay++;
                        freqOverOneDay++;
                    }
                    if (pojo.getVoltageDevOvertime() > 0) {
                        voltageOverDay++;
                        voltageOverOneDay++;
                    }
                    if (pojo.getUbalanceOvertime() > 0) {
                        ubalanceOverDay++;
                        ubalanceOverOneDay++;
                    }
                    if (pojo.getFlickerOvertime() > 0) {
                        flickerOverDay++;
                        flickerOverOneDay++;
                    }
                    if (pojo.getINegOvertime() > 0) {
                        iNegOverDay++;
                        iNegOverOneDay++;
                    }
                    if (vHarmFlag(pojo)) {
                        uharmOverDay++;
                        uharmOverOneDay++;
                    }
                    if (iHarmFlag(pojo)) {
                        iharmOverDay++;
                        iharmOverOneDay++;
                    }
                    if (inHarmFlag(pojo)) {
                        inuharmOverDay++;
                        inuharmOverOneDay++;
                    }


                    //区域报告
                    if (Objects.nonNull(param.getAreaReportFlag()) && param.getAreaReportFlag() == 1) {
                        if (pojo.getUharm3Overtime() > 0) {
                            threeOverV++;
                            threeOneV++;
                        }
                        if (pojo.getUharm5Overtime() > 0) {
                            fiveOverV++;
                            fiveOneV++;
                        }
                        if (pojo.getUharm7Overtime() > 0) {
                            sevenOverV++;
                            sevenOneV++;
                        }
                        if (pojo.getUharm11Overtime() > 0) {
                            elevenOverV++;
                            elevenOneV++;
                        }

                        if (vOtherHarmFlag(pojo)) {
                            otherOverV++;
                            otherOneV++;
                        }



                        if (pojo.getIharm3Overtime() > 0) {
                            threeOverI++;
                            threeOneI++;
                        }
                        if (pojo.getIharm5Overtime() > 0) {
                            fiveOverI++;
                            fiveOneI++;
                        }
                        if (pojo.getIharm7Overtime() > 0) {
                            sevenOverI++;
                            sevenOneI++;
                        }
                        if (pojo.getIharm11Overtime() > 0) {
                            elevenOverI++;
                            elevenOneI++;
                        }
                        if (iOtherHarmFlag(pojo)) {
                            otherOverI++;
                            otherOneI++;
                        }
                    }

                }

                freqOverOneDayList.add((double) freqOverOneDay / data.size() * 100);
                voltageOverOneDayList.add((double) voltageOverOneDay / data.size() * 100);
                ubalanceOverOneDayList.add((double) ubalanceOverOneDay / data.size() * 100);
                flickerOverOneDayList.add((double) flickerOverOneDay / data.size() * 100);
                iNegOverOneDayList.add((double) iNegOverOneDay / data.size() * 100);
                uharmOverOneDayList.add((double) uharmOverOneDay / data.size() * 100);
                iharmOverOneDayList.add((double) iharmOverOneDay / data.size() * 100);
                inuharmOverOneDayList.add((double) inuharmOverOneDay / data.size() * 100);


                threeOverOneDayList.add((double) threeOneV / data.size() * 100);
                fiveOverOneDayList.add((double) fiveOneV / data.size() * 100);
                sevenOverOneDayList.add((double) sevenOneV / data.size() * 100);
                elevenOverOneDayList.add((double) elevenOneV / data.size() * 100);
                elevenOverOneDayList.add((double) elevenOneV / data.size() * 100);
                otherOverOneDayList.add((double) otherOneV / data.size() * 100);


                threeOverOneDayIList.add((double) threeOneI / data.size() * 100);
                fiveOverOneDayIList.add((double) fiveOneI / data.size() * 100);
                sevenOverOneDayIList.add((double) sevenOneI / data.size() * 100);
                elevenOverOneDayIList.add((double) elevenOneI / data.size() * 100);
                otherOverOneDayIList.add((double) otherOneI / data.size() * 100);

            }


            if (onlineCount != 0) {
                overAreaLimitVO.setFrequencyDayAvgBiLi(BigDecimal.valueOf(freqOverOneDayList.stream().mapToDouble(it -> it).average().orElse(0)).setScale(2, RoundingMode.HALF_UP).doubleValue());
                overAreaLimitVO.setVoltageDayAvgBiLi(BigDecimal.valueOf(voltageOverOneDayList.stream().mapToDouble(it -> it).average().orElse(0)).setScale(2, RoundingMode.HALF_UP).doubleValue());
                overAreaLimitVO.setThreePhaseVoltageDayAvgBiLi(BigDecimal.valueOf(ubalanceOverOneDayList.stream().mapToDouble(it -> it).average().orElse(0)).setScale(2, RoundingMode.HALF_UP).doubleValue());
                overAreaLimitVO.setFlickerDayAvgBiLi(BigDecimal.valueOf(flickerOverOneDayList.stream().mapToDouble(it -> it).average().orElse(0)).setScale(2, RoundingMode.HALF_UP).doubleValue());
                overAreaLimitVO.setNegativeDayAvgBiLi(BigDecimal.valueOf(iNegOverOneDayList.stream().mapToDouble(it -> it).average().orElse(0)).setScale(2, RoundingMode.HALF_UP).doubleValue());
                overAreaLimitVO.setHarmonicVoltageDayAvgBiLi(BigDecimal.valueOf(uharmOverOneDayList.stream().mapToDouble(it -> it).average().orElse(0)).setScale(2, RoundingMode.HALF_UP).doubleValue());
                overAreaLimitVO.setHarmonicCurrentDayAvgBiLi(BigDecimal.valueOf(iharmOverOneDayList.stream().mapToDouble(it -> it).average().orElse(0)).setScale(2, RoundingMode.HALF_UP).doubleValue());
                overAreaLimitVO.setInterHarmonicDayAvgBiLi(BigDecimal.valueOf(inuharmOverOneDayList.stream().mapToDouble(it -> it).average().orElse(0)).setScale(2, RoundingMode.HALF_UP).doubleValue());


                overAreaLimitVO.setFrequencyOverDayBiLi(freqCount == 0 ? 0.0 : BigDecimal.valueOf(freqOverDay * 1.0 / freqCount).setScale(2, RoundingMode.HALF_UP).doubleValue());
                overAreaLimitVO.setVoltageOverDayBiLi(voltageCount == 0 ? 0.0 : BigDecimal.valueOf(voltageOverDay * 1.0 / voltageCount).setScale(2, RoundingMode.HALF_UP).doubleValue());
                overAreaLimitVO.setThreePhaseVoltageOverDayBiLi(ubalanceCount == 0 ? 0.0 : BigDecimal.valueOf(ubalanceOverDay * 1.0 / ubalanceCount).setScale(2, RoundingMode.HALF_UP).doubleValue());
                overAreaLimitVO.setFlickerOverDayBiLi(flickerCount == 0 ? 0.0 : BigDecimal.valueOf(flickerOverDay * 1.0 / flickerCount).setScale(2, RoundingMode.HALF_UP).doubleValue());
                overAreaLimitVO.setNegativeOverDayBiLi(iNegCount == 0 ? 0.0 : BigDecimal.valueOf(iNegOverDay * 1.0 / iNegCount).setScale(2, RoundingMode.HALF_UP).doubleValue());
                overAreaLimitVO.setHarmonicVoltageOverDayBiLi(uharmCount == 0 ? 0.0 : BigDecimal.valueOf(uharmOverDay * 1.0 / uharmCount).setScale(2, RoundingMode.HALF_UP).doubleValue());
                overAreaLimitVO.setHarmonicCurrentOverDayBiLi(iharmCount == 0 ? 0.0 : BigDecimal.valueOf(iharmOverDay * 1.0 / iharmCount).setScale(2, RoundingMode.HALF_UP).doubleValue());
                overAreaLimitVO.setInterHarmonicOverDayBiLi(inuharmCount == 0 ? 0.0 : BigDecimal.valueOf(inuharmOverDay * 1.0 / inuharmCount).setScale(2, RoundingMode.HALF_UP).doubleValue());
            }

            //以下代码只有再区域报告时候会被调用
            if (Objects.nonNull(param.getAreaReportFlag()) && param.getAreaReportFlag() == 1) {
                List<OverAreaLimitVO.InnerHarmV> innerHarmVList = new ArrayList<>();
                innerHarmVList.add(assLimitVO("3",threeVList,threeV,data.size(),threeOverV,threeOverOneDayList));
                innerHarmVList.add(assLimitVO("5",fiveVList,fiveV,data.size(),fiveOverV,fiveOverOneDayList));
                innerHarmVList.add(assLimitVO("7",sevenVList,sevenV,data.size(),sevenOverV,sevenOverOneDayList));
                innerHarmVList.add(assLimitVO("11",elevenVList,elevenV,data.size(),elevenOverV,elevenOverOneDayList));
                innerHarmVList.add(assLimitVO("其他",otherVList,otherV,data.size(),otherOverV,otherOverOneDayList));
                overAreaLimitVO.setInnerHarmVList(innerHarmVList);



                List<OverAreaLimitVO.InnerHarmV> innerHarmIList = new ArrayList<>();
                innerHarmIList.add(assLimitVO("3",threeIList,threeI,data.size(),threeOverI,threeOverOneDayIList));
                innerHarmIList.add(assLimitVO("5",fiveIList,fiveI,data.size(),fiveOverI,fiveOverOneDayIList));
                innerHarmIList.add(assLimitVO("7",sevenIList,sevenI,data.size(),sevenOverI,sevenOverOneDayIList));
                innerHarmIList.add(assLimitVO("11",elevenIList,elevenI,data.size(),elevenOverI,elevenOverOneDayIList));
                innerHarmIList.add(assLimitVO("其他",otherIList,otherI,data.size(),otherOverI,otherOverOneDayIList));
                overAreaLimitVO.setInnerHarmIList(innerHarmIList);
            }

        }
        return overAreaLimitVO;
    }

    /**
     * 对应报告表格一行数据
     * @param name
     * @param lineIds
     * @param limitCount
     * @param onlineNum
     * @param everydayAllNum
     * @param dayList 时间范围 日超标占比集合
     * @return
     */
    private OverAreaLimitVO.InnerHarmV assLimitVO(String name,Set<String> lineIds,int limitCount,int onlineNum,int everydayAllNum,List<Double> dayList){
        OverAreaLimitVO.InnerHarmV inner = new OverAreaLimitVO.InnerHarmV();
        inner.setName(name);
        inner.setLimitIdList(new ArrayList<>(lineIds));
        inner.setLimitCount(limitCount);
        inner.setLimitRate(BigDecimal.valueOf(limitCount * 1.0 / onlineNum * 100).setScale(2, RoundingMode.HALF_UP).doubleValue());
        inner.setDayAvg(BigDecimal.valueOf(dayList.stream().mapToDouble(it -> it).average().orElse(0)).setScale(2, RoundingMode.HALF_UP).doubleValue());
        inner.setDayLimit(limitCount == 0 ? 0.0 : BigDecimal.valueOf(everydayAllNum * 1.0 / limitCount).setScale(2, RoundingMode.HALF_UP).doubleValue());
        return inner;
    }


    private boolean vHarmFlag(RStatLimitTargetDPO x) {
        int count = x.getUaberranceOvertime() +
                x.getUharm2Overtime() + x.getUharm3Overtime() + x.getUharm4Overtime() + x.getUharm5Overtime() +
                x.getUharm6Overtime() + x.getUharm7Overtime() + x.getUharm8Overtime() + x.getUharm9Overtime() +
                x.getUharm10Overtime() + x.getUharm11Overtime() + x.getUharm12Overtime() + x.getUharm13Overtime() + x.getUharm14Overtime() +
                x.getUharm15Overtime() + x.getUharm16Overtime() + x.getUharm17Overtime() + x.getUharm18Overtime() + x.getUharm19Overtime() +
                x.getUharm20Overtime() + x.getUharm21Overtime() + x.getUharm22Overtime() + x.getUharm23Overtime() + x.getUharm24Overtime() +
                x.getUharm25Overtime();
        return count > 0;
    }

    private boolean vOtherHarmFlag(RStatLimitTargetDPO x) {
        int count = x.getUharm2Overtime()  + x.getUharm4Overtime() +
                x.getUharm6Overtime() + x.getUharm8Overtime() + x.getUharm9Overtime() +
                x.getUharm10Overtime() + x.getUharm12Overtime() + x.getUharm13Overtime() + x.getUharm14Overtime() +
                x.getUharm15Overtime() + x.getUharm16Overtime() + x.getUharm17Overtime() + x.getUharm18Overtime() + x.getUharm19Overtime() +
                x.getUharm20Overtime() + x.getUharm21Overtime() + x.getUharm22Overtime() + x.getUharm23Overtime() + x.getUharm24Overtime() +
                x.getUharm25Overtime();
        return count > 0;
    }

    private boolean iHarmFlag(RStatLimitTargetDPO rStatLimitRateDPO) {
        int count = rStatLimitRateDPO.getIharm2Overtime() + rStatLimitRateDPO.getIharm3Overtime() + rStatLimitRateDPO.getIharm4Overtime() +
                rStatLimitRateDPO.getIharm5Overtime() + rStatLimitRateDPO.getIharm6Overtime() + rStatLimitRateDPO.getIharm7Overtime() +
                rStatLimitRateDPO.getIharm8Overtime() + rStatLimitRateDPO.getIharm9Overtime() + rStatLimitRateDPO.getIharm10Overtime() +
                rStatLimitRateDPO.getIharm11Overtime() + rStatLimitRateDPO.getIharm12Overtime() + rStatLimitRateDPO.getIharm13Overtime() +
                rStatLimitRateDPO.getIharm14Overtime() + rStatLimitRateDPO.getIharm15Overtime() + rStatLimitRateDPO.getIharm16Overtime() +
                rStatLimitRateDPO.getIharm17Overtime() + rStatLimitRateDPO.getIharm18Overtime() + rStatLimitRateDPO.getIharm19Overtime() +
                rStatLimitRateDPO.getIharm20Overtime() + rStatLimitRateDPO.getIharm21Overtime() + rStatLimitRateDPO.getIharm22Overtime() +
                rStatLimitRateDPO.getIharm23Overtime() + rStatLimitRateDPO.getIharm24Overtime() + rStatLimitRateDPO.getIharm25Overtime();
        return count > 0;
    }

    private boolean iOtherHarmFlag(RStatLimitTargetDPO rStatLimitRateDPO) {
        int count = rStatLimitRateDPO.getIharm2Overtime() + rStatLimitRateDPO.getIharm4Overtime()  + rStatLimitRateDPO.getIharm6Overtime() +
                rStatLimitRateDPO.getIharm8Overtime() + rStatLimitRateDPO.getIharm9Overtime() + rStatLimitRateDPO.getIharm10Overtime()  +
                rStatLimitRateDPO.getIharm12Overtime() + rStatLimitRateDPO.getIharm13Overtime() +
                rStatLimitRateDPO.getIharm14Overtime() + rStatLimitRateDPO.getIharm15Overtime() + rStatLimitRateDPO.getIharm16Overtime() +
                rStatLimitRateDPO.getIharm17Overtime() + rStatLimitRateDPO.getIharm18Overtime() + rStatLimitRateDPO.getIharm19Overtime() +
                rStatLimitRateDPO.getIharm20Overtime() + rStatLimitRateDPO.getIharm21Overtime() + rStatLimitRateDPO.getIharm22Overtime() +
                rStatLimitRateDPO.getIharm23Overtime() + rStatLimitRateDPO.getIharm24Overtime() + rStatLimitRateDPO.getIharm25Overtime();
        return count > 0;
    }

    private boolean inHarmVFlag(RStatLimitTargetDPO rStatLimitRateDPO) {
        int count = rStatLimitRateDPO.getInuharm1Overtime() + rStatLimitRateDPO.getInuharm2Overtime()  + rStatLimitRateDPO.getInuharm3Overtime() +
                rStatLimitRateDPO.getInuharm4Overtime() + rStatLimitRateDPO.getInuharm5Overtime() + rStatLimitRateDPO.getInuharm6Overtime()  +
                rStatLimitRateDPO.getInuharm7Overtime() + rStatLimitRateDPO.getInuharm8Overtime() +
                rStatLimitRateDPO.getInuharm9Overtime() + rStatLimitRateDPO.getInuharm10Overtime() + rStatLimitRateDPO.getInuharm11Overtime()+
                rStatLimitRateDPO.getInuharm12Overtime() + rStatLimitRateDPO.getInuharm13Overtime() + rStatLimitRateDPO.getInuharm14Overtime()+
                rStatLimitRateDPO.getInuharm15Overtime() + rStatLimitRateDPO.getInuharm16Overtime();
        return count > 0;
    }

    private boolean inHarmFlag(RStatLimitTargetDPO t) {
        int count = t.getInuharm1Overtime() + t.getInuharm2Overtime() + t.getInuharm3Overtime() + t.getInuharm4Overtime() + t.getInuharm5Overtime() + t.getInuharm6Overtime() + t.getInuharm7Overtime() + t.getInuharm8Overtime() + t.getInuharm9Overtime() + t.getInuharm10Overtime() + t.getInuharm11Overtime() + t.getInuharm12Overtime() + t.getInuharm13Overtime() + t.getInuharm14Overtime() + t.getInuharm15Overtime() + t.getInuharm16Overtime();
        return count > 0;
    }


    /**
     * 功能描述: 处理区域在线监测点数、超标监测点数
     *
     * @param list 集合
     * @return
     * @author xy
     * @date 2022/2/25 15:05
     */
    private List<?> getAllData(List<String> list, String startTime, String endTime, String databaseName) {
        List<?> result = new ArrayList<>();
        switch (databaseName) {
            case Param.LIMIT_TARGET:
                result = targetMapper.getSumTargetList(list, startTime, endTime);
                break;
            case Param.LIMIT_RATE:
                result = rateDMapper.getSumPassRateList(list, startTime, endTime);
                break;
            default:
                break;
        }
        return result;
    }

    public List<GeneralVO> getEventDetail(List<String> list, String startTime, String endTime, List<DictData> eventStatis) {
        Map<String, String> dictMap = eventStatis.stream().collect(Collectors.toMap(DictData::getCode, DictData::getId));
        HashMap<String, Object> condMap = new HashMap<>();
        condMap.put("startTime", DateUtil.beginOfDay(DateUtil.parse(startTime)).toString());
        condMap.put("endTime", DateUtil.endOfDay(DateUtil.parse(endTime)).toString());
        condMap.put("ids", list);
        condMap.put(DicDataEnum.VOLTAGE_DIP.getCode(), dictMap.get(DicDataEnum.VOLTAGE_DIP.getCode())); //电压暂降
        condMap.put(DicDataEnum.SHORT_INTERRUPTIONS.getCode(), dictMap.get(DicDataEnum.SHORT_INTERRUPTIONS.getCode())); //短时中断
        condMap.put(DicDataEnum.VOLTAGE_RISE.getCode(), dictMap.get(DicDataEnum.VOLTAGE_RISE.getCode())); //电压暂升
        return eventDetailFeignClient.getEventDetailCount(condMap).getData();
    }
}
