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

import net.northking.cctp.common.exception.PlatformRuntimeException;
import net.northking.cctp.common.security.authentication.NKSecurityContext;
import net.northking.cctp.dw.constant.DWError;
import net.northking.cctp.dw.db.entity.CpProjectDetail;
import net.northking.cctp.dw.db.entity.MidProjectInfo;
import net.northking.cctp.dw.db.service.CpProjectDetailService;
import net.northking.cctp.dw.dto.CpProjectDetailAddDto;
import net.northking.cctp.dw.dto.CpProjectDetailQueryDto;
import net.northking.cctp.dw.utils.BeanHelper;
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: 2022/1/4 10:57
 */
@Service
public class CpProjectDetailApiServiceImpl implements CpProjectDetailApiService {

    @Autowired
    private CpProjectDetailService detailService;

    @Autowired
    private ChartApiService chartApiService;

    @Override
    @Transactional
    public void add(CpProjectDetailAddDto inputDto) {

        try {
            CpProjectDetail cpd = new CpProjectDetail();
            cpd.setProjectId(inputDto.getProjectId());
            cpd.setDetailType(inputDto.getDetailType());
            cpd.setTenantId(NKSecurityContext.getTenantId());
            detailService.deleteByExample(cpd);
            CpProjectDetail detail = BeanHelper.copyProperties(inputDto, CpProjectDetail.class);
            detailService.insert(detail);

            //  若未设置计划时间或未设置计划数或设置计划开始时间大于当前，则删除旧数据
            if (Objects.isNull(inputDto.getStartTime()) || Objects.isNull(inputDto.getPlanAmount()) || inputDto.getStartTime().compareTo(new Date()) > 0) {
                chartApiService.deleteByProjectIdAndDetailType(inputDto.getProjectId(), inputDto.getDetailType());
            }

            //  修改进度图数据
            if (Objects.nonNull(inputDto.getStartTime()) && Objects.nonNull(inputDto.getPlanAmount())) {
                chartApiService.progressDataDeal(inputDto.getProjectId(), inputDto.getPlanAmount(),
                        inputDto.getStartTime(), inputDto.getEndTime(), inputDto.getDetailType());
            }

            //  修改总进度数据
            chartApiService.progressDataDeal(inputDto.getProjectId());
        } catch (Exception e) {
            throw new PlatformRuntimeException(DWError.SET_PROJECT_DETAILS_FAILED, e);
        }
    }

    @Override
    public List<CpProjectDetail> query(CpProjectDetail cpd) {
        return detailService.query(cpd);
    }

    @Override
    public List<CpProjectDetailQueryDto> findByProjectId(String projectId) {
        CpProjectDetail detail = new CpProjectDetail();
        detail.setProjectId(projectId);
        return BeanHelper.copyWithCollection(detailService.query(detail), CpProjectDetailQueryDto.class);
    }

    @Override
    public List<CpProjectDetail> findByProjectIds(List<MidProjectInfo> dataList) {
        return detailService.findByProjectIds(dataList);
    }
}
