Browse Source

组卷的保存 bug 修复

yemeishu 2 days ago
parent
commit
889fe41e18
2 changed files with 49 additions and 6 deletions
  1. 4 2
      app/Services/ExamTypeStrategy.php
  2. 45 4
      app/Services/QuestionBankService.php

+ 4 - 2
app/Services/ExamTypeStrategy.php

@@ -1211,18 +1211,20 @@ class ExamTypeStrategy
 
 
     /**
     /**
      * 原有逻辑(保留作为回退方案)
      * 原有逻辑(保留作为回退方案)
+     * 修复:解决DISTINCT和ORDER BY冲突问题
      */
      */
     private function getKnowledgePointsFromChaptersLegacy(array $chapterIds, int $limit = 25): array
     private function getKnowledgePointsFromChaptersLegacy(array $chapterIds, int $limit = 25): array
     {
     {
         try {
         try {
-            // 查询 textbook_chapter_knowledge_relation 表(修复:添加排序避免数据偏向)
+            // 修复方案1:使用GROUP BY替代DISTINCT,并选择需要的字段
             $kpCodes = DB::table('textbook_chapter_knowledge_relation as tckr')
             $kpCodes = DB::table('textbook_chapter_knowledge_relation as tckr')
                 ->leftJoin('textbook_catalog_nodes as tcn', 'tckr.catalog_chapter_id', '=', 'tcn.id')
                 ->leftJoin('textbook_catalog_nodes as tcn', 'tckr.catalog_chapter_id', '=', 'tcn.id')
                 ->whereIn('tckr.catalog_chapter_id', $chapterIds)
                 ->whereIn('tckr.catalog_chapter_id', $chapterIds)
+                ->select('tckr.kp_code', 'tcn.sort_order')
                 ->orderBy('tcn.sort_order', 'asc')
                 ->orderBy('tcn.sort_order', 'asc')
                 ->orderBy('tckr.kp_code', 'asc')
                 ->orderBy('tckr.kp_code', 'asc')
                 ->limit($limit)
                 ->limit($limit)
-                ->distinct()
+                ->groupBy('tckr.kp_code')
                 ->pluck('tckr.kp_code')
                 ->pluck('tckr.kp_code')
                 ->toArray();
                 ->toArray();
 
 

+ 45 - 4
app/Services/QuestionBankService.php

@@ -547,13 +547,14 @@ class QuestionBankService
                 ]);
                 ]);
 
 
                 // 使用Laravel模型保存到 papers 表
                 // 使用Laravel模型保存到 papers 表
