CommonServiceImpl.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package com.wtkj.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  4. import com.wtkj.dto.UploadFileDTO;
  5. import com.wtkj.dto.UploadInnerFileDTO;
  6. import com.wtkj.entity.FileAndFolder;
  7. import com.wtkj.entity.FileAndFolderHistory;
  8. import com.wtkj.entity.InnerFileAndFolder;
  9. import com.wtkj.service.ICommonService;
  10. import com.wtkj.service.IFileAndFolderHistoryService;
  11. import com.wtkj.service.IFileAndFolderService;
  12. import com.wtkj.service.IInnerFileAndFolderService;
  13. import com.wutong.file.feign.IFileClient;
  14. import com.wutong.file.vo.FileVO;
  15. import lombok.AllArgsConstructor;
  16. import org.springblade.core.log.exception.ServiceException;
  17. import org.springblade.core.secure.utils.AuthUtil;
  18. import org.springblade.core.tool.api.R;
  19. import org.springblade.core.tool.utils.Func;
  20. import org.springblade.core.tool.utils.StringUtil;
  21. import org.springframework.stereotype.Service;
  22. import org.springframework.transaction.annotation.Transactional;
  23. import org.springframework.util.CollectionUtils;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import java.util.Objects;
  27. import static com.wtkj.config.MagicValue.TWO;
  28. /**
  29. * @author Blizzard
  30. * @create at 2023-09-14 16:07
  31. * @describe
  32. */
  33. @Service
  34. @AllArgsConstructor
  35. public class CommonServiceImpl implements ICommonService {
  36. private final IFileClient fileClient;
  37. private final IFileAndFolderService fileAndFolderService;
  38. private final IFileAndFolderHistoryService historyService;
  39. private final IInnerFileAndFolderService innerFileAndFolderService;
  40. @Override
  41. public boolean uploadFile(List<UploadFileDTO> files) {
  42. boolean flag = false;
  43. List<FileAndFolder> fileList = new ArrayList<>();
  44. if (!CollectionUtils.isEmpty(files)) {
  45. Long projectId = files.get(0).getProjectId();
  46. Long parentId = files.get(0).getParentId();
  47. Long stageId = files.get(0).getStageId();
  48. files.forEach(file -> {
  49. Long fileId = file.getBladeFileId();
  50. R<FileVO> rpc = fileClient.findById(fileId);
  51. if (rpc.isSuccess()) {
  52. FileAndFolder newFile = new FileAndFolder();
  53. newFile.setProjectId(projectId);
  54. newFile.setStageId(stageId);
  55. newFile.setParentId(parentId);
  56. newFile.setBladeFileId(file.getBladeFileId());
  57. newFile.setType(1);
  58. newFile.setTitle(rpc.getData().getOriginalFileName());
  59. if (parentId != null && parentId > 0L) {
  60. FileAndFolder folder = fileAndFolderService.getById(parentId);
  61. if (folder != null) {
  62. newFile.setIsPrivate(folder.getIsPrivate());
  63. newFile.setPrivateUser(folder.getPrivateUser());
  64. }
  65. }
  66. fileList.add(newFile);
  67. }
  68. });
  69. flag = fileAndFolderService.saveBatch(fileList);
  70. }
  71. return flag;
  72. }
  73. @Override
  74. @Transactional(rollbackFor = Exception.class)
  75. public boolean coverFile(List<UploadFileDTO> files) {
  76. boolean flag = false;
  77. List<FileAndFolder> fileList = new ArrayList<>();
  78. List<FileAndFolderHistory> histories = new ArrayList<>();
  79. if (!CollectionUtils.isEmpty(files)) {
  80. Long projectId = files.get(0).getProjectId();
  81. Long parentId = files.get(0).getParentId();
  82. Long stageId = files.get(0).getStageId();
  83. files.forEach(file -> {
  84. Long fileId = file.getBladeFileId();
  85. R<FileVO> rpc = fileClient.findById(fileId);
  86. if (rpc.isSuccess()) {
  87. //是否存在同名文件
  88. LambdaQueryWrapper<FileAndFolder> lqw = Wrappers.lambdaQuery();
  89. lqw.eq(FileAndFolder::getParentId, parentId);
  90. lqw.eq(FileAndFolder::getTitle, rpc.getData().getOriginalFileName());
  91. FileAndFolder old = fileAndFolderService.getOne(lqw);
  92. if (old != null) {
  93. //存在同名文件
  94. //此处不删除旧的同名文件,直接修改值 ---> 再保存历史记录信息 修改已存在的的数据 更新url 文件大小等信息
  95. //先根据旧的fileId判断是否存在历史记录
  96. List<FileAndFolderHistory> hisList = historyService.getByLatestId(old.getBladeFileId());
  97. if (!CollectionUtils.isEmpty(hisList)) {
  98. histories.forEach(h -> {
  99. h.setLatestId(fileId);
  100. histories.add(h);
  101. });
  102. }
  103. //历史文件和新上传文件不是同一个的时候才保存
  104. if (!fileId.equals(old.getBladeFileId())) {
  105. FileAndFolderHistory his = new FileAndFolderHistory();
  106. his.setLatestId(fileId);
  107. his.setHistoryId(old.getBladeFileId());
  108. hisList.add(his);
  109. }
  110. old.setBladeFileId(fileId);
  111. } else {
  112. FileAndFolder newFile = new FileAndFolder();
  113. newFile.setProjectId(projectId);
  114. newFile.setStageId(stageId);
  115. newFile.setParentId(parentId);
  116. newFile.setBladeFileId(file.getBladeFileId());
  117. newFile.setType(1);
  118. newFile.setTitle(rpc.getData().getOriginalFileName());
  119. if (parentId != null && parentId > 0L) {
  120. FileAndFolder folder = fileAndFolderService.getById(parentId);
  121. if (folder != null) {
  122. newFile.setIsPrivate(folder.getIsPrivate());
  123. newFile.setPrivateUser(folder.getPrivateUser());
  124. }
  125. }
  126. fileList.add(newFile);
  127. }
  128. }
  129. });
  130. if (!CollectionUtils.isEmpty(histories)) {
  131. historyService.saveOrUpdateBatch(histories);
  132. }
  133. flag = fileAndFolderService.saveOrUpdateBatch(fileList);
  134. }
  135. return flag;
  136. }
  137. @Override
  138. public boolean remove(List<Long> ids) {
  139. boolean flag = false;
  140. if (!CollectionUtils.isEmpty(ids)) {
  141. ids.forEach(id -> {
  142. FileAndFolder byId = fileAndFolderService.getById(id);
  143. //如果是文件夹
  144. if (Objects.equals(byId.getType(), TWO)) {
  145. List<FileAndFolder> children = fileAndFolderService.getChildren(null, id);
  146. if (!CollectionUtils.isEmpty(children)) {
  147. throw new ServiceException("删除文件夹失败:请先删除下级");
  148. }
  149. }
  150. });
  151. flag = fileAndFolderService.removeByIds(ids);
  152. }
  153. return flag;
  154. }
  155. @Override
  156. public boolean move(String ids, Long stageId, Long parentId) {
  157. boolean flag = false;
  158. if (StringUtil.isNotBlank(ids)) {
  159. List<FileAndFolder> list = new ArrayList<>();
  160. List<Long> idList = Func.toLongList(ids);
  161. idList.forEach(id -> {
  162. FileAndFolder byId = fileAndFolderService.getById(id);
  163. if (byId != null) {
  164. byId.setStageId(stageId);
  165. byId.setParentId(parentId);
  166. list.add(byId);
  167. }
  168. });
  169. flag = fileAndFolderService.updateBatchById(list);
  170. }
  171. return flag;
  172. }
  173. @Override
  174. public boolean delete(List<Long> ids) {
  175. boolean flag = false;
  176. Long deptId = Func.firstLong(AuthUtil.getDeptId());
  177. if (!CollectionUtils.isEmpty(ids)) {
  178. ids.forEach(id -> {
  179. InnerFileAndFolder byId = innerFileAndFolderService.getById(id);
  180. //如果是文件夹
  181. if (byId != null && Objects.equals(byId.getType(), TWO)) {
  182. if (!deptId.equals(byId.getDeptId())) {
  183. throw new ServiceException("非你部门下文件,禁止删除");
  184. }
  185. List<InnerFileAndFolder> children = innerFileAndFolderService.getChildren(byId.getDeptId(), id);
  186. if (!CollectionUtils.isEmpty(children)) {
  187. throw new ServiceException("删除文件夹失败:请先删除下级");
  188. }
  189. }
  190. });
  191. flag = innerFileAndFolderService.removeByIds(ids);
  192. }
  193. return flag;
  194. }
  195. @Override
  196. public boolean uploadInnerFile(List<UploadInnerFileDTO> files) {
  197. boolean flag = false;
  198. List<InnerFileAndFolder> fileList = new ArrayList<>();
  199. if (!CollectionUtils.isEmpty(files)) {
  200. Long parentId = files.get(0).getParentId();
  201. Long deptId = files.get(0).getDeptId();
  202. files.forEach(file -> {
  203. Long fileId = file.getBladeFileId();
  204. R<FileVO> rpc = fileClient.findById(fileId);
  205. if (rpc.isSuccess()) {
  206. InnerFileAndFolder newFile = new InnerFileAndFolder();
  207. newFile.setDeptId(deptId);
  208. newFile.setParentId(parentId);
  209. newFile.setBladeFileId(file.getBladeFileId());
  210. newFile.setType(1);
  211. newFile.setTitle(rpc.getData().getOriginalFileName());
  212. fileList.add(newFile);
  213. }
  214. });
  215. flag = innerFileAndFolderService.saveBatch(fileList);
  216. }
  217. return flag;
  218. }
  219. }