package com.gyee.frame.common.spring;

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 WindPowerstationTestingPointService windPowerstationTestingPointService;
    @Resource
    private WindTurbineTestingPointAiService windTurbineTestingPointAiService;
    @Resource
    private WindturbinepowercurvefittingService windturbinepowercurvefittingService;
    @Resource
    private Warning2Service warning2Service;
    @Resource
    private ModelpowerRdService modelpowerRdService;
    @Resource
    private EquipmentmodelService equipmentmodelService;
    @Resource
    private WarningClassifyService warningClassifyService;
    @Resource
    private ManufacturerService manufacturerService;
    @Resource
    private ModelPowerService modelPowerService;


    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<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 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, Warning2>  warningmap = new HashMap<String, Warning2>();
    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, WindTurbineTestingPointAi2>> wtpAimap = new HashMap<String, Map<String, WindTurbineTestingPointAi2>>();// 风电机测点AI表
//    public static Map<String, Map<String, WindPowerStationTestingPoint2>> stationPointmap = new HashMap<String, Map<String, WindPowerStationTestingPoint2>>();//场站测点
    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>>();// 风电机类型对应功率表


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

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

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

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

        }

        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);
                }
                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("id ASC");
        windturbineExample.createCriteria().andWindpowerstationidNotLike("%GDC");
        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();

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

        Warning2Example warning2Example=new Warning2Example();
        warning2Example.setOrderByClause("id ASC");
        List<Warning2> warnls= warning2Service.selectByExample(warning2Example);

        if(warnls!=null && !warnls.isEmpty()) {
            for(int i=0;i<warnls.size();i++){
                Warning2 warning=warnls.get(i);
                warningmap.put(warning.getId(),warning);
            }
        }
            if(wtls!=null && !wtls.isEmpty()){

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

                Windturbine point=wtls.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= windPowerstationTestingPointService.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();
        List<WarningClassify> wcls = warningClassifyService.selectByExample(wcexample);
        if (!wcls.isEmpty()) {
            for (WarningClassify model : wcls) {
                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);
                    }
                }
            }
        }

    }

}
