Browse Source

优化判断考试任务是否生题完成接口

master
luogw 1 month ago
parent
commit
3de5693ba4
  1. 2
      src/main/java/com/project/interaction/controller/InteractionController.java
  2. 30
      src/main/java/com/project/question/domain/service/impl/QuestionInventoryDomainServiceImpl.java

2
src/main/java/com/project/interaction/controller/InteractionController.java

@ -62,7 +62,7 @@ public class InteractionController {
} }
/** /**
* 判断任务是否删除 * 判断任务是否删除供算法判断请求是否需要生题
*/ */
@PostMapping("/isDeletedTask") @PostMapping("/isDeletedTask")
public Result<Boolean> isDeletedTask(Long taskId,@RequestParam("taskKpIds") List<Long> taskKpIds,Integer type){ public Result<Boolean> isDeletedTask(Long taskId,@RequestParam("taskKpIds") List<Long> taskKpIds,Integer type){

30
src/main/java/com/project/question/domain/service/impl/QuestionInventoryDomainServiceImpl.java

@ -1,5 +1,6 @@
package com.project.question.domain.service.impl; package com.project.question.domain.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.project.base.domain.result.Result; import com.project.base.domain.result.Result;
@ -19,10 +20,13 @@ import lombok.extern.slf4j.Slf4j;
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 java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@Service @Service
@Slf4j @Slf4j
@ -113,6 +117,18 @@ public class QuestionInventoryDomainServiceImpl implements QuestionInventoryDoma
List<TaskKnowledgePointEntity> kpList = taskKpMapper.selectList( List<TaskKnowledgePointEntity> kpList = taskKpMapper.selectList(
new LambdaQueryWrapper<TaskKnowledgePointEntity>().eq(TaskKnowledgePointEntity::getTaskId , taskId)); new LambdaQueryWrapper<TaskKnowledgePointEntity>().eq(TaskKnowledgePointEntity::getTaskId , taskId));
// 批量查询知识点库存: Map<kpId, Map<questionType, stock>>,避免 N+1
Map<Long, Map<Integer, Integer>> kpStockMap = new HashMap<>();
Set<Long> kpIdSet = kpList.stream().map(TaskKnowledgePointEntity::getId).collect(Collectors.toSet());
if (CollUtil.isNotEmpty(kpIdSet)) {
for (Map<String, Object> row : taskKpMapper.batchCountByKpAndType(kpIdSet)) {
Long kpId = ((Number) row.get("kpId")).longValue();
Integer qType = ((Number) row.get("questionType")).intValue();
Integer stock = ((Number) row.get("stock")).intValue();
kpStockMap.computeIfAbsent(kpId, k -> new HashMap<>()).put(qType, stock);
}
}
for (QuestionTypeEnum questionType : QuestionTypeEnum.values()) { for (QuestionTypeEnum questionType : QuestionTypeEnum.values()) {
if (QuestionTypeEnum.MULTIPLE_CHOICE.equals(questionType)) { if (QuestionTypeEnum.MULTIPLE_CHOICE.equals(questionType)) {
continue; continue;
@ -128,7 +144,8 @@ public class QuestionInventoryDomainServiceImpl implements QuestionInventoryDoma
int passedCount = 0; int passedCount = 0;
for (TaskKnowledgePointEntity kp : kpList) { for (TaskKnowledgePointEntity kp : kpList) {
kpCount++; kpCount++;
int currentStock = questionKpMapper.countAvailableByKp(kp.getId(), questionType.getValue()); int currentStock = kpStockMap.getOrDefault(kp.getId(), Collections.emptyMap())
.getOrDefault(questionType.getValue(), 0);
boolean isEnough = currentStock >= watermark; boolean isEnough = currentStock >= watermark;
if (isEnough) { if (isEnough) {
passedCount++; passedCount++;
@ -144,6 +161,15 @@ public class QuestionInventoryDomainServiceImpl implements QuestionInventoryDoma
List<TaskKnowledgeClusterEntity> clusterList = clusterMapper.selectList( List<TaskKnowledgeClusterEntity> clusterList = clusterMapper.selectList(
new LambdaQueryWrapper<TaskKnowledgeClusterEntity>().eq(TaskKnowledgeClusterEntity::getTaskId, taskId)); new LambdaQueryWrapper<TaskKnowledgeClusterEntity>().eq(TaskKnowledgeClusterEntity::getTaskId, taskId));
// 批量查询簇库存: Map<clusterId, stock>,避免 N+1
Map<Long, Integer> clusterStockMap = new HashMap<>();
Set<Long> clusterIdSet = clusterList.stream().map(TaskKnowledgeClusterEntity::getId).collect(Collectors.toSet());
if (CollUtil.isNotEmpty(clusterIdSet)) {
for (Map<String, Object> row : taskKpMapper.batchCountByCluster(clusterIdSet)) {
clusterStockMap.put(((Number) row.get("clusterId")).longValue(), ((Number) row.get("stock")).intValue());
}
}
// 按簇大小分组统计 // 按簇大小分组统计
Map<Integer, Integer> sizeTotalMap = new HashMap<>(); Map<Integer, Integer> sizeTotalMap = new HashMap<>();
Map<Integer, Integer> sizePassedMap = new HashMap<>(); Map<Integer, Integer> sizePassedMap = new HashMap<>();
@ -154,7 +180,7 @@ public class QuestionInventoryDomainServiceImpl implements QuestionInventoryDoma
} }
int w = cluster.getClusterSize(); int w = cluster.getClusterSize();
int watermark = (int) Math.ceil(waterLine * w * questionTypeMap.get(QuestionTypeEnum.MULTIPLE_CHOICE.name())); int watermark = (int) Math.ceil(waterLine * w * questionTypeMap.get(QuestionTypeEnum.MULTIPLE_CHOICE.name()));
int currentStock = questionKpMapper.countAvailableByCluster(cluster.getId()); int currentStock = clusterStockMap.getOrDefault(cluster.getId(), 0);
boolean isEnough = currentStock >= watermark; boolean isEnough = currentStock >= watermark;
sizeTotalMap.merge(w, 1, Integer::sum); sizeTotalMap.merge(w, 1, Integer::sum);

Loading…
Cancel
Save