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.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.AddProjectFileUploadBo;
import com.dsk.cscec.domain.bo.QueryProjectFileUploadDetailBo;
import com.dsk.cscec.domain.vo.QueryProjectFileUploadDetailVo;
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;
import java.util.Objects;

/**
 * 项目成本文件表(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(AddProjectFileUploadBo fileUploadBo, SysOssVo oss) {
        //校验项目是否存在
        this.checkProjectExist(fileUploadBo.getProjectId());

        CbProjectFile projectFile = BeanUtil.toBean(fileUploadBo, CbProjectFile.class);
        projectFile.setFileName(oss.getOriginalName());
        projectFile.setFileOssId(oss.getOssId());
        projectFile.setFileOssUrl(oss.getUrl());
        //项目文件状态:准备中
        projectFile.setFileParseStatus(CbProjectConstants.PROJECT_FILE_STATUS_PREPARING);
        //删除状态:未删除
        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);
    }

    /**
     * 获取项目文件上传详情
     *
     * @param uploadDetailBo 查询信息
     * @return 文件列表
     */
    @Override
    public QueryProjectFileUploadDetailVo getProjectFileUploadDetail(QueryProjectFileUploadDetailBo uploadDetailBo) {
        Long projectId = uploadDetailBo.getProjectId();
        //校验项目是否存在
        this.checkProjectExist(projectId);

        List<CbProjectFile> projectFileList = baseMapper.selectList(new LambdaQueryWrapper<CbProjectFile>()
                .eq(CbProjectFile::getProjectId, projectId)
                //成本阶段
                .eq(CbProjectFile::getCbStage, uploadDetailBo.getCbStage())
                //删除状态:否
                .eq(CbProjectFile::getDelFlag, CbProjectConstants.DELETE_FLAG_EXIST));

        QueryProjectFileUploadDetailVo draftDetailVo = new QueryProjectFileUploadDetailVo();
        draftDetailVo.setProjectId(projectId);
        if (projectFileList.isEmpty()) {
            return draftDetailVo;
        }

        //按照成本类型分类填充
        projectFileList.forEach(fileList -> {
            //TODO:此写法太丑陋,待优化
            if (Objects.equals(fileList.getCbType(), CbProjectConstants.CB_TYPE_DIRECT_EXPENSE)) {
                //直接费成本
                draftDetailVo.getDirectExpense().add(fileList);
            } else if (Objects.equals(fileList.getCbType(), CbProjectConstants.CB_TYPE_QUANTITY_SUMMARY)) {
                //工料汇总
                draftDetailVo.getQuantitySummary().add(fileList);
            } else if (Objects.equals(fileList.getCbType(), CbProjectConstants.CB_TYPE_MEASURE_PROJECT)) {
                //措施项目
                draftDetailVo.getMeasureProject().add(fileList);
            } else if (Objects.equals(fileList.getCbType(), CbProjectConstants.CB_TYPE_OTHER_PROJECT)) {
                //其他项目
                draftDetailVo.getOtherProject().add(fileList);
            } else if (Objects.equals(fileList.getCbType(), CbProjectConstants.CB_TYPE_SCENE_EXPENSE)) {
                //现场经费
                draftDetailVo.getSceneExpense().add(fileList);
            } else if (Objects.equals(fileList.getCbType(), CbProjectConstants.CB_TYPE_SUMMARY)) {
                //成本汇总
                draftDetailVo.getCbSummary().add(fileList);
            }
        });
        return draftDetailVo;
    }

    /**
     * 校验项目是否存在
     *
     * @param projectId 项目ID
     */
    private void checkProjectExist(Long projectId) {
        //校验项目是否存在
        if (!projectRecordMapper.exists(new LambdaQueryWrapper<CbProjectRecord>()
                .eq(CbProjectRecord::getId, projectId)
                .eq(CbProjectRecord::getDelFlag, CbProjectConstants.DELETE_FLAG_EXIST))) {
            throw new ServiceException("该项目不存在");
        }
    }
}
