package com.dsk.cscec.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsk.cscec.constant.CbProjectConstants;
import com.dsk.cscec.domain.CbProjectFile;
import com.dsk.cscec.domain.CbProjectRecord;
import com.dsk.cscec.domain.bo.AddCbProjectBo;
import com.dsk.cscec.domain.bo.EditProjectInfoBo;
import com.dsk.cscec.domain.bo.ImportCbProjectDataBo;
import com.dsk.cscec.mapper.CbProjectFileMapper;
import com.dsk.cscec.mapper.CbProjectRecordMapper;
import com.dsk.cscec.service.CbProjectRecordService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 项目台账表(CbProjectRecord)表服务实现类
 *
 * @author sxk
 * @since 2024-02-05 11:37:53
 */
@Service("cbProjectRecordService")
public class CbProjectRecordServiceImpl extends ServiceImpl<CbProjectRecordMapper, CbProjectRecord> implements CbProjectRecordService {
    @Resource
    private CbProjectRecordMapper baseMapper;
    @Resource
    private CbProjectFileMapper projectFileMapper;

    /**
     * 新增项目
     *
     * @param addCbProjectBo 新增对象
     * @return 新增结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CbProjectRecord addProject(AddCbProjectBo addCbProjectBo) {
        CbProjectRecord projectRecord = BeanUtil.toBean(addCbProjectBo, CbProjectRecord.class);
        //项目文件状态:准备中
        projectRecord.setProjectFileStatus(CbProjectConstants.PROJECT_FILE_STATUS_PREPARING);
        //删除状态:否
        projectRecord.setDelFlag(CbProjectConstants.DELETE_FLAG_EXIST);
        if (baseMapper.insert(projectRecord) > 0) {
            return projectRecord;
        }
        return null;
    }

    /**
     * 根据项目文件导入数据
     *
     * @param importDataBo 导入对象
     */
    @Override
    public void importData(ImportCbProjectDataBo importDataBo) {
        Long projectId = importDataBo.getProjectId();
        Integer cbStage = importDataBo.getCbStage();
        //校验项目是否存在
        this.checkProjectExist(projectId);

        //校验各个成本类型是否至少有1个文件
        //直接费成本
        this.checkCbTypeExistFile(importDataBo, CbProjectConstants.CB_TYPE_DIRECT_EXPENSE, CbProjectConstants.CB_TYPE_DIRECT_EXPENSE_NAME);
        //工料汇总
        this.checkCbTypeExistFile(importDataBo, CbProjectConstants.CB_TYPE_QUANTITY_SUMMARY, CbProjectConstants.CB_TYPE_QUANTITY_SUMMARY_NAME);
        //措施项目
        this.checkCbTypeExistFile(importDataBo, CbProjectConstants.CB_TYPE_MEASURE_PROJECT, CbProjectConstants.CB_TYPE_MEASURE_PROJECT_NAME);
        //其他项目
        this.checkCbTypeExistFile(importDataBo, CbProjectConstants.CB_TYPE_OTHER_PROJECT, CbProjectConstants.CB_TYPE_OTHER_PROJECT_NAME);
        //现场经费
        this.checkCbTypeExistFile(importDataBo, CbProjectConstants.CB_TYPE_SCENE_EXPENSE, CbProjectConstants.CB_TYPE_SCENE_EXPENSE_NAME);
        //成本汇总
        this.checkCbTypeExistFile(importDataBo, CbProjectConstants.CB_TYPE_SUMMARY, CbProjectConstants.CB_TYPE_SUMMARY_NAME);

        //TODO:调各个成本类型的解析文件方法
        //直接费成本
        //工料汇总
        //措施项目
        //其他项目
        //现场经费
        //成本汇总

    }

    /**
     * 校验各个成本类型是否至少有1个文件
     *
     * @param importDataBo 导入对象
     * @param cbType       成本类型字典（0:直接费成本、1:工料汇总、2:措施项目、3:其他项目、4:现场经费、5:成本汇总）
     * @param cbTypeName   成本类型名称
     */
    private void checkCbTypeExistFile(ImportCbProjectDataBo importDataBo, Integer cbType, String cbTypeName) {
        boolean isExist = projectFileMapper.exists(new LambdaQueryWrapper<CbProjectFile>()
                .eq(CbProjectFile::getProjectId, importDataBo.getProjectId())
                .eq(CbProjectFile::getCbStage, importDataBo.getCbStage())
                .eq(CbProjectFile::getCbType, cbType)
                //项目文件状态:准备中
                .eq(CbProjectFile::getFileParseStatus, CbProjectConstants.PROJECT_FILE_STATUS_PREPARING)
                //删除状态:否
                .eq(CbProjectFile::getDelFlag, CbProjectConstants.DELETE_FLAG_EXIST));
        Assert.isTrue(isExist, cbTypeName + "至少需要上传1个文件");
    }

    /**
     * 修改项目信息
     *
     * @param editBo 修改对象
     * @return 修改结果
     */
    @Override
    public Integer editProjectInfo(EditProjectInfoBo editBo) {
        //校验项目是否存在
        this.checkProjectExist(editBo.getProjectId());

        CbProjectRecord projectRecord = baseMapper.selectById(editBo.getProjectId());
        projectRecord.setProjectName(editBo.getProjectName());
        projectRecord.setIpmProjectNo(editBo.getIpmProjectNo());
        projectRecord.setIsGetProjectDetail(editBo.getIsGetProjectDetail());
        projectRecord.setProjectFileName(editBo.getProjectFileName());
        return baseMapper.updateById(projectRecord);
    }

    /**
     * 校验项目是否存在
     *
     * @param projectId 项目ID
     */
    private void checkProjectExist(Long projectId) {
        CbProjectRecord projectRecord = baseMapper.selectOne(new LambdaQueryWrapper<CbProjectRecord>()
                .eq(CbProjectRecord::getId, projectId)
                .eq(CbProjectRecord::getDelFlag, CbProjectConstants.DELETE_FLAG_EXIST));
        Assert.notNull(projectRecord, "该项目不存在");
    }

    /**
     * 获取草稿弹窗列表
     *
     * @return 草稿弹窗列表
     */
    @Override
    public List<CbProjectRecord> getDraftDialogList() {
        return baseMapper.selectList(new LambdaQueryWrapper<CbProjectRecord>()
                .eq(CbProjectRecord::getProjectFileStatus,CbProjectConstants.PROJECT_FILE_STATUS_PREPARING));
    }
}
