package com.gyee.frame.common.spring;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.gyee.frame.common.cache.IGlobalCache;
import com.gyee.frame.model.auto.*;
import com.gyee.frame.service.*;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务启动前执行，进行全局变量初始化
 */
@Component
public class InitialRunner implements CommandLineRunner {


    @Resource
    private SysQuartzJobService sysQuartzJobService;
    @Resource
    private WindpowerstationService windpowerstationService;
    @Resource
    private ProjectService projectService;
    @Resource
    private LineService lineService;
    @Resource
    private WindturbineService windturbineService;
    @Resource
    private WindPowerstationTestingPoint2Service windPowerstationTestingPoint2Service;
    @Resource
    private WindTurbineTestingPointAiService windTurbineTestingPointAiService;
    @Resource
    private WindturbinepowercurvefittingService windturbinepowercurvefittingService;
    @Resource
    private WarningService warningService;
    @Resource
    private ModelpowerRdService modelpowerRdService;
    @Resource
    private EquipmentmodelService equipmentmodelService;
    @Resource
    private WarningClassifyService warningClassifyService;
    @Resource
    private ManufacturerService manufacturerService;
    @Resource
    private ModelPowerService modelPowerService;
    @Resource
    private IGlobalCache globalCache;
    @Resource
    private StoptypeService stoptypeService;
//    @Resource
//    private TaosMapService taosMapService;
    @Resource
    private WindTurbineTestingPointaiyjService windTurbineTestingPointaiyjService;
    @Resource
    private Alertrule2ervice alertrule2ervice;
    @Resource
    private WindturbinepartsService windturbinepartsService;

    public static List<Windpowerstation> wpallls = new ArrayList<Windpowerstation>(); // 风电场LIST集合（包含光电电）
    public static List<Project> pjallls = new ArrayList<Project>(); //项目LIST集合（包含光电电）
    public static List<Line> lineallls = new ArrayList<Line>(); // 线路LIST集合（包含光电电）
    public static List<Windturbine> wtallls = new ArrayList<Windturbine>(); // 风电机LIST集合（包含光电电）

    public static List<Windpowerstation> wpls = new ArrayList<Windpowerstation>(); // 风电场LIST集合
    public static List<Windpowerstation> spls = new ArrayList<Windpowerstation>(); // 光伏场LIST集合
    
    public static List<Project> pjls = new ArrayList<Project>(); // 项目LIST集合
    public static List<Line> linels = new ArrayList<Line>(); // 线路LIST集合
    public static List<Windturbine> wtls = new ArrayList<Windturbine>(); // 风电机LIST集合

    public static List< Alertrule2> alertrulels = new ArrayList< Alertrule2>(); // 自定义报警规则LIST集合


    public static Map<String,  Alertrule2> alertrulemap = new HashMap<String,  Alertrule2>();//自定义报警规则MAP

    public static Map<String, Windpowerstation> wpmap = new HashMap<String, Windpowerstation>();//风场（包含光电电）
    public static Map<String, Project> pjmap = new HashMap<String, Project>(); // 项目MAP（包含光电电）
    public static Map<String, Line> lnmap = new HashMap<String, Line>(); // 线路（包含光电电）
    public static Map<String, Windturbine> wtmap = new HashMap<String, Windturbine>();//风机（包含光电电）

    public static Map<String, Stoptype> stoptypemap = new HashMap<String, Stoptype>();//停机类型
    public static List<Warning> warnls = new ArrayList<Warning>(); // 报警LIST集合
    public static Map<String, Warning> warningmap = new HashMap<String, Warning>();// 报警MAP集合
    public static List<Windturbineparts> windturbinepartsls = new ArrayList<Windturbineparts>();
    public static Map<String, Windturbineparts> windturbinepartsmap = new HashMap<String, Windturbineparts>();
    public static Map<String, Map<String, List<Warning>>> warnmodelmap = new HashMap<>();
    public static Map<String, List<Warning>> warnmap = new HashMap<>();// 按照型号划分报警MAP集合 <型号，报警集合>
    public static List<WarningClassify> warningClassls = new ArrayList<WarningClassify>(); //报警类型列表
    public static Map<String, WarningClassify> warningClassifymap = new HashMap<String, WarningClassify>();
    public static Map<String, Manufacturer> manufacturermap = new HashMap<String, Manufacturer>();

