forked from gzzfw/backEnd/gz-dyh

zhouxiantao
2024-09-14 6f4ed9dbb428374e5513fbf88aae1377fd423a51
dyh-service/dyh-sys/src/main/java/cn/huge/module/file/service/FileInfoService.java
@@ -1,5 +1,6 @@
package cn.huge.module.file.service;
import cn.huge.base.common.constant.FileCatEnum;
import cn.huge.base.common.exception.ServiceException;
import cn.huge.base.common.utils.DateUtils;
import cn.huge.base.common.utils.IdUtils;
@@ -11,12 +12,20 @@
import cn.huge.module.file.domain.dto.FileForCatListDTO;
import cn.huge.module.file.domain.dto.UploaderDTO;
import cn.huge.module.file.domain.po.FileInfo;
import cn.huge.module.sys.constant.FileOwnerTypeBaseEnum;
import cn.huge.module.sys.dto.FileIdInfoBaseDTO;
import cn.huge.module.sys.dto.FileIdTypeInfoBaseDTO;
import cn.huge.module.sys.dto.FileInfoBaseDTO;
import cn.huge.module.sys.dto.FileTypeInfoBaseDTO;
import cn.huge.module.file.domain.po.FileRelate;
import cn.huge.module.file.utils.FtpUtils;
import cn.huge.module.file.utils.FtpMultipartFileWrapper;
import cn.huge.module.sys.constant.FileOwnerTypeBaseEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -28,6 +37,12 @@
import org.springframework.web.multipart.MultipartFile;
import java.util.*;
import javax.annotation.PostConstruct;
import java.util.*;
import java.util.stream.Collectors;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
 * @title: 附件信息表业务逻辑处理
