package com.gyee.frame.service.websocket;


import com.gyee.frame.common.spring.Constant;
import com.gyee.frame.common.spring.InitialRunner;
import com.gyee.frame.model.auto.*;
import com.gyee.frame.model.custom.MatrixVo;
import com.gyee.frame.model.custom.PointData;
import com.gyee.frame.service.*;
import com.gyee.frame.util.*;
import org.springframework.stereotype.Service;

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

@Service
public class MatrixPushByWpService {


    @Resource
    private WindPowerstationTestingPoint2Service windPowerstationTestingPoint2Service;
    @Resource
    private WindpowerinfodayService windpowerinfodayService;
    @Resource
    private ProjectPlanService projectPlanService;
    @Resource
    private WindTurbineTestingPointAiService windTurbineTestingPointAiService;
    @Resource
    private GeneralAppearancePushService generalAppearancePushService;
    @Resource
    private WpMttrAndMtbMonthService wpMttrAndMtbMonthService;
    IRealTimeDataBaseUtil realApiUtil = RealTimeDataBaseFactory.createRealTimeDataBase();


    public Map<String, Object> findMatrixById(String wpId) throws Exception {
        Map<String, Object> map =new HashMap<>();
        if(StringUtils.notEmp(wpId))
        {
            return  findMatrix(wpId);
        }



        return map;

    }

    public Map<String, Object> findSimpleMatrixById(String wpId) throws Exception {
        Map<String, Object> map =new HashMap<>();
        if(StringUtils.notEmp(wpId))
        {
            return  findSimpleMatrix(wpId);
        }



        return map;

    }

