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.MidTestPointService;
import net.northking.cctp.dw.db.service.MtTestChartService;
import net.northking.cctp.dw.db.service.PointCompleteDetailsService;
import net.northking.cctp.dw.dto.MidTestPointDetailDto;
import net.northking.cctp.dw.dto.TestPointPieDto;
import net.northking.cctp.dw.utils.DataDealUtil;
import net.northking.cctp.dw.utils.DateDealUtils;
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.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author: puchao.ma
 * @date: 2021/12/14 16:56
 */
@Service
public class MtTestPointApiServiceImpl implements MtTestPointApiService {

    @Autowired
    private PointCompleteDetailsService pointCompleteDetailsService;

    @Autowired
    private MidTestPointService midTestPointService;

    @Autowired
    private CpProjectDetailApiService projectDetailApiService;

    @Autowired
    private MtTestChartService testChartService;

    @Override
    @Transactional
    public Integer addBatchComplete(List<PointCompleteDetails> details) {
        return pointCompleteDetailsService.insertByBatch(details);
    }

    /**
     * 清空表数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAllComplete(PointCompleteDetails pointCompleteDetails) {
        pointCompleteDetailsService.deleteByExample(pointCompleteDetails);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAllMid(List<MidProjectInfo> midProjectInfos) {
        midTestPointService.deleteByData(midProjectInfos);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addBatchMid(List<MidTestPoint> testPoints) {
        midTestPointService.insertByBatch(testPoints);
    }

    /**
     * 测试点进度图数据生成
     *
     * @return
     */
    @Override
    @Transactional
    public List<MtTestChart> createTestPointPlanProgress(String tenantId) {
        return midTestPointService.createTestPointProgress(tenantId);
    }

    /**
     * 测试点饼图
     *
     * @param projectId
     * @return
     */
    @Override
    public TestPointPieDto testPointPie(String projectId) {
        TestPointPieDto pieDto = new TestPointPieDto();
        List<MidTestPointDetailDto> pointPie = null;
        try {
            pointPie = midTestPointService.testPointPie(projectId);
        } catch (Exception e) {
            throw new PlatformRuntimeException(DWError.QUERY_TEST_POINT_PIE_FAILED, e);
        }

        if (CollectionUtils.isNotEmpty(pointPie) && Objects.nonNull(pointPie.get(0).getPlanAmount())) {
            int linkCase = pointPie.stream().mapToInt(MidTestPointDetailDto::getCompleted).sum();
            int news = pointPie.stream().mapToInt(MidTestPointDetailDto::getUncompleted).sum();
            int total = pointPie.stream().mapToInt(MidTestPointDetailDto::getTotal).sum();

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

            if (total != 0) {
                pieDto.setLinkCase(linkCase);
                pieDto.setNews(news);
                pieDto.setUncompleted(uncompleted);
                pieDto.setLinkCaseRatio(DataDealUtil.calculateFour(linkCase, Math.max(planAmount, total)));
                pieDto.setNewsRatio(DataDealUtil.calculateFour(news, Math.max(planAmount, total)));
                pieDto.setUncompletedRatio(DataDealUtil.calculateFour(uncompleted, planAmount));
            } else {
                pieDto.setLinkCase(0);
                pieDto.setNews(0);
                pieDto.setUncompleted(planAmount);
                pieDto.setLinkCaseRatio(0.0);
                pieDto.setNewsRatio(0.0);
                pieDto.setUncompletedRatio(100.0);
            }

            double v = DataDealUtil.calculateTwo((linkCase + news) * 100, planAmount);
            pieDto.setPlanCompletedRatio(v + "%");
        }
        return pieDto;
    }

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

    @Override
    public Integer testPointComplete(DwTestPoint dwTestPoint) {
        //  获取上周六
        Date lastSat = DateDealUtils.getLastSat();
        return pointCompleteDetailsService.testPointComplete(lastSat,dwTestPoint);
    }

    @Override
    public Integer deleteAllCompleteByProjectIds(PointCompleteDetails pointCompleteDetails) {
        return pointCompleteDetailsService.deleteAllCompleteByProjectIds(pointCompleteDetails);
    }

}
