/*
 * Copyright (c) 京北方信息技术股份有限公司 Corporation 2021 . All rights reserved.
 *
 */
package net.northking.cctp.dw.db.impl;

import net.northking.cctp.common.cache.EntityNameCache;
import net.northking.cctp.common.db.BasicDao;
import net.northking.cctp.common.db.PaginationService;
import net.northking.cctp.dw.db.dao.MidCaseDesignDao;
import net.northking.cctp.dw.db.dao.MidTestPointDao;
import net.northking.cctp.dw.db.dao.SynBugInfoDao;
import net.northking.cctp.dw.db.entity.*;
import net.northking.cctp.dw.db.service.MidCaseDesignService;
import net.northking.cctp.dw.db.service.MidManualExecJobService;
import net.northking.cctp.dw.dto.MidCaseDesignDetailDto;
import net.northking.cctp.dw.dto.ProjectDataVo;
import net.northking.cctp.dw.feign.ManualTestFeignClient;
import net.northking.cctp.dw.feign.PlatformFeignClient;
import net.northking.cctp.dw.feign.ProjectsFeignClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 按设计统计数据中间表
 *
 * <p>文件内容由代码生成器产生，请不要手动修改！ <br>
 * createdate:  2021-12-15 09:48:17  <br>
 *
 * @author: maven-cctp-plugin  <br>
 * @since: 1.0 <br>
 */
@Service
@ConditionalOnMissingBean(name = "MidCaseDesignServiceImpl")
public class MidCaseDesignServiceImpl extends PaginationService<MidCaseDesign> implements MidCaseDesignService {
    private static final Logger logger = LoggerFactory.getLogger(MidCaseDesignServiceImpl.class);

    private final MidCaseDesignDao midCaseDesignDao;
    @Autowired
    private EntityNameCache entityNameCache;
    @Autowired
    private ProjectsFeignClient projectsFeignClient;
    @Autowired
    private PlatformFeignClient platformFeignClient;
    @Autowired
    private MidTestPointDao midTestPointDao;
    @Autowired
    private ManualTestFeignClient manualTestFeignClient;
    @Autowired
    private SynBugInfoDao synBugInfoDao;
    @Autowired
    private MidManualExecJobService midManualExecJobService;

    public MidCaseDesignServiceImpl(MidCaseDesignDao midCaseDesignDao) {
        this.midCaseDesignDao = midCaseDesignDao;
    }

    public BasicDao<MidCaseDesign> getDao() {
        return midCaseDesignDao;
    }

    public MidCaseDesign createEntity() {
        return new MidCaseDesign();
    }

    @Override
    protected void beforeInsert(MidCaseDesign midCaseDesign) {

    }

    @Override
    protected void beforeUpdate(MidCaseDesign midCaseDesign) {

    }

    @Override
    public List<MtTestChart> createCaseDesignProgress(String tenantId) {
        return midCaseDesignDao.createCaseDesignProgress(tenantId);
    }

    @Override
    public List<MidCaseDesignDetailDto> caseDesignPie(String projectId) {
        return midCaseDesignDao.caseDesignPie(projectId);
    }

    @Override
    public Integer countCaseDesignInfoData(List<MidProjectInfo> midProjectInfos,String tenantId) {
        return midCaseDesignDao.countCaseDesignInfoData(midProjectInfos,tenantId);
    }

    @Override
    public List<MidCaseDesign> findByTenantId(List<MidProjectInfo> midProjectInfos) {
        return midCaseDesignDao.findByTenantId(midProjectInfos);
    }

    @Override
    public Integer deleteByData(List<MidProjectInfo> midProjectInfos) {
        return midCaseDesignDao.deleteByData(midProjectInfos);
    }