    /**
     * 风场矩阵
     * @param wpId
     * @return
     * @throws Exception
     */
    private Map<String, Object> findMatrix(String wpId) throws Exception {

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

        if(InitialRunner.wpmap.containsKey(wpId))
        {

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

            int fcdjnum = 0;
            int fcyxnum = 0;
            int fcgznum = 0;
            int fclxnum = 0;
            int fcwhnum = 0;
            int fcxdnum = 0;

            int gfdjnum = 0;
            int gfyxnum = 0;
            int gfgznum = 0;
            int gflxnum = 0;
            int gfwhnum = 0;
            int gfxdnum = 0;
            String[] ycPoints1 = Constant.TPOINT_WP_FGLYC.split(",");

            //        Map<String, List<MatrixVo>> fjmap = new HashMap<>();
            List< List<MatrixVo>> fjvos=new ArrayList<>();
            Map<String, Map<String, Object>> fczbmap = new HashMap<>();



            if(StringUtils.notEmp(wpId))
            {
                Map<String, Object> jczbmap = new HashMap<>();

                Windpowerstation wp= InitialRunner.wpmap.get(wpId);
                jczbmap.put("name",wp.getName());
                /*************************************************风场指标统计*************************************************************/


                List<String> jczbls = new ArrayList<>();
                //实时风速
                jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.TPOINT_WP_SSFS).getCode());
                //预测功率
                jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, ycPoints1[0]).getCode());
                //保证功率
                jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.ZBZGL).getCode());
                //应发功率
                jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.TPOINT_WP_ZZSGL).getCode());
                //实际功率
                jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.TPOINT_WP_SJGL).getCode());
                //AGC出线有功
                jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.AGC001).getCode());
                //AGC有功设定
                jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.AGC002).getCode());
                //待机台数
                jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.TPOINT_WT_DJTS).getCode());
                //运行台数
                jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.TPOINT_WT_YXTS).getCode());
                //故障台数
                jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.TPOINT_WT_GZTJ).getCode());
                //离线台数
                jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.TPOINT_WT_TXZD).getCode());
                //维护台数
                jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.TPOINT_WT_WHTJ).getCode());
                //限电台数
                jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.TPOINT_WT_XDTS).getCode());


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

                if (!jczblist.isEmpty() && jczblist.size() == jczbls.size()) {

                    //实时风速
                    jczbmap.put("ssfs", MathUtil.twoBit(jczblist.get(0).getPointValueInDouble()));
                    //预测功率
                    jczbmap.put("ycgl", MathUtil.twoBit(jczblist.get(1).getPointValueInDouble()));
                    //保证功率
                    double bzgl = new BigDecimal(jczblist.get(2).getPointValueInDouble()).divide(new BigDecimal(1000), 2, RoundingMode.HALF_EVEN).doubleValue();
                    jczbmap.put("bzgl", bzgl);
                    //应发功率
                    double yfgl = new BigDecimal(jczblist.get(3).getPointValueInDouble()).divide(new BigDecimal(1000), 2, RoundingMode.HALF_EVEN).doubleValue();
                    jczbmap.put("yfgl", yfgl);
                    //实际功率
                    jczbmap.put("sjgl", MathUtil.twoBit(jczblist.get(4).getPointValueInDouble()));
                    //AGC出线有功
                    jczbmap.put("agccxyg", MathUtil.twoBit(jczblist.get(5).getPointValueInDouble()));
                    //AGC有功设定
                    jczbmap.put("agcygsd", MathUtil.twoBit(jczblist.get(6).getPointValueInDouble()));


                    Double djts = MathUtil.twoBit(jczblist.get(7).getPointValueInDouble());
                    //待机台数
                    jczbmap.put("djts", djts);
                    //运行台数
                    Double yxts = MathUtil.twoBit(jczblist.get(8).getPointValueInDouble());
                    jczbmap.put("yxts", yxts);
                    //故障台数
                    Double gzts = MathUtil.twoBit(jczblist.get(9).getPointValueInDouble());
                    jczbmap.put("gzts", gzts);
                    //离线台数
                    Double lxts = MathUtil.twoBit(jczblist.get(10).getPointValueInDouble());
                    jczbmap.put("lxts", lxts);
                    //维护台数
                    Double whts = MathUtil.twoBit(jczblist.get(11).getPointValueInDouble());
                    jczbmap.put("whts", whts);
                    //限电台数
                    Double xdts = MathUtil.twoBit(jczblist.get(12).getPointValueInDouble());
                    jczbmap.put("xdts", xdts);

                    jczbmap.put("jrts", djts+yxts+gzts+lxts+whts+xdts);
                } else {
                    //实时风速
                    jczbmap.put("ssfs", 0.0);
                    //预测功率
                    jczbmap.put("ssfs", 0.0);
                    //保证功率
                    jczbmap.put("bzgl", 0.0);
                    //应发功率
                    jczbmap.put("yfgl", 0.0);
                    //实际功率
                    jczbmap.put("sjgl", 0.0);
                    //AGC出线有功
                    jczbmap.put("agccxyg", 0.0);
                    //AGC有功设定
                    jczbmap.put("agcygsd", 0.0);
                    //待机台数
                    jczbmap.put("djts", 0.0);
                    //运行台数
                    jczbmap.put("yxts", 0.0);
                    //故障台数
                    jczbmap.put("gzts", 0.0);
                    //离线台数
                    jczbmap.put("lxts", 0.0);
                    //维护台数
                    jczbmap.put("whts", 0.0);
                    //限电台数
                    jczbmap.put("xdts", 0.0);
                    jczbmap.put("jrts", 0.0);
                }

                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(wpId, 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);
                }
                fczbmap.put("jczbmap",jczbmap);
/*************************************************风场指标统计*************************************************************/




