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; import cn.huge.base.common.utils.ObjectUtils; import cn.huge.module.client.api.impl.UtilsClientImpl; import cn.huge.module.constant.BaseConsts; import cn.huge.module.file.dao.mapper.FileInfoMapper; import cn.huge.module.file.domain.dto.FileForCatDTO; 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; import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageRequest; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import java.io.InputStream; 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: 附件信息表业务逻辑处理 * @Description 附件信息表业务逻辑处理 * @company hugeinfo * @author liyj * @Time 2024-08-28 20:06:18 * @version 1.0.0 */ @Slf4j @Service @Transactional(rollbackFor = Exception.class) public class FileInfoService extends ServiceImpl{ @Autowired private FileInfoMapper mapper; @Autowired private FileRelateService fileRelateService; @Autowired private UtilsClientImpl utilsClient; /** * 更新对象 * @param entity 对象 */ public void updateFileInfo(FileInfo entity){ try{ mapper.updateFileInfo(entity); }catch (Exception e){ log.error("[FileInfoService.updateFileInfo]调用失败,异常信息:"+e, e); throw new ServiceException("FileInfoService.updateFileInfo", e); } } /** * 条件更新对象 * @param entity 对象 * @param terms 条件 */ public void updateFileInfoTerms(FileInfo entity, Map terms){ try{ mapper.updateFileInfoTerms(entity, terms); }catch (Exception e){ log.error("[FileInfoService.updateFileInfoTerms]调用失败,异常信息:"+e, e); throw new ServiceException("FileInfoService.updateFileInfoTerms", e); } } /** * 根据编号物理删除 * @param id 查询条件集合 */ public void deleteFileInfo(String id){ try{ mapper.deleteFileInfo(id); }catch (Exception e){ log.error("[FileInfoService.deleteFileInfo]调用失败,异常信息:"+e, e); throw new ServiceException("FileInfoService.deleteFileInfo", e); } } /** * 按条件查询 * @param terms 条件 * @return List */ public List listTerms(Map terms){ return mapper.listTerms(terms); } /** * 按条件统计 * @param terms 条件 * @return long */ public long countTerms(Map terms){ return mapper.countTerms(terms); } /** * 按条件分页查询 * @param page 分页对象 * @param terms 条件 * @return Page */ public Page pageQuery(PageRequest page, Map terms){ long total = mapper.countTerms(terms); List content = mapper.pageTerms(page, terms); return new PageImpl(content, page, total); } /** * 新增或更新对象 * @param fileInfo 实体对象 */ public void saveFileInfo(FileInfo fileInfo){ try{ Date nowDate = DateUtils.getNowDate(); // 判断是否新增 if (IdUtils.checkNewId(fileInfo.getId())){ fileInfo.setId(utilsClient.getNewTimeId()); fileInfo.setCreateTime(nowDate); } fileInfo.setUpdateTime(nowDate); this.saveOrUpdate(fileInfo); }catch (Exception e){ log.error("[FileInfoService.saveFileInfo]调用失败,异常信息:"+e, e); throw new ServiceException("FileInfoService.saveFileInfo", e); } } /** * 根据多个所属编号查询附件并先根据ownerId再根据附件类型分组 * @param terms * @return List */ public List listIdTypeInfoByOwnerIdList(Map terms){ try { List fileTypeInfoBaseDTOList = new ArrayList<>(); List 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 */ private List getIdFileTypeInfoBaseDTOList(List fileInfoList){ List fileIdTypeInfoBaseDTOList = new ArrayList<>(); //把Id过滤出来 Set ownerIdSet = new HashSet<>(); for(FileInfo fileInfo: fileInfoList){ ownerIdSet.add(fileInfo.getOwnerId()); } //把附件放入对应的OwnerId下 Map> map = new HashMap<>(); for(String ownerId: ownerIdSet) { List 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 ownerIdFileInfoList = map.get(ownerId); if (CollectionUtils.isNotEmpty(ownerIdFileInfoList)) { List fileTypeInfoBaseDTOList = new ArrayList<>(); Set 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 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 listInfoByOwnerIdList(Map terms) { try { List fileIdInfoBaseDTOList = new ArrayList<>(); List 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 */ private List getFileInfoBaseDTOList(List fileInfoList){ List fileIdInfoBaseDTOList = new ArrayList<>(); //把Id过滤出来 Set ownerIdSet = new HashSet<>(); for(FileInfo fileInfo: fileInfoList){ ownerIdSet.add(fileInfo.getOwnerId()); } for(String ownerId: ownerIdSet){ FileIdInfoBaseDTO fileIdInfoBaseDTO = new FileIdInfoBaseDTO(); fileIdInfoBaseDTO.setOwnerId(ownerId); List 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 所属业务编号 * @param ownerType 所属业务名称 * @param fileCount 第几份附件 * @param uploaderDTO 上传人信息 * @return */ public FileInfo webUploadFile(MultipartFile file, String mainId, 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.setMainId(mainId); fileInfo.setOwnerId(ownerId); fileInfo.setOwnerCat(FileOwnerTypeBaseEnum.getCat(ownerType)); fileInfo.setOwnerCatName(FileOwnerTypeBaseEnum.getDes(fileInfo.getOwnerCat())); fileInfo.setOwnerType(ownerType); fileInfo.setOwnerTypeName(FileOwnerTypeBaseEnum.getDes(ownerType)); fileInfo.setUploaderId(uploaderDTO.getUploaderId()); fileInfo.setUploaderName(uploaderDTO.getUploaderName()); fileInfo.setUploaderType(uploaderDTO.getUploaderType()); 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); } } /** * web端上传附件,保存附件信息和业务关系 * @param ownerId 所属业务编号 * @param ownerType 所属业务名称 * @param fileId 文件ID * @return */ public FileInfo webCopyFile(String fileId, String mainId, String ownerId, String ownerType) { try { int fileCount = fileRelateService.countByOwnerIdAndType(ownerId, ownerType) + 1; // FtpUtils ftpUtils = new FtpUtils(); FileInfo fileInfoOld = mapper.selectById(fileId); FileInfo fileInfo = new FileInfo(); if(ObjectUtils.isNotEmpty(fileInfoOld)){ // InputStream inputStream = ftpUtils.retrieveFileStream(fileInfoOld.getPath()); String fileNewId = utilsClient.getNewTimeId(); // 创建附件信息 BeanUtils.copyProperties(fileInfoOld, fileInfo); fileInfo.setId(fileNewId); // int index = fileInfoOld.getTrueName().lastIndexOf(BaseConsts.DOT); // String fileName = fileInfoOld.getTrueName().substring(0, index); // fileInfo.setFileName(fileName + BaseConsts.UNDER + fileInfo.getId() + BaseConsts.DOT + fileInfo.getSuffix()); // fileInfo.setFullPath(fileInfo.getPath() + BaseConsts.SLANT + fileInfo.getFileName()); // 上传到ftp服务器 // ftpUtils.upload(fileInfo.getPath(), fileInfo.getFileName(), inputStream); // 保存附件信息 fileInfo.setName(FileOwnerTypeBaseEnum.getDes(ownerType) + BaseConsts.UNDER + fileCount); fileInfo.setShowUrl("/api/web/fileInfo/show" + BaseConsts.SLANT + fileInfo.getId()); fileInfo.setDownUrl("/api/web/fileInfo/down" + BaseConsts.SLANT + fileInfo.getId()); fileInfo.setZipUrl(fileInfo.getShowUrl()); fileInfo.setMainId(mainId); fileInfo.setOwnerId(ownerId); fileInfo.setOwnerCat(FileOwnerTypeBaseEnum.getCat(ownerType)); fileInfo.setOwnerCatName(FileOwnerTypeBaseEnum.getDes(fileInfo.getOwnerCat())); fileInfo.setOwnerType(ownerType); fileInfo.setOwnerTypeName(FileOwnerTypeBaseEnum.getDes(ownerType)); mapper.insert(fileInfo); // 保存业务关系 fileRelateService.saveByFileInfo(fileInfo); } return fileInfo; } catch (Exception e) { log.error("service方法[FileInfoService.uploadFile]调用失败,异常信息:"+e, e); throw new ServiceException("FileInfoService.uploadFile", e); } } public static void main(String[] args) { FileInfo fileInfo = new FileInfo(); fileInfo.setFileName("一本账模板_2 ((团结)(2024-28).xlsx"); int i = fileInfo.getFileName().lastIndexOf("."); System.out.println(fileInfo.getFileName().substring(0,i)); } /** * wechat端上传附件,保存附件信息和业务关系 * @param file 附件 * @param ownerId 所属业务编号 * @param ownerType 所属业务名称 * @param fileCount 第几份附件 * @param uploaderDTO 上传人信息 * @return */ public FileInfo wechatUploadFile(MultipartFile file, String mainId, String ownerId, String ownerType, int fileCount, UploaderDTO uploaderDTO) { try { String fileId = utilsClient.getNewTimeId(); // 创建附件信息 FileInfo fileInfo = new FtpMultipartFileWrapper(file).toWechatFileInfo(fileId); // 上传到ftp服务器 FtpUtils ftpUtils = new FtpUtils(); ftpUtils.upload(fileInfo.getPath(), fileInfo.getFileName(), file.getInputStream()); // 保存附件信息 fileInfo.setName(FileOwnerTypeBaseEnum.getDes(ownerType) + BaseConsts.UNDER + fileCount); fileInfo.setMainId(mainId); fileInfo.setOwnerId(ownerId); fileInfo.setOwnerCat(FileOwnerTypeBaseEnum.getCat(ownerType)); fileInfo.setOwnerCatName(FileOwnerTypeBaseEnum.getDes(fileInfo.getOwnerCat())); fileInfo.setOwnerType(ownerType); fileInfo.setOwnerTypeName(FileOwnerTypeBaseEnum.getDes(ownerType)); fileInfo.setUploaderId(uploaderDTO.getUploaderId()); fileInfo.setUploaderName(uploaderDTO.getUploaderName()); fileInfo.setUploaderType(uploaderDTO.getUploaderType()); 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 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 */ public List webListFileByCat(Map terms){ List fileForCatListDTOList = new ArrayList<>(); // 查询附件信息进行封装 List oldFileForCatDTOList = mapper.listFileByCatTerms(terms); if (CollectionUtils.isNotEmpty(oldFileForCatDTOList)) { Set setTmp = new HashSet<>(); for (FileForCatDTO fileForCatDTO: oldFileForCatDTOList) { if (ObjectUtils.isNotEmpty(fileForCatDTO.getOwnerCat())) { setTmp.add(fileForCatDTO.getOwnerCat()); } } Iterator it = setTmp.iterator(); while (it.hasNext()) { String ownerCat = it.next(); FileForCatListDTO fileForCatListDTO = new FileForCatListDTO(); fileForCatListDTO.setOwnerCat(ownerCat); fileForCatListDTO.setOwnerCatName(FileOwnerTypeBaseEnum.getDes(ownerCat)); List newFileForCatDTOS = new ArrayList<>(); for (FileForCatDTO fileForCatDTO : oldFileForCatDTOList) { if (ownerCat.equals(fileForCatDTO.getOwnerCat())) { newFileForCatDTOS.add(fileForCatDTO); } } fileForCatListDTO.setFileList(newFileForCatDTOS); fileForCatListDTO.setFileSize(newFileForCatDTOS.size()); fileForCatListDTOList.add(fileForCatListDTO); } } return fileForCatListDTOList; } /** * 根据编号删除附件 * @param id 附件编号 */ public void deleteFileById(String id) { try{ // 删除关系表 QueryWrapper fileRelateQueryWrapper = new QueryWrapper<>(); fileRelateQueryWrapper.eq("file_id", id); fileRelateService.remove(fileRelateQueryWrapper); // 删除附件信息表 mapper.deleteById(id); // 删除ftp文件 }catch (Exception e){ log.error("service方法[FileInfoService.deleteFileById]调用失败,异常信息:"+e, e); throw new ServiceException("FileInfoService.deleteFileById", e); } } /** * 根据多个所属编号查询附件 * @param mainId * @return List */ public List listByMainId(String mainId){ List fileList = new ArrayList<>(); Map terms = new HashMap<>(); terms.put("mainId", mainId); List 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 listByMainIdAndType(String mainId,String ownerType){ List fileList = new ArrayList<>(); Map terms = new HashMap<>(); terms.put("mainId", mainId); terms.put("ownerType", ownerType); List fileInfoList = mapper.listFileByType(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 terms 条件 * @return List */ public List listFileInfoByTerms(Map terms) { try { return mapper.listFileInfoByTerms(terms); }catch (Exception e){ log.error("[FileInfoService.listFileInfoByTerms]调用失败,异常信息:"+e, e); throw new ServiceException("FileInfoService.listFileInfoByTerms", e); } } }