    @Override
    public List<ProjectDataVo> getTestPointAmountAndCaseDesignAmountByProjectId(String tenantId,String projectId) {
        // manual-test feign调用获取当前租户与用例关联的测试点id
        List<String> testPointIds = manualTestFeignClient.getTestPointIds(tenantId);
        if (null==testPointIds||testPointIds.size()==0) {
            return new ArrayList<>();
        }
        List<ProjectDataVo> projectDataVos=midCaseDesignDao.getTestPointAmountAndCaseDesignAmountByProjectId(testPointIds,projectId);
        if (null==projectDataVos||projectDataVos.size()==0)return new ArrayList<>();
        return projectDataVos;
    }

    @Override
    public ProjectDataVo getTestPointDesignRateByProjectId(String projectId,String tenantId) {
        //调platformFeign获取租户下关联的测试角色id集合
        List<String> testRoleIds = platformFeignClient.getRoleAmount(tenantId);
        if (null!=testRoleIds&&testRoleIds.size()!=0) {
            // 调projectFeign获取系统关联测试角色人员个数
            List<String> projectIds=new ArrayList<>();
            projectIds.add(projectId);
            Map<String,Integer> roleAmount = projectsFeignClient.getRoles(projectIds,testRoleIds.get(0));
            //计算结果
            if (null!=roleAmount&&null!=roleAmount.get(projectId)&&roleAmount.get(projectId)>0) {
                ProjectDataVo result = midCaseDesignDao.getTestPointDesignRateByProjectId(projectId, roleAmount.get(projectId));
                ProjectDataVo tmp = midCaseDesignDao.getCaseDesignRateByProjectId(projectId, roleAmount.get(projectId));
                if (null!=result) {
                    result.setCaseDesignRate(tmp.getCaseDesignRate());
                    return result;
                }
                return tmp;
            }
        }
        return null;
    }

    @Override
    public ProjectDataVo getTestPointDesignRateByTenantId(String tenantId,List<String> projectIds) {
         //调platformFeign获取租户下关联的测试角色id集合
        List<String> testRoleIds = platformFeignClient.getRoleAmount(tenantId);
        if (null!=testRoleIds&&testRoleIds.size()!=0) {
            // 调projectFeign获取系统关联测试角色人员个数
            Map<String,Integer> roleAmount = projectsFeignClient.getRoles(projectIds,testRoleIds.get(0));
            //计算结果
            if (null!=roleAmount) {
                Double testPointRate=0.0;
                ProjectDataVo projectDataVo=new ProjectDataVo();
                for (String projectId:projectIds) {
                    if (null != roleAmount.get(projectId) && roleAmount.get(projectId) > 0){
                        ProjectDataVo result = midCaseDesignDao.getTestPointDesignRateByProjectId(projectId, roleAmount.get(projectId));
                        if (null!=result&&result.getTestPointDesignRate()!=null) {
                            testPointRate += result.getTestPointDesignRate();
                        }
                    }
                }
                projectDataVo.setTestPointDesignRate(testPointRate/projectIds.size());
                return projectDataVo;
            }
        }
        return null;
    }

    @Override
    public ProjectDataVo getCaseDesignRateByTenantId(String tenantId,List<String> projectIds) {
         //调platformFeign获取租户下关联的测试角色id集合
        List<String> testRoleIds = platformFeignClient.getRoleAmount(tenantId);
        if (null!=testRoleIds&&testRoleIds.size()!=0) {
            // 调projectFeign获取系统关联测试角色人员个数
            Map<String,Integer> roleAmount = projectsFeignClient.getRoles(projectIds,testRoleIds.get(0));
            //计算结果
            if (null!=roleAmount) {
                Double caseDesignRate=0.0;
                ProjectDataVo projectDataVo=new ProjectDataVo();
                for (String projectId:projectIds) {
                    if (null != roleAmount.get(projectId) && roleAmount.get(projectId) > 0){
                    ProjectDataVo tmp = midCaseDesignDao.getCaseDesignRateByProjectId(projectId, roleAmount.get(projectId));
                    if (null!=tmp&&tmp.getCaseDesignRate()!=null) {
                        caseDesignRate += tmp.getCaseDesignRate();
                        }
                    }
                }
                projectDataVo.setCaseDesignRate(caseDesignRate/projectIds.size());
                return projectDataVo;
            }
        }
        return null;
    }

