package com.gyee.frame.service.websocket;


import com.gyee.frame.common.spring.Constant;
import com.gyee.frame.common.spring.ConstantTR;
import com.gyee.frame.common.spring.InitialRunner;
import com.gyee.frame.model.auto.*;
import com.gyee.frame.model.custom.DataVo;
import com.gyee.frame.model.custom.PointData;
import com.gyee.frame.service.*;
import com.gyee.frame.util.DateUtils;
import com.gyee.frame.util.IRealTimeDataBaseUtil;
import com.gyee.frame.util.MathUtil;
import com.gyee.frame.util.StringUtils;
import com.gyee.frame.util.golden.EdosUtil;
import org.springframework.stereotype.Service;

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

@Service
public class GenreSetPushService {


    @Resource
    private WindPowerstationTestingPointService windPowerstationTestingPointService;
    @Resource
    private WindpowerinfodayService windpowerinfodayService;
    @Resource
    private ProjectPlanService projectPlanService;
    @Resource
    private RealPowerPushService realPowerPushService;
    @Resource
    private ShutdowneventService shutdowneventService;
    @Resource
    private WpMttrAndMtbMonthService wpMttrAndMtbMonthService;

    private final String QS = "0";
    private final String FD = "-1";
    private final String GF = "-2";

    IRealTimeDataBaseUtil realApiUtil = new EdosUtil();


    public Map<String, Object> genreSetMap() throws Exception {

        Map<String, Object> map = new HashMap<>();


        map.put(QS, findBasicDataInfo(QS));
        map.put(FD, findBasicDataInfo(FD));
        map.put(GF, findBasicDataInfo(GF));

        map.put("SBQ_FDC",findBasicDataInfo("SBQ_FDC"));
        map.put("SBQ02_GC",findBasicDataInfo("SBQ02_GC"));
        map.put("SBQ05_XL",findBasicDataInfo("SBQ05_XL"));
        return map;

    }