/*************************************************风机指标统计*************************************************************/
                List<String> fjzbls = new ArrayList<>();

                List<Windturbine> wtls=InitialRunner.wp_wtmap.get(wpId);

               // Map<String, MatrixVo> vomap = new HashMap<>();
                List<MatrixVo> vos = new ArrayList<>();
                for(Windturbine wt:wtls)
                {
                    //风机状态
                    fjzbls.add(windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wt.getId(), Constant.FJZT).getId());
                    //风速
                    fjzbls.add(windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wt.getId(), Constant.TPOINT_WT_FJSSFS).getId());
                    //功率
                    fjzbls.add(windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wt.getId(), Constant.TPOINT_WT_FJGL).getId());
                    //发电机转数
                    fjzbls.add(windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wt.getId(), Constant.TPOINT_WT_FDJZS).getId());

                }

                List<PointData> fjzblist = realApiUtil.getRealData(fjzbls);

                if (!fjzblist.isEmpty() && fjzblist.size() == fjzbls.size()) {


                    int step=4;
                    for(int i=0;i<wtls.size();i++)
                    {
                        Windturbine wt=wtls.get(i);

                        MatrixVo vo=new MatrixVo();
                        vo.setWtId(wt.getId());

                        if(StringUtils.notEmp(wt.getStatus()))
                        {
                            String num=wt.getStatus().substring(wt.getStatus().length()-3);
                            vo.setWtnum(num);

                        }

                        vo.setWpId(wt.getWindpowerstationid());
                        double fjzt = MathUtil.twoBit(fjzblist.get(i * step).getPointValueInDouble());
                        vo.setFjzt(fjzt);
                        vo.setFs(MathUtil.twoBit(fjzblist.get(i*step+1).getPointValueInDouble()));
                        double gl = MathUtil.twoBit(fjzblist.get(i * step + 2).getPointValueInDouble());
                        vo.setGl(gl);
                        vo.setFdjzs(MathUtil.twoBit(fjzblist.get(i*step+3).getPointValueInDouble()));
                        vo.setWtCode(wt.getCode());

                        if (fjzt == 3) {
                            vo.setLyl(0.0);
                        } else {
                            if (InitialRunner.mlmap.containsKey(wt.getModelid())) {
                                Equipmentmodel ml = InitialRunner.mlmap.get(wt.getModelid());
                                double generatingcapacity = ml.getPowerproduction();
                                BigDecimal temp = new BigDecimal(generatingcapacity).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP);
                                double lyl = new BigDecimal(gl).divide(temp, 2, RoundingMode.HALF_UP).doubleValue();
                                vo.setLyl(lyl);
                            } else {
                                vo.setLyl(0.0);
                            }
                        }
                        //vomap.put(wt.getId(),vo);
                        vos.add(vo);

                    }

                }
                //fjmap.put(wpId,vomap);
                fjvos.add(vos);
