package com.njcn.mysql.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.njcn.mysql.bo.enums.TargetEnum;
import com.njcn.mysql.bo.po.*;
import com.njcn.mysql.service.*;
import com.njcn.mysql.util.MaxValueProcessor;
import com.njcn.mysql.util.PubUtils;
import com.njcn.oracle.bo.po.*;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.ZoneId;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author xy
 */
@Service
@AllArgsConstructor
public class OracleToMysqlServiceImpl implements OracleToMysqlService {

    private final OracleDataService oracleDataService;
    private final IPqLineBakService pqLineBakService;
    private final IRStatDataHarmRateVDService rStatDataHarmRateVDService;
    private final IntegrityService integrityService;
    private final IRStatDataVDService rStatDataVDService;
    private final IRStatComassesDService statComassesDService;
    private final IRStatAssesDService statAssesDService;
    private final IRStatDataIDService statDataIDService;
    private final IRStatLimitRateDService rLimitRateDService;
    private final IRStatLimitTargetDService rStatLimitTargetDService;
    private final IRStatDataPltDService statDataPltDService;
    private final IRStatDataFlickerDService statDataFlickerDService;
    private final IRStatDataInharmVDService statDataInharmVDService;
    private final IPqLineDetailService pqLineDetailService;


    @Override
    public void insertDayHarmRateV(String startTime, String endTime) {
        List<String> timeList = PubUtils.getTimeList(startTime,endTime);
        List<PqLineBak> bakList = pqLineBakService.list();
        //lineId:Oracle监测点ID id:Mysql监测点ID
        Map<String, String> oracleRelationMysql = bakList.stream().collect(Collectors.toMap(PqLineBak::getLineId, PqLineBak::getId));
        timeList.forEach(item->{
            System.out.println("insertDayHarmRateV->当前执行日期:"+ item);
            List<RStatDataHarmrateVD> result = new ArrayList<>();
            long system1 = System.currentTimeMillis();
            List<DayHarmrateV> list = oracleDataService.getDayHarmRate(item);
            if(CollUtil.isNotEmpty(list)){
                list.forEach(data->{
                    if (ObjUtil.isNotNull(oracleRelationMysql.get(data.getLineid()))) {
                        RStatDataHarmrateVD po1 = getData1(data,oracleRelationMysql,"AVG");
                        PubUtils.setValuesUsingReflection(po1,data,false,"getV","setV",null);

                        RStatDataHarmrateVD po2 = getData1(data,oracleRelationMysql,"MAX");
                        PubUtils.setValuesUsingReflection(po2,data,true,"getV","setV","Max");

                        RStatDataHarmrateVD po3 = getData1(data,oracleRelationMysql,"MIN");
                        PubUtils.setValuesUsingReflection(po3,data,true,"getV","setV","Min");

                        RStatDataHarmrateVD po4 = getData1(data,oracleRelationMysql,"CP95");
                        PubUtils.setValuesUsingReflection(po4,data,true,"getV","setV","Cp95");
                        MaxValueProcessor.process(po1);
                        MaxValueProcessor.process(po2);
                        MaxValueProcessor.process(po3);
                        MaxValueProcessor.process(po4);
                        result.addAll(Arrays.asList(po1,po2,po3,po4));
                    }
                });
            } else {
                long system2 = System.currentTimeMillis();
                System.out.println("insertDayHarmRateV->查询oracle数据:0,执行时间:" + (system2-system1)/1000.0 + "s");
            }
            if(CollUtil.isNotEmpty(result)){
                rStatDataHarmRateVDService.insert(result);
                long system2 = System.currentTimeMillis();
                System.out.println("insertDayHarmRateV->查询oracle数据:"+(CollUtil.isNotEmpty(list)?list.size():0)+",插入mysql数据:"+ result.size() + ",执行时间:" + (system2-system1)/1000.0 + "s");
            }
        });
    }

    @Override
    public void insertPqsIntegrity(String startTime, String endTime) {
        List<String> timeList = PubUtils.getTimeList(startTime,endTime);
        List<PqLineBak> bakList = pqLineBakService.list();
        //lineId:Oracle监测点ID id:Mysql监测点ID
        Map<String, String> oracleRelationMysql = bakList.stream().collect(Collectors.toMap(PqLineBak::getLineId, PqLineBak::getId));
        ZoneId zoneId = ZoneId.systemDefault();
        timeList.forEach(item->{
            System.out.println("insertPqsIntegrity->当前执行日期:"+ item);
            List<RStatIntegrityD> result = new ArrayList<>();
            long system1 = System.currentTimeMillis();
            List<PqsIntegrity> list = oracleDataService.getPqsIntegrity(item);
            if(CollUtil.isNotEmpty(list)){
                list.forEach(data->{
                    if (ObjUtil.isNotNull(oracleRelationMysql.get(data.getLineIndex()))) {
                        RStatIntegrityD po = new RStatIntegrityD();
                        po.setTimeId(data.getTimeid().toInstant().atZone(zoneId).toLocalDate());
                        po.setLineIndex(oracleRelationMysql.get(data.getLineIndex()));
                        po.setDueTime(data.getDue());
                        po.setRealTime(data.getReal());
                        MaxValueProcessor.process(po);
                        result.add(po);
                    }
                });
            } else {
                long system2 = System.currentTimeMillis();
                System.out.println("insertPqsIntegrity->查询oracle数据:0,执行时间:" + (system2-system1)/1000.0 + "s");
            }
            if(CollUtil.isNotEmpty(result)){
                integrityService.insert(result);
                long system2 = System.currentTimeMillis();
                System.out.println("insertPqsIntegrity->查询oracle数据:"+(CollUtil.isNotEmpty(list)?list.size():0)+",插入mysql数据:"+ result.size() + ",执行时间:" + (system2-system1)/1000.0 + "s");
            }
        });
    }

    @Override
    public void insertDayV(String startTime, String endTime) {
        List<String> timeList = PubUtils.getTimeList(startTime,endTime);
        List<PqLineBak> bakList = pqLineBakService.list();
        //lineId:Oracle监测点ID id:Mysql监测点ID
        Map<String, String> oracleRelationMysql = bakList.stream().collect(Collectors.toMap(PqLineBak::getLineId, PqLineBak::getId));
        timeList.forEach(item->{
            System.out.println("insertDayV->当前执行日期:"+ item);
            List<RStatDataVD> result = new ArrayList<>();
            long system1 = System.currentTimeMillis();
            List<DayV> list = oracleDataService.getDayV(item);
            if(CollUtil.isNotEmpty(list)){
                List<DayV> tList = list.stream().filter(type ->Objects.equals(type.getPhasicType(),"T")).collect(Collectors.toList());
                Map<String, DayV> dayMap = tList.stream().collect(Collectors.toMap(DayV::getLineid, Function.identity()));
                list.forEach(data->{
                    if (ObjUtil.isNotNull(oracleRelationMysql.get(data.getLineid()))) {
                        RStatDataVD po1 = getData2(data,oracleRelationMysql,"AVG");
                        //处理rmsLvr，将ab、bc、ca数据分配到a、b、c中
                        if (Objects.equals(data.getPhasicType(),"A")) {
                            po1.setRmsLvr(Objects.isNull(dayMap.get(data.getLineid()))?0.0f:dayMap.get(data.getLineid()).getRmsab());
                        } else if (Objects.equals(data.getPhasicType(),"B")) {
                            po1.setRmsLvr(Objects.isNull(dayMap.get(data.getLineid()))?0.0f:dayMap.get(data.getLineid()).getRmsbc());
                        } else if (Objects.equals(data.getPhasicType(),"C")) {
                            po1.setRmsLvr(Objects.isNull(dayMap.get(data.getLineid()))?0.0f:dayMap.get(data.getLineid()).getRmsca());
                        } else if (Objects.equals(data.getPhasicType(),"T")) {
                            po1.setRmsLvr(0.0f);
                        }
                        PubUtils.setValuesUsingReflection(po1,data,false,"getV","setV",null);

                        RStatDataVD po2 = getData2(data,oracleRelationMysql,"MAX");
                        if (Objects.equals(data.getPhasicType(),"A")) {
                            po2.setRmsLvr(Objects.isNull(dayMap.get(data.getLineid()))?0.0f:dayMap.get(data.getLineid()).getRmsabMax());
                        } else if (Objects.equals(data.getPhasicType(),"B")) {
                            po2.setRmsLvr(Objects.isNull(dayMap.get(data.getLineid()))?0.0f:dayMap.get(data.getLineid()).getRmsbcMax());
                        } else if (Objects.equals(data.getPhasicType(),"C")) {
                            po2.setRmsLvr(Objects.isNull(dayMap.get(data.getLineid()))?0.0f:dayMap.get(data.getLineid()).getRmscaMax());
                        } else if (Objects.equals(data.getPhasicType(),"T")) {
                            po2.setRmsLvr(0.0f);
                        }
                        PubUtils.setValuesUsingReflection(po2,data,true,"getV","setV","Max");

                        RStatDataVD po3 = getData2(data,oracleRelationMysql,"MIN");
                        if (Objects.equals(data.getPhasicType(),"A")) {
                            po3.setRmsLvr(Objects.isNull(dayMap.get(data.getLineid()))?0.0f:dayMap.get(data.getLineid()).getRmsabMin());
                        } else if (Objects.equals(data.getPhasicType(),"B")) {
                            po3.setRmsLvr(Objects.isNull(dayMap.get(data.getLineid()))?0.0f:dayMap.get(data.getLineid()).getRmsbcMin());
                        } else if (Objects.equals(data.getPhasicType(),"C")) {
                            po3.setRmsLvr(Objects.isNull(dayMap.get(data.getLineid()))?0.0f:dayMap.get(data.getLineid()).getRmscaMin());
                        } else if (Objects.equals(data.getPhasicType(),"T")) {
                            po3.setRmsLvr(0.0f);
                        }
                        PubUtils.setValuesUsingReflection(po3,data,true,"getV","setV","Min");

                        RStatDataVD po4 = getData2(data,oracleRelationMysql,"CP95");
                        if (Objects.equals(data.getPhasicType(),"A")) {
                            po4.setRmsLvr(Objects.isNull(dayMap.get(data.getLineid()))?0.0f:dayMap.get(data.getLineid()).getRmsabCp95());
                        } else if (Objects.equals(data.getPhasicType(),"B")) {
                            po4.setRmsLvr(Objects.isNull(dayMap.get(data.getLineid()))?0.0f:dayMap.get(data.getLineid()).getRmsbcCp95());
                        } else if (Objects.equals(data.getPhasicType(),"C")) {
                            po4.setRmsLvr(Objects.isNull(dayMap.get(data.getLineid()))?0.0f:dayMap.get(data.getLineid()).getRmscaCp95());
                        } else if (Objects.equals(data.getPhasicType(),"T")) {
                            po4.setRmsLvr(0.0f);
                        }
                        PubUtils.setValuesUsingReflection(po4,data,true,"getV","setV","Cp95");
                        MaxValueProcessor.process(po1);
                        MaxValueProcessor.process(po2);
                        MaxValueProcessor.process(po3);
                        MaxValueProcessor.process(po4);
                        result.addAll(Arrays.asList(po1,po2,po3,po4));
                    }
                });
            } else {
                long system2 = System.currentTimeMillis();
                System.out.println("insertDayV->查询oracle数据:0,执行时间:" + (system2-system1)/1000.0 + "s");
            }
            if(CollUtil.isNotEmpty(result)){
                rStatDataVDService.insert(result);
                long system2 = System.currentTimeMillis();
                System.out.println("insertDayV->查询oracle数据:"+(CollUtil.isNotEmpty(list)?list.size():0)+",插入mysql数据:"+ result.size() + ",执行时间:" + (system2-system1)/1000.0 + "s");
            }
        });
    }

