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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gyee.common.model.StringUtils;
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.FaultLiminatedefectsVo;
import com.gyee.generation.model.vo.Location;
import com.gyee.generation.model.vo.WpType;
import com.gyee.generation.service.auto.*;
import com.gyee.generation.util.DateUtils;
import com.gyee.generation.util.statisticcs.Initial;
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 WindPowerInfo4Service {

    //    private static final Logger logger = LoggerFactory.getLogger(WindPowerInfo2Service.class);


    @Resource
    private IProEconPowerstationInfoDay2Service proEconPowerstationInfoDay2Service;
    @Resource
    private IProEconPowerstationInfoDay3Service proEconPowerstationInfoDay3Service;
    @Resource
    private IProEconPowerstationInfoDay4Service proEconPowerstationInfoDay4Service;

    @Resource
    private IProEconEquipmentInfoDay5Service proEconEquipmentInfoDay5Service;

    @Resource
    private IProEconFaultLiminatedefectsService proEconFaultLiminatedefectsService;


    /**
     * 计算区域日信息
     */
    public void calRegionInfoDay(Date recordDate) {
        Calendar c = Calendar.getInstance();
        c.setTime(recordDate);
        if (c.get(Calendar.HOUR_OF_DAY) == 0 && c.get(Calendar.MINUTE) == 0) {
            c.add(Calendar.DAY_OF_MONTH, -1);
        }


        recordDate = DateUtils.truncate(c.getTime());
        QueryWrapper<ProEconPowerstationInfoDay4> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("record_date", DateUtils.truncate(recordDate)).eq("location", Location.cp.getValue());
        List<ProEconPowerstationInfoDay4> wpinfodayls = proEconPowerstationInfoDay4Service.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<ProEconPowerstationInfoDay4>>> rgmap = new HashMap<>();


            for (ProEconPowerstationInfoDay4 wpinfo : wpinfodayls) {

                if (rgmap.containsKey(wpinfo.getRegionId())) {
                    Map<String, List<ProEconPowerstationInfoDay4>> map = rgmap.get(wpinfo.getRegionId());
                    List<ProEconPowerstationInfoDay4> qbls = map.get(CompanyType.qb.id);
                    List<ProEconPowerstationInfoDay4> fdls = map.get(CompanyType.fd.id);
                    List<ProEconPowerstationInfoDay4> 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<ProEconPowerstationInfoDay4>> map = new HashMap<>();

                    List<ProEconPowerstationInfoDay4> qbls = new ArrayList<>();
                    List<ProEconPowerstationInfoDay4> gfls = new ArrayList<>();
                    List<ProEconPowerstationInfoDay4> 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<ProEconPowerstationInfoDay4> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("record_date", DateUtils.truncate(recordDate)).eq("location", Location.rg.getValue());
            List<String> idls = proEconPowerstationInfoDay4Service.list(queryWrapper2).stream()
                    //                    .filter(i -> i.getRecordDate().compareTo(DateUtils.truncate(recordDate)) == 0
                    ////                            && CacheContext.wpmap.containsKey(i.getWindpowerstationId())
                    //                            && i.getLocation().equals(Location.rg.getValue()))
                    .map(ProEconPowerstationInfoDay4::getId)
                    .collect(Collectors.toList());

            if (idls.size() > 0) {

                proEconPowerstationInfoDay4Service.removeByIds(idls);

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

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

                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);

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


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


            }

        }

    }

    /**
     * 计算公司日信息
     */
    public void calCompanyInfoDay(Date recordDate) {
        Calendar c = Calendar.getInstance();
        c.setTime(recordDate);
        if (c.get(Calendar.HOUR_OF_DAY) == 0 && c.get(Calendar.MINUTE) == 0) {
            c.add(Calendar.DAY_OF_MONTH, -1);
        }


        recordDate = DateUtils.truncate(c.getTime());

        QueryWrapper<ProEconPowerstationInfoDay4> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("record_date", DateUtils.truncate(recordDate)).eq("location", Location.wp.getValue());
        List<ProEconPowerstationInfoDay4> wpinfodayls = proEconPowerstationInfoDay4Service.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<ProEconPowerstationInfoDay4>>> cpmap = new HashMap<>();


            for (ProEconPowerstationInfoDay4 wpinfo : wpinfodayls) {

                if (cpmap.containsKey(wpinfo.getCompanyId())) {
                    Map<String, List<ProEconPowerstationInfoDay4>> map = cpmap.get(wpinfo.getCompanyId());
                    List<ProEconPowerstationInfoDay4> qbls = map.get(CompanyType.qb.id);
                    List<ProEconPowerstationInfoDay4> fdls = map.get(CompanyType.fd.id);
                    List<ProEconPowerstationInfoDay4> 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<ProEconPowerstationInfoDay4>> map = new HashMap<>();

                    List<ProEconPowerstationInfoDay4> qbls = new ArrayList<>();
                    List<ProEconPowerstationInfoDay4> gfls = new ArrayList<>();
                    List<ProEconPowerstationInfoDay4> 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<ProEconPowerstationInfoDay4> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("record_date", DateUtils.truncate(recordDate)).eq("location", Location.cp.getValue());
            List<String> idls = proEconPowerstationInfoDay4Service.list(queryWrapper2).stream()
                    //                    .filter(i -> i.getRecordDate().compareTo(DateUtils.truncate(recordDate)) == 0
                    ////                            && CacheContext.wpmap.containsKey(i.getWindpowerstationId())
                    //                            && i.getLocation().equals(Location.cp.getValue()))
                    .map(ProEconPowerstationInfoDay4::getId)
                    .collect(Collectors.toList());

            if (idls.size() > 0) {

                proEconPowerstationInfoDay4Service.removeByIds(idls);

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

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

                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);

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


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


            }

        }

    }

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

        if (!ls.isEmpty()) {


            // * 日MTBF

            DoubleSummaryStatistics summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay4::getRmtbf).summaryStatistics();
            pewp.setRmtbf(StringUtils.round(summaryStatistics.getSum(), 2));

            // * 日MTTR

            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay4::getRmttr).summaryStatistics();
            pewp.setRmttr(StringUtils.round(summaryStatistics.getSum(), 2));


            //* 日MTTF

            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay4::getRmttf).summaryStatistics();
            pewp.setRmttf(StringUtils.round(summaryStatistics.getSum(), 2));


            //* 日复位及时率

            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay4::getRfwjsl).summaryStatistics();
            pewp.setRfwjsl(StringUtils.round(summaryStatistics.getAverage(), 2));


            //* 日状态转换及时率

            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay4::getRztzhjsl).summaryStatistics();
            pewp.setRztzhjsl(StringUtils.round(summaryStatistics.getAverage(), 2));


            //* 日故障消缺及时率

            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay4::getRgzxqjsl).summaryStatistics();
            pewp.setRgzxqjsl(StringUtils.round(summaryStatistics.getAverage(), 2));

            //* 月MTBF

            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay4::getYmtbf).summaryStatistics();
            pewp.setYmtbf(StringUtils.round(summaryStatistics.getSum(), 2));

            //* 月MTTR

            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay4::getYmttr).summaryStatistics();
            pewp.setYmttr(StringUtils.round(summaryStatistics.getSum(), 2));


            // * 月MTTF

            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay4::getYmttf).summaryStatistics();
            pewp.setYmttf(StringUtils.round(summaryStatistics.getSum(), 2));


            // * 月复位及时率

            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay4::getYfwjsl).summaryStatistics();
            pewp.setYfwjsl(StringUtils.round(summaryStatistics.getAverage(), 2));


            // * 月状态转换及时率

            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay4::getYztzhjsl).summaryStatistics();
            pewp.setYztzhjsl(StringUtils.round(summaryStatistics.getAverage(), 2));


            // * 月故障消缺及时率

            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay4::getYgzxqjsl).summaryStatistics();
            pewp.setYgzxqjsl(StringUtils.round(summaryStatistics.getAverage(), 2));

            // * 年MTBF

            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay4::getNmtbf).summaryStatistics();
            pewp.setNmtbf(StringUtils.round(summaryStatistics.getSum(), 2));

            //  * 年MTTR

            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay4::getNmttr).summaryStatistics();
            pewp.setNmttr(StringUtils.round(summaryStatistics.getSum(), 2));


            // * 年MTTF

            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay4::getNmttf).summaryStatistics();
            pewp.setNmttf(StringUtils.round(summaryStatistics.getSum(), 2));


            //* 年复位及时率

            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay4::getNfwjsl).summaryStatistics();
            pewp.setNfwjsl(StringUtils.round(summaryStatistics.getAverage(), 2));


            //* 年状态转换及时率

            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay4::getNztzhjsl).summaryStatistics();
            pewp.setNztzhjsl(StringUtils.round(summaryStatistics.getAverage(), 2));


            //* 年故障消缺及时率

            summaryStatistics = ls.stream().mapToDouble(ProEconPowerstationInfoDay4::getNgzxqjsl).summaryStatistics();
            pewp.setNgzxqjsl(StringUtils.round(summaryStatistics.getAverage(), 2));


        }
    }

    /**
     * 计算场站日信息
     */
    public void calWindpowerInfoDay(Date recordDate) {
        Calendar c = Calendar.getInstance();
        c.setTime(recordDate);
        if (c.get(Calendar.HOUR_OF_DAY) == 0 && c.get(Calendar.MINUTE) == 0) {
            c.add(Calendar.DAY_OF_MONTH, -1);
        }

        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        Date end = c.getTime();

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


        recordDate = DateUtils.truncate(c.getTime());

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

                ProEconPowerstationInfoDay4 pewp = new ProEconPowerstationInfoDay4();
                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<ProEconPowerstationInfoDay4> pepidls = new ArrayList<>();
                Calendar cl = Calendar.getInstance();
                cl.setTime(recordDate);

                //获得当日设备统计2表数据
                ProEconPowerstationInfoDay2 pepid2 = null;
                List<ProEconPowerstationInfoDay2> pepid2ls;
                QueryWrapper<ProEconPowerstationInfoDay2> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("record_date", DateUtils.truncate(cl.getTime()))
                        .eq("windpowerstation_id", wp.getId())
                        .eq("location", Location.wp.getValue());
                pepid2ls = proEconPowerstationInfoDay2Service.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());

                if (!pepid2ls.isEmpty()) {
                    pepid2 = pepid2ls.get(0);
                }
                //获得当日设备统计3表数据
                ProEconPowerstationInfoDay3 pepid3 = null;
                List<ProEconPowerstationInfoDay3> pepid3ls;

                QueryWrapper<ProEconPowerstationInfoDay3> queryWrapper3 = new QueryWrapper<>();
                queryWrapper3.eq("record_date", DateUtils.truncate(cl.getTime()))
                        .eq("windpowerstation_id", wp.getId())
                        .eq("location", Location.wp.getValue());
                pepid3ls = proEconPowerstationInfoDay3Service.list(queryWrapper3);
                //                        .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());

                if (!pepid3ls.isEmpty()) {
                    pepid3 = pepid3ls.get(0);
                }
                //昨日的统计结果
                cl.add(Calendar.DAY_OF_MONTH, -1);
                if (cl.get(Calendar.DAY_OF_MONTH) != 1) {
                    QueryWrapper<ProEconPowerstationInfoDay4> queryWrapper4 = new QueryWrapper<>();
                    queryWrapper4.eq("record_date", DateUtils.truncate(cl.getTime()))
                            .eq("windpowerstation_id", wp.getId())
                            .eq("location", Location.wp.getValue());
                    pepidls = proEconPowerstationInfoDay4Service.list(queryWrapper4);
                    //                    .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());
                }


                calDetiall(pewp, end, begin, CacheContext.wpwtmap.get(wp.getId()), pepidls, pepid2, pepid3, "0");

                //*******************************************年信息统计*********************************************************/
                //                wpinfodayls.add(pewp);
                dayls.add(pewp);
                // proEconPowerstationInfoDay4Service.save(pewp);
                wpls.add(wp.getId());
            }


        }

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

        if (idls.size() > 0) {

            proEconPowerstationInfoDay4Service.removeByIds(idls);

        }

        List<ProEconPowerstationInfoDay4> templs = new ArrayList<>();
        for (ProEconPowerstationInfoDay4 vo : dayls) {
            templs.add(vo);
            if (templs.size() == 1000) {
                proEconPowerstationInfoDay4Service.saveBatch(templs);
                templs = new ArrayList<>();
            }
        }

        if (!templs.isEmpty()) {
            proEconPowerstationInfoDay4Service.saveBatch(templs);
        }
        //        proEconPowerstationInfoDay4Service.(dayls);
    }

    /**
     * 计算项目日信息
     */
    public void calProjectInfoDay(Date recordDate) {
        Calendar c = Calendar.getInstance();
        c.setTime(recordDate);
        if (c.get(Calendar.HOUR_OF_DAY) == 0 && c.get(Calendar.MINUTE) == 0) {
            c.add(Calendar.DAY_OF_MONTH, -1);
        }

        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        Date end = c.getTime();

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


        recordDate = DateUtils.truncate(c.getTime());


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

            ProEconPowerstationInfoDay4 pewp = new ProEconPowerstationInfoDay4();
            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<ProEconPowerstationInfoDay4> pepidls = new ArrayList<>();
            Calendar cl = Calendar.getInstance();
            cl.setTime(recordDate);

            //获得当日设备统计2表数据
            ProEconPowerstationInfoDay2 pepid2 = null;
            List<ProEconPowerstationInfoDay2> pepid2ls;
            QueryWrapper<ProEconPowerstationInfoDay2> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("record_date", DateUtils.truncate(cl.getTime()))
                    .eq("location", Location.pj.getValue())
                    .eq("project_id", pj.getId());
            pepid2ls = proEconPowerstationInfoDay2Service.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());

            if (!pepid2ls.isEmpty()) {
                pepid2 = pepid2ls.get(0);
            }
            //获得当日设备统计3表数据
            ProEconPowerstationInfoDay3 pepid3 = null;
            List<ProEconPowerstationInfoDay3> pepid3ls;
            QueryWrapper<ProEconPowerstationInfoDay3> queryWrapper3 = new QueryWrapper<>();
            queryWrapper3.eq("record_date", DateUtils.truncate(cl.getTime()))
                    .eq("location", Location.pj.getValue())
                    .eq("project_id", pj.getId());
            pepid3ls = proEconPowerstationInfoDay3Service.list(queryWrapper3);
            //                    .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());

            if (!pepid3ls.isEmpty()) {
                pepid3 = pepid3ls.get(0);
            }


            //昨日的统计结果
            cl.add(Calendar.DAY_OF_MONTH, -1);
            if (cl.get(Calendar.DAY_OF_MONTH) != 1) {
                QueryWrapper<ProEconPowerstationInfoDay4> queryWrapper4 = new QueryWrapper<>();
                queryWrapper4.eq("record_date", DateUtils.truncate(cl.getTime()))
                        .eq("location", Location.pj.getValue())
                        .eq("project_id", pj.getId());
                pepidls = proEconPowerstationInfoDay4Service.list(queryWrapper4);
                //                        .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());
            }


            calDetiall(pewp, end, begin, CacheContext.pjwtmap.get(pj.getId()), pepidls, pepid2, pepid3, "1");


            dayls.add(pewp);
            // proEconPowerstationInfoDay4Service.save(pewp);

            //            }

            pjls.add(pj.getWindpowerstationId());
        }

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

        if (idls.size() > 0) {

            proEconPowerstationInfoDay4Service.removeByIds(idls);

        }
        List<ProEconPowerstationInfoDay4> templs = new ArrayList<>();
        for (ProEconPowerstationInfoDay4 vo : dayls) {
            templs.add(vo);
            if (templs.size() == 1000) {
                proEconPowerstationInfoDay4Service.saveBatch(templs);
                templs = new ArrayList<>();
            }
        }

        if (!templs.isEmpty()) {
            proEconPowerstationInfoDay4Service.saveBatch(templs);
        }
        //        proEconPowerstationInfoDay4Service.(dayls);
    }

    /**
     * 计算线路日信息
     */
    public void calLineInfoDay(Date recordDate) {
        Calendar c = Calendar.getInstance();
        c.setTime(recordDate);
        if (c.get(Calendar.HOUR_OF_DAY) == 0 && c.get(Calendar.MINUTE) == 0) {
            c.add(Calendar.DAY_OF_MONTH, -1);
        }

        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        Date end = c.getTime();

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


        recordDate = DateUtils.truncate(c.getTime());

        List<String> lnls = new ArrayList<>();
        List<ProEconPowerstationInfoDay4> dayls = new ArrayList<>();
        for (ProBasicLine ln : CacheContext.lnls) {


            ProEconPowerstationInfoDay4 pewp = new ProEconPowerstationInfoDay4();
            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<ProEconPowerstationInfoDay4> pepidls = new ArrayList<>();
            Calendar cl = Calendar.getInstance();
            cl.setTime(recordDate);

            //获得当日设备统计2表数据
            ProEconPowerstationInfoDay2 pepid2 = null;
            List<ProEconPowerstationInfoDay2> pepid2ls;
            QueryWrapper<ProEconPowerstationInfoDay2> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("record_date", DateUtils.truncate(cl.getTime()))
                    .eq("location", Location.ln.getValue())
                    .eq("line_id", ln.getId());
            pepid2ls = proEconPowerstationInfoDay2Service.list(queryWrapper2);


            if (!pepid2ls.isEmpty()) {
                pepid2 = pepid2ls.get(0);
            }
            //获得当日设备统计3表数据
            ProEconPowerstationInfoDay3 pepid3 = null;
            List<ProEconPowerstationInfoDay3> pepid3ls;
            QueryWrapper<ProEconPowerstationInfoDay3> queryWrapper3 = new QueryWrapper<>();
            queryWrapper3.eq("record_date", DateUtils.truncate(cl.getTime()))
                    .eq("location", Location.ln.getValue())
                    .eq("line_id", ln.getId());
            pepid3ls = proEconPowerstationInfoDay3Service.list(queryWrapper3);


            if (!pepid3ls.isEmpty()) {
                pepid3 = pepid3ls.get(0);
            }
            //昨日的统计结果
            cl.add(Calendar.DAY_OF_MONTH, -1);
            if (cl.get(Calendar.DAY_OF_MONTH) != 1) {
                QueryWrapper<ProEconPowerstationInfoDay4> queryWrapper4 = new QueryWrapper<>();
                queryWrapper4.eq("record_date", DateUtils.truncate(cl.getTime()))
                        .eq("location", Location.ln.getValue())
                        .eq("line_id", ln.getId());
                pepidls = proEconPowerstationInfoDay4Service.list(queryWrapper4);

            }


            calDetiall(pewp, end, begin, CacheContext.lnwtmap.get(ln.getId()), pepidls, pepid2, pepid3, "2");

            dayls.add(pewp);
            // proEconPowerstationInfoDay4Service.save(pewp);

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

        }

        //判断是否有重复记录，先删除重复记录
        QueryWrapper<ProEconPowerstationInfoDay4> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("record_date", DateUtils.truncate(recordDate))
                .in("windpowerstation_id", lnls)
                .eq("location", Location.ln.getValue());
        List<String> idls = proEconPowerstationInfoDay4Service.list(queryWrapper).stream()

                .map(ProEconPowerstationInfoDay4::getId)
                .collect(Collectors.toList());

        if (idls.size() > 0) {

            proEconPowerstationInfoDay4Service.removeByIds(idls);

        }
        List<ProEconPowerstationInfoDay4> templs = new ArrayList<>();
        for (ProEconPowerstationInfoDay4 vo : dayls) {
            templs.add(vo);
            if (templs.size() == 1000) {
                proEconPowerstationInfoDay4Service.saveBatch(templs);
                templs = new ArrayList<>();
            }
        }

        if (!templs.isEmpty()) {
            proEconPowerstationInfoDay4Service.saveBatch(templs);
        }
        //        proEconPowerstationInfoDay4Service.(dayls);
    }

    private void calDetiall(ProEconPowerstationInfoDay4 pewp, Date end, Date begin, List<ProBasicEquipment> wtls,
                            List<ProEconPowerstationInfoDay4> pepidls, ProEconPowerstationInfoDay2 pepid2,
                            ProEconPowerstationInfoDay3 pepid3, String types) {

        ///*******************************************日信息统计*********************************************************/
        //MTTR、MTTF、MTBF

        if (null != wtls && !wtls.isEmpty()) {
            ProBasicEquipment wt = wtls.get(0);
            if (StringUtils.notEmp(pepid2) && StringUtils.notEmp(pepid3)) {
                //故障小时
                double rgzxsmx = pepid2.getRhjgzxsmx();
                //故障次数
                double rgzcsmx = pepid3.getRhjgzcsmx();
                if (rgzcsmx != 0) {
                    double mtbf = new BigDecimal(24 * wtls.size()).divide(new BigDecimal(rgzcsmx), 2, RoundingMode.HALF_EVEN).doubleValue();
                    double mttr = new BigDecimal(rgzxsmx).divide(new BigDecimal(rgzcsmx), 2, RoundingMode.HALF_EVEN).doubleValue();
                    double mttf = mtbf - mttr;

                    pewp.setRmtbf(mtbf);
                    pewp.setRmttr(mttr);
                    pewp.setRmttf(mttf);
                } else {
                    double mtbf = 24 * wtls.size();
                    double mttr = 0.0;
                    double mttf = mtbf - mttr;

                    pewp.setRmtbf(mtbf);
                    pewp.setRmttr(mttr);
                    pewp.setRmttf(mttf);
                }
            }
            //MTTR、MTTF、MTBF

            //复位及时率、状态转换率、消缺及时率
            switch (types) {
                case "0":
                    //日场站统计
                    QueryWrapper<ProEconEquipmentInfoDay5> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("record_date", DateUtils.truncate(begin))
                            .eq("windpowerstation_id", wt.getWindpowerstationId());
                    List<ProEconEquipmentInfoDay5> pestrls = proEconEquipmentInfoDay5Service.list(queryWrapper);
                    //                            .stream()
                    //                            .filter(i -> i.getRecordDate().compareTo(begin) == 0 && i.getWindpwoerId().equals(wt.getWindpowerstationId())
                    //                            ).collect(Collectors.toList());
                    //大于3米20分钟转换及时次数
                    DoubleSummaryStatistics timerate8_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate8).summaryStatistics();
                    //大于3米20分钟转换及时次数合计
                    double timerate8_sum = timerate8_summary.getSum();

                    //大于3米状态转换总次数次数
                    DoubleSummaryStatistics timerate14_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate14).summaryStatistics();
                    //大于3米状态转换总次数合计
                    double timerate14_sum = timerate14_summary.getSum();

                    if (timerate14_sum != 0) {
                        //日状态转换及时率
                        double temp = new BigDecimal(timerate8_sum).divide(new BigDecimal(timerate14_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                        pewp.setRztzhjsl(temp);
                    } else {
                        pewp.setRztzhjsl(0.0);
                    }

                    //20分钟复位及时次数
                    DoubleSummaryStatistics timerate19_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate19).summaryStatistics();
                    //20分钟复位及时次数合计
                    double timerate19_sum = timerate19_summary.getSum();

                    //总复位次数
                    DoubleSummaryStatistics timerate20_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate20).summaryStatistics();
                    //总复位次数合计
                    double timerate20_sum = timerate20_summary.getSum();

                    if (timerate20_sum != 0) {
                        //日复位及时率
                        double temp = new BigDecimal(timerate19_sum).divide(new BigDecimal(timerate20_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                        pewp.setRfwjsl(temp);
                    } else {
                        pewp.setRfwjsl(0.0);
                    }
                    //                    QueryWrapper<ProEconFaultLiminatedefects> queryWrapper2 = new QueryWrapper<>();
                    //                    queryWrapper2.eq("date_time",DateUtils.truncate(begin))
                    //                            .eq("windpowerstation_id",wt.getWindpowerstationId());
                    //                    List<ProEconFaultLiminatedefects> peflls = proEconFaultLiminatedefectsService.list(queryWrapper2);
                    ////                            .stream()
                    ////                            .filter(i -> i.getDateTime().compareTo(begin) == 0 && i.getWindpowerstationId().equals(wt.getWindpowerstationId())
                    ////                            ).collect(Collectors.toList());
                    //                    //消缺及时次数
                    //                    DoubleSummaryStatistics right_summary = peflls.stream().mapToDouble(ProEconFaultLiminatedefects::getRightCount).summaryStatistics();
                    //                    //消缺及时次数合计
                    //                    double right_sum = right_summary.getSum();
                    //
                    //                    //故障次数次数
                    //                    DoubleSummaryStatistics fault_summary = peflls.stream().mapToDouble(ProEconFaultLiminatedefects::getFaultCount).summaryStatistics();
                    //                    //故障次数次数合计
                    //                    double fault_sum = fault_summary.getSum();

                    List<FaultLiminatedefectsVo> peflls = proEconFaultLiminatedefectsService.queryFaultLiminatedefectsList(DateUtils.truncate(begin), wt.getWindpowerstationId());

                    if (!peflls.isEmpty()) {
                        FaultLiminatedefectsVo faultvo = peflls.get(0);
                        //消缺及时次数合计
                        //消缺及时次数合计
                        double right_sum = null != faultvo ? faultvo.getRightCount() : 0;
                        //故障次数次数合计
                        double fault_sum = null != faultvo ? faultvo.getFaultCount() : 0;
                        if (fault_sum != 0) {
                            //日消缺及时率
                            double temp = new BigDecimal(right_sum).divide(new BigDecimal(fault_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                            pewp.setRgzxqjsl(temp);
                        } else {
                            pewp.setRgzxqjsl(0.0);
                        }
                    }


                    //日场站统计
                case "1":
                    //日工程统计
                    queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("record_date", DateUtils.truncate(begin))
                            .eq("project_id", wt.getProjectId());
                    pestrls = proEconEquipmentInfoDay5Service.list(queryWrapper);
                    //                            .stream()
                    //                            .filter(i -> i.getRecordDate().compareTo(begin) == 0 && i.getProjectId().equals(wt.getProjectId())
                    //                            ).collect(Collectors.toList());
                    //大于3米20分钟转换及时次数
                    timerate8_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate8).summaryStatistics();
                    //大于3米20分钟转换及时次数合计
                    timerate8_sum = timerate8_summary.getSum();

                    //大于3米状态转换总次数次数
                    timerate14_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate14).summaryStatistics();
                    //大于3米状态转换总次数合计
                    timerate14_sum = timerate14_summary.getSum();

                    if (timerate14_sum != 0) {
                        //日状态转换及时率
                        double temp = new BigDecimal(timerate8_sum).divide(new BigDecimal(timerate14_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                        pewp.setRztzhjsl(temp);
                    } else {
                        pewp.setRztzhjsl(0.0);
                    }

                    //20分钟复位及时次数
                    timerate19_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate19).summaryStatistics();
                    //20分钟复位及时次数合计
                    timerate19_sum = timerate19_summary.getSum();

                    //总复位次数
                    timerate20_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate20).summaryStatistics();
                    //总复位次数合计
                    timerate20_sum = timerate20_summary.getSum();

                    if (timerate20_sum != 0) {
                        //日复位及时率
                        double temp = new BigDecimal(timerate19_sum).divide(new BigDecimal(timerate20_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                        pewp.setRfwjsl(temp);
                    } else {
                        pewp.setRfwjsl(0.0);
                    }


                    //                    queryWrapper2 = new QueryWrapper<>();
                    //                    queryWrapper2.eq("date_time", DateUtils.truncate(begin))
                    //                            .eq("project_id", wt.getProjectId());
                    //                    peflls = proEconFaultLiminatedefectsService.list(queryWrapper2);
                    ////                    .stream()
                    ////                            .filter(i -> i.getDateTime().compareTo(begin) == 0 && i.getProjectId().equals(wt.getProjectId())
                    ////                            ).collect(Collectors.toList());
                    //                    //消缺及时次数
                    //                    right_summary = peflls.stream().mapToDouble(ProEconFaultLiminatedefects::getRightCount).summaryStatistics();
                    //                    //消缺及时次数合计
                    //                    right_sum = right_summary.getSum();
                    //
                    //                    //故障次数次数
                    //                    fault_summary = peflls.stream().mapToDouble(ProEconFaultLiminatedefects::getFaultCount).summaryStatistics();
                    //                    //故障次数次数合计
                    //                    fault_sum = fault_summary.getSum();
                    peflls = proEconFaultLiminatedefectsService.queryFaultLiminatedefectsProjectList(DateUtils.truncate(begin), wt.getProjectId());

                    if (!peflls.isEmpty()) {
                        FaultLiminatedefectsVo faultvo = peflls.get(0);
                        //消缺及时次数合计
                        double right_sum = null != faultvo ? faultvo.getRightCount() : 0;
                        //故障次数次数合计
                        double fault_sum = null != faultvo ? faultvo.getFaultCount() : 0;
                        if (fault_sum != 0) {
                            //日消缺及时率
                            double temp = new BigDecimal(right_sum).divide(new BigDecimal(fault_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                            pewp.setRgzxqjsl(temp);
                        } else {
                            pewp.setRgzxqjsl(0.0);
                        }
                    }


                    //日工程统计
                case "2":
                    //日线路统计
                    queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("record_date", DateUtils.truncate(begin))
                            .eq("line_id", wt.getLineId());
                    pestrls = proEconEquipmentInfoDay5Service.list(queryWrapper);
                    //                            .stream()
                    //                            .filter(i -> i.getRecordDate().compareTo(begin) == 0 && i.getLineId().equals(wt.getLineId())
                    //                            ).collect(Collectors.toList());
                    //大于3米20分钟转换及时次数
                    timerate8_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate8).summaryStatistics();
                    //大于3米20分钟转换及时次数合计
                    timerate8_sum = timerate8_summary.getSum();

                    //大于3米状态转换总次数次数
                    timerate14_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate14).summaryStatistics();
                    //大于3米状态转换总次数合计
                    timerate14_sum = timerate14_summary.getSum();

                    if (timerate14_sum != 0) {
                        //日状态转换及时率
                        double temp = new BigDecimal(timerate8_sum).divide(new BigDecimal(timerate14_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                        pewp.setRztzhjsl(temp);
                    } else {
                        pewp.setRztzhjsl(0.0);
                    }

                    //20分钟复位及时次数
                    timerate19_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate19).summaryStatistics();
                    //20分钟复位及时次数合计
                    timerate19_sum = timerate19_summary.getSum();

                    //总复位次数
                    timerate20_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate20).summaryStatistics();
                    //总复位次数合计
                    timerate20_sum = timerate20_summary.getSum();

                    if (timerate20_sum != 0) {
                        //日复位及时率
                        double temp = new BigDecimal(timerate19_sum).divide(new BigDecimal(timerate20_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                        pewp.setRfwjsl(temp);
                    } else {
                        pewp.setRfwjsl(0.0);
                    }
                    //                    queryWrapper2 = new QueryWrapper<>();
                    //                    queryWrapper2.eq("date_time", DateUtils.truncate(begin))
                    //                            .eq("line_id", wt.getLineId());
                    //
                    //                    peflls = proEconFaultLiminatedefectsService.list(queryWrapper2);
                    ////                            .stream()
                    ////                            .filter(i -> i.getDateTime().compareTo(begin) == 0 && i.getLineId().equals(wt.getLineId())
                    ////                            ).collect(Collectors.toList());
                    //                    //消缺及时次数
                    //                    right_summary = peflls.stream().mapToDouble(ProEconFaultLiminatedefects::getRightCount).summaryStatistics();
                    //                    //消缺及时次数合计
                    //                    right_sum = right_summary.getSum();
                    //
                    //                    //故障次数次数
                    //                    fault_summary = peflls.stream().mapToDouble(ProEconFaultLiminatedefects::getFaultCount).summaryStatistics();
                    //                    //故障次数次数合计
                    //                    fault_sum = fault_summary.getSum();
                    //                    if (fault_sum != 0) {
                    //                        //日消缺及时率
                    //                        double temp = new BigDecimal(right_sum).divide(new BigDecimal(fault_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                    //                        pewp.setRgzxqjsl(temp);
                    //                    } else {
                    //                        pewp.setRgzxqjsl(0.0);
                    //                    }
                    peflls = proEconFaultLiminatedefectsService.queryFaultLiminatedefectsLineList(DateUtils.truncate(begin), wt.getLineId());

                    if (!peflls.isEmpty()) {
                        FaultLiminatedefectsVo faultvo = peflls.get(0);
                        //消缺及时次数合计
                        double right_sum = null != faultvo ? faultvo.getRightCount() : 0;
                        //故障次数次数合计
                        double fault_sum = null != faultvo ? faultvo.getFaultCount() : 0;
                        if (fault_sum != 0) {
                            //日消缺及时率
                            double temp = new BigDecimal(right_sum).divide(new BigDecimal(fault_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                            pewp.setRgzxqjsl(temp);
                        } else {
                            pewp.setRgzxqjsl(0.0);
                        }
                    }


                    //日线路统计
                default:

            }

            //复位及时率、状态转换率、消缺及时率

        }

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

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

        if (pepidls.isEmpty()) {
            setPowerstationInfoDay4Month(pewp);

        } else {


            Calendar cal = Calendar.getInstance();
            cal.setTime(end);

            int days = cal.get(Calendar.DAY_OF_MONTH);

            cal.set(Calendar.DAY_OF_MONTH, 1);
            Date monthbegin = cal.getTime();
            //MTTR、MTTF、MTBF

            if (!wtls.isEmpty()) {
                ProBasicEquipment wt = wtls.get(0);
                if (StringUtils.notEmp(pepid2) && StringUtils.notEmp(pepid3)) {
                    //故障小时
                    double rgzxsmx = pepid2.getRhjgzxsmx();
                    //故障次数
                    double rgzcsmx = pepid3.getRhjgzcsmx();
                    if (rgzcsmx != 0) {
                        double mtbf = new BigDecimal(24 * wtls.size() * days).divide(new BigDecimal(rgzcsmx), 2, RoundingMode.HALF_EVEN).doubleValue();
                        double mttr = new BigDecimal(rgzxsmx).divide(new BigDecimal(rgzcsmx), 2, RoundingMode.HALF_EVEN).doubleValue();
                        double mttf = mtbf - mttr;

                        pewp.setYmtbf(mtbf);
                        pewp.setYmttr(mttr);
                        pewp.setYmttf(mttf);
                    } else {
                        double mtbf = 24 * wtls.size() * days;
                        double mttr = 0.0;
                        double mttf = mtbf - mttr;

                        pewp.setYmtbf(mtbf);
                        pewp.setYmttr(mttr);
                        pewp.setYmttf(mttf);
                    }
                }
                //复位及时率、状态转换率、消缺及时率

                //MTTR、MTTF、MTBF
                switch (types) {
                    case "0":
                        //月场站统计

                        QueryWrapper<ProEconEquipmentInfoDay5> queryWrapper = new QueryWrapper<>();
                        queryWrapper.ge("record_date", monthbegin).le("record_date", end).eq("windpowerstation_id", wt.getWindpowerstationId());
                        List<ProEconEquipmentInfoDay5> pestrls = proEconEquipmentInfoDay5Service.list(queryWrapper);
                        //                                .stream()
                        //                                .filter(i ->
                        //                                        (i.getRecordDate().compareTo(monthbegin) == 0 || i.getRecordDate().after(monthbegin))
                        //                                                && (i.getRecordDate().compareTo(end) == 0 || i.getRecordDate().before(end))
                        //                                                && i.getWindpwoerId().equals(wt.getWindpowerstationId())
                        //                                ).collect(Collectors.toList());
                        //大于3米20分钟转换及时次数
                        DoubleSummaryStatistics timerate8_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate8).summaryStatistics();
                        //大于3米20分钟转换及时次数合计
                        double timerate8_sum = timerate8_summary.getSum();

                        //大于3米状态转换总次数次数
                        DoubleSummaryStatistics timerate14_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate14).summaryStatistics();
                        //大于3米状态转换总次数合计
                        double timerate14_sum = timerate14_summary.getSum();

                        if (timerate14_sum != 0) {
                            //月状态转换及时率
                            double temp = new BigDecimal(timerate8_sum).divide(new BigDecimal(timerate14_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                            pewp.setYztzhjsl(temp);
                        } else {
                            pewp.setYztzhjsl(0.0);
                        }

                        //20分钟复位及时次数
                        DoubleSummaryStatistics timerate19_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate19).summaryStatistics();
                        //20分钟复位及时次数合计
                        double timerate19_sum = timerate19_summary.getSum();

                        //总复位次数
                        DoubleSummaryStatistics timerate20_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate20).summaryStatistics();
                        //总复位次数合计
                        double timerate20_sum = timerate20_summary.getSum();

                        if (timerate20_sum != 0) {
                            //月复位及时率
                            double temp = new BigDecimal(timerate19_sum).divide(new BigDecimal(timerate20_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                            pewp.setYfwjsl(temp);
                        } else {
                            pewp.setYfwjsl(0.0);
                        }
                        //                        QueryWrapper<ProEconFaultLiminatedefects> queryWrapper2 = new QueryWrapper<>();
                        //                        queryWrapper2.ge("date_time", monthbegin).le("date_time", end).eq("windpowerstation_id", wt.getWindpowerstationId());
                        //                        List<ProEconFaultLiminatedefects> peflls = proEconFaultLiminatedefectsService.list(queryWrapper2);
                        ////                                .stream()
                        ////                                .filter(i ->
                        ////                                        (i.getDateTime().compareTo(monthbegin) == 0 || i.getDateTime().after(monthbegin))
                        ////                                                && (i.getDateTime().compareTo(end) == 0 || i.getDateTime().before(end))
                        ////                                                && i.getWindpowerstationId().equals(wt.getWindpowerstationId())
                        ////                                ).collect(Collectors.toList());
                        //                        //消缺及时次数
                        //                        DoubleSummaryStatistics right_summary = peflls.stream().mapToDouble(ProEconFaultLiminatedefects::getRightCount).summaryStatistics();
                        //                        //消缺及时次数合计
                        //                        double right_sum = right_summary.getSum();
                        //
                        //                        //故障次数次数
                        //                        DoubleSummaryStatistics fault_summary = peflls.stream().mapToDouble(ProEconFaultLiminatedefects::getFaultCount).summaryStatistics();
                        //                        //故障次数次数合计
                        //                        double fault_sum = fault_summary.getSum();
                        //
                        //                        if (fault_sum != 0) {
                        //                            //月消缺及时率
                        //                            double temp = new BigDecimal(right_sum).divide(new BigDecimal(fault_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                        //                            pewp.setYgzxqjsl(temp);
                        //                        } else {
                        //                            pewp.setYgzxqjsl(0.0);
                        //                        }
                        List<FaultLiminatedefectsVo> peflls = proEconFaultLiminatedefectsService.queryFaultLiminatedefectsRangeList(monthbegin, end, wt.getWindpowerstationId());

                        if (!peflls.isEmpty()) {
                            FaultLiminatedefectsVo faultvo = peflls.get(0);
                            //消缺及时次数合计
                            double right_sum = null != faultvo ? faultvo.getRightCount() : 0;
                            //故障次数次数合计
                            double fault_sum = null != faultvo ? faultvo.getFaultCount() : 0;
                            if (fault_sum != 0) {
                                //月消缺及时率
                                double temp = new BigDecimal(right_sum).divide(new BigDecimal(fault_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                                pewp.setYgzxqjsl(temp);
                            } else {
                                pewp.setYgzxqjsl(0.0);
                            }
                        }
                        //月场站统计
                    case "1":
                        //月工程统计

                        queryWrapper = new QueryWrapper<>();
                        queryWrapper.ge("record_date", monthbegin).le("record_date", end).eq("project_id", wt.getProjectId());
                        pestrls = proEconEquipmentInfoDay5Service.list(queryWrapper);
                        //                                .stream()
                        //                                .filter(i ->
                        //                                        (i.getRecordDate().compareTo(monthbegin) == 0 || i.getRecordDate().after(monthbegin))
                        //                                                && (i.getRecordDate().compareTo(end) == 0 || i.getRecordDate().before(end))
                        //                                                && i.getProjectId().equals(wt.getProjectId())
                        //                                ).collect(Collectors.toList());
                        //大于3米20分钟转换及时次数
                        timerate8_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate8).summaryStatistics();
                        //大于3米20分钟转换及时次数合计
                        timerate8_sum = timerate8_summary.getSum();

                        //大于3米状态转换总次数次数
                        timerate14_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate14).summaryStatistics();
                        //大于3米状态转换总次数合计
                        timerate14_sum = timerate14_summary.getSum();

                        if (timerate14_sum != 0) {
                            //月状态转换及时率
                            double temp = new BigDecimal(timerate8_sum).divide(new BigDecimal(timerate14_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                            pewp.setYztzhjsl(temp);
                        } else {
                            pewp.setYztzhjsl(0.0);
                        }

                        //20分钟复位及时次数
                        timerate19_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate19).summaryStatistics();
                        //20分钟复位及时次数合计
                        timerate19_sum = timerate19_summary.getSum();

                        //总复位次数
                        timerate20_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate20).summaryStatistics();
                        //总复位次数合计
                        timerate20_sum = timerate20_summary.getSum();

                        if (timerate20_sum != 0) {
                            //月复位及时率
                            double temp = new BigDecimal(timerate19_sum).divide(new BigDecimal(timerate20_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                            pewp.setYfwjsl(temp);
                        } else {
                            pewp.setYfwjsl(0.0);
                        }
                        //                        queryWrapper2 = new QueryWrapper<>();
                        //                        queryWrapper2.ge("date_time", monthbegin).le("date_time", end).eq("project_id", wt.getProjectId());
                        //                        peflls = proEconFaultLiminatedefectsService.list(queryWrapper2);
                        ////                                .stream()
                        ////                                .filter(i ->
                        ////                                        (i.getDateTime().compareTo(monthbegin) == 0 || i.getDateTime().after(monthbegin))
                        ////                                                && (i.getDateTime().compareTo(end) == 0 || i.getDateTime().before(end))
                        ////                                                && i.getProjectId().equals(wt.getProjectId())
                        ////                                ).collect(Collectors.toList());
                        //                        //消缺及时次数
                        //                        right_summary = peflls.stream().mapToDouble(ProEconFaultLiminatedefects::getRightCount).summaryStatistics();
                        //                        //消缺及时次数合计
                        //                        right_sum = right_summary.getSum();
                        //
                        //                        //故障次数次数
                        //                        fault_summary = peflls.stream().mapToDouble(ProEconFaultLiminatedefects::getFaultCount).summaryStatistics();
                        //                        //故障次数次数合计
                        //                        fault_sum = fault_summary.getSum();
                        //
                        //                        if (fault_sum != 0) {
                        //                            //月消缺及时率
                        //                            double temp = new BigDecimal(right_sum).divide(new BigDecimal(fault_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                        //                            pewp.setYgzxqjsl(temp);
                        //                        } else {
                        //                            pewp.setYgzxqjsl(0.0);
                        //                        }

                        peflls = proEconFaultLiminatedefectsService.queryFaultLiminatedefectsProjectRangeList(monthbegin, end, wt.getProjectId());

                        if (!peflls.isEmpty()) {
                            FaultLiminatedefectsVo faultvo = peflls.get(0);
                            //消缺及时次数合计
                            double right_sum = null != faultvo ? faultvo.getRightCount() : 0;
                            //故障次数次数合计
                            double fault_sum = null != faultvo ? faultvo.getFaultCount() : 0;
                            if (fault_sum != 0) {
                                //月消缺及时率
                                double temp = new BigDecimal(right_sum).divide(new BigDecimal(fault_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                                pewp.setYgzxqjsl(temp);
                            } else {
                                pewp.setYgzxqjsl(0.0);
                            }
                        }
                        //月工程统计
                    case "2":
                        //月线路统计
                        queryWrapper = new QueryWrapper<>();
                        queryWrapper.ge("record_date", monthbegin).le("record_date", end).eq("line_id", wt.getLineId());
                        pestrls = proEconEquipmentInfoDay5Service.list(queryWrapper);
                        //                                .stream()
                        //                                .filter(i ->
                        //                                        (i.getRecordDate().compareTo(monthbegin) == 0 || i.getRecordDate().after(monthbegin))
                        //                                                && (i.getRecordDate().compareTo(end) == 0 || i.getRecordDate().before(end))
                        //                                                && i.getLineId().equals(wt.getLineId())
                        //                                ).collect(Collectors.toList());
                        //大于3米20分钟转换及时次数
                        timerate8_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate8).summaryStatistics();
                        //大于3米20分钟转换及时次数合计
                        timerate8_sum = timerate8_summary.getSum();

                        //大于3米状态转换总次数次数
                        timerate14_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate14).summaryStatistics();
                        //大于3米状态转换总次数合计
                        timerate14_sum = timerate14_summary.getSum();

                        if (timerate14_sum != 0) {
                            //月状态转换及时率
                            double temp = new BigDecimal(timerate8_sum).divide(new BigDecimal(timerate14_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                            pewp.setYztzhjsl(temp);
                        } else {
                            pewp.setYztzhjsl(0.0);
                        }

                        //20分钟复位及时次数
                        timerate19_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate19).summaryStatistics();
                        //20分钟复位及时次数合计
                        timerate19_sum = timerate19_summary.getSum();

                        //总复位次数
                        timerate20_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate20).summaryStatistics();
                        //总复位次数合计
                        timerate20_sum = timerate20_summary.getSum();

                        if (timerate20_sum != 0) {
                            //月复位及时率
                            double temp = new BigDecimal(timerate19_sum).divide(new BigDecimal(timerate20_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                            pewp.setYfwjsl(temp);
                        } else {
                            pewp.setYfwjsl(0.0);
                        }
                        //                        queryWrapper2 = new QueryWrapper<>();
                        //                        queryWrapper2.ge("date_time", monthbegin).le("date_time", end).eq("line_Id", wt.getLineId());
                        //                        peflls = proEconFaultLiminatedefectsService.list(queryWrapper2);
                        ////                                .stream()
                        ////                                .filter(i ->
                        ////                                        (i.getDateTime().compareTo(monthbegin) == 0 || i.getDateTime().after(monthbegin))
                        ////                                                && (i.getDateTime().compareTo(end) == 0 || i.getDateTime().before(end))
                        ////                                                && i.getLineId().equals(wt.getLineId())
                        ////                                ).collect(Collectors.toList());
                        //                        //消缺及时次数
                        //                        right_summary = peflls.stream().mapToDouble(ProEconFaultLiminatedefects::getRightCount).summaryStatistics();
                        //                        //消缺及时次数合计
                        //                        right_sum = right_summary.getSum();
                        //
                        //                        //故障次数次数
                        //                        fault_summary = peflls.stream().mapToDouble(ProEconFaultLiminatedefects::getFaultCount).summaryStatistics();
                        //                        //故障次数次数合计
                        //                        fault_sum = fault_summary.getSum();
                        //
                        //                        if (fault_sum != 0) {
                        //                            //月消缺及时率
                        //                            double temp = new BigDecimal(right_sum).divide(new BigDecimal(fault_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                        //                            pewp.setYgzxqjsl(temp);
                        //                        } else {
                        //                            pewp.setYgzxqjsl(0.0);
                        //                        }
                        //月线路统计
                        peflls = proEconFaultLiminatedefectsService.queryFaultLiminatedefectsLineRangeList(monthbegin, end, wt.getLineId());

                        if (!peflls.isEmpty()) {
                            FaultLiminatedefectsVo faultvo = peflls.get(0);
                            //消缺及时次数合计
                            //消缺及时次数合计
                            double right_sum = null != faultvo ? faultvo.getRightCount() : 0;
                            //故障次数次数合计
                            double fault_sum = null != faultvo ? faultvo.getFaultCount() : 0;
                            if (fault_sum != 0) {
                                //月消缺及时率
                                double temp = new BigDecimal(right_sum).divide(new BigDecimal(fault_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                                pewp.setYgzxqjsl(temp);
                            } else {
                                pewp.setYgzxqjsl(0.0);
                            }
                        }
                    default:

                }
                //MTTR、MTTF、MTBF

            } else {
                setPowerstationInfoDay4Month(pewp);
            }


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

            //*******************************************年信息统计*********************************************************/

            if (pepidls.isEmpty()) {
                setPowerstationInfoDay4Year(pewp);

            } else {


                cal = Calendar.getInstance();
                cal.setTime(end);

                days = cal.get(Calendar.DAY_OF_YEAR);
                cal.set(Calendar.MONTH, 0);
                cal.set(Calendar.DAY_OF_YEAR, 1);
                Date yearbegin = cal.getTime();
                //MTTR、MTTF、MTBF

                if (!wtls.isEmpty()) {
                    ProBasicEquipment wt = wtls.get(0);
                    if (StringUtils.notEmp(pepid2) && StringUtils.notEmp(pepid3)) {
                        //故障小时
                        double rgzxsmx = pepid2.getRhjgzxsmx();
                        //故障次数
                        double rgzcsmx = pepid3.getRhjgzcsmx();
                        if (rgzcsmx != 0) {
                            double mtbf = new BigDecimal(24 * wtls.size() * days).divide(new BigDecimal(rgzcsmx), 2, RoundingMode.HALF_EVEN).doubleValue();
                            double mttr = new BigDecimal(rgzxsmx).divide(new BigDecimal(rgzcsmx), 2, RoundingMode.HALF_EVEN).doubleValue();
                            double mttf = mtbf - mttr;

                            pewp.setNmtbf(mtbf);
                            pewp.setNmttr(mttr);
                            pewp.setNmttf(mttf);
                        } else {
                            double mtbf = 24 * wtls.size() * days;
                            double mttr = 0.0;
                            double mttf = mtbf - mttr;

                            pewp.setNmtbf(mtbf);
                            pewp.setNmttr(mttr);
                            pewp.setNmttf(mttf);
                        }
                    }
                    //MTTR、MTTF、MTBF

                    //复位及时率、状态转换率、消缺及时率
                    switch (types) {
                        case "0":
                            //年场站统计
                            QueryWrapper<ProEconEquipmentInfoDay5> queryWrapper = new QueryWrapper<>();
                            queryWrapper.ge("record_date", yearbegin).le("record_date", end).eq("windpowerstation_id", wt.getWindpowerstationId());
                            List<ProEconEquipmentInfoDay5> pestrls = proEconEquipmentInfoDay5Service.list(queryWrapper);
                            //                                    .stream()
                            //                                    .filter(i ->
                            //                                            (i.getRecordDate().compareTo(yearbegin) == 0 || i.getRecordDate().after(yearbegin))
                            //                                                    && (i.getRecordDate().compareTo(end) == 0 || i.getRecordDate().before(end))
                            //                                                    && i.getWindpwoerId().equals(wt.getWindpowerstationId())
                            //                                    ).collect(Collectors.toList());
                            //大于3米20分钟转换及时次数
                            DoubleSummaryStatistics timerate8_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate8).summaryStatistics();
                            //大于3米20分钟转换及时次数合计
                            double timerate8_sum = timerate8_summary.getSum();

                            //大于3米状态转换总次数次数
                            DoubleSummaryStatistics timerate14_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate14).summaryStatistics();
                            //大于3米状态转换总次数合计
                            double timerate14_sum = timerate14_summary.getSum();

                            if (timerate14_sum != 0) {
                                //日状态转换及时率
                                double temp = new BigDecimal(timerate8_sum).divide(new BigDecimal(timerate14_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                                pewp.setNztzhjsl(temp);
                            } else {
                                pewp.setNztzhjsl(0.0);
                            }

                            //20分钟复位及时次数
                            DoubleSummaryStatistics timerate19_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate19).summaryStatistics();
                            //20分钟复位及时次数合计
                            double timerate19_sum = timerate19_summary.getSum();

                            //总复位次数
                            DoubleSummaryStatistics timerate20_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate20).summaryStatistics();
                            //总复位次数合计
                            double timerate20_sum = timerate20_summary.getSum();

                            if (timerate20_sum != 0) {
                                //日复位及时率
                                double temp = new BigDecimal(timerate19_sum).divide(new BigDecimal(timerate20_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                                pewp.setNfwjsl(temp);
                            } else {
                                pewp.setNfwjsl(0.0);
                            }

                            //                            QueryWrapper<ProEconFaultLiminatedefects> queryWrapper2 = new QueryWrapper<>();
                            //                            queryWrapper2.ge("date_time", yearbegin).le("date_time", end).eq("windpowerstation_id", wt.getWindpowerstationId());
                            //                            List<ProEconFaultLiminatedefects> peflls = proEconFaultLiminatedefectsService.list(queryWrapper2);
                            ////                            .stream()
                            ////                                    .filter(i ->
                            ////                                            (i.getDateTime().compareTo(yearbegin) == 0 || i.getDateTime().after(yearbegin))
                            ////                                                    && (i.getDateTime().compareTo(end) == 0 || i.getDateTime().before(end))
                            ////                                                    && i.getWindpowerstationId().equals(wt.getWindpowerstationId())
                            ////                                    ).collect(Collectors.toList());
                            //                            //消缺及时次数
                            //                            DoubleSummaryStatistics right_summary = peflls.stream().mapToDouble(ProEconFaultLiminatedefects::getRightCount).summaryStatistics();
                            //                            //消缺及时次数合计
                            //                            double right_sum = right_summary.getSum();
                            //
                            //                            //故障次数次数
                            //                            DoubleSummaryStatistics fault_summary = peflls.stream().mapToDouble(ProEconFaultLiminatedefects::getFaultCount).summaryStatistics();
                            //                            //故障次数次数合计
                            //                            double fault_sum = fault_summary.getSum();
                            //
                            //                            if (fault_sum != 0) {
                            //                                //日消缺及时率
                            //                                double temp = new BigDecimal(right_sum).divide(new BigDecimal(fault_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                            //                                pewp.setNgzxqjsl(temp);
                            //                            } else {
                            //                                pewp.setNgzxqjsl(0.0);
                            //                            }

                            List<FaultLiminatedefectsVo> peflls = proEconFaultLiminatedefectsService.queryFaultLiminatedefectsRangeList(yearbegin, end, wt.getWindpowerstationId());

                            if (!peflls.isEmpty()) {
                                FaultLiminatedefectsVo faultvo = peflls.get(0);
                                //消缺及时次数合计
                                //消缺及时次数合计
                                double right_sum = null != faultvo ? faultvo.getRightCount() : 0;
                                //故障次数次数合计
                                double fault_sum = null != faultvo ? faultvo.getFaultCount() : 0;
                                if (fault_sum != 0) {
                                    //年消缺及时率
                                    double temp = new BigDecimal(right_sum).divide(new BigDecimal(fault_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                                    pewp.setNgzxqjsl(temp);
                                } else {
                                    pewp.setNgzxqjsl(0.0);
                                }
                            }
                            //年场站统计
                        case "1":
                            //年工程统计
                            queryWrapper = new QueryWrapper<>();
                            queryWrapper.ge("record_date", yearbegin).le("record_date", end).eq("windpowerstation_id", wt.getWindpowerstationId());
                            pestrls = proEconEquipmentInfoDay5Service.list(queryWrapper);
                            //                                    .stream()
                            //                                    .filter(i ->
                            //                                            (i.getRecordDate().compareTo(yearbegin) == 0 || i.getRecordDate().after(yearbegin))
                            //                                                    && (i.getRecordDate().compareTo(end) == 0 || i.getRecordDate().before(end))
                            //                                                    && i.getProjectId().equals(wt.getProjectId())
                            //                                    ).collect(Collectors.toList());
                            //大于3米20分钟转换及时次数
                            timerate8_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate8).summaryStatistics();
                            //大于3米20分钟转换及时次数合计
                            timerate8_sum = timerate8_summary.getSum();

                            //大于3米状态转换总次数次数
                            timerate14_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate14).summaryStatistics();
                            //大于3米状态转换总次数合计
                            timerate14_sum = timerate14_summary.getSum();

                            if (timerate14_sum != 0) {
                                //日状态转换及时率
                                double temp = new BigDecimal(timerate8_sum).divide(new BigDecimal(timerate14_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                                pewp.setNztzhjsl(temp);
                            } else {
                                pewp.setNztzhjsl(0.0);
                            }

                            //20分钟复位及时次数
                            timerate19_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate19).summaryStatistics();
                            //20分钟复位及时次数合计
                            timerate19_sum = timerate19_summary.getSum();

                            //总复位次数
                            timerate20_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate20).summaryStatistics();
                            //总复位次数合计
                            timerate20_sum = timerate20_summary.getSum();

                            if (timerate20_sum != 0) {
                                //日复位及时率
                                double temp = new BigDecimal(timerate19_sum).divide(new BigDecimal(timerate20_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                                pewp.setNfwjsl(temp);
                            } else {
                                pewp.setNfwjsl(0.0);
                            }

                            //                            queryWrapper2 = new QueryWrapper<>();
                            //                            queryWrapper2.ge("date_time", yearbegin).le("date_time", end).eq("project_id", wt.getProjectId());
                            //                            peflls = proEconFaultLiminatedefectsService.list(queryWrapper2);
                            ////                                    .stream()
                            ////                                    .filter(i ->
                            ////                                            (i.getDateTime().compareTo(yearbegin) == 0 || i.getDateTime().after(yearbegin))
                            ////                                                    && (i.getDateTime().compareTo(end) == 0 || i.getDateTime().before(end))
                            ////                                                    && i.getProjectId().equals(wt.getProjectId())
                            ////                                    ).collect(Collectors.toList());
                            //                            //消缺及时次数
                            //                            right_summary = peflls.stream().mapToDouble(ProEconFaultLiminatedefects::getRightCount).summaryStatistics();
                            //                            //消缺及时次数合计
                            //                            right_sum = right_summary.getSum();
                            //
                            //                            //故障次数次数
                            //                            fault_summary = peflls.stream().mapToDouble(ProEconFaultLiminatedefects::getFaultCount).summaryStatistics();
                            //                            //故障次数次数合计
                            //                            fault_sum = fault_summary.getSum();
                            //
                            //                            if (fault_sum != 0) {
                            //                                //日消缺及时率
                            //                                double temp = new BigDecimal(right_sum).divide(new BigDecimal(fault_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                            //                                pewp.setNgzxqjsl(temp);
                            //                            } else {
                            //                                pewp.setNgzxqjsl(0.0);
                            //                            }


                            peflls = proEconFaultLiminatedefectsService.queryFaultLiminatedefectsProjectRangeList(yearbegin, end, wt.getProjectId());

                            if (!peflls.isEmpty()) {
                                FaultLiminatedefectsVo faultvo = peflls.get(0);
                                //消缺及时次数合计
                                double right_sum = null != faultvo ? faultvo.getRightCount() : 0;
                                //故障次数次数合计
                                double fault_sum = null != faultvo ? faultvo.getFaultCount() : 0;
                                if (fault_sum != 0) {
                                    //年消缺及时率
                                    double temp = new BigDecimal(right_sum).divide(new BigDecimal(fault_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                                    pewp.setNgzxqjsl(temp);
                                } else {
                                    pewp.setNgzxqjsl(0.0);
                                }
                            }
                            //年工程统计
                        case "2":
                            //年线路统计
                            queryWrapper = new QueryWrapper<>();
                            queryWrapper.ge("record_date", yearbegin).le("record_date", end).eq("line_id", wt.getLineId());
                            pestrls = proEconEquipmentInfoDay5Service.list(queryWrapper);
                            //                                    .stream()
                            //                                    .filter(i ->
                            //                                            (i.getRecordDate().compareTo(yearbegin) == 0 || i.getRecordDate().after(yearbegin))
                            //                                                    && (i.getRecordDate().compareTo(end) == 0 || i.getRecordDate().before(end))
                            //                                                    && i.getLineId().equals(wt.getLineId())
                            //                                    ).collect(Collectors.toList());
                            //大于3米20分钟转换及时次数
                            timerate8_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate8).summaryStatistics();
                            //大于3米20分钟转换及时次数合计
                            timerate8_sum = timerate8_summary.getSum();

                            //大于3米状态转换总次数次数
                            timerate14_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate14).summaryStatistics();
                            //大于3米状态转换总次数合计
                            timerate14_sum = timerate14_summary.getSum();

                            if (timerate14_sum != 0) {
                                //日状态转换及时率
                                double temp = new BigDecimal(timerate8_sum).divide(new BigDecimal(timerate14_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                                pewp.setNztzhjsl(temp);
                            } else {
                                pewp.setNztzhjsl(0.0);
                            }

                            //20分钟复位及时次数
                            timerate19_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate19).summaryStatistics();
                            //20分钟复位及时次数合计
                            timerate19_sum = timerate19_summary.getSum();

                            //总复位次数
                            timerate20_summary = pestrls.stream().mapToDouble(ProEconEquipmentInfoDay5::getTimerate20).summaryStatistics();
                            //总复位次数合计
                            timerate20_sum = timerate20_summary.getSum();

                            if (timerate20_sum != 0) {
                                //日复位及时率
                                double temp = new BigDecimal(timerate19_sum).divide(new BigDecimal(timerate20_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                                pewp.setNfwjsl(temp);
                            } else {
                                pewp.setNfwjsl(0.0);
                            }
                            //                            queryWrapper2 = new QueryWrapper<>();
                            //                            queryWrapper2.ge("date_time", monthbegin).le("date_time", end).eq("line_id", wt.getLineId());
                            //                            peflls = proEconFaultLiminatedefectsService.list(queryWrapper2);
                            ////                                    .stream()
                            ////                                    .filter(i ->
                            ////                                            (i.getDateTime().compareTo(yearbegin) == 0 || i.getDateTime().after(yearbegin))
                            ////                                                    && (i.getDateTime().compareTo(end) == 0 || i.getDateTime().before(end))
                            ////                                                    && i.getLineId().equals(wt.getLineId())
                            ////                                    ).collect(Collectors.toList());
                            //                            //消缺及时次数
                            //                            right_summary = peflls.stream().mapToDouble(ProEconFaultLiminatedefects::getRightCount).summaryStatistics();
                            //                            //消缺及时次数合计
                            //                            right_sum = right_summary.getSum();
                            //
                            //                            //故障次数次数
                            //                            fault_summary = peflls.stream().mapToDouble(ProEconFaultLiminatedefects::getFaultCount).summaryStatistics();
                            //                            //故障次数次数合计
                            //                            fault_sum = fault_summary.getSum();
                            //
                            //                            if (fault_sum != 0) {
                            //                                //日消缺及时率
                            //                                double temp = new BigDecimal(right_sum).divide(new BigDecimal(fault_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                            //                                pewp.setNgzxqjsl(temp);
                            //                            } else {
                            //                                pewp.setNgzxqjsl(0.0);
                            //                            }

                            peflls = proEconFaultLiminatedefectsService.queryFaultLiminatedefectsLineRangeList(yearbegin, end, wt.getLineId());

                            if (!peflls.isEmpty()) {
                                FaultLiminatedefectsVo faultvo = peflls.get(0);
                                //消缺及时次数合计
                                //消缺及时次数合计
                                double right_sum = null != faultvo ? faultvo.getRightCount() : 0;
                                //故障次数次数合计
                                double fault_sum = null != faultvo ? faultvo.getFaultCount() : 0;
                                if (fault_sum != 0) {
                                    //年消缺及时率
                                    double temp = new BigDecimal(right_sum).divide(new BigDecimal(fault_sum), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
                                    pewp.setNgzxqjsl(temp);
                                } else {
                                    pewp.setNgzxqjsl(0.0);
                                }
                            }
                            //年线路统计
                        default:

                    }


                } else {
                    setPowerstationInfoDay4Year(pewp);
                }
                //复位及时率、状态转换率、消缺及时率

            }


        }
    }

    private void setPowerstationInfoDay4Month(ProEconPowerstationInfoDay4 pewp) {

        //* 月MTBF
        pewp.setYmtbf(pewp.getRmtbf());


        // * 月MTTR

        pewp.setYmttr(pewp.getRmttr());


        // * 月MTTF

        pewp.setYmttf(pewp.getRmttf());


        // * 月复位及时率

        pewp.setYfwjsl(pewp.getRfwjsl());


        // * 月状态转换及时率

        pewp.setYztzhjsl(pewp.getRztzhjsl());


        // * 月故障消缺及时率

        pewp.setYgzxqjsl(pewp.getRgzxqjsl());
    }

    private void setPowerstationInfoDay4Year(ProEconPowerstationInfoDay4 pewp) {

        // * 年MTBF

        pewp.setNmtbf(pewp.getRmtbf());


        // * 年MTTR

        pewp.setNmttr(pewp.getRmttr());


        // * 年MTTF

        pewp.setNmttf(pewp.getRmttf());


        // * 年复位及时率

        pewp.setNfwjsl(pewp.getRfwjsl());


        // * 年状态转换及时率

        pewp.setNztzhjsl(pewp.getRztzhjsl());


        // * 年故障消缺及时率

        pewp.setNgzxqjsl(pewp.getRgzxqjsl());
    }
}







