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

import net.northking.cctp.common.cache.EntityNameCache;
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.CaseDesignPieDto;
import net.northking.cctp.dw.dto.DwCaseDesignInfoDto;
import net.northking.cctp.dw.dto.MidCaseDesignDetailDto;
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 org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * @author: puchao.ma
 * @date: 2021/12/15 9:35
 */
@Service
public class MtCaseDesignApiServiceImpl implements MtCaseDesignApiService {

    @Autowired
    private MidCaseDesignService midCaseDesignService;

    @Autowired
    private DwCaseDesignInfoService dwCaseDesignInfoService;

    @Autowired
    private CaseDesignCompleteDetailsService caseDesignCompleteDetailsService;

    @Autowired
    private EntityNameCache entityNameCache;

    @Autowired
    private MtTestChartService testChartService;

    @Autowired
    private MidTestPointService midTestPointService;

    @Override
    @Transactional
    public void deleteAllMid() {
        MidCaseDesign caseDesign = new MidCaseDesign();
        midCaseDesignService.deleteByExample(caseDesign);
    }

    @Override
    @Transactional
    public void addBatchMid(List<MidCaseDesign> midCaseDesigns) {
        midCaseDesignService.insertByBatch(midCaseDesigns);
    }

    @Override
    @Transactional
    public void deleteAllComplete() {
        CaseDesignCompleteDetails details = new CaseDesignCompleteDetails();
        caseDesignCompleteDetailsService.deleteByExample(details);
    }

    @Override
    @Transactional
    public void addBatchComplete(List<CaseDesignCompleteDetails> completeDetails) {
        caseDesignCompleteDetailsService.insertByBatch(completeDetails);
    }

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

    /**
     * 案例饼图
     *
     * @param projectId
     * @return
     */
    @Override
    @Transactional
    public CaseDesignPieDto caseDesignPie(String projectId) {
        CaseDesignPieDto pieDto = new CaseDesignPieDto();
        List<MidCaseDesignDetailDto> designPie = null;
        try {
            designPie = midCaseDesignService.caseDesignPie(projectId);
        } catch (Exception e) {
            throw new PlatformRuntimeException(DWError.QUERY_CASE_DESIGN_PIE_FAILED, e);
        }

        if (CollectionUtils.isNotEmpty(designPie) && Objects.nonNull(designPie.get(0).getPlanAmount())) {
            int completed = designPie.stream().mapToInt(MidCaseDesign::getCompleted).sum();
            int designing = designPie.stream().mapToInt(MidCaseDesign::getDesigning).sum();
            int total = designPie.stream().mapToInt(MidCaseDesign::getTotal).sum();

            Integer planAmount = designPie.get(0).getPlanAmount();
            int uncompleted = Math.max(planAmount - total, 0);

            if (total != 0) {
                pieDto.setCompleted(completed);
                pieDto.setDesigning(designing);
                pieDto.setUncompleted(uncompleted);
                pieDto.setCompletedRatio(DataDealUtil.calculateFour(completed, Math.max(planAmount, total)));
                pieDto.setDesigningRatio(DataDealUtil.calculateFour(designing, Math.max(planAmount, total)));
                pieDto.setUncompletedRatio(DataDealUtil.calculateFour(uncompleted, planAmount));
            } else {
                pieDto.setCompleted(0);
                pieDto.setCompletedRatio(0.0);
                pieDto.setDesigning(0);
                pieDto.setDesigningRatio(0.0);
                pieDto.setUncompleted(planAmount);
                pieDto.setUncompletedRatio(100.0);
            }

            double v = DataDealUtil.calculateTwo(completed * 100, planAmount);
            pieDto.setPlanCompletedRatio(((v >= 100 ? 100 : v) > 0 ? v : 0) + "%");
        }
        return pieDto;
    }

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

    /**
     * 计算比例
     *
     * @param sum
     * @param total
     * @return
     */
    private double calculate(int sum, int total) {
        BigDecimal decimal = BigDecimal.valueOf((double) sum / total);
        return decimal.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    @Override
    public Integer caseDesignComplete(DwCaseDesignInfoDto item) {
        return caseDesignCompleteDetailsService.caseDesignComplete(item);
    }

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

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

    @Override
    public List<DwCaseDesignInfoDto> getProjectId() {
        return dwCaseDesignInfoService.getProjectId();
    }

    @Override
    public Integer deleteCompleteByProjectId(String projectId) {
        CaseDesignCompleteDetails example = new CaseDesignCompleteDetails();
        example.setProjectId(projectId);
        return caseDesignCompleteDetailsService.deleteByExample(example);
    }

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

    @Override
    public Integer deleteCompleteByProjectIds(DwCaseDesignInfoDto dwCaseDesignInfoDto) {
        return caseDesignCompleteDetailsService.deleteCompleteByProjectIds(dwCaseDesignInfoDto);
    }
}
