Commit 04a05af1c1d0f15758bf4cbe84d1bce4827d31f3
1 parent
9d5d8c95
fix(pms): 过滤知识库文档已删除标签并保留历史编号
Showing
5 changed files
with
127 additions
and
0 deletions
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 | @@ -16,6 +16,7 @@ import cn.iocoder.yudao.module.pms.dal.dataobject.kb.library.PmsKnowledgeLibrary | ||
| 16 | import cn.iocoder.yudao.module.pms.enums.kb.content.PmsKnowledgeContentLevelEnum; | 16 | import cn.iocoder.yudao.module.pms.enums.kb.content.PmsKnowledgeContentLevelEnum; |
| 17 | import cn.iocoder.yudao.module.pms.enums.kb.content.PmsKnowledgeDocumentTypeEnum; | 17 | import cn.iocoder.yudao.module.pms.enums.kb.content.PmsKnowledgeDocumentTypeEnum; |
| 18 | import cn.iocoder.yudao.module.pms.service.kb.content.PmsKnowledgeContentPermissionService; | 18 | import cn.iocoder.yudao.module.pms.service.kb.content.PmsKnowledgeContentPermissionService; |
| 19 | +import cn.iocoder.yudao.module.pms.service.kb.content.PmsKnowledgeDocumentLabelService; | ||
| 19 | import cn.iocoder.yudao.module.pms.service.kb.content.PmsKnowledgeDocumentService; | 20 | import cn.iocoder.yudao.module.pms.service.kb.content.PmsKnowledgeDocumentService; |
| 20 | import cn.iocoder.yudao.module.pms.service.kb.interaction.PmsKnowledgeDocumentLikeService; | 21 | import cn.iocoder.yudao.module.pms.service.kb.interaction.PmsKnowledgeDocumentLikeService; |
| 21 | import cn.iocoder.yudao.module.pms.service.kb.interaction.PmsKnowledgeFavoriteService; | 22 | import cn.iocoder.yudao.module.pms.service.kb.interaction.PmsKnowledgeFavoriteService; |
| @@ -47,6 +48,7 @@ import java.util.Set; | @@ -47,6 +48,7 @@ import java.util.Set; | ||
| 47 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; | 48 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; |
| 48 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; | 49 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; |
| 49 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; | 50 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; |
| 51 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList; | ||
| 50 | import static cn.iocoder.yudao.framework.common.util.collection.MapUtils.findAndThen; | 52 | import static cn.iocoder.yudao.framework.common.util.collection.MapUtils.findAndThen; |
| 51 | import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; | 53 | import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; |
| 52 | 54 | ||
| @@ -61,6 +63,8 @@ public class PmsKnowledgeDocumentController { | @@ -61,6 +63,8 @@ public class PmsKnowledgeDocumentController { | ||
| 61 | @Resource | 63 | @Resource |
| 62 | private PmsKnowledgeContentPermissionService contentPermissionService; | 64 | private PmsKnowledgeContentPermissionService contentPermissionService; |
| 63 | @Resource | 65 | @Resource |
| 66 | + private PmsKnowledgeDocumentLabelService documentLabelService; | ||
| 67 | + @Resource | ||
| 64 | private PmsKnowledgeFavoriteService favoriteService; | 68 | private PmsKnowledgeFavoriteService favoriteService; |
| 65 | @Resource | 69 | @Resource |
| 66 | private PmsKnowledgeDocumentLikeService documentLikeService; | 70 | private PmsKnowledgeDocumentLikeService documentLikeService; |
| @@ -150,6 +154,9 @@ public class PmsKnowledgeDocumentController { | @@ -150,6 +154,9 @@ public class PmsKnowledgeDocumentController { | ||
| 150 | && Boolean.FALSE.equals(documentVO.getDownloadStatus())) { | 154 | && Boolean.FALSE.equals(documentVO.getDownloadStatus())) { |
| 151 | documentVO.setContent(null); | 155 | documentVO.setContent(null); |
| 152 | } | 156 | } |
| 157 | + // 1.3 忽略已删除的标签编号,避免编辑回显后保存被拒:https://github.com/YunaiV/ruoyi-vue-pro/issues/1275 | ||
| 158 | + Set<Long> existLabelIds = documentLabelService.getDocumentLabelMap(document.getLabelIds()).keySet(); | ||
| 159 | + documentVO.setLabelIds(filterList(document.getLabelIds(), existLabelIds::contains)); | ||
| 153 | 160 | ||
| 154 | // 2. 一次查询并拼接创建人和点赞用户信息 | 161 | // 2. 一次查询并拼接创建人和点赞用户信息 |
| 155 | List<PmsKnowledgeDocumentLikeDO> likes = documentLikeService.getDocumentLikeList(document.getId()); | 162 | List<PmsKnowledgeDocumentLikeDO> likes = documentLikeService.getDocumentLikeList(document.getId()); |
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 | @@ -8,6 +8,9 @@ import cn.iocoder.yudao.module.pms.dal.dataobject.kb.content.PmsKnowledgeDocumen | ||
| 8 | 8 | ||
| 9 | import java.util.Collection; | 9 | import java.util.Collection; |
| 10 | import java.util.List; | 10 | import java.util.List; |
| 11 | +import java.util.Map; | ||
| 12 | + | ||
| 13 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; | ||
| 11 | 14 | ||
| 12 | /** | 15 | /** |
| 13 | * PMS 知识库文档标签 Service 接口 | 16 | * PMS 知识库文档标签 Service 接口 |
| @@ -54,6 +57,24 @@ public interface PmsKnowledgeDocumentLabelService { | @@ -54,6 +57,24 @@ public interface PmsKnowledgeDocumentLabelService { | ||
| 54 | List<PmsKnowledgeDocumentLabelDO> getDocumentLabelList(); | 57 | List<PmsKnowledgeDocumentLabelDO> getDocumentLabelList(); |
| 55 | 58 | ||
| 56 | /** | 59 | /** |
| 60 | + * 获得文档标签列表 | ||
| 61 | + * | ||
| 62 | + * @param ids 标签编号集合 | ||
| 63 | + * @return 标签列表 | ||
| 64 | + */ | ||
| 65 | + List<PmsKnowledgeDocumentLabelDO> getDocumentLabelList(Collection<Long> ids); | ||
| 66 | + | ||
| 67 | + /** | ||
| 68 | + * 获得文档标签 Map | ||
| 69 | + * | ||
| 70 | + * @param ids 标签编号集合 | ||
| 71 | + * @return 文档标签 Map | ||
| 72 | + */ | ||
| 73 | + default Map<Long, PmsKnowledgeDocumentLabelDO> getDocumentLabelMap(Collection<Long> ids) { | ||
| 74 | + return convertMap(getDocumentLabelList(ids), PmsKnowledgeDocumentLabelDO::getId); | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + /** | ||
| 57 | * 校验文档标签均存在 | 78 | * 校验文档标签均存在 |
| 58 | * | 79 | * |
| 59 | * @param ids 标签编号集合 | 80 | * @param ids 标签编号集合 |
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; | @@ -17,6 +17,7 @@ import org.springframework.stereotype.Service; | ||
| 17 | import org.springframework.validation.annotation.Validated; | 17 | import org.springframework.validation.annotation.Validated; |
| 18 | 18 | ||
| 19 | import java.util.Collection; | 19 | import java.util.Collection; |
| 20 | +import java.util.Collections; | ||
| 20 | import java.util.HashSet; | 21 | import java.util.HashSet; |
| 21 | import java.util.List; | 22 | import java.util.List; |
| 22 | import java.util.Set; | 23 | import java.util.Set; |
| @@ -82,6 +83,14 @@ public class PmsKnowledgeDocumentLabelServiceImpl implements PmsKnowledgeDocumen | @@ -82,6 +83,14 @@ public class PmsKnowledgeDocumentLabelServiceImpl implements PmsKnowledgeDocumen | ||
| 82 | } | 83 | } |
| 83 | 84 | ||
| 84 | @Override | 85 | @Override |
| 86 | + public List<PmsKnowledgeDocumentLabelDO> getDocumentLabelList(Collection<Long> ids) { | ||
| 87 | + if (CollUtil.isEmpty(ids)) { | ||
| 88 | + return Collections.emptyList(); | ||
| 89 | + } | ||
| 90 | + return documentLabelMapper.selectByIds(ids); | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + @Override | ||
| 85 | public void validateDocumentLabelList(Collection<Long> ids) { | 94 | public void validateDocumentLabelList(Collection<Long> ids) { |
| 86 | if (CollUtil.isEmpty(ids)) { | 95 | if (CollUtil.isEmpty(ids)) { |
| 87 | return; | 96 | return; |
yudao-module-pms/src/test/java/cn/iocoder/yudao/module/pms/service/kb/content/PmsKnowledgeDocumentLabelServiceImplTest.java
| @@ -21,6 +21,7 @@ import java.util.Arrays; | @@ -21,6 +21,7 @@ import java.util.Arrays; | ||
| 21 | import java.util.Collections; | 21 | import java.util.Collections; |
| 22 | import java.util.HashSet; | 22 | import java.util.HashSet; |
| 23 | import java.util.List; | 23 | import java.util.List; |
| 24 | +import java.util.Map; | ||
| 24 | 25 | ||
| 25 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; | 26 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; |
| 26 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId; | 27 | 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; | @@ -28,6 +29,7 @@ import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; | ||
| 28 | import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstants.KNOWLEDGE_DOCUMENT_LABEL_NOT_EXISTS; | 29 | import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstants.KNOWLEDGE_DOCUMENT_LABEL_NOT_EXISTS; |
| 29 | import static org.junit.jupiter.api.Assertions.assertEquals; | 30 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 30 | import static org.junit.jupiter.api.Assertions.assertNull; | 31 | import static org.junit.jupiter.api.Assertions.assertNull; |
| 32 | +import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| 31 | import static org.mockito.Mockito.when; | 33 | import static org.mockito.Mockito.when; |
| 32 | 34 | ||
| 33 | /** | 35 | /** |
| @@ -91,6 +93,68 @@ public class PmsKnowledgeDocumentLabelServiceImplTest extends BaseDbUnitTest { | @@ -91,6 +93,68 @@ public class PmsKnowledgeDocumentLabelServiceImplTest extends BaseDbUnitTest { | ||
| 91 | } | 93 | } |
| 92 | 94 | ||
| 93 | @Test | 95 | @Test |
| 96 | + public void testDeleteDocumentLabel_keepDocumentLabelIds() { | ||
| 97 | + // mock 数据 | ||
| 98 | + PmsKnowledgeDocumentLabelDO deletedLabel = randomDocumentLabelDO(); | ||
| 99 | + documentLabelMapper.insert(deletedLabel); | ||
| 100 | + PmsKnowledgeDocumentLabelDO otherLabel = randomDocumentLabelDO(); | ||
| 101 | + documentLabelMapper.insert(otherLabel); | ||
| 102 | + PmsKnowledgeDocumentDO document = randomDocumentDO(randomLongId(), | ||
| 103 | + Arrays.asList(deletedLabel.getId(), otherLabel.getId())); | ||
| 104 | + documentMapper.insert(document); | ||
| 105 | + | ||
| 106 | + // 调用 | ||
| 107 | + documentLabelService.deleteDocumentLabel(deletedLabel.getId()); | ||
| 108 | + | ||
| 109 | + // 断言:删除标签不改写文档,历史编号在详情读取时忽略 | ||
| 110 | + assertEquals(Arrays.asList(deletedLabel.getId(), otherLabel.getId()), | ||
| 111 | + documentMapper.selectById(document.getId()).getLabelIds()); | ||
| 112 | + } | ||
| 113 | + | ||
| 114 | + @Test | ||
| 115 | + public void testGetDocumentLabelList_ignoreDeleted() { | ||
| 116 | + // mock 数据 | ||
| 117 | + PmsKnowledgeDocumentLabelDO label = randomDocumentLabelDO(); | ||
| 118 | + documentLabelMapper.insert(label); | ||
| 119 | + PmsKnowledgeDocumentLabelDO deletedLabel = randomDocumentLabelDO(); | ||
| 120 | + documentLabelMapper.insert(deletedLabel); | ||
| 121 | + documentLabelMapper.deleteById(deletedLabel.getId()); | ||
| 122 | + | ||
| 123 | + // 调用 | ||
| 124 | + List<PmsKnowledgeDocumentLabelDO> list = documentLabelService.getDocumentLabelList( | ||
| 125 | + Arrays.asList(label.getId(), deletedLabel.getId())); | ||
| 126 | + Map<Long, PmsKnowledgeDocumentLabelDO> map = documentLabelService.getDocumentLabelMap( | ||
| 127 | + Arrays.asList(label.getId(), deletedLabel.getId())); | ||
| 128 | + | ||
| 129 | + // 断言 | ||
| 130 | + assertEquals(1, list.size()); | ||
| 131 | + assertEquals(label.getId(), list.get(0).getId()); | ||
| 132 | + assertEquals(Collections.singleton(label.getId()), map.keySet()); | ||
| 133 | + } | ||
| 134 | + | ||
| 135 | + @Test | ||
| 136 | + public void testGetDocumentLabelList_empty() { | ||
| 137 | + // 调用,并断言 | ||
| 138 | + assertTrue(documentLabelService.getDocumentLabelList(null).isEmpty()); | ||
| 139 | + assertTrue(documentLabelService.getDocumentLabelList(Collections.emptyList()).isEmpty()); | ||
| 140 | + assertTrue(documentLabelService.getDocumentLabelMap(null).isEmpty()); | ||
| 141 | + } | ||
| 142 | + | ||
| 143 | + @Test | ||
| 144 | + public void testValidateDocumentLabelList_deleted() { | ||
| 145 | + // mock 数据 | ||
| 146 | + PmsKnowledgeDocumentLabelDO label = randomDocumentLabelDO(); | ||
| 147 | + documentLabelMapper.insert(label); | ||
| 148 | + PmsKnowledgeDocumentLabelDO deletedLabel = randomDocumentLabelDO(); | ||
| 149 | + documentLabelMapper.insert(deletedLabel); | ||
| 150 | + documentLabelMapper.deleteById(deletedLabel.getId()); | ||
| 151 | + | ||
| 152 | + // 调用,并断言异常 | ||
| 153 | + assertServiceException(() -> documentLabelService.validateDocumentLabelList( | ||
| 154 | + Arrays.asList(label.getId(), deletedLabel.getId())), KNOWLEDGE_DOCUMENT_LABEL_NOT_EXISTS); | ||
| 155 | + } | ||
| 156 | + | ||
| 157 | + @Test | ||
| 94 | public void testGetDocumentLabel_success() { | 158 | public void testGetDocumentLabel_success() { |
| 95 | // mock 数据 | 159 | // mock 数据 |
| 96 | PmsKnowledgeDocumentLabelDO documentLabel = randomDocumentLabelDO(); | 160 | PmsKnowledgeDocumentLabelDO documentLabel = randomDocumentLabelDO(); |
yudao-module-pms/src/test/java/cn/iocoder/yudao/module/pms/service/kb/content/PmsKnowledgeDocumentServiceImplTest.java
| @@ -37,9 +37,11 @@ import java.util.Arrays; | @@ -37,9 +37,11 @@ import java.util.Arrays; | ||
| 37 | import java.util.Collections; | 37 | import java.util.Collections; |
| 38 | import java.util.Map; | 38 | import java.util.Map; |
| 39 | 39 | ||
| 40 | +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | ||
| 40 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; | 41 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; |
| 41 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId; | 42 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId; |
| 42 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; | 43 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; |
| 44 | +import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstants.KNOWLEDGE_DOCUMENT_LABEL_NOT_EXISTS; | ||
| 43 | import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstants.KNOWLEDGE_DOCUMENT_MOVE_INVALID; | 45 | import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstants.KNOWLEDGE_DOCUMENT_MOVE_INVALID; |
| 44 | import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstants.KNOWLEDGE_DOCUMENT_MOVE_TARGET_INVALID; | 46 | import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstants.KNOWLEDGE_DOCUMENT_MOVE_TARGET_INVALID; |
| 45 | import static cn.iocoder.yudao.module.pms.enums.ErrorCodeConstants.KNOWLEDGE_DOCUMENT_NOT_EXISTS; | 47 | 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; | @@ -47,6 +49,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| 47 | import static org.junit.jupiter.api.Assertions.assertTrue; | 49 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| 48 | import static org.mockito.ArgumentMatchers.anyCollection; | 50 | import static org.mockito.ArgumentMatchers.anyCollection; |
| 49 | import static org.mockito.ArgumentMatchers.eq; | 51 | import static org.mockito.ArgumentMatchers.eq; |
| 52 | +import static org.mockito.Mockito.doThrow; | ||
| 50 | import static org.mockito.Mockito.verify; | 53 | import static org.mockito.Mockito.verify; |
| 51 | import static org.mockito.Mockito.when; | 54 | import static org.mockito.Mockito.when; |
| 52 | 55 | ||
| @@ -205,6 +208,29 @@ public class PmsKnowledgeDocumentServiceImplTest extends BaseDbUnitTest { | @@ -205,6 +208,29 @@ public class PmsKnowledgeDocumentServiceImplTest extends BaseDbUnitTest { | ||
| 205 | } | 208 | } |
| 206 | 209 | ||
| 207 | @Test | 210 | @Test |
| 211 | + public void testUpdateDocument_labelNotExists() { | ||
| 212 | + // mock 数据:文档关联的旧标签已被删除 | ||
| 213 | + Long userId = randomLongId(); | ||
| 214 | + PmsKnowledgeLibraryDO library = randomLibraryDO(); | ||
| 215 | + libraryMapper.insert(library); | ||
| 216 | + PmsKnowledgeDocumentDO document = randomDocumentDO(library.getId(), 0L, 0L) | ||
| 217 | + .setContent("旧正文").setLabelIds(Arrays.asList(1L, 2L)); | ||
| 218 | + documentMapper.insert(document); | ||
| 219 | + doThrow(exception(KNOWLEDGE_DOCUMENT_LABEL_NOT_EXISTS)).when(documentLabelService) | ||
| 220 | + .validateDocumentLabelList(eq(Arrays.asList(1L, 2L))); | ||
| 221 | + // 准备参数:原样提交旧标签 | ||
| 222 | + PmsKnowledgeDocumentUpdateReqVO reqVO = new PmsKnowledgeDocumentUpdateReqVO() | ||
| 223 | + .setId(document.getId()).setContent("新正文").setLabelIds(Arrays.asList(1L, 2L)); | ||
| 224 | + | ||
| 225 | + // 调用,并断言异常 | ||
| 226 | + assertServiceException(() -> documentService.updateDocument(reqVO, userId), KNOWLEDGE_DOCUMENT_LABEL_NOT_EXISTS); | ||
| 227 | + // 断言:文档未被修改 | ||
| 228 | + PmsKnowledgeDocumentDO dbDocument = documentMapper.selectById(document.getId()); | ||
| 229 | + assertEquals("旧正文", dbDocument.getContent()); | ||
| 230 | + assertEquals(Arrays.asList(1L, 2L), dbDocument.getLabelIds()); | ||
| 231 | + } | ||
| 232 | + | ||
| 233 | + @Test | ||
| 208 | public void testDeleteDocument_recursive() { | 234 | public void testDeleteDocument_recursive() { |
| 209 | // mock 数据 | 235 | // mock 数据 |
| 210 | Long userId = randomLongId(); | 236 | Long userId = randomLongId(); |