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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gyee.common.contant.ContantXk;
import com.gyee.common.model.PointData;
import com.gyee.common.model.StringUtils;
import com.gyee.generation.config.feign.RemoteServiceBuilder;
import com.gyee.generation.init.CacheContext;
import com.gyee.generation.mapper.auto.ProEconEquipmentInfoDay1Mapper;
import com.gyee.generation.model.auto.ProBasicEquipment;
import com.gyee.generation.model.auto.ProBasicEquipmentPoint;
import com.gyee.generation.model.auto.ProEconEquipmentInfoDay1;
import com.gyee.generation.model.vo.WpType;
import com.gyee.generation.service.auto.IProEconEquipmentInfoDay1Service;
import com.gyee.generation.task.thread.EquipmentInfo1Thread;
import com.gyee.generation.util.DateUtils;
import com.gyee.generation.util.realtimesource.IEdosUtil;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.stream.Collectors;

@Service
public class EquipmentInfo1Service {

    //    private static final Logger logger = LoggerFactory.getLogger(EquipmentInfo1Service.class);
    @Resource
    private IEdosUtil edosUtil;
    @Resource
    private RemoteServiceBuilder remoteService;
    @Resource
    private IProEconEquipmentInfoDay1Service proEconEquipmentInfoDay1Service;
    @Resource
    @Qualifier("applicationTaskExecutor")  // 指定要注入的 Executor Bean
    private Executor executor;

    @Resource
    private ProEconEquipmentInfoDay1Mapper proEconEquipmentInfoDay1Mapper;

