package com.gyee.generation.service.realtimelibrary;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gyee.common.contant.ContantXk;
import com.gyee.common.model.PointData;
import com.gyee.common.util.DateUtils;
import com.gyee.generation.init.CacheContext;
import com.gyee.generation.model.auto.ProBasicPowerstation;
import com.gyee.generation.model.auto.ProBasicPowerstationPoint;
import com.gyee.generation.model.auto.ProEconPowerstationInfoDay2;
import com.gyee.generation.service.auto.IProEconPowerstationInfoDay2Service;
import com.gyee.generation.util.PointUtil;
import com.gyee.generation.util.realtimesource.IEdosUtil;
import org.springframework.stereotype.Service;

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

/*
@author   谢生杰
@date   2022/12/2-9:49
后增加计算指标
*/
@Service
public class CalculationService {
    @Resource
    private IEdosUtil edosUtil;
    @Resource
    private IProEconPowerstationInfoDay2Service proEconPowerstationInfoDay2Service;

    public void calWp() throws Exception {

        List<PointData> resultList = new ArrayList<>();

        List<ProBasicPowerstation> wpls = CacheContext.wpls;
        Map<String, Map<String, ProBasicPowerstationPoint>> wppointmap = CacheContext.wppointmap;
        Date currentDate = DateUtils.getCurrentDate();
        Date monthFirst = DateUtils.getMonthFirst(currentDate);
        double hoursDiff = DateUtils.hoursDiff2(currentDate, monthFirst);


        Date toDate1 = DateUtils.parseDate(DateUtils.toDate1(currentDate));
        Map<String, ProEconPowerstationInfoDay2> tempMap = new HashMap<>();
        QueryWrapper<ProEconPowerstationInfoDay2> qw = new QueryWrapper<>();
        qw.eq("record_date", toDate1);
        List<ProEconPowerstationInfoDay2> powerstationInfoDay2s = proEconPowerstationInfoDay2Service.list(qw);
        powerstationInfoDay2s.stream().forEach(info -> {
            tempMap.put(info.getForeignKeyId(), info);
        });

        wpls.stream().forEach(wp -> {
            Map<String, ProBasicPowerstationPoint> powerstationPointMap = wppointmap.get(wp.getId());
            ProBasicPowerstationPoint rfdlPoint = powerstationPointMap.get(ContantXk.RFDL);
            ProBasicPowerstationPoint nfdlPoint = powerstationPointMap.get(ContantXk.NFDL);
            ProBasicPowerstationPoint yfdlPoint = powerstationPointMap.get(ContantXk.YFDL);
            ProBasicPowerstationPoint yllfdlPoint = powerstationPointMap.get(ContantXk.YLLFDL);

            ProBasicPowerstationPoint jymPoint = powerstationPointMap.get(ContantXk.JYM);
            ProBasicPowerstationPoint jysPoint = powerstationPointMap.get(ContantXk.JYS);
            ProBasicPowerstationPoint jyeyhtPoint = powerstationPointMap.get(ContantXk.JYEYHT);
            ProBasicPowerstationPoint jyeyhlPoint = powerstationPointMap.get(ContantXk.JYEYHL);
            ProBasicPowerstationPoint rlyxsPoint = powerstationPointMap.get(ContantXk.RLYXS);
            ProBasicPowerstationPoint ylyxsPoint = powerstationPointMap.get(ContantXk.YLYXS);
            ProBasicPowerstationPoint nlyxsPoint = powerstationPointMap.get(ContantXk.NLYXS);
            ProBasicPowerstationPoint sbklylPoint = powerstationPointMap.get(ContantXk.SBKLYL);
            ProBasicPowerstationPoint dxkyxsPoint = powerstationPointMap.get(ContantXk.DXKYXS);
            ProBasicPowerstationPoint fnlylPoint = powerstationPointMap.get(ContantXk.FNLYL);


            double jym = 0;
            double jys = 0;
            double jyeyht = 0;
            double jyeyhl = 0;
            double rlyxs = 0;
            double ylyxs = 0;
            double nlyxs = 0;
            double ysbklyl = 0;
            double ydxkyxs = 0;
            double yfnlyl = 0;
            if (hoursDiff != 0) {
                if (tempMap.containsKey(wp.getId())) {
                    ProEconPowerstationInfoDay2 powerstationInfoDay2 = tempMap.get(wp.getId());
                    ysbklyl = (hoursDiff - powerstationInfoDay2.getYhjgztjxs() - powerstationInfoDay2.getYhjjxtjxs()) / hoursDiff * 100;
                    ydxkyxs = (hoursDiff - powerstationInfoDay2.getYhjgztjxs()) / hoursDiff * 100;
                }
            }

            try {
                double yfdl = edosUtil.getSectionData(yfdlPoint, currentDate.getTime()).getPointValueInDouble();
                double nfdl = edosUtil.getSectionData(nfdlPoint, currentDate.getTime()).getPointValueInDouble();
                double yllfdl = edosUtil.getSectionData(yllfdlPoint, currentDate.getTime()).getPointValueInDouble();
                double rfdl = edosUtil.getSectionData(rfdlPoint, currentDate.getTime()).getPointValueInDouble();
                //                jym = nfdl * 0.37 / 1000;
                //                jys = nfdl * 3.05 / 1000;
                //                jyeyht = nfdl * 0.8568 / 1000;
                //                jyeyhl = nfdl * 0.0059 / 1000;

                jym = new BigDecimal(nfdl * 0.37).divide(new BigDecimal(1000 * 10000), 2, RoundingMode.HALF_EVEN).doubleValue();
                jys = new BigDecimal(nfdl * 3.05).divide(new BigDecimal(1000 * 10000), 2, RoundingMode.HALF_EVEN).doubleValue();
                jyeyht = new BigDecimal(nfdl * 0.8568).divide(new BigDecimal(1000 * 10000), 2, RoundingMode.HALF_EVEN).doubleValue();
                jyeyhl = new BigDecimal(nfdl * 0.0059).divide(new BigDecimal(1000 * 10000), 2, RoundingMode.HALF_EVEN).doubleValue();

                rlyxs = rfdl / (wp.getJrwindCapacity() + wp.getJrCapacity());
                ylyxs = yfdl / (wp.getJrwindCapacity() + wp.getJrCapacity());
                nlyxs = nfdl / (wp.getJrwindCapacity() + wp.getJrCapacity());
                if (yllfdl != 0) {
                    yfnlyl = new BigDecimal(yfdl).divide(new BigDecimal(yllfdl), 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).doubleValue();
                }
                if (yfnlyl > 100) {
                    yfnlyl = 100;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            resultList.add(PointUtil.createPointData(currentDate, jym, jymPoint.getNemCode(), jymPoint.getName()));
            resultList.add(PointUtil.createPointData(currentDate, jys, jysPoint.getNemCode(), jysPoint.getName()));
            resultList.add(PointUtil.createPointData(currentDate, jyeyht, jyeyhtPoint.getNemCode(), jyeyhtPoint.getName()));
            resultList.add(PointUtil.createPointData(currentDate, jyeyhl, jyeyhlPoint.getNemCode(), jyeyhlPoint.getName()));
            resultList.add(PointUtil.createPointData(currentDate, rlyxs, rlyxsPoint.getNemCode(), rlyxsPoint.getName()));
            resultList.add(PointUtil.createPointData(currentDate, ylyxs, ylyxsPoint.getNemCode(), ylyxsPoint.getName()));
            resultList.add(PointUtil.createPointData(currentDate, nlyxs, nlyxsPoint.getNemCode(), nlyxsPoint.getName()));
            resultList.add(PointUtil.createPointData(currentDate, ysbklyl, sbklylPoint.getNemCode(), sbklylPoint.getName()));
            resultList.add(PointUtil.createPointData(currentDate, ydxkyxs, dxkyxsPoint.getNemCode(), dxkyxsPoint.getName()));
            resultList.add(PointUtil.createPointData(currentDate, yfnlyl, fnlylPoint.getNemCode(), fnlylPoint.getName()));

        });
        edosUtil.sendMultiPoint(resultList);
    }
}