/*************************************************风机指标统计*************************************************************/




            }

            //风场指标MAP
            map.put("fczbmap", fczbmap);
            //风机指标MAP
            map.put("fjmap", fjvos);



        }



        return map;
    }

    /**
     * 风场总貌
     * @param wpId
     * @return
     * @throws Exception
     */
    public Map<String, Object> findGeneralAppearance(String wpId) throws Exception {

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

//        if(InitialRunner.wpmap.containsKey(wpId))
//        {

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

            int fcdjnum = 0;
            int fcyxnum = 0;
            int fcgznum = 0;
            int fclxnum = 0;
            int fcwhnum = 0;
            int fcxdnum = 0;

            int gfdjnum = 0;
            int gfyxnum = 0;
            int gfgznum = 0;
            int gflxnum = 0;
            int gfwhnum = 0;
            int gfxdnum = 0;
            String[] ycPoints1 = Constant.TPOINT_WP_FGLYC.split(",");

            //        Map<String, List<MatrixVo>> fjmap = new HashMap<>();
            List< List<MatrixVo>> fjvos=new ArrayList<>();
            Map<String, Map<String, Object>> fczbmap = new HashMap<>();



            if(StringUtils.notEmp(wpId))
            {
                Map<String, Object> jczbmap = new HashMap<>();


                if(InitialRunner.wpmap.containsKey(wpId))
                {
                    Windpowerstation wp= InitialRunner.wpmap.get(wpId);
                    jczbmap.put("name",wp.getName());
                }else if(InitialRunner.pjmap.containsKey(wpId))
                {
                    Project wp= InitialRunner.pjmap.get(wpId);
                    jczbmap.put("name",wp.getName());
                }


                /*************************************************风场指标统计*************************************************************/


                List<String> jczbls = new ArrayList<>();
                //实时风速
                jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.TPOINT_WP_SSFS).getCode());
                //预测功率
                jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, ycPoints1[0]).getCode());
                //保证功率
                jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.ZBZGL).getCode());
                //应发功率
                jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.TPOINT_WP_ZZSGL).getCode());
                //实际功率
                jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.TPOINT_WP_SJGL).getCode());
                //AGC出线有功
                jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.AGC001).getCode());
                //AGC有功设定
                jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.AGC002).getCode());
                //待机台数
                jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.TPOINT_WT_DJTS).getCode());
                //运行台数
                jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.TPOINT_WT_YXTS).getCode());
                //故障台数
                jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.TPOINT_WT_GZTJ).getCode());
                //离线台数
                jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.TPOINT_WT_TXZD).getCode());
                //维护台数
                jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.TPOINT_WT_WHTJ).getCode());
                //限电台数
                jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.TPOINT_WT_XDTS).getCode());


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

                if (!jczblist.isEmpty() && jczblist.size() == jczbls.size()) {

                    //实时风速
                    jczbmap.put("ssfs", MathUtil.twoBit(jczblist.get(0).getPointValueInDouble()));
                    //预测功率
                    jczbmap.put("ycgl", MathUtil.twoBit(jczblist.get(1).getPointValueInDouble()));
                    //保证功率
                    double bzgl = new BigDecimal(jczblist.get(2).getPointValueInDouble()).divide(new BigDecimal(1000), 2, RoundingMode.HALF_EVEN).doubleValue();
                    jczbmap.put("bzgl", bzgl);
                    //应发功率
                    double yfgl = new BigDecimal(jczblist.get(3).getPointValueInDouble()).divide(new BigDecimal(1000), 2, RoundingMode.HALF_EVEN).doubleValue();
                    jczbmap.put("yfgl", yfgl);
                    //实际功率
                    jczbmap.put("sjgl", MathUtil.twoBit(jczblist.get(4).getPointValueInDouble()));
                    //AGC出线有功
                    jczbmap.put("agccxyg", MathUtil.twoBit(jczblist.get(5).getPointValueInDouble()));
                    //AGC有功设定
                    jczbmap.put("agcygsd", MathUtil.twoBit(jczblist.get(6).getPointValueInDouble()));


                    Double djts = MathUtil.twoBit(jczblist.get(7).getPointValueInDouble());
                    //待机台数
                    jczbmap.put("djts", djts);
                    //运行台数
                    Double yxts = MathUtil.twoBit(jczblist.get(8).getPointValueInDouble());
                    jczbmap.put("yxts", yxts);
                    //故障台数
                    Double gzts = MathUtil.twoBit(jczblist.get(9).getPointValueInDouble());
                    jczbmap.put("gzts", gzts);
                    //离线台数
                    Double lxts = MathUtil.twoBit(jczblist.get(10).getPointValueInDouble());
                    jczbmap.put("lxts", lxts);
                    //维护台数
                    Double whts = MathUtil.twoBit(jczblist.get(11).getPointValueInDouble());
                    jczbmap.put("whts", whts);
                    //限电台数
                    Double xdts = MathUtil.twoBit(jczblist.get(12).getPointValueInDouble());
                    jczbmap.put("xdts", xdts);
                    //接入台数
                    jczbmap.put("jrts", djts+yxts+gzts+lxts+whts+xdts);

                } else {
                    //实时风速
                    jczbmap.put("ssfs", 0.0);
                    //预测功率
                    jczbmap.put("ssfs", 0.0);
                    //保证功率
                    jczbmap.put("bzgl", 0.0);
                    //应发功率
                    jczbmap.put("yfgl", 0.0);
                    //实际功率
                    jczbmap.put("sjgl", 0.0);
                    //AGC出线有功
                    jczbmap.put("agccxyg", 0.0);
                    //AGC有功设定
                    jczbmap.put("agcygsd", 0.0);
                    //待机台数
                    jczbmap.put("djts", 0.0);
                    //运行台数
                    jczbmap.put("yxts", 0.0);
                    //故障台数
                    jczbmap.put("gzts", 0.0);
                    //离线台数
                    jczbmap.put("lxts", 0.0);
                    //维护台数
                    jczbmap.put("whts", 0.0);
                    //限电台数
                    jczbmap.put("xdts", 0.0);
                    //接入台数
                    jczbmap.put("jrts", 0.0);
                }


                Map<String, Object> zmmap=generalAppearancePushService.findGeneralAppearance(wpId);

                //风场指标MAP
                map.put("zmmap", zmmap);
                fczbmap.put("jczbmap",jczbmap);
