package com.zjty.inspect.task;

import com.zjty.inspect.dao.MasterCoefficientModelDao;
import com.zjty.inspect.dao.MasterRuleCollectionDao;
import com.zjty.inspect.dao.MasterRuleDao;
import com.zjty.inspect.dao.TechnologyDao;
import com.zjty.inspect.entity.MasterRule;
import com.zjty.inspect.entity.MasterRuleCollection;
import com.zjty.inspect.service.InspectService;
import com.zjty.inspect.utils.AnalysisFile;
import com.zjty.inspect.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author Mcj
 * @date 2020-03-02 14:35
 */

@Component
public class task implements CommandLineRunner {

    @Autowired
    private MasterCoefficientModelDao coefficientModelDao;

    @Autowired
    private MasterRuleDao ruleDao;

    @Autowired
    private TechnologyDao technologyDao;

    @Autowired
    private InspectService inspectService;

    @Autowired
    private MasterRuleCollectionDao ruleCollectionDao;

    @Autowired
    private AnalysisFile analysisFile;

    @Override
    public void run(String... args) {
//        ArrayList<MasterCoefficientModel> coefficientModels = new ArrayList<>();
//        //无，弱，强
//        MasterCoefficientModel coefficientModel = new MasterCoefficientModel();
//        coefficientModel.setScale(1);
//        coefficientModel.setId(UUIDUtil.getUUID());
//        coefficientModel.setName("安全能力");
//        coefficientModel.setMin(1);
//        coefficientModel.setMax(3);
//        coefficientModel.setBelowCoefficient(0D);
//        coefficientModel.setMediumCoefficient(0.1);
//        coefficientModel.setTopCoefficient(0.2);
//
//        MasterCoefficientModel coefficientMode2 = new MasterCoefficientModel();
//        coefficientMode2.setScale(2);
//        coefficientMode2.setId(UUIDUtil.getUUID());
//        coefficientMode2.setName("安全能力");
//        coefficientMode2.setMin(1);
//        coefficientMode2.setMax(3);
//        coefficientMode2.setBelowCoefficient(-0.1);
//        coefficientMode2.setMediumCoefficient(0D);
//        coefficientMode2.setTopCoefficient(0.1);
//
//        MasterCoefficientModel coefficientMode3 = new MasterCoefficientModel();
//        coefficientMode3.setScale(3);
//        coefficientMode3.setId(UUIDUtil.getUUID());
//        coefficientMode3.setName("安全能力");
//        coefficientMode3.setMin(1);
//        coefficientMode3.setMax(3);
//        coefficientMode3.setBelowCoefficient(-0.2);
//        coefficientMode3.setMediumCoefficient(-0.1);
//        coefficientMode3.setTopCoefficient(0D);
//
//        MasterCoefficientModel coefficientMode4 = new MasterCoefficientModel();
//        coefficientMode4.setScale(1);
//        coefficientMode4.setId(UUIDUtil.getUUID());
//        coefficientMode4.setName("容灾能力");
//        coefficientMode4.setMin(1);
//        coefficientMode4.setMax(2);
//        coefficientMode4.setBelowCoefficient(0D);
//        coefficientMode4.setMediumCoefficient(0D);
//        coefficientMode4.setTopCoefficient(0.05);
//
//        MasterCoefficientModel coefficientMode5 = new MasterCoefficientModel();
//        coefficientMode5.setScale(2);
//        coefficientMode5.setId(UUIDUtil.getUUID());
//        coefficientMode5.setName("容灾能力");
//        coefficientMode5.setMin(1);
//        coefficientMode5.setMax(3);
//        coefficientMode5.setBelowCoefficient(0.05);
//        coefficientMode5.setMediumCoefficient(0D);
//        coefficientMode5.setTopCoefficient(0D);
//
//        MasterCoefficientModel coefficientMode7 = new MasterCoefficientModel();
//        coefficientMode7.setScale(3);
//        coefficientMode7.setId(UUIDUtil.getUUID());
//        coefficientMode7.setName("容灾能力");
//        coefficientMode7.setMin(1);
//        coefficientMode7.setMax(3);
//        coefficientMode7.setBelowCoefficient(-0.1);
//        coefficientMode7.setMediumCoefficient(0D);
//        coefficientMode7.setTopCoefficient(0D);
//        MasterCoefficientModel coefficientMode6 = new MasterCoefficientModel();
//        coefficientMode6.setScale(3);
//        coefficientMode6.setId(UUIDUtil.getUUID());
//        coefficientMode6.setName("模块数");
//        coefficientMode6.setMin(4);
//        coefficientMode6.setMax(10);
//        coefficientMode6.setBelowCoefficient(-0.3);
//        coefficientMode6.setMediumCoefficient(0D);
//        coefficientMode6.setTopCoefficient(0.3);
//
//        MasterCoefficientModel coefficientMode8 = new MasterCoefficientModel();
//        coefficientMode8.setScale(2);
//        coefficientMode8.setId(UUIDUtil.getUUID());
//        coefficientMode8.setName("模块数");
//        coefficientMode8.setMin(4);
//        coefficientMode8.setMax(10);
//        coefficientMode8.setBelowCoefficient(-0.3);
//        coefficientMode8.setMediumCoefficient(0D);
//        coefficientMode8.setTopCoefficient(0.3);
//
//        MasterCoefficientModel coefficientMode9 = new MasterCoefficientModel();
//        coefficientMode9.setScale(1);
//        coefficientMode9.setId(UUIDUtil.getUUID());
//        coefficientMode9.setName("模块数");
//        coefficientMode9.setMin(4);
//        coefficientMode9.setMax(10);
//        coefficientMode9.setBelowCoefficient(-0.3);
//        coefficientMode9.setMediumCoefficient(0D);
//        coefficientMode9.setTopCoefficient(0.3);
//
//        MasterCoefficientModel coefficientMode10 = new MasterCoefficientModel();
//        coefficientMode10.setScale(1);
//        coefficientMode10.setId(UUIDUtil.getUUID());
//        coefficientMode10.setName("数据量");
//        coefficientMode10.setMin(1);
//        coefficientMode10.setMax(100);
//        coefficientMode10.setBelowCoefficient(-0.05);
//        coefficientMode10.setMediumCoefficient(0D);
//        coefficientMode10.setTopCoefficient(0.05);
//
//
//        MasterCoefficientModel coefficientMode11 = new MasterCoefficientModel();
//        coefficientMode11.setScale(2);
//        coefficientMode11.setId(UUIDUtil.getUUID());
//        coefficientMode11.setName("数据量");
//        coefficientMode11.setMin(1);
//        coefficientMode11.setMax(100);
//        coefficientMode11.setBelowCoefficient(-0.05);
//        coefficientMode11.setMediumCoefficient(0D);
//        coefficientMode11.setTopCoefficient(0.05);
//
//        MasterCoefficientModel coefficientMode12 = new MasterCoefficientModel();
//        coefficientMode12.setScale(3);
//        coefficientMode12.setId(UUIDUtil.getUUID());
//        coefficientMode12.setName("数据量");
//        coefficientMode12.setMin(1);
//        coefficientMode12.setMax(100);
//        coefficientMode12.setBelowCoefficient(-0.05);
//        coefficientMode12.setMediumCoefficient(0D);
//        coefficientMode12.setTopCoefficient(0.05);
//
//        MasterCoefficientModel coefficientMode13 = new MasterCoefficientModel();
//        coefficientMode13.setScale(1);
//        coefficientMode13.setId(UUIDUtil.getUUID());
//        coefficientMode13.setName("数据库表");
//        coefficientMode13.setMin(10);
//        coefficientMode13.setMax(100);
//        coefficientMode13.setBelowCoefficient(-0.05);
//        coefficientMode13.setMediumCoefficient(0D);
//        coefficientMode13.setTopCoefficient(0.05);
//
//        MasterCoefficientModel coefficientMode14 = new MasterCoefficientModel();
//        coefficientMode14.setScale(2);
//        coefficientMode14.setId(UUIDUtil.getUUID());
//        coefficientMode14.setName("数据库表");
//        coefficientMode14.setMin(10);
//        coefficientMode14.setMax(100);
//        coefficientMode14.setBelowCoefficient(-0.05);
//        coefficientMode14.setMediumCoefficient(0D);
//        coefficientMode14.setTopCoefficient(0.05);
//
//        MasterCoefficientModel coefficientMode15 = new MasterCoefficientModel();
//        coefficientMode15.setScale(3);
//        coefficientMode15.setId(UUIDUtil.getUUID());
//        coefficientMode15.setName("数据库表");
//        coefficientMode15.setMin(10);
//        coefficientMode15.setMax(100);
//        coefficientMode15.setBelowCoefficient(-0.05);
//        coefficientMode15.setMediumCoefficient(0D);
//        coefficientMode15.setTopCoefficient(0.05);
//
//        coefficientModels.add(coefficientMode6);
//        coefficientModels.add(coefficientMode5);
//        coefficientModels.add(coefficientMode4);
//        coefficientModels.add(coefficientMode3);
//        coefficientModels.add(coefficientMode2);
//        coefficientModels.add(coefficientModel);
//        coefficientModels.add(coefficientMode7);
//        coefficientModels.add(coefficientMode8);
//        coefficientModels.add(coefficientMode9);
//        coefficientModels.add(coefficientMode10);
//        coefficientModels.add(coefficientMode11);
//        coefficientModels.add(coefficientMode12);
//        coefficientModels.add(coefficientMode13);
//        coefficientModels.add(coefficientMode14);
//        coefficientModels.add(coefficientMode15);
//        coefficientModelDao.saveAll(coefficientModels);
//
//        MasterTechnology JavaTechnology = new MasterTechnology();
//        JavaTechnology.setTechnologyName("Java依赖");
//        JavaTechnology.setAdvice("国产化支持的依赖");
//        JavaTechnology.setFund(5);
//        JavaTechnology.setId(UUIDUtil.getUUID());
//        JavaTechnology.setSupport(1);
//
//        MasterTechnology NotCnTechnology = new MasterTechnology();
//        NotCnTechnology.setTechnologyName("非国产化依赖");
//        NotCnTechnology.setSupport(2);
//        NotCnTechnology.setAdvice("国产化不支持的依赖");
//        NotCnTechnology.setFund(5);
//        NotCnTechnology.setId(UUIDUtil.getUUID());
//
//        MasterTechnology UnKnowunTechnology = new MasterTechnology();
//        UnKnowunTechnology.setTechnologyName("未知依赖");
//        UnKnowunTechnology.setSupport(3);
//        UnKnowunTechnology.setAdvice("未知的依赖");
//        UnKnowunTechnology.setFund(5);
//        UnKnowunTechnology.setId(UUIDUtil.getUUID());
//
//        MasterRule NotCnJavaTechnology = new MasterRule();
//        NotCnJavaTechnology.setId(UUIDUtil.getUUID());
//        NotCnJavaTechnology.setSuffix("*");
//        NotCnJavaTechnology.setTarget("mysql-connect");
//        NotCnJavaTechnology.setTechnologyId(NotCnTechnology.getId());
//
//
//        MasterRule rule = new MasterRule();
//        rule.setId(UUIDUtil.getUUID());
//        rule.setSuffix("*");
//        rule.setTarget("mysql-connect");
//        rule.setTechnologyId(UnKnowunTechnology.getId());
//
////        MasterRule UnknowunMysqlRule = new MasterRule();
////        UnknowunMysqlRule.setId(UUIDUtil.getUUID());
////        UnknowunMysqlRule.setSuffix("*");
////        UnknowunMysqlRule.setTarget("mysql-connect");
////        UnknowunMysqlRule.setTechnologyId(UnKnowunTechnology.getId());
//
//        technologyDao.save(JavaTechnology);
//        technologyDao.save(NotCnTechnology);
//        technologyDao.save(UnKnowunTechnology);
//        ruleDao.save(rule);
//        ruleDao.save(NotCnJavaTechnology);
       // ruleDao.save(UnknowunMysqlRule);

        //文件后缀map
        if(ruleCollectionDao.count()!=0){
            return;
        }
        HashMap<String, ArrayList<String>> map = new HashMap<>();
        //
        HashMap<String, MasterRule> map1 = new HashMap<>();

        ArrayList<MasterRuleCollection> ruleCollections = new ArrayList<>();
        List<MasterRule> all = ruleDao.findAll();
        for (MasterRule rule : all) {
            if(!map.containsKey(rule.getTarget()+":"+rule.getTechnologyId())){
                ArrayList<String> suffixes = new ArrayList<>();
                suffixes.add(rule.getSuffix());
                map.put(rule.getTarget()+":"+rule.getTechnologyId(),suffixes);
            }else{
                map.get(rule.getTarget()+":"+rule.getTechnologyId()).add(rule.getSuffix());
            }
            map1.put(rule.getTarget()+":"+rule.getTechnologyId(),rule);
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (String key : map.keySet()) {
            MasterRuleCollection ruleCollection = new MasterRuleCollection();
            ruleCollection.setId(UUIDUtil.getUUID());
            ArrayList<String> suffixes = map.get(key);
            for (String suffix : suffixes) {
                if(stringBuilder.length()==0){
                    stringBuilder.append(suffix);
                }else{
                    stringBuilder.append(",").append(suffix);
                }
            }
            ruleCollection.setSuffix(stringBuilder.toString());
            stringBuilder.delete(0,stringBuilder.length());
            MasterRule rule = map1.get(key);
            ruleCollection.setTarget(rule.getTarget());
            ruleCollection.setTechnologyId(rule.getTechnologyId());
            ruleCollection.setTechnologyName(rule.getTechnologyName());
            ruleCollections.add(ruleCollection);
        }
        ruleCollectionDao.saveAll(ruleCollections);
    }
}
