FileMangerService.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package com.example.fms.service;
  2. import com.example.fms.common.model.dto.DownloadSysFileDTO;
  3. import com.example.fms.common.model.dto.SystemFileDTO;
  4. import com.example.fms.common.model.vo.SystemFileVO;
  5. import com.example.fms.domain.SystemFile;
  6. import com.example.fms.exception.XiaoShiException;
  7. import com.example.fms.mapper.SystemFileMapper;
  8. import lombok.RequiredArgsConstructor;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.springframework.beans.BeanUtils;
  11. import org.springframework.stereotype.Service;
  12. import org.springframework.util.CollectionUtils;
  13. import org.springframework.web.multipart.MultipartFile;
  14. import java.util.List;
  15. /**
  16. * @Author xiexiang
  17. * @Date 2023/8/9
  18. */
  19. @RequiredArgsConstructor
  20. @Slf4j
  21. @Service
  22. public class FileMangerService {
  23. private final FileFactoryService fileFactoryService;
  24. private final SystemFileService systemFileService;
  25. private final SystemFileMapper systemFileMapper;
  26. /**
  27. * 上传文件
  28. *
  29. * @param files
  30. * @param sourceId
  31. */
  32. public List<Integer> add(List<MultipartFile> files, Integer sourceId){
  33. if(files != null && files.size() != 0){
  34. //1.获取文件上传到服务器,调用FileFactory的uploadFile
  35. List<SystemFileDTO> systemFileDTOS = fileFactoryService.uploadFiles(files, sourceId);
  36. //2.根据上传后的返回实体类信息,将实体类信息入库,调用systemFileService add
  37. if(systemFileDTOS != null && systemFileDTOS.size() != 0 ) {
  38. List<Integer> insertIds = systemFileService.add(systemFileDTOS,sourceId);
  39. return insertIds;
  40. } else {
  41. throw new XiaoShiException("入表信息为空");
  42. }
  43. } else {
  44. throw new XiaoShiException("上传文件为空");
  45. }
  46. }
  47. //取系统文件
  48. public byte[] download(Integer fileId) throws Exception {
  49. DownloadSysFileDTO downloadSysFileDTO = new DownloadSysFileDTO();
  50. if(fileId != null){
  51. SystemFileVO systemFileVO = systemFileService.query(fileId);
  52. if(systemFileVO.getIsDelete().equals(1)){
  53. return null;
  54. } else {
  55. BeanUtils.copyProperties(systemFileVO,downloadSysFileDTO);
  56. downloadSysFileDTO.setSourceId(systemFileVO.getPType());
  57. return fileFactoryService.download(downloadSysFileDTO);
  58. }
  59. }
  60. return null;
  61. }
  62. /**
  63. * 删除系统文件
  64. *
  65. * @param ids
  66. * @param type
  67. */
  68. public void delete(List<Integer> ids, Integer type){
  69. //首先判断传入的需要删除的id集合不能为空
  70. if(CollectionUtils.isEmpty(ids)){
  71. throw new XiaoShiException("需要删除的id集合不能为空");
  72. }
  73. if(type == null){
  74. throw new XiaoShiException("删除类型不能为空");
  75. }
  76. //有逻辑删除和物理删除两种,需要进行判定,传入类型为2时,物理删除;传入类型为1时,逻辑删除
  77. //逻辑删除(更新isDelete字段为1)
  78. if(type.equals(1)){
  79. //遍历传入的id集合,获取需要更新的对象,赋值给实体类
  80. for(int i = 0; i < ids.size(); i++){
  81. SystemFileVO systemFileVO = systemFileMapper.query(ids.get(i));
  82. systemFileVO.setIsDelete(1);
  83. //将查询出来的vo赋值给实体类
  84. SystemFile systemFile = new SystemFile();
  85. BeanUtils.copyProperties(systemFileVO, systemFile);
  86. //更新表
  87. int row = systemFileMapper.update(systemFile);
  88. if(row != 1){
  89. String mes = "逻辑删除系统文件第" + i + "条失败";
  90. log.info("逻辑删除失败,{}", mes);
  91. throw new XiaoShiException(mes);
  92. }
  93. }
  94. }
  95. //物理删除(删除服务器文件,删除数据库中记录)
  96. //sftp删除文件
  97. if(type.equals(2)){
  98. for(int i = 0; i < ids.size(); i++){
  99. //根据id到表中查询该文件记录的数据
  100. SystemFileVO systemFileVO = systemFileService.query(ids.get(i));
  101. //pType判断是服务器还是阿里云OSS
  102. int pType = systemFileVO.getPType();
  103. String filePath = systemFileVO.getFilePath();
  104. fileFactoryService.delete(filePath, pType);
  105. int row = systemFileMapper.deleteById(ids.get(i));
  106. if(row != 1){
  107. throw new XiaoShiException("删除异常");
  108. } else {
  109. log.info("删除成功");
  110. }
  111. }
  112. }
  113. }
  114. }