    public static Map<String, List<Windturbine>> wp_wtmap = new HashMap<String, List<Windturbine>>();
    public static Map<String, List<Windturbine>> pj_wtmap = new HashMap<String, List<Windturbine>>();
    public static Map<String, List<Windturbine>> ln_wtmap = new HashMap<String, List<Windturbine>>();
    public static Map<String, List<Project>> pjsmap = new HashMap<String, List<Project>>(); // 通过风场编号获得所属项目
    public static Map<String, List<Line>> lnsmap = new HashMap<String, List<Line>>(); // 通过项目编号获得所属线路
    public static Map<String, Map<Double, ModelpowerRd>> mprdMap = new HashMap<String, Map<Double, ModelpowerRd>>();
    public static Map<String, Map<String, WindTurbineTestingPointAi>> wtPointmap = new HashMap<String, Map<String, WindTurbineTestingPointAi>>();// 风电机测点AI表
    public static Map<String, Map<String, WindPowerStationTestingPoint>> wpPointmap = new HashMap<String, Map<String, WindPowerStationTestingPoint>>();//场站测点
    public static Map<String, Map<String, WindPowerStationTestingPoint>> pjPointmap = new HashMap<String, Map<String, WindPowerStationTestingPoint>>();//项目测点
    public static Map<String, Map<String, WindPowerStationTestingPoint>> lnPointmap = new HashMap<String, Map<String, WindPowerStationTestingPoint>>();//线路测点
    public static Map<String, SysQuartzJob> jobsmap = new HashMap<String, SysQuartzJob>();
    public static Map<String, Map<Double, Windturbinepowercurvefitting>> wpcmap = new HashMap<String, Map<Double, Windturbinepowercurvefitting>>();
    public static Map<String, Equipmentmodel> mlmap = new HashMap<String, Equipmentmodel>();// 风电机模型
    public static Map<String, Map<String, ModelPower>> modelPowermap = new HashMap<String, Map<String, ModelPower>>();// 风电机类型对应功率表
    public static Map<String, String> taosmap = new HashMap<String, String>(); // Taos映射
    public static Map<String, Map<String, WindTurbineTestingPointaiyj>> wtpAiyjmap = new HashMap<String, Map<String, WindTurbineTestingPointaiyj>>();// 风电机测点AI表--针对风机预警
    public static Map<String, Map<String, Object>> appendYcglMap = new HashMap<String, Map<String, Object>>();// 预测功率追加


    public static Map<String,Integer> wppointnummap=new HashMap<>();
    Map<String, Object> ycmap = new HashMap<>();