    @Override
    public ProjectDataVo getBugEscapeRateByTenantId(String tenantId,List<String> projectIds) {
        //调project的feign获取需求池中类型为缺陷修复率需求"3"
        Integer cpDemandAmount = projectsFeignClient.cpDemandPlanAmount(tenantId);
        //查租户下的所有缺陷数
        Long l = synBugInfoDao.count(tenantId,projectIds);
        Integer bugNum = new Integer(l.toString());
        //
        ProjectDataVo projectDataVo=new ProjectDataVo();
        projectDataVo.setBugEscapeRate( (cpDemandAmount/((double)bugNum)));
        return projectDataVo;
    }

    @Override
    public ProjectDataVo getData(String tenantId,List<String> projectIds) {
        ProjectDataVo projectDataVo=new ProjectDataVo();
        ProjectDataVo tmp1 = midManualExecJobService.getCaseBugFindRateByTenantId(tenantId,projectIds);//用例缺陷发现率
        // 调project的feign获取当前租户下未被删除且是叶子节点的系统
        ProjectDataVo tmp2 = getTestPointDesignRateByTenantId(tenantId,projectIds);//人均测试点设计率
        ProjectDataVo tmp3 = getCaseDesignRateByTenantId(tenantId,projectIds);//人均用例设计率
        ProjectDataVo tmp4 = getBugEscapeRateByTenantId(tenantId,projectIds);//缺陷逃逸率
        if (null!=tmp1&&null!=tmp1.getCaseBugFindRate()){
            projectDataVo.setCaseBugFindRate(keep2Bit(tmp1.getCaseBugFindRate()));

        }else {
            projectDataVo.setCaseBugFindRate(0.0);
        }
        if (null!=tmp2&&null!=tmp2.getTestPointDesignRate()){
            projectDataVo.setTestPointDesignRate(keep2Bit(tmp2.getTestPointDesignRate()));
        }else {
            projectDataVo.setTestPointDesignRate(0.0);
        }
        if (null!=tmp3&&null!=tmp3.getCaseDesignRate()){
            projectDataVo.setCaseDesignRate(keep2Bit(tmp3.getCaseDesignRate()));
        }else {
            projectDataVo.setCaseDesignRate(0.0);
        }
        if (null!=tmp4&&null!=tmp4.getBugEscapeRate()){
            projectDataVo.setBugEscapeRate(keep2Bit(tmp4.getBugEscapeRate()));
        }else {
            projectDataVo.setBugEscapeRate(0.0);
        }
        return projectDataVo;
    }

    private Double keep2Bit(Double data) {
        BigDecimal bigDecimal=new BigDecimal(data);
        return bigDecimal.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    @Override
    public ProjectDataVo getProjectEffData(String projectId,String tenantId) {
        ProjectDataVo projectDataVo=new ProjectDataVo();
        ProjectDataVo tmp1 = midManualExecJobService.getCaseBugFindRateByProjectId(projectId);
        ProjectDataVo tmp2 = getTestPointDesignRateByProjectId(projectId,tenantId);
        if (null!=tmp1&&null!=tmp1.getCaseBugFindRate()){
            projectDataVo.setCaseBugFindRate(keep2Bit(tmp1.getCaseBugFindRate()));
        }else {
            projectDataVo.setCaseBugFindRate(0.0);
        }
        if (null!=tmp2){
            if (null!=tmp2.getTestPointDesignRate()){
                projectDataVo.setTestPointDesignRate(keep2Bit(tmp2.getTestPointDesignRate()));
            }else {
                projectDataVo.setTestPointDesignRate(0.0);
            }
            if (null!=tmp2.getCaseDesignRate()){
                projectDataVo.setCaseDesignRate(keep2Bit(tmp2.getCaseDesignRate()));
            }else {
                projectDataVo.setCaseDesignRate(0.0);
            }
        }else {
            projectDataVo.setTestPointDesignRate(0.0);
            projectDataVo.setCaseDesignRate(0.0);
        }
        return projectDataVo;
    }

}

