package com.gyee.runeconomy.service.monitor;

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.DateUtils;
import com.gyee.common.util.DoubleUtils;
import com.gyee.common.util.SortUtils;
import com.gyee.runeconomy.dto.ProjectplanVo;
import com.gyee.runeconomy.init.CacheContext;
import com.gyee.runeconomy.model.auto.*;
import com.gyee.runeconomy.service.TokenService;
import com.gyee.runeconomy.service.auto.*;
import com.gyee.runeconomy.util.StringUtils;
import com.gyee.runeconomy.util.realtimesource.IEdosUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class ProjectPlanService {
    private static Logger logger = LoggerFactory.getLogger(MonitorService.class);
    @Resource
    private IProBasicProjectPlanService proBasicProjectPlanService;
    @Resource
    private IProBasicPowerstationPointService proBasicPowerstationPointService;
    @Resource
    private IProBasicEquipmentPointService proBasicEquipmentPointService;
    @Resource
    private IProBasicCompanyService proBasicCompanyService;
    @Resource
    private IProEconTargetdataService proEconTargetdataService;
    @Resource
    private IProEconTargetdataFristService proEconTargetdataFristService;

    @Resource
    private IProBasicPowerPointService powerPointService;

    @Resource
    private IProEconPowerstationInfoDay5Service proEconPowerstationInfoDay5Service;

    private final String QS = "0";
    private final String FD = "-1";
    private final String GF = "-2";

    @Resource
    private IEdosUtil edosUtil;
    Map<String, Object> map = new HashMap<>();
    @Resource
    private TokenService tokenService;

    //计划电量

    public List<ProjectplanVo> planvalue(String wpId, String timetype) throws Exception {

        List<ProjectplanVo> vos = new ArrayList<ProjectplanVo>();
        if (wpId.endsWith(QS) || wpId.endsWith(FD) || wpId.endsWith(GF) || CacheContext.wpmapls.containsKey(wpId)) {

            List<ProBasicPowerstation> wplist = new ArrayList<>();
            if (CacheContext.wpmapls.containsKey(wpId)) {
                wplist = CacheContext.wpmapls.get(wpId);
            }
            HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
            //            List<ProBasicPowerstation> wplsToken = tokenService.getWpls(request);
            List<ProBasicPowerstation> wplsToken = wplist;
            for (ProBasicPowerstation wp : wplsToken) {
                if (wpId.contains(FD)) {
                    if (wp.getId().endsWith("FDC_STA")) {
                        wplist.add(wp);
                    }
                } else if (wpId.contains(GF)) {
                    if (wp.getId().endsWith("GDC_STA")) {
                        wplist.add(wp);
                    }
                } else if (wpId.contains(QS)) {
                    wplist.add(wp);
                }
            }
            List<ProBasicCompany> cpls = CacheContext.cpls;
            // 获取当前日期 暂时
            Date currentDate5 = new Date();

            Date current = (Date) currentDate5;

            // 创建 Calendar 对象，并设置为当前日期
            Calendar calendar5 = Calendar.getInstance();
            calendar5.setTime(currentDate5);

            // 设置 beginDate 为前一天的早上六点
            calendar5.add(Calendar.DAY_OF_MONTH, -1);
            calendar5.set(Calendar.HOUR_OF_DAY, 6);
            calendar5.set(Calendar.MINUTE, 0);
            calendar5.set(Calendar.SECOND, 0);
            calendar5.set(Calendar.MILLISECOND, 0);
            Date beginDate = calendar5.getTime();

            // 设置 endDate 为当天的23点59分59秒
            calendar5.set(Calendar.HOUR_OF_DAY, 23);
            calendar5.set(Calendar.MINUTE, 59);
            calendar5.set(Calendar.SECOND, 59);
            calendar5.set(Calendar.MILLISECOND, 999);
            Date endDate = calendar5.getTime();


            // 获取去年最后一天的早上6点的时间
            Calendar calendar7 = Calendar.getInstance();
            calendar7.add(Calendar.YEAR, -1); // 减去1年
            calendar7.set(Calendar.MONTH, Calendar.DECEMBER); // 设置月份为12月
            calendar7.set(Calendar.DAY_OF_MONTH, 31); // 设置天数为31日
            calendar7.set(Calendar.HOUR_OF_DAY, 6); // 设置小时数为6时
            calendar7.set(Calendar.MINUTE, 0); // 设置分钟数为0分
            calendar7.set(Calendar.SECOND, 0); // 设置秒数为0秒
            // 获取开始时间
            Date qnbeginDate = calendar7.getTime();

            // 获取去年最后一天晚上23点59分59秒的时间
            Calendar calendar8 = Calendar.getInstance();
            calendar8.add(Calendar.YEAR, -1); // 减去1年
            calendar8.set(Calendar.MONTH, Calendar.DECEMBER); // 设置月份为12月
            calendar8.set(Calendar.DAY_OF_MONTH, 31); // 设置天数为31日
            calendar8.set(Calendar.HOUR_OF_DAY, 23); // 设置小时数为23时
            calendar8.set(Calendar.MINUTE, 59); // 设置分钟数为59分
            calendar8.set(Calendar.SECOND, 59); // 设置秒数为59秒
            // 获取结束时间
            Date qnendDate = calendar8.getTime();

            // 获取前年最后一天的早上6点的时间
            Calendar calendar9 = Calendar.getInstance();
            calendar9.add(Calendar.YEAR, -2); // 减去1年
            calendar9.set(Calendar.MONTH, Calendar.DECEMBER); // 设置月份为12月
            calendar9.set(Calendar.DAY_OF_MONTH, 31); // 设置天数为31日
            calendar9.set(Calendar.HOUR_OF_DAY, 6); // 设置小时数为6时
            calendar9.set(Calendar.MINUTE, 0); // 设置分钟数为0分
            calendar9.set(Calendar.SECOND, 0); // 设置秒数为0秒
            // 获取开始时间
            Date qynbeginDate = calendar9.getTime();

            // 获取去年最后一天晚上23点59分59秒的时间
            Calendar calendar10 = Calendar.getInstance();
            calendar10.add(Calendar.YEAR, -2); // 减去1年
            calendar10.set(Calendar.MONTH, Calendar.DECEMBER); // 设置月份为12月
            calendar10.set(Calendar.DAY_OF_MONTH, 31); // 设置天数为31日
            calendar10.set(Calendar.HOUR_OF_DAY, 23); // 设置小时数为23时
            calendar10.set(Calendar.MINUTE, 59); // 设置分钟数为59分
            calendar10.set(Calendar.SECOND, 59); // 设置秒数为59秒
            // 获取结束时间
            Date qynendDate = calendar10.getTime();

            Map<String, ProBasicPowerstationPoint> wtpointmap = null;
            Map<String, Map<String, ProBasicPowerstationPoint>> pointwpmap = CacheContext.wppointmap;
            Map<String, Map<String, ProBasicPowerstationPoint>> pointwpmap1 = CacheContext.companypointmap;
            Map<String, Map<String, ProBasicPowerstationPoint>> pointwpmap2 = CacheContext.regionpointmap;
            Map<String, Map<String, ProBasicPowerstationPoint>> pointwpmap3 = CacheContext.grouppointmap;

            if (pointwpmap.containsKey(wpId)) {
                wtpointmap = pointwpmap.get(wpId);
            } else if (pointwpmap1.containsKey(wpId)) {
                wtpointmap = pointwpmap1.get(wpId);
            } else if (pointwpmap2.containsKey(wpId)) {
                wtpointmap = pointwpmap2.get(wpId);
            } else if (pointwpmap3.containsKey(wpId)) {
                wtpointmap = pointwpmap3.get(wpId);
            }

            String month = wtpointmap.get(ContantXk.YFDL).getNemCode();
            String rfdl = wtpointmap.get(ContantXk.RFDL).getNemCode();
            String nfdl = wtpointmap.get(ContantXk.NFDL).getNemCode();

            if (timetype.contains("year")) {
                Calendar cal = Calendar.getInstance();
                int currentyear = cal.get(Calendar.YEAR);
                int twoyear = cal.get(Calendar.YEAR) - 1; //2021
                int threeyear = cal.get(Calendar.YEAR) - 2;


                Calendar twoyearsj = Calendar.getInstance();
                twoyearsj.add(Calendar.YEAR, -2);
                Date threeDate = twoyearsj.getTime();

                Calendar oneyearsj = Calendar.getInstance();
                oneyearsj.add(Calendar.YEAR, -1);
                Date twoDate = oneyearsj.getTime();

                Calendar currentyearsj = Calendar.getInstance();
                currentyearsj.add(Calendar.YEAR, 0);
                Date currentDate = currentyearsj.getTime();

                for (int i = 0; i < 3; i++) {
                    ProjectplanVo vo = new ProjectplanVo();
                    vo.setJhdl(0.0);
                    vo.setSjdl(0.0);
                    vo.setLyxs(0.0);
                    vo.setHours(i);
                    vos.add(vo);
                }

                double bnjhdlsum = 0.0;
                BigDecimal bnsjdlsum = BigDecimal.valueOf(0.0);

                double twojhdlsum = 0.0;
                BigDecimal twosjdlsum = BigDecimal.valueOf(0.0);

                double threejhdlsum = 0.0;
                BigDecimal threesjdlsum = BigDecimal.valueOf(0.0);

                List<ProBasicProjectPlan> currentls = proBasicProjectPlanService.getList(String.valueOf(currentyear)); //本年所有集合
                List<ProBasicProjectPlan> twols = proBasicProjectPlanService.getList(String.valueOf(twoyear));
                List<ProBasicProjectPlan> threels = proBasicProjectPlanService.getList(String.valueOf(threeyear));
                List<ProEconPowerstationInfoDay5> jn = null;
                List<ProEconPowerstationInfoDay5> qn = null;
                List<ProEconPowerstationInfoDay5> qyn = null;
                if (wpId.endsWith("0") || wpId.endsWith("-1") || wpId.endsWith("-2")) {

                    QueryWrapper<ProEconPowerstationInfoDay5> qw3 = new QueryWrapper<>();
                    QueryWrapper<ProEconPowerstationInfoDay5> qw4 = new QueryWrapper<>();
                    QueryWrapper<ProEconPowerstationInfoDay5> qw5 = new QueryWrapper<>();
                    if (wpId.equals("KGDL_FGS0")) {
                        qw3.lambda().in(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_KGDL_GFFD_ZGS0", "SXJ_KGDL_FLFD_ZGS0");
                        qw4.lambda().in(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_KGDL_GFFD_ZGS0", "SXJ_KGDL_FLFD_ZGS0");
                        qw5.lambda().in(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_KGDL_GFFD_ZGS0", "SXJ_KGDL_FLFD_ZGS0");
                    } else if (wpId.equals("KGDL_FGS-1")) {
                        qw3.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_KGDL_FLFD_ZGS0");
                        qw4.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_KGDL_FLFD_ZGS0");
                        qw5.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_KGDL_FLFD_ZGS0");
                    } else if (wpId.equals("KGDL_FGS-2")) {
                        qw3.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_KGDL_GFFD_ZGS0");
                        qw4.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_KGDL_GFFD_ZGS0");
                        qw5.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_KGDL_GFFD_ZGS0");
                    } else if (wpId.equals("SXJ_RGN0")) {
                        qw3.lambda().in(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_RGN-1", "SXJ_RGN-2");
                        qw4.lambda().in(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_RGN-1", "SXJ_RGN-2");
                        qw5.lambda().in(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_RGN-1", "SXJ_RGN-2");
                    } else if (wpId.equals("SXJ_RGN-1")) {
                        qw3.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_RGN-1");
                        qw4.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_RGN-1");
                        qw5.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_RGN-1");
                    } else if (wpId.equals("SXJ_RGN-2")) {
                        qw3.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_RGN-2");
                        qw4.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_RGN-2");
                        qw5.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_RGN-2");
                    }
                    qw3.lambda().between(ProEconPowerstationInfoDay5::getRecordDate, beginDate, endDate);
                    jn = proEconPowerstationInfoDay5Service.list(qw3);
                    qw4.lambda().between(ProEconPowerstationInfoDay5::getRecordDate, qnbeginDate, qnendDate);
                    qn = proEconPowerstationInfoDay5Service.list(qw4);
                    qw5.lambda().between(ProEconPowerstationInfoDay5::getRecordDate, qynbeginDate, qynendDate);
                    qyn = proEconPowerstationInfoDay5Service.list(qw5);
                }
                if (!wplist.isEmpty()) {
                    for (ProBasicPowerstation wpls : wplist) {

                        //本年所有集合
                        List<ProBasicProjectPlan> currentjh = currentls.stream().filter(wp -> wp.getWindpowerstationId().equals(wpls.getId())).collect(Collectors.toList());
                        //去年所有集合
                        List<ProBasicProjectPlan> twojh = twols.stream().filter(wp -> wp.getWindpowerstationId().equals(wpls.getId())).collect(Collectors.toList());
                        //前年所有集合
                        List<ProBasicProjectPlan> threejh = threels.stream().filter(wp -> wp.getWindpowerstationId().equals(wpls.getId())).collect(Collectors.toList());

                        bnjhdlsum += currentjh.stream().mapToDouble(ProBasicProjectPlan::getGeneratingCapacity).sum(); //本年计划电量合

                        twojhdlsum += twojh.stream().mapToDouble(ProBasicProjectPlan::getGeneratingCapacity).sum(); //去年计划电量合

                        threejhdlsum += threejh.stream().mapToDouble(ProBasicProjectPlan::getGeneratingCapacity).sum(); //前年计划电量合


                        if (!(wpId.endsWith("0") || wpId.endsWith("-1") || wpId.endsWith("-2"))) {
                            QueryWrapper<ProEconPowerstationInfoDay5> qw3 = new QueryWrapper<>();
                            QueryWrapper<ProEconPowerstationInfoDay5> qw2 = new QueryWrapper<>();
                            QueryWrapper<ProEconPowerstationInfoDay5> qw1 = new QueryWrapper<>();

                            qw3.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, wpId);
                            qw3.lambda().between(ProEconPowerstationInfoDay5::getRecordDate, beginDate, endDate);
                            jn = proEconPowerstationInfoDay5Service.list(qw3);

                            qw2.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, wpId);
                            qw2.lambda().between(ProEconPowerstationInfoDay5::getRecordDate, qnbeginDate, qnendDate);
                            qn = proEconPowerstationInfoDay5Service.list(qw2);

                            qw1.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, wpId);
                            qw1.lambda().between(ProEconPowerstationInfoDay5::getRecordDate, qynbeginDate, qynendDate);
                            qyn = proEconPowerstationInfoDay5Service.list(qw1);
                        }
                    }


                    //                    PointData bnsjdl = edosUtil.getHistMatrix(nfdl, currentDate.getTime() / 1000);
                    //                    PointData qnsjdl = edosUtil.getHistMatrix(nfdl, twoDate.getTime() / 1000);
                    //                    PointData threensjdl = edosUtil.getHistMatrix(nfdl, threeDate.getTime() / 1000);
                    //
                    //                    threesjdlsum += threensjdl.getPointValueInDouble();
                    //                    twosjdlsum += qnsjdl.getPointValueInDouble();
                    //                    bnsjdlsum += bnsjdl.getPointValueInDouble();

                    //                    //本年所有集合
                    //                    List<ProEconPowerstationInfoDay5> sjcurrent = jn.stream().filter(wp -> wp.getForeignKeyId().equals(wpls.getId())).collect(Collectors.toList());
                    //                    //去年所有集合
                    //                    List<ProEconPowerstationInfoDay5> sjtwo = qn.stream().filter(wp -> wp.getForeignKeyId().equals(wpls.getId())).collect(Collectors.toList());
                    //                    //前年所有集合
                    //                    List<ProEconPowerstationInfoDay5> sjthree = qyn.stream().filter(wp -> wp.getForeignKeyId().equals(wpls.getId())).collect(Collectors.toList());
                    //

                    bnsjdlsum = jn.stream().map(ProEconPowerstationInfoDay5::getNfdldb).reduce(BigDecimal.ZERO, BigDecimal::add);

                    twosjdlsum = qn.stream().map(ProEconPowerstationInfoDay5::getNfdldb).reduce(BigDecimal.ZERO, BigDecimal::add); //去年计划电量合

                    threesjdlsum = qyn.stream().map(ProEconPowerstationInfoDay5::getNfdldb).reduce(BigDecimal.ZERO, BigDecimal::add); //前年计划电量合
                    double sxzjrl = 0.0;
                    if (!wpId.contains("_STA")) {
                        for (ProBasicCompany cp : cpls) {
                            sxzjrl += (cp.getJrwindCapacity() + cp.getJrCapacity()) / 10000;
                        }
                    } else {
                        List<ProBasicPowerstation> collect = CacheContext.wpls.stream().filter(ww -> wpId.equals(ww.getId())).collect(Collectors.toList());
                        sxzjrl += (collect.get(0).getJrCapacity() + collect.get(0).getJrwindCapacity()) / 10000;
                    }

                    vos.get(0).setJhdl(StringUtils.round(bnjhdlsum, 2));
                    vos.get(1).setJhdl(StringUtils.round(twojhdlsum, 2));
                    vos.get(2).setJhdl(StringUtils.round(threejhdlsum, 2));


                    Date date = DateUtils.getCurrentDate(); //当前时间
                    PointData data = edosUtil.getSectionData(rfdl, DateUtils.getEndOfDay(date).getTime());
                    Double rfdlsj = (DoubleUtils.keepPrecision(data.getPointValueInDouble() / 10000, 2));

                    //                    double bnsjdlhj = (bnsjdlsum.doubleValue() /10000) + rfdlsj;
                    double bnsjdlhj = (bnsjdlsum.doubleValue() / 10000);
                    double qnsjdlhj = twosjdlsum.doubleValue() / 10000;
                    double qynsjdlhj = threesjdlsum.doubleValue() / 10000;

                    vos.get(0).setSjdl(StringUtils.round(bnsjdlhj, 2));
                    vos.get(1).setSjdl(StringUtils.round(qnsjdlhj, 2));
                    vos.get(2).setSjdl(StringUtils.round(qynsjdlhj, 2));

                    vos.get(0).setLyxs(StringUtils.round(bnsjdlhj / sxzjrl, 2));
                    vos.get(1).setLyxs(StringUtils.round(qnsjdlhj / sxzjrl, 2));
                    vos.get(2).setLyxs(StringUtils.round(qynsjdlhj / sxzjrl, 2));


                }
            }

            if (timetype.contains("month")) {

                if (!wplist.isEmpty()) {
                    int day = 0;
                    int month2 = DateUtils.getMonth(new Date());//4
                    DateUtils.getMonthLast(new Date());
                    Date date = DateUtils.getCurrentDate();

                    //如果是当前月，开始时间减一天，如果是其它月为最后一天
                    Date dyxtsj = null;
                    Date zqxtsj = null;
                    //当前系统时间
                    dyxtsj = DateUtils.today();
                    //当前系统时间最后一天
                    zqxtsj = DateUtils.getMonthLast(DateUtils.today());

                    List<ProEconPowerstationInfoDay5> list = null;
                    List<ProEconPowerstationInfoDay5> list1 = null;
                    List<ProEconPowerstationInfoDay5> list2 = null;
                    for (int i = month2; i > 0; i--) {
                        double bnjhdlsum = 0.0;
                        BigDecimal bnsjdlsum = BigDecimal.valueOf(0.0);
                        BigDecimal bnsjdlsum1 = BigDecimal.valueOf(0.0);
                        BigDecimal bnsjdlsum2 = BigDecimal.valueOf(0.0);
                        day = i;
                        for (ProBasicPowerstation wpls : wplist) {
                            List<ProBasicProjectPlan> currentls = proBasicProjectPlanService.getmonthList(String.valueOf(day)); //本月所有集合
                            //本年所有集合
                            List<ProBasicProjectPlan> currentjh = currentls.stream().filter(wp -> wp.getWindpowerstationId().equals(wpls.getId())).collect(Collectors.toList());
                            bnjhdlsum += currentjh.stream().mapToDouble(ProBasicProjectPlan::getGeneratingCapacity).sum(); //本月计划电量合
                        }
                        ProjectplanVo vo = new ProjectplanVo();
                        vo.setJhdl(StringUtils.round(bnjhdlsum, 2));
                        vo.setHours(i);

                        QueryWrapper<ProEconPowerstationInfoDay5> qw1 = new QueryWrapper<>();
                        QueryWrapper<ProEconPowerstationInfoDay5> qw2 = new QueryWrapper<>();
                        QueryWrapper<ProEconPowerstationInfoDay5> qw3 = new QueryWrapper<>();
                        QueryWrapper<ProEconPowerstationInfoDay5> qw4 = new QueryWrapper<>();
                        if (i == month2) {
                            qw1.lambda().lt(ProEconPowerstationInfoDay5::getRecordDate, dyxtsj)
                                    .ge(ProEconPowerstationInfoDay5::getRecordDate, DateUtils.getMonthFirst(dyxtsj));
                        } else {
                            qw1.lambda().le(ProEconPowerstationInfoDay5::getRecordDate, zqxtsj)
                                    .ge(ProEconPowerstationInfoDay5::getRecordDate, DateUtils.getMonthFirst(zqxtsj));
                        }
                        if (wpId.equals("KGDL_FGS0")) {
                            qw1.lambda().in(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_KGDL_GFFD_ZGS0", "SXJ_KGDL_FLFD_ZGS0");
                        } else if (wpId.equals("KGDL_FGS-1")) {
                            qw1.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_KGDL_FLFD_ZGS0");
                        } else if (wpId.equals("KGDL_FGS-2")) {
                            qw1.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_KGDL_GFFD_ZGS0");
                        } else if (wpId.equals("SXJ_RGN-1")) {
                            qw1.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_RGN-1");
                        } else if (wpId.equals("SXJ_RGN-2")) {
                            qw1.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_RGN-2");
                        } else {
                            qw1.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, wpId);
                        }
                        qw1.lambda().orderByDesc(ProEconPowerstationInfoDay5::getRecordDate);
                        qw1.last("LIMIT 1");
                        list = proEconPowerstationInfoDay5Service.list(qw1);

                        if (wpId.equals("SXJ_RGN0")) {
                            if (i == month2) {
                                qw2.lambda().lt(ProEconPowerstationInfoDay5::getRecordDate, dyxtsj)
                                        .ge(ProEconPowerstationInfoDay5::getRecordDate, DateUtils.getMonthFirst(dyxtsj));
                            } else {
                                qw2.lambda().le(ProEconPowerstationInfoDay5::getRecordDate, zqxtsj)
                                        .ge(ProEconPowerstationInfoDay5::getRecordDate, DateUtils.getMonthFirst(zqxtsj));
                            }

                            if (i == month2) {
                                qw3.lambda().lt(ProEconPowerstationInfoDay5::getRecordDate, dyxtsj)
                                        .ge(ProEconPowerstationInfoDay5::getRecordDate, DateUtils.getMonthFirst(dyxtsj));
                            } else {
                                qw3.lambda().le(ProEconPowerstationInfoDay5::getRecordDate, zqxtsj)
                                        .ge(ProEconPowerstationInfoDay5::getRecordDate, DateUtils.getMonthFirst(zqxtsj));
                            }
                            qw2.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_RGN-1");
                            qw2.lambda().orderByDesc(ProEconPowerstationInfoDay5::getRecordDate);
                            qw2.last("LIMIT 1");

                            qw3.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_RGN-2");
                            qw3.lambda().orderByDesc(ProEconPowerstationInfoDay5::getRecordDate);
                            qw3.last("LIMIT 1");
                            list1 = proEconPowerstationInfoDay5Service.list(qw2);
                            list2 = proEconPowerstationInfoDay5Service.list(qw3);
                        }


                        //                        PointData data = edosUtil.getSectionData(month, DateUtils.getEndOfDay(DateUtils.getMonthLast(date)).getTime());
                        //                        vo.setSjdl(DoubleUtils.keepPrecision(data.getPointValueInDouble() / 10000, 2));

                        if (wpId.equals("SXJ_RGN0")) {
                            bnsjdlsum1 = list1.stream().map(ProEconPowerstationInfoDay5::getYfdldb).reduce(BigDecimal.ZERO, BigDecimal::add);
                            bnsjdlsum2 = list2.stream().map(ProEconPowerstationInfoDay5::getYfdldb).reduce(BigDecimal.ZERO, BigDecimal::add);

                            bnsjdlsum = bnsjdlsum1.add(bnsjdlsum2);
                        } else {
                            bnsjdlsum = list.stream().map(ProEconPowerstationInfoDay5::getYfdldb).reduce(BigDecimal.ZERO, BigDecimal::add);
                        }
                        double sxzjrl = 0;
                        if (!wpId.contains("_STA")) {
                            for (ProBasicCompany cp : cpls) {
                                sxzjrl += (cp.getJrwindCapacity() + cp.getJrCapacity()) / 10000;
                            }
                        } else {
                            List<ProBasicPowerstation> collect = CacheContext.wpls.stream().filter(ww -> wpId.equals(ww.getId())).collect(Collectors.toList());
                            sxzjrl += (collect.get(0).getJrCapacity() + collect.get(0).getJrwindCapacity()) / 10000;
                        }
                        Date date1 = DateUtils.getCurrentDate(); //当前时间
                        PointData data = edosUtil.getSectionData(rfdl, DateUtils.getEndOfDay(date1).getTime());
                        Double rfdlsj = (DoubleUtils.keepPrecision(data.getPointValueInDouble() / 10000, 2));
                        if (i == month2) {
                            //                            vo.setSjdl(StringUtils.round((bnsjdlsum.doubleValue() / 10000) + rfdlsj,2));
                            vo.setSjdl(StringUtils.round(bnsjdlsum.doubleValue() / 10000, 2));
                            vo.setLyxs(StringUtils.round((bnsjdlsum.doubleValue() / 10000) / sxzjrl, 2));
                        } else {
                            vo.setSjdl(StringUtils.round(bnsjdlsum.doubleValue() / 10000, 2));
                            vo.setLyxs(StringUtils.round((bnsjdlsum.doubleValue() / 10000) / sxzjrl, 2));
                        }
                        date = DateUtils.addMonths(date, -1);
                        dyxtsj = DateUtils.addMonths(dyxtsj, -1);
                        zqxtsj = DateUtils.addMonths(zqxtsj, -1);
                        vos.add(vo);
                        SortUtils.sort(vos, "hours", SortUtils.ASC);
                    }
                }
            }
            if (timetype.contains("day")) {
                if (!wplist.isEmpty()) {
                    int day = 0;
                    Calendar cal = Calendar.getInstance();
                    int dom = cal.get(Calendar.DAY_OF_MONTH);
                    int month2 = DateUtils.getMonth(new Date());

                    Date date = DateUtils.getCurrentDate(); //当前时间

                    double byjhdlsum = 0.0;
                    double bysjdlsum = 0.0;
                    for (ProBasicPowerstation wpls : wplist) {
                        List<ProBasicProjectPlan> currentls = proBasicProjectPlanService.getmonthList(String.valueOf(month2)); //本月所有集合
                        //本年所有集合
                        List<ProBasicProjectPlan> currentjh = currentls.stream().filter(wp -> wp.getWindpowerstationId().equals(wpls.getId())).collect(Collectors.toList());
                        byjhdlsum += currentjh.stream().mapToDouble(ProBasicProjectPlan::getGeneratingCapacity).sum(); //本月计划电量合

                    }

                    List<ProEconPowerstationInfoDay5> list = null;

                    BigDecimal sjdlsum = BigDecimal.valueOf(0.0);
                    for (int i = dom; i > 0; i--) {
                        ProjectplanVo vo = new ProjectplanVo();
                        vo.setJhdl(StringUtils.round(byjhdlsum / 30, 2));
                        PointData data = edosUtil.getSectionData(rfdl, DateUtils.getEndOfDay(date).getTime());
                        QueryWrapper<ProEconPowerstationInfoDay5> qw1 = new QueryWrapper<>();
                        if (wpId.equals("KGDL_FGS0")) {
                            qw1.lambda().in(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_KGDL_GFFD_ZGS0", "SXJ_KGDL_FLFD_ZGS0");
                        } else if (wpId.equals("KGDL_FGS-1")) {
                            qw1.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_KGDL_FLFD_ZGS0");
                        } else if (wpId.equals("KGDL_FGS-2")) {
                            qw1.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_KGDL_GFFD_ZGS0");
                        } else if (wpId.equals("SXJ_RGN-1")) {
                            qw1.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_RGN-1");
                        } else if (wpId.equals("SXJ_RGN-2")) {
                            qw1.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, "SXJ_RGN-2");
                        } else {
                            qw1.lambda().eq(ProEconPowerstationInfoDay5::getForeignKeyId, wpId);
                        }
                        qw1.lambda().eq(ProEconPowerstationInfoDay5::getRecordDate, current);
                        qw1.lambda().orderByDesc(ProEconPowerstationInfoDay5::getRecordDate);
                        list = proEconPowerstationInfoDay5Service.list(qw1);
                        sjdlsum = list.stream().map(ProEconPowerstationInfoDay5::getRfdldb).reduce(BigDecimal.ZERO, BigDecimal::add);
                        //                        vo.setSjdl(DoubleUtils.keepPrecision(data.getPointValueInDouble() / 10000, 2));
                        vo.setSjdl(sjdlsum.doubleValue() / 10000);
                        date = DateUtils.addDays(date, -1);
                        current = DateUtils.addDays(current, -1);

                        vo.setHours(i);
                        vos.add(vo);
                        SortUtils.sort(vos, "hours", SortUtils.ASC);
                    }

                }
            }


        }
        return vos;

    }
}
