package net.northking.cctp.testbug.api.service;

import net.northking.cctp.common.cache.EntityNameCache;

import net.northking.cctp.platform.db.entity.CpRuleConfig;
import net.northking.cctp.testbug.constants.TestBugConstants;
import net.northking.cctp.testbug.dto.BusinessRuleDto;
import net.northking.cctp.testbug.dto.project.CpProjectRuleQueryDto;
import net.northking.cctp.testbug.dto.project.RuleConfigValue;
import net.northking.cctp.testbug.feign.client.ProjectsFeignClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 业务规则实现类
 */
@Service
@ConditionalOnMissingBean(name = "CltBusinessNoApiServiceImpl")
public class MtBusinessRuleServiceImpl implements MtBusinessRuleService {
    @Autowired
    private ProjectsFeignClient projectsFeignClient;
    @Autowired
    private EntityNameCache entityNameCache;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public BusinessRuleDto getBusinessRuleConfig(BusinessRuleDto inputDto) {
        BusinessRuleDto result = new BusinessRuleDto();
        if(inputDto.getRuleNoList()==null||inputDto.getRuleNoList().size()<1){
            result = getBusinessRuleConfigOne(inputDto);
        }else{
            BusinessRuleDto query = new BusinessRuleDto();
            Map<String,BusinessRuleDto> map = new HashMap<>();
            query.setProjectId(inputDto.getProjectId());
            for (String ruleNo : inputDto.getRuleNoList()) {
                query.setRuleNo(ruleNo);
                map.put(ruleNo,getBusinessRuleConfigOne(query));
            }
            result.setRuleConfigMap(map);
        }
        return result;
    }

    private BusinessRuleDto getBusinessRuleConfigOne(BusinessRuleDto inputDto){
        RuleConfigValue rule = getRuleInfo(inputDto);
        BusinessRuleDto result = new BusinessRuleDto();
        result.setIsValid(TestBugConstants.STR_ZERO);
        if(rule.getChecked()){
            result.setIsValid(TestBugConstants.STR_ONE);
        }
        return result;
    }


    /**
     * 更改为从redis获取项目规则信息
     * @param inputDto
     * @return
     */
    public RuleConfigValue getRuleInfo(BusinessRuleDto inputDto){
        RuleConfigValue rule = new RuleConfigValue();
        CpProjectRuleQueryDto ruleQuery = new CpProjectRuleQueryDto();
        ruleQuery.setRuleNo(inputDto.getRuleNo());
        ruleQuery.setProjectId(inputDto.getProjectId());
        //缓存中获取当前项目的规则集合id
        String collectionId = (String)redisTemplate.opsForValue().get(TestBugConstants.PRO_RULE_COLLECTION +inputDto.getProjectId());
        //如果规则集合id为空 则调用feign
        if (collectionId == null || collectionId.isEmpty()){
            rule = projectsFeignClient.queryRuleConfig(ruleQuery).getData();
        }else{
            //获取规则内容
            Object configList = redisTemplate.opsForHash().get(TestBugConstants.PRO_RULE_MAP+collectionId,inputDto.getRuleNo());
            if (configList!=null){
                CpRuleConfig cpRuleConfig = (CpRuleConfig)configList;
                BeanUtils.copyProperties(cpRuleConfig,rule);
            }else {
                rule.setRuleNo(inputDto.getRuleNo());
                rule.setRuleValue(null);
                rule.setChecked(false);
            }
        }
        return rule;
    }
}
