目标关联知识点数据查询优化--引入Map减少数据库查询次数

master
Alan 3 months ago
parent d47cd60437
commit 7821b179e2
  1. 2
      src/main/java/com/teaching/backend/model/entity/courses/ObjectiveContentKnow.java
  2. 45
      src/main/java/com/teaching/backend/service/impl/courses/ObjectiveContentKnowServiceImpl.java
  3. 2
      src/main/java/com/teaching/backend/utils/CourseCode.java

@ -45,7 +45,7 @@ public class ObjectiveContentKnow implements Serializable {
* 关联的知识点 * 关联的知识点
*/ */
// private String know; // private String know;
private Integer knowId; private Long knowId;
} }

@ -20,6 +20,7 @@ import com.teaching.backend.model.vo.knowtmp.KnowTmpVO;
import com.teaching.backend.service.courses.IObjectiveContentKnowService; import com.teaching.backend.service.courses.IObjectiveContentKnowService;
import com.teaching.backend.utils.CourseCode; import com.teaching.backend.utils.CourseCode;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.neo4j.driver.internal.value.StringValue;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -30,6 +31,7 @@ import java.math.RoundingMode;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -97,12 +99,19 @@ public class ObjectiveContentKnowServiceImpl extends ServiceImpl<ObjectiveConten
ObjectiveContentKnowVO objectiveContentKnowVO = new ObjectiveContentKnowVO(); ObjectiveContentKnowVO objectiveContentKnowVO = new ObjectiveContentKnowVO();
// 根据传入的目标内容ID找到课程ID和总学时 // 根据传入的目标内容ID找到课程ID和总学时
String objectiveID = objectiveContentsMapper.selectOne(new LambdaQueryWrapper<ObjectiveContents>() String objectiveID = objectiveContentsMapper.selectById(objectiveContentId).getObjectiveId();
.eq(ObjectiveContents::getId, objectiveContentId)).getObjectiveId(); CourseObjectives courseObjective = courseObjectivesMapper.selectOne(new LambdaQueryWrapper<CourseObjectives>()
String pid = courseObjectivesMapper.selectOne(new LambdaQueryWrapper<CourseObjectives>() .eq(CourseObjectives::getId, objectiveID));
.eq(CourseObjectives::getId, objectiveID)).getPid(); String pid = courseObjective.getPid();
String courseId = courseObjectivesMapper.selectOne(new LambdaQueryWrapper<CourseObjectives>() //当前内容属于总目标类型的
.eq(CourseObjectives::getId, pid)).getCourseId(); String courseId;
if(pid.equals(String.valueOf(CourseCode.CONTENT_BELONGS_TO_TOTAL_OBJECTIVE.getValue()))){
courseId = courseObjective.getCourseId();
}
else {
courseId = courseObjectivesMapper.selectOne(new LambdaQueryWrapper<CourseObjectives>()
.eq(CourseObjectives::getId, pid)).getCourseId();
}
Integer classhours = coursesMapper.selectById(courseId).getClasshours(); Integer classhours = coursesMapper.selectById(courseId).getClasshours();
// 获取当前目标内容下关联的知识点数据 // 获取当前目标内容下关联的知识点数据
@ -127,22 +136,35 @@ public class ObjectiveContentKnowServiceImpl extends ServiceImpl<ObjectiveConten
} }
// 创建ObjectiveContentKnowVO对象,并计算知识点数量、总时间和关联的知识点部分数据 // 创建ObjectiveContentKnowVO对象,并计算知识点数量、总时间和关联的知识点部分数据
// TODO 查询知识点的数据 有优化空间!!!
private ObjectiveContentKnowVO createObjectiveContentKnowVO(String objectiveOrContentId, List<ObjectiveContentKnow> objectiveContentKnows) { private ObjectiveContentKnowVO createObjectiveContentKnowVO(String objectiveOrContentId, List<ObjectiveContentKnow> objectiveContentKnows) {
ObjectiveContentKnowVO vo = new ObjectiveContentKnowVO(); ObjectiveContentKnowVO vo = new ObjectiveContentKnowVO();
vo.setObjectiveOrContent(objectiveOrContentId); vo.setObjectiveOrContent(objectiveOrContentId);
vo.setKnowsNumber(objectiveContentKnows.size()); vo.setKnowsNumber(objectiveContentKnows.size());
// 计算知识点的总时间,并收集关联的知识点部分数据 // 提取所有的 Know IDs
List<Long> knowIds = objectiveContentKnows.stream()
.map(ObjectiveContentKnow::getKnowId)
.collect(Collectors.toList());
// 一次性查询所有需要的 Knowtmp 数据
List<Knowtmp> knows = knowtmpMapper.selectList(new LambdaQueryWrapper<Knowtmp>()
.in(Knowtmp::getId, knowIds));
// 使用 Map 来存储 Knowtmp 数据,以便快速查找
Map<Long, Knowtmp> knowMap = knows.stream()
.collect(Collectors.toMap(Knowtmp::getId, know -> know));
Double knowsTime = 0.0; Double knowsTime = 0.0;
List<KnowTmpVO> knowTmpVOList = new ArrayList<>(); List<KnowTmpVO> knowTmpVOList = new ArrayList<>();
for (ObjectiveContentKnow contentKnow : objectiveContentKnows) { for (ObjectiveContentKnow contentKnow : objectiveContentKnows) {
Knowtmp know = knowtmpMapper.selectOne(new LambdaQueryWrapper<Knowtmp>() Knowtmp know = knowMap.get(contentKnow.getKnowId());
.eq(Knowtmp::getId, contentKnow.getKnowId()));
if (know == null) { if (know == null) {
throw new BusinessException(ErrorCode.KNOW_NOT_EXIT); log.warn("知识点不存在,id:{}", contentKnow.getKnowId());
// throw new BusinessException(ErrorCode.KNOW_NOT_EXIT);
continue; // 知识点不存在时跳过
} }
knowsTime += know.getHour(); knowsTime += know.getHour();
// 将当前知识点数据添加到knowTmpVOList中 // 将当前知识点数据添加到knowTmpVOList中
@ -158,6 +180,7 @@ public class ObjectiveContentKnowServiceImpl extends ServiceImpl<ObjectiveConten
return vo; return vo;
} }
// 计算知识点学时占总学时的比例,并返回百分比形式的结果 // 计算知识点学时占总学时的比例,并返回百分比形式的结果
private Double calculatePercentage(Double knowsTime, Integer classhours) { private Double calculatePercentage(Double knowsTime, Integer classhours) {
if (classhours == null || classhours == 0) { if (classhours == null || classhours == 0) {

@ -12,7 +12,7 @@ public enum CourseCode {
TOTAL_OBJECTIVE_TYPE(0, "课程总目标"), TOTAL_OBJECTIVE_TYPE(0, "课程总目标"),
TOTAL_OBJECTIVE_HAS_NO_CHILD(0, "课程总目标下面没有分项目标"), TOTAL_OBJECTIVE_HAS_NO_CHILD(0, "课程总目标下面没有分项目标"),
Total_EXIT(0, "课程总目标"), CONTENT_BELONGS_TO_TOTAL_OBJECTIVE(0, "该内容属于总目标"),
COURSE_UPDATING(1, "课程正在修改中"); COURSE_UPDATING(1, "课程正在修改中");

Loading…
Cancel
Save