    @Override
    public void insertLimitRate(String startTime, String endTime) {
        ZoneId zoneId = ZoneId.systemDefault();
        List<String> timeList = PubUtils.getTimeList(startTime,endTime);
        List<PqLineBak> bakList = pqLineBakService.list();
        //lineId:Oracle监测点ID id:Mysql监测点ID
        Map<String, String> oracleRelationMysql = bakList.stream().collect(Collectors.toMap(PqLineBak::getLineId, PqLineBak::getId));
        timeList.forEach(item->{
            System.out.println("insertLimitRate->当前执行日期:"+ item);
            List<RStatLimitRateD> result = new ArrayList<>();
            long system1 = System.currentTimeMillis();
            List<LimitRate> list = oracleDataService.getLimitRate(item);
            if(CollUtil.isNotEmpty(list)){
                list.forEach(data->{
                    if (ObjUtil.isNotNull(oracleRelationMysql.get(data.getMyindex().toString()))) {
                        RStatLimitRateD po = new RStatLimitRateD();
                        po.setTime(data.getTimeid().toInstant().atZone(zoneId).toLocalDate());
                        po.setLineId(oracleRelationMysql.get(data.getMyindex().toString()));
                        po.setAllTime(data.getAlltime());
                        po.setFlickerAllTime(data.getFlicketAlltime());
                        BeanUtils.copyProperties(data,po);
                        MaxValueProcessor.process(po);
                        result.add(po);
                    }
                });
            } else {
                long system2 = System.currentTimeMillis();
                System.out.println("insertLimitRate->查询oracle数据:0,执行时间:" + (system2-system1)/1000.0 + "s");
            }
            if(CollUtil.isNotEmpty(result)){
                rLimitRateDService.insert(result);
                long system2 = System.currentTimeMillis();
                System.out.println("insertLimitRate->查询oracle数据:"+(CollUtil.isNotEmpty(list)?list.size():0)+",插入mysql数据:"+ result.size() + ",执行时间:" + (system2-system1)/1000.0 + "s");
            }
        });
    }

    @Override
    public void insertLimitTarget(String startTime, String endTime) {
        ZoneId zoneId = ZoneId.systemDefault();
        List<String> timeList = PubUtils.getTimeList(startTime,endTime);
        List<PqLineBak> bakList = pqLineBakService.list();
        //lineId:Oracle监测点ID id:Mysql监测点ID
        Map<String, String> oracleRelationMysql = bakList.stream().collect(Collectors.toMap(PqLineBak::getLineId, PqLineBak::getId));
        timeList.forEach(item->{
            System.out.println("insertLimitTarget->当前执行日期:"+ item);
            List<RStatLimitTargetD> result = new ArrayList<>();
            long system1 = System.currentTimeMillis();
            List<LimitTarget> list = oracleDataService.getLimitTarget(item);
            if(CollUtil.isNotEmpty(list)){
                list.forEach(data->{
                    if (ObjUtil.isNotNull(oracleRelationMysql.get(data.getMyindex().toString()))) {
                        RStatLimitTargetD po = new RStatLimitTargetD();
                        po.setTime(data.getTimeid().toInstant().atZone(zoneId).toLocalDate());
                        po.setLineId(oracleRelationMysql.get(data.getMyindex().toString()));
                        po.setAllTime(data.getAlltime());
                        po.setFlickerAllTime(data.getFlicketAlltime());
                        BeanUtils.copyProperties(data,po);
                        MaxValueProcessor.process(po);
                        result.add(po);
                    }
                });
            } else {
                long system2 = System.currentTimeMillis();
                System.out.println("insertLimitTarget->查询oracle数据:0,执行时间:" + (system2-system1)/1000.0 + "s");
            }
            if(CollUtil.isNotEmpty(result)){
                rStatLimitTargetDService.insert(result);
                long system2 = System.currentTimeMillis();
                System.out.println("insertLimitTarget->查询oracle数据:"+(CollUtil.isNotEmpty(list)?list.size():0)+",插入mysql数据:"+ result.size() + ",执行时间:" + (system2-system1)/1000.0 + "s");
            }
        });
    }