    /**
     * 计算设备日信息
     */
    public void calEquipmentInfoDay(Date recordDate) throws Exception {
        Calendar c = Calendar.getInstance();
        c.setTime(recordDate);

        if (c.get(Calendar.HOUR_OF_DAY) == 0)
        //      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());


        Map<String, ProEconEquipmentInfoDay1> pep1map = new ConcurrentHashMap<>();
        Calendar cl = Calendar.getInstance();
        cl.setTime(DateUtils.truncate(recordDate));
        cl.add(Calendar.DAY_OF_MONTH, -1);

        QueryWrapper<ProEconEquipmentInfoDay1> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("record_date", cl.getTime());
        List<ProEconEquipmentInfoDay1> pep1ls = proEconEquipmentInfoDay1Service.list(queryWrapper2);
        if (!pep1ls.isEmpty()) {
            for (ProEconEquipmentInfoDay1 pep : pep1ls) {
                pep1map.put(pep.getWindturbineId(), pep);
            }
        }
        List<ProEconEquipmentInfoDay1> dayls = new CopyOnWriteArrayList<>();
        //        final CountDownLatch latch = new CountDownLatch(CacheContext.wtls.size());
        //
        List<String> wpls = new ArrayList<>();
        //        for(ProBasicEquipment wt:CacheContext.wtls)
        //        {
        //            if(!wpls.contains(wt.getWindpowerstationId()))
        //            {
        //                wpls.add(wt.getWindpowerstationId());
        //            }
        //            new Thread(new EquipmentInfo1Thread(executor,  wt,  pep1map,
        //                    recordDate,  end,  begin,dayls,
        //                    edosUtil,latch)).start();
        //        }
        //        latch.await();
        //
        //        for (Map.Entry<String, List<ProBasicEquipment>> entry : CacheContext.wpwtmap.entrySet()) {
        //
        //
        //            final CountDownLatch latch = new CountDownLatch(entry.getValue().size());
        //            for (ProBasicEquipment wt : entry.getValue()) {
        //                if (!wpls.contains(wt.getWindpowerstationId())) {
        //                    wpls.add(wt.getWindpowerstationId());
        //                }
        //
        //                new Thread(new EquipmentInfo1Thread(executor,  wt,  pep1map,
        //                        recordDate,  end,  begin,dayls,
        //                        edosUtil,latch)).start();
        //
        //            }
        //            latch.await();
        //        }
        List<ProBasicEquipment> wttemps = new ArrayList<>();

        for (ProBasicEquipment wt : CacheContext.wtls) {

            if (!wpls.contains(wt.getWindpowerstationId())) {
                wpls.add(wt.getWindpowerstationId());
            }
            wttemps.add(wt);
            if (wttemps.size() == 120) {

                final CountDownLatch latch = new CountDownLatch(120);
                for (ProBasicEquipment wt1 : wttemps) {
                    new Thread(new EquipmentInfo1Thread(executor, wt1, pep1map,
                            recordDate, end, begin, dayls,
                            edosUtil,remoteService, latch)).start();
                }
                latch.await();
                Thread.sleep(12);
                wttemps = new ArrayList<>();
            }

        }

        if (!wttemps.isEmpty()) {

            final CountDownLatch latch = new CountDownLatch(wttemps.size());
            for (ProBasicEquipment wt1 : wttemps) {
                new Thread(new EquipmentInfo1Thread(executor, wt1, pep1map,
                        recordDate, end, begin, dayls,
                        edosUtil, remoteService,latch)).start();
            }
            latch.await();
        }

        System.out.println("ok");
        c = Calendar.getInstance();
        c.setTime(recordDate);

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

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

        QueryWrapper<ProEconEquipmentInfoDay1> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("record_date", recordDate);
        queryWrapper.in("windpowerstation_id", wpls);

        //判断是否有重复记录，先删除重复记录
        List<String> idls = proEconEquipmentInfoDay1Service.list(queryWrapper).stream()
                .map(ProEconEquipmentInfoDay1::getId)
                .collect(Collectors.toList());

        List<String> tempids = new ArrayList<>();

        for (int i = 0; i < idls.size(); i++) {
            tempids.add(idls.get(i));
            if (tempids.size() == 1000) {
                proEconEquipmentInfoDay1Service.removeByIds(tempids);
                tempids = new ArrayList<>();
            }
        }

        if (!tempids.isEmpty()) {
            proEconEquipmentInfoDay1Service.removeByIds(tempids);
        }

        List<ProEconEquipmentInfoDay1> templs = new ArrayList<>();
        for (ProEconEquipmentInfoDay1 vo : dayls) {
            if (StringUtils.notEmp(vo) && StringUtils.notEmp(vo.getWindturbineId())) {
                templs.add(vo);
                if (templs.size() == 1000) {
                    proEconEquipmentInfoDay1Service.saveBatch(templs);
                    templs = new ArrayList<>();
                }
            }

        }


        if (!templs.isEmpty()) {
            proEconEquipmentInfoDay1Service.saveBatch(templs);
        }
        //
        //        List<ProEconEquipmentInfoDay1> templs=new ArrayList<>();
        //        for(ProEconEquipmentInfoDay1 vo:dayls)
        //        {
        //            if(StringUtils.notEmp(vo) && StringUtils.notEmp(vo.getWindturbineId()))
        //            {
        //                templs.add(vo);
        //                if(templs.size()==1000)
        //                {
        //                    proEconEquipmentInfoDay1Mapper.insertOrUpdateBath(templs);
        //                    templs=new ArrayList<>();
        //                }
        //            }
        //
        //        }
        //
        //        if(!templs.isEmpty())
        //        {
        //            proEconEquipmentInfoDay1Mapper.insertOrUpdateBath(templs);
        //        }
        //
    }

