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 MatrixDetialGfPushService {


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



        double gfdjts=0.0;
        double gfyxts=0.0;
        double gfgzts=0.0;
        double gflxts=0.0;
        double gfwhts=0.0;
        double gfxdts=0.0;
        double gfslts=0.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) {

            if(wp.getId().endsWith("FDC"))
            {
                continue;
            }
            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());




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


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

            }

            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_NBQJLDL).getId());
                //功率
                fjzbls.add(windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wt.getId(), Constant.TPOINT_WT_FJGL).getId());
                //交流电压
                fjzbls.add(windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wt.getId(), Constant.TPOINT_WT_NBQJLDY).getId());
            }

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

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


                int step=4;


                double djts=0.0;
                double yxts=0.0;
                double gzts=0.0;
                double lxts=0.0;
                double whts=0.0;
                double xdts=0.0;
                double slts=0.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(MathUtil.twoBit(fjzblist.get(i*step).getPointValueInDouble()));

                    switch (Double.valueOf(fjzt).intValue()) {
                        case 0:
                            djts++;
                            break;
                        case 1:
                            yxts++;
                            break;
                        case 2:
                            gzts++;
                            break;
                        case 3:
                            lxts++;
                            break;
                        case 4:
                            whts++;
                            break;
                        case 5:
                            xdts++;
                            break;
                        case 6:
                            xdts++;
                            break;

                        default:

                            break;
                    }
                    vo.setFjzt(fjzt);
                    vo.setFs(MathUtil.twoBit(fjzblist.get(i*step+1).getPointValueInDouble()));
                    vo.setGl(MathUtil.twoBit(fjzblist.get(i*step+2).getPointValueInDouble()));
                    vo.setFdl(MathUtil.twoBit(fjzblist.get(i*step+3).getPointValueInDouble()));
                    vo.setWtCode(wt.getCode());
                    vo.setWpId(wt.getWindpowerstationid());
                    vos.add(vo);

                }





                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",djts+yxts+gzts+lxts+whts+xdts+slts);

                fczbmap.put(wp.getId(),jczbmap);

                gfdjts = gfdjts + Double.valueOf(djts).intValue();
                gfyxts = gfyxts + Double.valueOf(yxts).intValue();
                gfgzts = gfgzts + Double.valueOf(gzts).intValue();
                gflxts = gflxts + Double.valueOf(lxts).intValue();
                gfwhts = gfwhts + Double.valueOf(whts).intValue();
                gfxdts = gfxdts + Double.valueOf(xdts).intValue();
                gfslts = gfslts + Double.valueOf(slts).intValue();


            }

            fjvos.add(vos);

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

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

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


        map.put("gfdjts", gfdjts);
        map.put("gfyxts", gfyxts);
        map.put("gfgzts", gfgzts);
        map.put("gflxts", gflxts);
        map.put("gfwhts", gfwhts);
        map.put("gfxdts", gfxdts);
        map.put("gfslts", gfslts);

        double gfjrnum = gfdjts + gfyxts + gfgzts + gflxts + gfwhts + gfxdts+gfslts;

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


        return map;
    }


}