    public Map<String, Object> findBasicDataInfo(String id) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.notEmp(id)) {


            Double zjrl = 0.0;
            Double zjts = 0.0;

            Double sjgl = 0.0;
            Double bzgl = 0.0;
            Double yfgl = 0.0;
/*************************************************风场指标*************************************************************/

        Map<String, Object> fcmap = new HashMap<>();

        if(id.equals(QS) || id.equals(FD) || id.equals(GF) )
        {

            //初始化场站信息

            List<Windpowerstation> wplist=new ArrayList<>();

            for (Windpowerstation wp : InitialRunner.wpallls) {
                if (id.equals(FD)) {
                    if (wp.getId().endsWith("FDC")) {
                        wplist.add(wp);
                    }
                }else if (id.equals(GF)) {
                    if (wp.getId().endsWith("GDC")) {
                        wplist.add(wp);
                    }
                }else if (id.equals(QS)) {
                    wplist.add(wp);
                }

            }



            for (Windpowerstation wp : wplist) {

                zjrl += wp.getCapacity();
                zjts += wp.getQuantity();

                WindPowerStationTestingPoint2 fczt = windPowerstationTestingPointService.getWindPowerStationTestingPoint2(wp.getId(), Constant.FJZT);
                WindPowerStationTestingPoint2 fcgl = windPowerstationTestingPointService.getWindPowerStationTestingPoint2(wp.getId(), Constant.TPOINT_WP_SJGL);

                Double fcztvalue = realApiUtil.getRealData(fczt).getPointValueInDouble();
                Double fcglvalue = realApiUtil.getRealData(fcgl).getPointValueInDouble();

                StringBuilder sb = new StringBuilder();
                sb.append(wp.getId()).append("_").append("zjrl");
                fcmap.put(String.valueOf(sb), wp.getCapacity());
                sb.setLength(0);
                sb.append(wp.getId()).append("_").append("zjts");
                fcmap.put(String.valueOf(sb), Double.valueOf(wp.getQuantity()));
                sb.setLength(0);
                sb.append(wp.getId()).append("_").append("fczt");
                fcmap.put(String.valueOf(sb), fcztvalue);
                sb.setLength(0);
                sb.append(wp.getId()).append("_").append("fcgl");
                fcmap.put(String.valueOf(sb), fcglvalue);

                fcmap.put("name",wp.getName());
                fcmap.put("id",wp.getId());
            }

            //所属风场指标
            map.put("fcmap", fcmap);
        }else if(InitialRunner.wpmap.containsKey(id))
        {

            if(InitialRunner.pjsmap.containsKey(id))
            {
                List<Project> pjls=InitialRunner.pjsmap.get(id);
                for(Project pj:pjls)
                {
                    if(InitialRunner.lnsmap.containsKey(pj.getId()))
                    {
                        List<Line> lnls=InitialRunner.lnsmap.get(pj.getId());
                        for(Line ln:lnls)
                        {
                            zjrl += ln.getCapacity();
                            zjts += ln.getQuantity();

                            WindPowerStationTestingPoint2 fczt = windPowerstationTestingPointService.getWindPowerStationTestingPoint2(ln.getId(), Constant.FJZT);
                            WindPowerStationTestingPoint2 fcgl = windPowerstationTestingPointService.getWindPowerStationTestingPoint2(ln.getId(), Constant.TPOINT_WP_SJGL);

                            Double fcztvalue = realApiUtil.getRealData(fczt).getPointValueInDouble();
                            Double fcglvalue = realApiUtil.getRealData(fcgl).getPointValueInDouble();

                            StringBuilder sb = new StringBuilder();
                            sb.append(ln.getId()).append("_").append("zjrl");
                            fcmap.put(String.valueOf(sb), ln.getCapacity());
                            sb.setLength(0);
                            sb.append(ln.getId()).append("_").append("zjts");
                            fcmap.put(String.valueOf(sb), Double.valueOf(ln.getQuantity()));
                            sb.setLength(0);
                            sb.append(ln.getId()).append("_").append("fczt");
                            fcmap.put(String.valueOf(sb), fcztvalue);
                            sb.setLength(0);
                            sb.append(ln.getId()).append("_").append("fcgl");
                            fcmap.put(String.valueOf(sb), fcglvalue);
                            fcmap.put("name",ln.getName());
                            fcmap.put("id",ln.getId());
                        }
                    }
                }
            }
        }else if(InitialRunner.pjmap.containsKey(id))
        {

            if(InitialRunner.lnsmap.containsKey(id))
            {
                List<Line> lnls=InitialRunner.lnsmap.get(id);
                for(Line ln:lnls)
                {
                    zjrl += ln.getCapacity();
                    zjts += ln.getQuantity();

                    WindPowerStationTestingPoint2 fczt = windPowerstationTestingPointService.getWindPowerStationTestingPoint2(ln.getId(), Constant.FJZT);
                    WindPowerStationTestingPoint2 fcgl = windPowerstationTestingPointService.getWindPowerStationTestingPoint2(ln.getId(), Constant.TPOINT_WP_SJGL);

                    Double fcztvalue = realApiUtil.getRealData(fczt).getPointValueInDouble();
                    Double fcglvalue = realApiUtil.getRealData(fcgl).getPointValueInDouble();

                    StringBuilder sb = new StringBuilder();
                    sb.append(ln.getId()).append("_").append("zjrl");
                    fcmap.put(String.valueOf(sb), ln.getCapacity());
                    sb.setLength(0);
                    sb.append(ln.getId()).append("_").append("zjts");
                    fcmap.put(String.valueOf(sb), Double.valueOf(ln.getQuantity()));
                    sb.setLength(0);
                    sb.append(ln.getId()).append("_").append("fczt");
                    fcmap.put(String.valueOf(sb), fcztvalue);
                    sb.setLength(0);
                    sb.append(ln.getId()).append("_").append("fcgl");
                    fcmap.put(String.valueOf(sb), fcglvalue);
                    fcmap.put("name",ln.getName());
                    fcmap.put("id",ln.getId());
                }
            }
        }





/*************************************************风场指标*************************************************************/

/*************************************************基础指标*************************************************************/
            Map<String, Double> jczbmap = new HashMap<>();


            if (InitialRunner.wpmap.containsKey(id)) {
                Windpowerstation wp = InitialRunner.wpmap.get(id);

                zjrl = wp.getCapacity();
                zjts = Double.valueOf(wp.getQuantity());


            } else if (InitialRunner.pjmap.containsKey(id)) {
                Project pj = InitialRunner.pjmap.get(id);

                zjrl = pj.getCapacity();
                zjts = Double.valueOf(pj.getQuantity());
            } else if (InitialRunner.lnmap.containsKey(id)) {
                Line ln = InitialRunner.lnmap.get(id);

                zjrl = ln.getCapacity();
                zjts = Double.valueOf(ln.getQuantity());
            }

            jczbmap.put("zjrl", zjrl);
            jczbmap.put("zjts", zjts);

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

            jczbls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.TPOINT_WP_SJGL).getCode());
            jczbls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.TPOINT_WP_RFDL).getCode());
            jczbls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.TPOINT_WP_YFDL).getCode());
            jczbls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.TPOINT_WP_NFDL).getCode());
            jczbls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.TPOINT_WP_SSFS).getCode());
            jczbls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.TPOINT_WP_SWDL).getCode());
            jczbls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.ZBZGL).getCode());
            jczbls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.TPOINT_WP_ZZSGL).getCode());


            List<PointData> jczblist = realApiUtil.getRealData(jczbls);

            if (!jczblist.isEmpty() && jczblist.size() == jczbls.size()) {
                //实际功率
                sjgl = MathUtil.twoBit(jczblist.get(0).getPointValueInDouble());
                jczbmap.put("sjgl", sjgl);
                //日发电量
                jczbmap.put("rfdl", MathUtil.twoBit(jczblist.get(1).getPointValueInDouble()));
                //月发电量
                jczbmap.put("yfdl", MathUtil.twoBit(jczblist.get(2).getPointValueInDouble()));
                //年发电量
                jczbmap.put("nfdl", MathUtil.twoBit(jczblist.get(3).getPointValueInDouble()));
                //月利用小时
                jczbmap.put("ylyxs", (new BigDecimal(jczblist.get(2).getPointValueInDouble()).multiply(new BigDecimal(10))).divide(new BigDecimal(zjrl), 2, RoundingMode.HALF_EVEN).doubleValue());
                //年利用小时
                jczbmap.put("nlyxs", (new BigDecimal(jczblist.get(3).getPointValueInDouble()).multiply(new BigDecimal(10))).divide(new BigDecimal(zjrl), 2, RoundingMode.HALF_EVEN).doubleValue());
                //实时风速
                jczbmap.put("ssfs", MathUtil.twoBit(jczblist.get(4).getPointValueInDouble()));
                //上网电量
                jczbmap.put("swdl", MathUtil.twoBit(jczblist.get(5).getPointValueInDouble()));
                //保证功率
                bzgl = new BigDecimal(jczblist.get(6).getPointValueInDouble()).divide(new BigDecimal(1000), 2, RoundingMode.HALF_EVEN).doubleValue();
                jczbmap.put("bzgl", bzgl);
                //应发功率
                yfgl = new BigDecimal(jczblist.get(7).getPointValueInDouble()).divide(new BigDecimal(1000), 2, RoundingMode.HALF_EVEN).doubleValue();
                jczbmap.put("yfgl", yfgl);

            } else {
                //实际功率
                jczbmap.put("sjgl", 0.0);
                //日发电量
                jczbmap.put("rfdl", 0.0);
                //月发电量
                jczbmap.put("yfdl", 0.0);
                //年发电量
                jczbmap.put("nfdl", 0.0);
                //月利用小时
                jczbmap.put("ylyxs", 0.0);
                //年利用小时
                jczbmap.put("nlyxs", 0.0);
                //实时风速
                jczbmap.put("ssfs", 0.0);
                //上网电量
                jczbmap.put("swdl", 0.0);
                //保证功率
                jczbmap.put("bzgl", 0.0);
                //应发功率
                jczbmap.put("yfgl", 0.0);

            }


            if (id.equals(QS)) {
                // 减排二氧化碳
                Double jpeyht = realApiUtil.getRealData(ConstantTR.JPEYHT).getPointValueInDouble();
                // 节约标煤
                Double jybm = realApiUtil.getRealData(ConstantTR.JYBM).getPointValueInDouble();
                // 减排二氧化硫
                Double jpeyhl = realApiUtil.getRealData(ConstantTR.JPEYHL).getPointValueInDouble();
                // 节约水
                Double jys = realApiUtil.getRealData(ConstantTR.JYS).getPointValueInDouble();
                // 安全天数
                Double aqts = realApiUtil.getRealData(ConstantTR.AQTS).getPointValueInDouble();

                Double gfsjgl = realApiUtil.getRealData(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(GF, Constant.TPOINT_WP_SJGL)).getPointValueInDouble();

                bzgl = bzgl + gfsjgl;
                yfgl = yfgl + gfsjgl;
                jczbmap.put("aqts", StringUtils.round(aqts, 2));
                jczbmap.put("jpeyht", StringUtils.round(jpeyht, 2));
                jczbmap.put("jybm", StringUtils.round(jybm, 2));
                jczbmap.put("jpeyhl", StringUtils.round(jpeyhl, 2));
                jczbmap.put("jys", StringUtils.round(jys, 2));

            }
            if (bzgl == 0.0) {
                bzgl = 1.0;
            }
            Double glbz = new BigDecimal(sjgl).divide(new BigDecimal(bzgl), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
            if (bzgl == 1) {
                bzgl = 0.0;
            }
            if (glbz > 100) {
                glbz = 100.0;
            }
            jczbmap.put("glbz", StringUtils.round(glbz, 2));

            Calendar c = Calendar.getInstance();

            int tempyear = c.get(Calendar.YEAR);
            int tempmonth = c.get(Calendar.MONTH) + 1;
            int daynum=c.get(Calendar.DAY_OF_MONTH);
            List<WpMttrAndMtbMonth> mmls=wpMttrAndMtbMonthService.getMttrAndMtbf(id, tempyear, tempmonth);

            if(!mmls.isEmpty())
            {
                WpMttrAndMtbMonth mm=mmls.get(0);
                double stoptimes=mm.getStoprtimes();
                double stophours=mm.getStophours();
                double mttr=0.0;
                double mtbf=mm.getWthours();
                if(stoptimes!=0)
                {
                    mttr=new BigDecimal(stophours).divide(new BigDecimal(stoptimes), 2, RoundingMode.HALF_EVEN).doubleValue();
                    mtbf=new BigDecimal(mm.getWthours()).divide(new BigDecimal(stoptimes), 2, RoundingMode.HALF_EVEN).doubleValue();

                }

                jczbmap.put("mttr", mttr);
                jczbmap.put("mtbf", mtbf);
            }
            map.put("jczbmap", jczbmap);


/*************************************************基础指标*************************************************************/


/*************************************************明细状态*************************************************************/

            Map<String, Double> mxztmap = new HashMap<>();

            List<String> mxztls = new ArrayList<>();
            //待机台数
            mxztls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.DJNUM).getCode());
            //手动停机台数
            mxztls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.TJNUM).getCode());
            //正常发电台数
            mxztls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.FDNUM).getCode());
            // 缺陷降出力台数
            mxztls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.FDJCLNUM).getCode());
            // 限电降出力台数
            mxztls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.XDJCLNUM).getCode());
            // 限电停机台数
            mxztls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.XDTJNUM).getCode());
            // 故障停机台数
            mxztls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.GZNUM).getCode());
            // 场内受累停机台数
            mxztls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.CNGZNUM).getCode());
            // 检修停机台数
            mxztls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.JXNUM).getCode());
            // 场内受累检修台数
            mxztls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.CNJXNUM).getCode());
            // 电网受累台数
            mxztls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.DWSLNUM).getCode());
            // 环境受累台数
            mxztls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.HJSLNUM).getCode());
            // 风机离线台数
            mxztls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.LXNUM).getCode());


            List<PointData> mxztlist = realApiUtil.getRealData(mxztls);

            if (!mxztlist.isEmpty() && mxztlist.size() == mxztls.size()) {
                mxztmap.put("djts", MathUtil.twoBit(mxztlist.get(0).getPointValueInDouble()));
                mxztmap.put("sdtjts", MathUtil.twoBit(mxztlist.get(1).getPointValueInDouble()));
                mxztmap.put("yxts", MathUtil.twoBit(mxztlist.get(2).getPointValueInDouble()));
                mxztmap.put("fdjclts", MathUtil.twoBit(mxztlist.get(3).getPointValueInDouble()));
                mxztmap.put("xdjclts", MathUtil.twoBit(mxztlist.get(4).getPointValueInDouble()));
                mxztmap.put("xdtjts", MathUtil.twoBit(mxztlist.get(5).getPointValueInDouble()));
                mxztmap.put("gzts", MathUtil.twoBit(mxztlist.get(6).getPointValueInDouble()));
                mxztmap.put("cnslgzts", MathUtil.twoBit(mxztlist.get(7).getPointValueInDouble()));
                mxztmap.put("jxts", MathUtil.twoBit(mxztlist.get(8).getPointValueInDouble()));
                mxztmap.put("cnsljxts", MathUtil.twoBit(mxztlist.get(9).getPointValueInDouble()));
                mxztmap.put("dwslts", MathUtil.twoBit(mxztlist.get(10).getPointValueInDouble()));
                mxztmap.put("hjslts", MathUtil.twoBit(mxztlist.get(11).getPointValueInDouble()));
                mxztmap.put("lxts", MathUtil.twoBit(mxztlist.get(12).getPointValueInDouble()));
            } else {

                mxztmap.put("djts", 0.0);
                mxztmap.put("sdtjts", 0.0);
                mxztmap.put("yxts", 0.0);
                mxztmap.put("fdjclts", 0.0);
                mxztmap.put("xdjclts", 0.0);
                mxztmap.put("xdtjts", 0.0);
                mxztmap.put("gzts", 0.0);
                mxztmap.put("cnslgzts", 0.0);
                mxztmap.put("jxts", 0.0);
                mxztmap.put("cnsljxts", 0.0);
                mxztmap.put("dwslts", 0.0);
                mxztmap.put("hjslts", 0.0);
                mxztmap.put("lxts", 0.0);
            }
            map.put("mxztmap", mxztmap);
