package com.gyee.generation.service.realtimelibrary;

/*
@author   谢生杰
@date   2022/11/1-9:38
*/


import com.gyee.common.contant.ContantXk;
import com.gyee.common.model.PointData;
import com.gyee.common.util.DateUtils;
import com.gyee.generation.init.CacheContext;
import com.gyee.generation.model.auto.*;
import com.gyee.generation.service.auto.IProBasicMeterPointService;
import com.gyee.generation.util.PointUtil;
import com.gyee.generation.util.StringUtils;
import com.gyee.generation.util.realtimesource.IEdosUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

@Service
@Slf4j
public class ElectricityMeteringService {

    @Resource
    private IProBasicMeterPointService meterPointService;
    @Resource
    private IEdosUtil edosUtil;

    /**
     * 存储电计量实时数据
     *
     * @throws Exception
     */
    public void saveGenerationDatas() throws Exception {

        List<ProBasicLine> lnls = CacheContext.lnls;
        List<ProBasicMeterPoint> meterpoint = CacheContext.meterpoints;
        List<ProBasicProject> projects = CacheContext.pjls;
        List<ProBasicPowerstation> wpls = CacheContext.wpls;
        Map<String, Map<String, ProBasicPowerstationPoint>> linepointmap = CacheContext.linepointmap;
        Map<String, Map<String, ProBasicPowerstationPoint>> propointmap = CacheContext.propointmap;
        Map<String, Map<String, ProBasicPowerstationPoint>> wppointmap = CacheContext.wppointmap;
        Map<String, List<ProBasicLine>> prolinemap = CacheContext.prolinemap;
        Map<String, List<ProBasicProject>> wppromap = CacheContext.wppromap;
        Date samedayZero = DateUtils.getSamedayZero();
        Date monthFirstZero = DateUtils.getMonthFirstZero();
        Date yearFirstZero = DateUtils.getYearFirstZero();
        Date currentDate = DateUtils.getCurrentDate();
        Date tomorrow = DateUtils.addDays(samedayZero, 1);
        Date date = DateUtils.addSeconds(currentDate, -1);

        List<PointData> lineResultList = new ArrayList<>();
        lnls.stream().forEach(line -> {
            Map<String, ProBasicPowerstationPoint> proBasicWppointMap = linepointmap.get(line.getId());
            //线路发电量
            log.info(line.getName() + "电计量电量计算开始！");
            List<ProBasicMeterPoint> meterpoints = meterpoint.stream().filter(me -> null != me.getLineId()).collect(Collectors.toList());
            Optional<ProBasicMeterPoint> fdlOptional = meterpoints.stream().filter(me -> me.getLineId().equals(line.getId()) && "ZXYG".equals(me.getUniformCode()) && "进线".equals(me.getMeterType()) && "主".equals(me.getMeterSort())).findFirst();
            getLineFdl(line.getId(), samedayZero, monthFirstZero, yearFirstZero, date, currentDate, lineResultList, proBasicWppointMap, fdlOptional);
            log.info(line.getName() + "电计量电量计算完毕！");

            //线路上网电量
            log.info(line.getName() + "电计量上网电量计算开始！");
            List<PointData> fdlList = new ArrayList<>();
            Optional<ProBasicMeterPoint> swdlOptional = meterpoints.stream().filter(me -> me.getUniformCode().equals("ZXYG") && me.getMeterType().equals("出线") && me.getMeterSort().equals("主") && me.getLineId().contains(line.getId())).findFirst();
            getLineOtherFdl(linepointmap, samedayZero, monthFirstZero, yearFirstZero, date, currentDate, lineResultList, line, proBasicWppointMap, fdlOptional, fdlList, swdlOptional);
            log.info(line.getName() + "电计量上网电量计算完毕！");
            //线路购网电量
            log.info(line.getName() + "电计量购网电量计算开始！");
            Optional<ProBasicMeterPoint> gwdlOptional = meterpoints.stream().filter(me -> me.getUniformCode().equals("FXYG") && me.getMeterType().equals("出线") && me.getMeterSort().equals("主") && me.getLineId().contains(line.getId())).findFirst();
            getLineOtherFdl(linepointmap, samedayZero, monthFirstZero, yearFirstZero, date, currentDate, lineResultList, line, proBasicWppointMap, fdlOptional, fdlList, gwdlOptional);
            log.info(line.getName() + "电计量购网电量计算完毕！");

            //线路厂用电量
            log.info(line.getName() + "电计量厂用电量计算开始！");
            Optional<ProBasicMeterPoint> zybOptional = meterpoints.stream().filter(me -> me.getUniformCode().equals("ZXYG") && me.getMeterType().equals("站用变") && me.getMeterSort().equals("主") && me.getLineId().contains(line.getId())).findFirst();
            getLineOtherFdl(linepointmap, samedayZero, monthFirstZero, yearFirstZero, date, currentDate, lineResultList, line, proBasicWppointMap, fdlOptional, fdlList, zybOptional);
            log.info(line.getName() + "电计量厂用电量计算完毕！");

            //农网购网电量
            log.info(line.getName() + "农网购网电量计算开始！");
            setZero(lineResultList, date, currentDate, proBasicWppointMap, ContantXk.RNWGWDL, ContantXk.YNWGWDL, ContantXk.NNWGWDL);
            log.info(line.getName() + "农网购网电量计算完毕！");
        });
        //线路存储
        edosUtil.sendMultiPoint(lineResultList);

        List<PointData> proResultList = new ArrayList<>();
        projects.stream().forEach(pro -> {
            Map<String, ProBasicPowerstationPoint> proBasicWppointMap = propointmap.get(pro.getId());
            List<ProBasicLine> proBasicLines = prolinemap.get(pro.getId());
            AtomicReference<Double> rfdl = new AtomicReference<>((double) 0);
            AtomicReference<Double> yfdl = new AtomicReference<>((double) 0);
            AtomicReference<Double> nfdl = new AtomicReference<>((double) 0);
            AtomicReference<Double> rswdl = new AtomicReference<>((double) 0);
            AtomicReference<Double> yswdl = new AtomicReference<>((double) 0);
            AtomicReference<Double> nswdl = new AtomicReference<>((double) 0);
            AtomicReference<Double> rgwgwdl = new AtomicReference<>((double) 0);
            AtomicReference<Double> ygwgwdl = new AtomicReference<>((double) 0);
            AtomicReference<Double> ngwgwdl = new AtomicReference<>((double) 0);
            AtomicReference<Double> rnwgwdl = new AtomicReference<>((double) 0);
            AtomicReference<Double> ynwgwdl = new AtomicReference<>((double) 0);
            AtomicReference<Double> nnwgwdl = new AtomicReference<>((double) 0);
            AtomicReference<Double> rcydl = new AtomicReference<>((double) 0);
            AtomicReference<Double> ycydl = new AtomicReference<>((double) 0);
            AtomicReference<Double> ncydl = new AtomicReference<>((double) 0);
            proBasicLines.stream().forEach(line -> {
                try {
                    double linerfdl = getLineValue(line.getId(), ContantXk.RFDLSYZ, currentDate, linepointmap);
                    rfdl.updateAndGet(v -> new Double((double) (v + linerfdl)));
                    double lineyfdl = getLineValue(line.getId(), ContantXk.YFDLSYZ, currentDate, linepointmap);
                    yfdl.updateAndGet(v -> new Double((double) (v + lineyfdl)));
                    double linenfdl = getLineValue(line.getId(), ContantXk.NFDLSYZ, currentDate, linepointmap);
                    nfdl.updateAndGet(v -> new Double((double) (v + linenfdl)));
                    double linerswdl = getLineValue(line.getId(), ContantXk.RSWDL, currentDate, linepointmap);
                    rswdl.updateAndGet(v -> new Double((double) (v + linerswdl)));
                    double lineyswdl = getLineValue(line.getId(), ContantXk.YSWDL, currentDate, linepointmap);
                    yswdl.updateAndGet(v -> new Double((double) (v + lineyswdl)));
                    double linenswdl = getLineValue(line.getId(), ContantXk.NSWDL, currentDate, linepointmap);
                    nswdl.updateAndGet(v -> new Double((double) (v + linenswdl)));
                    double linergwgwdl = getLineValue(line.getId(), ContantXk.RGWGWDL, currentDate, linepointmap);
                    rgwgwdl.updateAndGet(v -> new Double((double) (v + linergwgwdl)));
                    double lineygwgwdl = getLineValue(line.getId(), ContantXk.YGWGWDL, currentDate, linepointmap);
                    ygwgwdl.updateAndGet(v -> new Double((double) (v + lineygwgwdl)));
                    double linengwgwdl = getLineValue(line.getId(), ContantXk.NGWGWDL, currentDate, linepointmap);
                    ngwgwdl.updateAndGet(v -> new Double((double) (v + linengwgwdl)));
                    double linernwgwdl = getLineValue(line.getId(), ContantXk.RNWGWDL, currentDate, linepointmap);
                    rnwgwdl.updateAndGet(v -> new Double((double) (v + linernwgwdl)));
                    double lineynwgwdl = getLineValue(line.getId(), ContantXk.YNWGWDL, currentDate, linepointmap);
                    ynwgwdl.updateAndGet(v -> new Double((double) (v + lineynwgwdl)));
                    double linennwgwdl = getLineValue(line.getId(), ContantXk.NNWGWDL, currentDate, linepointmap);
                    nnwgwdl.updateAndGet(v -> new Double((double) (v + linennwgwdl)));
                    double linercydl = getLineValue(line.getId(), ContantXk.RCYDL, currentDate, linepointmap);
                    rcydl.updateAndGet(v -> new Double((double) (v + linercydl)));
                    double lineycydl = getLineValue(line.getId(), ContantXk.YCYDL, currentDate, linepointmap);
                    ycydl.updateAndGet(v -> new Double((double) (v + lineycydl)));
                    double linencydl = getLineValue(line.getId(), ContantXk.NCYDL, currentDate, linepointmap);
                    ncydl.updateAndGet(v -> new Double((double) (v + linencydl)));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            ProBasicPowerstationPoint rfdlpoint = proBasicWppointMap.get(ContantXk.RFDLSYZ);
            proResultList.add(PointUtil.createPointData(date, rfdl.get(), rfdlpoint.getNemCode(), rfdlpoint.getName()));
            ProBasicPowerstationPoint yfdlpoint = proBasicWppointMap.get(ContantXk.YFDLSYZ);
            proResultList.add(PointUtil.createPointData(date, yfdl.get(), yfdlpoint.getNemCode(), yfdlpoint.getName()));
            ProBasicPowerstationPoint nfdlpoint = proBasicWppointMap.get(ContantXk.NFDLSYZ);
            proResultList.add(PointUtil.createPointData(date, nfdl.get(), nfdlpoint.getNemCode(), nfdlpoint.getName()));

            ProBasicPowerstationPoint rswdlpoint = proBasicWppointMap.get(ContantXk.RSWDL);
            proResultList.add(PointUtil.createPointData(date, rswdl.get(), rswdlpoint.getNemCode(), rswdlpoint.getName()));
            ProBasicPowerstationPoint yswdlpoint = proBasicWppointMap.get(ContantXk.YSWDL);
            proResultList.add(PointUtil.createPointData(date, yswdl.get(), yswdlpoint.getNemCode(), yswdlpoint.getName()));
            ProBasicPowerstationPoint nswdlpoint = proBasicWppointMap.get(ContantXk.NSWDL);
            proResultList.add(PointUtil.createPointData(date, nswdl.get(), nswdlpoint.getNemCode(), nswdlpoint.getName()));

            ProBasicPowerstationPoint rgwgwdlpoint = proBasicWppointMap.get(ContantXk.RGWGWDL);
            proResultList.add(PointUtil.createPointData(date, rgwgwdl.get(), rgwgwdlpoint.getNemCode(), rgwgwdlpoint.getName()));
            ProBasicPowerstationPoint ygwgwdlpoint = proBasicWppointMap.get(ContantXk.YGWGWDL);
            proResultList.add(PointUtil.createPointData(date, ygwgwdl.get(), ygwgwdlpoint.getNemCode(), ygwgwdlpoint.getName()));
            ProBasicPowerstationPoint ngwgwdlpoint = proBasicWppointMap.get(ContantXk.NGWGWDL);
            proResultList.add(PointUtil.createPointData(date, ngwgwdl.get(), ngwgwdlpoint.getNemCode(), ngwgwdlpoint.getName()));

            ProBasicPowerstationPoint rnwgwdlpoint = proBasicWppointMap.get(ContantXk.RNWGWDL);
            proResultList.add(PointUtil.createPointData(date, rnwgwdl.get(), rnwgwdlpoint.getNemCode(), rnwgwdlpoint.getName()));
            ProBasicPowerstationPoint ynwgwdlpoint = proBasicWppointMap.get(ContantXk.YNWGWDL);
            proResultList.add(PointUtil.createPointData(date, ynwgwdl.get(), ynwgwdlpoint.getNemCode(), ynwgwdlpoint.getName()));
            ProBasicPowerstationPoint nnwgwdlpoint = proBasicWppointMap.get(ContantXk.NNWGWDL);
            proResultList.add(PointUtil.createPointData(date, nnwgwdl.get(), nnwgwdlpoint.getNemCode(), nnwgwdlpoint.getName()));

            ProBasicPowerstationPoint rcydlpoint = proBasicWppointMap.get(ContantXk.RCYDL);
            proResultList.add(PointUtil.createPointData(date, rcydl.get(), rcydlpoint.getNemCode(), rcydlpoint.getName()));
            ProBasicPowerstationPoint ycydlpoint = proBasicWppointMap.get(ContantXk.YCYDL);
            proResultList.add(PointUtil.createPointData(date, ycydl.get(), ycydlpoint.getNemCode(), ycydlpoint.getName()));
            ProBasicPowerstationPoint ncydlpoint = proBasicWppointMap.get(ContantXk.NCYDL);
            proResultList.add(PointUtil.createPointData(date, ncydl.get(), ncydlpoint.getNemCode(), ncydlpoint.getName()));

        });
        //期次存储
        edosUtil.sendMultiPoint(proResultList);

        List<PointData> wpResultList = new ArrayList<>();
        wpls.stream().forEach(wp -> {
            Map<String, ProBasicPowerstationPoint> wpBasicWppointMap = wppointmap.get(wp.getId());
            List<ProBasicProject> proBasicPros = wppromap.get(wp.getId());
            AtomicReference<Double> rfdl = new AtomicReference<>((double) 0);
            AtomicReference<Double> yfdl = new AtomicReference<>((double) 0);
            AtomicReference<Double> nfdl = new AtomicReference<>((double) 0);
            AtomicReference<Double> rswdl = new AtomicReference<>((double) 0);
            AtomicReference<Double> yswdl = new AtomicReference<>((double) 0);
            AtomicReference<Double> nswdl = new AtomicReference<>((double) 0);
            AtomicReference<Double> rgwgwdl = new AtomicReference<>((double) 0);
            AtomicReference<Double> ygwgwdl = new AtomicReference<>((double) 0);
            AtomicReference<Double> ngwgwdl = new AtomicReference<>((double) 0);
            AtomicReference<Double> rnwgwdl = new AtomicReference<>((double) 0);
            AtomicReference<Double> ynwgwdl = new AtomicReference<>((double) 0);
            AtomicReference<Double> nnwgwdl = new AtomicReference<>((double) 0);
            AtomicReference<Double> rcydl = new AtomicReference<>((double) 0);
            AtomicReference<Double> ycydl = new AtomicReference<>((double) 0);
            AtomicReference<Double> ncydl = new AtomicReference<>((double) 0);
            proBasicPros.stream().forEach(line -> {    //期次
                try {
                    double linerfdl = getLineValue(line.getId(), ContantXk.RFDLSYZ, currentDate, propointmap);
                    rfdl.updateAndGet(v -> new Double((double) (v + linerfdl)));
                    double lineyfdl = getLineValue(line.getId(), ContantXk.YFDLSYZ, currentDate, propointmap);
                    yfdl.updateAndGet(v -> new Double((double) (v + lineyfdl)));
                    double linenfdl = getLineValue(line.getId(), ContantXk.NFDLSYZ, currentDate, propointmap);
                    nfdl.updateAndGet(v -> new Double((double) (v + linenfdl)));
                    double linerswdl = getLineValue(line.getId(), ContantXk.RSWDL, currentDate, propointmap);
                    rswdl.updateAndGet(v -> new Double((double) (v + linerswdl)));
                    double lineyswdl = getLineValue(line.getId(), ContantXk.YSWDL, currentDate, propointmap);
                    yswdl.updateAndGet(v -> new Double((double) (v + lineyswdl)));
                    double linenswdl = getLineValue(line.getId(), ContantXk.NSWDL, currentDate, propointmap);
                    nswdl.updateAndGet(v -> new Double((double) (v + linenswdl)));
                    double linergwgwdl = getLineValue(line.getId(), ContantXk.RGWGWDL, currentDate, propointmap);
                    rgwgwdl.updateAndGet(v -> new Double((double) (v + linergwgwdl)));
                    double lineygwgwdl = getLineValue(line.getId(), ContantXk.YGWGWDL, currentDate, propointmap);
                    ygwgwdl.updateAndGet(v -> new Double((double) (v + lineygwgwdl)));
                    double linengwgwdl = getLineValue(line.getId(), ContantXk.NGWGWDL, currentDate, propointmap);
                    ngwgwdl.updateAndGet(v -> new Double((double) (v + linengwgwdl)));
                    double linernwgwdl = getLineValue(line.getId(), ContantXk.RNWGWDL, currentDate, propointmap);
                    rnwgwdl.updateAndGet(v -> new Double((double) (v + linernwgwdl)));
                    double lineynwgwdl = getLineValue(line.getId(), ContantXk.YNWGWDL, currentDate, propointmap);
                    ynwgwdl.updateAndGet(v -> new Double((double) (v + lineynwgwdl)));
                    double linennwgwdl = getLineValue(line.getId(), ContantXk.NNWGWDL, currentDate, propointmap);
                    nnwgwdl.updateAndGet(v -> new Double((double) (v + linennwgwdl)));
                    double linercydl = getLineValue(line.getId(), ContantXk.RCYDL, currentDate, propointmap);
                    rcydl.updateAndGet(v -> new Double((double) (v + linercydl)));
                    double lineycydl = getLineValue(line.getId(), ContantXk.YCYDL, currentDate, propointmap);
                    ycydl.updateAndGet(v -> new Double((double) (v + lineycydl)));
                    double linencydl = getLineValue(line.getId(), ContantXk.NCYDL, currentDate, propointmap);
                    ncydl.updateAndGet(v -> new Double((double) (v + linencydl)));
                } catch (Exception e) {

                    e.printStackTrace();
                }
            });
            ProBasicPowerstationPoint rfdlpoint = wpBasicWppointMap.get(ContantXk.RFDLSYZ);
            wpResultList.add(PointUtil.createPointData(date, rfdl.get(), rfdlpoint.getNemCode(), rfdlpoint.getName()));
            ProBasicPowerstationPoint yfdlpoint = wpBasicWppointMap.get(ContantXk.YFDLSYZ);
            wpResultList.add(PointUtil.createPointData(date, yfdl.get(), yfdlpoint.getNemCode(), yfdlpoint.getName()));
            ProBasicPowerstationPoint nfdlpoint = wpBasicWppointMap.get(ContantXk.NFDLSYZ);
            wpResultList.add(PointUtil.createPointData(date, nfdl.get(), nfdlpoint.getNemCode(), nfdlpoint.getName()));

            ProBasicPowerstationPoint rswdlpoint = wpBasicWppointMap.get(ContantXk.RSWDL);
            wpResultList.add(PointUtil.createPointData(date, rswdl.get(), rswdlpoint.getNemCode(), rswdlpoint.getName()));
            ProBasicPowerstationPoint yswdlpoint = wpBasicWppointMap.get(ContantXk.YSWDL);
            wpResultList.add(PointUtil.createPointData(date, yswdl.get(), yswdlpoint.getNemCode(), yswdlpoint.getName()));
            ProBasicPowerstationPoint nswdlpoint = wpBasicWppointMap.get(ContantXk.NSWDL);
            wpResultList.add(PointUtil.createPointData(date, nswdl.get(), nswdlpoint.getNemCode(), nswdlpoint.getName()));

            ProBasicPowerstationPoint rgwgwdlpoint = wpBasicWppointMap.get(ContantXk.RGWGWDL);
            wpResultList.add(PointUtil.createPointData(date, rgwgwdl.get(), rgwgwdlpoint.getNemCode(), rgwgwdlpoint.getName()));
            ProBasicPowerstationPoint ygwgwdlpoint = wpBasicWppointMap.get(ContantXk.YGWGWDL);
            wpResultList.add(PointUtil.createPointData(date, ygwgwdl.get(), ygwgwdlpoint.getNemCode(), ygwgwdlpoint.getName()));
            ProBasicPowerstationPoint ngwgwdlpoint = wpBasicWppointMap.get(ContantXk.NGWGWDL);
            wpResultList.add(PointUtil.createPointData(date, ngwgwdl.get(), ngwgwdlpoint.getNemCode(), ngwgwdlpoint.getName()));

            ProBasicPowerstationPoint rnwgwdlpoint = wpBasicWppointMap.get(ContantXk.RNWGWDL);
            wpResultList.add(PointUtil.createPointData(date, rnwgwdl.get(), rnwgwdlpoint.getNemCode(), rnwgwdlpoint.getName()));
            ProBasicPowerstationPoint ynwgwdlpoint = wpBasicWppointMap.get(ContantXk.YNWGWDL);
            wpResultList.add(PointUtil.createPointData(date, ynwgwdl.get(), ynwgwdlpoint.getNemCode(), ynwgwdlpoint.getName()));
            ProBasicPowerstationPoint nnwgwdlpoint = wpBasicWppointMap.get(ContantXk.NNWGWDL);
            wpResultList.add(PointUtil.createPointData(date, nnwgwdl.get(), nnwgwdlpoint.getNemCode(), nnwgwdlpoint.getName()));

            ProBasicPowerstationPoint rcydlpoint = wpBasicWppointMap.get(ContantXk.RCYDL);
            wpResultList.add(PointUtil.createPointData(date, rcydl.get(), rcydlpoint.getNemCode(), rcydlpoint.getName()));
            ProBasicPowerstationPoint ycydlpoint = wpBasicWppointMap.get(ContantXk.YCYDL);
            wpResultList.add(PointUtil.createPointData(date, ycydl.get(), ycydlpoint.getNemCode(), ycydlpoint.getName()));
            ProBasicPowerstationPoint ncydlpoint = wpBasicWppointMap.get(ContantXk.NCYDL);
            wpResultList.add(PointUtil.createPointData(date, ncydl.get(), ncydlpoint.getNemCode(), ncydlpoint.getName()));

        });
        //场站存储
        edosUtil.sendMultiPoint(wpResultList);
    }

    private double getLineValue(String lineid, String uniformcode, Date currentDate, Map<String, Map<String, ProBasicPowerstationPoint>> linepointmap) throws Exception {
        ProBasicPowerstationPoint proBasicWppoint = null;
        try {
            proBasicWppoint = linepointmap.get(lineid).get(uniformcode);
        } catch (Exception e) {
            linepointmap.get(lineid).get(uniformcode);
        }
        return edosUtil.getSectionData(proBasicWppoint, currentDate.getTime()).getPointValueInDouble();
    }


    private void setZero(List<PointData> resultList, Date date, Date currentDate, Map<String, ProBasicPowerstationPoint> linemap, String rcode, String ycode, String ncode) {
        //线路日发电量
        PointData r = new PointData();
        r.setPointTime(date.getTime());
        r.setPointValue("0");
        r.setPointValueInDouble(0);
        r.setEdnaId(linemap.get(rcode).getNemCode());
        r.setPointName(linemap.get(rcode).getName());
        resultList.add(r);
        //线路月发电量
        PointData y = new PointData();
        y.setPointTime(date.getTime());
        y.setPointValue("0");
        y.setPointValueInDouble(0);
        y.setEdnaId(linemap.get(ycode).getNemCode());
        y.setPointName(linemap.get(ycode).getName());
        resultList.add(y);
        //线路年发电量
        PointData n = new PointData();
        n.setPointTime(date.getTime());
        n.setPointValue("0");
        n.setPointValueInDouble(0);
        n.setEdnaId(linemap.get(ncode).getNemCode());
        n.setPointName(linemap.get(ncode).getName());
        resultList.add(n);
    }

    private void getLineOtherFdl(Map<String, Map<String, ProBasicPowerstationPoint>> linepointmap, Date samedayZero, Date monthFirstZero, Date yearFirstZero, Date date, Date currentDate, List<PointData> lineResultList, ProBasicLine line, Map<String, ProBasicPowerstationPoint> proBasicWppointMap, Optional<ProBasicMeterPoint> fdlOptional, List<PointData> fdlList, Optional<ProBasicMeterPoint> swdlOptional) {
        if (swdlOptional.isPresent()) {
            ProBasicMeterPoint swdlMeterpoint = swdlOptional.get();
            if (StringUtils.isEmpty(fdlList)) {
                String[] lines = swdlMeterpoint.getLineId().split("\\|");
                Arrays.stream(lines).forEach(l -> {
                    Optional<ProBasicMeterPoint> fdOptional = CacheContext.meterpoints.stream().filter(me -> me.getLineId().equals(l) && me.getUniformCode().equals("ZXYG") && me.getMeterType().equals("进线") && me.getMeterSort().equals("主")).findFirst();

                    getLineFdl(l, samedayZero, monthFirstZero, yearFirstZero, date, currentDate, fdlList, proBasicWppointMap, fdOptional);
                });
            }
            //获取电量百分比，算其他电量
            Map<String, Double> percentageMap = getPercentage(fdlList, swdlMeterpoint, linepointmap, line);

            String pointid = swdlMeterpoint.getNemCode();
            Double real = 0.0;
            Double sameday = 0.0;
            Double month = 0.0;
            Double year = 0.0;
            try {
                real = edosUtil.getSectionData(pointid, currentDate.getTime()).getPointValueInDouble();
                sameday = edosUtil.getSectionData(pointid, samedayZero.getTime()).getPointValueInDouble();
                month = edosUtil.getSectionData(pointid, monthFirstZero.getTime()).getPointValueInDouble();
                year = edosUtil.getSectionData(pointid, yearFirstZero.getTime()).getPointValueInDouble();

                Double magnification = swdlMeterpoint.getMagnification();
                Double xs = swdlMeterpoint.getXs();


                //线路上网电量
                //                double xlswdl = (real - sameday) * magnification / xs * percentageMap.get("percentage");

                double xlswdl = 0.0;
                if (StringUtils.notEmp(xs) && xs != 0) {
                    xlswdl = new BigDecimal(real - sameday).multiply(new BigDecimal(magnification)).divide(new BigDecimal(xs), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(percentageMap.get("percentage"))).doubleValue();
                }
                ProBasicPowerstationPoint rpoint = proBasicWppointMap.get(ContantXk.RSWDL);
                PointData rpointData = PointUtil.createPointData(date, xlswdl, rpoint.getNemCode(), rpoint.getName());
                lineResultList.add(rpointData);
                //线路月上网发电量
                //                double xlyswdl = (real - month) * magnification / xs * percentageMap.get("ypercentage");

                double xlyswdl = 0.0;
                if (StringUtils.notEmp(xs) && xs != 0) {
                    xlyswdl = new BigDecimal(real - month).multiply(new BigDecimal(magnification)).divide(new BigDecimal(xs), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(percentageMap.get("ypercentage"))).doubleValue();
                }
                ProBasicPowerstationPoint ypoint = proBasicWppointMap.get(ContantXk.YSWDL);
                PointData ypointData = PointUtil.createPointData(date, xlyswdl, ypoint.getNemCode(), ypoint.getName());
                lineResultList.add(ypointData);
                //线路年上网发电量
                //                double xlnswfdl = (real - year) * magnification / xs * percentageMap.get("npercentage");

                double xlnswfdl = 0.0;
                if (StringUtils.notEmp(xs) && xs != 0) {
                    xlnswfdl = new BigDecimal(real - year).multiply(new BigDecimal(magnification)).divide(new BigDecimal(xs), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(percentageMap.get("npercentage"))).doubleValue();
                }
                ProBasicPowerstationPoint npoint = proBasicWppointMap.get(ContantXk.NSWDL);
                PointData npointData = PointUtil.createPointData(date, xlnswfdl, npoint.getNemCode(), npoint.getName());
                lineResultList.add(npointData);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void getLineFdl(String line, Date samedayZero, Date monthFirstZero, Date yearFirstZero, Date date, Date currentDate, List<PointData> lineResultList, Map<String, ProBasicPowerstationPoint> proBasicWppointMap, Optional<ProBasicMeterPoint> fdlOptional) {
        if (fdlOptional.isPresent()) {
            ProBasicMeterPoint proBasicMeterPoint = fdlOptional.get();
            String pointid = proBasicMeterPoint.getNemCode();
            Double real = 0.0;
            Double sameday = 0.0;
            Double month = 0.0;
            Double year = 0.0;
            try {
                real = edosUtil.getSectionData(pointid, currentDate.getTime()).getPointValueInDouble();
                sameday = edosUtil.getSectionData(pointid, samedayZero.getTime()).getPointValueInDouble();
                month = edosUtil.getSectionData(pointid, monthFirstZero.getTime()).getPointValueInDouble();
                year = edosUtil.getSectionData(pointid, yearFirstZero.getTime()).getPointValueInDouble();

                Double magnification = fdlOptional.get().getMagnification();
                Double xs = proBasicMeterPoint.getXs();

                Map<String, ProBasicPowerstationPoint> powerstationPointMap = CacheContext.linepointmap.get(line);

                //线路日发电量
                //                double xlrfdl = (real - sameday) * magnification / xs;
                double xlrfdl = 0.0;
                if (StringUtils.notEmp(xs) && xs != 0) {
                    xlrfdl = new BigDecimal(real - sameday).multiply(new BigDecimal(magnification)).divide(new BigDecimal(xs), 2, RoundingMode.HALF_EVEN).doubleValue();
                }

                ProBasicPowerstationPoint rpoint = powerstationPointMap.get(ContantXk.RFDLSYZ);
                PointData r = PointUtil.createPointData(date, xlrfdl, rpoint.getNemCode(), rpoint.getName());
                lineResultList.add(r);
                //线路月发电量
                //                double xlyfdl = (real - month) * magnification / xs;

                double xlyfdl = 0.0;
                if (StringUtils.notEmp(xs) && xs != 0) {
                    xlyfdl = new BigDecimal(real - month).multiply(new BigDecimal(magnification)).divide(new BigDecimal(xs), 2, RoundingMode.HALF_EVEN).doubleValue();
                }
                ProBasicPowerstationPoint ypoint = powerstationPointMap.get(ContantXk.YFDLSYZ);
                PointData y = PointUtil.createPointData(date, xlyfdl, ypoint.getNemCode(), ypoint.getName());
                lineResultList.add(y);
                //线路年发电量
                //                double xlnfdl = (real - year) * magnification / xs;

                double xlnfdl = 0.0;
                if (StringUtils.notEmp(xs) && xs != 0) {
                    xlnfdl = new BigDecimal(real - year).multiply(new BigDecimal(magnification)).divide(new BigDecimal(xs), 2, RoundingMode.HALF_EVEN).doubleValue();
                }
                ProBasicPowerstationPoint npoint = powerstationPointMap.get(ContantXk.NFDLSYZ);
                PointData n = PointUtil.createPointData(date, xlnfdl, npoint.getNemCode(), npoint.getName());
                lineResultList.add(n);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private Map<String, Double> getPercentage(List<PointData> fdlList, ProBasicMeterPoint swdlMeterpoint, Map<String, Map<String, ProBasicPowerstationPoint>> linepointmap, ProBasicLine line) {

        Map<String, Double> percentageMap = new HashMap<>();
        String[] swdlLines = swdlMeterpoint.getLineId().split("\\|");
        AtomicReference<Double> xldl = new AtomicReference<>((double) 0);
        AtomicReference<Double> xlzdl = new AtomicReference<>((double) 0);
        AtomicReference<Double> xlydl = new AtomicReference<>((double) 0);
        AtomicReference<Double> xlzydl = new AtomicReference<>((double) 0);
        AtomicReference<Double> xlndl = new AtomicReference<>((double) 0);
        AtomicReference<Double> xlzndl = new AtomicReference<>((double) 0);
        Arrays.stream(swdlLines).forEach(l -> {
            Optional<PointData> xldlfirst = fdlList.stream().filter(res -> res.getEdnaId().equals(linepointmap.get(l).get(ContantXk.RFDLSYZ).getNemCode())).findFirst();
            if (xldlfirst.isPresent()) {
                if (l.equals(line.getId())) {
                    xldl.set(xldlfirst.get().getPointValueInDouble());
                    xlzdl.updateAndGet(v -> new Double((double) (v + xldlfirst.get().getPointValueInDouble())));
                } else {
                    xlzdl.updateAndGet(v -> new Double((double) (v + xldlfirst.get().getPointValueInDouble())));
                }
            }
            Optional<PointData> xlydlfirst = fdlList.stream().filter(res -> res.getEdnaId().equals(linepointmap.get(l).get(ContantXk.YFDLSYZ).getNemCode())).findFirst();
            if (xlydlfirst.isPresent()) {
                if (l.equals(line.getId())) {
                    xlydl.set(xlydlfirst.get().getPointValueInDouble());
                    xlzydl.updateAndGet(v -> new Double((double) (v + xlydlfirst.get().getPointValueInDouble())));
                } else {
                    xlzydl.updateAndGet(v -> new Double((double) (v + xlydlfirst.get().getPointValueInDouble())));
                }
            }
            Optional<PointData> xlndlfirst = fdlList.stream().filter(res -> res.getEdnaId().equals(linepointmap.get(l).get(ContantXk.NFDLSYZ).getNemCode())).findFirst();
            if (xlndlfirst.isPresent()) {
                if (l.equals(line.getId())) {
                    xlndl.set(xlndlfirst.get().getPointValueInDouble());
                    xlzndl.updateAndGet(v -> new Double((double) (v + xlndlfirst.get().getPointValueInDouble())));
                } else {
                    xlzndl.updateAndGet(v -> new Double((double) (v + xlndlfirst.get().getPointValueInDouble())));
                }
            }
        });
        //电量百分比
        double percentage = xlzdl.get() != 0 ? xldl.get() / xlzdl.get() : 0;
        percentageMap.put("percentage", percentage);
        double ypercentage = xlzydl.get() != 0 ? xlydl.get() / xlzydl.get() : 0;
        percentageMap.put("ypercentage", ypercentage);
        double npercentage = xlzndl.get() != 0 ? xlndl.get() / xlzndl.get() : 0;
        percentageMap.put("npercentage", npercentage);
        return percentageMap;
    }


}
