Commit db5c29c1 authored by wujipeng's avatar wujipeng
Browse files

管理员-题库管理-理论题库-优化

parent ad868040
package com.platform.business.model.vo;
import lombok.Data;
/**
* 管理端试题所属层级路径中的轻量节点。
*
* <p>该对象只暴露试题回显和级联定位所需的稳定字段,不携带树接口中的题量、
* 子节点、状态及审计信息,避免分页响应因复用完整树节点对象而产生冗余数据。</p>
*/
@Data
public class TheoreticalLibraryPathNodeVO {
/** 层级节点唯一标识,前端可据此定位或回显级联选项。 */
private String id;
/** 直接上级节点标识;根节点固定为字符串 {@code 0}。 */
private String parentId;
/** 稳定节点类型代码,例如 CRAFT、LEVEL、CHAPTER、DIRECTION。 */
private String nodeType;
/** 当前层级节点的展示名称。 */
private String name;
/** 当前层级节点的业务编码。 */
private String code;
}
...@@ -5,6 +5,7 @@ import com.platform.business.model.entity.TheoreticalLibraryEntity; ...@@ -5,6 +5,7 @@ import com.platform.business.model.entity.TheoreticalLibraryEntity;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import java.util.ArrayList;
import java.util.List; import java.util.List;
/** 管理端试题列表与详情响应。 */ /** 管理端试题列表与详情响应。 */
...@@ -17,6 +18,11 @@ public class TheoreticalLibraryQuestionVO extends TheoreticalLibraryEntity { ...@@ -17,6 +18,11 @@ public class TheoreticalLibraryQuestionVO extends TheoreticalLibraryEntity {
private String typeLabel; private String typeLabel;
private String difficultyLabel; private String difficultyLabel;
private String fullPath; private String fullPath;
/**
* 试题所属的完整结构化层级路径,按根节点到末级节点排列并包含末级节点本身。
* JOB 为工艺→职级→章→节→目→点,COMP 为竞赛类型→题目分类→题目方向。
*/
private List<TheoreticalLibraryPathNodeVO> nodePath = new ArrayList<>();
private Boolean noExam; private Boolean noExam;
private String answerText; private String answerText;
private List<String> keywordList; private List<String> keywordList;
......
...@@ -21,6 +21,7 @@ import com.platform.business.model.entity.TheoreticalLibraryNodeEntity; ...@@ -21,6 +21,7 @@ import com.platform.business.model.entity.TheoreticalLibraryNodeEntity;
import com.platform.business.model.entity.UserEntity; import com.platform.business.model.entity.UserEntity;
import com.platform.business.model.vo.TheoreticalLibraryNodeCountVO; import com.platform.business.model.vo.TheoreticalLibraryNodeCountVO;
import com.platform.business.model.vo.TheoreticalLibraryNodeVO; import com.platform.business.model.vo.TheoreticalLibraryNodeVO;
import com.platform.business.model.vo.TheoreticalLibraryPathNodeVO;
import com.platform.business.model.vo.TheoreticalLibraryQuestionVO; import com.platform.business.model.vo.TheoreticalLibraryQuestionVO;
import com.platform.business.service.TheoreticalLibraryAdminService; import com.platform.business.service.TheoreticalLibraryAdminService;
import com.platform.exception.BusinessException; import com.platform.exception.BusinessException;
...@@ -252,7 +253,7 @@ public class TheoreticalLibraryAdminServiceImpl implements TheoreticalLibraryAdm ...@@ -252,7 +253,7 @@ public class TheoreticalLibraryAdminServiceImpl implements TheoreticalLibraryAdm
if (rows == null) { if (rows == null) {
return Collections.emptyList(); return Collections.emptyList();
} }
rows.forEach(this::decorateQuestion); decorateQuestions(rows);
return rows; return rows;
} }
...@@ -272,7 +273,7 @@ public class TheoreticalLibraryAdminServiceImpl implements TheoreticalLibraryAdm ...@@ -272,7 +273,7 @@ public class TheoreticalLibraryAdminServiceImpl implements TheoreticalLibraryAdm
TheoreticalLibraryEntity entity = requireQuestion(id, false); TheoreticalLibraryEntity entity = requireQuestion(id, false);
TheoreticalLibraryQuestionVO vo = new TheoreticalLibraryQuestionVO(); TheoreticalLibraryQuestionVO vo = new TheoreticalLibraryQuestionVO();
BeanUtils.copyProperties(entity, vo); BeanUtils.copyProperties(entity, vo);
decorateQuestion(vo); decorateQuestions(Collections.singletonList(vo));
return vo; return vo;
} }
...@@ -589,7 +590,35 @@ public class TheoreticalLibraryAdminServiceImpl implements TheoreticalLibraryAdm ...@@ -589,7 +590,35 @@ public class TheoreticalLibraryAdminServiceImpl implements TheoreticalLibraryAdm
question.setNodeId(leaf.getId()); question.setNodeId(leaf.getId());
} }
private void decorateQuestion(TheoreticalLibraryQuestionVO vo) { /**
* 批量补充分页或详情响应中的展示字段及结构化层级路径。
*
* <p>同一批响应按题库类型只查询一次完整节点集合,再通过内存映射回溯每道题的
* 祖先链,避免原实现对每道题逐级执行 {@code selectById} 所产生的 N+1 查询。</p>
*/
private void decorateQuestions(List<TheoreticalLibraryQuestionVO> questions) {
Map<String, Map<String, TheoreticalLibraryNodeEntity>> nodeMaps = new HashMap<>();
for (TheoreticalLibraryQuestionVO question : questions) {
if (!StringUtils.hasText(question.getNodeId())
|| !StringUtils.hasText(question.getBankType())) {
continue;
}
String bankType = question.getBankType();
if (!nodeMaps.containsKey(bankType)) {
Map<String, TheoreticalLibraryNodeEntity> nodeMap = flatNodes(bankType).stream()
.collect(Collectors.toMap(TheoreticalLibraryNodeEntity::getId,
node -> node, (left, right) -> left, LinkedHashMap::new));
nodeMaps.put(bankType, nodeMap);
}
}
for (TheoreticalLibraryQuestionVO question : questions) {
decorateQuestion(question, nodeMaps.get(question.getBankType()));
}
}
/** 使用同一条节点路径同时生成 fullPath 和 nodePath,保证两种返回格式不会漂移。 */
private void decorateQuestion(TheoreticalLibraryQuestionVO vo,
Map<String, TheoreticalLibraryNodeEntity> nodeMap) {
vo.setStem(vo.getProblem()); vo.setStem(vo.getProblem());
String typeCode = TheoreticalLibraryAdminConstants.typeCodeFromDatabase(vo.getType()); String typeCode = TheoreticalLibraryAdminConstants.typeCodeFromDatabase(vo.getType());
vo.setTypeCode(typeCode); vo.setTypeCode(typeCode);
...@@ -599,12 +628,25 @@ public class TheoreticalLibraryAdminServiceImpl implements TheoreticalLibraryAdm ...@@ -599,12 +628,25 @@ public class TheoreticalLibraryAdminServiceImpl implements TheoreticalLibraryAdm
vo.setKeywordList(readKeywords(vo.getKeywords())); vo.setKeywordList(readKeywords(vo.getKeywords()));
vo.setOptions(readOptions(vo)); vo.setOptions(readOptions(vo));
vo.setAnswerText(displayAnswer(vo)); vo.setAnswerText(displayAnswer(vo));
if (StringUtils.hasText(vo.getNodeId())) { vo.setNodePath(new ArrayList<>());
TheoreticalLibraryNodeEntity leaf = nodeMapper.selectById(vo.getNodeId()); if (!StringUtils.hasText(vo.getNodeId()) || nodeMap == null) {
if (leaf != null) { return;
vo.setFullPath(path(leaf).stream().map(TheoreticalLibraryNodeEntity::getName) }
.collect(Collectors.joining(" → "))); TheoreticalLibraryNodeEntity leaf = nodeMap.get(vo.getNodeId());
} if (leaf == null) {
return;
}
List<TheoreticalLibraryNodeEntity> entityPath = path(leaf, nodeMap);
vo.setFullPath(entityPath.stream().map(TheoreticalLibraryNodeEntity::getName)
.collect(Collectors.joining(" → ")));
for (TheoreticalLibraryNodeEntity node : entityPath) {
TheoreticalLibraryPathNodeVO pathNode = new TheoreticalLibraryPathNodeVO();
pathNode.setId(node.getId());
pathNode.setParentId(node.getParentId());
pathNode.setNodeType(node.getNodeType());
pathNode.setName(node.getName());
pathNode.setCode(node.getCode());
vo.getNodePath().add(pathNode);
} }
} }
...@@ -676,6 +718,32 @@ public class TheoreticalLibraryAdminServiceImpl implements TheoreticalLibraryAdm ...@@ -676,6 +718,32 @@ public class TheoreticalLibraryAdminServiceImpl implements TheoreticalLibraryAdm
return result; return result;
} }
/**
* 使用已批量加载的节点映射回溯路径;断链和循环仍按保存链路的规则拒绝脏数据。
*/
private List<TheoreticalLibraryNodeEntity> path(
TheoreticalLibraryNodeEntity leaf,
Map<String, TheoreticalLibraryNodeEntity> nodeMap) {
List<TheoreticalLibraryNodeEntity> result = new ArrayList<>();
Set<String> visited = new HashSet<>();
TheoreticalLibraryNodeEntity current = leaf;
while (current != null) {
if (!visited.add(current.getId())) {
throw new BusinessException("题库层级存在循环引用,请先修复数据");
}
result.add(current);
if (TheoreticalLibraryAdminConstants.ROOT_PARENT_ID.equals(current.getParentId())) {
break;
}
current = nodeMap.get(current.getParentId());
if (current == null) {
throw new BusinessException("题库层级链不完整,请先修复上级节点");
}
}
Collections.reverse(result);
return result;
}
private List<String> descendantIds(String rootId, boolean includeRoot) { private List<String> descendantIds(String rootId, boolean includeRoot) {
TheoreticalLibraryNodeEntity root = requireNode(rootId); TheoreticalLibraryNodeEntity root = requireNode(rootId);
List<TheoreticalLibraryNodeEntity> all = flatNodes(root.getBankType()); List<TheoreticalLibraryNodeEntity> all = flatNodes(root.getBankType());
......
...@@ -56,7 +56,25 @@ practical-business/src/main/resources/sql/theoretical_library_management.sql ...@@ -56,7 +56,25 @@ practical-business/src/main/resources/sql/theoretical_library_management.sql
``` ```
选择祖先节点时自动包含全部后代。列表和详情同时返回稳定代码、中文标签、完整层级路径、 选择祖先节点时自动包含全部后代。列表和详情同时返回稳定代码、中文标签、完整层级路径、
结构化选项、`answerText`、关键词数组及 `noExam`。 结构化选项、`answerText`、关键词数组及 `noExam`。其中 `nodePath` 按根节点到末级节点
排列,并包含试题绑定的末级节点;它只返回级联回显所需的稳定节点字段:
```json
{
"fullPath": "污水 → 一级 → 第一章 → 第一节 → 第一目 → 知识点",
"nodePath": [
{"id": "1", "parentId": "0", "nodeType": "CRAFT", "name": "污水", "code": "CRAFT-WATER"},
{"id": "2", "parentId": "1", "nodeType": "LEVEL", "name": "一级", "code": "LEVEL-1"},
{"id": "3", "parentId": "2", "nodeType": "CHAPTER", "name": "第一章", "code": "CH-1"},
{"id": "4", "parentId": "3", "nodeType": "SECTION", "name": "第一节", "code": "SEC-1"},
{"id": "5", "parentId": "4", "nodeType": "TOPIC", "name": "第一目", "code": "TOPIC-1"},
{"id": "6", "parentId": "5", "nodeType": "POINT", "name": "知识点", "code": "POINT-1"}
]
}
```
职级题库固定返回“工艺→职级→章→节→目→点”,竞赛题库固定返回
“竞赛类型→题目分类→题目方向”。节点不存在时 `nodePath` 返回空数组。
- `POST /questions/filter-ids`:同一筛选请求,返回页面“全选筛选结果”的 ID,最多 5000 条。 - `POST /questions/filter-ids`:同一筛选请求,返回页面“全选筛选结果”的 ID,最多 5000 条。
- `GET /questions/{id}`:预览和编辑回显。 - `GET /questions/{id}`:预览和编辑回显。
......
package com.platform.business.service.impl; package com.platform.business.service.impl;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.platform.business.mapper.TheoreticalLibraryMapper; import com.platform.business.mapper.TheoreticalLibraryMapper;
import com.platform.business.mapper.TheoreticalLibraryNodeMapper; import com.platform.business.mapper.TheoreticalLibraryNodeMapper;
import com.platform.business.model.dto.TheoreticalLibraryOptionDTO; import com.platform.business.model.dto.TheoreticalLibraryOptionDTO;
import com.platform.business.model.dto.TheoreticalLibraryQuestionQueryDTO;
import com.platform.business.model.dto.TheoreticalLibraryQuestionSaveDTO; import com.platform.business.model.dto.TheoreticalLibraryQuestionSaveDTO;
import com.platform.business.model.entity.TheoreticalLibraryNodeEntity; import com.platform.business.model.entity.TheoreticalLibraryNodeEntity;
import com.platform.business.model.vo.TheoreticalLibraryNodeCountVO; import com.platform.business.model.vo.TheoreticalLibraryNodeCountVO;
import com.platform.business.model.vo.TheoreticalLibraryNodeVO; import com.platform.business.model.vo.TheoreticalLibraryNodeVO;
import com.platform.business.model.vo.TheoreticalLibraryQuestionVO;
import com.platform.exception.BusinessException; import com.platform.exception.BusinessException;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
...@@ -21,9 +24,13 @@ import java.util.Collections; ...@@ -21,9 +24,13 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
...@@ -96,6 +103,92 @@ class TheoreticalLibraryAdminServiceImplTest { ...@@ -96,6 +103,92 @@ class TheoreticalLibraryAdminServiceImplTest {
verify(libraryMapper, never()).insert(any()); verify(libraryMapper, never()).insert(any());
} }
@Test
void pageShouldReturnCompleteJobNodePathAndLoadNodesOnlyOnce() {
TheoreticalLibraryQuestionVO first = question("q-1", "JOB", "point");
TheoreticalLibraryQuestionVO second = question("q-2", "JOB", "point");
Page<TheoreticalLibraryQuestionVO> pageRows = new Page<>(1, 10);
pageRows.setTotal(2L);
pageRows.add(first);
pageRows.add(second);
when(libraryMapper.selectAdminQuestions(any())).thenReturn(pageRows);
when(nodeMapper.selectList(any())).thenReturn(completeJobPath());
TheoreticalLibraryQuestionQueryDTO query = new TheoreticalLibraryQuestionQueryDTO();
query.setBankType("JOB");
List<TheoreticalLibraryQuestionVO> result = service.page(query);
// 必须原样返回 PageHelper 的 Page,控制器才能继续读取正确的 total 等分页元数据。
assertSame(pageRows, result);
assertEquals(2L, ((Page<?>) result).getTotal());
assertEquals(2, result.size());
assertEquals(6, result.get(0).getNodePath().size());
assertEquals("craft", result.get(0).getNodePath().get(0).getId());
assertEquals("point", result.get(0).getNodePath().get(5).getId());
assertEquals("污水 → 一级 → 第一章 → 第一节 → 第一目 → 知识点",
result.get(0).getFullPath());
// 两道题共用同一层级树,分页装饰阶段只允许批量读取一次节点集合。
verify(nodeMapper, times(1)).selectList(any());
}
@Test
void detailShouldReturnCompleteCompetitionNodePath() {
TheoreticalLibraryNodeEntity competition =
node("competition", "0", "COMPETITION", "职业技能竞赛", "COMP-1");
TheoreticalLibraryNodeEntity category =
node("category", "competition", "CATEGORY", "理论知识", "CAT-1");
TheoreticalLibraryNodeEntity direction =
node("direction", "category", "DIRECTION", "污水工艺理论", "DIR-1");
competition.setBankType("COMP");
category.setBankType("COMP");
direction.setBankType("COMP");
when(libraryMapper.selectOne(any()))
.thenReturn(question("q-comp", "COMP", "direction"));
when(nodeMapper.selectList(any()))
.thenReturn(Arrays.asList(competition, category, direction));
TheoreticalLibraryQuestionVO result = service.detail("q-comp");
assertEquals(3, result.getNodePath().size());
assertEquals("COMPETITION", result.getNodePath().get(0).getNodeType());
assertEquals("DIRECTION", result.getNodePath().get(2).getNodeType());
assertEquals("职业技能竞赛 → 理论知识 → 污水工艺理论", result.getFullPath());
}
@Test
void pageShouldReturnEmptyNodePathWhenBoundNodeDoesNotExist() {
when(libraryMapper.selectAdminQuestions(any()))
.thenReturn(Collections.singletonList(question("q-1", "JOB", "missing")));
when(nodeMapper.selectList(any())).thenReturn(Collections.emptyList());
TheoreticalLibraryQuestionQueryDTO query = new TheoreticalLibraryQuestionQueryDTO();
query.setBankType("JOB");
TheoreticalLibraryQuestionVO result = service.page(query).get(0);
assertTrue(result.getNodePath().isEmpty());
assertNull(result.getFullPath());
}
@Test
void pageShouldRejectBrokenOrCyclicNodePath() {
TheoreticalLibraryQuestionQueryDTO query = new TheoreticalLibraryQuestionQueryDTO();
query.setBankType("JOB");
when(libraryMapper.selectAdminQuestions(any()))
.thenReturn(Collections.singletonList(question("q-1", "JOB", "point")));
when(nodeMapper.selectList(any())).thenReturn(Collections.singletonList(
node("point", "missing", "POINT", "知识点", "POINT-1")));
assertThrows(BusinessException.class, () -> service.page(query));
TheoreticalLibraryNodeEntity point =
node("point", "topic", "POINT", "知识点", "POINT-1");
TheoreticalLibraryNodeEntity topic =
node("topic", "point", "TOPIC", "第一目", "TOPIC-1");
when(nodeMapper.selectList(any())).thenReturn(Arrays.asList(point, topic));
assertThrows(BusinessException.class, () -> service.page(query));
}
private List<TheoreticalLibraryNodeEntity> completeJobPath() { private List<TheoreticalLibraryNodeEntity> completeJobPath() {
return Arrays.asList( return Arrays.asList(
node("craft", "0", "CRAFT", "污水", "CRAFT-WATER"), node("craft", "0", "CRAFT", "污水", "CRAFT-WATER"),
...@@ -126,4 +219,16 @@ class TheoreticalLibraryAdminServiceImplTest { ...@@ -126,4 +219,16 @@ class TheoreticalLibraryAdminServiceImplTest {
option.setCorrect(correct); option.setCorrect(correct);
return option; return option;
} }
/** 构造只包含分页装饰所需字段的试题,避免测试依赖无关业务属性。 */
private TheoreticalLibraryQuestionVO question(String id, String bankType, String nodeId) {
TheoreticalLibraryQuestionVO question = new TheoreticalLibraryQuestionVO();
question.setId(id);
question.setBankType(bankType);
question.setNodeId(nodeId);
question.setType("单选题");
question.setDifficulty("EASY");
question.setProblem("测试题干");
return question;
}
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment