package com.ruoyi.tool.service.impl;

import java.util.ArrayList;
import java.util.List;


import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.invest.domain.TProjectInformation;
import com.ruoyi.invest.service.ITProjectInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.tool.mapper.TUnifyFileMapper;
import com.ruoyi.tool.domain.TUnifyFile;
import com.ruoyi.tool.service.ITUnifyFileService;

/**
 * 附件Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-02-23
 */
@Service
public class TUnifyFileServiceImpl implements ITUnifyFileService 
{
    @Autowired
    private TUnifyFileMapper tUnifyFileMapper;
    @Autowired
    private ITProjectInformationService tProjectInformationService;

    /**
     * 查询附件
     * 
     * @param id 附件主键
     * @return 附件
     */
    @Override
    public TUnifyFile selectTUnifyFileById(String id)
    {
        return tUnifyFileMapper.selectTUnifyFileById(id);
    }

    /**
     * 查询附件列表
     * 
     * @param tUnifyFile 附件
     * @return 附件
     */
    @Override
    public List<TUnifyFile> selectTUnifyFileList(TUnifyFile tUnifyFile)
    {
        return tUnifyFileMapper.selectTUnifyFileList(tUnifyFile);
    }

    /**
     * 新增附件
     * 
     * @param tUnifyFile 附件
     * @return 结果
     */
    @Override
    public int insertTUnifyFile(TUnifyFile tUnifyFile)
    {
        tUnifyFile.setCreateTime(DateUtils.getNowDate());
        tUnifyFile.setId(IdUtils.fastSimpleUUID());
        return tUnifyFileMapper.insertTUnifyFile(tUnifyFile);
    }

    /**
     * 批量新增关联附件
     * @param tUnifyFileList
     * @param fileId
     * @param fileBusinessId
     * @param uploadType
     * @param createBy
     * @return
     */
    @Override
    public void insertTUnifyFileList(List<TUnifyFile> tUnifyFileList, String fileId, String fileBusinessId, String uploadType, String createBy) {
        if(null != tUnifyFileList && !tUnifyFileList.isEmpty()){
            for (TUnifyFile tUnifyFile:
                    tUnifyFileList) {
                if(tUnifyFile.getId() == null){
                    tUnifyFile.setId(IdUtils.fastSimpleUUID());
                    tUnifyFile.setCreateTime(DateUtils.getNowDate());
                    tUnifyFile.setFileId(fileId);
                    tUnifyFile.setFileBusinessId(fileBusinessId);
                    tUnifyFile.setUploadType(uploadType);//文件类型:公司信息
                    tUnifyFile.setCreateBy(createBy);
                    tUnifyFileMapper.insertTUnifyFile(tUnifyFile);
                }
            }
        }
    }
    /**
     * 批量新增关联附件
     * @param tUnifyFileList
     * @param projectPoolId
     * @param fileBusinessId
     * @param uploadType
     * @param createBy
     * @return
     */
    @Override
    public void insertTUnifyFileLists(List<TUnifyFile> tUnifyFileList, String projectPoolId, String fileBusinessId, String uploadType, String createBy,String fileType,String projectStage) {
        if(null != tUnifyFileList && !tUnifyFileList.isEmpty()){
            for (TUnifyFile tUnifyFile:
                    tUnifyFileList) {
                if(tUnifyFile.getId() == null){
                    tUnifyFile.setId(IdUtils.fastSimpleUUID());
                    tUnifyFile.setCreateTime(DateUtils.getNowDate());
                    tUnifyFile.setFileId(projectPoolId);
                    tUnifyFile.setFileBusinessId(fileBusinessId);
                    tUnifyFile.setUploadType(uploadType);//文件类型:公司信息
                    tUnifyFile.setCreateBy(createBy);
                    tUnifyFileMapper.insertTUnifyFile(tUnifyFile);
                    tProjectInformationService.insertTProjectInformation(new TProjectInformation(IdUtils.fastSimpleUUID(),
                            tUnifyFile.getNewUploadName(), fileType,
                            projectPoolId, projectStage, fileBusinessId, createBy,tUnifyFile.getId()));

                }
            }
        }
    }

    /**
     * 修改附件
     * 
     * @param tUnifyFile 附件
     * @return 结果
     */
    @Override
    public int updateTUnifyFile(TUnifyFile tUnifyFile)
    {
        tUnifyFile.setUpdateTime(DateUtils.getNowDate());
        return tUnifyFileMapper.updateTUnifyFile(tUnifyFile);
    }

    /**
     * 批量删除附件
     * 
     * @param ids 需要删除的附件主键
     * @return 结果
     */
    @Override
    public int deleteTUnifyFileByIds(String[] ids)
    {
        return tUnifyFileMapper.deleteTUnifyFileByIds(ids);
    }

    /**
     * 删除附件信息
     * 
     * @param id 附件主键
     * @return 结果
     */
    @Override
    public int deleteTUnifyFileById(String id)
    {
        return tUnifyFileMapper.deleteTUnifyFileById(id);
    }

    /**
     * 批量删除附件 (修改del_flag状态)
     * @param ids
     * @return
     */
    @Override
    public int updateTUnifyFileByIds(String[] ids) {
        return tUnifyFileMapper.updateTUnifyFileByIds(ids);
    }

    /**
     * 批量删除附件
     *
     * @param fileBusinessIds
     * @return
     */
    @Override
    public int updateTUnifyFileBusinessIds(String[] fileBusinessIds) {
        return tUnifyFileMapper.updateTUnifyFileBusinessIds(fileBusinessIds);
    }

    /**
     * 根据附件业务ID()获取附件详情信息列表
     * @param fileBusinessId
     * @return
     */
    @Override
    public List<TUnifyFile> selectTUnifyFileByBusinessIdList(String fileBusinessId,String uploadType) {
        return tUnifyFileMapper.selectTUnifyFileByBusinessIdList(fileBusinessId,uploadType);
    }
    /**
     * 根据ID()获取附件详情信息列表
     * @param id
     * @return
     */
    @Override
    public List<TUnifyFile> selectTUnifyFileByIdList(String id,String uploadType) {
        return tUnifyFileMapper.selectTUnifyFileByIdList(id,uploadType);
    }

    /**
     * 根据fileId()获取项目中所有附件列表
     * @param fileId
     * @return
     */
    @Override
    public List<TUnifyFile> listFileId(String fileId) {
        return tUnifyFileMapper.listFileId(fileId);
    }

}
