package com.gyee.frame.common.spring;

import com.alibaba.fastjson.JSON;
import com.gyee.frame.common.cache.IGlobalCache;
import com.gyee.frame.model.auto.*;
import com.gyee.frame.model.custom.PointData;
import com.gyee.frame.service.WindPowerstationTestingPoint2Service;
import com.gyee.frame.service.WindTurbineTestingPointAiService;
import com.gyee.frame.service.WindturbinepowercurvefittingService;
import com.gyee.frame.util.DateUtils;
import com.gyee.frame.util.IRealTimeDataBaseUtil;
import com.gyee.frame.util.RealTimeDataBaseFactory;

import java.text.DecimalFormat;
import java.util.*;


public  class InitialRedis {

    public static Map<String, Map<Double, Windturbinepowercurvefitting>> wpcmap = new HashMap<String, Map<Double, Windturbinepowercurvefitting>>();


    IRealTimeDataBaseUtil realApiUtil = RealTimeDataBaseFactory.createRealTimeDataBase();

    public  void intialWindturbinepowercurvefitting() throws Exception {


        WindturbinepowercurvefittingService windturbinepowercurvefittingService= SpringUtils.getBean("windturbinepowercurvefittingService");
        IGlobalCache globalCache= SpringUtils.getBean("globalCache");

        WindturbinepowercurvefittingExample windturbinepowercurvefittingExample = new WindturbinepowercurvefittingExample();
        windturbinepowercurvefittingExample.setOrderByClause("WINDTURBINEID ASC,speed ASC");
        List<Windturbinepowercurvefitting> wpclist = windturbinepowercurvefittingService.selectByExample(windturbinepowercurvefittingExample);

        if (!wpclist.isEmpty()) {
            for (Windturbinepowercurvefitting wc : wpclist) {
                if (wpcmap.containsKey(wc.getWindturbineid())) {
                    Map<Double, Windturbinepowercurvefitting> map = wpcmap.get(wc.getWindturbineid());
                    map.put(wc.getSpeed(), wc);
                } else {
                    Map<Double, Windturbinepowercurvefitting> map = new HashMap<Double, Windturbinepowercurvefitting>();
                    map.put(wc.getSpeed(), wc);
                    wpcmap.put(wc.getWindturbineid(), map);
                }
            }
        }

        globalCache.set("wpcmap", JSON.toJSONString(wpcmap));
    }