//

/*************************************************明细状态*************************************************************/

/*************************************************五项损失**************************************************************/

            Map<String, Double> wxssmap = new HashMap<>();
            List<String> wxssls = new ArrayList<>();

            // 日场内受累检修
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.RLZSSDL).getCode());
            // 日场内受累故障
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.RSZSSDL).getCode());
            // 月场内受累检修
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.YLZSSDL).getCode());
            // 月场内受累故障
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.YSZSSDL).getCode());
            // 年场内受累检修
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.NLZSSDL).getCode());
            // 年场内受累故障
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.NSZSSDL).getCode());

            //日待机损失
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.RSDJZSDL).getCode());
            //日手动停机损失
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.RSSTZSDL).getCode());
            //日性能损失
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.RXNZSDL).getCode());
            //日缺陷降出力损失
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.RSQXZSDL).getCode());
            //日故障损失
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.RGZSSDL).getCode());
            //日检修损失
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.RJXSSDL).getCode());

            //月待机损失
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.YSDJZSDL).getCode());
            //月手动停机损失
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.YSSTZSDL).getCode());
            //月性能损失
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.YXNZSDL).getCode());
            //月缺陷降出力损失
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.YSQXZSDL).getCode());
            //月故障损失
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.YGZSSDL).getCode());
            //月检修损失
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.YJXSSDL).getCode());


            //年待机损失
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.NSDJZSDL).getCode());
            //年手动停机损失
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.NSSTZSDL).getCode());
            //年性能损失
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.NXNZSDL).getCode());
            //年缺陷降出力损失
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.NSQXZSDL).getCode());
            //年故障损失
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.NGZSSDL).getCode());
            //年检修损失
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.NJXSSDL).getCode());


            //日限电损失
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.RXDSSDL).getCode());
            //月限电损失
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.YXDSSDL).getCode());
            //年限电损失
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.NXDSSDL).getCode());


            //日场外受累电网
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.RWZSSDL).getCode());
            // 日场外受累天气
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.RTZSSDL).getCode());
            // 月场外受累电网
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.YWZSSDL).getCode());
            // 月场外受累天气
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.YTZSSDL).getCode());
            // 年场外受累电网
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.NWZSSDL).getCode());
            // 年场外受累天气
            wxssls.add(windPowerstationTestingPointService.getWindPowerStationTestingPoint2(id, Constant.NTZSSDL).getCode());


            List<PointData> wxsslist = realApiUtil.getRealData(wxssls);

            if (!wxsslist.isEmpty() && wxsslist.size() == wxssls.size()) {

                Double rjxsl = MathUtil.twoBit(wxsslist.get(0).getPointValueInDouble());
                // 日场内受累故障
                Double rgzzsl = MathUtil.twoBit(wxsslist.get(1).getPointValueInDouble());
                // 月场内受累检修
                Double yjxsl = MathUtil.twoBit(wxsslist.get(2).getPointValueInDouble());
                // 月场内受累故障
                Double ygzzsl = MathUtil.twoBit(wxsslist.get(3).getPointValueInDouble());
                // 年场内受累检修
                Double njxsl = MathUtil.twoBit(wxsslist.get(4).getPointValueInDouble());
                // 年场内受累故障
                Double ngzzsl = MathUtil.twoBit(wxsslist.get(5).getPointValueInDouble());

                wxssmap.put("rjxsl", rjxsl);
                wxssmap.put("rgzzsl", rgzzsl);
                wxssmap.put("yjxsl", yjxsl);
                wxssmap.put("ygzzsl", ygzzsl);
                wxssmap.put("njxsl", njxsl);
                wxssmap.put("ngzzsl", ngzzsl);

                //日待机损失
                Double rdjss = MathUtil.twoBit(wxsslist.get(6).getPointValueInDouble());
                //日手动停机损失
                Double rsdtjss = MathUtil.twoBit(wxsslist.get(7).getPointValueInDouble());
                //日性能损失
                Double rxnss = MathUtil.twoBit(wxsslist.get(8).getPointValueInDouble());
                //日缺陷降出力损失
                Double rqxjclss = MathUtil.twoBit(wxsslist.get(9).getPointValueInDouble());

                //日欠发损失=日待机损失+日手动停机损失+日性能损失+日缺陷降出力损失
                Double rqfssdl = new BigDecimal(rdjss + rsdtjss + rxnss + rqxjclss).divide(new BigDecimal(10000), 2, RoundingMode.HALF_EVEN).doubleValue();

                Double rgzss = MathUtil.twoBit(wxsslist.get(10).getPointValueInDouble());
                //日故障损失电量=日故障损失+日故障受累
                Double rgzssdl = new BigDecimal(rgzss + rgzzsl).divide(new BigDecimal(10000), 2, RoundingMode.HALF_EVEN).doubleValue();
                //日检修损失电量=日检修损失+日检修受累
                Double rjxss = MathUtil.twoBit(wxsslist.get(11).getPointValueInDouble());
                Double rjxssdl = new BigDecimal(rjxss + rjxsl).divide(new BigDecimal(10000), 2, RoundingMode.HALF_EVEN).doubleValue();


                wxssmap.put("rqfssdl", rqfssdl);
                wxssmap.put("rgzssdl", rgzssdl);
                wxssmap.put("rjxssdl", rjxssdl);

                // 月场内受累检修
                Double ydjss = MathUtil.twoBit(wxsslist.get(12).getPointValueInDouble());
                // 月场内受累检修
                Double ysdtjss = MathUtil.twoBit(wxsslist.get(13).getPointValueInDouble());
                // 月场内受累检修
                Double yxnss = MathUtil.twoBit(wxsslist.get(14).getPointValueInDouble());
                // 月场内受累检修
                Double yqxjclss = MathUtil.twoBit(wxsslist.get(15).getPointValueInDouble());
                //月欠发损失=月待机损失+月手动停机损失+月性能损失+月缺陷降出力损失
                Double yqfssdl = new BigDecimal(ydjss + ysdtjss + yxnss + yqxjclss).divide(new BigDecimal(10000), 2, RoundingMode.HALF_EVEN).doubleValue();

                Double ygzss = MathUtil.twoBit(wxsslist.get(16).getPointValueInDouble());
                //月故障损失电量=月故障损失+月故障受累
                Double ygzssdl = new BigDecimal(ygzss + ygzzsl).divide(new BigDecimal(10000), 2, RoundingMode.HALF_EVEN).doubleValue();
                Double yjxss = MathUtil.twoBit(wxsslist.get(17).getPointValueInDouble());
                //月检修损失电量=月检修损失+月检修受累
                Double yjxssdl = new BigDecimal(yjxss + yjxsl).divide(new BigDecimal(10000), 2, RoundingMode.HALF_EVEN).doubleValue();

                wxssmap.put("yqfssdl", yqfssdl);
                wxssmap.put("ygzssdl", ygzssdl);
                wxssmap.put("yjxssdl", yjxssdl);

                // 年场内受累检修
                Double ndjss = MathUtil.twoBit(wxsslist.get(18).getPointValueInDouble());
                // 年场内受累检修
                Double nsdtjss = MathUtil.twoBit(wxsslist.get(19).getPointValueInDouble());
                // 年场内受累检修
                Double nxnss = MathUtil.twoBit(wxsslist.get(20).getPointValueInDouble());
                // 年场内受累检修
                Double nqxjclss = MathUtil.twoBit(wxsslist.get(21).getPointValueInDouble());

                Double nqfssdl = new BigDecimal(ndjss + nsdtjss + nxnss + nqxjclss).divide(new BigDecimal(10000), 2, RoundingMode.HALF_EVEN).doubleValue();

                Double ngzss = MathUtil.twoBit(wxsslist.get(22).getPointValueInDouble());
                //年故障损失电量=年故障损失+年故障受累
                Double ngzssdl = new BigDecimal(ngzss + ngzzsl).divide(new BigDecimal(10000), 2, RoundingMode.HALF_EVEN).doubleValue();
                Double njxss = MathUtil.twoBit(wxsslist.get(23).getPointValueInDouble());
                //年检修损失电量=年检修损失+年检修受累
                Double njxssdl = new BigDecimal(njxss + njxsl).divide(new BigDecimal(10000), 2, RoundingMode.HALF_EVEN).doubleValue();

                wxssmap.put("nqfssdl", nqfssdl);
                wxssmap.put("ngzssdl", ngzssdl);
                wxssmap.put("njxssdl", njxssdl);

                double temp = 0.0;
                temp = MathUtil.twoBit(wxsslist.get(24).getPointValueInDouble());
                Double rxdssdl = new BigDecimal(temp).divide(new BigDecimal(10000), 2, RoundingMode.HALF_EVEN).doubleValue();
                temp = MathUtil.twoBit(wxsslist.get(25).getPointValueInDouble());
                Double yxdssdl = new BigDecimal(temp).divide(new BigDecimal(10000), 2, RoundingMode.HALF_EVEN).doubleValue();
                temp = MathUtil.twoBit(wxsslist.get(26).getPointValueInDouble());
                Double nxdssdl = new BigDecimal(temp).divide(new BigDecimal(10000), 2, RoundingMode.HALF_EVEN).doubleValue();

                wxssmap.put("rxdssdl", StringUtils.round(rxdssdl < 0.0 ? 0.0 : rxdssdl, 2));
                wxssmap.put("yxdssdl", StringUtils.round(yxdssdl < 0.0 ? 0.0 : yxdssdl, 2));
                wxssmap.put("nxdssdl", StringUtils.round(nxdssdl < 0.0 ? 0.0 : nxdssdl, 2));

                Double rdwsl = MathUtil.twoBit(wxsslist.get(27).getPointValueInDouble());
                Double rtqsl = MathUtil.twoBit(wxsslist.get(28).getPointValueInDouble());
                Double rslssdl = new BigDecimal(rdwsl + rtqsl).divide(new BigDecimal(10000), 2, RoundingMode.HALF_EVEN).doubleValue();
                Double ydwsl = MathUtil.twoBit(wxsslist.get(29).getPointValueInDouble());
                Double ytqsl = MathUtil.twoBit(wxsslist.get(30).getPointValueInDouble());
                Double yslssdl = new BigDecimal(ydwsl + ytqsl).divide(new BigDecimal(10000), 2, RoundingMode.HALF_EVEN).doubleValue();
                Double ndwsl = MathUtil.twoBit(wxsslist.get(31).getPointValueInDouble());
                Double ntqsl = MathUtil.twoBit(wxsslist.get(32).getPointValueInDouble());
                Double nslssdl = new BigDecimal(ndwsl + ntqsl).divide(new BigDecimal(10000), 2, RoundingMode.HALF_EVEN).doubleValue();

                wxssmap.put("rslssdl", rslssdl);
                wxssmap.put("yslssdl", yslssdl);
                wxssmap.put("nslssdl", nslssdl);

            } else {

                wxssmap.put("rjxsl", 0.0);
                wxssmap.put("rgzzsl", 0.0);
                wxssmap.put("yjxsl", 0.0);
                wxssmap.put("ygzzsl", 0.0);
                wxssmap.put("njxsl", 0.0);
                wxssmap.put("ngzzsl", 0.0);
                wxssmap.put("rqfssdl", 0.0);
                wxssmap.put("rgzssdl", 0.0);
                wxssmap.put("rjxssdl", 0.0);

                wxssmap.put("yqfssdl", 0.0);
                wxssmap.put("ygzssdl", 0.0);
                wxssmap.put("yjxssdl", 0.0);

                wxssmap.put("nqfssdl", 0.0);
                wxssmap.put("ngzssdl", 0.0);
                wxssmap.put("njxssdl", 0.0);


                wxssmap.put("rxdssdl", 0.0);
                wxssmap.put("yxdssdl", 0.0);
                wxssmap.put("nxdssdl", 0.0);


                wxssmap.put("rslssdl", 0.0);
                wxssmap.put("yslssdl", 0.0);
                wxssmap.put("nslssdl", 0.0);

            }
            map.put("wxssmap", wxssmap);

