瀏覽代碼

fix(assemble): fallback to child knowledge points when kp pool is short

yemeishu 2 周之前
父節點
當前提交
decd14e5a2
共有 1 個文件被更改,包括 40 次插入0 次删除
  1. 40 0
      app/Services/ExamTypeStrategy.php

+ 40 - 0
app/Services/ExamTypeStrategy.php

@@ -950,6 +950,10 @@ class ExamTypeStrategy
         Log::info('ExamTypeStrategy: 构建知识点组卷参数', $params);
 
         $kpCodeList = $params['kp_code_list'] ?? [];
+        if (!is_array($kpCodeList)) {
+            $kpCodeList = [];
+        }
+        $kpCodeList = array_values(array_unique(array_filter($kpCodeList)));
         $studentId = $params['student_id'] ?? null;
         $totalQuestions = $params['total_questions'] ?? 20;
 
@@ -964,6 +968,41 @@ class ExamTypeStrategy
             'total_questions' => $totalQuestions
         ]);
 
+        // assemble_type=2 走知识点扩展策略(复用 QuestionExpansionService)。
+        // 只在基础题池不足时,才会触发子知识点补充(由扩展策略内部控制)。
+        $assembleType = (int) ($params['assemble_type'] ?? 4);
+        $priorityQuestionIds = [];
+        $basePoolCount = 0;
+        $finalPoolCount = 0;
+        $childAddedCount = 0;
+
+        if ($assembleType === 2) {
+            $expansionStrategy = $this->questionExpansionService->expandQuestionsByKnowledgePoints(
+                $params,
+                $studentId ? (string) $studentId : null,
+                $kpCodeList,
+                [],
+                (int) $totalQuestions
+            );
+
+            $priorityQuestionIds = array_values(array_unique(array_filter($expansionStrategy['mistake_question_ids'] ?? [])));
+            $basePoolCount = (int) (
+                ($expansionStrategy['expansion_details']['step1_direct_kp']['added_count'] ?? 0)
+                + ($expansionStrategy['expansion_details']['step2_same_kp']['added_count'] ?? 0)
+            );
+            $finalPoolCount = count($priorityQuestionIds);
+            $childAddedCount = max(0, $finalPoolCount - $basePoolCount);
+        }
+
+        // 三项汇总日志放在外围,便于统一观察策略触发情况。
+        Log::info('ExamTypeStrategy: 知识点组卷题池评估', [
+            'original_kp_count' => count($kpCodeList),
+            'child_kp_added_count' => $childAddedCount,
+            'pool_count_after_expand' => $finalPoolCount,
+            'total_questions' => (int) $totalQuestions,
+            'assemble_type' => $assembleType,
+        ]);
+
         // 如果有学生ID,获取已做过的题目ID列表(用于排除)
         $answeredQuestionIds = [];
         if ($studentId) {
@@ -977,6 +1016,7 @@ class ExamTypeStrategy
         // 组装增强参数
         $enhanced = array_merge($params, [
             'kp_codes' => $kpCodeList,
+            'mistake_question_ids' => $basePoolCount < (int) $totalQuestions ? $priorityQuestionIds : [],
             'exclude_question_ids' => $answeredQuestionIds,
             'paper_name' => $params['paper_name'] ?? ('知识点组卷_' . now()->format('Ymd_His')),
             // 知识点组卷:442配比(配合固定分值5/5/10,各题型贡献均等)