    @Override
    public void insertComAsses(String startTime, String endTime) {
        List<String> timeList = PubUtils.getTimeList(startTime,endTime);
        List<PqLineBak> bakList = pqLineBakService.list();
        //lineId:Oracle监测点ID id:Mysql监测点ID
        Map<String, String> oracleRelationMysql = bakList.stream().collect(Collectors.toMap(PqLineBak::getLineId, PqLineBak::getId));
        timeList.forEach(item-> {
            System.out.println("insertComAsses->当前执行日期:"+ item);
            List<RStatComassesD> result = new ArrayList<>();
            long system1 = System.currentTimeMillis();
            List<PqsComAsses> pqsComAsses = oracleDataService.getPqsComAsses(item);
            if(CollUtil.isNotEmpty(pqsComAsses)){
                pqsComAsses.forEach(data->{
                    if (ObjUtil.isNotNull(oracleRelationMysql.get(data.getLineid()))) {
                        RStatComassesD  comAsses = new RStatComassesD();
                        comAsses.setTime(data.getTimeid());
                        comAsses.setLineId(oracleRelationMysql.get(data.getLineid()));
                        comAsses.setFreqDev1(data.getFreqDev1());
                        comAsses.setFreqDev2(data.getFreqDev2());
                        comAsses.setFreqDev3(data.getFreqDev3());
                        comAsses.setFreqDev4(data.getFreqDev4());
                        comAsses.setFreqDev5(data.getFreqDev5());
                        comAsses.setVuDev1(data.getVuDev1());
                        comAsses.setVuDev2(data.getVuDev2());
                        comAsses.setVuDev3(data.getVuDev3());
                        comAsses.setVuDev4(data.getVuDev4());
                        comAsses.setVuDev5(data.getVuDev5());
                        comAsses.setDataPst1(data.getDataPlt1());
                        comAsses.setDataPst2(data.getDataPlt2());
                        comAsses.setDataPst3(data.getDataPlt3());
                        comAsses.setDataPst4(data.getDataPlt4());
                        comAsses.setDataPst5(data.getDataPlt5());
                        comAsses.setVUnbalance1(data.getVUnbalance1());
                        comAsses.setVUnbalance2(data.getVUnbalance2());
                        comAsses.setVUnbalance3(data.getVUnbalance3());
                        comAsses.setVUnbalance4(data.getVUnbalance4());
                        comAsses.setVUnbalance5(data.getVUnbalance5());
                        comAsses.setVThd1(data.getVThd1());
                        comAsses.setVThd2(data.getVThd2());
                        comAsses.setVThd3(data.getVThd3());
                        comAsses.setVThd4(data.getVThd4());
                        comAsses.setVThd5(data.getVThd5());
                        comAsses.setEvent1(data.getEvent1());
                        comAsses.setEvent2(data.getEvent2());
                        comAsses.setEvent3(data.getEvent3());
                        comAsses.setEvent4(data.getEvent4());
                        comAsses.setEvent5(data.getEvent5());
                        MaxValueProcessor.process(comAsses);
                        result.add(comAsses);
                    }
                });
            } else {
                long system2 = System.currentTimeMillis();
                System.out.println("insertComAsses->查询oracle数据:0,执行时间:" + (system2-system1)/1000.0 + "s");
            }
            if(CollUtil.isNotEmpty(result)){
                statComassesDService.insert(result);
                long system2 = System.currentTimeMillis();
                System.out.println("insertComAsses->查询oracle数据:"+(CollUtil.isNotEmpty(pqsComAsses)?pqsComAsses.size():0)+",插入mysql数据:"+ result.size() + ",执行时间:" + (system2-system1)/1000.0 + "s");
            }
        });
    }

    @Override
    public void insertAsses(String startTime, String endTime) {
        List<String> timeList = PubUtils.getTimeList(startTime,endTime);
        List<PqLineBak> bakList = pqLineBakService.list();
        //lineId:Oracle监测点ID id:Mysql监测点ID
        Map<String, String> oracleRelationMysql = bakList.stream().collect(Collectors.toMap(PqLineBak::getLineId, PqLineBak::getId));
        timeList.forEach(item-> {
            System.out.println("insertAsses->当前执行日期:"+ item);
            List<RStatAssesD> result = new ArrayList<>();
            long system1 = System.currentTimeMillis();
            List<PqsAsses> pqsComAsses = oracleDataService.getPqsAsses(item);
            if(CollUtil.isNotEmpty(pqsComAsses)){
                pqsComAsses.forEach(data->{
                    if (ObjUtil.isNotNull(oracleRelationMysql.get(data.getLineid()))) {
                        RStatAssesD  assesD = new RStatAssesD();
                        assesD.setTime(data.getTimeid());
                        assesD.setLineId(oracleRelationMysql.get(data.getLineid()));
                        assesD.setVuDev(data.getVuDev());
                        assesD.setFreqDev(data.getFreqDev());
                        assesD.setDataPlt(data.getDataPlt());
                        assesD.setVUnbalanceCp95(data.getVUnbalanceCP95());
                        assesD.setVThdCp95(data.getVThdCP95());
                        MaxValueProcessor.process(assesD);
                        result.add(assesD);
                    }
                });
            } else {
                long system2 = System.currentTimeMillis();
                System.out.println("insertAsses->查询oracle数据:0,执行时间:" + (system2-system1)/1000.0 + "s");
            }
            if(CollUtil.isNotEmpty(result)){
                statAssesDService.insert(result);
                long system2 = System.currentTimeMillis();
                System.out.println("insertAsses->查询oracle数据:"+(CollUtil.isNotEmpty(pqsComAsses)?pqsComAsses.size():0)+",插入mysql数据:"+ result.size() + ",执行时间:" + (system2-system1)/1000.0 + "s");
            }
        });
    }

