package com.css.simulation.resource.server.app.service;

import api.common.pojo.param.scene.ScoringRulesParam;
import api.common.pojo.po.scene.ScoringRulesPO;
import api.common.util.LogUtil;
import api.common.util.StringUtil;
import api.common.util.TimeUtil;
import com.css.simulation.resource.server.infra.db.mysql.mapper.ScenePackageMapper;
import com.css.simulation.resource.server.infra.db.mysql.mapper.ScoringRulesMapper;
import com.css.simulation.resource.server.infra.util.AuthUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service
public class ScoringRulesService {

    @Resource
    private ScoringRulesMapper scoringRulesMapper;
    @Resource
    private ScenePackageMapper scenePackageMapper;
    /**
     * 打分规则数据录入：
     */
    @SneakyThrows
    public void saveScoringRules(ScoringRulesPO params) {
        params.setCreateTime(TimeUtil.getNowForMysql());
        params.setModifyTime(TimeUtil.getNowForMysql());
        params.setRulesId(StringUtil.getRandomUUID());
        params.setRulesCode(StringUtil.getRandomCode());
        params.setCreateUserId(AuthUtil.getCurrentUserId());
        params.setIsDeleted("0");
        scoringRulesMapper.saveScoringRules(params);
        LogUtil.insert();
    }

    @SneakyThrows
    public List<ScoringRulesPO> queryScoringRulesListCSB(ScoringRulesParam params) {
        List<ScoringRulesPO> list = new ArrayList<>();
        if (params != null) {
            if (params.getShare() != null && params.getShare().equals("0")) {
                params.setCreateUserId(AuthUtil.getCurrentUserId());
            }
            list = scoringRulesMapper.queryScoringRulesList(params);
            params.setCreateUserId("");
            params.setShare("1");
            List<ScoringRulesPO> list1 = scoringRulesMapper.queryScoringRulesList(params);
            list.addAll(list1);
        }
        //查询共有
        return list;
    }

    @SneakyThrows
    public List<ScoringRulesPO> queryScoringRulesList(ScoringRulesParam params) {
        if (params != null && params.getShare() != null && params.getShare().equals("0")) {
            params.setCreateUserId(AuthUtil.getCurrentUserId());
        }
        return scoringRulesMapper.queryScoringRulesList(params);
    }


    @SneakyThrows
    public ScoringRulesPO queryScoringRules(ScoringRulesParam params) {
        return scoringRulesMapper.queryScoringRules(params);
    }

    @SneakyThrows
    public ScoringRulesPO queryScoringRulesByRuleName(ScoringRulesParam params) {
        return scoringRulesMapper.queryScoringRulesByRuleName(params);
    }

    @SneakyThrows
    public ScoringRulesPO querySelfOnly(String ruleName, String createUserId) {
        return scoringRulesMapper.querySelfOnly(ruleName, createUserId);
    }

    @SneakyThrows
    public void updateScoringRules(ScoringRulesPO params) {
        params.setModifyTime(TimeUtil.getNowForMysql());
        params.setModifyUserId(AuthUtil.getCurrentUserId());
        scoringRulesMapper.updateScoringRules(params);
        LogUtil.update();
    }

    /**
     * 删除打分规则
     *
     * @param params 参数
     */
    @SneakyThrows
    public void deleteScoringRules(ScoringRulesPO params) {
        params.setIsDeleted("1");
        params.setModifyTime(TimeUtil.getNowForMysql());
        params.setModifyUserId(AuthUtil.getCurrentUserId());
        scoringRulesMapper.updateScoringRules(params);
        //同步把使用该评分规则的测试包设为禁用
        scenePackageMapper.updateIsUnavailable3(params);
        LogUtil.delete();
    }

    @SneakyThrows
    public Integer queryCsbById(ScoringRulesParam params) {
        return scoringRulesMapper.queryCsbById(params);
    }

    @SneakyThrows
    public List<Map<String, Object>> queryScoringRulesListCSBNew(ScoringRulesParam params) {
        Optional.ofNullable(params).orElseThrow(() -> new RuntimeException("参数为空。"));
        if (params.getShare() != null && params.getShare().equals("0")) {
            params.setCreateUserId(AuthUtil.getCurrentUserId());
        }
        List<Map<String, Object>> treeList = new ArrayList<>();
        List<ScoringRulesPO> listSy = scoringRulesMapper.queryScoringRulesList(params);

        //查询共有
        params.setCreateUserId("");
        params.setShare("1");
        List<ScoringRulesPO> listGy = scoringRulesMapper.queryScoringRulesList(params);

        Map<String, Object> typeMap = new HashMap<>();
        typeMap.put("code", "公有");
        typeMap.put("name", "公有");
        List<Map<String, Object>> colourList = new LinkedList<>();
        listGy.forEach(vo -> {
            Map<String, Object> colourMap = new HashMap<>();
            colourMap.put("code", vo.getRulesId());
            colourMap.put("name", vo.getRuleName());
            colourMap.put("vo", vo);
            colourList.add(colourMap);
        });
        typeMap.put("children", colourList);
        Map<String, Object> typeMap1 = new HashMap<>();
        typeMap1.put("code", "私有");
        typeMap1.put("name", "私有");
        List<Map<String, Object>> colourList1 = new LinkedList<>();
        listSy.forEach(vo -> {
            Map<String, Object> colourMap = new HashMap<>();
            colourMap.put("code", vo.getRulesId());
            colourMap.put("name", vo.getRuleName());
            colourMap.put("vo", vo);
            colourList1.add(colourMap);
        });
        typeMap1.put("children", colourList1);
        treeList.add(typeMap);
        treeList.add(typeMap1);
        return treeList;
    }

}