/*************************************************风场指标统计*************************************************************/


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

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

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

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

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


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


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


                //日场外受累电网
                wxssls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.RWZSSDL).getCode());
                // 日场外受累天气
                wxssls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.RTZSSDL).getCode());
                // 月场外受累电网
                wxssls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.YWZSSDL).getCode());
                // 月场外受累天气
                wxssls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.YTZSSDL).getCode());
                // 年场外受累电网
                wxssls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.NWZSSDL).getCode());
                // 年场外受累天气
                wxssls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, 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<String> fjzbls = new ArrayList<>();
                List<Windturbine> wtls=new ArrayList<>();
                if(InitialRunner.wp_wtmap.containsKey(wpId))
                {
                    wtls=InitialRunner.wp_wtmap.get(wpId);
                }else
                {
                    wtls=InitialRunner.pj_wtmap.get(wpId);
                }

                // Map<String, MatrixVo> vomap = new HashMap<>();
                List<MatrixVo> vos = new ArrayList<>();
                for(Windturbine wt:wtls)
                {
                    //风机状态
                    fjzbls.add(windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wt.getId(), Constant.FJZT).getId());
//                    //风速
//                    fjzbls.add(windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wt.getId(), Constant.TPOINT_WT_FJSSFS).getId());
//                    //功率
//                    fjzbls.add(windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wt.getId(), Constant.TPOINT_WT_FJGL).getId());
//                    //发电机转数
//                    fjzbls.add(windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wt.getId(), Constant.TPOINT_WT_FDJZS).getId());

                }

                List<PointData> fjzblist = realApiUtil.getRealData(fjzbls);

                if (!fjzblist.isEmpty() && fjzblist.size() == fjzbls.size()) {


                    int step=4;
                    int slts=0;
                    for(int i=0;i<wtls.size();i++)
                    {
                        Windturbine wt=wtls.get(i);

                        MatrixVo vo=new MatrixVo();
                        vo.setWtId(wt.getId());

                        if(StringUtils.notEmp(wt.getStatus()))
                        {
                            String num=wt.getStatus().substring(wt.getStatus().length()-3);
                            vo.setWtnum(num);

                        }

                        vo.setWpId(wt.getWindpowerstationid());
                        double fjzt = MathUtil.twoBit(fjzblist.get(i ).getPointValueInDouble());
                        vo.setFjzt(fjzt);
//                        vo.setFs(MathUtil.twoBit(fjzblist.get(i*step+1).getPointValueInDouble()));
//                        double gl = MathUtil.twoBit(fjzblist.get(i * step + 2).getPointValueInDouble());
//                        vo.setGl(gl);
//                        vo.setFdjzs(MathUtil.twoBit(fjzblist.get(i*step+3).getPointValueInDouble()));
                        vo.setWtCode(wt.getCode());
                        if (fjzt == 6) {
                            slts++;
                        }
//
//                        if (fjzt == 3) {
//                            vo.setLyl(0.0);
//                        } else {
//                            if (InitialRunner.mlmap.containsKey(wt.getModelid())) {
//                                Equipmentmodel ml = InitialRunner.mlmap.get(wt.getModelid());
//                                double generatingcapacity = ml.getPowerproduction();
//                                BigDecimal temp = new BigDecimal(generatingcapacity).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP);
//                                double lyl = new BigDecimal(gl).divide(temp, 2, RoundingMode.HALF_UP).doubleValue();
//                                vo.setLyl(lyl);
//                            } else {
//                                vo.setLyl(0.0);
//                            }
//                        }
                        //vomap.put(wt.getId(),vo);
                        vos.add(vo);

                    }

                    jczbmap.put("slts", slts);
                    if(jczbmap.containsKey("jrts"))
                    {
                        Double jrts=(Double)jczbmap.get("jrts");
                        jrts=jrts+slts;
                        jczbmap.put("jrts",jrts);
                    }


                }
                //fjmap.put(wpId,vomap);
                fjvos.add(vos);
/*************************************************风机指标统计*************************************************************/

/*************************************************关系库**************************************************************/


