package com.gyee.generation.service;//package com.gyee.generation.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gyee.common.contant.ContantXk;
import com.gyee.common.model.PointData;
import com.gyee.generation.init.CacheContext;
import com.gyee.generation.model.auto.*;
import com.gyee.generation.model.vo.CompanyType;
import com.gyee.generation.model.vo.Location;
import com.gyee.generation.model.vo.WpType;
import com.gyee.generation.service.auto.IProEconPowerstationInfoDay6Service;
import com.gyee.generation.util.DateUtils;
import com.gyee.generation.util.StringUtils;
import com.gyee.generation.util.realtimesource.IEdosUtil;
import com.gyee.generation.util.statisticcs.Initial;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class WindPowerInfo6Service {

    //    private static final Logger logger = LoggerFactory.getLogger(WindPowerInfo1Service.class);
    @Resource
    private IEdosUtil edosUtil;

    @Resource
    private IProEconPowerstationInfoDay6Service proEconPowerstationInfoDay6Service;


    @Value("${initialcode}")
    private String initialcode;

    /**
     * 计算区域日信息
     */
    public void calRegionInfoDay(Date recordDate) {

        QueryWrapper<ProEconPowerstationInfoDay6> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("record_date", DateUtils.truncate(recordDate)).eq("location", Location.cp.getValue());
        List<ProEconPowerstationInfoDay6> wpinfodayls = proEconPowerstationInfoDay6Service.list(queryWrapper);
        //                .stream()
        //                .filter(i -> i.getRecordDate().compareTo(DateUtils.truncate(recordDate)) == 0
        ////                        && CacheContext.wpmap.containsKey(i.getWindpowerstationId())
        //                        && i.getLocation().equals(Location.cp.getValue()))
        //                .collect(Collectors.toList());
        if (!wpinfodayls.isEmpty()) {

            Map<String, Map<String, List<ProEconPowerstationInfoDay6>>> rgmap = new HashMap<>();


            for (ProEconPowerstationInfoDay6 wpinfo : wpinfodayls) {

                if (rgmap.containsKey(wpinfo.getRegionId())) {
                    Map<String, List<ProEconPowerstationInfoDay6>> map = rgmap.get(wpinfo.getRegionId());
                    List<ProEconPowerstationInfoDay6> qbls = map.get(CompanyType.qb.id);
                    List<ProEconPowerstationInfoDay6> fdls = map.get(CompanyType.fd.id);
                    List<ProEconPowerstationInfoDay6> gfls = map.get(CompanyType.gf.id);

                    if (wpinfo.getForeignKeyId().contains("-1")) {
                        fdls.add(wpinfo);
                    } else if (wpinfo.getForeignKeyId().contains("-2")) {
                        gfls.add(wpinfo);
                    } else if (wpinfo.getForeignKeyId().contains("0")) {
                        qbls.add(wpinfo);
                    }

                } else {
                    Map<String, List<ProEconPowerstationInfoDay6>> map = new HashMap<>();

                    List<ProEconPowerstationInfoDay6> qbls = new ArrayList<>();
                    List<ProEconPowerstationInfoDay6> gfls = new ArrayList<>();
                    List<ProEconPowerstationInfoDay6> fdls = new ArrayList<>();

                    if (wpinfo.getForeignKeyId().contains("-1")) {
                        fdls.add(wpinfo);
                    } else if (wpinfo.getForeignKeyId().contains("-2")) {
                        gfls.add(wpinfo);
                    } else if (wpinfo.getForeignKeyId().contains("0")) {
                        qbls.add(wpinfo);
                    }
                    map.put(CompanyType.qb.id, qbls);
                    map.put(CompanyType.fd.id, fdls);
                    map.put(CompanyType.gf.id, gfls);
                    rgmap.put(wpinfo.getRegionId(), map);
                }
            }


            //判断是否有重复记录，先删除重复记录
            QueryWrapper<ProEconPowerstationInfoDay6> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("record_date", DateUtils.truncate(recordDate)).eq("location", Location.rg.getValue());
            List<String> idls = proEconPowerstationInfoDay6Service.list(queryWrapper2).stream()
                    //                    .filter(i -> i.getRecordDate().compareTo(DateUtils.truncate(recordDate)) == 0
                    ////                            && CacheContext.wpmap.containsKey(i.getWindpowerstationId())
                    //                            && i.getLocation().equals(Location.rg.getValue()))
                    .map(ProEconPowerstationInfoDay6::getId)
                    .collect(Collectors.toList());

            if (idls.size() > 0) {

                proEconPowerstationInfoDay6Service.removeByIds(idls);

            }
            for (Map.Entry<String, Map<String, List<ProEconPowerstationInfoDay6>>> entry : rgmap.entrySet()) {
                Map<String, List<ProEconPowerstationInfoDay6>> map = entry.getValue();
                List<ProEconPowerstationInfoDay6> qbls = map.get(CompanyType.qb.id);
                List<ProEconPowerstationInfoDay6> fdls = map.get(CompanyType.fd.id);
                List<ProEconPowerstationInfoDay6> gfls = map.get(CompanyType.gf.id);
                //                if(CacheContext.cpwpmap.size()==qbls.size())
                //                {

                ProEconPowerstationInfoDay6 qb = new ProEconPowerstationInfoDay6();
                ProEconPowerstationInfoDay6 fd = new ProEconPowerstationInfoDay6();
                ProEconPowerstationInfoDay6 gf = new ProEconPowerstationInfoDay6();

                if (CacheContext.rgmap.containsKey(entry.getKey())) {
                    ProBasicRegion cp = CacheContext.rgmap.get(entry.getKey());
                    gf.setRegionId(cp.getId());

                    gf.setRecordDate(DateUtils.truncate(recordDate));
                    gf.setForeignKeyId(cp.getId() + "-2");
                    gf.setLocation(Location.rg.getValue());

                    fd.setRegionId(cp.getId());

                    fd.setRecordDate(DateUtils.truncate(recordDate));
                    fd.setForeignKeyId(cp.getId() + "-1");
                    fd.setLocation(Location.rg.getValue());

                    qb.setRegionId(cp.getId());

                    qb.setRecordDate(DateUtils.truncate(recordDate));
                    qb.setForeignKeyId(cp.getId() + "0");
                    qb.setLocation(Location.rg.getValue());

                    Initial.initial(qb);
                    Initial.initial(fd);
                    Initial.initial(gf);
                    //计算区域级全部
                    calCp(qb, qbls);
                    //计算公司级风电场站
                    calCp(fd, fdls);
                    //计算公司级光电场站
                    calCp(gf, gfls);

                    proEconPowerstationInfoDay6Service.save(qb);
                    proEconPowerstationInfoDay6Service.save(fd);
                    proEconPowerstationInfoDay6Service.save(gf);
                }


                //                }else
                //                {
                //                    logger.debug("公司所属场站数量与保存的场站日信息数量不一致，未进行{0}统计-------结束", entry.getKey());
                //
                //                }


            }
        }

    }

    /**
     * 计算公司日信息
     */
    public void calCompanyInfoDay(Date recordDate) {
        QueryWrapper<ProEconPowerstationInfoDay6> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("record_date", DateUtils.truncate(recordDate)).eq("location", Location.wp.getValue());
        List<ProEconPowerstationInfoDay6> wpinfodayls = proEconPowerstationInfoDay6Service.list(queryWrapper);
        //        .stream()
        //                .filter(i -> i.getRecordDate().compareTo(DateUtils.truncate(recordDate)) == 0
        ////                        && CacheContext.wpmap.containsKey(i.getWindpowerstationId())
        //                        && i.getLocation().equals(Location.wp.getValue()))
        //                .collect(Collectors.toList());
        if (!wpinfodayls.isEmpty()) {

            Map<String, Map<String, List<ProEconPowerstationInfoDay6>>> cpmap = new HashMap<>();


            for (ProEconPowerstationInfoDay6 wpinfo : wpinfodayls) {

                if (cpmap.containsKey(wpinfo.getCompanyId())) {
                    Map<String, List<ProEconPowerstationInfoDay6>> map = cpmap.get(wpinfo.getCompanyId());
                    List<ProEconPowerstationInfoDay6> qbls = map.get(CompanyType.qb.id);
                    List<ProEconPowerstationInfoDay6> fdls = map.get(CompanyType.fd.id);
                    List<ProEconPowerstationInfoDay6> gfls = map.get(CompanyType.gf.id);

                    if (wpinfo.getWindpowerstationId().contains(WpType.FDC.id)) {
                        fdls.add(wpinfo);
                    } else {
                        gfls.add(wpinfo);
                    }
                    qbls.add(wpinfo);
                } else {
                    Map<String, List<ProEconPowerstationInfoDay6>> map = new HashMap<>();

                    List<ProEconPowerstationInfoDay6> qbls = new ArrayList<>();
                    List<ProEconPowerstationInfoDay6> gfls = new ArrayList<>();
                    List<ProEconPowerstationInfoDay6> fdls = new ArrayList<>();

                    if (wpinfo.getWindpowerstationId().contains(WpType.FDC.id)) {
                        fdls.add(wpinfo);
                    } else {
                        gfls.add(wpinfo);
                    }
                    qbls.add(wpinfo);
                    map.put(CompanyType.qb.id, qbls);
                    map.put(CompanyType.fd.id, fdls);
                    map.put(CompanyType.gf.id, gfls);
                    cpmap.put(wpinfo.getCompanyId(), map);
                }
            }


            //判断是否有重复记录，先删除重复记录
            QueryWrapper<ProEconPowerstationInfoDay6> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("record_date", DateUtils.truncate(recordDate)).eq("location", Location.cp.getValue());
            List<String> idls = proEconPowerstationInfoDay6Service.list(queryWrapper2).stream()
                    //                    .filter(i -> i.getRecordDate().compareTo(DateUtils.truncate(recordDate)) == 0
                    ////                            && CacheContext.wpmap.containsKey(i.getWindpowerstationId())
                    //                            && i.getLocation().equals(Location.cp.getValue()))
                    .map(ProEconPowerstationInfoDay6::getId)
                    .collect(Collectors.toList());

            if (idls.size() > 0) {

                proEconPowerstationInfoDay6Service.removeByIds(idls);

            }
            for (Map.Entry<String, Map<String, List<ProEconPowerstationInfoDay6>>> entry : cpmap.entrySet()) {
                Map<String, List<ProEconPowerstationInfoDay6>> map = entry.getValue();
                List<ProEconPowerstationInfoDay6> qbls = map.get(CompanyType.qb.id);
                List<ProEconPowerstationInfoDay6> fdls = map.get(CompanyType.fd.id);
                List<ProEconPowerstationInfoDay6> gfls = map.get(CompanyType.gf.id);
                //                if(CacheContext.cpwpmap.size()==qbls.size())
                //                {

                ProEconPowerstationInfoDay6 qb = new ProEconPowerstationInfoDay6();
                ProEconPowerstationInfoDay6 fd = new ProEconPowerstationInfoDay6();
                ProEconPowerstationInfoDay6 gf = new ProEconPowerstationInfoDay6();

                if (CacheContext.cpmap.containsKey(entry.getKey())) {
                    ProBasicCompany cp = CacheContext.cpmap.get(entry.getKey());
                    gf.setRegionId(cp.getRegionId());
                    gf.setCompanyId(cp.getId());
                    gf.setRecordDate(DateUtils.truncate(recordDate));
                    gf.setForeignKeyId(cp.getId() + "-2");
                    gf.setLocation(Location.cp.getValue());


                    fd.setRegionId(cp.getRegionId());
                    fd.setCompanyId(cp.getId());
                    fd.setRecordDate(DateUtils.truncate(recordDate));
                    fd.setForeignKeyId(cp.getId() + "-1");
                    fd.setLocation(Location.cp.getValue());

                    qb.setRegionId(cp.getRegionId());
                    qb.setCompanyId(cp.getId());
                    qb.setRecordDate(DateUtils.truncate(recordDate));
                    qb.setForeignKeyId(cp.getId() + "0");
                    qb.setLocation(Location.cp.getValue());

                    Initial.initial(qb);
                    Initial.initial(fd);
                    Initial.initial(gf);
                    //计算公司级全部场站
                    calCp(qb, qbls);
                    //计算公司级风电场站
                    calCp(fd, fdls);
                    //计算公司级光电场站
                    calCp(gf, gfls);

                    proEconPowerstationInfoDay6Service.save(qb);
                    proEconPowerstationInfoDay6Service.save(fd);
                    proEconPowerstationInfoDay6Service.save(gf);
                }


                //                }else
                //                {
                //                    logger.debug("公司所属场站数量与保存的场站日信息数量不一致，未进行{0}统计-------结束", entry.getKey());
                //
                //                }


            }
        }

    }

    private void calCp(ProEconPowerstationInfoDay6 pewp, List<ProEconPowerstationInfoDay6> ls) {

        if (!ls.isEmpty()) {
            //日发电量
            //            DoubleSummaryStatistics summaryStatistics=ls.stream().mapToDouble(ProEconPowerstationInfoDay1::getRfdl).summaryStatistics();
            //            pewp.setRfdl(com.gyee.common.model.StringUtils.round(summaryStatistics.getSum(),2));


            //* 日最大风速（测风塔）
            DoubleSummaryStatistics summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay6::getRzdfscft).summaryStatistics();
            pewp.setRzdfscft(com.gyee.common.model.StringUtils.round(summaryStatistics.getMax(), 2));

            // * 日最小风速（测风塔）
            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay6::getRzxfscft).summaryStatistics();
            pewp.setRzxfscft(com.gyee.common.model.StringUtils.round(summaryStatistics.getMin(), 2));

            // * 日最大功率（出线）
            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay6::getRzdglcx).summaryStatistics();
            pewp.setRzdglcx(com.gyee.common.model.StringUtils.round(summaryStatistics.getMax(), 2));

            // * 日最小功率（出线）
            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay6::getRzxglcx).summaryStatistics();
            pewp.setRzxglcx(com.gyee.common.model.StringUtils.round(summaryStatistics.getMin(), 2));

            // * 日平均功率（出线）
            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay6::getRpjglcx).summaryStatistics();
            pewp.setRpjglcx(com.gyee.common.model.StringUtils.round(summaryStatistics.getAverage(), 2));

            // * 日平均风速（测风塔）
            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay6::getRpjfscft).summaryStatistics();
            pewp.setRpjfscft(com.gyee.common.model.StringUtils.round(summaryStatistics.getAverage(), 2));

            // * 月最大风速（测风塔）
            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay6::getYzdfscft).summaryStatistics();
            pewp.setYzdfscft(com.gyee.common.model.StringUtils.round(summaryStatistics.getMax(), 2));

            // * 月最小风速（测风塔）
            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay6::getYzxfscft).summaryStatistics();
            pewp.setYzxfscft(com.gyee.common.model.StringUtils.round(summaryStatistics.getMin(), 2));

            // * 月最大功率（出线）
            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay6::getYzdglcx).summaryStatistics();
            pewp.setYzdglcx(com.gyee.common.model.StringUtils.round(summaryStatistics.getMax(), 2));

            // * 月最小功率（出线）
            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay6::getYzxglcx).summaryStatistics();
            pewp.setYzxglcx(com.gyee.common.model.StringUtils.round(summaryStatistics.getMin(), 2));

            //  * 月平均功率（出线）
            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay6::getYpjglcx).summaryStatistics();
            pewp.setYpjglcx(com.gyee.common.model.StringUtils.round(summaryStatistics.getAverage(), 2));

            // * 月平均风速（测风塔）
            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay6::getYpjfscft).summaryStatistics();
            pewp.setYpjfscft(com.gyee.common.model.StringUtils.round(summaryStatistics.getAverage(), 2));

            // * 年最大风速（测风塔）
            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay6::getNzdfscft).summaryStatistics();
            pewp.setNzdfscft(com.gyee.common.model.StringUtils.round(summaryStatistics.getMax(), 2));

            // * 年最小风速（测风塔）
            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay6::getNzxfscft).summaryStatistics();
            pewp.setNzxfscft(com.gyee.common.model.StringUtils.round(summaryStatistics.getMin(), 2));

            // * 年最大功率（出线）
            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay6::getNzdglcx).summaryStatistics();
            pewp.setNzdglcx(com.gyee.common.model.StringUtils.round(summaryStatistics.getMax(), 2));

            // * 年最小功率（出线）
            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay6::getNzxglcx).summaryStatistics();
            pewp.setNzxglcx(com.gyee.common.model.StringUtils.round(summaryStatistics.getMin(), 2));

            // * 年平均功率（出线）
            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay6::getNpjglcx).summaryStatistics();
            pewp.setNpjglcx(com.gyee.common.model.StringUtils.round(summaryStatistics.getAverage(), 2));

            // * 年平均风速（测风塔）
            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay6::getNpjfscft).summaryStatistics();
            pewp.setNpjfscft(com.gyee.common.model.StringUtils.round(summaryStatistics.getAverage(), 2));

        }
    }

    /**
     * 计算场站日信息
     */
    public void calWindpowerInfoDay(Date recordDate) throws Exception {
        Calendar c = Calendar.getInstance();
        c.setTime(recordDate);

        Date end = c.getTime();

        Date begin = DateUtils.truncate(c.getTime());


        //        List<ProEconPowerstationInfoDay6> wpinfodayls=new ArrayList<>();


        List<String> wpls = new ArrayList<>();
        List<ProEconPowerstationInfoDay6> dayls = new ArrayList<>();
        for (ProBasicPowerstation wp : CacheContext.wpls) {
            if (CacheContext.wppointmap.containsKey(wp.getId())) {
                Map<String, ProBasicPowerstationPoint> pointmap = CacheContext.wppointmap.get(wp.getId());

                ProEconPowerstationInfoDay6 pewp = new ProEconPowerstationInfoDay6();
                Initial.initial(pewp);
                pewp.setRegionId(wp.getRegionId());
                pewp.setCompanyId(wp.getCompanyId());
                pewp.setRecordDate(DateUtils.truncate(recordDate));
                pewp.setForeignKeyId(wp.getId());
                pewp.setWindpowerstationId(wp.getId());
                pewp.setLocation(Location.wp.getValue());

                //昨日的统计结果
                List<ProEconPowerstationInfoDay6> pepid1ls = new ArrayList<>();
                Calendar cl = Calendar.getInstance();
                cl.setTime(recordDate);
                if (cl.get(Calendar.DAY_OF_MONTH) != 1) {
                    QueryWrapper<ProEconPowerstationInfoDay6> queryWrapper2 = new QueryWrapper<>();
                    queryWrapper2.eq("record_date", DateUtils.truncate(cl.getTime()))
                            .eq("windPowerStation_Id", wp.getId())
                            .eq("location", Location.wp.getValue());
                    pepid1ls = proEconPowerstationInfoDay6Service.list(queryWrapper2);
                    //                            .stream()
                    //                            .filter(i -> i.getRecordDate().compareTo(DateUtils.truncate(cl.getTime())) == 0
                    //                                    && i.getLocation().equals(Location.wp.getValue())
                    //                                    && i.getWindpowerstationId().equals(wp.getId())
                    //                                  )
                    //                            .collect(Collectors.toList());
                }
                extracted(end, begin, pointmap, pewp, pepid1ls);
                //*******************************************年信息统计*********************************************************/
                //                wpinfodayls.add(pewp);
                dayls.add(pewp);
                // proEconPowerstationInfoDay6Service.save(pewp);
                wpls.add(wp.getId());
            }


        }

        //判断是否有重复记录，先删除重复记录
        QueryWrapper<ProEconPowerstationInfoDay6> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("record_date", DateUtils.truncate(recordDate))
                .in("windpowerstation_id", wpls)
                .eq("location", Location.wp.getValue());
        List<String> idls = proEconPowerstationInfoDay6Service.list(queryWrapper).stream()
                //                .filter(i -> i.getRecordDate().compareTo(DateUtils.truncate(recordDate)) == 0
                //                        && CacheContext.wpmap.containsKey(i.getWindpowerstationId())
                //                        && i.getLocation().equals(Location.wp.getValue()))
                .map(ProEconPowerstationInfoDay6::getId)
                .collect(Collectors.toList());

        if (idls.size() > 0) {

            proEconPowerstationInfoDay6Service.removeByIds(idls);

        }
        proEconPowerstationInfoDay6Service.saveBatch(dayls);
    }

    /**
     * 计算项目日信息
     */
    public void calProjectInfoDay(Date recordDate) throws Exception {
        Calendar c = Calendar.getInstance();
        c.setTime(recordDate);

        Date end = c.getTime();

        Date begin = DateUtils.truncate(c.getTime());

        List<String> pjls = new ArrayList<>();
        List<ProEconPowerstationInfoDay6> dayls = new ArrayList<>();
        for (ProBasicProject pj : CacheContext.pjls) {
            if (CacheContext.propointmap.containsKey(pj.getId())) {
                Map<String, ProBasicPowerstationPoint> pointmap = CacheContext.propointmap.get(pj.getId());

                ProEconPowerstationInfoDay6 pewp = new ProEconPowerstationInfoDay6();
                if (CacheContext.wpmap.containsKey(pj.getWindpowerstationId())) {
                    ProBasicPowerstation wp = CacheContext.wpmap.get(pj.getWindpowerstationId());
                    pewp.setRegionId(wp.getRegionId());
                    pewp.setCompanyId(wp.getCompanyId());
                }
                Initial.initial(pewp);
                pewp.setForeignKeyId(pj.getId());
                pewp.setWindpowerstationId(pj.getWindpowerstationId());
                pewp.setProjectId(pj.getId());
                pewp.setLocation(Location.pj.getValue());
                pewp.setRecordDate(DateUtils.truncate(recordDate));

                //昨日的统计结果
                List<ProEconPowerstationInfoDay6> pepid1ls = new ArrayList<>();
                Calendar cl = Calendar.getInstance();
                cl.setTime(recordDate);
                if (cl.get(Calendar.DAY_OF_MONTH) != 1) {
                    QueryWrapper<ProEconPowerstationInfoDay6> queryWrapper2 = new QueryWrapper<>();
                    queryWrapper2.eq("record_date", DateUtils.truncate(cl.getTime()))
                            .eq("project_Id", pj.getId())
                            .eq("location", Location.pj.getValue());
                    pepid1ls = proEconPowerstationInfoDay6Service.list(queryWrapper2);
                    //                    .stream()
                    //                            .filter(i -> i.getRecordDate().compareTo(DateUtils.truncate(cl.getTime())) == 0
                    //                                    && i.getLocation().equals(Location.pj.getValue())
                    //                                    && i.getProjectId().equals(pj.getId())
                    //
                    //                            )
                    //                            .collect(Collectors.toList());
                }
                extracted(end, begin, pointmap, pewp, pepid1ls);


                dayls.add(pewp);
                // proEconPowerstationInfoDay6Service.save(pewp);
                pjls.add(pj.getWindpowerstationId());
            }


        }

        //判断是否有重复记录，先删除重复记录
        QueryWrapper<ProEconPowerstationInfoDay6> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("record_date", DateUtils.truncate(recordDate))
                .in("windpowerstation_id", pjls)
                .eq("location", Location.pj.getValue());
        List<String> idls = proEconPowerstationInfoDay6Service.list(queryWrapper).stream()
                //                .filter(i -> i.getRecordDate().compareTo(DateUtils.truncate(recordDate)) == 0
                //                        && CacheContext.pjmap.containsKey(i.getProjectId())
                //                        && i.getLocation().equals(Location.pj.getValue()))
                .map(ProEconPowerstationInfoDay6::getId)
                .collect(Collectors.toList());

        if (idls.size() > 0) {

            proEconPowerstationInfoDay6Service.removeByIds(idls);

        }
        proEconPowerstationInfoDay6Service.saveBatch(dayls);
    }

    /**
     * 计算线路日信息
     */
    public void calLineInfoDay(Date recordDate) throws Exception {
        Calendar c = Calendar.getInstance();
        c.setTime(recordDate);

        Date end = c.getTime();

        Date begin = DateUtils.truncate(c.getTime());

        List<String> lnls = new ArrayList<>();
        List<ProEconPowerstationInfoDay6> dayls = new ArrayList<>();
        for (ProBasicLine ln : CacheContext.lnls) {
            if (CacheContext.linepointmap.containsKey(ln.getId())) {
                Map<String, ProBasicPowerstationPoint> pointmap = CacheContext.linepointmap.get(ln.getId());

                ProEconPowerstationInfoDay6 pewp = new ProEconPowerstationInfoDay6();
                if (CacheContext.pjmap.containsKey(ln.getProjectId())) {
                    ProBasicProject pj = CacheContext.pjmap.get(ln.getProjectId());
                    if (CacheContext.wpmap.containsKey(pj.getWindpowerstationId())) {
                        ProBasicPowerstation wp = CacheContext.wpmap.get(pj.getWindpowerstationId());
                        pewp.setRegionId(wp.getRegionId());
                        pewp.setCompanyId(wp.getCompanyId());
                        pewp.setWindpowerstationId(wp.getId());
                    }

                }
                Initial.initial(pewp);
                pewp.setForeignKeyId(ln.getId());
                pewp.setLineId(ln.getId());
                pewp.setLocation(Location.ln.getValue());
                pewp.setProjectId(ln.getProjectId());
                pewp.setRecordDate(DateUtils.truncate(recordDate));

                //昨日的统计结果
                List<ProEconPowerstationInfoDay6> pepid1ls = new ArrayList<>();
                Calendar cl = Calendar.getInstance();
                cl.setTime(recordDate);
                if (cl.get(Calendar.DAY_OF_MONTH) != 1) {
                    QueryWrapper<ProEconPowerstationInfoDay6> queryWrapper2 = new QueryWrapper<>();
                    queryWrapper2.eq("record_date", DateUtils.truncate(cl.getTime()))
                            .eq("line_Id", ln.getId())
                            .eq("location", Location.ln.getValue());
                    pepid1ls = proEconPowerstationInfoDay6Service.list(queryWrapper2);
                    //                            .stream()
                    //                            .filter(i -> i.getRecordDate().compareTo(DateUtils.truncate(cl.getTime())) == 0
                    //                                    && i.getLocation().equals(Location.ln.getValue())
                    //                                    && i.getLineId().equals(ln.getId())
                    //
                    //                            ).collect(Collectors.toList());
                }
                extracted(end, begin, pointmap, pewp, pepid1ls);

                dayls.add(pewp);
                // proEconPowerstationInfoDay6Service.save(pewp);
                if (CacheContext.pjmap.containsKey(ln.getProjectId())) {
                    lnls.add(CacheContext.pjmap.get(ln.getProjectId()).getWindpowerstationId());
                }
            }


        }

        //判断是否有重复记录，先删除重复记录
        QueryWrapper<ProEconPowerstationInfoDay6> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("record_date", DateUtils.truncate(recordDate))
                .in("windpowerstation_id", lnls)
                .eq("location", Location.ln.getValue());
        List<String> idls = proEconPowerstationInfoDay6Service.list(queryWrapper).stream()
                //                .filter(i -> i.getRecordDate().compareTo(DateUtils.truncate(recordDate)) == 0
                //                        && CacheContext.lnmap.containsKey(i.getLineId())
                //                        && i.getLocation().equals(Location.ln.getValue()))
                .map(ProEconPowerstationInfoDay6::getId)
                .collect(Collectors.toList());

        if (idls.size() > 0) {

            proEconPowerstationInfoDay6Service.removeByIds(idls);

        }
        proEconPowerstationInfoDay6Service.saveBatch(dayls);
    }

    private void extracted(Date end, Date begin, Map<String, ProBasicPowerstationPoint> pointmap, ProEconPowerstationInfoDay6 pewp, List<ProEconPowerstationInfoDay6> pepid1ls) throws Exception {
        //*******************************************日信息统计*********************************************************/
        if (pointmap.containsKey(ContantXk.FCCFTFS70)) {

            ProBasicPowerstationPoint point = pointmap.get(ContantXk.FCCFTFS70);
            if (StringUtils.notEmp(point.getNemCode()) && !point.getNemCode().equals(initialcode)) {
                List<PointData> pointls = edosUtil.getHistoryDatasSnap(point.getNemCode(), begin.getTime() / 1000, end.getTime() / 1000);
                if (!pointls.isEmpty()) {
                    DoubleSummaryStatistics summaryStatistics = pointls.stream().mapToDouble(PointData::getPointValueInDouble).summaryStatistics();
                    //日最大风速
                    pewp.setRzdfscft(summaryStatistics.getMax());
                    //日最小风速
                    pewp.setRzxfscft(summaryStatistics.getMin());
                    //日平均风速
                    pewp.setRpjfscft(summaryStatistics.getAverage());
                }
            } else {
                if (pointmap.containsKey(ContantXk.SSPJFS)) {
                    point = pointmap.get(ContantXk.SSPJFS);
                    List<PointData> pointls = edosUtil.getHistoryDatasSnap(point.getNemCode(), begin.getTime() / 1000, end.getTime() / 1000);
                    if (!pointls.isEmpty()) {
                        DoubleSummaryStatistics summaryStatistics = pointls.stream().mapToDouble(PointData::getPointValueInDouble).summaryStatistics();
                        //日最大风速
                        pewp.setRzdfscft(summaryStatistics.getMax());
                        //日最小风速
                        pewp.setRzxfscft(summaryStatistics.getMin());
                        //日平均风速
                        pewp.setRpjfscft(summaryStatistics.getAverage());
                    }

                }
            }


        } else if (pointmap.containsKey(ContantXk.SSPJFS)) {
            ProBasicPowerstationPoint point = pointmap.get(ContantXk.SSPJFS);

            List<PointData> pointls = edosUtil.getHistoryDatasSnap(point.getNemCode(), begin.getTime() / 1000, end.getTime() / 1000);
            if (!pointls.isEmpty()) {
                DoubleSummaryStatistics summaryStatistics = pointls.stream().mapToDouble(PointData::getPointValueInDouble).summaryStatistics();
                //日最大风速
                pewp.setRzdfscft(summaryStatistics.getMax());
                //日最小风速
                pewp.setRzxfscft(summaryStatistics.getMin());
                //日平均风速
                pewp.setRpjfscft(summaryStatistics.getAverage());
            }

        }


        if (pointmap.containsKey(ContantXk.SSZGL)) {
            ProBasicPowerstationPoint point = pointmap.get(ContantXk.SSZGL);
            List<PointData> pointls = edosUtil.getHistoryDatasSnap(point.getNemCode(), begin.getTime() / 1000, end.getTime() / 1000);
            if (!pointls.isEmpty()) {
                DoubleSummaryStatistics summaryStatistics = pointls.stream().mapToDouble(PointData::getPointValueInDouble).summaryStatistics();
                //日最大功率
                pewp.setRzdglcx(summaryStatistics.getMax());
                //日最小功率
                pewp.setRzxglcx(summaryStatistics.getMin());
                //日平均功率
                pewp.setRpjglcx(summaryStatistics.getAverage());
            }
        }


        //*******************************************日信息统计*********************************************************/

        //*******************************************月信息统计*********************************************************/

        if (pepid1ls.isEmpty()) {
            setPowerandSpeedMonth(pewp);
        } else {


            ProEconPowerstationInfoDay6 pepid = pepid1ls.get(0);

            //如果昨日大于今日最大功率
            if (pepid.getYzdglcx() > pewp.getRzdglcx()) {
                pewp.setYzdglcx(pepid.getYzdglcx());
            } else {
                pewp.setYzdglcx(pewp.getRzdglcx());
            }
            //如果昨日大于今日最大风速
            if (pepid.getYzdfscft() > pewp.getRzdfscft()) {
                pewp.setYzdfscft(pepid.getYzdfscft());
            } else {
                pewp.setYzdfscft(pewp.getRzdfscft());
            }

            //如果昨日小于今日最小功率
            if (pepid.getYzxglcx() < pewp.getRzxglcx()) {
                pewp.setYzxglcx(pepid.getYzxglcx());
            } else {
                pewp.setYzxglcx(pewp.getRzxglcx());
            }
            //如果昨日小于今日最小风速
            if (pepid.getYzxfscft() < pewp.getRzxfscft()) {
                pewp.setYzxfscft(pepid.getYzxfscft());
            } else {
                pewp.setYzxfscft(pewp.getRzxfscft());
            }
            double pjfs = BigDecimal.valueOf(pewp.getYpjfscft() + pepid.getRpjfscft()).divide(new BigDecimal(2), 4, RoundingMode.HALF_EVEN).doubleValue();

            pewp.setYzxfscft(pjfs);

            double pjgl = BigDecimal.valueOf(pewp.getYpjglcx() + pepid.getRpjglcx()).divide(new BigDecimal(2), 4, RoundingMode.HALF_EVEN).doubleValue();
            pewp.setYzxglcx(pjgl);

        }


        //*******************************************月信息统计*********************************************************/

        //*******************************************年信息统计*********************************************************/
        if (pepid1ls.isEmpty()) {
            setPowerandSpeedYear(pewp);
        } else {

            ProEconPowerstationInfoDay6 pepid = pepid1ls.get(0);

            //如果昨年大于今年最大功率
            if (pepid.getNzdglcx() > pewp.getRzdglcx()) {
                pewp.setNzdglcx(pepid.getNzdglcx());
            } else {
                pewp.setNzdglcx(pewp.getRzdglcx());
            }
            //如果昨年大于今年最大风速
            if (pepid.getNzdfscft() > pewp.getRzdfscft()) {
                pewp.setNzdfscft(pepid.getNzdfscft());
            } else {
                pewp.setNzdfscft(pewp.getRzdfscft());
            }

            //如果昨年小于今年最小功率
            if (pepid.getNzxglcx() < pewp.getRzxglcx()) {
                pewp.setNzxglcx(pepid.getNzxglcx());
            } else {
                pewp.setNzxglcx(pewp.getRzxglcx());
            }
            //如果昨年小于今年最小风速
            if (pepid.getNzxfscft() < pewp.getRzxfscft()) {
                pewp.setNzxfscft(pepid.getNzxfscft());
            } else {
                pewp.setNzxfscft(pewp.getRzxfscft());
            }
            double pjfs = BigDecimal.valueOf(pewp.getNpjfscft() + pepid.getRpjfscft()).divide(new BigDecimal(2), 4, RoundingMode.HALF_EVEN).doubleValue();

            pewp.setNzxfscft(pjfs);

            double pjgl = BigDecimal.valueOf(pewp.getNpjglcx() + pepid.getRpjglcx()).divide(new BigDecimal(2), 4, RoundingMode.HALF_EVEN).doubleValue();
            pewp.setNzxglcx(pjgl);

        }
    }

    private void setPowerandSpeedYear(ProEconPowerstationInfoDay6 pewp) {
        //年最大风速
        pewp.setNzdfscft(pewp.getRzdfscft());
        //年最小风速
        pewp.setNzxfscft(pewp.getRzxfscft());
        //年平均风速
        pewp.setNpjfscft(pewp.getRpjfscft());

        //年最大功率
        pewp.setNzdglcx(pewp.getRzdglcx());
        //年最小功率
        pewp.setNzxglcx(pewp.getRzxglcx());
        //年平均功率
        pewp.setNpjglcx(pewp.getRpjglcx());

    }

    private void setPowerandSpeedMonth(ProEconPowerstationInfoDay6 pewp) {

        //月最大风速
        pewp.setYzdfscft(pewp.getRzdfscft());
        //月最小风速
        pewp.setYzxfscft(pewp.getRzxfscft());
        //月平均风速
        pewp.setYpjfscft(pewp.getRpjfscft());

        //月最大功率
        pewp.setYzdglcx(pewp.getRzdglcx());
        //月最小功率
        pewp.setYzxglcx(pewp.getRzxglcx());
        //月平均功率
        pewp.setYpjglcx(pewp.getRpjglcx());
    }


}







