From 04a05af1c1d0f15758bf4cbe84d1bce4827d31f3 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Fri, 23 Oct 2026 01:14:20 +0800 Subject: [PATCH] fix(pms): 过滤知识库文档已删除标签并保留历史编号 --- yudao-module-pms/src/main/java/cn/iocoder/yudao/module/pms/controller/admin/kb/content/PmsKnowledgeDocumentController.java | 7 +++++++ yudao-module-pms/src/main/java/cn/iocoder/yudao/module/pms/service/kb/content/PmsKnowledgeDocumentLabelService.java | 21 +++++++++++++++++++++ yudao-module-pms/src/main/java/cn/iocoder/yudao/module/pms/service/kb/content/PmsKnowledgeDocumentLabelServiceImpl.java | 9 +++++++++ yudao-module-pms/src/test/java/cn/iocoder/yudao/module/pms/service/kb/content/PmsKnowledgeDocumentLabelServiceImplTest.java | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ yudao-module-pms/src/test/java/cn/iocoder/yudao/module/pms/service/kb/content/PmsKnowledgeDocumentServiceImplTest.java | 26 ++++++++++++++++++++++++++ 5 files changed, 127 insertions(+), 0 deletions(-) diff --git a/yudao-module-pms/src/main/java/cn/iocoder/yudao/module/pms/controller/admin/kb/content/PmsKnowledgeDocumentController.java b/yudao-module-pms/src/main/java/cn/iocoder/yudao/module/pms/controller/admin/kb/content/PmsKnowledgeDocumentController.java index aca906c..8566184 100644 --- a/yudao-module-pms/src/main/java/cn/iocoder/yudao/module/pms/controller/admin/kb/content/PmsKnowledgeDocumentController.java +++ b/yudao-module-pms/src/main/java/cn/iocoder/yudao/module/pms/controller/admin/kb/content/PmsKnowledgeDocumentController.java @@ -16,6 +16,7 @@ import cn.iocoder.yudao.module.pms.dal.dataobject.kb.library.PmsKnowledgeLibrary import cn.iocoder.yudao.module.pms.enums.kb.content.PmsKnowledgeContentLevelEnum; import cn.iocoder.yudao.module.pms.enums.kb.content.PmsKnowledgeDocumentTypeEnum; import cn.iocoder.yudao.module.pms.service.kb.content.PmsKnowledgeContentPermissionService; +import cn.iocoder.yudao.module.pms.service.kb.content.PmsKnowledgeDocumentLabelService; import cn.iocoder.yudao.module.pms.service.kb.content.PmsKnowledgeDocumentService; import cn.iocoder.yudao.module.pms.service.kb.interaction.PmsKnowledgeDocumentLikeService; import cn.iocoder.yudao.module.pms.service.kb.interaction.PmsKnowledgeFavoriteService; @@ -47,6 +48,7 @@ import java.util.Set; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList; import static cn.iocoder.yudao.framework.common.util.collection.MapUtils.findAndThen; import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; @@ -61,6 +63,8 @@ public class PmsKnowledgeDocumentController { @Resource private PmsKnowledgeContentPermissionService contentPermissionService; @Resource + private PmsKnowledgeDocumentLabelService documentLabelService; + @Resource private PmsKnowledgeFavoriteService favoriteService; @Resource private PmsKnowledgeDocumentLikeService documentLikeService; @@ -150,6 +154,9 @@ public class PmsKnowledgeDocumentController { && Boolean.FALSE.equals(documentVO.getDownloadStatus())) { documentVO.setContent(null); } + // 1.3 忽略已删除的标签编号,避免编辑回显后保存被拒:https://github.com/YunaiV/ruoyi-vue-pro/issues/1275 + Set existLabelIds = documentLabelService.getDocumentLabelMap(document.getLabelIds()).keySet(); + documentVO.setLabelIds(filterList(document.getLabelIds(), existLabelIds::contains)); // 2. 一次查询并拼接创建人和点赞用户信息 List likes = documentLikeService.getDocumentLikeList(document.getId()); diff --git a/yudao-module-pms/src/main/java/cn/iocoder/yudao/module/pms/service/kb/content/PmsKnowledgeDocumentLabelService.java b/yudao-module-pms/src/main/java/cn/iocoder/yudao/module/pms/service/kb/content/PmsKnowledgeDocumentLabelService.java index 3f7e9e6..af55103 100644 --- a/yudao-module-pms/src/main/java/cn/iocoder/yudao/module/pms/service/kb/content/PmsKnowledgeDocumentLabelService.java +++ b/yudao-module-pms/src/main/java/cn/iocoder/yudao/module/pms/service/kb/content/PmsKnowledgeDocumentLabelService.java @@ -8,6 +8,9 @@ import cn.iocoder.yudao.module.pms.dal.dataobject.kb.content.PmsKnowledgeDocumen import java.util.Collection; import java.util.List; +import java.util.Map; + +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; /** * PMS 知识库文档标签 Service 接口 @@ -54,6 +57,24 @@ public interface PmsKnowledgeDocumentLabelService { List getDocumentLabelList(); /** + * 获得文档标签列表 + * + * @param ids 标签编号集合 + * @return 标签列表 + */ + List getDocumentLabelList(Collection ids); + + /** + * 获得文档标签 Map + * + * @param ids 标签编号集合 + * @return 文档标签 Map + */ + default Map getDocumentLabelMap(Collection ids) { + return convertMap(getDocumentLabelList(ids), PmsKnowledgeDocumentLabelDO::getId); + } + + /** * 校验文档标签均存在 * * @param ids 标签编号集合 diff --git a/yudao-module-pms/src/main/java/cn/iocoder/yudao/module/pms/service/kb/content/PmsKnowledgeDocumentLabelServiceImpl.java b/yudao-module-pms/src/main/java/cn/iocoder/yudao/module/pms/service/kb/content/PmsKnowledgeDocumentLabelServiceImpl.java index 4a91d81..6ac6700 100644 --- a/yudao-module-pms/src/main/java/cn/iocoder/yudao/module/pms/service/kb/content/PmsKnowledgeDocumentLabelServiceImpl.java +++ b/yudao-module-pms/src/main/java/cn/iocoder/yudao/module/pms/service/kb/content/PmsKnowledgeDocumentLabelServiceImpl.java @@ -17,6 +17,7 @@ import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; import java.util.Collection; +import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -82,6 +83,14 @@ public class PmsKnowledgeDocumentLabelServiceImpl implements PmsKnowledgeDocumen } @Override + public List getDocumentLabelList(Collection ids) { + if (CollUtil.isEmpty(ids)) { + return Collections.emptyList(); + } + return documentLabelMapper.selectByIds(ids); + } + + @Override public void validateDocumentLabelList(Collection ids) { if (CollUtil.isEmpty(ids)) { return; diff --git a/yudao-module-pms/src/test/java/cn/iocoder/yudao/module/pms/service/kb/content/PmsKnowledgeDocumentLabelServiceImplTest.java b/yudao-module-pms/src/test/java/cn/iocoder/yudao/module/pms/service/kb/content/PmsKnowledgeDocumentLabelServiceImplTest.java index daa10a7..2899c03 100644 --- a/yudao-module-pms/src/test/java/cn/iocoder/yudao/module/pms/service/kb/content/PmsKnowledgeDocumentLabelServiceImplTest.java +++ b/yudao-module-pms/src/test/java/cn/iocoder/yudao/module/pms/service/kb/content/PmsKnowledgeDocumentLabelServiceImplTest.java @@ -21,6 +21,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.List; +import java.util.Map; import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId; @@ -28,6 +29,7 @@ import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstants.KNOWLEDGE_DOCUMENT_LABEL_NOT_EXISTS; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.when; /** @@ -91,6 +93,68 @@ public class PmsKnowledgeDocumentLabelServiceImplTest extends BaseDbUnitTest { } @Test + public void testDeleteDocumentLabel_keepDocumentLabelIds() { + // mock 数据 + PmsKnowledgeDocumentLabelDO deletedLabel = randomDocumentLabelDO(); + documentLabelMapper.insert(deletedLabel); + PmsKnowledgeDocumentLabelDO otherLabel = randomDocumentLabelDO(); + documentLabelMapper.insert(otherLabel); + PmsKnowledgeDocumentDO document = randomDocumentDO(randomLongId(), + Arrays.asList(deletedLabel.getId(), otherLabel.getId())); + documentMapper.insert(document); + + // 调用 + documentLabelService.deleteDocumentLabel(deletedLabel.getId()); + + // 断言:删除标签不改写文档,历史编号在详情读取时忽略 + assertEquals(Arrays.asList(deletedLabel.getId(), otherLabel.getId()), + documentMapper.selectById(document.getId()).getLabelIds()); + } + + @Test + public void testGetDocumentLabelList_ignoreDeleted() { + // mock 数据 + PmsKnowledgeDocumentLabelDO label = randomDocumentLabelDO(); + documentLabelMapper.insert(label); + PmsKnowledgeDocumentLabelDO deletedLabel = randomDocumentLabelDO(); + documentLabelMapper.insert(deletedLabel); + documentLabelMapper.deleteById(deletedLabel.getId()); + + // 调用 + List list = documentLabelService.getDocumentLabelList( + Arrays.asList(label.getId(), deletedLabel.getId())); + Map map = documentLabelService.getDocumentLabelMap( + Arrays.asList(label.getId(), deletedLabel.getId())); + + // 断言 + assertEquals(1, list.size()); + assertEquals(label.getId(), list.get(0).getId()); + assertEquals(Collections.singleton(label.getId()), map.keySet()); + } + + @Test + public void testGetDocumentLabelList_empty() { + // 调用,并断言 + assertTrue(documentLabelService.getDocumentLabelList(null).isEmpty()); + assertTrue(documentLabelService.getDocumentLabelList(Collections.emptyList()).isEmpty()); + assertTrue(documentLabelService.getDocumentLabelMap(null).isEmpty()); + } + + @Test + public void testValidateDocumentLabelList_deleted() { + // mock 数据 + PmsKnowledgeDocumentLabelDO label = randomDocumentLabelDO(); + documentLabelMapper.insert(label); + PmsKnowledgeDocumentLabelDO deletedLabel = randomDocumentLabelDO(); + documentLabelMapper.insert(deletedLabel); + documentLabelMapper.deleteById(deletedLabel.getId()); + + // 调用,并断言异常 + assertServiceException(() -> documentLabelService.validateDocumentLabelList( + Arrays.asList(label.getId(), deletedLabel.getId())), KNOWLEDGE_DOCUMENT_LABEL_NOT_EXISTS); + } + + @Test public void testGetDocumentLabel_success() { // mock 数据 PmsKnowledgeDocumentLabelDO documentLabel = randomDocumentLabelDO(); diff --git a/yudao-module-pms/src/test/java/cn/iocoder/yudao/module/pms/service/kb/content/PmsKnowledgeDocumentServiceImplTest.java b/yudao-module-pms/src/test/java/cn/iocoder/yudao/module/pms/service/kb/content/PmsKnowledgeDocumentServiceImplTest.java index 08bb986..565c38d 100644 --- a/yudao-module-pms/src/test/java/cn/iocoder/yudao/module/pms/service/kb/content/PmsKnowledgeDocumentServiceImplTest.java +++ b/yudao-module-pms/src/test/java/cn/iocoder/yudao/module/pms/service/kb/content/PmsKnowledgeDocumentServiceImplTest.java @@ -37,9 +37,11 @@ import java.util.Arrays; import java.util.Collections; import java.util.Map; +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId; import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; +import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstants.KNOWLEDGE_DOCUMENT_LABEL_NOT_EXISTS; import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstants.KNOWLEDGE_DOCUMENT_MOVE_INVALID; import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstants.KNOWLEDGE_DOCUMENT_MOVE_TARGET_INVALID; import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstants.KNOWLEDGE_DOCUMENT_NOT_EXISTS; @@ -47,6 +49,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.anyCollection; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -205,6 +208,29 @@ public class PmsKnowledgeDocumentServiceImplTest extends BaseDbUnitTest { } @Test + public void testUpdateDocument_labelNotExists() { + // mock 数据:文档关联的旧标签已被删除 + Long userId = randomLongId(); + PmsKnowledgeLibraryDO library = randomLibraryDO(); + libraryMapper.insert(library); + PmsKnowledgeDocumentDO document = randomDocumentDO(library.getId(), 0L, 0L) + .setContent("旧正文").setLabelIds(Arrays.asList(1L, 2L)); + documentMapper.insert(document); + doThrow(exception(KNOWLEDGE_DOCUMENT_LABEL_NOT_EXISTS)).when(documentLabelService) + .validateDocumentLabelList(eq(Arrays.asList(1L, 2L))); + // 准备参数:原样提交旧标签 + PmsKnowledgeDocumentUpdateReqVO reqVO = new PmsKnowledgeDocumentUpdateReqVO() + .setId(document.getId()).setContent("新正文").setLabelIds(Arrays.asList(1L, 2L)); + + // 调用,并断言异常 + assertServiceException(() -> documentService.updateDocument(reqVO, userId), KNOWLEDGE_DOCUMENT_LABEL_NOT_EXISTS); + // 断言:文档未被修改 + PmsKnowledgeDocumentDO dbDocument = documentMapper.selectById(document.getId()); + assertEquals("旧正文", dbDocument.getContent()); + assertEquals(Arrays.asList(1L, 2L), dbDocument.getLabelIds()); + } + + @Test public void testDeleteDocument_recursive() { // mock 数据 Long userId = randomLongId(); -- libgit2 0.21.4