    public  void intialWpScatter() throws Exception {



        WindPowerstationTestingPoint2Service windPowerstationTestingPoint2Service= SpringUtils.getBean("windPowerstationTestingPoint2Service");
        WindTurbineTestingPointAiService windTurbineTestingPointAiService= SpringUtils.getBean("windTurbineTestingPointAiService");
        IGlobalCache globalCache= SpringUtils.getBean("globalCache");


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

        int month=cal.get(Calendar.MONTH)+1;


        cal.set(Calendar.MONTH, 0);

        for(int j=0;j<month;j++)
        {
            List<List<Double>> vos = new ArrayList<List<Double>>();
            cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
            Date beginDate = cal.getTime();
            Date endDate =null;
            if(month-1==cal.get(Calendar.MONTH))
            {
                cal = Calendar.getInstance();
               //cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
                endDate = cal.getTime();
            }else
            {
                cal.add(Calendar.MONTH,1);;
                endDate = cal.getTime();
            }


            for(Windpowerstation wp:InitialRunner.wpls)
            {
                String wpId=wp.getId();
                WindPowerStationTestingPoint gl = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.TPOINT_WP_SJGL);
                WindPowerStationTestingPoint fs = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.TPOINT_WP_SSFS);

                int daynum = DateUtils.daysDiff(beginDate, endDate);
                long pried = 600 ;
                long count = 6 * 24 * daynum;

                DecimalFormat dcmFmt = new DecimalFormat("0.00");
                List<PointData> gls = realApiUtil.getHistoryDatasSnap(gl, beginDate.getTime() / 1000, endDate.getTime() / 1000, count, pried);

                List<PointData> fss = realApiUtil.getHistoryDatasSnap(fs, beginDate.getTime() / 1000, endDate.getTime() / 1000, count, pried);


                if (!gls.isEmpty() && !fss.isEmpty()) {
                    for (int i = 0; i < fss.size(); i++) {

                        List<Double> ds = new ArrayList<Double>();

                        double f = fss.get(i).getPointValueInDouble();
                        ds.add(Double.valueOf(dcmFmt.format(f)));
                        f = gls.get(i).getPointValueInDouble();
                        ds.add(Double.valueOf(dcmFmt.format(f)));
                        vos.add(ds);
                    }
                }

               // cal.add(Calendar.MONTH,1);
                StringBuilder sb=new StringBuilder();
                sb.append("scatter_").append(wpId).append("_").append(j+1);
                globalCache.set(String.valueOf(sb), JSON.toJSONString(vos));
                System.out.println(String.valueOf(sb.append("完成")));
            }
        }



    }



    public  void intialPjScatter() throws Exception {



        WindPowerstationTestingPoint2Service windPowerstationTestingPoint2Service= SpringUtils.getBean("windPowerstationTestingPoint2Service");
        WindTurbineTestingPointAiService windTurbineTestingPointAiService= SpringUtils.getBean("windTurbineTestingPointAiService");
        IGlobalCache globalCache= SpringUtils.getBean("globalCache");


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

        int month=cal.get(Calendar.MONTH)+1;


        cal.set(Calendar.MONTH, 0);

        for(int j=0;j<month;j++)
        {
            List<List<Double>> vos = new ArrayList<List<Double>>();
            cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
            Date beginDate = cal.getTime();
            Date endDate =null;
            if(month-1==cal.get(Calendar.MONTH))
            {
                cal = Calendar.getInstance();
                //cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
                endDate = cal.getTime();
            }else
            {
                cal.add(Calendar.MONTH,1);;
                endDate = cal.getTime();
            }


            for(Project pj:InitialRunner.pjls)
            {
                String wpId=pj.getId();
                WindPowerStationTestingPoint gl = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.TPOINT_WP_SJGL);
                WindPowerStationTestingPoint fs = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.TPOINT_WP_SSFS);

                int daynum = DateUtils.daysDiff(beginDate, endDate);
                long pried = 600 ;
                long count = 6 * 24 * daynum;

                DecimalFormat dcmFmt = new DecimalFormat("0.00");
                List<PointData> gls = realApiUtil.getHistoryDatasSnap(gl, beginDate.getTime() / 1000, endDate.getTime() / 1000, count, pried);

                List<PointData> fss = realApiUtil.getHistoryDatasSnap(fs, beginDate.getTime() / 1000, endDate.getTime() / 1000, count, pried);


                if (!gls.isEmpty() && !fss.isEmpty()) {
                    for (int i = 0; i < fss.size(); i++) {

                        List<Double> ds = new ArrayList<Double>();

                        double f = fss.get(i).getPointValueInDouble();
                        ds.add(Double.valueOf(dcmFmt.format(f)));
                        f = gls.get(i).getPointValueInDouble();
                        ds.add(Double.valueOf(dcmFmt.format(f)));
                        vos.add(ds);
                    }
                }

                // cal.add(Calendar.MONTH,1);
                StringBuilder sb=new StringBuilder();
                sb.append("scatter_").append(wpId).append("_").append(j+1);
                globalCache.set(String.valueOf(sb), JSON.toJSONString(vos));
                System.out.println(String.valueOf(sb.append("完成")));
            }
        }



    }



    public  void intialLnScatter() throws Exception {



        WindPowerstationTestingPoint2Service windPowerstationTestingPoint2Service= SpringUtils.getBean("windPowerstationTestingPoint2Service");
        WindTurbineTestingPointAiService windTurbineTestingPointAiService= SpringUtils.getBean("windTurbineTestingPointAiService");
        IGlobalCache globalCache= SpringUtils.getBean("globalCache");


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

        int month=cal.get(Calendar.MONTH)+1;


        cal.set(Calendar.MONTH, 0);

        for(int j=0;j<month;j++)
        {
            List<List<Double>> vos = new ArrayList<List<Double>>();
            cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
            Date beginDate = cal.getTime();
            Date endDate =null;
            if(month-1==cal.get(Calendar.MONTH))
            {
                cal = Calendar.getInstance();
                //cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
                endDate = cal.getTime();
            }else
            {
                cal.add(Calendar.MONTH,1);;
                endDate = cal.getTime();
            }


            for(Line wp:InitialRunner.linels)
            {
                String wpId=wp.getId();
                WindPowerStationTestingPoint gl = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.TPOINT_WP_SJGL);
                WindPowerStationTestingPoint fs = windPowerstationTestingPoint2Service.getWindPowerStationTestingPoint2(wpId, Constant.TPOINT_WP_SSFS);

                int daynum = DateUtils.daysDiff(beginDate, endDate);
                long pried = 600 ;
                long count = 6 * 24 * daynum;

                DecimalFormat dcmFmt = new DecimalFormat("0.00");
                List<PointData> gls = realApiUtil.getHistoryDatasSnap(gl, beginDate.getTime() / 1000, endDate.getTime() / 1000, count, pried);

                List<PointData> fss = realApiUtil.getHistoryDatasSnap(fs, beginDate.getTime() / 1000, endDate.getTime() / 1000, count, pried);


                if (!gls.isEmpty() && !fss.isEmpty()) {
                    for (int i = 0; i < fss.size(); i++) {

                        List<Double> ds = new ArrayList<Double>();

                        double f = fss.get(i).getPointValueInDouble();
                        ds.add(Double.valueOf(dcmFmt.format(f)));
                        f = gls.get(i).getPointValueInDouble();
                        ds.add(Double.valueOf(dcmFmt.format(f)));
                        vos.add(ds);
                    }
                }

                // cal.add(Calendar.MONTH,1);
                StringBuilder sb=new StringBuilder();
                sb.append("scatter_").append(wpId).append("_").append(j+1);
                globalCache.set(String.valueOf(sb), JSON.toJSONString(vos));
                System.out.println(String.valueOf(sb.append("完成")));
            }
        }



    }


    public  void intialWtScatter() throws Exception {


        WindTurbineTestingPointAiService windTurbineTestingPointAiService= SpringUtils.getBean("windTurbineTestingPointAiService");
        IGlobalCache globalCache= SpringUtils.getBean("globalCache");


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

        int month=cal.get(Calendar.MONTH)+1;


        cal.set(Calendar.MONTH, 0);


            List<List<Double>> vos = new ArrayList<List<Double>>();
            cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
            Date beginDate = cal.getTime();
            Date endDate =null;
            if(month-1==cal.get(Calendar.MONTH))
            {
                cal = Calendar.getInstance();
                //cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
                endDate = cal.getTime();
            }else
            {
                cal.add(Calendar.MONTH,1);;
                endDate = cal.getTime();
            }


            for(Windturbine wt:InitialRunner.wtls)
            {
                String wpId=wt.getId();
                WindTurbineTestingPointAi gl = windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wpId, Constant.TPOINT_WT_FJGL);
                WindTurbineTestingPointAi fs = windTurbineTestingPointAiService.getWindTurbineTestingPointAi2(wpId, Constant.TPOINT_WT_FJSSFS);

                int daynum = DateUtils.daysDiff(beginDate, endDate);
                long pried = 600 ;
                long count = 6 * 24 * daynum;

                DecimalFormat dcmFmt = new DecimalFormat("0.00");
                List<PointData> gls = realApiUtil.getHistoryDatasSnap(gl, beginDate.getTime() / 1000, endDate.getTime() / 1000, count, pried);

                List<PointData> fss = realApiUtil.getHistoryDatasSnap(fs, beginDate.getTime() / 1000, endDate.getTime() / 1000, count, pried);


                if (!gls.isEmpty() && !fss.isEmpty()) {
                    for (int i = 0; i < fss.size(); i++) {

                        List<Double> ds = new ArrayList<Double>();

                        double f = fss.get(i).getPointValueInDouble();
                        ds.add(Double.valueOf(dcmFmt.format(f)));
                        f = gls.get(i).getPointValueInDouble();
                        ds.add(Double.valueOf(dcmFmt.format(f)));
                        vos.add(ds);
                    }
                }

                // cal.add(Calendar.MONTH,1);
                StringBuilder sb=new StringBuilder();
                sb.append("scatter_").append(wpId).append("_").append(month);
                globalCache.set(String.valueOf(sb), JSON.toJSONString(vos));
                System.out.println(String.valueOf(sb.append("完成")));
            }




    }

    public static void main(String[]args) throws Exception {

//        SpringApplication.run(SpringbootStart.class, args);
//
//        InitialRedis initialRedis=new InitialRedis();
//        //初始化风机拟合功率
//      // initialRedis.intialWindturbinepowercurvefitting();
//
//        //初始化场站没有散点图
//        initialRedis.intialWpScatter();
    }
}