    private void extracted(Date recordDate, Date end, Date begin, Map<String, ProBasicEquipmentPoint> pointmap, ProEconEquipmentInfoDay1 pewp, List<ProEconEquipmentInfoDay1> pepid1ls) throws Exception {


        if (pewp.getWindpowerstationId().contains(WpType.FDC.id)) {
            //*******************************************日信息统计*********************************************************/
            if (pointmap.containsKey(ContantXk.RPJFS)) {
                ProBasicEquipmentPoint point = pointmap.get(ContantXk.RPJFS);
                List<PointData> pointls = edosUtil.getHistoryDatasSnap(point.getNemCode(), begin.getTime() / 1000, end.getTime() / 1000);
                if (!pointls.isEmpty()) {
                    DoubleSummaryStatistics summaryStatistics = pointls.stream().mapToDouble(PointData::getPointValueInDouble).summaryStatistics();
                    //日最大风速
                    pewp.setRzdfs(summaryStatistics.getMax());
                    //日最小风速
                    pewp.setRzxfs(summaryStatistics.getMin());
                    //日平均风速
                    pewp.setRpjfs(summaryStatistics.getAverage());
                }

            }
        } else {
            //*******************************************日信息统计*********************************************************/
            if (pointmap.containsKey(ContantXk.RPJGZD)) {
                ProBasicEquipmentPoint point = pointmap.get(ContantXk.RPJGZD);
                List<PointData> pointls = edosUtil.getHistoryDatasSnap(point.getNemCode(), begin.getTime() / 1000, end.getTime() / 1000);
                if (!pointls.isEmpty()) {
                    DoubleSummaryStatistics summaryStatistics = pointls.stream().mapToDouble(PointData::getPointValueInDouble).summaryStatistics();
                    //日最大风速
                    pewp.setRzdfs(summaryStatistics.getMax());
                    //日最小风速
                    pewp.setRzxfs(summaryStatistics.getMin());
                    //日平均风速
                    pewp.setRpjfs(summaryStatistics.getAverage());
                }

            }
        }


        if (pointmap.containsKey(ContantXk.RPJGL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.RPJGL);
            List<PointData> pointls = edosUtil.getHistoryDatasSnap(point.getNemCode(), begin.getTime() / 1000, end.getTime() / 1000);
            if (!pointls.isEmpty()) {
                DoubleSummaryStatistics summaryStatistics = pointls.stream().mapToDouble(PointData::getPointValueInDouble).summaryStatistics();
                //日最大功率
                pewp.setRzdgl(summaryStatistics.getMax());
                //日最小功率
                pewp.setRzxgl(summaryStatistics.getMin());
                //日平均功率
                pewp.setRpjgl(summaryStatistics.getSum());
            }
        }


        if (pointmap.containsKey(ContantXk.RFDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.RFDL);

            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //日发电量
                pewp.setRfdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }
        if (pointmap.containsKey(ContantXk.RKYDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.RKYDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //日可用电量
                pewp.setRkydl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }


        if (pointmap.containsKey(ContantXk.RLLFDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.RLLFDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //日理论发电量
                pewp.setRllfdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }


        if (pointmap.containsKey(ContantXk.RGZSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.RGZSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //日故障损失电量
                pewp.setRgzssdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }
        if (pointmap.containsKey(ContantXk.RCNSLGZSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.RCNSLGZSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //日场内受累故障损失电量
                pewp.setRcnslgzssdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }

        if (pointmap.containsKey(ContantXk.RJXSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.RJXSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //日检修损失电量
                pewp.setRjxssdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }

        if (pointmap.containsKey(ContantXk.RCNSLJXSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.RCNSLJXSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //日场内受累检修损失电量
                pewp.setRcnsljxssdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }

        if (pointmap.containsKey(ContantXk.RDJSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.RDJSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //                //日待机损失电量
                //                pewp.setRdjssdl(StringUtils.round(pointValue.getPointValueInDouble(),2));

                pewp.setRdjssdl(0.0);
            }
        }
        if (pointmap.containsKey(ContantXk.RQXJCLSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.RQXJCLSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //日缺陷降出力损失电量
                //                pewp.setRqxjclssdl(StringUtils.round(pointValue.getPointValueInDouble(),2));
                pewp.setRqxjclssdl(0.0);
            }
        }
        if (pointmap.containsKey(ContantXk.RSDTJSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.RSDTJSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //日手动停机损失电量
                //                pewp.setRsdtjssdl(StringUtils.round(pointValue.getPointValueInDouble(),2));
                pewp.setRsdtjssdl(0.0);

            }
        }
        if (pointmap.containsKey(ContantXk.RXNSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.RXNSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //日性能损失电量
                pewp.setRxnssdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));
                //                pewp.setRxnssdl(0.0);

            }
        }

        if (pointmap.containsKey(ContantXk.RXDTJSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.RXDTJSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //日限电停机损失电量
                pewp.setRxdtjssdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));
                pewp.setRxdtjssdl(0.0);
            }
        }


        if (pointmap.containsKey(ContantXk.RXDJCLSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.RXDJCLSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //日限电降出力损失电量
                pewp.setRxdjclssdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));
                pewp.setRxdjclssdl(0.0);
            }
        }

        if (pointmap.containsKey(ContantXk.RCWSLDWSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.RCWSLDWSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //日场外受累电网损失电量
                pewp.setRcwsldwssdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));
                pewp.setRcwsldwssdl(0.0);
            }
        }
        if (pointmap.containsKey(ContantXk.RCWSLTQSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.RCWSLTQSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //日场外受累天气损失电量
                pewp.setRcwsltqssdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));
                pewp.setRcwsltqssdl(0.0);
            }
        }

        if (pointmap.containsKey(ContantXk.RZFDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.RZFDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //日增发电量
                pewp.setRzfdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }

        if (pointmap.containsKey(ContantXk.RQFDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.RQFDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //日欠发电量
                pewp.setRqfdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }
        if (pointmap.containsKey(ContantXk.RZHD)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.RZHD);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //日自耗电
                pewp.setRzhd(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }

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

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

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


            if (pointmap.containsKey(ContantXk.YPJFS)) {
                ProBasicEquipmentPoint point = pointmap.get(ContantXk.YPJFS);
                List<PointData> pointls = edosUtil.getHistoryDatasSnap(point.getNemCode(), begin.getTime() / 1000, end.getTime() / 1000);
                if (!pointls.isEmpty()) {
                    DoubleSummaryStatistics summaryStatistics = pointls.stream().mapToDouble(PointData::getPointValueInDouble).summaryStatistics();
                    //月最大风速
                    pewp.setYzdfs(summaryStatistics.getMax());
                    //月最小风速
                    pewp.setYzxfs(summaryStatistics.getMin());
                    //月平均风速
                    pewp.setYpjfs(summaryStatistics.getAverage());
                }

            }

            if (pointmap.containsKey(ContantXk.YPJGL)) {
                ProBasicEquipmentPoint point = pointmap.get(ContantXk.YPJGL);
                List<PointData> pointls = edosUtil.getHistoryDatasSnap(point.getNemCode(), begin.getTime() / 1000, end.getTime() / 1000);
                if (!pointls.isEmpty()) {
                    DoubleSummaryStatistics summaryStatistics = pointls.stream().mapToDouble(PointData::getPointValueInDouble).summaryStatistics();
                    //月最大功率
                    pewp.setYzdgl(summaryStatistics.getMax());
                    //月最小功率
                    pewp.setYzxgl(summaryStatistics.getMin());
                    //月平均功率
                    pewp.setYpjgl(summaryStatistics.getSum());
                }
            }

        }


        if (pointmap.containsKey(ContantXk.YFDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.YFDL);

            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //月发电量
                pewp.setYfdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }
        if (pointmap.containsKey(ContantXk.YKYDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.YKYDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //月可用电量
                pewp.setYkydl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }


        if (pointmap.containsKey(ContantXk.YLLFDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.YLLFDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //月理论发电量
                pewp.setYllfdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }


        if (pointmap.containsKey(ContantXk.YGZSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.YGZSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //月故障损失电量
                pewp.setYgzssdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }
        if (pointmap.containsKey(ContantXk.YCNSLGZSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.YCNSLGZSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //月场内受累故障损失电量
                pewp.setYcnslgzssdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }

        if (pointmap.containsKey(ContantXk.YJXSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.YJXSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //月检修损失电量
                pewp.setYjxssdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }

        if (pointmap.containsKey(ContantXk.YCNSLJXSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.YCNSLJXSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //月场内受累检修损失电量
                pewp.setYcnsljxssdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }

        if (pointmap.containsKey(ContantXk.YDJSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.YDJSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //月待机损失电量
                pewp.setYdjssdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));
                pewp.setYdjssdl(0.0);
            }
        }
        if (pointmap.containsKey(ContantXk.YQXJCLSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.YQXJCLSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //月缺陷降出力损失电量
                pewp.setYqxjclssdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));
                pewp.setYqxjclssdl(0.0);
            }
        }
        if (pointmap.containsKey(ContantXk.YSDTJSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.YSDTJSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //月手动停机损失电量
                pewp.setYsdtjssdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));
                pewp.setYsdtjssdl(0.0);
            }
        }
        if (pointmap.containsKey(ContantXk.YXNSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.YXNSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //月性能损失电量
                pewp.setYxnssdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));
                pewp.setYxnssdl(0.0);
            }
        }

        if (pointmap.containsKey(ContantXk.YXDTJSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.YXDTJSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //月限电停机损失电量
                pewp.setYxdtjssdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }


        if (pointmap.containsKey(ContantXk.YXDJCLSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.YXDJCLSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //月限电降出力损失电量
                pewp.setYxdjclssdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }

        if (pointmap.containsKey(ContantXk.YCWSLDWSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.YCWSLDWSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //月场外受累电网损失电量
                pewp.setYcwsldwssdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }
        if (pointmap.containsKey(ContantXk.YCWSLTQSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.YCWSLTQSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //月场外受累天气损失电量
                pewp.setYcwsltqssdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }

        if (pointmap.containsKey(ContantXk.YZFDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.YZFDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //月增发电量
                pewp.setYzfdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }

        if (pointmap.containsKey(ContantXk.YQFDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.YQFDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //月欠发电量
                pewp.setYqfdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }
        if (pointmap.containsKey(ContantXk.YZHD)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.YZHD);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //月自耗电
                pewp.setYzhd(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }


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

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

            if (pointmap.containsKey(ContantXk.NPJFS)) {
                ProBasicEquipmentPoint point = pointmap.get(ContantXk.NPJFS);
                List<PointData> pointls = edosUtil.getHistoryDatasSnap(point.getNemCode(), begin.getTime() / 1000, end.getTime() / 1000);
                if (!pointls.isEmpty()) {
                    DoubleSummaryStatistics summaryStatistics = pointls.stream().mapToDouble(PointData::getPointValueInDouble).summaryStatistics();
                    //年最大风速
                    pewp.setNzdfs(summaryStatistics.getMax());
                    //年最小风速
                    pewp.setNzxfs(summaryStatistics.getMin());
                    //年平均风速
                    pewp.setNpjfs(summaryStatistics.getSum());
                }

            }

            if (pointmap.containsKey(ContantXk.NPJGL)) {
                ProBasicEquipmentPoint point = pointmap.get(ContantXk.NPJFS);
                List<PointData> pointls = edosUtil.getHistoryDatasSnap(point.getNemCode(), begin.getTime() / 1000, end.getTime() / 1000);
                if (!pointls.isEmpty()) {
                    DoubleSummaryStatistics summaryStatistics = pointls.stream().mapToDouble(PointData::getPointValueInDouble).summaryStatistics();
                    //年最大功率
                    pewp.setNzdgl(summaryStatistics.getMax());
                    //年最小功率
                    pewp.setNzxgl(summaryStatistics.getMin());
                    //年平均功率
                    pewp.setNpjgl(summaryStatistics.getAverage());
                }
            }

        }

        if (pointmap.containsKey(ContantXk.NFDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.NFDL);

            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //年发电量
                pewp.setNfdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }
        if (pointmap.containsKey(ContantXk.NKYDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.NKYDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //年可用电量
                pewp.setNkydl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }


        if (pointmap.containsKey(ContantXk.NLLFDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.NLLFDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //年理论发电量
                pewp.setNllfdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }


        if (pointmap.containsKey(ContantXk.NGZSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.NGZSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //年故障损失电量
                pewp.setNgzssdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }
        if (pointmap.containsKey(ContantXk.NCNSLGZSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.NCNSLGZSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //年场内受累故障损失电量
                pewp.setNcnslgzssdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }

        if (pointmap.containsKey(ContantXk.NJXSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.NJXSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //年检修损失电量
                pewp.setNjxssdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }

        if (pointmap.containsKey(ContantXk.NCNSLJXSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.NCNSLJXSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //年场内受累检修损失电量
                pewp.setNcnsljxssdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }

        if (pointmap.containsKey(ContantXk.NDJSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.NDJSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //年待机损失电量
                //                pewp.setNdjssdl(StringUtils.round(pointValue.getPointValueInDouble(),2));
                pewp.setNdjssdl(0.0);
            }
        }
        if (pointmap.containsKey(ContantXk.NQXJCLSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.NQXJCLSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //年缺陷降出力损失电量
                //                pewp.setNqxjclssdl(StringUtils.round(pointValue.getPointValueInDouble(),2));
                pewp.setNqxjclssdl(0.0);
            }
        }
        if (pointmap.containsKey(ContantXk.NSDTJSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.NSDTJSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //年手动停机损失电量
                pewp.setNsdtjssdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));
                pewp.setNsdtjssdl(0.0);
            }
        }
        if (pointmap.containsKey(ContantXk.NXNSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.NXNSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //年性能损失电量
                pewp.setNxnssdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));
                pewp.setNxnssdl(0.0);
            }
        }

        if (pointmap.containsKey(ContantXk.NXDTJSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.NXDTJSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //年限电停机损失电量
                pewp.setNxdtjssdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }


        if (pointmap.containsKey(ContantXk.NXDJCLSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.NXDJCLSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //年限电降出力损失电量
                pewp.setNxdjclssdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }

        if (pointmap.containsKey(ContantXk.NCWSLDWSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.NCWSLDWSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //年场外受累电网损失电量
                pewp.setNcwsldwssdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }
        if (pointmap.containsKey(ContantXk.NCWSLTQSSDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.NCWSLTQSSDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //年场外受累天气损失电量
                //   pewp.setNcwsltqssdl(StringUtils.round(pointValue.getPointValueInDouble(),2));
                pewp.setNcwsltqssdl(0.0);
            }
        }

        if (pointmap.containsKey(ContantXk.NZFDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.NZFDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //年增发电量
                pewp.setNzfdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }

        if (pointmap.containsKey(ContantXk.NQFDL)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.NQFDL);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //年欠发电量
                pewp.setNqfdl(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }
        if (pointmap.containsKey(ContantXk.NZHD)) {
            ProBasicEquipmentPoint point = pointmap.get(ContantXk.NZHD);
            PointData pointValue = edosUtil.getHistMatrix(point.getNemCode(), recordDate.getTime() / 1000);
            if (StringUtils.notEmp(pointValue)) {
                //年自耗电
                pewp.setNzhd(StringUtils.round(pointValue.getPointValueInDouble(), 2));

            }
        }
    }

    private void setPowerandSpeedYear(ProEconEquipmentInfoDay1 pewp) {
        //年最大风速
        pewp.setNzdfs(pewp.getRzdfs());
        //年最小风速
        pewp.setNzxfs(pewp.getRzxfs());
        //年平均风速
        pewp.setNpjfs(pewp.getRpjfs());

        //年最大功率
        pewp.setNzdgl(pewp.getRzdgl());
        //年最小功率
        pewp.setNzxgl(pewp.getRzxgl());
        //年平均功率
        pewp.setNpjgl(pewp.getRpjgl());

    }

    private void setPowerandSpeedMonth(ProEconEquipmentInfoDay1 pewp) {

        //月最大风速
        pewp.setYzdfs(pewp.getRzdfs());
        //月最小风速
        pewp.setYzxfs(pewp.getRzxfs());
        //月平均风速
        pewp.setYpjfs(pewp.getRpjfs());

        //月最大功率
        pewp.setYzdgl(pewp.getRzdgl());
        //月最小功率
        pewp.setYzxgl(pewp.getRzxgl());
        //月平均功率
        pewp.setYpjgl(pewp.getRpjgl());
    }


}