    @Override
    public void insertDataI(String startTime, String endTime) {
        List<String> timeList = PubUtils.getTimeList(startTime,endTime);
        List<PqLineBak> bakList = pqLineBakService.list();
        //lineId:Oracle监测点ID id:Mysql监测点ID
        Map<String, String> oracleRelationMysql = bakList.stream().collect(Collectors.toMap(PqLineBak::getLineId, PqLineBak::getId));
        timeList.forEach(item-> {
            System.out.println("insertDataI->当前执行日期:"+ item);
            List<RStatDataID> result = new ArrayList<>();
            long system1 = System.currentTimeMillis();
            List<DayI> list = oracleDataService.getDayI(item);
            if(CollUtil.isNotEmpty(list)){
                list.forEach(data->{
                    if (ObjUtil.isNotNull(oracleRelationMysql.get(data.getLineid()))) {
                        RStatDataID po1 = getDataI(data,oracleRelationMysql,"AVG");
                        PubUtils.setValuesUsingReflection(po1,data,false,"getI","setI",null);

                        RStatDataID po2 = getDataI(data,oracleRelationMysql,"MAX");
                        PubUtils.setValuesUsingReflection(po2,data,true,"getI","setI","Max");

                        RStatDataID po3 = getDataI(data,oracleRelationMysql,"MIN");
                        PubUtils.setValuesUsingReflection(po3,data,true,"getI","setI","Min");

                        RStatDataID po4 = getDataI(data,oracleRelationMysql,"CP95");
                        PubUtils.setValuesUsingReflection(po4,data,true,"getI","setI","Cp95");
                        MaxValueProcessor.process(po1);
                        MaxValueProcessor.process(po2);
                        MaxValueProcessor.process(po3);
                        MaxValueProcessor.process(po4);
                        result.addAll(Arrays.asList(po1,po2,po3,po4));
                    }
                });
            } else {
                long system2 = System.currentTimeMillis();
                System.out.println("insertDataI->查询oracle数据:0,执行时间:" + (system2-system1)/1000.0 + "s");
            }
            if(CollUtil.isNotEmpty(result)){
                statDataIDService.insert(result);
                long system2 = System.currentTimeMillis();
                System.out.println("insertDataI->查询oracle数据:"+(CollUtil.isNotEmpty(list)?list.size():0)+",插入mysql数据:"+ result.size() + ",执行时间:" + (system2-system1)/1000.0 + "s");
            }
        });
    }

    @Override
    public void insertPlt(String startTime, String endTime) {
        List<String> timeList = PubUtils.getTimeList(startTime,endTime);
        List<PqLineBak> bakList = pqLineBakService.list();
        //lineId:Oracle监测点ID id:Mysql监测点ID
        Map<String, String> oracleRelationMysql = bakList.stream().collect(Collectors.toMap(PqLineBak::getLineId, PqLineBak::getId));
        timeList.forEach(item-> {
            System.out.println("insertPlt->当前执行日期:"+ item);
            List<RStatDataPltD> result = new ArrayList<>();
            long system1 = System.currentTimeMillis();
            List<DayPlt> list = oracleDataService.getDayPlt(item);
            if(CollUtil.isNotEmpty(list)){
                list.forEach(data->{
                    if (ObjUtil.isNotNull(oracleRelationMysql.get(data.getLineid()))) {
                        RStatDataPltD po1 = getDataPlt(data,oracleRelationMysql,"AVG");
                        RStatDataPltD po2 = getDataPlt(data,oracleRelationMysql,"MAX");
                        RStatDataPltD po3 = getDataPlt(data,oracleRelationMysql,"MIN");
                        RStatDataPltD po4 = getDataPlt(data,oracleRelationMysql,"CP95");
                        result.addAll(Arrays.asList(po1,po2,po3,po4));
                    }
                });
            } else {
                long system2 = System.currentTimeMillis();
                System.out.println("insertPlt->查询oracle数据:0,执行时间:" + (system2-system1)/1000.0 + "s");
            }
            if(CollUtil.isNotEmpty(result)){
                statDataPltDService.insert(result);
                long system2 = System.currentTimeMillis();
                System.out.println("insertPlt->查询oracle数据:"+(CollUtil.isNotEmpty(list)?list.size():0)+",插入mysql数据:"+ result.size() + ",执行时间:" + (system2-system1)/1000.0 + "s");
            }
        });
    }

    @Override
    public void insertFlicker(String startTime, String endTime) {
        List<String> timeList = PubUtils.getTimeList(startTime,endTime);
        List<PqLineBak> bakList = pqLineBakService.list();
        //lineId:Oracle监测点ID id:Mysql监测点ID
        Map<String, String> oracleRelationMysql = bakList.stream().collect(Collectors.toMap(PqLineBak::getLineId, PqLineBak::getId));
        timeList.forEach(item-> {
            System.out.println("insertFlicker->当前执行日期:"+ item);
            List<RStatDataFlickerD> result = new ArrayList<>();
            long system1 = System.currentTimeMillis();
            List<DayFlicker> list = oracleDataService.getDayFlicker(item);
            if(CollUtil.isNotEmpty(list)){
                list.forEach(data->{
                    if (ObjUtil.isNotNull(oracleRelationMysql.get(data.getLineid()))) {
                        RStatDataFlickerD po1 = getDataFlicker(data,oracleRelationMysql,"AVG");
                        RStatDataFlickerD po2 = getDataFlicker(data,oracleRelationMysql,"MAX");
                        RStatDataFlickerD po3 = getDataFlicker(data,oracleRelationMysql,"MIN");
                        RStatDataFlickerD po4 = getDataFlicker(data,oracleRelationMysql,"CP95");
                        result.addAll(Arrays.asList(po1,po2,po3,po4));
                    }
                });
            } else {
                long system2 = System.currentTimeMillis();
                System.out.println("insertFlicker->查询oracle数据:0,执行时间:" + (system2-system1)/1000.0 + "s");
            }
            if(CollUtil.isNotEmpty(result)){
                statDataFlickerDService.insert(result);
                long system2 = System.currentTimeMillis();
                System.out.println("insertFlicker->查询oracle数据:"+(CollUtil.isNotEmpty(list)?list.size():0)+",插入mysql数据:"+ result.size() + ",执行时间:" + (system2-system1)/1000.0 + "s");
            }
        });
    }

