package cn.gyee.tamplate.init;

import cn.gyee.tamplate.model.auto.*;
import cn.gyee.tamplate.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName : CacheContext
 * @Author : xieshengjie
 * @Date: 2021/1/18 14:48
 * @Description : 初始化资源
 */
@Component
public class CacheContext implements CommandLineRunner {
    private Logger logger= LoggerFactory.getLogger(CacheContext.class);
    @Resource
    private IWindturbinetestingpointai2Service windturbinetestingpointai2Service;
    @Resource
    private IWindpowerstationtestingpoint2Service windpowerstationtestingpoint2Service;
    @Resource
    private IProjectService projectService;
    @Resource
    private IWindpowerstationService windpowerstationService;
    @Resource
    private IMeterpoint2Service meterpoint2Service;
    @Resource
    private IAnalysiswindtargetService analysiswindtargetService;

    public static Map<String, Map<String, Windturbinetestingpointai2>> wtpointMap = new HashMap<>();

    public static Map<String,List<Windturbinetestingpointai2>> wtpmap = new HashMap<>();

    public static Map<String,Map<String, Windpowerstationtestingpoint2>> wppointmap =new HashMap<>();

    public static List<Project> projects = new ArrayList<>();

    public static List<Windpowerstation> stations = new ArrayList<>();

    public static Map<String,Windpowerstation> wpmaps = new HashMap<>();

    public static Map<String,String> wpmap = new HashMap<>();

    public static Map<String,Meterpoint2> meterMap = new HashMap<>();

    public static Map<String,List<Meterpoint2>> wpmeterMap = new HashMap<>();

    public static List<Analysiswindtarget> targetlist = new ArrayList<>();

    public static Map<String,Project> projectMap = new HashMap<>();

    @Override
    public void run(String... args) throws Exception {

        targetlist = analysiswindtargetService.list();

        for (Meterpoint2 meterpoint2 : meterpoint2Service.list()) {
            meterMap.put(meterpoint2.getId(),meterpoint2);
            if (wpmeterMap.containsKey(meterpoint2.getWindpowerstationid())){
                wpmeterMap.get(meterpoint2.getWindpowerstationid()).add(meterpoint2);
            }else{
                List<Meterpoint2> list = new ArrayList<>();
                list.add(meterpoint2);
                wpmeterMap.put(meterpoint2.getWindpowerstationid(),list);
            }
        }

        projects = projectService.list().stream().sorted(Comparator.comparing(Project::getOrdernum)).collect(Collectors.toList());
        for (Project project : projects) {
            projectMap.put(project.getId(),project);
        }
        stations = windpowerstationService.list();
        for (Windpowerstation station : stations) {
            wpmap.put(station.getId(),station.getName());
            wpmaps.put(station.getId(),station);
        }

        List<Windturbinetestingpointai2> wtplist = windturbinetestingpointai2Service.list();
        for (Windturbinetestingpointai2 pointai : wtplist) {
            if (wtpointMap.containsKey(pointai.getWindturbineid())){
                wtpointMap.get(pointai.getWindturbineid()).put(pointai.getUniformcode(),pointai);
            }else{
                Map<String, Windturbinetestingpointai2> map = new HashMap<>();
                map.put(pointai.getUniformcode(),pointai);
                wtpointMap.put(pointai.getWindturbineid(),map);
            }
        }

        //----------------------------------------------------------------------------------------------------
        List<Windpowerstationtestingpoint2> wplist = windpowerstationtestingpoint2Service.list();
        for (Windpowerstationtestingpoint2 windpowerstationtestingpoint2 : wplist) {
            if (wppointmap.containsKey(windpowerstationtestingpoint2.getWindpowerstationid())){
                wppointmap.get(windpowerstationtestingpoint2.getWindpowerstationid()).put(windpowerstationtestingpoint2.getUniformcode(),windpowerstationtestingpoint2);
            }else{
                Map<String,Windpowerstationtestingpoint2> map = new HashMap<>();
                map.put(windpowerstationtestingpoint2.getUniformcode(),windpowerstationtestingpoint2);
                wppointmap.put(windpowerstationtestingpoint2.getWindpowerstationid(),map);
            }
        }



    }
}
