Browse Source

bug修复

V1.0
luoweijian 1 week ago
parent
commit
2fa3597f11
  1. 52
      src/main/java/com/project/exam/domain/service/impl/AssemblePaperDomainServiceImpl.java
  2. 43
      src/main/java/com/project/question/mapper/TaskKnowledgePointMapper.java

52
src/main/java/com/project/exam/domain/service/impl/AssemblePaperDomainServiceImpl.java

@ -214,6 +214,58 @@ public class AssemblePaperDomainServiceImpl implements AssemblePaperDomainServic
List<TaskKnowledgePointDTO> kpList = taskKnowledgePointBaseService.lambdaQuery()
.eq(TaskKnowledgePointEntity::getTaskId, dto.getId()).list().stream()
.map(entity -> entity.toDTO(TaskKnowledgePointDTO::new)).toList();
// ========== 新增:批量查询库存,内存过滤 ==========
// 1. 批量查询知识点级别的各题型库存
Set<Long> allKpIds = kpList.stream().map(TaskKnowledgePointDTO::getId).collect(Collectors.toSet());
Map<Long, Map<Integer, Integer>> kpStockMap = new HashMap<>();
if (CollUtil.isNotEmpty(allKpIds)) {
List<Map<String, Object>> kpStockList = taskKnowledgePointMapper.batchCountByKpAndType(allKpIds);
for (Map<String, Object> row : kpStockList) {
Long kpId = ((Number) row.get("kpId")).longValue();
Integer questionType = ((Number) row.get("questionType")).intValue();
Integer stock = ((Number) row.get("stock")).intValue();
kpStockMap.computeIfAbsent(kpId, k -> new HashMap<>()).put(questionType, stock);
}
}
// 2. 批量查询簇级别的多选题库存
Set<Long> allClusterIds = kpList.stream()
.filter(kp -> Objects.nonNull(kp.getClusterId()))
.map(TaskKnowledgePointDTO::getClusterId)
.collect(Collectors.toSet());
Map<Long, Integer> clusterStockMap = new HashMap<>();
if (CollUtil.isNotEmpty(allClusterIds)) {
List<Map<String, Object>> clusterStockList = taskKnowledgePointMapper.batchCountByCluster(allClusterIds);
for (Map<String, Object> row : clusterStockList) {
Long clusterId = ((Number) row.get("clusterId")).longValue();
Integer stock = ((Number) row.get("stock")).intValue();
clusterStockMap.put(clusterId, stock);
}
}
// 3. 内存过滤:只保留有库存的知识点
kpList = kpList.stream().filter(kp -> {
// 多选题种子:需要簇级别有多选题库存(排除单知识点簇)
if (Objects.nonNull(kp.getClusterSize()) && kp.getClusterSize() > 1) {
Integer clusterStock = clusterStockMap.getOrDefault(kp.getClusterId(), 0);
if (clusterStock > 0) {
return true;
}
}
// 单选/判断种子:需要知识点级别有对应题型库存
Map<Integer, Integer> typeStock = kpStockMap.getOrDefault(kp.getId(), Collections.emptyMap());
return typeStock.getOrDefault(QuestionTypeEnum.SINGLE_CHOICE.getValue(), 0) > 0
|| typeStock.getOrDefault(QuestionTypeEnum.TRUE_FALSE.getValue(), 0) > 0;
}).toList();
// 4. 检查过滤后的种子数量是否足够
if (kpList.size() < totalNum) {
log.warn(">>> [库存不足] 任务: {}, 需要种子: {}, 可用种子: {}", dto.getId(), totalNum, kpList.size());
throw new BusinessErrorException("当前题库可用题目不足,请联系管理员补货");
}
// ========== 新增结束 ==========
// 按簇分组
Map<Long, List<TaskKnowledgePointDTO>> clusterGroup = kpList.stream()
.collect(Collectors.groupingBy(TaskKnowledgePointDTO::getClusterId));

43
src/main/java/com/project/question/mapper/TaskKnowledgePointMapper.java

@ -3,11 +3,11 @@ package com.project.question.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.project.question.domain.dto.TaskKnowledgePointDTO;
import com.project.question.domain.entity.TaskKnowledgePointEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.*;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@Mapper
public interface TaskKnowledgePointMapper extends BaseMapper<TaskKnowledgePointEntity> {
@ -28,4 +28,41 @@ public interface TaskKnowledgePointMapper extends BaseMapper<TaskKnowledgePointE
") AS kp_stats ON kp.id = kp_stats.kp_id " +
"ORDER BY kp_stats.single DESC;")
List<TaskKnowledgePointDTO> selectKpOrderBySingle(@Param("id") Long id);
/**
* 批量查询知识点下各题型的可用库存
* 返回 Map<kpId, Map<questionType, stock>>
*/
@MapKey("kpId")
@Select("<script>" +
"SELECT r.kp_id AS kpId, q.question_type AS questionType, COUNT(DISTINCT r.question_id) AS stock " +
"FROM evaluator_question_kp_rel r " +
"INNER JOIN evaluator_question q ON r.question_id = q.id " +
"WHERE r.kp_id IN " +
"<foreach collection='kpIds' item='id' open='(' separator=',' close=')'>" +
"#{id}" +
"</foreach> " +
"AND q.use_status = 0 AND q.deleted = 0 " +
"GROUP BY r.kp_id, q.question_type" +
"</script>")
List<Map<String, Object>> batchCountByKpAndType(@Param("kpIds") Collection<Long> kpIds);
/**
* 批量查询簇下多选题的可用库存
* 返回 Map<clusterId, stock>
*/
@MapKey("clusterId")
@Select("<script>" +
"SELECT r.cluster_id AS clusterId, COUNT(DISTINCT r.question_id) AS stock " +
"FROM evaluator_question_kp_rel r " +
"INNER JOIN evaluator_question q ON r.question_id = q.id " +
"WHERE r.cluster_id IN " +
"<foreach collection='clusterIds' item='id' open='(' separator=',' close=')'>" +
"#{id}" +
"</foreach> " +
"AND q.question_type = 2 " +
"AND q.use_status = 0 AND q.deleted = 0 " +
"GROUP BY r.cluster_id" +
"</script>")
List<Map<String, Object>> batchCountByCluster(@Param("clusterIds") Collection<Long> clusterIds);
}

Loading…
Cancel
Save