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

import net.northking.cctp.common.exception.PlatformRuntimeException;
import net.northking.cctp.dw.constant.DWError;
import net.northking.cctp.dw.db.entity.*;
import net.northking.cctp.dw.db.service.*;
import net.northking.cctp.dw.dto.ManualExecJobPieDto;
import net.northking.cctp.dw.dto.MidManualExecJobDetailDto;
import net.northking.cctp.dw.utils.DataDealUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author: puchao.ma
 * @date: 2021/12/15 10:18
 */
@Service
public class MtManualExecJobApiServiceImpl implements MtManualExecJobApiService {

    @Autowired
    private MidManualExecJobService midManualExecJobService;

    @Autowired
    private MtCaseExecPassRatioService mtCaseExecPassRatioService;

    @Autowired
    private MidValidExecJobService midValidExecJobService;

    @Autowired
    private MtCaseExecOverallService caseExecOverallService;

    @Autowired
    private MtTestChartService testChartService;

    @Override
    public void deleteAllMid(List<MidProjectInfo> midProjectInfos) {
        midManualExecJobService.deleteByData(midProjectInfos);
    }

    @Override
    public void addBatchMid(List<MidManualExecJob> execJobs) {
        midManualExecJobService.insertByBatch(execJobs);
    }

    /**
     * 用例执行饼图
     *
     * @param projectId
     * @return
     */
    @Override
    public ManualExecJobPieDto execJobPie(String projectId) {
        ManualExecJobPieDto pieDto = new ManualExecJobPieDto();
        List<MidManualExecJobDetailDto> execJobPie = null;
        try {
            execJobPie = midManualExecJobService.execJobPie(projectId);
        } catch (Exception e) {
            throw new PlatformRuntimeException(DWError.QUERY_MANUAL_EXEC_PIE_FAILED, e);
        }

        if (CollectionUtils.isNotEmpty(execJobPie) && Objects.nonNull(execJobPie.get(0).getPlanAmount())) {

            int nonExec = execJobPie.stream().mapToInt(MidManualExecJobDetailDto::getNonExec).sum();
            int invalid = execJobPie.stream().mapToInt(MidManualExecJobDetailDto::getInvalid).sum();
            int fail = execJobPie.stream().mapToInt(MidManualExecJobDetailDto::getFail).sum();
            int blocked = execJobPie.stream().mapToInt(MidManualExecJobDetailDto::getBlocked).sum();
            int hangUp = execJobPie.stream().mapToInt(MidManualExecJobDetailDto::getHangUp).sum();
            int unfinished = execJobPie.stream().mapToInt(MidManualExecJobDetailDto::getUnfinished).sum();
            int pass = execJobPie.stream().mapToInt(MidManualExecJobDetailDto::getPass).sum();
            int total = execJobPie.stream().mapToInt(MidManualExecJobDetailDto::getTotal).sum();
            Integer planAmount = execJobPie.get(0).getPlanAmount();
            total = total - nonExec;

            if (total != 0) {
                pieDto.setInvalid(invalid);
                pieDto.setFail(fail);
                pieDto.setBlocked(blocked);
                pieDto.setHangUp(hangUp);
                pieDto.setUnfinished(unfinished);
                pieDto.setPass(pass);
                //  计算比例
                pieDto.setInvalidRatio(DataDealUtil.calculateFour(invalid, total));
                pieDto.setFailRatio(DataDealUtil.calculateFour(fail, total));
                pieDto.setBlockedRatio(DataDealUtil.calculateFour(blocked, total));
                pieDto.setHangUpRatio(DataDealUtil.calculateFour(hangUp, total));
                pieDto.setUnfinishedRatio(DataDealUtil.calculateFour(unfinished, total));
                pieDto.setPassRatio(DataDealUtil.calculateFour(pass, total));

                double v = DataDealUtil.calculateTwo((pass + fail) * 100, planAmount);
                pieDto.setPlanCompleteRatio(v + "%");
            } else {
                pieDto.setInvalid(0);
                pieDto.setFail(0);
                pieDto.setBlocked(0);
                pieDto.setHangUp(0);
                pieDto.setUnfinished(0);
                pieDto.setPass(0);
                //  计算比例
                pieDto.setInvalidRatio(0.0);
                pieDto.setFailRatio(0.0);
                pieDto.setBlockedRatio(0.0);
                pieDto.setHangUpRatio(0.0);
                pieDto.setUnfinishedRatio(0.0);
                pieDto.setPassRatio(0.0);
                pieDto.setPlanCompleteRatio("0.0%");
            }
        }
        return pieDto;
    }

    /**
     * 生成用例执行进度数据
     *
     * @return
     */
    @Override
    public List<MtTestChart> createExecJobProgress(String tenantId) {
        return midManualExecJobService.createExecJobProgress(tenantId);
    }

    /**
     * 清空执行通过率数据
     */
    @Override
    public void deleteAllExecPassRatioData(MtCaseExecPassRatio ratio ) {
        mtCaseExecPassRatioService.deleteByExample(ratio);
    }

    /**
     * 新增执行铜绿数据
     *
     * @param ratios
     */
    @Override
    public void addBatchExecPassRatio(List<MtCaseExecPassRatio> ratios) {
        mtCaseExecPassRatioService.insertByBatch(ratios);
    }

    /**
     * 清空用户有效执行用例数中间表
     */
    @Override
    public void deleteAllMidValid(List<MidProjectInfo> dataList, String tenantId) {
        midValidExecJobService.deleteByData(dataList,tenantId);
    }

    /**
     * 新增用户有效执行用例数中间表
     *
     * @param ratios
     */
    @Override
    public void addBatchMidValid(List<MidValidExecJob> ratios) {
        midValidExecJobService.insertByBatch(ratios);
    }

    /**
     * 查询有效用例数据
     *
     * @return
     */
    @Override
    public List<MidValidExecJob> queryValidExecJob() {
        return midValidExecJobService.findAll();
    }

    /**
     * 删除案例执行总体情况
     */
    @Override
    public void deleteAllOverall(MtCaseExecOverall overall) {
        caseExecOverallService.deleteByExample(overall);
    }

    /**
     * 新增案例执行总体情况
     *
     * @param mtCaseExecOveralls
     */
    @Override
    public void addOveralls(List<MtCaseExecOverall> mtCaseExecOveralls) {
        caseExecOverallService.insertByBatch(mtCaseExecOveralls);
    }

    @Override
    public List<MidManualExecJob> findAll(List<MidProjectInfo> midProjectInfos) {
        return midManualExecJobService.findByTenantId(midProjectInfos);
    }

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

    @Override
    public Integer caseExecPassRatio(DwManualExecJob dwManualExecJob) {
        return mtCaseExecPassRatioService.caseExecPassRatio(dwManualExecJob);
    }

    @Override
    public Integer validExecJob(List<MidProjectInfo> dataList, String tenantId) {
        return midValidExecJobService.validExecJob(dataList,tenantId);
    }

    @Override
    public Integer caseExecOverall(DwManualExecJob dwManualExecJob) {
        return caseExecOverallService.caseExecOverall(dwManualExecJob);
    }

    @Override
    public List<DwManualExecJob> findProIdList() {
        return midManualExecJobService.findProIdList();
    }

    @Override
    public Integer deleteAllExecPassRatioDataByProjectIds(MtCaseExecPassRatio ratio) {
        return mtCaseExecPassRatioService.deleteAllExecPassRatioDataByProjectIds(ratio);
    }

    @Override
    public Integer deleteAllOverallByProjectIds(MtCaseExecOverall overall) {
        return caseExecOverallService.deleteAllOverallByProjectIds(overall);
    }
}