    @Override
    public void run(String... args) throws Exception {
        System.out.println(">>>>>>>>>>>>>>>服务启动执行，换成测点关联数据 <<<<<<<<<<<<<");

        //配置追加功率曲线测点
        Map<String, Object> ycmap = new HashMap<>();

        String[] ycPoints4 = Constant.TPOINT_QS3_YGCL4.split(",");
        String[] ycPoints24 = Constant.TPOINT_QS3_YGCL24.split(",");
        //添加预测风速第一个点，存储到当前的历史值
        ycmap.put("cdq1", "SLFGL.NX_GD_QSF_YC_P1_L1_001_CDQ001");
        ycmap.put("dq1","SLFGL.NX_GD_QSF_YC_P1_L1_001_DQ0001");
        //添加预测功率后续预测点字符串数组，实时数据
        ycmap.put("cdq2", ycPoints4);
        ycmap.put("dq2", ycPoints24);

        appendYcglMap.put("QS_FDC", ycmap);

        List< Alertrule2> alertrulels =alertrule2ervice.getAlertruleAllList();
        if(!alertrulels.isEmpty())
        {
            for(Alertrule2 ar:alertrulels)
            {
                alertrulemap.put(ar.getName(),ar);
            }
        }

        windturbinepartsls=windturbinepartsService.findAll();
        if(!windturbinepartsls.isEmpty())
        {
            for(Windturbineparts wp:windturbinepartsls)
            {
                windturbinepartsmap.put(wp.getId(),wp);
            }

        }




        List<SysQuartzJob> quartzJobs=sysQuartzJobService.selectByExample(new SysQuartzJobExample());

        if(!quartzJobs.isEmpty())
        {
            for(SysQuartzJob sysQuartzJob:quartzJobs){

                jobsmap.put(sysQuartzJob.getId(),sysQuartzJob);
            }

        }


        List<WindTurbineTestingPointaiyj> list = windTurbineTestingPointaiyjService.findAll();

        Map<String, WindTurbineTestingPointaiyj> yjmap = null;

        if (list != null && !list.isEmpty()) {
            for (WindTurbineTestingPointaiyj mp : list) {
                if (wtpAiyjmap.containsKey(mp.getWindturbineid())) {
                    yjmap = wtpAiyjmap.get(mp.getWindturbineid());
                    yjmap.put(mp.getUniformcode(), mp);
                } else {
                    yjmap = new HashMap<String, WindTurbineTestingPointaiyj>();
                    yjmap.put(mp.getUniformcode(), mp);
                    wtpAiyjmap.put(mp.getWindturbineid(), yjmap);
                }
            }
        }

        String wpString = JSON.toJSONString(globalCache.get("WP"));
        wpPointmap = JSONObject.parseObject(wpString, new TypeReference<Map<String, Map<String, WindPowerStationTestingPoint>>>() {
        });
        String pjString = JSON.toJSONString(globalCache.get("PJ"));
        pjPointmap = JSONObject.parseObject(pjString, new TypeReference<Map<String, Map<String, WindPowerStationTestingPoint>>>() {
        });
        String lnString = JSON.toJSONString(globalCache.get("LN"));
        lnPointmap = JSONObject.parseObject(lnString, new TypeReference<Map<String, Map<String, WindPowerStationTestingPoint>>>() {
        });

        String wtString = JSON.toJSONString(globalCache.get("WT"));
        wtPointmap = JSONObject.parseObject(wtString, new TypeReference<Map<String, Map<String, WindTurbineTestingPointAi>>>() {
        });
        String wpcmapString = (String)(globalCache.get("wpcmap"));
        wpcmap = JSONObject.parseObject(wpcmapString, new TypeReference<Map<String, Map<Double, Windturbinepowercurvefitting>>>() {
        });

        WindpowerstationExample windpowerstationExample = new WindpowerstationExample();
        windpowerstationExample.setOrderByClause("ordernum ASC");
//        windpowerstationExample.createCriteria().andIdNotLike("%MCH%");
        wpallls = windpowerstationService.selectByExample(windpowerstationExample);
        if (!wpallls.isEmpty()) {
            for (Windpowerstation wp : wpallls) {
                if (wp.getId().endsWith("FDC")) {
                    wpls.add(wp);
                }
                if (wp.getId().endsWith("GDC")) {
                	spls.add(wp);
                }
                wpmap.put(wp.getId(), wp);
            }
        }


        ProjectExample projectExample = new ProjectExample();
        projectExample.setOrderByClause("ordernum ASC");
        //       projectExample.createCriteria().andWindpowerstationidNotLike("%GDC");
        pjallls = projectService.selectByExample(projectExample);

        if (!pjallls.isEmpty()) {
            for (Project pj : pjallls) {
                pjmap.put(pj.getId(), pj);
                if (pj.getWindpowerstationid().endsWith("FDC")) {
                    pjls.add(pj);
                }
                if (pjsmap.containsKey(pj.getWindpowerstationid())) {
                    List<Project> pjls = pjsmap.get(pj.getWindpowerstationid());
                    pjls.add(pj);
                    pjsmap.put(pj.getWindpowerstationid(), pjls);
                } else {
                    List<Project> pjls = new ArrayList<Project>();
                    pjls.add(pj);
                    pjsmap.put(pj.getWindpowerstationid(), pjls);
                }
            }
        }

        LineExample lineExample = new LineExample();
        lineExample.setOrderByClause("ordernum ASC");
        lineExample.createCriteria().getAllCriteria();
        lineallls = lineService.selectByExample(lineExample);

        if (!lineallls.isEmpty()) {
            for (Line ln : lineallls) {
                lnmap.put(ln.getId(), ln);
                if (pjmap.containsKey(ln.getProjectid())) {
                    Project pj = pjmap.get(ln.getProjectid());
                    if (pj.getWindpowerstationid().endsWith("FDC")) {
                        linels.add(ln);
                    }
                }
                if (lnsmap.containsKey(ln.getProjectid())) {
                    List<Line> lnls = lnsmap.get(ln.getProjectid());
                    lnls.add(ln);
                    lnsmap.put(ln.getProjectid(), lnls);
                } else {
                    List<Line> lnls = new ArrayList<Line>();
                    lnls.add(ln);
                    lnsmap.put(ln.getProjectid(), lnls);
                }
            }
        }

        WindturbineExample windturbineExample = new WindturbineExample();
        windturbineExample.setOrderByClause("status ASC");

        wtallls = windturbineService.selectByExample(windturbineExample);

        if (!wtallls.isEmpty()) {
            for (Windturbine wt : wtallls) {
                if (wt.getWindpowerstationid().endsWith("FDC")) {
                    wtls.add(wt);
                }
                wtmap.put(wt.getId(), wt);
            }
        }

        mprdMap = modelpowerRdService.getModelpowerRdAllMap();

        StoptypeExample stoptypeExample = new StoptypeExample();
        stoptypeExample.setOrderByClause("name ASC");

        List<Stoptype> stoptypels = stoptypeService.selectByExample(stoptypeExample);
        if (stoptypels != null && !stoptypels.isEmpty()) {
            for (int i = 0; i < stoptypels.size(); i++) {
                stoptypemap.put(stoptypels.get(i).getId(),stoptypels.get(i));
            }
        }

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

        WarningExample warningExample = new WarningExample();
        warningExample.setOrderByClause("id ASC");

        WarningExample.Criteria criteria = warningExample.createCriteria();
        criteria.andEdnavalueNotEqualTo(0);
        criteria.andWarningclassifyidIsNotNull();
        warnls = warningService.selectByExample(warningExample);

        if (warnls != null && !warnls.isEmpty()) {
            for (int i = 0; i < warnls.size(); i++) {
                Warning warning = warnls.get(i);

                if (warnmodelmap.containsKey(warning.getModelid())) {
                    Map<String, List<Warning>> map = warnmodelmap.get(warning.getModelid());
                    if(map.containsKey(warning.getWarningclassifyid()))
                    {
                        List<Warning> ls =map.get(warning.getWarningclassifyid());
                        ls.add(warning);
                        map.put(warning.getWarningclassifyid(), ls);
                    }else
                    {
                        List<Warning> ls = new ArrayList<>();
                        ls.add(warning);
                        map.put(warning.getWarningclassifyid(), ls);
                    }
                    warnmodelmap.put(warning.getModelid(),map);
                } else {
                    Map<String, List<Warning>> map = new HashMap<>();
                    List<Warning> ls = new ArrayList<>();
                    ls.add(warning);
                    map.put(warning.getWarningclassifyid(), ls);
                    warnmodelmap.put(warning.getModelid(),map);
                }

                if (warnmap.containsKey(warning.getModelid())) {
                    List<Warning> ls =warnmap.get(warning.getModelid());
                    ls.add(warning);
                    warnmap.put(warning.getModelid(),ls);
                } else {
                    List<Warning> ls =new ArrayList<>();
                    ls.add(warning);
                    warnmap.put(warning.getModelid(),ls);
                }

                warningmap.put(warning.getId(), warning);
            }
        }
        if (wtallls != null && !wtallls.isEmpty()) {

            for (int i = 0; i < wtallls.size(); i++) {

                Windturbine point = wtallls.get(i);
                if (wp_wtmap.containsKey(point.getWindpowerstationid())) {
                    List<Windturbine> ls = wp_wtmap.get(point.getWindpowerstationid());
                    ls.add(point);
                    wp_wtmap.put(point.getWindpowerstationid(), ls);
                } else {
                    List<Windturbine> ls = new ArrayList();
                    ls.add(point);
                    wp_wtmap.put(point.getWindpowerstationid(), ls);
                }

                if (pj_wtmap.containsKey(point.getProjectid())) {
                    List<Windturbine> ls = pj_wtmap.get(point.getProjectid());
                    ls.add(point);
                    pj_wtmap.put(point.getProjectid(), ls);
                } else {
                    List<Windturbine> ls = new ArrayList();
                    ls.add(point);
                    pj_wtmap.put(point.getProjectid(), ls);
                }


                if (ln_wtmap.containsKey(point.getLineid())) {
                    List<Windturbine> ls = ln_wtmap.get(point.getLineid());
                    ls.add(point);
                    ln_wtmap.put(point.getLineid(), ls);
                } else {
                    List<Windturbine> ls = new ArrayList();
                    ls.add(point);
                    ln_wtmap.put(point.getLineid(), ls);
                }

            }


        }
//

//        WindPowerStationTestingPoint2Example windPowerstationTestingPointExample=new WindPowerStationTestingPoint2Example();
//        windPowerstationTestingPointExample.createCriteria().getAllCriteria();
//        List<WindPowerStationTestingPoint2> wpPointlist= windPowerstationTestingPoint2Service.selectByExample(windPowerstationTestingPointExample);
//
//        Map<String,WindPowerStationTestingPoint2> items=null;
//        if(wpPointlist!=null && !wpPointlist.isEmpty()){
//            for(int i=0;i<wpPointlist.size();i++){
//                WindPowerStationTestingPoint2 point=wpPointlist.get(i);
//                if(stationPointmap.containsKey(point.getWindpowerstationid())){
//                    items=stationPointmap.get(point.getWindpowerstationid());
//                    items.put(point.getUniformcode(), point);
//                }
//                else{
//                    items=new HashMap<String,WindPowerStationTestingPoint2>();
//                    stationPointmap.put(point.getWindpowerstationid(), items);
//                    items.put(point.getUniformcode(), point);
//                }
//
//            }
//        }
//
//        WindTurbineTestingPointAi2Example windTurbineTestingPointAiExample=new WindTurbineTestingPointAi2Example();
//        windTurbineTestingPointAiExample.createCriteria().getAllCriteria();
//        List<WindTurbineTestingPointAi2> wtAilist= windTurbineTestingPointAiService.selectByExample(windTurbineTestingPointAiExample);
//
//        Map<String, WindTurbineTestingPointAi2> map = null;
//
//        if(wtAilist != null && !wtAilist.isEmpty()){
//            for(WindTurbineTestingPointAi2 mp : wtAilist){
//                if(wtpAimap.containsKey(mp.getWindturbineid())){
//                    map = wtpAimap.get(mp.getWindturbineid());
//                    map.put(mp.getUniformcode(), mp);
//                }else{
//                    map = new HashMap<String, WindTurbineTestingPointAi2>();
//                    map.put(mp.getUniformcode(), mp);
//                    wtpAimap.put(mp.getWindturbineid(), map);
//                }
//            }
//        }

        EquipmentmodelExample example = new EquipmentmodelExample();
        List<Equipmentmodel> mlls = equipmentmodelService.selectByExample(example);
        if (!mlls.isEmpty()) {
            for (Equipmentmodel model : mlls) {
                mlmap.put(model.getId(), model);
            }
        }

        WarningClassifyExample wcexample = new WarningClassifyExample();
        warningClassls = warningClassifyService.selectByExample(wcexample);
        if (!warningClassls.isEmpty()) {
            for (WarningClassify model : warningClassls) {
                warningClassifymap.put(model.getId(), model);
            }
        }

        ManufacturerExample mfexample = new ManufacturerExample();
        List<Manufacturer> mfls = manufacturerService.selectByExample(mfexample);
        if (!mfls.isEmpty()) {
            for (Manufacturer model : mfls) {
                manufacturermap.put(model.getId(), model);
            }
        }

        ModelPowerExample mpexample = new ModelPowerExample();
        List<ModelPower> mpls = modelPowerService.selectByExample(mpexample);
        if (!mpls.isEmpty()) {

            Map<String, ModelPower> mpmap = null;

            if (mpls != null && !mpls.isEmpty()) {
                for (ModelPower mp : mpls) {
                    if (modelPowermap.containsKey(mp.getModelid())) {
                        mpmap = modelPowermap.get(mp.getModelid());
                        mpmap.put(String.valueOf(mp.getSpeed().doubleValue()), mp);
                    } else {
                        mpmap = new HashMap<String, ModelPower>();
                        mpmap.put(String.valueOf(mp.getSpeed().doubleValue()), mp);
                        modelPowermap.put(mp.getModelid(), mpmap);
                    }
                }
            }
        }
        wppointnummap= windPowerstationTestingPoint2Service.getWpPointTotal();
    }

}
