package com.gyee.generation.service;

import com.alibaba.fastjson.JSON;
import com.gyee.common.contant.ContantXk;
import com.gyee.common.model.PointData;
import com.gyee.common.util.DateUtils;
import com.gyee.generation.init.CacheContext;
import com.gyee.generation.model.auto.*;
import com.gyee.generation.util.realtimesource.IEdosUtil;
import com.gyee.generation.util.redis.RedisService;
import org.springframework.stereotype.Service;

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

@Service
public class InitialRedisService {


    @Resource
    private IEdosUtil edosUtil;

    @Resource
    private RedisService redisService;


    public void initialRedisAll() throws Exception {


        System.out.println("初始化开始！");
        intialWpScatter();
        intialPjScatter();
        intialLnScatter();
        intialWtScatter();
        System.out.println("初始化完成！");

    }


    public void intialWpScatter() throws Exception {

        redisService.select(9);

        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 (ProBasicPowerstation wp : CacheContext.wpls) {
                String wpId = wp.getId();
                if (CacheContext.wppointmap.containsKey(wpId)) {
                    Map<String, ProBasicPowerstationPoint> pointMap = CacheContext.wppointmap.get(wpId);

                    if (pointMap.containsKey(ContantXk.SSZGL) && pointMap.containsKey(ContantXk.SSPJFS)) {
                        ProBasicPowerstationPoint gl = pointMap.get(ContantXk.SSZGL);
                        ProBasicPowerstationPoint fs = pointMap.get(ContantXk.SSPJFS);

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

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

                        List<PointData> fss = edosUtil.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);
                        redisService.set(String.valueOf(sb), JSON.toJSONString(vos));
                        System.out.println(String.valueOf(sb.append("完成")));
                    }


                }


            }
        }
    }


    public void intialPjScatter() throws Exception {


        redisService.select(9);
        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 (ProBasicProject pj : CacheContext.pjls) {
                String pjId = pj.getId();
                if (CacheContext.propointmap.containsKey(pjId)) {
                    Map<String, ProBasicPowerstationPoint> pointMap = CacheContext.propointmap.get(pjId);

                    if (pointMap.containsKey(ContantXk.SSZGL) && pointMap.containsKey(ContantXk.SSPJFS)) {

                        ProBasicPowerstationPoint gl = pointMap.get(ContantXk.SSZGL);
                        ProBasicPowerstationPoint fs = pointMap.get(ContantXk.SSPJFS);

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

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

                        List<PointData> fss = edosUtil.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(pjId).append("_").append(j + 1);
                        redisService.set(String.valueOf(sb), JSON.toJSONString(vos));
                        System.out.println(String.valueOf(sb.append("完成")));
                    }
                }

            }
        }


    }


    public void intialLnScatter() throws Exception {

        redisService.select(9);

        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 (ProBasicLine ln : CacheContext.lnls) {
                String lnId = ln.getId();
                if (CacheContext.linepointmap.containsKey(lnId)) {
                    Map<String, ProBasicPowerstationPoint> pointMap = CacheContext.linepointmap.get(lnId);

                    if (pointMap.containsKey(ContantXk.SSZGL) && pointMap.containsKey(ContantXk.SSPJFS)) {

                        ProBasicPowerstationPoint gl = pointMap.get(ContantXk.SSZGL);
                        ProBasicPowerstationPoint fs = pointMap.get(ContantXk.SSPJFS);

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

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

                        List<PointData> fss = edosUtil.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(lnId).append("_").append(j + 1);
                        redisService.set(String.valueOf(sb), JSON.toJSONString(vos));
                        System.out.println(String.valueOf(sb.append("完成")));
                    }
                }

            }
        }


    }


    public void intialWtScatter() throws Exception {

        redisService.select(9);
        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 (ProBasicEquipment wt : CacheContext.wtls) {
            String wtId = wt.getId();
            if (CacheContext.wtpAimap.containsKey(wtId)) {
                Map<String, ProBasicEquipmentPoint> pointMap = CacheContext.wtpAimap.get(wtId);

                if (pointMap.containsKey(ContantXk.CJ_SSGL) && pointMap.containsKey(ContantXk.CJ_SSFS)) {

                    ProBasicEquipmentPoint gl = pointMap.get(ContantXk.CJ_SSGL);
                    ProBasicEquipmentPoint fs = pointMap.get(ContantXk.CJ_SSFS);

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

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

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


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

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

                            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(wtId).append("_").append(month);
                        redisService.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();
    }
}
