本文最后更新于 2026年5月29日 下午
删除通知接口的实现
删除通知接口的需求
检查通知是否存在
找出不存在的 通知id
遍历每个通知ID,将相关图片移动到deleted文件夹,并同步删除文件表记录
查找需要移动的图片URL并去重
批量移动图片到 deleted 文件夹
同步删除文件表记录
逻辑删除通知(将 del_flag 从 0 > 1)
代码实现
在 NoticeController 中添加接口
1 2 3 4 5 6 7 8 9
| @DeleteMapping("/{ids}") @PreAuthorize("@ps.hasPermission('sys:notice:delete')") @SystemLog(businessName = "删除通知(逻辑删除)") @ApiOperation(value = "删除通知接口(批量逻辑删除)", notes = "删除通知,支持批量删除", response = String.class) @ApiImplicitParam(name = "ids", value = "通知id", dataType = "Long", paramType = "path", required = true) public ResponseResult deleteNotice(@PathVariable Long[] ids) { return sysNoticeService.deleteNotice(ids); }
|
在 NoticeServiceImpl 中
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
| @Override public ResponseResult deleteNotice(Long[] ids) { if (ids == null || ids.length == 0) { return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "请选择要删除的通知"); }
List<Long> idList = List.of(ids);
List<SysNotice> noticesToDelete = sysNoticeService.listByIds(idList); if (noticesToDelete.size() != ids.length) { Set<Long> existIds = noticesToDelete.stream() .map(SysNotice::getId) .collect(Collectors.toSet());
List<Long> notExistIds = idList.stream() .filter(id -> !existIds.contains(id)) .toList();
return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "删除失败!!原因:以下通知 id 不存在:" + notExistIds); }
for (Long noticeId : idList) { SysNotice notice = sysNoticeService.getById(noticeId);
Set<String> imageUrlSet = new LinkedHashSet<>(); if (StringUtils.hasText(notice.getContent())) { List<String> contentImages = ImageUrlUtils.extractImageUrls(notice.getContent()); imageUrlSet.addAll(contentImages); log.info("通知ID: {} 内容中提取到的图片URL: {}", noticeId, contentImages); }
List<String> imageUrls = new ArrayList<>(imageUrlSet); log.info("通知ID: {} 去重后的图片URL: {}", noticeId, imageUrls);
if (!imageUrls.isEmpty()) { int movedCount = ossFileService.batchMoveFilesToDeleted(imageUrls); log.info("通知ID: {} 的图片已移动到deleted文件夹,成功: {}/{}", noticeId, movedCount, imageUrls.size());
for (String imageUrl : imageUrls) { deleteFileRecordByUrl(imageUrl); } log.info("文章ID: {} 的图片文件表记录已删除,共 {} 条", noticeId, imageUrls.size()); } }
sysNoticeService.removeByIds(idList); log.info("批量逻辑删除通知成功,ID列表: {}", idList);
return ResponseResult.okResult(); }
|
PS:该系列只做为作者学习开发项目做的笔记用
不一定符合读者来学习,仅供参考
预告
后续会记录博客的开发过程
每次学习会做一份笔记来进行发表
“一花一世界,一叶一菩提”
版权所有 © 2026 云梦泽
欢迎访问我的个人网站:https://hgt12.github.io/