+                // 注意:total_questions将在题目处理完成后再更新,确保与实际插入的题目数量一致
                 $paper = \App\Models\Paper::create([
                 $paper = \App\Models\Paper::create([
                     'paper_id' => $paperId,
                     'paper_id' => $paperId,
                     'student_id' => $examData['student_id'] ?? '',
                     'student_id' => $examData['student_id'] ?? '',
                     'teacher_id' => $examData['teacher_id'] ?? '',
                     'teacher_id' => $examData['teacher_id'] ?? '',
                     'paper_name' => $examData['paper_name'] ?? '未命名试卷',
                     'paper_name' => $examData['paper_name'] ?? '未命名试卷',
                     'paper_type' => 'auto_generated',
                     'paper_type' => 'auto_generated',
-                    'total_questions' => count($examData['questions']), // 使用实际题目数量
+                    'total_questions' => 0, // 临时设为0,处理完题目后再更新
                     'total_score' => $examData['total_score'] ?? 0,
                     'total_score' => $examData['total_score'] ?? 0,
                     'status' => 'draft',
                     'status' => 'draft',
                     'difficulty_category' => $examData['difficulty_category'] ?? '基础',
                     'difficulty_category' => $examData['difficulty_category'] ?? '基础',
@@ -582,12 +583,23 @@ class QuestionBankService
 
 
                 // 准备题目数据
                 // 准备题目数据
                 $questionInsertData = [];
                 $questionInsertData = [];
+                $skippedQuestions = 0;
+                Log::info('开始处理题目数据', [
+                    'paper_id' => $paperId,
+                    'total_questions' => count($examData['questions'])
+                ]);
+
                 foreach ($examData['questions'] as $index => $question) {
                 foreach ($examData['questions'] as $index => $question) {
                     // 验证题目基本数据
                     // 验证题目基本数据
                     if (empty($question['stem']) && empty($question['content'])) {
                     if (empty($question['stem']) && empty($question['content'])) {
+                        $skippedQuestions++;
                         Log::warning('跳过没有内容的题目', [
                         Log::warning('跳过没有内容的题目', [
                             'paper_id' => $paperId,
                             'paper_id' => $paperId,
-                            'question_index' => $index
+                            'question_index' => $index,
+                            'question_id' => $question['id'] ?? 'N/A',
+                            'has_stem' => !empty($question['stem']),
+                            'has_content' => !empty($question['content']),
+                            'stem_preview' => substr($question['stem'] ?? '', 0, 50)
                         ]);
                         ]);
                         continue;
                         continue;
                     }
                     }
@@ -685,10 +697,21 @@ class QuestionBankService
 
 
                 // 验证是否有有效的题目数据
                 // 验证是否有有效的题目数据
                 if (empty($questionInsertData)) {
                 if (empty($questionInsertData)) {
-                    Log::error('没有有效的题目数据可以保存', ['paper_id' => $paperId]);
+                    Log::error('没有有效的题目数据可以保存', [
+                        'paper_id' => $paperId,
+                        'total_input_questions' => count($examData['questions']),
+                        'skipped_questions' => $skippedQuestions
+                    ]);
                     throw new \Exception('没有有效的题目数据');
                     throw new \Exception('没有有效的题目数据');
                 }
                 }
 
 
+                Log::info('准备插入题目数据', [
+                    'paper_id' => $paperId,
+                    'total_input_questions' => count($examData['questions']),
+                    'skipped_questions' => $skippedQuestions,
+                    'questions_to_insert' => count($questionInsertData)
+                ]);
+
                 // 调试:检查第一个题目的solution字段
                 // 调试:检查第一个题目的solution字段
                 if (!empty($questionInsertData)) {
                 if (!empty($questionInsertData)) {
                     $firstQuestion = $questionInsertData[0];
                     $firstQuestion = $questionInsertData[0];
@@ -708,10 +731,28 @@ class QuestionBankService
                 // 验证插入结果,使用关联关系
                 // 验证插入结果,使用关联关系
                 $insertedQuestionCount = $paper->questions()->count();
                 $insertedQuestionCount = $paper->questions()->count();
 
 
+                Log::info('验证题目插入结果', [
+                    'paper_id' => $paperId,
+                    'expected_count' => count($questionInsertData),
+                    'actual_count' => $insertedQuestionCount,
+                    'difference' => $insertedQuestionCount - count($questionInsertData)
+                ]);
+
                 if ($insertedQuestionCount !== count($questionInsertData)) {
                 if ($insertedQuestionCount !== count($questionInsertData)) {
-                    throw new \Exception("题目插入数量不匹配:预期 {$insertedQuestionCount},实际 " . count($questionInsertData));
+                    Log::error('题目插入数量不匹配', [
+                        'paper_id' => $paperId,
+                        'expected' => count($questionInsertData),
+                        'actual' => $insertedQuestionCount,
+                        'difference' => $insertedQuestionCount - count($questionInsertData),
+                        'input_questions_count' => count($examData['questions']),
+                        'skipped_questions' => $skippedQuestions
+                    ]);
+                    throw new \Exception("题目插入数量不匹配:预期 " . count($questionInsertData) . ",实际 {$insertedQuestionCount}");
                 }
                 }
 
 
+                // 【重要】更新试卷的total_questions字段为实际插入的题目数量
+                $paper->update(['total_questions' => $insertedQuestionCount]);
+
                 Log::info('试卷保存成功', [
                 Log::info('试卷保存成功', [
                     'paper_id' => $paperId,
                     'paper_id' => $paperId,
                     'expected_questions' => count($questionInsertData),
                     'expected_questions' => count($questionInsertData),