/****************************************************五项损失************************************************************/
/****************************************************日功率曲线**********************************************************/

            List<DataVo> glvos =realPowerPushService.findRealPower(id);
            map.put("glvos", glvos);
/*************************************************日功率曲线*************************************************************/

/****************************************************日电量历史**********************************************************/

            List<DataVo> rdlvos = findPowerWpBar(id);
            map.put("rdlvos", rdlvos);
/*************************************************日电量历史*************************************************************/


/*************************************************关系库指标*************************************************************/

            Map<String, Double> gxkmap = new HashMap<>();
            List<Windpowerinfoday> wpdls = windpowerinfodayService.getWindpowerinfodayList(id, DateUtils.truncate(new Date()));
            if (!wpdls.isEmpty()) {
                Windpowerinfoday wd = wpdls.get(0);
                gxkmap.put("ndxkyss", wd.getYeardxkyxs());
                gxkmap.put("nsbklyl", wd.getYearsbklyl());

            } else {
                gxkmap.put("ndxkyss", 0.0);
                gxkmap.put("nsbklyl", 0.0);
            }
            Calendar cal = Calendar.getInstance();
            cal.setTime(new Date());
            int year = cal.get(Calendar.YEAR);
            List<ProjectPlan> pjls = projectPlanService.getProjectPlanList(String.valueOf(year));

            // 获取计划发电量信息
            setPlanInfo(gxkmap, pjls, id);

            Double ywcl = 0.0;
            Double nwcl = 0.0;

            Double yfdljh = 0.0;
            Double nfdljh = 0.0;

            Double yfdl = jczbmap.get("yfdl");
            Double nfdl = jczbmap.get("nfdl");

            yfdljh = gxkmap.get("yfdljh");
            nfdljh = gxkmap.get("nfdljh");



            if (yfdljh == 0.0) {
                nfdljh = 1.0;
            }
            if (yfdljh != 0.0) {
                ywcl = new BigDecimal(yfdl).divide(new BigDecimal(yfdljh), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
            }
            if (nfdljh == 0.0) {
                nfdljh = 1.0;
            }
            if (nfdljh != 0.0) {
                nwcl = new BigDecimal(nfdl).divide(new BigDecimal(nfdljh), 2, RoundingMode.HALF_EVEN).multiply(new BigDecimal(100)).doubleValue();
            }

            if (yfdljh > 100) {
                yfdljh = 100.0;
            }
            if (nfdljh > 100) {
                nfdljh = 100.0;
            }

            gxkmap.put("ywcl", StringUtils.round(ywcl, 2));
            gxkmap.put("nwcl", StringUtils.round(nwcl, 2));

            map.put("gxkmap", gxkmap);
        }
