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.Windpowerstation;
import com.gyee.frame.model.auto.Windturbine;
import com.gyee.frame.model.custom.MatrixVo;
import com.gyee.frame.model.custom.PointData;
import com.gyee.frame.service.ProjectPlanService;
import com.gyee.frame.service.WindPowerstationTestingPoint2Service;
import com.gyee.frame.service.WindTurbineTestingPointAiService;
import com.gyee.frame.service.WindpowerinfodayService;
import com.gyee.frame.util.IRealTimeDataBaseUtil;
import com.gyee.frame.util.MathUtil;
import com.gyee.frame.util.RealTimeDataBaseFactory;
import com.gyee.frame.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MatrixPushService {


    @Resource
    private WindPowerstationTestingPoint2Service windPowerstationTestingPoint2Service;
    @Resource
    private WindpowerinfodayService windpowerinfodayService;
    @Resource
    private ProjectPlanService projectPlanService;
    @Resource
    private WindTurbineTestingPointAiService windTurbineTestingPointAiService;


    IRealTimeDataBaseUtil realApiUtil = RealTimeDataBaseFactory.createRealTimeDataBase();


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

        Map<String, Object> map = findMatrix();


        return map;

    }

    private Map<String, Object> findMatrix() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();

        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 fcslnum = 0;

        int gfdjnum = 0;
        int gfyxnum = 0;
        int gfgznum = 0;
        int gflxnum = 0;
        int gfwhnum = 0;
        int gfxdnum = 0;
        int gfslnum = 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<>();
        for (Windpowerstation wp : InitialRunner.wpallls) {

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


            jczbmap.put("name", wp.getName());
            /*************************************************风场指标统计*************************************************************/


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

                if (wp.getId().endsWith("FDC")) {
                    fcdjnum = fcdjnum + djts.intValue();
                    fcyxnum = fcyxnum + yxts.intValue();
                    fcgznum = fcgznum + gzts.intValue();
                    fclxnum = fclxnum + lxts.intValue();
                    fcwhnum = fcwhnum + whts.intValue();
                    fcxdnum = fcxdnum + xdts.intValue();

                } else if (wp.getId().endsWith("GDC")) {

                    gfdjnum = gfdjnum + djts.intValue();
                    gfyxnum = gfyxnum + yxts.intValue();
                    gfgznum = gfgznum + gzts.intValue();
                    gflxnum = gflxnum + lxts.intValue();
                    gfwhnum = gfwhnum + whts.intValue();
                    gfxdnum = gfxdnum + xdts.intValue();
                }

            } 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);
            }

            fczbmap.put(wp.getId(), jczbmap);
/*************************************************风场指标统计*************************************************************/


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

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

            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());

            }

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

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


                int step = 3;
                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);

                    }


                    double fjzt = MathUtil.twoBit(fjzblist.get(i * step).getPointValueInDouble());
//                    if (fjzt == 6) {
//                        slts++;
//                    }
                    vo.setFjzt(fjzt);
                    vo.setFs(MathUtil.twoBit(fjzblist.get(i * step + 1).getPointValueInDouble()));
                    vo.setGl(MathUtil.twoBit(fjzblist.get(i * step + 2).getPointValueInDouble()));
                    vo.setWtCode(wt.getCode());
                    vo.setWpId(wt.getWindpowerstationid());
                    vos.add(vo);

                }


                jczbmap.put("slts", 0.0);

                double djts=(Double)jczbmap.get("djts");
                double yxts=(Double)jczbmap.get("yxts");
                double gzts=(Double)jczbmap.get("gzts");
                double lxts=(Double)jczbmap.get("lxts");
                double whts=(Double)jczbmap.get("whts");
                double xdts=(Double)jczbmap.get("xdts");
                jczbmap.put("jrts", Double.valueOf(slts)+djts+yxts+gzts+lxts+whts+xdts);
                if (wp.getId().endsWith("FDC")) {
                    fcslnum = fcslnum + slts;
                } else {
                    gfslnum = gfslnum + slts;
                }

            }
            fjvos.add(vos);

/*************************************************风机指标统计*************************************************************/

        }
        //风场待机台数
        map.put("fczbmap", fczbmap);
        //风机指标对象
        map.put("fjmap", fjvos);

/*******************************************风场、光伏状态数量统计***********************************************************/

        //风场待机台数
        map.put("fcdjnum", fcdjnum);
        //风场运行台数
        map.put("fcyxnum", fcyxnum);
        //风场故障台数
        map.put("fcgznum", fcgznum);
        //风场离线台数
        map.put("fclxnum", fclxnum);
        //风场维护台数
        map.put("fcwhnum", fcwhnum);
        //风场限电台数
        map.put("fcxdnum", fcxdnum);
        //风场限电台数
        map.put("fcslnum", fcslnum);
        //风场受累台数
        double fcjrnum = fcdjnum + fcyxnum + fcgznum + fclxnum + fcwhnum + fcxdnum + fcslnum;

        map.put("fcjrnum", fcjrnum);

        //光伏待机台数
        map.put("gfdjnum", gfdjnum);
        //光伏运行台数
        map.put("gfyxnum", gfyxnum);
        //光伏故障台数
        map.put("gfgznum", gfgznum);
        //光伏离线台数
        map.put("gflxnum", gflxnum);
        //光伏维护台数
        map.put("gfwhnum", gfwhnum);
        //光伏限电台数
        map.put("gfxdnum", gfxdnum);
        //光伏受累台数
        map.put("gfslnum", gfslnum);
        double gfjrnum = gfdjnum + gfyxnum + gfgznum + gflxnum + gfwhnum + gfslnum;

        map.put("gfjrnum", gfjrnum);
/*******************************************风场、光伏状态数量统计***********************************************************/


        return map;
    }


}


