package com.dsk.cscec.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsk.common.exception.ServiceException;
import com.dsk.cscec.constant.CbProjectConstants;
import com.dsk.cscec.domain.CbProjectFile;
import com.dsk.cscec.domain.CbProjectRecord;
import com.dsk.cscec.domain.bo.QueryCbProjectFileDetailBo;
import com.dsk.cscec.domain.bo.ProjectFileUploadBo;
import com.dsk.cscec.mapper.CbProjectFileMapper;
import com.dsk.cscec.mapper.CbProjectRecordMapper;
import com.dsk.cscec.service.CbProjectFileService;
import com.dsk.system.domain.vo.SysOssVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 项目成本文件表(CbProjectFile)表服务实现类
 *
 * @author sxk
 * @since 2024-02-05 14:01:06
 */
@Service("cbProjectFileService")
public class CbProjectFileServiceImpl extends ServiceImpl<CbProjectFileMapper, CbProjectFile> implements CbProjectFileService {
    @Resource
    private CbProjectFileMapper baseMapper;
    @Resource
    private CbProjectRecordMapper projectRecordMapper;

    /**
     * 添加项目上传文件记录
     *
     * @param fileUploadBo 上传信息
     * @param oss          oss信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addProjectFile(ProjectFileUploadBo fileUploadBo, SysOssVo oss) {
        //校验项目是否存在
        if (!projectRecordMapper.exists(new LambdaQueryWrapper<CbProjectRecord>()
                .eq(CbProjectRecord::getId, fileUploadBo.getProjectId())
                .eq(CbProjectRecord::getDelFlag, CbProjectConstants.DELETE_FLAG_EXIST))) {
            throw new ServiceException("项目ID无效,该项目不存在");
        }
        CbProjectFile projectFile = BeanUtil.toBean(fileUploadBo, CbProjectFile.class);
        projectFile.setFileName(oss.getOriginalName());
        projectFile.setFileOssId(oss.getOssId().toString());
        projectFile.setFileOssUrl(oss.getUrl());
        //项目文件状态:解析中
        projectFile.setFileParseStatus(CbProjectConstants.PROJECT_FILE_STATUS_PARSING);
        //删除状态:未删除
        projectFile.setDelFlag(CbProjectConstants.DELETE_FLAG_EXIST);
        baseMapper.insert(projectFile);
    }

    /**
     * 逻辑删除项目上传文件记录
     *
     * @param fileId 文件ID
     * @return 删除结果
     */
    @Override
    public Integer deleteProjectFile(Long fileId) {
        CbProjectFile projectFile = baseMapper.selectById(fileId);
        Assert.notNull(projectFile,"该文件不存在");
        projectFile.setDelFlag(CbProjectConstants.DELETE_FLAG_NOT_EXIST);
        return baseMapper.updateById(projectFile);
    }

    ///**
    // * 根据项目ID获取项目文件详情
    // *
    // * @param queryBo 查询体
    // * @return 文件详情列表
    // */
    //@Override
    //public List<CbProjectFile> getProjectFileDetail(QueryCbProjectFileDetailBo queryBo) {
    //    return baseMapper.selectList(new LambdaQueryWrapper<CbProjectFile>()
    //            .eq(CbProjectFile::getProjectId, queryBo.getProjectId())
    //            .eq(CbProjectFile::getCbStage, queryBo.getCbStage())
    //            //成本类型:未确定,即创建项目后未上传任何文件
    //            .ne(CbProjectFile::getCbType, CbProjectConstants.CB_TYPE_NOT_CONFIRM)
    //            .eq(CbProjectFile::getDelFlag, CbProjectConstants.DELETE_FLAG_EXIST)
    //            //项目文件状态:未上传
    //            .eq(CbProjectFile::getFileParseStatus, CbProjectConstants.PROJECT_FILE_STATUS_NOT_UPLOAD)
    //            .or()
    //            //项目文件状态:待解析
    //            .eq(CbProjectFile::getFileParseStatus, CbProjectConstants.PROJECT_FILE_STATUS_WAIT_PARSE));
    //}

}
