package com.gyee.generation.service;/*
@author   谢生杰
@date   2023/3/15-8:54
*/

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gyee.common.contant.ContantXk;
import com.gyee.common.model.PointData;
import com.gyee.common.util.CommonUtils;
import com.gyee.common.util.DateUtils;
import com.gyee.common.util.DoubleUtils;
import com.gyee.common.util.algorithm.MathUtils;
import com.gyee.generation.init.CacheContext;
import com.gyee.generation.model.auto.*;
import com.gyee.generation.service.auto.IProBasicBranchService;
import com.gyee.generation.service.auto.IProEconEquipmentInfoDay6Service;
import com.gyee.generation.service.auto.IProEconPowerstationInfoDay7Service;
import com.gyee.generation.util.StringUtils;
import com.gyee.generation.util.realtimesource.IEdosUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class EquipmentInfo6Service {
    @Resource
    private IEdosUtil edosUtil;
    @Resource
    private IProBasicBranchService proBasicBranchService;
    @Resource
    private IProEconEquipmentInfoDay6Service proEconEquipmentInfoDay6Service;
    @Resource
    private IProEconPowerstationInfoDay7Service proEconPowerstationInfoDay7Service;


    public void saveEquipmentInfo6(String begin, String end) {
        Map<String, Map<String, ProBasicEquipmentPoint>> wtpAimap = CacheContext.wtpAimap;
        Map<String, Map<String, ProBasicEquipmentPoint>> zlpointmap = CacheContext.zlpointmap;
        Map<String, ProEconEquipmentmodel> modelMap = CacheContext.modelMap;
        List<String> days = DateUtils.getDays(begin, end);

        days.stream().forEach(day -> {
            List<ProEconEquipmentInfoDay6> resultList = new ArrayList<>();
            Date date = DateUtils.parseDate(day);
            Date beforeDay = DateUtils.addDays(date, -1);

            //删除存在的当天记录
            QueryWrapper<ProEconEquipmentInfoDay6> removeQw = new QueryWrapper<>();
            removeQw.eq("record_date", date);
            proEconEquipmentInfoDay6Service.remove(removeQw);
            //查询前一天记录
            QueryWrapper<ProEconEquipmentInfoDay6> beforeQw = new QueryWrapper<>();
            beforeQw.eq("record_date", beforeDay);
            List<ProEconEquipmentInfoDay6> beforeList = proEconEquipmentInfoDay6Service.list(beforeQw);

            Date startOfDay = DateUtils.getStartOfDay(date);
            Date endOfDay = DateUtils.getEndOfDay(date);
            int month = DateUtils.getMonth(date);
            int day1 = DateUtils.getDay(date);
            for (ProBasicEquipment wt : CacheContext.wtls.stream().filter(i -> i.getEquipmentCategory() == -2).collect(Collectors.toList())) {
                Optional<ProEconEquipmentInfoDay6> first = beforeList.stream().filter(i -> i.getWindturbineId().equals(wt.getId())).findFirst();
                QueryWrapper<ProBasicBranch> qw = new QueryWrapper<>();
                qw.eq("intever_id", wt.getId());
                List<ProBasicBranch> basicBranches = proBasicBranchService.list(qw);

                ProEconEquipmentInfoDay6 rate = new ProEconEquipmentInfoDay6();

                rate.setId(CommonUtils.getUUID());
                rate.setRecordDate(date);
                rate.setWindpowerstationId(wt.getWindpowerstationId());
                rate.setProjectId(wt.getProjectId());
                rate.setLineId(wt.getLineId());
                rate.setWindturbineId(wt.getId());
                rate.setCompanyId(wt.getCompanyId());
                rate.setRegionId(wt.getRegionId());
                if (wtpAimap.containsKey(wt.getId())) {
                    double aveZlgl = 0;
                    double aveJlgl = 0;

                    Map<String, ProBasicEquipmentPoint> equipmentPointMap = wtpAimap.get(wt.getId());

                    if (equipmentPointMap.containsKey(ContantXk.RFDL) && equipmentPointMap.containsKey(ContantXk.CJ_SSFS)) {
                        ProBasicEquipmentPoint rfdlPoint = equipmentPointMap.get(ContantXk.RFDL);
                        ProBasicEquipmentPoint gzqdPoint = equipmentPointMap.get(ContantXk.CJ_SSFS);
                        double rfdl = 0;
                        double gzqd = 0;
                        try {
                            rfdl = edosUtil.getSectionData(rfdlPoint, endOfDay.getTime() / 1000).getPointValueInDouble();
                            List<PointData> gzqdPointdata = edosUtil.getHistoryDatasSnap(gzqdPoint, startOfDay.getTime() / 1000, endOfDay.getTime() / 1000, null, 1800l);
                            gzqd = gzqdPointdata.stream().mapToDouble(PointData::getPointValueInDouble).sum();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        double xtxl = rfdl / (gzqd / 1000 * modelMap.get(wt.getModelId()).getPowerProduction());
                        rate.setRxtxl(xtxl);

                        if (first.isPresent()) {
                            ProEconEquipmentInfoDay6 wtBefore = first.get();
                            if (day1 == 1) {
                                rate.setYxtxl(xtxl);
                            } else {
                                rate.setYxtxl((xtxl + wtBefore.getYxtxl()) / 2);
                            }

                            if (day1 == 1 && month == 1) {
                                rate.setNxtxl(xtxl);
                            } else {
                                rate.setNxtxl((xtxl + wtBefore.getNxtxl()) / 2);
                            }
                        } else {
                            rate.setYzdzlgl(xtxl);
                            rate.setNzdzlgl(xtxl);
                        }
                    }


                    if (equipmentPointMap.containsKey(ContantXk.NBQSRGL))   //逆变器直流功率
                    {
                        ProBasicEquipmentPoint point = equipmentPointMap.get(ContantXk.NBQSRGL);
                        List<PointData> pointls = null;
                        try {
                            pointls = edosUtil.getHistoryDatasSnap(point.getNemCode(), startOfDay.getTime() / 1000, endOfDay.getTime() / 1000, null, 1800l);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        if (!pointls.isEmpty()) {
                            DoubleSummaryStatistics summaryStatistics = pointls.stream().mapToDouble(PointData::getPointValueInDouble).summaryStatistics();
                            //最大功率
                            rate.setRzdzlgl(summaryStatistics.getMax());
                            aveZlgl = summaryStatistics.getAverage();

                            if (first.isPresent()) {
                                ProEconEquipmentInfoDay6 wtBefore = first.get();
                                if (day1 == 1) {
                                    rate.setYzdzlgl(summaryStatistics.getMax());
                                } else {
                                    rate.setYzdzlgl(summaryStatistics.getMax() > wtBefore.getYzdzlgl() ? summaryStatistics.getMax() : wtBefore.getYzdzlgl());
                                }

                                if (day1 == 1 && month == 1) {
                                    rate.setNzdzlgl(summaryStatistics.getMax());
                                } else {
                                    rate.setNzdzlgl(summaryStatistics.getMax() > wtBefore.getNzdzlgl() ? summaryStatistics.getMax() : wtBefore.getNzdzlgl());
                                }
                            } else {
                                rate.setYzdzlgl(summaryStatistics.getMax());
                                rate.setNzdzlgl(summaryStatistics.getMax());
                            }

                        }
                    }

                    if (equipmentPointMap.containsKey(ContantXk.NBQSCGL))   //逆变器交流功率
                    {
                        ProBasicEquipmentPoint point = equipmentPointMap.get(ContantXk.NBQSCGL);
                        List<PointData> pointls = null;
                        try {
                            pointls = edosUtil.getHistoryDatasSnap(point.getNemCode(), startOfDay.getTime() / 1000, endOfDay.getTime() / 1000, null, 1800l);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        if (!pointls.isEmpty()) {
                            DoubleSummaryStatistics summaryStatistics = pointls.stream().mapToDouble(PointData::getPointValueInDouble).summaryStatistics();
                            //最大功率
                            rate.setRzdjlgl(summaryStatistics.getMax());
                            aveJlgl = summaryStatistics.getAverage();

                            if (first.isPresent()) {
                                ProEconEquipmentInfoDay6 wtBefore = first.get();
                                if (day1 == 1) {
                                    rate.setYzdjlgl(summaryStatistics.getMax());
                                } else {
                                    rate.setYzdjlgl(summaryStatistics.getMax() > wtBefore.getYzdjlgl() ? summaryStatistics.getMax() : wtBefore.getYzdjlgl());

                                }
                                if (day1 == 1 && month == 1) {
                                    rate.setNzdjlgl(summaryStatistics.getMax());
                                } else {
                                    rate.setNzdjlgl(summaryStatistics.getMax() > wtBefore.getNzdjlgl() ? summaryStatistics.getMax() : wtBefore.getNzdjlgl());

                                }
                            } else {
                                rate.setYzdjlgl(summaryStatistics.getMax());
                                rate.setNzdjlgl(summaryStatistics.getMax());
                            }
                        }
                    }
                    rate.setRnbqzhxl(aveZlgl != 0 ? DoubleUtils.keepPrecision(aveJlgl / aveZlgl * 100, 2) : 0);
                    if (first.isPresent()) {
                        ProEconEquipmentInfoDay6 wtBefore = first.get();
                        if (day1 == 1) {
                            rate.setYnbqzhxl(rate.getRnbqzhxl());
                        } else {
                            rate.setYnbqzhxl((wtBefore.getYnbqzhxl() + rate.getRnbqzhxl()) / 2);
                        }

                        if (day1 == 1 && month == 1) {
                            rate.setNnbqzhxl(rate.getRnbqzhxl());
                        } else {
                            rate.setNnbqzhxl((wtBefore.getNnbqzhxl() + rate.getRnbqzhxl()) / 2);
                        }
                    } else {
                        rate.setYnbqzhxl(rate.getRnbqzhxl());
                        rate.setNnbqzhxl(rate.getRnbqzhxl());
                    }


                    ProBasicEquipmentPoint equipmentPoint = zlpointmap.get(basicBranches.get(0).getId()).get(ContantXk.ZLDL);
                    List<PointData> pointls = null;
                    try {
                        pointls = edosUtil.getHistoryDatasSnap(equipmentPoint.getNemCode(), startOfDay.getTime() / 1000, endOfDay.getTime() / 1000, null, 1800l);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    Map<String, List<PointData>> zltempMap = new HashMap<>();
                    for (int j = 0; j < basicBranches.size(); j++) {
                        ProBasicEquipmentPoint point = zlpointmap.get(basicBranches.get(j).getId()).get(ContantXk.ZLDL);
                        List<PointData> points = null;
                        try {
                            points = edosUtil.getHistoryDatasSnap(point.getNemCode(), startOfDay.getTime() / 1000, endOfDay.getTime() / 1000, null, 1800l);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        zltempMap.put(basicBranches.get(j).getId(), points);
                    }
                    double lslSum = 0;
                    for (int i = 0; i < pointls.size(); i++) {
                        List<Double> tempList = new ArrayList<>();
                        int sum = 0;
                        for (int j = 0; j < basicBranches.size(); j++) {
                            tempList.add(zltempMap.get(j).get(i).getPointValueInDouble());
                            sum += zltempMap.get(j).get(i).getPointValueInDouble();
                        }
                        Double[] array = (Double[]) tempList.toArray();
                        double bzfc = MathUtils.standardDiviation(array);
                        int ave = sum / basicBranches.size();
                        double lsl = bzfc / ave * 100;
                        lslSum += lsl;
                    }

                    double rlsl = DoubleUtils.keepPrecision(lslSum / pointls.size(), 2);
                    rate.setRlsl(rlsl);
                    if (first.isPresent()) {
                        ProEconEquipmentInfoDay6 wtBefore = first.get();
                        if (day1 == 1) {
                            rate.setYlsl(rlsl);
                        } else {
                            rate.setYlsl((wtBefore.getYlsl() + rlsl) / 2);
                        }

                        if (day1 == 1 && month == 1) {
                            rate.setNlsl(rlsl);
                        } else {
                            rate.setNlsl((wtBefore.getNlsl() + rlsl) / 2);
                        }
                    } else {
                        rate.setYlsl(rlsl);
                        rate.setNlsl(rlsl);
                    }
                }
                resultList.add(rate);
            }

            List<ProEconEquipmentInfoDay6> templs = new ArrayList<>();
            for (ProEconEquipmentInfoDay6 vo : resultList) {
                templs.add(vo);
                if (templs.size() == 1000) {
                    proEconEquipmentInfoDay6Service.saveOrUpdateBatch(templs);
                    templs = new ArrayList<>();
                }
            }

            if (!templs.isEmpty()) {
                proEconEquipmentInfoDay6Service.saveOrUpdateBatch(templs);
            }
            // proEconEquipmentInfoDay6Service.saveBatch(resultList);
        });
    }

    public void savePowerstationInfo(String begin, String end) {
        List<String> days = DateUtils.getDays(begin, end);


        days.stream().forEach(day -> {

            Date date = DateUtils.parseDate(day);
            QueryWrapper<ProEconEquipmentInfoDay6> qw = new QueryWrapper<>();
            qw.eq("record_date", date);
            List<ProEconEquipmentInfoDay6> list = proEconEquipmentInfoDay6Service.list(qw);


            Map<String, List<ProBasicCompany>> rgcpmap = CacheContext.rgcpmap;
            Map<String, List<ProBasicPowerstation>> cpwpmap = CacheContext.cpwpmap;
            Map<String, List<ProBasicProject>> wppromap = CacheContext.wppromap;
            Map<String, List<ProBasicLine>> prolinemap = CacheContext.prolinemap;
            Map<String, List<ProBasicEquipment>> lnwtmap = CacheContext.lnwtmap;
            Map<String, ProBasicProject> pjmap = CacheContext.pjmap;
            Map<String, ProBasicPowerstation> wpmap = CacheContext.wpmap;

            List<ProBasicRegion> rgls = CacheContext.rgls;
            List<ProBasicLine> lnls = CacheContext.lnls;
            List<ProBasicProject> pjls = CacheContext.pjls;
            List<ProBasicPowerstation> wpls = CacheContext.wpls;
            List<ProBasicCompany> cpls = CacheContext.cpls;

            List<ProEconPowerstationInfoDay7> lnResultlist = new ArrayList<>();
            lnls.stream().forEach(ln -> {
                List<ProEconEquipmentInfoDay6> equipmentInfoDay6s = list.stream().filter(i -> i.getLineId().equals(ln.getId())).collect(Collectors.toList());
                if (StringUtils.isNotEmpty(equipmentInfoDay6s)) {
                    double rxtxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getRxtxl).average().getAsDouble();
                    double yxtxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getYxtxl).average().getAsDouble();
                    double nxtxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getNxtxl).average().getAsDouble();
                    double rlsl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getRlsl).average().getAsDouble();
                    double ylsl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getYlsl).average().getAsDouble();
                    double nlsl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getNlsl).average().getAsDouble();
                    double rzhxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getRnbqzhxl).average().getAsDouble();
                    double yzhxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getYnbqzhxl).average().getAsDouble();
                    double nzhxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getNnbqzhxl).average().getAsDouble();
                    ProEconPowerstationInfoDay7 proEconPowerstationInfoDay7 = new ProEconPowerstationInfoDay7();
                    proEconPowerstationInfoDay7.setId(CommonUtils.getUUID());
                    proEconPowerstationInfoDay7.setForeignKeyId(ln.getId());
                    proEconPowerstationInfoDay7.setRecordDate(date);
                    proEconPowerstationInfoDay7.setWindpowerstationId(pjmap.get(ln.getProjectId()).getWindpowerstationId());
                    proEconPowerstationInfoDay7.setProjectId(ln.getProjectId());
                    proEconPowerstationInfoDay7.setLineId(ln.getId());
                    proEconPowerstationInfoDay7.setCompanyId(wpmap.get(pjmap.get(ln.getProjectId()).getWindpowerstationId()).getCompanyId());
                    proEconPowerstationInfoDay7.setRegionId(wpmap.get(pjmap.get(ln.getProjectId()).getWindpowerstationId()).getRegionId());
                    proEconPowerstationInfoDay7.setLocation("ln");
                    proEconPowerstationInfoDay7.setRxtxlpr(DoubleUtils.keepPrecision(rxtxl, 2));
                    proEconPowerstationInfoDay7.setYxtxlpr(DoubleUtils.keepPrecision(yxtxl, 2));
                    proEconPowerstationInfoDay7.setNxtxlpr(DoubleUtils.keepPrecision(nxtxl, 2));
                    proEconPowerstationInfoDay7.setRlsl(DoubleUtils.keepPrecision(rlsl, 2));
                    proEconPowerstationInfoDay7.setYlsl(DoubleUtils.keepPrecision(ylsl, 2));
                    proEconPowerstationInfoDay7.setNlsl(DoubleUtils.keepPrecision(nlsl, 2));
                    proEconPowerstationInfoDay7.setRnbqzhxl(DoubleUtils.keepPrecision(rzhxl, 2));
                    proEconPowerstationInfoDay7.setYnbqzhxl(DoubleUtils.keepPrecision(yzhxl, 2));
                    proEconPowerstationInfoDay7.setNnbqzhxl(DoubleUtils.keepPrecision(nzhxl, 2));
                    lnResultlist.add(proEconPowerstationInfoDay7);
                }
            });

            List<ProEconPowerstationInfoDay7> templs = new ArrayList<>();
            for (ProEconPowerstationInfoDay7 vo : lnResultlist) {
                templs.add(vo);
                if (templs.size() == 1000) {
                    proEconPowerstationInfoDay7Service.saveOrUpdateBatch(templs);
                    templs = new ArrayList<>();
                }
            }

            if (!templs.isEmpty()) {
                proEconPowerstationInfoDay7Service.saveOrUpdateBatch(templs);
            }
            //            proEconPowerstationInfoDay7Service.saveBatch(lnResultlist);

            List<ProEconPowerstationInfoDay7> pjResultlist = new ArrayList<>();
            pjls.stream().forEach(pj -> {
                List<ProEconEquipmentInfoDay6> equipmentInfoDay6s = list.stream().filter(i -> i.getProjectId().equals(pj.getId())).collect(Collectors.toList());
                if (StringUtils.isNotEmpty(equipmentInfoDay6s)) {
                    double rxtxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getRxtxl).average().getAsDouble();
                    double yxtxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getYxtxl).average().getAsDouble();
                    double nxtxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getNxtxl).average().getAsDouble();
                    double rlsl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getRlsl).average().getAsDouble();
                    double ylsl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getYlsl).average().getAsDouble();
                    double nlsl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getNlsl).average().getAsDouble();
                    double rzhxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getRnbqzhxl).average().getAsDouble();
                    double yzhxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getYnbqzhxl).average().getAsDouble();
                    double nzhxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getNnbqzhxl).average().getAsDouble();
                    ProEconPowerstationInfoDay7 proEconPowerstationInfoDay7 = new ProEconPowerstationInfoDay7();
                    proEconPowerstationInfoDay7.setId(CommonUtils.getUUID());
                    proEconPowerstationInfoDay7.setForeignKeyId(pj.getId());
                    proEconPowerstationInfoDay7.setRecordDate(date);
                    proEconPowerstationInfoDay7.setWindpowerstationId(pjmap.get(pj.getId()).getWindpowerstationId());
                    proEconPowerstationInfoDay7.setProjectId(pj.getId());
                    proEconPowerstationInfoDay7.setCompanyId(wpmap.get(pjmap.get(pj.getId()).getWindpowerstationId()).getCompanyId());
                    proEconPowerstationInfoDay7.setRegionId(wpmap.get(pjmap.get(pj.getId()).getWindpowerstationId()).getRegionId());
                    proEconPowerstationInfoDay7.setLocation("pj");
                    proEconPowerstationInfoDay7.setRxtxlpr(DoubleUtils.keepPrecision(rxtxl, 2));
                    proEconPowerstationInfoDay7.setYxtxlpr(DoubleUtils.keepPrecision(yxtxl, 2));
                    proEconPowerstationInfoDay7.setNxtxlpr(DoubleUtils.keepPrecision(nxtxl, 2));
                    proEconPowerstationInfoDay7.setRlsl(DoubleUtils.keepPrecision(rlsl, 2));
                    proEconPowerstationInfoDay7.setYlsl(DoubleUtils.keepPrecision(ylsl, 2));
                    proEconPowerstationInfoDay7.setNlsl(DoubleUtils.keepPrecision(nlsl, 2));
                    proEconPowerstationInfoDay7.setRnbqzhxl(DoubleUtils.keepPrecision(rzhxl, 2));
                    proEconPowerstationInfoDay7.setYnbqzhxl(DoubleUtils.keepPrecision(yzhxl, 2));
                    proEconPowerstationInfoDay7.setNnbqzhxl(DoubleUtils.keepPrecision(nzhxl, 2));
                    pjResultlist.add(proEconPowerstationInfoDay7);
                }
            });


            List<ProEconPowerstationInfoDay7> temp2ls = new ArrayList<>();
            for (ProEconPowerstationInfoDay7 vo : pjResultlist) {
                temp2ls.add(vo);
                if (temp2ls.size() == 1000) {
                    proEconPowerstationInfoDay7Service.saveOrUpdateBatch(temp2ls);
                    temp2ls = new ArrayList<>();
                }
            }

            if (!temp2ls.isEmpty()) {
                proEconPowerstationInfoDay7Service.saveOrUpdateBatch(temp2ls);
            }
            //            proEconPowerstationInfoDay7Service.saveBatch(pjResultlist);


            List<ProEconPowerstationInfoDay7> wpResultlist = new ArrayList<>();
            wpls.stream().forEach(wp -> {
                List<ProEconEquipmentInfoDay6> equipmentInfoDay6s = list.stream().filter(i -> i.getWindpowerstationId().equals(wp.getId())).collect(Collectors.toList());
                if (StringUtils.isNotEmpty(equipmentInfoDay6s)) {
                    double rxtxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getRxtxl).average().getAsDouble();
                    double yxtxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getYxtxl).average().getAsDouble();
                    double nxtxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getNxtxl).average().getAsDouble();
                    double rlsl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getRlsl).average().getAsDouble();
                    double ylsl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getYlsl).average().getAsDouble();
                    double nlsl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getNlsl).average().getAsDouble();
                    double rzhxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getRnbqzhxl).average().getAsDouble();
                    double yzhxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getYnbqzhxl).average().getAsDouble();
                    double nzhxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getNnbqzhxl).average().getAsDouble();
                    ProEconPowerstationInfoDay7 proEconPowerstationInfoDay7 = new ProEconPowerstationInfoDay7();
                    proEconPowerstationInfoDay7.setId(CommonUtils.getUUID());
                    proEconPowerstationInfoDay7.setForeignKeyId(wp.getId());
                    proEconPowerstationInfoDay7.setRecordDate(date);
                    proEconPowerstationInfoDay7.setWindpowerstationId(wp.getId());
                    proEconPowerstationInfoDay7.setCompanyId(wp.getCompanyId());
                    proEconPowerstationInfoDay7.setRegionId(wp.getRegionId());
                    proEconPowerstationInfoDay7.setLocation("wp");
                    proEconPowerstationInfoDay7.setRxtxlpr(DoubleUtils.keepPrecision(rxtxl, 2));
                    proEconPowerstationInfoDay7.setYxtxlpr(DoubleUtils.keepPrecision(yxtxl, 2));
                    proEconPowerstationInfoDay7.setNxtxlpr(DoubleUtils.keepPrecision(nxtxl, 2));
                    proEconPowerstationInfoDay7.setRlsl(DoubleUtils.keepPrecision(rlsl, 2));
                    proEconPowerstationInfoDay7.setYlsl(DoubleUtils.keepPrecision(ylsl, 2));
                    proEconPowerstationInfoDay7.setNlsl(DoubleUtils.keepPrecision(nlsl, 2));
                    proEconPowerstationInfoDay7.setRnbqzhxl(DoubleUtils.keepPrecision(rzhxl, 2));
                    proEconPowerstationInfoDay7.setYnbqzhxl(DoubleUtils.keepPrecision(yzhxl, 2));
                    proEconPowerstationInfoDay7.setNnbqzhxl(DoubleUtils.keepPrecision(nzhxl, 2));
                    wpResultlist.add(proEconPowerstationInfoDay7);
                }
            });

            List<ProEconPowerstationInfoDay7> temp3ls = new ArrayList<>();
            for (ProEconPowerstationInfoDay7 vo : wpResultlist) {
                temp3ls.add(vo);
                if (temp3ls.size() == 1000) {
                    proEconPowerstationInfoDay7Service.saveOrUpdateBatch(temp3ls);
                    temp3ls = new ArrayList<>();
                }
            }

            if (!temp3ls.isEmpty()) {
                proEconPowerstationInfoDay7Service.saveOrUpdateBatch(temp3ls);
            }
            //            proEconPowerstationInfoDay7Service.saveBatch(wpResultlist);


            List<ProEconPowerstationInfoDay7> cp0Resultlist = new ArrayList<>();
            cpls.stream().forEach(cp -> {
                List<ProEconEquipmentInfoDay6> equipmentInfoDay6s = list.stream().filter(i -> i.getCompanyId().equals(cp.getId())).collect(Collectors.toList());
                if (StringUtils.isNotEmpty(equipmentInfoDay6s)) {
                    double rxtxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getRxtxl).average().getAsDouble();
                    double yxtxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getYxtxl).average().getAsDouble();
                    double nxtxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getNxtxl).average().getAsDouble();
                    double rlsl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getRlsl).average().getAsDouble();
                    double ylsl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getYlsl).average().getAsDouble();
                    double nlsl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getNlsl).average().getAsDouble();
                    double rzhxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getRnbqzhxl).average().getAsDouble();
                    double yzhxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getYnbqzhxl).average().getAsDouble();
                    double nzhxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getNnbqzhxl).average().getAsDouble();
                    ProEconPowerstationInfoDay7 proEconPowerstationInfoDay7 = new ProEconPowerstationInfoDay7();
                    proEconPowerstationInfoDay7.setId(CommonUtils.getUUID());
                    proEconPowerstationInfoDay7.setForeignKeyId(cp.getId() + "0");
                    proEconPowerstationInfoDay7.setRecordDate(date);
                    proEconPowerstationInfoDay7.setCompanyId(cp.getId());
                    proEconPowerstationInfoDay7.setRegionId(cp.getRegionId());
                    proEconPowerstationInfoDay7.setLocation("cp");
                    proEconPowerstationInfoDay7.setRxtxlpr(DoubleUtils.keepPrecision(rxtxl, 2));
                    proEconPowerstationInfoDay7.setYxtxlpr(DoubleUtils.keepPrecision(yxtxl, 2));
                    proEconPowerstationInfoDay7.setNxtxlpr(DoubleUtils.keepPrecision(nxtxl, 2));
                    proEconPowerstationInfoDay7.setRlsl(DoubleUtils.keepPrecision(rlsl, 2));
                    proEconPowerstationInfoDay7.setYlsl(DoubleUtils.keepPrecision(ylsl, 2));
                    proEconPowerstationInfoDay7.setNlsl(DoubleUtils.keepPrecision(nlsl, 2));
                    proEconPowerstationInfoDay7.setRnbqzhxl(DoubleUtils.keepPrecision(rzhxl, 2));
                    proEconPowerstationInfoDay7.setYnbqzhxl(DoubleUtils.keepPrecision(yzhxl, 2));
                    proEconPowerstationInfoDay7.setNnbqzhxl(DoubleUtils.keepPrecision(nzhxl, 2));
                    cp0Resultlist.add(proEconPowerstationInfoDay7);
                }
            });
            proEconPowerstationInfoDay7Service.saveBatch(cp0Resultlist);

            List<ProEconPowerstationInfoDay7> cp2Resultlist = new ArrayList<>();
            cpls.stream().forEach(cp -> {
                List<ProEconEquipmentInfoDay6> equipmentInfoDay6s = list.stream().filter(i -> i.getCompanyId().equals(cp.getId())).collect(Collectors.toList());
                if (StringUtils.isNotEmpty(equipmentInfoDay6s)) {
                    double rxtxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getRxtxl).average().getAsDouble();
                    double yxtxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getYxtxl).average().getAsDouble();
                    double nxtxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getNxtxl).average().getAsDouble();
                    double rlsl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getRlsl).average().getAsDouble();
                    double ylsl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getYlsl).average().getAsDouble();
                    double nlsl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getNlsl).average().getAsDouble();
                    double rzhxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getRnbqzhxl).average().getAsDouble();
                    double yzhxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getYnbqzhxl).average().getAsDouble();
                    double nzhxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getNnbqzhxl).average().getAsDouble();
                    ProEconPowerstationInfoDay7 proEconPowerstationInfoDay7 = new ProEconPowerstationInfoDay7();
                    proEconPowerstationInfoDay7.setId(CommonUtils.getUUID());
                    proEconPowerstationInfoDay7.setForeignKeyId(cp.getId() + "-2");
                    proEconPowerstationInfoDay7.setRecordDate(date);
                    proEconPowerstationInfoDay7.setCompanyId(cp.getId());
                    proEconPowerstationInfoDay7.setRegionId(cp.getRegionId());
                    proEconPowerstationInfoDay7.setLocation("cp");
                    proEconPowerstationInfoDay7.setRxtxlpr(DoubleUtils.keepPrecision(rxtxl, 2));
                    proEconPowerstationInfoDay7.setYxtxlpr(DoubleUtils.keepPrecision(yxtxl, 2));
                    proEconPowerstationInfoDay7.setNxtxlpr(DoubleUtils.keepPrecision(nxtxl, 2));
                    proEconPowerstationInfoDay7.setRlsl(DoubleUtils.keepPrecision(rlsl, 2));
                    proEconPowerstationInfoDay7.setYlsl(DoubleUtils.keepPrecision(ylsl, 2));
                    proEconPowerstationInfoDay7.setNlsl(DoubleUtils.keepPrecision(nlsl, 2));
                    proEconPowerstationInfoDay7.setRnbqzhxl(DoubleUtils.keepPrecision(rzhxl, 2));
                    proEconPowerstationInfoDay7.setYnbqzhxl(DoubleUtils.keepPrecision(yzhxl, 2));
                    proEconPowerstationInfoDay7.setNnbqzhxl(DoubleUtils.keepPrecision(nzhxl, 2));
                    cp2Resultlist.add(proEconPowerstationInfoDay7);
                }
            });
            proEconPowerstationInfoDay7Service.saveBatch(cp2Resultlist);

            List<ProEconPowerstationInfoDay7> rp0Resultlist = new ArrayList<>();
            rgls.stream().forEach(rg -> {
                List<ProEconEquipmentInfoDay6> equipmentInfoDay6s = list.stream().filter(i -> i.getRegionId().equals(rg.getId())).collect(Collectors.toList());
                if (StringUtils.isNotEmpty(equipmentInfoDay6s)) {
                    double rxtxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getRxtxl).average().getAsDouble();
                    double yxtxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getYxtxl).average().getAsDouble();
                    double nxtxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getNxtxl).average().getAsDouble();
                    double rlsl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getRlsl).average().getAsDouble();
                    double ylsl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getYlsl).average().getAsDouble();
                    double nlsl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getNlsl).average().getAsDouble();
                    double rzhxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getRnbqzhxl).average().getAsDouble();
                    double yzhxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getYnbqzhxl).average().getAsDouble();
                    double nzhxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getNnbqzhxl).average().getAsDouble();
                    ProEconPowerstationInfoDay7 proEconPowerstationInfoDay7 = new ProEconPowerstationInfoDay7();
                    proEconPowerstationInfoDay7.setId(CommonUtils.getUUID());
                    proEconPowerstationInfoDay7.setForeignKeyId(rg.getId() + "0");
                    proEconPowerstationInfoDay7.setRecordDate(date);
                    proEconPowerstationInfoDay7.setRegionId(rg.getId());
                    proEconPowerstationInfoDay7.setLocation("rg");
                    proEconPowerstationInfoDay7.setRxtxlpr(DoubleUtils.keepPrecision(rxtxl, 2));
                    proEconPowerstationInfoDay7.setYxtxlpr(DoubleUtils.keepPrecision(yxtxl, 2));
                    proEconPowerstationInfoDay7.setNxtxlpr(DoubleUtils.keepPrecision(nxtxl, 2));
                    proEconPowerstationInfoDay7.setRlsl(DoubleUtils.keepPrecision(rlsl, 2));
                    proEconPowerstationInfoDay7.setYlsl(DoubleUtils.keepPrecision(ylsl, 2));
                    proEconPowerstationInfoDay7.setNlsl(DoubleUtils.keepPrecision(nlsl, 2));
                    proEconPowerstationInfoDay7.setRnbqzhxl(DoubleUtils.keepPrecision(rzhxl, 2));
                    proEconPowerstationInfoDay7.setYnbqzhxl(DoubleUtils.keepPrecision(yzhxl, 2));
                    proEconPowerstationInfoDay7.setNnbqzhxl(DoubleUtils.keepPrecision(nzhxl, 2));
                    rp0Resultlist.add(proEconPowerstationInfoDay7);
                }
            });
            proEconPowerstationInfoDay7Service.saveBatch(rp0Resultlist);

            List<ProEconPowerstationInfoDay7> rp2Resultlist = new ArrayList<>();
            rgls.stream().forEach(rg -> {
                List<ProEconEquipmentInfoDay6> equipmentInfoDay6s = list.stream().filter(i -> i.getRegionId().equals(rg.getId())).collect(Collectors.toList());
                if (StringUtils.isNotEmpty(equipmentInfoDay6s)) {
                    double rxtxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getRxtxl).average().getAsDouble();
                    double yxtxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getYxtxl).average().getAsDouble();
                    double nxtxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getNxtxl).average().getAsDouble();
                    double rlsl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getRlsl).average().getAsDouble();
                    double ylsl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getYlsl).average().getAsDouble();
                    double nlsl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getNlsl).average().getAsDouble();
                    double rzhxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getRnbqzhxl).average().getAsDouble();
                    double yzhxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getYnbqzhxl).average().getAsDouble();
                    double nzhxl = equipmentInfoDay6s.stream().mapToDouble(ProEconEquipmentInfoDay6::getNnbqzhxl).average().getAsDouble();
                    ProEconPowerstationInfoDay7 proEconPowerstationInfoDay7 = new ProEconPowerstationInfoDay7();
                    proEconPowerstationInfoDay7.setId(CommonUtils.getUUID());
                    proEconPowerstationInfoDay7.setForeignKeyId(rg.getId() + "-2");
                    proEconPowerstationInfoDay7.setRecordDate(date);
                    proEconPowerstationInfoDay7.setRegionId(rg.getId());
                    proEconPowerstationInfoDay7.setLocation("rg");
                    proEconPowerstationInfoDay7.setRxtxlpr(DoubleUtils.keepPrecision(rxtxl, 2));
                    proEconPowerstationInfoDay7.setYxtxlpr(DoubleUtils.keepPrecision(yxtxl, 2));
                    proEconPowerstationInfoDay7.setNxtxlpr(DoubleUtils.keepPrecision(nxtxl, 2));
                    proEconPowerstationInfoDay7.setRlsl(DoubleUtils.keepPrecision(rlsl, 2));
                    proEconPowerstationInfoDay7.setYlsl(DoubleUtils.keepPrecision(ylsl, 2));
                    proEconPowerstationInfoDay7.setNlsl(DoubleUtils.keepPrecision(nlsl, 2));
                    proEconPowerstationInfoDay7.setRnbqzhxl(DoubleUtils.keepPrecision(rzhxl, 2));
                    proEconPowerstationInfoDay7.setYnbqzhxl(DoubleUtils.keepPrecision(yzhxl, 2));
                    proEconPowerstationInfoDay7.setNnbqzhxl(DoubleUtils.keepPrecision(nzhxl, 2));
                    rp2Resultlist.add(proEconPowerstationInfoDay7);
                }
            });
            proEconPowerstationInfoDay7Service.saveBatch(rp2Resultlist);

        });
    }

}