    @Override
    public void insertInHarmV(String startTime, String endTime) {
        List<String> timeList = PubUtils.getTimeList(startTime,endTime);
        List<PqLineBak> bakList = pqLineBakService.list();
        //lineId:Oracle监测点ID id:Mysql监测点ID
        Map<String, String> oracleRelationMysql = bakList.stream().collect(Collectors.toMap(PqLineBak::getLineId, PqLineBak::getId));
        timeList.forEach(item-> {
            System.out.println("insertInHarmV->当前执行日期:"+ item);
            List<RStatDataInharmVD> result = new ArrayList<>();
            long system1 = System.currentTimeMillis();
            List<DayInharmV> list = oracleDataService.getDayInHarmV(item);
            if(CollUtil.isNotEmpty(list)){
                list.forEach(data->{
                    if (ObjUtil.isNotNull(oracleRelationMysql.get(data.getLineid()))) {
                        RStatDataInharmVD po1 = getDataInHarmV(data,oracleRelationMysql,"AVG");
                        RStatDataInharmVD po2 = getDataInHarmV(data,oracleRelationMysql,"MAX");
                        RStatDataInharmVD po3 = getDataInHarmV(data,oracleRelationMysql,"MIN");
                        RStatDataInharmVD po4 = getDataInHarmV(data,oracleRelationMysql,"CP95");
                        result.addAll(Arrays.asList(po1,po2,po3,po4));
                    }
                });
            } else {
                long system2 = System.currentTimeMillis();
                System.out.println("insertInHarmV->查询oracle数据:0,执行时间:" + (system2-system1)/1000.0 + "s");
            }
            if(CollUtil.isNotEmpty(result)){
                statDataInharmVDService.insert(result);
                long system2 = System.currentTimeMillis();
                System.out.println("insertInHarmV->查询oracle数据:"+(CollUtil.isNotEmpty(list)?list.size():0)+",插入mysql数据:"+ result.size() + ",执行时间:" + (system2-system1)/1000.0 + "s");
            }

        });
    }

    @Override
    public void LineRunFlag() {
        List<PqLineBak> bakList = pqLineBakService.list();
        //lineId:Oracle监测点ID id:Mysql监测点ID
        Map<String, String> oracleRelationMysql = bakList.stream().collect(Collectors.toMap(PqLineBak::getLineId, PqLineBak::getId));
        //获取oracle监测点接口
        List<Line> list = oracleDataService.getLineList();
        if(CollUtil.isNotEmpty(list)){
            list.forEach(data->{
                if (ObjUtil.isNotNull(oracleRelationMysql.get(data.getLineIndex()))) {
                    if(ObjUtil.isNotNull(data.getStatus())){
                        //mysql监测id
                        pqLineDetailService.update(new LambdaUpdateWrapper<PqLineDetail>()
                                .set(PqLineDetail::getRunFlag, data.getStatus())
                                .eq(PqLineDetail::getId, oracleRelationMysql.get(data.getLineIndex()))
                        );
                    }

                }
            });
        }

    }

    @Override
    public void lineSubstation() {
        List<PqLineBak> bakList = pqLineBakService.list();
        Map<String, String> map = bakList.stream().collect(Collectors.toMap(PqLineBak::getLineId, PqLineBak::getId));
        List<LineDetail> lineDetailList = oracleDataService.getLineDetail();
        if(CollUtil.isNotEmpty(lineDetailList)){
            lineDetailList.forEach(item->{
                String mysqlId = map.get(String.valueOf(item.getLineIndex()));
                if (ObjUtil.isNotNull(mysqlId) && StringUtils.isNotBlank(mysqlId)) {
                    pqLineDetailService.update(new LambdaUpdateWrapper<PqLineDetail>()
                            .set(PqLineDetail::getPowerSubstationName, item.getPowerSubstationName())
                            .eq(PqLineDetail::getId, mysqlId)
                    );
                }
            });
        }
    }

    public RStatDataHarmrateVD getData1(DayHarmrateV data, Map<String, String> oracleRelationMysql, String valueType) {
        ZoneId zoneId = ZoneId.systemDefault();
        RStatDataHarmrateVD po1 = new RStatDataHarmrateVD();
        po1.setTime(data.getTimeid().toInstant().atZone(zoneId).toLocalDate());
        po1.setLineId(oracleRelationMysql.get(data.getLineid()));
        po1.setPhaseType(data.getPhasicType());
        po1.setValueType(valueType);
        //异常数据判断
        po1.setQualityFlag(PubUtils.judgeData(TargetEnum.DAY_HARM_RATE_V.getCode(),data,null));
        MaxValueProcessor.process(po1);
        return po1;
    }

