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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.njcn.device.pms.api.DistributionMonitorClient;
import com.njcn.device.pms.api.MonitorClient;
import com.njcn.device.pms.pojo.po.DistributionMonitor;
import com.njcn.device.pms.pojo.po.Monitor;
import com.njcn.harmonic.pojo.po.RMpPartHarmonicDetailD;
import com.njcn.harmonic.pojo.po.RMpPartHarmonicDetailM;
import com.njcn.harmonic.pojo.po.RMpSurplusHarmonicDetailD;
import com.njcn.harmonic.pojo.po.RMpSurplusHarmonicDetailM;
import com.njcn.prepare.harmonic.constant.AlgorithmParam;
import com.njcn.prepare.harmonic.mapper.mysql.line.RMpPartHarmonicDetailDMapper;
import com.njcn.prepare.harmonic.mapper.mysql.line.RMpPartHarmonicDetailMMapper;
import com.njcn.prepare.harmonic.mapper.mysql.line.RMpSurplusHarmonicDetailDMapper;
import com.njcn.prepare.harmonic.mapper.mysql.line.RMpSurplusHarmonicDetailMMapper;
import com.njcn.prepare.harmonic.pojo.oracle.LimitRateOracle;
import com.njcn.prepare.harmonic.pojo.oracle.PqLinedetailPO;
import com.njcn.prepare.harmonic.pojo.param.LineParam;
import com.njcn.prepare.harmonic.service.mysql.line.SteadyIndexExceedService;
import com.njcn.prepare.harmonic.service.oracle.LimitRateOracleService;
import com.njcn.prepare.harmonic.service.oracle.PqLinedetailPOService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.List;
import java.util.Objects;

