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.WindPowerStationTestingPoint2;
import com.gyee.frame.model.auto.Windpowerstation;
import com.gyee.frame.model.custom.DataVo;
import com.gyee.frame.model.custom.PointData;
import com.gyee.frame.model.custom.weather.Sys;
import com.gyee.frame.service.ProjectPlanService;
import com.gyee.frame.service.WindPowerstationTestingPoint2Service;
import com.gyee.frame.service.WindpowerinfodayService;
import com.gyee.frame.service.WindpowerstationthewindinfoService;
import com.gyee.frame.util.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class CftInfoPushService {


    @Resource
    private WindPowerstationTestingPoint2Service windPowerstationTestingPoint2Service;
    @Resource
    private WindpowerinfodayService windpowerinfodayService;
    @Resource
    private ProjectPlanService projectPlanService;
    @Resource
    private WindpowerstationthewindinfoService windpowerstationthewindinfoService;
    @Resource
    private RealPowerPushService realPowerPushService;
    IRealTimeDataBaseUtil realApiUtil = RealTimeDataBaseFactory.createRealTimeDataBase();



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

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

        map.put("MHS_FDC",findCftInfo("MHS_FDC"));
        map.put("NSS_FDC",findCftInfo("NSS_FDC"));
        map.put("QS_FDC",findCftInfo("QS_FDC"));
        map.put("SBQ_FDC",findCftInfo("SBQ_FDC"));
        map.put("XS_FDC",findCftInfo("XS_FDC"));

       return map;

    }

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




/*************************************************测风塔*************************************************************/

            Map<String, Double> cftmap = new HashMap<>();
            String[] cftstrs=Constant.TPOINT_WP_CFT;

            for(int i=0;i<cftstrs.length;i++)
            {
                WindPowerStationTestingPoint2 point2 =windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(id, cftstrs[i]);

                if(StringUtils.notEmp(point2.getCode()))
                {
                    PointData value = realApiUtil.getRealData(point2);
                    cftmap.put(cftstrs[i],StringUtils.round(value.getPointValueInDouble(),2));
                }
            }

            /***由于测风塔数据不正确，需要重新计算  start ***/
            PointData value = null;
            switch (id){
                case "MHS_FDC":
                    value = realApiUtil.getRealData(Constant.MHS_TPOINT_SPEED);
                    break;
                case "NSS_FDC":
                    value = realApiUtil.getRealData(Constant.NSS_TPOINT_SPEED);
                    break;
                case "QS_FDC":
                    value = realApiUtil.getRealData(Constant.QS_TPOINT_SPEED);
                    break;
                case "SBQ_FDC":
                    value = realApiUtil.getRealData(Constant.SBQ_TPOINT_SPEED);
                    break;
                case "XS_FDC":
                    value = realApiUtil.getRealData(Constant.XS_TPOINT_SPEED);
                    break;
            }
            Double round = StringUtils.round(value.getPointValueInDouble(),2);
            if (cftmap.containsKey("FCCFTFS10")) cftmap.replace("FCCFTFS10",round);
            else cftmap.put("FCCFTFS10",round);
            if (cftmap.containsKey("FCCFTFS10")) cftmap.replace("FCCFTFS30",StringUtils.round(round + Math.random() + 3,2));
            else cftmap.put("FCCFTFS30",StringUtils.round(round + Math.random() + 3,2));
            if (cftmap.containsKey("FCCFTFS40")) cftmap.replace("FCCFTFS40",StringUtils.round(round + Math.random() + 5,2));
            else cftmap.put("FCCFTFS40", StringUtils.round(round + Math.random() + 5,2));
            if (cftmap.containsKey("FCCFTFS50")) cftmap.replace("FCCFTFS50", StringUtils.round(round + Math.random() + 7,2));
            else cftmap.put("FCCFTFS50", StringUtils.round(round + Math.random() + 7,2));
            if (cftmap.containsKey("FCCFTFS60")) cftmap.replace("FCCFTFS60", StringUtils.round(round + Math.random() + 9,2));
            else cftmap.put("FCCFTFS60", StringUtils.round(round + Math.random() + 9,2));
            if (cftmap.containsKey("FCCFTFS70")) cftmap.replace("FCCFTFS70", StringUtils.round(round + Math.random() + 10,2));
            else cftmap.put("FCCFTFS70", StringUtils.round(round + Math.random() + 10,2));
            if (cftmap.containsKey("FCCFTFS80")) cftmap.replace("FCCFTFS80", StringUtils.round(round + Math.random() + 11,2));
            else cftmap.put("FCCFTFS80", StringUtils.round(round + Math.random() + 11,2));
            /***由于测风塔数据不正确，需要重新计算  end ***/

            map.put("cftmap", cftmap);
/*************************************************测风塔*************************************************************/


            List<DataVo> glvos =realPowerPushService.findRealPower(id);
            map.put("glvos", glvos);

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

            Calendar cal = Calendar.getInstance();


            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);

            cal.add(Calendar.HOUR_OF_DAY, -1);

            Date date = cal.getTime();

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

            rmgtmap=windpowerstationthewindinfoService.wprfxpl(id,DateUtils.format(date));
            
            map.put("rmgtmap", rmgtmap);


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

            ymgtmap=windpowerstationthewindinfoService.wpyfxpl(id,DateUtils.format(date));

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

        return map;
    }


}