    public RStatDataVD getData2(DayV data, Map<String, String> oracleRelationMysql, String valueType) {
        ZoneId zoneId = ZoneId.systemDefault();
        RStatDataVD po1 = new RStatDataVD();
        po1.setTime(data.getTimeid().toInstant().atZone(zoneId).toLocalDate());
        po1.setLineId(oracleRelationMysql.get(data.getLineid()));
        po1.setPhasicType(data.getPhasicType());
        po1.setValueType(valueType);
        if (Objects.equals(valueType,"AVG")) {
            po1.setRms(data.getRms());
            po1.setVuDev(data.getVuDev());
            po1.setVlDev(data.getVlDev());
            po1.setFreq(data.getFreq());
            po1.setFreqDev(data.getFreqDev());
            po1.setVUnbalance(data.getVUnbalance());
            po1.setVPos(data.getVPos());
            po1.setVNeg(data.getVNeg());
            po1.setVZero(data.getVZero());
            po1.setVThd(data.getVThd());
            //异常数据判断
            po1.setQualityFlag(PubUtils.judgeData(TargetEnum.DAY_V.getCode(),data,null));
        } else if (Objects.equals(valueType,"MAX")) {
            po1.setRms(data.getRmsMax());
            po1.setVuDev(data.getVuDevMax());
            po1.setVlDev(data.getVlDevMax());
            po1.setFreq(data.getFreqMax());
            po1.setFreqDev(data.getFreqDevMax());
            po1.setVUnbalance(data.getVUnbalanceMax());
            po1.setVPos(data.getVPosMax());
            po1.setVNeg(data.getVNegMax());
            po1.setVZero(data.getVZeroMax());
            po1.setVThd(data.getVThdMax());
            //异常数据判断
            po1.setQualityFlag(PubUtils.judgeData(TargetEnum.DAY_V.getCode(),data,"Max"));
        } else if (Objects.equals(valueType,"MIN")) {
            po1.setRms(data.getRmsMin());
            po1.setVuDev(data.getVuDevMin());
            po1.setVlDev(data.getVlDevMin());
            po1.setFreq(data.getFreqMin());
            po1.setFreqDev(data.getFreqDevMin());
            po1.setVUnbalance(data.getVUnbalanceMin());
            po1.setVPos(data.getVPosMin());
            po1.setVNeg(data.getVNegMin());
            po1.setVZero(data.getVZeroMin());
            po1.setVThd(data.getVThdMin());
            //异常数据判断
            po1.setQualityFlag(PubUtils.judgeData(TargetEnum.DAY_V.getCode(),data,"Min"));
        } else if (Objects.equals(valueType,"CP95")) {
            po1.setRms(data.getRmsCp95());
            po1.setVuDev(data.getVuDevCp95());
            po1.setVlDev(data.getVlDevCp95());
            po1.setFreq(data.getFreqCp95());
            po1.setFreqDev(data.getFreqDevCp95());
            po1.setVUnbalance(data.getVUnbalanceCp95());
            po1.setVPos(data.getVPosCp95());
            po1.setVNeg(data.getVNegCp95());
            po1.setVZero(data.getVZeroCp95());
            po1.setVThd(data.getVThdCp95());
            //异常数据判断
            po1.setQualityFlag(PubUtils.judgeData(TargetEnum.DAY_V.getCode(),data,"Cp95"));
        }
        MaxValueProcessor.process(po1);
        return po1;
    }

    public RStatDataID getDataI(DayI data, Map<String, String> oracleRelationMysql, String valueType) {
        ZoneId zoneId = ZoneId.systemDefault();
        RStatDataID po1 = new RStatDataID();
        po1.setTime(data.getTimeid().toInstant().atZone(zoneId).toLocalDate());
        po1.setLineId(oracleRelationMysql.get(data.getLineid()));
        po1.setPhaseType(data.getPhasicType());
        po1.setValueType(valueType);
        if (Objects.equals(valueType,"AVG")) {
            po1.setRms(data.getRms());
            po1.setINeg(data.getINeg());
            po1.setIPos(data.getIPos());
            po1.setIThd(data.getIThd());
            po1.setIUnbalance(data.getIUnbalance());
            po1.setIZero(data.getIZero());
            //异常数据判断
            po1.setQualityFlag(PubUtils.judgeData(TargetEnum.DAY_I.getCode(),data,null));
        } else if (Objects.equals(valueType,"MAX")) {
            po1.setRms(data.getRmsMax());
            po1.setINeg(data.getINegMax());
            po1.setIPos(data.getIPosMax());
            po1.setIThd(data.getIThdMax());
            po1.setIUnbalance(data.getIUnbalanceMax());
            po1.setIZero(data.getIZeroMax());
            //异常数据判断
            po1.setQualityFlag(PubUtils.judgeData(TargetEnum.DAY_I.getCode(),data,"Max"));
        } else if (Objects.equals(valueType,"MIN")) {
            po1.setRms(data.getRmsMin());
            po1.setINeg(data.getINegMin());
            po1.setIPos(data.getIPosMin());
            po1.setIThd(data.getIThdMin());
            po1.setIUnbalance(data.getIUnbalanceMin());
            po1.setIZero(data.getIZeroMin());
            //异常数据判断
            po1.setQualityFlag(PubUtils.judgeData(TargetEnum.DAY_I.getCode(),data,"Min"));
        } else if (Objects.equals(valueType,"CP95")) {
            po1.setRms(data.getRmsCp95());
            po1.setINeg(data.getINegCp95());
            po1.setIPos(data.getIPosCp95());
            po1.setIThd(data.getIThdCp95());
            po1.setIUnbalance(data.getIUnbalanceCp95());
            po1.setIZero(data.getIZeroCp95());
            //异常数据判断
            po1.setQualityFlag(PubUtils.judgeData(TargetEnum.DAY_I.getCode(),data,"Cp95"));
        }
        MaxValueProcessor.process(po1);
        return po1;
    }

