|
@@ -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),
|