1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
| @Override @Transactional public ResponseResult deleteFile(Long[] ids) { if (ids == null || ids.length == 0) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "请选择要删除的文件"); }
List<Long> idList = List.of(ids); List<SysFile> filesToDelete = listByIds(idList);
if (filesToDelete.size() != idList.size()) { Set<Long> existIds = filesToDelete.stream() .map(SysFile::getId) .collect(Collectors.toSet());
List<Long> notExistIds = idList.stream() .filter(id -> !existIds.contains(id)) .collect(Collectors.toList());
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "删除失败,以下文件ID不存在:" + notExistIds); }
List<String> fileUrls = filesToDelete.stream() .map(SysFile::getUrl) .filter(StringUtils::hasText) .collect(Collectors.toList());
Map<String, List<Article>> fileArticleUsageMap = new HashMap<>(); Map<String, List<SysNotice>> fileNoticeUsageMap = new HashMap<>(); if (!fileUrls.isEmpty()) { List<Article> relatedArticles = articleService.list( Wrappers.<Article>lambdaQuery() .select(Article::getId, Article::getTitle, Article::getThumbnail, Article::getContent) .and(wrapper -> { wrapper.in(Article::getThumbnail, fileUrls); for (String fileUrl : fileUrls) { wrapper.or().like(Article::getContent, fileUrl); } }) );
for (Article article : relatedArticles) { if (StringUtils.hasText(article.getThumbnail()) && fileUrls.contains(article.getThumbnail())) { fileArticleUsageMap.computeIfAbsent(article.getThumbnail(), key -> new ArrayList<>()) .add(article); }
List<String> contentImages = ImageUrlUtils.extractImageUrls(article.getContent()); if (!contentImages.isEmpty()) { for (String imageUrl : contentImages) { if (StringUtils.hasText(imageUrl) && fileUrls.contains(imageUrl)) { fileArticleUsageMap.computeIfAbsent(imageUrl, key -> new ArrayList<>()) .add(article); } } } }
List<SysNotice> relatedNotices = sysNoticeService.list( Wrappers.<SysNotice>lambdaQuery() .select(SysNotice::getId, SysNotice::getTitle, SysNotice::getContent) .and(wrapper -> { for (String fileUrl : fileUrls) { wrapper.or().like(SysNotice::getContent, fileUrl); } }) );
for (SysNotice notice : relatedNotices) { List<String> contentImages = ImageUrlUtils.extractImageUrls(notice.getContent()); if (!contentImages.isEmpty()) { for (String imageUrl : contentImages) { if (StringUtils.hasText(imageUrl) && fileUrls.contains(imageUrl)) { fileNoticeUsageMap.computeIfAbsent(imageUrl, key -> new ArrayList<>()) .add(notice); } } } } }
List<String> errorMsg = new ArrayList<>(); for (SysFile file : filesToDelete) { String fileUrl = file.getUrl();
List<Article> usedArticles = fileArticleUsageMap.get(fileUrl); if (usedArticles != null && !usedArticles.isEmpty()) { String articleNames = usedArticles.stream() .map(Article::getTitle) .filter(StringUtils::hasText) .distinct() .collect(Collectors.joining("、")); if (!StringUtils.hasText(articleNames)) { articleNames = usedArticles.stream() .map(article -> String.valueOf(article.getId())) .distinct() .collect(Collectors.joining("、")); } errorMsg.add("【" + file.getName() + "】正在被文章【" + articleNames + "】使用"); }
List<SysNotice> usedNotices = fileNoticeUsageMap.get(fileUrl); if (usedNotices != null && !usedNotices.isEmpty()) { String noticeNames = usedNotices.stream() .map(SysNotice::getTitle) .filter(StringUtils::hasText) .distinct() .collect(Collectors.joining("、")); if (!StringUtils.hasText(noticeNames)) { noticeNames = usedNotices.stream() .map(notice -> String.valueOf(notice.getId())) .distinct() .collect(Collectors.joining("、")); } errorMsg.add("【" + file.getName() + "】正在被通知【" + noticeNames + "】内容使用"); }
SysUser user = sysUserService.getOne( Wrappers.<SysUser>lambdaQuery() .eq(SysUser::getAvatar, fileUrl) .select(SysUser::getUsername) );
if (user != null && StringUtils.hasText(user.getUsername())) { errorMsg.add("【" + file.getName() + "】正在被用户【" + user.getUsername() + "】使用"); } }
if (!errorMsg.isEmpty()) { String msg = String.join(";", errorMsg); return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, msg + ",无法删除"); }
boolean removed = removeByIds(idList); if (!removed) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "文件删除失败"); }
for (SysFile file : filesToDelete) { if (file != null && StringUtils.hasText(file.getUrl())) { ossFileService.moveFileToDeleted(file.getUrl()); } }
return ResponseResult.okResult(); }
|