package net.northking.cctp.testbug.feign.server;

import net.northking.cctp.common.exception.PlatformRuntimeException;
import net.northking.cctp.common.http.ResultWrapper;
import net.northking.cctp.platform.db.entity.CpRuleConfig;
import net.northking.cctp.testbug.constants.TestBugError;
import net.northking.cctp.testbug.dto.cases.MtProjectTemplateDetailDto;
import net.northking.cctp.testbug.dto.cases.MtProjectTemplateQueryDto;
import net.northking.cctp.testbug.dto.project.CpProjectDetailDto;
import net.northking.cctp.testbug.dto.project.CpProjectRuleQueryDto;
import net.northking.cctp.testbug.dto.project.RuleConfigValue;
import net.northking.cctp.testbug.dto.project.StatusRoleDto;
import net.northking.cctp.testbug.feign.client.ProjectsFeignClient;
import net.northking.cctp.testbug.feign.client.UserInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: puchao.ma
 * @date: 2021/10/25 15:23
 */
@Configuration
public class ProjectsFeignServer {

    private final static Logger logger = LoggerFactory.getLogger(ProjectsFeignServer.class);

    @Autowired
    private ProjectsFeignClient projectsFeignClient;
    @Autowired
    private RedisTemplate redisTemplate;

    public List<MtProjectTemplateDetailDto> feignPagingQuery(MtProjectTemplateQueryDto queryDto) {

        ResultWrapper<List<MtProjectTemplateDetailDto>> wrapper = projectsFeignClient.feginPagingQuery(queryDto);
        if (wrapper.isSuccess()) {
            return wrapper.getData();
        }
        return new ArrayList<>();
    }



    public List<CpProjectDetailDto> listProject(List<String> projectIds) {
        ResultWrapper<List<CpProjectDetailDto>> wrapper = projectsFeignClient.listProject(projectIds);
        if (wrapper.isSuccess()) {
            return wrapper.getData();
        }
        return new ArrayList<>();
    }

    public RuleConfigValue queryRuleConfig(CpProjectRuleQueryDto ruleQueryDto) {
        RuleConfigValue rule = new RuleConfigValue();
        //缓存中获取当前项目的规则集合id
        String collectionId = (String)redisTemplate.opsForValue().get("UITEST-PROJECT:RULE:COLLECTION:"+ruleQueryDto.getProjectId());
        //如果规则集合id为空 则调用feign
        if (collectionId == null || collectionId.isEmpty()){
            rule = projectsFeignClient.queryRuleConfig(ruleQueryDto).getData();
        }else{
            //获取规则内容
            Object configList = redisTemplate.opsForHash().get("UITEST-PROJECT:rule_collection:"+collectionId,ruleQueryDto.getRuleNo());
            if (configList!=null){
                CpRuleConfig cpRuleConfig = (CpRuleConfig)configList;
                BeanUtils.copyProperties(cpRuleConfig,rule);
            }else {
                rule.setRuleNo(ruleQueryDto.getRuleNo());
                rule.setRuleValue(null);
                rule.setChecked(false);
            }
        }
        return rule;
    }

    public List<String> queryBugEndStatus(StatusRoleDto dto) {
        ResultWrapper<List<String>> wrapper = projectsFeignClient.queryBugEndStatus(dto);

        try {
            if (wrapper.isSuccess() && CollectionUtils.isNotEmpty(wrapper.getData())) {
                return wrapper.getData().stream().distinct().collect(Collectors.toList());
            } else {
                return new ArrayList<>();
            }
        } catch (Exception e) {
            throw new PlatformRuntimeException(TestBugError.GET_BUG_END_STATUS_FAILED, e);
        }
    }

    public List<String> queryProjectIdList() {
        ResultWrapper<List<String>> wrapper = projectsFeignClient.queryProjectIdList();

        if (wrapper.isSuccess()) {
            return wrapper.getData();
        }
        return null;
    }

    public List<String> queryProjectId() {
        List<String> resut = new ArrayList<>();
        ResultWrapper<List<String>> wrapper = projectsFeignClient.queryProjectId();
        if (wrapper.isSuccess()) {
            resut = wrapper.getData();
        }
        return resut;
    }

    public List<String> queryDeletedProjectId() {
        List<String> resut = new ArrayList<>();
        ResultWrapper<List<String>> wrapper = projectsFeignClient.queryDeletedProjectId();
        if (wrapper.isSuccess()) {
            resut = wrapper.getData();
        }
        return resut;
    }

    public List<String> queryBugInitialStatus(StatusRoleDto dto) {
        try {
            List<String> wrapper = projectsFeignClient.queryBugInitialStatus(dto);
            return wrapper;
        } catch (Exception e) {
            throw new PlatformRuntimeException(TestBugError.GET_BUG_END_STATUS_FAILED, e);
        }
    }

    public List<UserInfo> queryAvailableRoles(StatusRoleDto dto) {
        try {
            List<UserInfo> userInfos = projectsFeignClient.queryAvailableRoles(dto);
            return userInfos;
        } catch (Exception e) {
            throw new PlatformRuntimeException(TestBugError.GET_BUG_END_STATUS_FAILED, e);
        }
    }
}
