package com.gyee.frame.service.app.windpowerstationdetail;

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.Windpowerinfoday;
import com.gyee.frame.model.auto.Windpowerstation;
import com.gyee.frame.model.custom.PointData;
import com.gyee.frame.model.enumeration.Messagetype;
import com.gyee.frame.netty.websocket.util.SocketToolService;
import com.gyee.frame.service.WindpowerinfodayService;
import com.gyee.frame.util.IRealTimeDataBaseUtil;
import com.gyee.frame.util.RealTimeDataBaseFactory;
import com.gyee.frame.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @author Wang Jiawen
 * @purpose
 * @date 2020-05-14 09:58
 */

@Service
public class WindpowerstationdetailService extends SocketToolService {

    //public static Map<String,String> comprehensive_target_pointMap = new HashMap<>();
    @Autowired
    private InitialRunner initialRunner;
    private static int decimal_digit = 0;
    @Resource
    private WindpowerinfodayService windpowerinfodayService;

    public void windpowerstationdetail_comprehensive_target() throws Exception {

        Map<String, Map<String, WindPowerStationTestingPoint2>> stationPointmap = InitialRunner.stationPointmap;
        List<Windpowerstation> wpls = new ArrayList<>();
        for(Windpowerstation wp:InitialRunner.wpls){
            wpls.add(wp);
        }
        Windpowerstation windpowerstationAll = new Windpowerstation();
        windpowerstationAll.setId("0");
        wpls.add(windpowerstationAll);
        Map<String,Map<String,Double>> windpowerstationpointDataMap = new HashMap();
        IRealTimeDataBaseUtil mongodb= RealTimeDataBaseFactory.createRealTimeDataBase();

        for(Windpowerstation windpowerstation:wpls){
            Map<String,Double> pointDataMap = new HashMap();
            double rfdl = 0;
            double aqts = 0;
            double swdl = 0;
            double ycfdl = 0;
            double zjrl = 0;

            try {
                //日发电量
                PointData rfdl_real = mongodb.getRealData(stationPointmap.get(windpowerstation.getId()).get(Constant.TPOINT_ALL_RFDL).getCode());
                rfdl = new BigDecimal(rfdl_real.getPointValueInDouble()).setScale(decimal_digit, RoundingMode.HALF_EVEN).doubleValue();
            }catch (Exception e){
                //System.out.println(e.getMessage());
            }

            try {
                //安全天数
                PointData aqts_real = mongodb.getRealData(stationPointmap.get("0").get(Constant.TPOINT_AQTS).getCode());
                aqts = new BigDecimal(aqts_real.getPointValueInDouble()).setScale(decimal_digit, RoundingMode.HALF_EVEN).doubleValue();
            }catch (Exception e){
                //System.out.println(e.getMessage());
            }


            try{
                //上网电量
                PointData swdl_real=mongodb.getRealData(stationPointmap.get(windpowerstation.getId()).get(Constant.TPOINT_WP_SWDL).getCode());
                swdl = new BigDecimal(swdl_real.getPointValueInDouble()).setScale(decimal_digit, RoundingMode.HALF_EVEN).doubleValue();
            }catch (Exception e){
                //System.out.println(e.getMessage());
            }

            //预测发电量
            try{
                if(windpowerstation.getId().equalsIgnoreCase("0")){
                    List<Windpowerstation> wps = InitialRunner.wpls;
                    for(Windpowerstation windpowerstationinner:wps){
                        ycfdl = ycfdl + 12;
                    }
                }else{
                    ycfdl = 6;
                }
            }
            catch(Exception e){
                System.out.printf(e.getMessage());
            }

            //装机容量
            try{
                if(windpowerstation.getCapacity()==null){
                    List<Windpowerstation> wps = InitialRunner.wpls;
                    for(Windpowerstation windpowerstationinner:wps){
                        zjrl = zjrl + windpowerstationinner.getCapacity();
                    }
                }else{
                    zjrl = windpowerstation.getCapacity();
                }

            }catch (Exception e){
                //System.out.println(e.getMessage());
            }
            pointDataMap.put("rfdl",rfdl);
            pointDataMap.put("aqts",aqts);
            pointDataMap.put("swdl",swdl);
            pointDataMap.put("ycfdl",ycfdl);
            pointDataMap.put("zjrl",zjrl);
            windpowerstationpointDataMap.put(windpowerstation.getId(),pointDataMap);
        }


        this.pushToWeb("pageNumber_7","functionNumber_1","all",windpowerstationpointDataMap, Messagetype.MESSAGE);
    }
    public void windpowerstationdetail_springbox_target() throws Exception {
        Map<String, Map<String, WindPowerStationTestingPoint2>> stationPointmap = InitialRunner.stationPointmap;
        List<Windpowerstation> wpls = new ArrayList<>();
        for(Windpowerstation wp:InitialRunner.wpls){
            wpls.add(wp);
        }
        Windpowerstation windpowerstationAll = new Windpowerstation();
        windpowerstationAll.setId("0");
        wpls.add(windpowerstationAll);
        Map<String,Map<String,Double>> windpowerstationpointDataMap = new HashMap();
        IRealTimeDataBaseUtil mongodb= RealTimeDataBaseFactory.createRealTimeDataBase();

        for(Windpowerstation windpowerstation:wpls){
            Map<String,Double> pointDataMap = new HashMap();
            double mttr = 99;
            double mtbf = 99;
            double jybm = 99;
            double jpeyhl = 99;
            double ylyxs = 99;
            double nlyxs = 99;
            double jyys = 99;
            double zhcydl = 99;
            double sbklyl = 99;
            double jpeyht = 99;


            //mttr
            mttr = jybm + 10;

            //mtbf
            mtbf = jpeyht + 12;

            try{
                //节约标煤
                PointData jybm_real=mongodb.getRealData(stationPointmap.get(windpowerstation.getId()).get(Constant.TPOINT_JYBM).getCode());
                jybm = new BigDecimal(jybm_real.getPointValueInDouble()).setScale(decimal_digit, RoundingMode.HALF_EVEN).doubleValue();
            }catch (Exception e){
                //System.out.println(e.getMessage());
            }


            try{
                //减排二氧化硫
                PointData jpeyhl_real=mongodb.getRealData(stationPointmap.get(windpowerstation.getId()).get(Constant.TPOINT_JPEYHL).getCode());
                jpeyhl = new BigDecimal(jpeyhl_real.getPointValueInDouble()).setScale(decimal_digit, RoundingMode.HALF_EVEN).doubleValue();
            }catch (Exception e){
                //System.out.println(e.getMessage());
            }


            try{
                //节约用水
                PointData jyys_real=mongodb.getRealData(stationPointmap.get(windpowerstation.getId()).get(Constant.TPOINT_JYS).getCode());
                jyys = new BigDecimal(jyys_real.getPointValueInDouble()).setScale(decimal_digit, RoundingMode.HALF_EVEN).doubleValue();
            }catch (Exception e){
                //System.out.println(e.getMessage());
            }

            Calendar c = Calendar.getInstance();
            Windpowerinfoday wp=windpowerinfodayService.findListByDate(windpowerstation.getId(),c.getTime());
            if(StringUtils.isNotNull(wp))
            {
                ylyxs=wp.getMonthlyxs();
                nlyxs=wp.getYearlyxs();
                //综合场用电率
                zhcydl=wp.getDayzhcydl();
                //设备可利用率
                sbklyl=wp.getDaysbklyl();
                //System.out.println(wp.getRecorddate()+ "___" +wp.getMonthlyxs());
                //System.out.println(wp.getRecorddate()+ "___" +wp.getYearlyxs());
                //System.out.println(wp.getRecorddate()+ "___" +wp.getDayzhcydl());
                //System.out.println(wp.getRecorddate()+ "___" +wp.getDaysbklyl());
            }


            try{
                //减排二氧化碳
                PointData jpeyht_real=mongodb.getRealData(stationPointmap.get(windpowerstation.getId()).get(Constant.TPOINT_JPEYHT).getCode());
                jpeyht = new BigDecimal(jpeyht_real.getPointValueInDouble()).setScale(decimal_digit, RoundingMode.HALF_EVEN).doubleValue();
            }catch (Exception e){
                //System.out.println(e.getMessage());
            }


            pointDataMap.put("mttr",mttr);
            pointDataMap.put("mtbf",mtbf);
            pointDataMap.put("jybm",jybm);
            pointDataMap.put("jpeyhl",jpeyhl);
            pointDataMap.put("ylyxs",ylyxs);
            pointDataMap.put("nlyxs",nlyxs);
            pointDataMap.put("jyys",jyys);
            pointDataMap.put("zhcydl",zhcydl);
            pointDataMap.put("sbklyl",sbklyl);
            pointDataMap.put("jpeyht",jpeyht);
            windpowerstationpointDataMap.put(windpowerstation.getId(),pointDataMap);
        }
        this.pushToWeb("pageNumber_7","functionNumber_2","all",windpowerstationpointDataMap, Messagetype.MESSAGE);
    }

/*    double  focastpower(String name){
        ForecastwindspeedamendedService forecastwindspeedamendedService= SpringUtils.getBean("forecastwindspeedamendedService");

        ForecastwindspeedamendedExample forecastwindspeedamendedExample=new ForecastwindspeedamendedExample();
        Calendar c = Calendar.getInstance();

        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        Date begin=c.getTime();
        c.add(Calendar.DAY_OF_MONTH,1);
        Date end=c.getTime();
        forecastwindspeedamendedExample.createCriteria().andWpidEqualTo(name).andRecodedateBetween(begin,end);
        List<Forecastwindspeedamended> wtls = forecastwindspeedamendedService.selectByExample(forecastwindspeedamendedExample);
        double tmp = 0;
        for(Forecastwindspeedamended wt:wtls)
        {
            tmp = tmp + wt.getDaypower().doubleValue()+wt.getNightpower().doubleValue();
        }
        return tmp;
    }*/
}
