package com.zd.chemical;

import com.zd.chemical.domain.HxpChemicalClassify;
import com.zd.chemical.domain.HxpClassifyConfig;
import com.zd.chemical.service.IHxpChemicalClassifyService;
import com.zd.chemical.service.IHxpClassifyConfigService;
import com.zd.common.core.utils.StringUtils;
import com.zd.common.security.annotation.EnableCustomConfig;
import com.zd.common.security.annotation.EnableRyFeignClients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@EnableCustomConfig
@EnableRyFeignClients
@SpringBootApplication
public class ZdChemicalApplication implements ApplicationRunner {

    @Autowired
    private IHxpChemicalClassifyService hxpChemicalClassifyService;

    @Autowired
    private IHxpClassifyConfigService hxpClassifyConfigService;

    public static void main(String[] args) {
        SpringApplication.run(ZdChemicalApplication.class, args);
        System.out.println("(♥◠‿◠)ﾉﾞ  化学品管理启动成功   ლ(´ڡ`ლ)ﾞ");
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        HxpChemicalClassify hxpChemicalClassify = new HxpChemicalClassify();
        List <HxpChemicalClassify> hxpChemicalClassifies =  hxpChemicalClassifyService.selectHxpChemicalClassifyList(hxpChemicalClassify);
        Optional.ofNullable(hxpChemicalClassifies).orElseGet(Collections::emptyList).stream()
                .forEach(a->{
                    HxpClassifyConfig hxpClassifyConfigSel = new HxpClassifyConfig();
                    hxpClassifyConfigSel.setJoinClassifyId(Integer.parseInt(a.getId()+""));
                    List<HxpClassifyConfig> classifyConfigList = hxpClassifyConfigService.selectHxpClassifyConfigList(hxpClassifyConfigSel);
                    if(classifyConfigList.size()==0 && a.getHazardLevel()==1){
                        //初始化危险化学品
                        HxpClassifyConfig classifyConfig = new HxpClassifyConfig();
                        classifyConfig.setJoinClassifyId(Integer.parseInt(a.getId()+""));
                        classifyConfig.initDanger();
                        hxpClassifyConfigService.insertHxpClassifyConfig(classifyConfig);
                    }else if(classifyConfigList.size()==0 && a.getHazardLevel()==2){
                        //初始化普通化学品
                        HxpClassifyConfig classifyConfig = new HxpClassifyConfig();
                        classifyConfig.setJoinClassifyId(Integer.parseInt(a.getId()+""));
                        classifyConfig.initOrdinary();
                        hxpClassifyConfigService.insertHxpClassifyConfig(classifyConfig);
                    }
                });
    }
}
