package com.dsk.cscec.service.impl;

import cn.hutool.core.bean.BeanUtil;
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.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;

/**
 * 项目台账表(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_WAIT_PARSE);
        //删除状态:否
        projectRecord.setDelFlag(CbProjectConstants.DELETE_FLAG_EXIST);
        if (baseMapper.insert(projectRecord) > 0) {
            CbProjectFile projectFile = new CbProjectFile();
            projectFile.setProjectId(projectRecord.getId());
            projectFile.setCbStage(projectRecord.getCbStage());
            //项目文件状态:未上传
            projectFile.setFileParseStatus(CbProjectConstants.PROJECT_FILE_STATUS_NOT_UPLOAD);
            //成本类型:未确定
            projectFile.setCbType(CbProjectConstants.CB_TYPE_NOT_CONFIRM);
            projectFile.setDelFlag(CbProjectConstants.DELETE_FLAG_EXIST);
            projectFileMapper.insert(projectFile);
            return projectRecord;
        }
        return null;
    }

    ///**
    // * 根据项目文件导入数据
    // *
    // * @param importDataBo 导入对象
    // * @return 导入结果
    // */
    //@Override
    //public Boolean importData(ImportCbProjectDataBo importDataBo) {
    //    Long projectId = importDataBo.getProjectId();
    //    Integer cbStage = importDataBo.getCbStage();
    //    //校验项目是否存在
    //    Assert.isTrue(baseMapper.exists(new LambdaQueryWrapper<CbProjectRecord>()
    //            .eq(CbProjectRecord::getId, projectId)
    //            .eq(CbProjectRecord::getCbStage, cbStage)
    //            .eq(CbProjectRecord::getDelFlag, CbProjectConstants.DELETE_FLAG_EXIST)), "该项目不存在");
    //    //TODO:调各个成本类型的导入文件方法
    //
    //    return null;
    //}
}