/**
 * 数据完整率算法
 *
 * @author qijian
 * @version 1.0.0
 * @createTime 2022/12/02 - 10:09
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class SteadyIndexExceedServiceImpl implements SteadyIndexExceedService {

    private final DistributionMonitorClient distributionMonitorClient;

    private final MonitorClient monitorClient;

    private final PqLinedetailPOService pqLinedetailPOService;

    private final LimitRateOracleService limitRateOracleService;

    private final RMpPartHarmonicDetailDMapper rMpPartHarmonicDetailDMapper;

    private final RMpSurplusHarmonicDetailDMapper rMpSurplusHarmonicDetailDMapper;

    private final RMpPartHarmonicDetailMMapper rMpPartHarmonicDetailMMapper;

    private final RMpSurplusHarmonicDetailMMapper rMpSurplusHarmonicDetailMMapper;

    /**
     * 日统计I、II、III类监测点是否超标限值
     * @author qijian
     * @date 2022/12/02
     */
    @Override
    @Async("asyncExecutor")
    public boolean isValueExceedID(LineParam lineParam) {
        if (Objects.equals(lineParam.getDataSource(), AlgorithmParam.ORACLE_DATA)) {
            hanlderOracleDataI(lineParam);
        } else if (Objects.equals(lineParam.getDataSource(), AlgorithmParam.INFLUX_DATA)) {
            hanlderInfluxDataI(lineParam);
        }
        return true;

    }

    /**
     * 日统计I、II类监测点是否超标限值
     * @author qijian
     * @date 2022/12/02
     */
    @Override
    @Async("asyncExecutor")
    public boolean isValueExceedIID(LineParam lineParam) {
        if (Objects.equals(lineParam.getDataSource(), AlgorithmParam.ORACLE_DATA)) {
            hanlderOracleDataII(lineParam);
        } else if (Objects.equals(lineParam.getDataSource(), AlgorithmParam.INFLUX_DATA)) {
            hanlderInfluxDataII(lineParam);
        }
        return true;

    }

    /**
     * 月统计I、II、III类监测点是否超标限值
     * @author qijian
     * @date 2022/12/12
     */
    @Override
    @Async("asyncExecutor")
    public boolean isValueExceedIM(LineParam lineParam) {
        //初始化
        String date = lineParam.getDataDate();
        List<String> lineIds = lineParam.getLineIds();

        //获取监测点数据，进行入库操作
        if (CollUtil.isEmpty(lineIds)){
            //先获取主网的监测点信息，再获取配网的监测点信息
            List<Monitor> monitorList = monitorClient.getMonitorList(lineIds).getData();
            for (Monitor monitor : monitorList) {
                //处理数据
                processDataIM(date,monitor.getLineId());
            }
            List<DistributionMonitor> distributionMonitorList = distributionMonitorClient.distributionMonitorList(lineIds).getData();
            for (DistributionMonitor distributionMonitor : distributionMonitorList) {
                //处理数据
                processDataIM(date,distributionMonitor.getMonitorId());
            }
        } else {
            //根据监测点类型取数据
            for (String lineId : lineIds) {
                //处理数据
                processDataIM(date,lineId);
            }
        }

        return true;
    }


    /**
     * 月统计I、II类监测点是否超标限值
     * @author qijian
     * @date 2022/12/12
     */
    @Override
    @Async("asyncExecutor")
    public boolean isValueExceedIIM(LineParam lineParam) {
        //初始化
        String date = lineParam.getDataDate();
        List<String> lineIds = lineParam.getLineIds();

        //获取监测点数据，进行入库操作
        if (CollUtil.isEmpty(lineIds)){
            //先获取主网的监测点信息，再获取配网的监测点信息
            List<Monitor> monitorList = monitorClient.getMonitorList(lineIds).getData();
            for (Monitor monitor : monitorList) {
                //处理数据
                processDataIIM(date,monitor.getLineId());
            }
            List<DistributionMonitor> distributionMonitorList = distributionMonitorClient.distributionMonitorList(lineIds).getData();
            for (DistributionMonitor distributionMonitor : distributionMonitorList) {
                //处理数据
                processDataIIM(date,distributionMonitor.getMonitorId());
            }
        } else {
            //根据监测点类型取数据
            for (String lineId : lineIds) {
                //处理数据
                processDataIIM(date,lineId);
            }
        }

        return true;
    }

    /**
     * 处理数据（oracle>mysql）
     * @author qijian
     * @date 2022/12/02
     */
    private void hanlderOracleDataI(LineParam lineParam) {
        //初始化
//        Date date = DateUtil.parse(lineParam.getDataDate());
        String date = lineParam.getDataDate();
        List<String> lineIds = lineParam.getLineIds();
        Integer lineType = lineParam.getLineType();
        Integer type = lineParam.getType();

        //获取监测点数据，进行入库操作
        if (CollUtil.isEmpty(lineIds)){
            //先获取主网的监测点信息，再获取配网的监测点信息
            List<Monitor> monitorList = monitorClient.getMonitorList(lineIds).getData();
            for (Monitor monitor : monitorList) {
                //根据mysql监测点取oracle监测点信息
                PqLinedetailPO pqLinedetailPO = pqLinedetailPOService.getOracleIndexIdByMonitorId(monitor.getLineId());
                //处理数据（oracle>mysql）
                processOracleDataI(date,type, monitor.getLineId(),pqLinedetailPO == null ? 0 : pqLinedetailPO.getLineIndex());
            }
            List<DistributionMonitor> distributionMonitorList = distributionMonitorClient.distributionMonitorList(lineIds).getData();
            for (DistributionMonitor distributionMonitor : distributionMonitorList) {
                //根据mysql监测点取oracle监测点信息
                PqLinedetailPO pqLinedetailPO = pqLinedetailPOService.getOracleIndexIdByMonitorId(distributionMonitor.getMonitorId());
                //处理数据（oracle>mysql）
                processOracleDataI(date,type, distributionMonitor.getMonitorId(),pqLinedetailPO == null ? 0 : pqLinedetailPO.getLineIndex());
            }
        } else {
            //根据监测点类型取数据
            for (String lineId : lineIds) {
                //根据mysql监测点取oracle监测点信息
                PqLinedetailPO pqLinedetailPO = pqLinedetailPOService.getOracleIndexIdByMonitorId(lineId);
                //处理数据（oracle>mysql）
                processOracleDataI(date,type, lineId, pqLinedetailPO == null ? 0 : pqLinedetailPO.getLineIndex());
            }
        }
    }

    /**
     * 处理数据（oracle>mysql）
     * @author qijian
     * @date 2022/12/05
     */
    private void processOracleDataI(String date, Integer type, String lineId, int lineIndex) {
        RMpPartHarmonicDetailD rMpPartHarmonicDetailD = new RMpPartHarmonicDetailD();
        rMpPartHarmonicDetailD.setMeasurementPointId(lineId);
        rMpPartHarmonicDetailD.setDataDate(LocalDate.parse(date));
        rMpPartHarmonicDetailD.setIsVDevLow(0);
        rMpPartHarmonicDetailD.setIsVDevUp(0);

        //1、根据limit_rate表获取数据
        LimitRateOracle limitRateOracle = limitRateOracleService.getOneByData(date, lineIndex);
        if (limitRateOracle == null){
            return;
        }

        //2、填入数据（大于0则为超标）
        //频率偏差越限次数
        rMpPartHarmonicDetailD.setIsFreq(limitRateOracle.getFreqDevOvertime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setFreqTimes(limitRateOracle.getFreqDevOvertime());
        //电压偏差越限次数
        rMpPartHarmonicDetailD.setIsVDev(limitRateOracle.getVoltageDevOvertime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setVDevTimes(limitRateOracle.getVoltageDevOvertime());
        //电压不平衡度越限次数
        rMpPartHarmonicDetailD.setIsUnbalance(limitRateOracle.getUbalanceOvertime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setUnbalanceTimes(limitRateOracle.getUbalanceOvertime());
        //电压谐波畸变率越限次数
        rMpPartHarmonicDetailD.setIsVThd(limitRateOracle.getUaberranceOvertime() > 0 ? 1 : 0);
        //电压谐波含有率越限总次数
        rMpPartHarmonicDetailD.setIsV(CalculateSumV(limitRateOracle) > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setVTimes(CalculateSumV(limitRateOracle));
        //电流谐波含有率越限总次数
        rMpPartHarmonicDetailD.setIsI(CalculateSumI(limitRateOracle) > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setITimes(CalculateSumI(limitRateOracle));
        //负序电流超标次数（建表挖的坑：null值特殊处理）
        Integer iNegOvertime = limitRateOracle.getINegOvertime() == null ? 0 : limitRateOracle.getINegOvertime();
        rMpPartHarmonicDetailD.setIsINeg(iNegOvertime > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setINegTimes(iNegOvertime);
        //稳态指标是否超标
        if (limitRateOracle.getFreqDevOvertime() > 0 || limitRateOracle.getVoltageDevOvertime() > 0 || limitRateOracle.getUbalanceOvertime() > 0 || limitRateOracle.getUaberranceOvertime() > 0 || iNegOvertime > 0 || CalculateSumV(limitRateOracle) > 0 || CalculateSumI(limitRateOracle) > 0 ){
            rMpPartHarmonicDetailD.setIsHarmonic(1);
        }else{
            rMpPartHarmonicDetailD.setIsHarmonic(0);
        }

        //电压谐波含有率越限次数
        rMpPartHarmonicDetailD.setIsV2(limitRateOracle.getUharm2Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsV3(limitRateOracle.getUharm3Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsV4(limitRateOracle.getUharm4Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsV5(limitRateOracle.getUharm5Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsV6(limitRateOracle.getUharm6Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsV7(limitRateOracle.getUharm7Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsV8(limitRateOracle.getUharm8Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsV9(limitRateOracle.getUharm9Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsV10(limitRateOracle.getUharm10Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsV11(limitRateOracle.getUharm11Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsV12(limitRateOracle.getUharm12Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsV13(limitRateOracle.getUharm13Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsV14(limitRateOracle.getUharm14Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsV15(limitRateOracle.getUharm15Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsV16(limitRateOracle.getUharm16Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsV17(limitRateOracle.getUharm17Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsV18(limitRateOracle.getUharm18Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsV19(limitRateOracle.getUharm19Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsV20(limitRateOracle.getUharm20Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsV21(limitRateOracle.getUharm21Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsV22(limitRateOracle.getUharm22Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsV23(limitRateOracle.getUharm23Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsV24(limitRateOracle.getUharm24Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsV25(limitRateOracle.getUharm25Overtime() > 0 ? 1 : 0);
        //电流谐波含有率越限次数
        rMpPartHarmonicDetailD.setIsI2(limitRateOracle.getIharm2Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsI3(limitRateOracle.getIharm3Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsI4(limitRateOracle.getIharm4Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsI5(limitRateOracle.getIharm5Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsI6(limitRateOracle.getIharm6Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsI7(limitRateOracle.getIharm7Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsI8(limitRateOracle.getIharm8Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsI9(limitRateOracle.getIharm9Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsI10(limitRateOracle.getIharm10Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsI11(limitRateOracle.getIharm11Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsI12(limitRateOracle.getIharm12Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsI13(limitRateOracle.getIharm13Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsI14(limitRateOracle.getIharm14Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsI15(limitRateOracle.getIharm15Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsI16(limitRateOracle.getIharm16Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsI17(limitRateOracle.getIharm17Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsI18(limitRateOracle.getIharm18Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsI19(limitRateOracle.getIharm19Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsI20(limitRateOracle.getIharm20Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsI21(limitRateOracle.getIharm21Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsI22(limitRateOracle.getIharm22Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsI23(limitRateOracle.getIharm23Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsI24(limitRateOracle.getIharm24Overtime() > 0 ? 1 : 0);
        rMpPartHarmonicDetailD.setIsI25(limitRateOracle.getIharm25Overtime() > 0 ? 1 : 0);

        //3、存库
        addDataI(rMpPartHarmonicDetailD);
    }

    /**
     * 处理数据（influxDB>mysql）
     * @author qijian
     * @date 2022/12/02
     */
    private void hanlderInfluxDataI(LineParam lineParam) {

    }

    /**
     * 处理数据（oracle>mysql）
     * @author qijian
     * @date 2022/12/02
     */
    private void hanlderOracleDataII(LineParam lineParam) {
        //初始化
//        Date date = DateUtil.parse(lineParam.getDataDate());
        String date = lineParam.getDataDate();
        List<String> lineIds = lineParam.getLineIds();
        Integer lineType = lineParam.getLineType();
        Integer type = lineParam.getType();

        //获取监测点数据，进行入库操作
        if (CollUtil.isEmpty(lineIds)){
            //先获取主网的监测点信息，再获取配网的监测点信息
            List<Monitor> monitorList = monitorClient.getMonitorList(lineIds).getData();
            for (Monitor monitor : monitorList) {
                //根据mysql监测点取oracle监测点信息
                PqLinedetailPO pqLinedetailPO = pqLinedetailPOService.getOracleIndexIdByMonitorId(monitor.getLineId());
                //处理数据（oracle>mysql）
                processOracleDataII(date,type, monitor.getLineId(),pqLinedetailPO == null ? 0 : pqLinedetailPO.getLineIndex());
            }
            List<DistributionMonitor> distributionMonitorList = distributionMonitorClient.distributionMonitorList(lineIds).getData();
            for (DistributionMonitor distributionMonitor : distributionMonitorList) {
                //根据mysql监测点取oracle监测点信息
                PqLinedetailPO pqLinedetailPO = pqLinedetailPOService.getOracleIndexIdByMonitorId(distributionMonitor.getMonitorId());
                //处理数据（oracle>mysql）
                processOracleDataII(date,type, distributionMonitor.getMonitorId(),pqLinedetailPO == null ? 0 : pqLinedetailPO.getLineIndex());
            }
        } else {
            //根据监测点类型取数据
            for (String lineId : lineIds) {
                //根据mysql监测点取oracle监测点信息
                PqLinedetailPO pqLinedetailPO = pqLinedetailPOService.getOracleIndexIdByMonitorId(lineId);
                //处理数据（oracle>mysql）
                processOracleDataII(date,type, lineId, pqLinedetailPO == null ? 0 : pqLinedetailPO.getLineIndex());
            }
        }
    }

    /**
     * 处理数据（oracle>mysql）
     * @author qijian
     * @date 2022/12/08
     */
    private void processOracleDataII(String date, Integer type, String lineId, int lineIndex) {
        RMpSurplusHarmonicDetailD rMpSurplusHarmonicDetailD = new RMpSurplusHarmonicDetailD();
        rMpSurplusHarmonicDetailD.setMeasurementPointId(lineId);
        rMpSurplusHarmonicDetailD.setDataDate(LocalDate.parse(date));

        //1、根据limit_rate表获取数据
        LimitRateOracle limitRateOracle = limitRateOracleService.getOneByData(date, lineIndex);
        if (limitRateOracle == null){
            return;
        }

        //2、填入数据（大于0则为超标）
        //闪变发生次数（建表挖的坑：null值特殊处理）
        Integer flickerOvertime = limitRateOracle.getFlickerOvertime() == null ? 0 : limitRateOracle.getFlickerOvertime();
        rMpSurplusHarmonicDetailD.setIsFlicker(flickerOvertime > 0 ? 1 : 0);
        rMpSurplusHarmonicDetailD.setFlickerTimes(flickerOvertime);
        //谐波电流含有率越限总次数（建表挖的坑：null值特殊处理）
        rMpSurplusHarmonicDetailD.setIsInuharm(CalculateSumInuharm(limitRateOracle) > 0 ? 1 : 0);
        rMpSurplusHarmonicDetailD.setInuharmTimes(CalculateSumInuharm(limitRateOracle));

        //谐波电流含有率越限次数（建表挖的坑：null值特殊处理）
        Integer harm1Overtime = limitRateOracle.getInuharm1Overtime() == null ? 0 : limitRateOracle.getInuharm1Overtime();
        rMpSurplusHarmonicDetailD.setIsInuharm1(harm1Overtime > 0 ? 1 : 0);
        Integer harm2Overtime = limitRateOracle.getInuharm2Overtime() == null ? 0 : limitRateOracle.getInuharm2Overtime();
        rMpSurplusHarmonicDetailD.setIsInuharm2(harm2Overtime > 0 ? 1 : 0);
        Integer harm3Overtime = limitRateOracle.getInuharm3Overtime() == null ? 0 : limitRateOracle.getInuharm3Overtime();
        rMpSurplusHarmonicDetailD.setIsInuharm3(harm3Overtime > 0 ? 1 : 0);
        Integer harm4Overtime = limitRateOracle.getInuharm4Overtime() == null ? 0 : limitRateOracle.getInuharm4Overtime();
        rMpSurplusHarmonicDetailD.setIsInuharm4(harm4Overtime > 0 ? 1 : 0);
        Integer harm5Overtime = limitRateOracle.getInuharm5Overtime() == null ? 0 : limitRateOracle.getInuharm5Overtime();
        rMpSurplusHarmonicDetailD.setIsInuharm5(harm5Overtime > 0 ? 1 : 0);
        Integer harm6Overtime = limitRateOracle.getInuharm6Overtime() == null ? 0 : limitRateOracle.getInuharm6Overtime();
        rMpSurplusHarmonicDetailD.setIsInuharm6(harm6Overtime > 0 ? 1 : 0);
        Integer harm7Overtime = limitRateOracle.getInuharm7Overtime() == null ? 0 : limitRateOracle.getInuharm7Overtime();
        rMpSurplusHarmonicDetailD.setIsInuharm7(harm7Overtime > 0 ? 1 : 0);
        Integer harm8Overtime = limitRateOracle.getInuharm8Overtime() == null ? 0 : limitRateOracle.getInuharm8Overtime();
        rMpSurplusHarmonicDetailD.setIsInuharm8(harm8Overtime > 0 ? 1 : 0);
        Integer harm9Overtime = limitRateOracle.getInuharm9Overtime() == null ? 0 : limitRateOracle.getInuharm9Overtime();
        rMpSurplusHarmonicDetailD.setIsInuharm9(harm9Overtime > 0 ? 1 : 0);
        Integer harm10Overtime = limitRateOracle.getInuharm10Overtime() == null ? 0 : limitRateOracle.getInuharm10Overtime();
        rMpSurplusHarmonicDetailD.setIsInuharm10(harm10Overtime > 0 ? 1 : 0);
        Integer harm11Overtime = limitRateOracle.getInuharm11Overtime() == null ? 0 : limitRateOracle.getInuharm11Overtime();
        rMpSurplusHarmonicDetailD.setIsInuharm11(harm11Overtime > 0 ? 1 : 0);
        Integer harm12Overtime = limitRateOracle.getInuharm12Overtime() == null ? 0 : limitRateOracle.getInuharm12Overtime();
        rMpSurplusHarmonicDetailD.setIsInuharm12(harm12Overtime > 0 ? 1 : 0);
        Integer harm13Overtime = limitRateOracle.getInuharm13Overtime() == null ? 0 : limitRateOracle.getInuharm13Overtime();
        rMpSurplusHarmonicDetailD.setIsInuharm13(harm13Overtime > 0 ? 1 : 0);
        Integer harm14Overtime = limitRateOracle.getInuharm14Overtime() == null ? 0 : limitRateOracle.getInuharm14Overtime();
        rMpSurplusHarmonicDetailD.setIsInuharm14(harm14Overtime > 0 ? 1 : 0);
        Integer harm15Overtime = limitRateOracle.getInuharm15Overtime() == null ? 0 : limitRateOracle.getInuharm15Overtime();
        rMpSurplusHarmonicDetailD.setIsInuharm15(harm15Overtime > 0 ? 1 : 0);
        Integer harm16Overtime = limitRateOracle.getInuharm16Overtime() == null ? 0 : limitRateOracle.getInuharm16Overtime();
        rMpSurplusHarmonicDetailD.setIsInuharm16(harm16Overtime > 0 ? 1 : 0);

        //3、存库
        addDataII(rMpSurplusHarmonicDetailD);
    }


    /**
     * 处理数据（influxDB>mysql）
     * @author qijian
     * @date 2022/12/02
     */
    private void hanlderInfluxDataII(LineParam lineParam) {

    }

    /**
     * 处理数据
     * @author qijian
     * @date 2022/12/12
     */
    private void processDataIM(String date, String lineId) {
        LocalDate parseDate = LocalDate.parse(date);
        //获取当前月的第一天
        LocalDate firstDay = parseDate.with(TemporalAdjusters.firstDayOfMonth());
        //获取当前月的最后一天
        LocalDate lastDay = parseDate.with(TemporalAdjusters.lastDayOfMonth());
        //Day表取累计数据，塞入Month表
        RMpPartHarmonicDetailM sumData = rMpPartHarmonicDetailDMapper.getPartSumByData(lineId, firstDay, lastDay);
        if (sumData == null){
            return;
        }

        //存库
        sumData.setDataDate(firstDay);
        addDataIM(sumData);
    }

    /**
     * 处理数据
     * @author qijian
     * @date 2022/12/12
     */
    private void processDataIIM(String date, String lineId) {
        LocalDate parseDate = LocalDate.parse(date);
        //获取当前月的第一天
        LocalDate firstDay = parseDate.with(TemporalAdjusters.firstDayOfMonth());
        //获取当前月的最后一天
        LocalDate lastDay = parseDate.with(TemporalAdjusters.lastDayOfMonth());
        //Day表取累计数据，塞入Month表
        RMpSurplusHarmonicDetailM sumData = rMpSurplusHarmonicDetailDMapper.getSurplusSumByData(lineId, firstDay, lastDay);
        if (sumData == null){
            return;
        }

        //存库
        sumData.setDataDate(firstDay);
        addDataIIM(sumData);
    }

    /**
     * 计算求和（谐波电流含有率越限次数）
     * @author qijian
     * @date 2022/12/08
     */
    private Integer CalculateSumInuharm(LimitRateOracle limitRateOracle) {
        return (limitRateOracle.getInuharm1Overtime() == null ? 0 : limitRateOracle.getInuharm1Overtime())
                + (limitRateOracle.getInuharm2Overtime() == null ? 0 : limitRateOracle.getInuharm2Overtime())
                + (limitRateOracle.getInuharm3Overtime() == null ? 0 : limitRateOracle.getInuharm3Overtime())
                + (limitRateOracle.getInuharm4Overtime() == null ? 0 : limitRateOracle.getInuharm4Overtime())
                + (limitRateOracle.getInuharm5Overtime() == null ? 0 : limitRateOracle.getInuharm5Overtime())
                + (limitRateOracle.getInuharm6Overtime() == null ? 0 : limitRateOracle.getInuharm6Overtime())
                + (limitRateOracle.getInuharm7Overtime() == null ? 0 : limitRateOracle.getInuharm7Overtime())
                + (limitRateOracle.getInuharm8Overtime() == null ? 0 : limitRateOracle.getInuharm8Overtime())
                + (limitRateOracle.getInuharm9Overtime() == null ? 0 : limitRateOracle.getInuharm9Overtime())
                + (limitRateOracle.getInuharm10Overtime() == null ? 0 : limitRateOracle.getInuharm10Overtime())
                + (limitRateOracle.getInuharm11Overtime() == null ? 0 : limitRateOracle.getInuharm11Overtime())
                + (limitRateOracle.getInuharm12Overtime() == null ? 0 : limitRateOracle.getInuharm12Overtime())
                + (limitRateOracle.getInuharm13Overtime() == null ? 0 : limitRateOracle.getInuharm13Overtime())
                + (limitRateOracle.getInuharm14Overtime() == null ? 0 : limitRateOracle.getInuharm14Overtime())
                + (limitRateOracle.getInuharm15Overtime() == null ? 0 : limitRateOracle.getInuharm15Overtime())
                + (limitRateOracle.getInuharm16Overtime() == null ? 0 : limitRateOracle.getInuharm16Overtime());
    }

    /**
     * 计算求和（电压谐波含有率越限次数）
     * @author qijian
     * @date 2022/12/05
     */
    private Integer CalculateSumV(LimitRateOracle limitRateOracle) {
        return limitRateOracle.getUharm2Overtime()
                + limitRateOracle.getUharm3Overtime()
                + limitRateOracle.getUharm4Overtime()
                + limitRateOracle.getUharm5Overtime()
                + limitRateOracle.getUharm6Overtime()
                + limitRateOracle.getUharm7Overtime()
                + limitRateOracle.getUharm8Overtime()
                + limitRateOracle.getUharm9Overtime()
                + limitRateOracle.getUharm10Overtime()
                + limitRateOracle.getUharm11Overtime()
                + limitRateOracle.getUharm12Overtime()
                + limitRateOracle.getUharm13Overtime()
                + limitRateOracle.getUharm14Overtime()
                + limitRateOracle.getUharm15Overtime()
                + limitRateOracle.getUharm16Overtime()
                + limitRateOracle.getUharm17Overtime()
                + limitRateOracle.getUharm18Overtime()
                + limitRateOracle.getUharm19Overtime()
                + limitRateOracle.getUharm20Overtime()
                + limitRateOracle.getUharm21Overtime()
                + limitRateOracle.getUharm22Overtime()
                + limitRateOracle.getUharm23Overtime()
                + limitRateOracle.getUharm24Overtime()
                + limitRateOracle.getUharm25Overtime();
    }

    /**
     * 计算求和（电流谐波含有率越限次数）
     * @author qijian
     * @date 2022/12/05
     */
    private Integer CalculateSumI(LimitRateOracle limitRateOracle) {
        return limitRateOracle.getIharm2Overtime()
                + limitRateOracle.getIharm3Overtime()
                + limitRateOracle.getIharm4Overtime()
                + limitRateOracle.getIharm5Overtime()
                + limitRateOracle.getIharm6Overtime()
                + limitRateOracle.getIharm7Overtime()
                + limitRateOracle.getIharm8Overtime()
                + limitRateOracle.getIharm9Overtime()
                + limitRateOracle.getIharm10Overtime()
                + limitRateOracle.getIharm11Overtime()
                + limitRateOracle.getIharm12Overtime()
                + limitRateOracle.getIharm13Overtime()
                + limitRateOracle.getIharm14Overtime()
                + limitRateOracle.getIharm15Overtime()
                + limitRateOracle.getIharm16Overtime()
                + limitRateOracle.getIharm17Overtime()
                + limitRateOracle.getIharm18Overtime()
                + limitRateOracle.getIharm19Overtime()
                + limitRateOracle.getIharm20Overtime()
                + limitRateOracle.getIharm21Overtime()
                + limitRateOracle.getIharm22Overtime()
                + limitRateOracle.getIharm23Overtime()
                + limitRateOracle.getIharm24Overtime()
                + limitRateOracle.getIharm25Overtime();
    }

    /**
     * 存入数据库
     * @author qijian
     * @date 2022/12/05
     */
    private void addDataI(RMpPartHarmonicDetailD rMpPartHarmonicDetailD) {
        LambdaQueryWrapper<RMpPartHarmonicDetailD> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(RMpPartHarmonicDetailD::getMeasurementPointId, rMpPartHarmonicDetailD.getMeasurementPointId())
                .eq(RMpPartHarmonicDetailD::getDataDate, rMpPartHarmonicDetailD.getDataDate());
        RMpPartHarmonicDetailD rMpPartHarmonicDetailDOne = rMpPartHarmonicDetailDMapper.selectOne(lambdaQueryWrapper);
        if (Objects.nonNull(rMpPartHarmonicDetailDOne)) {
            rMpPartHarmonicDetailDMapper.update(rMpPartHarmonicDetailD, lambdaQueryWrapper);
        } else {
            rMpPartHarmonicDetailDMapper.insert(rMpPartHarmonicDetailD);
        }
    }

    /**
     * 存入数据库
     * @author qijian
     * @date 2022/12/05
     */
    private void addDataII(RMpSurplusHarmonicDetailD rMpSurplusHarmonicDetailD) {
        LambdaQueryWrapper<RMpSurplusHarmonicDetailD> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(RMpSurplusHarmonicDetailD::getMeasurementPointId, rMpSurplusHarmonicDetailD.getMeasurementPointId())
                .eq(RMpSurplusHarmonicDetailD::getDataDate, rMpSurplusHarmonicDetailD.getDataDate());
        RMpSurplusHarmonicDetailD rMpSurplusHarmonicDetailDOne = rMpSurplusHarmonicDetailDMapper.selectOne(lambdaQueryWrapper);
        if (Objects.nonNull(rMpSurplusHarmonicDetailDOne)) {
            rMpSurplusHarmonicDetailDMapper.update(rMpSurplusHarmonicDetailD, lambdaQueryWrapper);
        } else {
            rMpSurplusHarmonicDetailDMapper.insert(rMpSurplusHarmonicDetailD);
        }
    }

    /**
     * 存入数据库
     * @author qijian
     * @date 2022/12/12
     */
    private void addDataIM(RMpPartHarmonicDetailM sumData) {
        LambdaQueryWrapper<RMpPartHarmonicDetailM> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(RMpPartHarmonicDetailM::getMeasurementPointId, sumData.getMeasurementPointId())
                .eq(RMpPartHarmonicDetailM::getDataDate, sumData.getDataDate());
        RMpPartHarmonicDetailM rMpPartHarmonicDetailMOne = rMpPartHarmonicDetailMMapper.selectOne(lambdaQueryWrapper);
        if (Objects.nonNull(rMpPartHarmonicDetailMOne)) {
            rMpPartHarmonicDetailMMapper.update(sumData, lambdaQueryWrapper);
        } else {
            rMpPartHarmonicDetailMMapper.insert(sumData);
        }
    }

    /**
     * 存入数据库
     * @author qijian
     * @date 2022/12/12
     */
    private void addDataIIM(RMpSurplusHarmonicDetailM sumData) {
        LambdaQueryWrapper<RMpSurplusHarmonicDetailM> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(RMpSurplusHarmonicDetailM::getMeasurementPointId, sumData.getMeasurementPointId())
                .eq(RMpSurplusHarmonicDetailM::getDataDate, sumData.getDataDate());
        RMpSurplusHarmonicDetailM rMpSurplusHarmonicDetailMOne = rMpSurplusHarmonicDetailMMapper.selectOne(lambdaQueryWrapper);
        if (Objects.nonNull(rMpSurplusHarmonicDetailMOne)) {
            rMpSurplusHarmonicDetailMMapper.update(sumData, lambdaQueryWrapper);
        } else {
            rMpSurplusHarmonicDetailMMapper.insert(sumData);
        }
    }

}
