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.Equipmentmodel;
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 MatrixDetialPushService {


    @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 fcsdtjnum = 0;
        int fczcfdnum = 0;
        int fcqxjclnum = 0;
        int fcxdjclnum = 0;
        int fcxdtjnum = 0;
        int fcgztjnum = 0;
        int fccnsltjnum = 0;
        int fcjxtjnum = 0;
        int fccnsljxnum = 0;
        int fcdwslnum = 0;
        int fchjslnum = 0;
        int fclxnum = 0;

        int gfdjnum = 0;
        int gfsdtjnum = 0;
        int gfzcfdnum = 0;
        int gfqxjclnum = 0;
        int gfxdjclnum = 0;
        int gfxdtjnum = 0;
        int gfgztjnum = 0;
        int gfcnsltjnum = 0;
        int gfjxtjnum = 0;
        int gfcnsljxnum = 0;
        int gfdwslnum = 0;
        int gfhjslnum = 0;
        int gflxnum = 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.wpls) {

            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.DJNUM).getCode());
            //手动停机台数
            jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wp.getId(), Constant.TJNUM).getCode());
            //正常发电台数
            jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wp.getId(), Constant.FDNUM).getCode());
            // 缺陷降出力台数
            jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wp.getId(), Constant.FDJCLNUM).getCode());
            // 限电降出力台数
            jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wp.getId(), Constant.XDJCLNUM).getCode());
            // 限电停机台数
            jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wp.getId(), Constant.XDTJNUM).getCode());
            // 故障停机台数
            jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wp.getId(), Constant.GZNUM).getCode());
            // 场内受累停机台数
            jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wp.getId(), Constant.CNGZNUM).getCode());
            // 检修停机台数
            jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wp.getId(), Constant.JXNUM).getCode());
            // 场内受累检修台数
            jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wp.getId(), Constant.CNJXNUM).getCode());
            // 电网受累台数
            jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wp.getId(), Constant.DWSLNUM).getCode());
            // 环境受累台数
            jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wp.getId(), Constant.HJSLNUM).getCode());
            // 风机离线台数
            jczbls.add(windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wp.getId(), Constant.LXNUM).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 djnum = MathUtil.twoBit(jczblist.get(7).getPointValueInDouble());
                jczbmap.put("djnum", djnum);
                //手动停机台数
                Double sdtjnum = MathUtil.twoBit(jczblist.get(8).getPointValueInDouble());
                jczbmap.put("sdtjts", sdtjnum);
                //正常发电台数
                Double zcfdnum = MathUtil.twoBit(jczblist.get(9).getPointValueInDouble());
                jczbmap.put("zcfdnum", zcfdnum);
                //缺陷降出力台数
                Double qxjclnum = MathUtil.twoBit(jczblist.get(10).getPointValueInDouble());
                jczbmap.put("qxjclnum", qxjclnum);
                // 限电降出力台数
                Double xdjclnum = MathUtil.twoBit(jczblist.get(11).getPointValueInDouble());
                jczbmap.put("xdjclnum", xdjclnum);
                // 限电停机台数
                Double xdtjnum = MathUtil.twoBit(jczblist.get(12).getPointValueInDouble());
                jczbmap.put("xdtjnum", xdtjnum);
                // 故障停机台数
                Double gztjnum = MathUtil.twoBit(jczblist.get(13).getPointValueInDouble());
                jczbmap.put("gztjnum", gztjnum);
                // 场内受累停机台数
                Double cnsltjnum = MathUtil.twoBit(jczblist.get(14).getPointValueInDouble());
                jczbmap.put("cnsltjnum", cnsltjnum);
                // 检修停机台数
                Double jxtjnum = MathUtil.twoBit(jczblist.get(15).getPointValueInDouble());
                jczbmap.put("jxtjnum", jxtjnum);
                // 场内受累检修台数
                Double cnsljxnum = MathUtil.twoBit(jczblist.get(16).getPointValueInDouble());
                jczbmap.put("cnsljxnum", cnsljxnum);
                // 电网受累台数
                Double dwslnum = MathUtil.twoBit(jczblist.get(17).getPointValueInDouble());
                jczbmap.put("dwslnum", dwslnum);
                // 环境受累台数
                Double hjslnum = MathUtil.twoBit(jczblist.get(18).getPointValueInDouble());
                jczbmap.put("hjslnum", hjslnum);
                // 风机离线台数
                Double lxnum = MathUtil.twoBit(jczblist.get(19).getPointValueInDouble());
                jczbmap.put("lxnum", lxnum);


                double djts = djnum + sdtjnum;
                double yxts = zcfdnum + qxjclnum;
                double gzts = gztjnum + cnsltjnum;
                double lxts = lxnum;
                double whts = jxtjnum + cnsljxnum;
                double xdts = xdjclnum + xdtjnum;
                double slts = dwslnum + hjslnum;

                jczbmap.put("djts", djts);
                jczbmap.put("yxts", yxts);
                jczbmap.put("gzts", gzts);
                jczbmap.put("lxts", lxts);
                jczbmap.put("whts", whts);
                jczbmap.put("xdts", xdts);
                jczbmap.put("slts", slts);

                jczbmap.put("jrts", djnum + sdtjnum + zcfdnum + qxjclnum + xdjclnum + xdtjnum + gztjnum + cnsltjnum + jxtjnum + cnsljxnum + dwslnum + hjslnum + lxnum);

                if (wp.getId().endsWith("FDC")) {

                    fcdjnum = fcdjnum + djnum.intValue();
                    fcsdtjnum = fcsdtjnum + sdtjnum.intValue();
                    fczcfdnum = fczcfdnum + zcfdnum.intValue();
                    fcqxjclnum = fcqxjclnum + qxjclnum.intValue();
                    fcxdjclnum = fcxdjclnum + xdjclnum.intValue();
                    fcxdtjnum = fcxdtjnum + xdtjnum.intValue();
                    fcgztjnum = fcgztjnum + gztjnum.intValue();
                    fccnsltjnum = fccnsltjnum + cnsltjnum.intValue();
                    fcjxtjnum = fcjxtjnum + jxtjnum.intValue();
                    fccnsljxnum = fccnsljxnum + cnsljxnum.intValue();
                    fcdwslnum = fcdwslnum + dwslnum.intValue();
                    fchjslnum = fchjslnum + hjslnum.intValue();
                    fclxnum = fclxnum + lxnum.intValue();

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

                    gfdjnum = gfdjnum + djnum.intValue();
                    gfsdtjnum = gfsdtjnum + sdtjnum.intValue();
                    gfzcfdnum = gfzcfdnum + zcfdnum.intValue();
                    gfqxjclnum = gfqxjclnum + qxjclnum.intValue();
                    gfxdjclnum = gfxdjclnum + xdjclnum.intValue();
                    gfxdtjnum = gfxdtjnum + xdtjnum.intValue();
                    gfgztjnum = gfgztjnum + gztjnum.intValue();
                    gfcnsltjnum = gfcnsltjnum + cnsltjnum.intValue();
                    gfjxtjnum = gfjxtjnum + jxtjnum.intValue();
                    gfcnsljxnum = gfcnsljxnum + cnsljxnum.intValue();
                    gfdwslnum = gfdwslnum + dwslnum.intValue();
                    gfhjslnum = gfhjslnum + hjslnum.intValue();
                    gflxnum = gflxnum + lxnum.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("djnum", 0.0);
                //手动停机台数
                jczbmap.put("sdtjts", 0.0);
                //正常发电台数
                jczbmap.put("zcfdnum", 0.0);
                //缺陷降出力台数
                jczbmap.put("qxjclnum", 0.0);
                // 限电降出力台数
                jczbmap.put("xdjclnum", 0.0);
                // 限电停机台数
                jczbmap.put("xdtjnum", 0.0);
                // 故障停机台数
                jczbmap.put("gztjnum", 0.0);
                // 场内受累停机台数
                jczbmap.put("cnsltjnum", 0.0);
                // 检修停机台数
                jczbmap.put("jxtjnum", 0.0);
                // 场内受累检修台数
                jczbmap.put("cnsljxnum", 0.0);
                // 电网受累台数
                jczbmap.put("dwslnum", 0.0);
                // 环境受累台数
                jczbmap.put("hjslnum", 0.0);
                // 风机离线台数
                jczbmap.put("lxnum", 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.ZTMX).getId());
                //风速
                fjzbls.add(windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wt.getId(), Constant.TPOINT_WT_FJSSFS).getId());
                //功率
                fjzbls.add(windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wt.getId(), Constant.TPOINT_WT_FJGL).getId());

                //风机转数
                if (wt.getWindpowerstationid().equals("QS_FDC")) {
                    fjzbls.add(windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wt.getId(), Constant.QSTPOINT_WT_FDJZS).getId());
                } else {
                    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.setFjzt(MathUtil.twoBit(fjzblist.get(i * step).getPointValueInDouble()));
                    vo.setFs(MathUtil.twoBit(fjzblist.get(i * step + 1).getPointValueInDouble()));
                    vo.setGl(MathUtil.twoBit(fjzblist.get(i * step + 2).getPointValueInDouble()));

                    //风机转数

                    double fdjzs = MathUtil.twoBit(fjzblist.get(i * step + 3).getPointValueInDouble());
                    Equipmentmodel model = InitialRunner.mlmap.get(wt.getModelid());
                    if (model.getPowerproduction() == 2000) {
                        vo.setFdjzs(fdjzs * 9.55);
                    } else {
                        vo.setFdjzs(fdjzs);
                    }


                    vo.setWtCode(wt.getCode());
                    vo.setWpId(wt.getWindpowerstationid());
                    vos.add(vo);

                }

            }
            fjvos.add(vos);

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

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

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


        map.put("fcdjnum", fcdjnum);
        map.put("fcsdtjnum", fcsdtjnum);
        map.put("fczcfdnum", fczcfdnum);
        map.put("fcqxjclnum", fcqxjclnum);
        map.put("fcxdjclnum", fcxdjclnum);
        map.put("fcxdtjnum", fcxdtjnum);
        map.put("fcgztjnum", fcgztjnum);
        map.put("fccnsltjnum", fccnsltjnum);
        map.put("fcjxtjnum", fcjxtjnum);
        map.put("fccnsljxnum", fccnsljxnum);
        map.put("fcdwslnum", fcdwslnum);
        map.put("fchjslnum", fchjslnum);
        map.put("fclxnum", fclxnum);


        double fcjrnum = fcdjnum + fcsdtjnum + fczcfdnum + fcqxjclnum + fcxdjclnum + fcxdtjnum + fcgztjnum + fccnsltjnum + fcjxtjnum + fccnsljxnum + fcdwslnum + fchjslnum + fclxnum;

        map.put("fcjrnum", fcjrnum);
//        map.put("gfdjnum", gfdjnum);
//        map.put("gfsdtjnum", gfsdtjnum);
//        map.put("gfzcfdnum", gfzcfdnum);
//        map.put("gfqxjclnum", gfqxjclnum);
//        map.put("gfxdjclnum", gfxdjclnum);
//        map.put("gfxdtjnum", gfxdtjnum);
//        map.put("gfgztjnum", gfgztjnum);
//        map.put("gfcnsltjnum", gfcnsltjnum);
//        map.put("gfjxtjnum", gfjxtjnum);
//        map.put("gfcnsljxnum", gfcnsljxnum);
//        map.put("gfdwslnum", gfdwslnum);
//        map.put("gfhjslnum", gfhjslnum);
//        map.put("gflxnum", gflxnum);


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


        return map;
    }


}


