package com.zjty.inspect.task;

import com.zjty.inspect.dao.CoefficientModelDao;
import com.zjty.inspect.dao.RuleCollectionDao;
import com.zjty.inspect.dao.RuleDao;
import com.zjty.inspect.dao.TechnologyDao;
import com.zjty.inspect.entity.CoefficientModel;
import com.zjty.inspect.entity.Rule;
import com.zjty.inspect.entity.RuleCollection;
import com.zjty.inspect.entity.Technology;
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 CoefficientModelDao coefficientModelDao;

    @Autowired
    private RuleDao ruleDao;

    @Autowired
    private TechnologyDao technologyDao;

    @Autowired
    private InspectService inspectService;

    @Autowired
    private RuleCollectionDao ruleCollectionDao;

    @Autowired
    private AnalysisFile analysisFile;

    @Override
    public void run(String... args) {
//        ArrayList<CoefficientModel> coefficientModels = new ArrayList<>();
//        //无，弱，强
//        CoefficientModel coefficientModel = new CoefficientModel();
//        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);
//
//        CoefficientModel coefficientMode2 = new CoefficientModel();
//        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);
//
//        CoefficientModel coefficientMode3 = new CoefficientModel();
//        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);
//
//        CoefficientModel coefficientMode4 = new CoefficientModel();
//        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);
//
//        CoefficientModel coefficientMode5 = new CoefficientModel();
//        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);
//
//        CoefficientModel coefficientMode7 = new CoefficientModel();
//        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);
//        CoefficientModel coefficientMode6 = new CoefficientModel();
//        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);
//
//        CoefficientModel coefficientMode8 = new CoefficientModel();
//        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);
//
//        CoefficientModel coefficientMode9 = new CoefficientModel();
//        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);
//
//        CoefficientModel coefficientMode10 = new CoefficientModel();
//        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);
//
//
//        CoefficientModel coefficientMode11 = new CoefficientModel();
//        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);
//
//        CoefficientModel coefficientMode12 = new CoefficientModel();
//        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);
//
//        CoefficientModel coefficientMode13 = new CoefficientModel();
//        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);
//
//        CoefficientModel coefficientMode14 = new CoefficientModel();
//        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);
//
//        CoefficientModel coefficientMode15 = new CoefficientModel();
//        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);
//
//        Technology JavaTechnology = new Technology();
//        JavaTechnology.setTechnologyName("Java依赖");
//        JavaTechnology.setAdvice("国产化支持的依赖");
//        JavaTechnology.setFund(5);
//        JavaTechnology.setId(UUIDUtil.getUUID());
//        JavaTechnology.setSupport(1);
//
//        Technology NotCnTechnology = new Technology();
//        NotCnTechnology.setTechnologyName("非国产化依赖");
//        NotCnTechnology.setSupport(2);
//        NotCnTechnology.setAdvice("国产化不支持的依赖");
//        NotCnTechnology.setFund(5);
//        NotCnTechnology.setId(UUIDUtil.getUUID());
//
//        Technology UnKnowunTechnology = new Technology();
//        UnKnowunTechnology.setTechnologyName("未知依赖");
//        UnKnowunTechnology.setSupport(3);
//        UnKnowunTechnology.setAdvice("未知的依赖");
//        UnKnowunTechnology.setFund(5);
//        UnKnowunTechnology.setId(UUIDUtil.getUUID());
//
//        Rule NotCnJavaTechnology = new Rule();
//        NotCnJavaTechnology.setId(UUIDUtil.getUUID());
//        NotCnJavaTechnology.setSuffix("*");
//        NotCnJavaTechnology.setTarget("mysql-connect");
//        NotCnJavaTechnology.setTechnologyId(NotCnTechnology.getId());
//
//
//        Rule rule = new Rule();
//        rule.setId(UUIDUtil.getUUID());
//        rule.setSuffix("*");
//        rule.setTarget("mysql-connect");
//        rule.setTechnologyId(UnKnowunTechnology.getId());
//
////        Rule UnknowunMysqlRule = new Rule();
////        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, Rule> map1 = new HashMap<>();

        ArrayList<RuleCollection> ruleCollections = new ArrayList<>();
        List<Rule> all = ruleDao.findAll();
        for (Rule 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()) {
            RuleCollection ruleCollection = new RuleCollection();
            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());
            Rule rule = map1.get(key);
            ruleCollection.setTarget(rule.getTarget());
            ruleCollection.setTechnologyId(rule.getTechnologyId());
            ruleCollection.setTechnologyName(rule.getTechnologyName());
            ruleCollections.add(ruleCollection);
        }
        ruleCollectionDao.saveAll(ruleCollections);
    }
}
