FileMangerService.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package com.example.fms.service;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.example.fms.common.model.dto.DownloadSysFileDTO;
  4. import com.example.fms.common.model.dto.SystemFileDTO;
  5. import com.example.fms.common.model.vo.ConfigSettingVO;
  6. import com.example.fms.common.model.vo.SystemFileVO;
  7. import com.example.fms.common.utils.ExcuteConfigUtils;
  8. import com.example.fms.domain.SystemFile;
  9. import com.example.fms.exception.XiaoShiException;
  10. import com.example.fms.mapper.SystemFileMapper;
  11. import lombok.RequiredArgsConstructor;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.springframework.beans.BeanUtils;
  14. import org.springframework.stereotype.Service;
  15. import org.springframework.util.CollectionUtils;
  16. import org.springframework.web.multipart.MultipartFile;
  17. import java.io.IOException;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. /**
  21. * 文件管理外部调用接口Service层
  22. * @Author xiexiang
  23. * @Date 2023/8/9
  24. */
  25. @RequiredArgsConstructor
  26. @Slf4j
  27. @Service
  28. public class FileMangerService {
  29. private final SystemFileService systemFileService;
  30. private final SystemFileMapper systemFileMapper;
  31. private final FileFactory fileFactory;
  32. /**
  33. * 上传文件的最外部接口
  34. * @param files 需要上传的文件
  35. * @param sourceId configSettingVO的主键id,判断存储在哪个服务器路径之下
  36. * @return insertIds 文件在数据库中的id集合
  37. * @throws IOException
  38. */
  39. public List<String> uploadFile(List<MultipartFile> files, Integer sourceId) throws IOException {
  40. if (files != null && files.size() != 0) {
  41. //1.调用解析配置方法,获取配置信息
  42. List<ConfigSettingVO> configSettingVOS = ExcuteConfigUtils.excuteConfigVO();
  43. //2.根据传入id,获得配置类,根据配置类选择使用的上传方法
  44. ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getId().equals(sourceId)).findFirst().orElse(null);
  45. //3.获取文件上传到服务器,调用工厂类
  46. String sourceName = configSettingVO.getSourceName();
  47. //TODO 调用工厂方法,工厂方法会根据sourceName创建并返回对应的方法的对象
  48. IFileFactory fileFactoryObject = fileFactory.createObject(sourceName);
  49. List<SystemFileDTO> systemFileDTOS = fileFactoryObject.uploadFile(files, configSettingVO);
  50. //4.根据上传后的返回实体类信息,将实体类信息入库,调用systemFileService add
  51. if (systemFileDTOS != null && systemFileDTOS.size() != 0) {
  52. List<String> GUIDs = systemFileService.add(systemFileDTOS, sourceId);
  53. return GUIDs;
  54. } else {
  55. throw new XiaoShiException("入表信息为空");
  56. }
  57. } else {
  58. throw new XiaoShiException("上传文件为空");
  59. }
  60. }
  61. public String replaceFile(MultipartFile file, String fileGuid, Integer sourceId){
  62. List<String> deleteFileGuids = new ArrayList<>();
  63. Integer type = 2;
  64. deleteFileGuids.add(fileGuid);
  65. this.deleteFile(deleteFileGuids, type);
  66. //1.调用解析配置方法,获取配置信息
  67. List<ConfigSettingVO> configSettingVOS = ExcuteConfigUtils.excuteConfigVO();
  68. //2.根据传入id,获得配置类,根据配置类选择使用的上传方法
  69. ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getId().equals(sourceId)).findFirst().orElse(null);
  70. //3.获取文件上传到服务器,调用工厂类
  71. String sourceName = configSettingVO.getSourceName();
  72. //TODO 调用工厂方法,工厂方法会根据sourceName创建并返回对应的方法的对象
  73. IFileFactory fileFactoryObject = fileFactory.createObject(sourceName);
  74. SystemFileDTO systemFileDTO = fileFactoryObject.replaceFile(fileGuid, file, configSettingVO);
  75. String res = systemFileService.replace(systemFileDTO, sourceId);
  76. return res;
  77. }
  78. /**
  79. * 下载文件
  80. * @param fileId 文件id
  81. * @return fileData 文件流
  82. * @throws Exception
  83. */
  84. public byte[] downloadFile(String fileId) {
  85. //1.新建一个对象
  86. DownloadSysFileDTO downloadSysFileDTO = new DownloadSysFileDTO();
  87. //2.根据传入文件id查询到文件信息
  88. LambdaQueryWrapper<SystemFile> queryWrapper = new LambdaQueryWrapper<>();
  89. queryWrapper.eq(SystemFile::getGuid, fileId);
  90. SystemFile systemFile = systemFileService.getOne(queryWrapper, false);
  91. //外层做过判断,一定可以查到
  92. //3.文件信息标记为已经删除的,返回null
  93. if (systemFile.getIsDelete().equals(1)) {
  94. return new byte[0];
  95. } else {
  96. //3.对象赋值给新的
  97. BeanUtils.copyProperties(systemFile, downloadSysFileDTO);
  98. //4.设置pType(存储在哪条路径下)
  99. Integer id = downloadSysFileDTO.getPType();
  100. //5.调用解析配置方法,获取配置信息
  101. List<ConfigSettingVO> configSettingVOS = ExcuteConfigUtils.excuteConfigVO();
  102. if(configSettingVOS == null && configSettingVOS.size() == 0){
  103. throw new XiaoShiException("缺失配置文件");
  104. }
  105. //6.根据id获取需要的配置类
  106. ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getId().equals(id)).findFirst().orElse(null);
  107. //7.设置sourceId为数据库中的sourceId字段
  108. downloadSysFileDTO.setSourceId(systemFile.getSourceId());
  109. //8.调用工厂类下载方法
  110. String sourceName = configSettingVO.getSourceName();
  111. //TODO 调用工厂方法,工厂方法会根据sourceName创建并返回对应的方法的对象
  112. IFileFactory fileFactoryObject = fileFactory.createObject(sourceName);
  113. byte[] fileData = fileFactoryObject.downloadFile(downloadSysFileDTO, configSettingVO);
  114. return fileData;
  115. }
  116. }
  117. /**
  118. * 删除系统文件
  119. * 分为物理删除和逻辑删除,逻辑删除只更新数据库中isDelete字段,物理删除需要调用工厂类方法
  120. * @param guIds 需要删除的GUID集合
  121. * @param type 删除方式判断字段,1为逻辑删除,2为物理删除
  122. */
  123. public String deleteFile(List<String> guIds, Integer type) {
  124. //1.首先判断传入的需要删除的id集合不能为空
  125. if (CollectionUtils.isEmpty(guIds)) {
  126. throw new XiaoShiException("需要删除的GUID集合不能为空");
  127. }
  128. //2.其次判断传入的删除类型不能为空
  129. if (type == null) {
  130. throw new XiaoShiException("删除类型不能为空");
  131. }
  132. //3.1 判断如果是逻辑删除(更新isDelete字段为1)
  133. if (type.equals(1)) {
  134. //3.1.1 遍历传入的id集合
  135. for (int i = 0; i < guIds.size(); i++) {
  136. //3.1.2 获取需要更新的对象
  137. LambdaQueryWrapper<SystemFile> queryWrapper = new LambdaQueryWrapper<>();
  138. queryWrapper.eq(SystemFile::getGuid, guIds.get(i));
  139. SystemFile systemFileVO = systemFileService.getOne(queryWrapper);
  140. //3.1.3 将对象的是否删除字段置为1
  141. systemFileVO.setIsDelete(1);
  142. //3.1.4 将查询出来的vo赋值给实体类
  143. SystemFile systemFile = new SystemFile();
  144. BeanUtils.copyProperties(systemFileVO, systemFile);
  145. //3.1.5 数据入表
  146. int row = systemFileMapper.update(systemFile);
  147. //3.1.6 判断是否成功
  148. if (row != 1) {
  149. String mes = "逻辑删除系统文件第" + i + "条失败";
  150. throw new XiaoShiException(mes);
  151. }
  152. }
  153. String mes = "逻辑删除成功";
  154. return mes;
  155. } else if (type.equals(2)) { //3.2 物理删除(先删除服务器文件,删除数据库中记录)
  156. //3.2.1 遍历传入的GUID集合
  157. List<Integer> deleteIds = new ArrayList<>();
  158. for (int i = 0; i < guIds.size(); i++) {
  159. //3.2.2 根据GUID到表中查询该文件记录的数据
  160. LambdaQueryWrapper<SystemFile> queryWrapper = new LambdaQueryWrapper<>();
  161. queryWrapper.eq(SystemFile::getGuid, guIds.get(i));
  162. SystemFile systemFileVO = systemFileService.getOne(queryWrapper);
  163. //3.2.3 根据pType判断存储在服务器的哪个地址下
  164. int pType = systemFileVO.getPType();
  165. //3.2.4 文件路径
  166. String filePath = systemFileVO.getFilePath();
  167. //3.2.5 调用解析配置方法,获取指定配置信息
  168. List<ConfigSettingVO> configSettingVOS = ExcuteConfigUtils.excuteConfigVO();
  169. ConfigSettingVO configSettingVO = configSettingVOS.stream().filter(item -> item.getId().equals(pType)).findFirst().orElse(null);
  170. //3.2.6 配置类的删除方法
  171. String sourceName = configSettingVO.getSourceName();
  172. //TODO 调用工厂方法,工厂方法会根据sourceName创建并返回对应的方法的对象
  173. IFileFactory fileFactoryObject = fileFactory.createObject(sourceName);
  174. fileFactoryObject.deleteFile(filePath, configSettingVO);
  175. deleteIds.add(systemFileVO.getId());
  176. // //3.2.7 调用removeById删除此条记录,主键为根据GUID查询到的对象的id
  177. // Boolean isDelete = systemFileService.removeById(systemFileVO.getId());
  178. // if (isDelete != true) {
  179. // throw new XiaoShiException("第" + i + "条删除异常");
  180. // }
  181. }
  182. Boolean isDelete = systemFileService.removeByIds(deleteIds);
  183. if (isDelete != true) {
  184. throw new XiaoShiException("删除异常");
  185. }
  186. String mes = "文件删除成功,文件记录删除成功";
  187. return mes;
  188. } else {
  189. String mes = "删除类型错误";
  190. return mes;
  191. }
  192. }
  193. }