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;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * 服务启动执行
 */
@Component
public class InitialRunner implements CommandLineRunner {


    @Resource
    private RMConfigService rMConfigService;
    @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 WindTurbineTestingPointDiService windTurbineTestingPointDiService;
    //RMConfig

    public static Map<String, RMConfig> rmcmap = new HashMap<String, RMConfig>();
    public static List<Windpowerstation> wpallls = new ArrayList<Windpowerstation>(); // 风电场LIST集合
    public static List<Windpowerstation> wpls = new ArrayList<Windpowerstation>(); // 风电场LIST集合
    public static List<Project> pjls = new ArrayList<Project>(); // 项目LIST集合
    public static List<Windturbine> wtls = new ArrayList<Windturbine>(); // 风电机LIST集合
//    public static List<Line> lnls = new ArrayList<Line>(); // 线路LIST集合

    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, 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, CopyOnWriteArraySet<String>> keyidMap = new HashMap<String, CopyOnWriteArraySet<String>>();

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

        RMConfigExample example=new RMConfigExample();
        example.createCriteria().getAllCriteria();
        List<RMConfig> list= rMConfigService.selectByExample(example);
        if(!list.isEmpty())
        {
            for(RMConfig rm:list)
            {
                rmcmap.put(rm.getId(),rm);
            }
        }

        WindpowerstationExample windpowerstationExample=new WindpowerstationExample();
        windpowerstationExample.setOrderByClause("ordernum ASC");
        windpowerstationExample.createCriteria().andIdNotLike("%MCH%");
        wpallls= windpowerstationService.selectByExample(windpowerstationExample);

        windpowerstationExample=new WindpowerstationExample();
        windpowerstationExample.setOrderByClause("ordernum ASC");
        windpowerstationExample.createCriteria().andIdNotLike("%GDC");
        wpls= windpowerstationService.selectByExample(windpowerstationExample);

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

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

        WindturbineExample windturbineExample=new WindturbineExample();
        windturbineExample.setOrderByClause("id ASC");
        windturbineExample.createCriteria().andWindpowerstationidNotLike("%GDC");
        wtls= windturbineService.selectByExample(windturbineExample);


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


//        String pageNumber = "pageNumber_4";
//        String functionNumber = "functionNumber_2";
//        StringBuilder sb = new StringBuilder();
//        sb.append(pageNumber).append("_").append(functionNumber);
//
//        String jobkey = String.valueOf(sb);
//
//
//        CopyOnWriteArraySet<String> set=new CopyOnWriteArraySet<String>();
//        for(Windpowerstation wp: wpls)
//        {
//            StringBuilder sb2 = new StringBuilder();
//            sb2.append("wp_").append(wp.getId());
//            set.add(String.valueOf(sb2));
//        }
//
//        for(Project pj: pjls)
//        {
//            StringBuilder sb2 = new StringBuilder();
//            sb2.append("pj_").append(pj.getId());
//            set.add(String.valueOf(sb2));
//        }
//
//        keyidMap.put(jobkey,set);
    }

}