/*************************************************关系库指标**************************************************************/

        return map;
    }

    private List<DataVo> findPowerWpBar(String wpId) throws Exception {
        List<DataVo> vos = new ArrayList<DataVo>();
        if (StringUtils.empty(wpId)) {
            wpId = "0";
        }

        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
        c.add(Calendar.DAY_OF_MONTH, 1);
        // c.add(Calendar.MONTH, -1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        Date beginDate = c.getTime();

        c = Calendar.getInstance();
        // c.add(Calendar.MONTH, -1);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        c.add(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        Date endDate = c.getTime();
        c = Calendar.getInstance();
        long num = c.get(Calendar.DAY_OF_MONTH);
        // long num = 30;

        WindPowerStationTestingPoint2 rfdlai = windPowerstationTestingPointService.getWindPowerStationTestingPoint2(wpId, Constant.TPOINT_WP_RFDL);
        WindPowerStationTestingPoint2 swdlai = windPowerstationTestingPointService.getWindPowerStationTestingPoint2(wpId, Constant.TPOINT_WP_SWDL);
        WindPowerStationTestingPoint2 gwdlai = windPowerstationTestingPointService.getWindPowerStationTestingPoint2(wpId, Constant.TPOINT_WP_GWDL);
        WindPowerStationTestingPoint2 ssfs = windPowerstationTestingPointService.getWindPowerStationTestingPoint2(wpId, Constant.TPOINT_WT_PJFS);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        List<PointData> rfdls = realApiUtil.getHistoryDatasSnap(rfdlai, beginDate.getTime() / 1000, endDate.getTime() / 1000, num, 86400L);
        List<PointData> swdls = realApiUtil.getHistoryDatasSnap(swdlai, beginDate.getTime() / 1000, endDate.getTime() / 1000, num, 86400L);
        List<PointData> gwdls = realApiUtil.getHistoryDatasSnap(gwdlai, beginDate.getTime() / 1000, endDate.getTime() / 1000, num, 86400L);
        List<PointData> ssfss = realApiUtil.getHistoryDatasSnap(ssfs, beginDate.getTime() / 1000, endDate.getTime() / 1000, num, 86400L);

        if (!rfdls.isEmpty() && !swdls.isEmpty() && !gwdls.isEmpty()) {
            Calendar cal = Calendar.getInstance();
            int today = c.get(Calendar.DAY_OF_MONTH);
            // int today = 30;
            for (int i = 0; i < rfdls.size(); i++) {
                DataVo vo = new DataVo();

                Date date = new Date(rfdls.get(i).getPointTime() * 1000);
                cal.setTime(date);

                // vo.setTime(dateformat.parse(entry.getKey()).getTime());

                if (i + 1 <= today) {
                    vo.setTimestr(i + 1 + "日");
                    vo.setValue1(StringUtils.round(rfdls.get(i).getPointValueInDouble(), 2));// 日发电量
                    vo.setValue2(StringUtils.round(swdls.get(i).getPointValueInDouble(), 2));// 上网电量
                    vo.setValue3(StringUtils.round(gwdls.get(i).getPointValueInDouble(), 2));// 购网电量
                    vo.setSpeed(StringUtils.round(ssfss.get(i).getPointValueInDouble(), 2));
                } else {
                    vo.setTimestr("");
                    vo.setValue1(0.0);// 日发电量
                    vo.setValue2(0.0);// 上网电量
                    vo.setValue3(0.0);// 购网电量
                    //vo.setSpeed(0.0);
                }

                vos.add(vo);
            }
        }

        if (vos.size() < 31) {
            for (int i = 0; i < 30; i++) {
                DataVo vo = new DataVo();
                vo.setTimestr("");

                vo.setValue1(0.0);// 日发电量
                vo.setValue2(0.0);// 上网电量
                vo.setValue3(0.0);// 购网电量
                // vo.setSpeed(0.0);
                vos.add(vo);
                if (vos.size() == 31) {
                    break;
                }
            }
        }

        return vos;
    }

    private void setPlanInfo(Map<String, Double> gxkmap, List<ProjectPlan> planls, String id) {

        double yfdljh = 0.0;
        double nfdljh = 0.0;

        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int month = cal.get(Calendar.MONTH) + 1;
        int num=1;

        List<ProjectPlan> planlist=new ArrayList<>();

        for(ProjectPlan pl:planls)
        {
            if (id.equals(QS)) {
                planlist.add(pl);
            }else  if (id.equals(FD)) {
                if (pl.getWindpower().endsWith("FDC")) {
                    planlist.add(pl);
                }
            }else  if (id.equals(GF)) {
                if (pl.getWindpower().endsWith("GDC")) {
                    planlist.add(pl);
                }
            }else  if (InitialRunner.wpmap.containsKey(id)) {
                if (pl.getWindpower().equals(id)) {
                    planlist.add(pl);
                }
            }else  if (InitialRunner.pjmap.containsKey(id)) {
                if (pl.getProjectid().equals(id)) {
                    planlist.add(pl);
                }
            }else  if (InitialRunner.lnmap.containsKey(id)) {
                Line ln=InitialRunner.lnmap.get(id);

                if (InitialRunner.lnsmap.containsKey(ln.getProjectid())) {
                    List<Line> linels = InitialRunner.lnsmap.get(ln.getProjectid());
                    if (!linels.isEmpty()) {
                        num = linels.size();
                    }
                } else {
                    num = 1;
                }

                if (pl.getProjectid().equals(ln.getProjectid())) {
                    planlist.add(pl);
                }
            }
        }

        for (ProjectPlan plan : planlist) {

            double fdl = null != plan.getGeneratingcapacity() ? Double.valueOf(plan.getGeneratingcapacity()) : 0.0;

            if (StringUtils.notEmp(plan.getMonth())) {
                int planmonth = Integer.valueOf(plan.getMonth());
                if (planmonth == month) {

                    yfdljh = yfdljh + fdl;
                }

                nfdljh = nfdljh + fdl;
            }
        }

        if (InitialRunner.lnmap.containsKey(id)) {
            yfdljh = new BigDecimal(yfdljh).divide(new BigDecimal(num), 2, RoundingMode.HALF_EVEN).doubleValue();
            nfdljh = new BigDecimal(nfdljh).divide(new BigDecimal(num), 2, RoundingMode.HALF_EVEN).doubleValue();
        }
        gxkmap.put("yfdljh", StringUtils.round(yfdljh, 0));
        gxkmap.put("nfdljh", StringUtils.round(nfdljh, 0));


    }






}