@@ -141,6 +156,157 @@
    }
    /**
     * 根据多个所属编号查询附件并先根据ownerId再根据附件类型分组
     * @param terms
     * @return List
     */
    public List<FileIdTypeInfoBaseDTO> listIdTypeInfoByOwnerIdList(Map<String, Object> terms){
        try {
            List<FileIdTypeInfoBaseDTO> fileTypeInfoBaseDTOList = new ArrayList<>();
            List<FileInfo> fileInfoList = mapper.listFile(terms);
            if(CollectionUtils.isNotEmpty(fileInfoList)){
                fileTypeInfoBaseDTOList = this.getIdFileTypeInfoBaseDTOList(fileInfoList);
            }
            return fileTypeInfoBaseDTOList;
        }catch (Exception e){
            log.error("[FileInfoService.listIdTypeInfoByOwnerIdList]调用失败,异常信息:"+e, e);
            throw new ServiceException("FileInfoService.listIdTypeInfoByOwnerIdList", e);
        }
    }
    /**
     * 根据多个编号查询
     * @param fileInfoList 附件列表
     * @return List<FileTypeInfoBaseDTO>
     */
    private List<FileIdTypeInfoBaseDTO> getIdFileTypeInfoBaseDTOList(List<FileInfo> fileInfoList){
        List<FileIdTypeInfoBaseDTO> fileIdTypeInfoBaseDTOList = new ArrayList<>();
        //把Id过滤出来
        Set<String> ownerIdSet = new HashSet<>();
        for(FileInfo fileInfo: fileInfoList){
            ownerIdSet.add(fileInfo.getOwnerId());
        }
        //把附件放入对应的OwnerId下
        Map<String, List<FileInfo>> map = new HashMap<>();
        for(String ownerId: ownerIdSet) {
            List<FileInfo> fileInfoListByOwnerIdList = new ArrayList<>();
            for (FileInfo fileInfo : fileInfoList) {
                if(ownerId.equals(fileInfo.getOwnerId())){
                    fileInfoListByOwnerIdList.add(fileInfo);
                }
            }
            if(CollectionUtils.isNotEmpty(fileInfoListByOwnerIdList)){
                map.put(ownerId, fileInfoListByOwnerIdList);
            }
        }
        //根据OwnerId编号
        for(String ownerId: ownerIdSet){
            if(map.containsKey(ownerId)){
                FileIdTypeInfoBaseDTO fileIdTypeInfoBaseDTO = new FileIdTypeInfoBaseDTO();
                fileIdTypeInfoBaseDTO.setOwnerId(ownerId);
                List<FileInfo> ownerIdFileInfoList = map.get(ownerId);
                if (CollectionUtils.isNotEmpty(ownerIdFileInfoList)) {
                    List<FileTypeInfoBaseDTO> fileTypeInfoBaseDTOList = new ArrayList<>();
                    Set<String> setTmp = new HashSet<>();
                    for (FileInfo fileInfo: ownerIdFileInfoList) {
                        if (ObjectUtils.isNotEmpty(fileInfo.getOwnerType())) {
                            setTmp.add(fileInfo.getOwnerType());
                        }
                    }
                    //根据文件类型分类
                    for(String ownerType: setTmp){
                        FileTypeInfoBaseDTO fileTypeInfoBaseDTO = new FileTypeInfoBaseDTO();
                        fileTypeInfoBaseDTO.setOwnerType(ownerType);
                        fileTypeInfoBaseDTO.setOwnerTypeName(FileOwnerTypeBaseEnum.getDes(ownerType));
                        fileTypeInfoBaseDTO.setSize(ownerIdFileInfoList.size());
                        List<FileInfoBaseDTO> fileInfoBaseDTOList = new ArrayList<>();
                        StringBuffer fileNames = new StringBuffer();
                        for(FileInfo fileInfo: ownerIdFileInfoList){
                            if(ownerType.equals(fileInfo.getOwnerType())){
                                FileInfoBaseDTO fileInfoBaseDTO = new FileInfoBaseDTO();
                                BeanUtils.copyProperties(fileInfo, fileInfoBaseDTO);
                                fileInfoBaseDTO.setOwnerCatName(FileOwnerTypeBaseEnum.getDes(fileInfo.getOwnerCat()));
                                fileInfoBaseDTO.setOwnerTypeName(FileOwnerTypeBaseEnum.getDes(fileInfo.getOwnerType()));
                                fileInfoBaseDTOList.add(fileInfoBaseDTO);
                                if(StringUtils.isNotBlank(fileNames)){
                                    fileNames.append(fileInfo.getName());
                                }else{
                                    fileNames.append("、" + fileInfo.getName());
                                }
                            }
                        }
                        fileTypeInfoBaseDTO.setFileNames(fileNames.toString());
                        fileTypeInfoBaseDTO.setFileList(fileInfoBaseDTOList);
                        fileTypeInfoBaseDTOList.add(fileTypeInfoBaseDTO);
                    }
                    fileIdTypeInfoBaseDTO.setFileList(fileTypeInfoBaseDTOList);
                    fileIdTypeInfoBaseDTOList.add(fileIdTypeInfoBaseDTO);
                }
            }
        }
        return fileIdTypeInfoBaseDTOList;
    }
    /**
     * 根据多个所属编号查询附件
     * @param terms
     * @return List
     */
    public List<FileIdInfoBaseDTO> listInfoByOwnerIdList(Map<String, Object> terms) {
        try {
            List<FileIdInfoBaseDTO> fileIdInfoBaseDTOList = new ArrayList<>();
            List<FileInfo> fileInfoList = mapper.listFile(terms);
            if(CollectionUtils.isNotEmpty(fileInfoList)){
                fileIdInfoBaseDTOList = this.getFileInfoBaseDTOList(fileInfoList);
            }
            return fileIdInfoBaseDTOList;
        }catch (Exception e){
            log.error("[FileInfoService.listInfoByOwnerIdList]调用失败,异常信息:"+e, e);
            throw new ServiceException("FileInfoService.listInfoByOwnerIdList", e);
        }
    }
    /**
     * 根据多个所属编号查询附件并根据ownerId分组
     * @param fileInfoList 附件列表
     * @return List<FileTypeInfoBaseDTO>
     */
    private List<FileIdInfoBaseDTO> getFileInfoBaseDTOList(List<FileInfo> fileInfoList){
        List<FileIdInfoBaseDTO> fileIdInfoBaseDTOList = new ArrayList<>();
        //把Id过滤出来
        Set<String> ownerIdSet = new HashSet<>();
        for(FileInfo fileInfo: fileInfoList){
            ownerIdSet.add(fileInfo.getOwnerId());
        }
        for(String ownerId: ownerIdSet){
            FileIdInfoBaseDTO fileIdInfoBaseDTO = new FileIdInfoBaseDTO();
            fileIdInfoBaseDTO.setOwnerId(ownerId);
            List<FileInfoBaseDTO> fileInfoBaseDTOList = new ArrayList<>();
            for (FileInfo fileInfo : fileInfoList) {
                if (ownerId.equals(fileInfo.getOwnerId())) {
                    FileInfoBaseDTO fileInfoBaseDTO = new FileInfoBaseDTO();
                    BeanUtils.copyProperties(fileInfo, fileInfoBaseDTO);
                    // TODO: 2022/3/31 简化
                    fileInfoBaseDTO.setOwnerCatName(FileOwnerTypeBaseEnum.getDes(fileInfo.getOwnerCat()));
                    fileInfoBaseDTO.setOwnerTypeName(FileOwnerTypeBaseEnum.getDes(fileInfo.getOwnerType()));
                    fileInfoBaseDTOList.add(fileInfoBaseDTO);
                }
            }
            fileIdInfoBaseDTO.setFileList(fileInfoBaseDTOList);
            fileIdInfoBaseDTOList.add(fileIdInfoBaseDTO);
        }
        return fileIdInfoBaseDTOList;
    }
    /**
     * web端上传附件,保存附件信息和业务关系
     * @param file 附件
     * @param ownerId 所属业务编号
@@ -219,6 +385,42 @@
    }
    /**
     * 上传附件,保存附件信息和业务关系
     * @param file 附件
     * @param ownerId 所属业务编号
     * @param ownerType 所属业务名称
     * @param fileCount 第几份附件
     * @param uploaderDTO 上传人信息
     * @return
     */
    public FileInfo uploadFile(MultipartFile file, String ownerId, String ownerType, int fileCount, UploaderDTO uploaderDTO) {
        try {
            String fileId = utilsClient.getNewTimeId();
            // 创建附件信息
            FileInfo fileInfo = new FtpMultipartFileWrapper(file).toWebFileInfo(fileId);
            // 上传到ftp服务器
            FtpUtils ftpUtils = new FtpUtils();
            ftpUtils.upload(fileInfo.getPath(), fileInfo.getFileName(), file.getInputStream());
            // 保存附件信息
            fileInfo.setName(FileOwnerTypeBaseEnum.getDes(ownerType) + BaseConsts.UNDER + fileCount);
            fileInfo.setOwnerId(ownerId);
            fileInfo.setOwnerCat(FileOwnerTypeBaseEnum.getCat(ownerType));
            fileInfo.setOwnerCatName(FileOwnerTypeBaseEnum.getDes(fileInfo.getOwnerCat()));
            fileInfo.setOwnerType(ownerType);
            fileInfo.setOwnerTypeName(FileOwnerTypeBaseEnum.getDes(ownerType));
            fileInfo.setCustId(uploaderDTO.getCustId());
            mapper.insert(fileInfo);
            // 保存业务关系
            fileRelateService.saveByFileInfo(fileInfo);
            return fileInfo;
        } catch (Exception e) {
            log.error("service方法[FileInfoService.uploadFile]调用异常:"+e, e);
            throw new ServiceException("FileInfoService.uploadFile", e);
        }
    }
    /**
     * 查看附件组件-分类查询附件
     * @param terms
     * @return
@@ -273,4 +475,49 @@
        }
    }
    /**
     * 根据多个所属编号查询附件
     * @param mainId
     * @return List
     */
    public List<FileInfoBaseDTO> listByMainId(String mainId){
        List<FileInfoBaseDTO> fileList = new ArrayList<>();
        Map<String, Object> terms = new HashMap<>();
        terms.put("mainId", mainId);
        List<FileInfo> fileInfoList = mapper.listFile(terms);
        if(CollectionUtils.isNotEmpty(fileInfoList)){
            for (FileInfo fileInfo : fileInfoList) {
                FileInfoBaseDTO fileInfoBaseDTO = new FileInfoBaseDTO();
                BeanUtils.copyProperties(fileInfo, fileInfoBaseDTO);
                fileInfoBaseDTO.setOwnerCatName(FileOwnerTypeBaseEnum.getDes(fileInfo.getOwnerCat()));
                fileInfoBaseDTO.setOwnerTypeName(FileOwnerTypeBaseEnum.getDes(fileInfo.getOwnerType()));
                fileList.add(fileInfoBaseDTO);
            }
        }
        return fileList;
    }
    /**
     * 根据多个所属编号查询附件
     * @param mainId,ownerType
     * @return List
     */
    public List<FileInfoBaseDTO> listByMainIdAndType(String mainId,String ownerType){
        List<FileInfoBaseDTO> fileList = new ArrayList<>();
        Map<String, Object> terms = new HashMap<>();
        terms.put("mainId", mainId);
        terms.put("types", ownerType);
        List<FileInfo> fileInfoList = mapper.listFile(terms);
        if(CollectionUtils.isNotEmpty(fileInfoList)){
            for (FileInfo fileInfo : fileInfoList) {
                FileInfoBaseDTO fileInfoBaseDTO = new FileInfoBaseDTO();
                BeanUtils.copyProperties(fileInfo, fileInfoBaseDTO);
                fileInfoBaseDTO.setOwnerCatName(FileOwnerTypeBaseEnum.getDes(fileInfo.getOwnerCat()));
                fileInfoBaseDTO.setOwnerTypeName(FileOwnerTypeBaseEnum.getDes(fileInfo.getOwnerType()));
                fileList.add(fileInfoBaseDTO);
            }
        }
        return fileList;
    }
}