//            Map<String, Double> gxkmap = new HashMap<>();
//            List<Windpowerinfoday> wpdls = windpowerinfodayService.getWindpowerinfodayList(wpId, DateUtils.truncate(new Date()));
//            if (!wpdls.isEmpty()) {
//                Windpowerinfoday wd = wpdls.get(0);
//                gxkmap.put("ndxkyss", wd.getYeardxkyxs());
//                gxkmap.put("nsbklyl", wd.getYearsbklyl());
//
//                gxkmap.put("ydxkyss", wd.getMonthdxkyxs());
//                gxkmap.put("ysbklyl", wd.getMonthsbklyl());
//
//            } else {
//                gxkmap.put("ndxkyss", 0.0);
//                gxkmap.put("nsbklyl", 0.0);
//                gxkmap.put("ydxkyss", 0.0);
//                gxkmap.put("ysbklyl", 0.0);
//            }
//            map.put("gxkmap", gxkmap);

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

            }

            //风场指标MAP
            map.put("fczbmap", fczbmap);
            //风机指标MAP
            map.put("fjmap", fjvos);



//        }



        return map;
    }


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

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

        if(InitialRunner.wpmap.containsKey(wpId))
        {
            List< List<MatrixVo>> fjvos=new ArrayList<>();




/*************************************************风机指标统计*************************************************************/
                List<String> fjzbls = new ArrayList<>();

                List<Windturbine> wtls=InitialRunner.wp_wtmap.get(wpId);

                // Map<String, MatrixVo> vomap = new HashMap<>();
                List<MatrixVo> vos = new ArrayList<>();
                for(Windturbine wt:wtls)
                {
                    //风机状态
                    fjzbls.add(windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wt.getId(), Constant.FJZT).getId());

                }

                List<PointData> fjzblist = realApiUtil.getRealData(fjzbls);

                if (!fjzblist.isEmpty() && fjzblist.size() == fjzbls.size()) {

                    for(int i=0;i<wtls.size();i++)
                    {
                        Windturbine wt=wtls.get(i);

                        MatrixVo vo=new MatrixVo();
                        vo.setWtId(wt.getId());

                        if(StringUtils.notEmp(wt.getStatus()))
                        {
                            String num=wt.getStatus().substring(wt.getStatus().length()-3);
                            vo.setWtnum(num);

                        }

                        vo.setWpId(wt.getWindpowerstationid());
                        double fjzt = MathUtil.twoBit(fjzblist.get(i).getPointValueInDouble());
                        vo.setFjzt(fjzt);
                        vo.setWtCode(wt.getCode());

                        vos.add(vo);

                    }

                }
                //fjmap.put(wpId,vomap);
                fjvos.add(vos);
/*************************************************风机指标统计*************************************************************/



        //风机指标MAP
        map.put("fjmap", fjvos);
        }



        return map;
    }


    public List< List<MatrixVo>> findSimpleMatrixList(String wpId) throws Exception {

            List< List<MatrixVo>> fjvos=new ArrayList<>();
            if(InitialRunner.wpmap.containsKey(wpId))
            {



/*************************************************风机指标统计*************************************************************/
                List<String> fjzbls = new ArrayList<>();

                List<Windturbine> wtls=InitialRunner.wp_wtmap.get(wpId);

                // Map<String, MatrixVo> vomap = new HashMap<>();
                List<MatrixVo> vos = new ArrayList<>();
                for(Windturbine wt:wtls)
                {
                    //风机状态
                    fjzbls.add(windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wt.getId(), Constant.FJZT).getId());

                }

                List<PointData> fjzblist = realApiUtil.getRealData(fjzbls);

                if (!fjzblist.isEmpty() && fjzblist.size() == fjzbls.size()) {

                    for(int i=0;i<wtls.size();i++)
                    {
                        Windturbine wt=wtls.get(i);

                        MatrixVo vo=new MatrixVo();
                        vo.setWtId(wt.getId());

                        if(StringUtils.notEmp(wt.getStatus()))
                        {
                            String num=wt.getStatus().substring(wt.getStatus().length()-3);
                            vo.setWtnum(num);

                        }

                        vo.setWpId(wt.getWindpowerstationid());
                        double fjzt = MathUtil.twoBit(fjzblist.get(i).getPointValueInDouble());
                        vo.setFjzt(fjzt);
                        vo.setWtCode(wt.getCode());

                        vos.add(vo);

                    }

                }
                //fjmap.put(wpId,vomap);
                fjvos.add(vos);
/*************************************************风机指标统计*************************************************************/


            }



            return fjvos;
    }
}