    public RStatDataPltD getDataPlt(DayPlt data, Map<String, String> oracleRelationMysql, String valueType) {
        ZoneId zoneId = ZoneId.systemDefault();
        RStatDataPltD po1 = new RStatDataPltD();
        po1.setTime(data.getTimeid().toInstant().atZone(zoneId).toLocalDate());
        po1.setLineId(oracleRelationMysql.get(data.getLineid()));
        po1.setPhaseType(data.getPhasicType());
        po1.setValueType(valueType);
        if (Objects.equals(valueType,"AVG")) {
            po1.setPlt(data.getPlt());
            //异常数据判断
            po1.setQualityFlag(PubUtils.judgeData(TargetEnum.DAY_PLT.getCode(),data,null));
        } else if (Objects.equals(valueType,"MAX")) {
            po1.setPlt(data.getPltMax());
            //异常数据判断
            po1.setQualityFlag(PubUtils.judgeData(TargetEnum.DAY_PLT.getCode(),data,"Max"));
        } else if (Objects.equals(valueType,"MIN")) {
            po1.setPlt(data.getPltMin());
            //异常数据判断
            po1.setQualityFlag(PubUtils.judgeData(TargetEnum.DAY_PLT.getCode(),data,"Min"));
        } else if (Objects.equals(valueType,"CP95")) {
            po1.setPlt(data.getPltCp95());
            //异常数据判断
            po1.setQualityFlag(PubUtils.judgeData(TargetEnum.DAY_PLT.getCode(),data,"Cp95"));
        }
        MaxValueProcessor.process(po1);
        return po1;
    }

    public RStatDataFlickerD getDataFlicker(DayFlicker data, Map<String, String> oracleRelationMysql, String valueType) {
        ZoneId zoneId = ZoneId.systemDefault();
        RStatDataFlickerD po1 = new RStatDataFlickerD();
        po1.setTime(data.getTimeid().toInstant().atZone(zoneId).toLocalDate());
        po1.setLineId(oracleRelationMysql.get(data.getLineid()));
        po1.setPhaseType(data.getPhasicType());
        po1.setValueType(valueType);
        if (Objects.equals(valueType,"AVG")) {
            po1.setFluc(data.getFluc());
            po1.setPlt(data.getPlt());
            po1.setPst(data.getPst());
            //异常数据判断
            po1.setQualityFlag(PubUtils.judgeData(TargetEnum.DAY_FLICKER.getCode(),data,null));
        } else if (Objects.equals(valueType,"MAX")) {
            po1.setFluc(data.getFlucMax());
            po1.setPlt(data.getPltMax());
            po1.setPst(data.getPstMax());
            //异常数据判断
            po1.setQualityFlag(PubUtils.judgeData(TargetEnum.DAY_FLICKER.getCode(),data,"Max"));
        } else if (Objects.equals(valueType,"MIN")) {
            po1.setFluc(data.getFlucMin());
            po1.setPlt(data.getPltMin());
            po1.setPst(data.getPstMin());
            //异常数据判断
            po1.setQualityFlag(PubUtils.judgeData(TargetEnum.DAY_FLICKER.getCode(),data,"Min"));
        } else if (Objects.equals(valueType,"CP95")) {
            po1.setFluc(data.getFlucCp95());
            po1.setPlt(data.getPltCp95());
            po1.setPst(data.getPstCp95());
            //异常数据判断
            po1.setQualityFlag(PubUtils.judgeData(TargetEnum.DAY_FLICKER.getCode(),data,"Cp95"));
        }
        MaxValueProcessor.process(po1);
        return po1;
    }

    public RStatDataInharmVD getDataInHarmV(DayInharmV data, Map<String, String> oracleRelationMysql, String valueType) {
        ZoneId zoneId = ZoneId.systemDefault();
        RStatDataInharmVD po1 = new RStatDataInharmVD();
        po1.setTime(data.getTimeid().toInstant().atZone(zoneId).toLocalDate());
        po1.setLineId(oracleRelationMysql.get(data.getLineid()));
        po1.setPhaseType(data.getPhasicType());
        po1.setValueType(valueType);
        if (Objects.equals(valueType,"AVG")) {
            PubUtils.setValuesUsingReflection(po1,data,false,"getV","setV",null);
            //异常数据判断
            po1.setQualityFlag(PubUtils.judgeData(TargetEnum.DAY_IN_HARM_V.getCode(),data,null));
        } else if (Objects.equals(valueType,"MAX")) {
            PubUtils.setValuesUsingReflection(po1,data,true,"getV","setV","Max");
            //异常数据判断
            po1.setQualityFlag(PubUtils.judgeData(TargetEnum.DAY_IN_HARM_V.getCode(),data,"Max"));
        } else if (Objects.equals(valueType,"MIN")) {
            PubUtils.setValuesUsingReflection(po1,data,true,"getV","setV","Min");
            //异常数据判断
            po1.setQualityFlag(PubUtils.judgeData(TargetEnum.DAY_IN_HARM_V.getCode(),data,"Min"));
        } else if (Objects.equals(valueType,"CP95")) {
            PubUtils.setValuesUsingReflection(po1,data,true,"getV","setV","Cp95");
            //异常数据判断
            po1.setQualityFlag(PubUtils.judgeData(TargetEnum.DAY_IN_HARM_V.getCode(),data,"Cp95"));
        }
        MaxValueProcessor.process(po1);
        return po1;
    }
}
