| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- <?php
- namespace App\Services;
- use App\Models\StudentExercise;
- use App\Models\MistakeRecord;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Str;
- /**
- * 学生作答分析服务
- * 负责处理学生作答结果的保存、分析和掌握度更新
- */
- class StudentAnswerAnalysisService
- {
- public function __construct(
- private readonly LocalAIAnalysisService $aiAnalysisService
- ) {}
- /**
- * 保存作答记录
- */
- public function saveAnswerRecord(array $data): array
- {
- // 生成唯一记录ID
- $recordId = 'ans_' . Str::uuid()->toString();
- // 计算统计数据
- $answers = $data['answers'];
- $correctCount = 0;
- $wrongCount = 0;
- $totalScore = 0;
- $obtainedScore = 0;
- foreach ($answers as $answer) {
- $score = (float) ($answer['score'] ?? 0);
- $maxScore = (float) ($answer['max_score'] ?? $score);
- $totalScore += $maxScore;
- $obtainedScore += $score;
- if ($answer['is_correct']) {
- $correctCount++;
- } else {
- $wrongCount++;
- }
- }
- $accuracyRate = ($correctCount + $wrongCount) > 0
- ? round($correctCount / ($correctCount + $wrongCount), 4)
- : 0;
- // 保存到 student_exercises 表
- foreach ($answers as $answer) {
- StudentExercise::create([
- 'student_id' => $data['student_id'],
- 'question_id' => $answer['question_id'],
- 'question_content' => json_encode([
- 'question_id' => $answer['question_id'],
- 'question_number' => $answer['question_number'] ?? null,
- 'paper_id' => $data['paper_id'],
- ]),
- 'student_answer' => $answer['student_answer'] ?? '',
- 'correct_answer' => $answer['correct_answer'] ?? '',
- 'is_correct' => $answer['is_correct'],
- 'submission_status' => 'completed',
- 'kp_code' => $answer['knowledge_point'] ?? null,
- 'difficulty_level' => 0.5, // 默认难度
- 'time_spent_seconds' => 0, // 默认耗时
- 'created_at' => $data['answer_time'] ?? now(),
- 'updated_at' => now(),
- ]);
- // 保存错题记录
- if (!$answer['is_correct']) {
- $this->saveMistakeRecord($data, $answer);
- }
- }
- return [
- 'record_id' => $recordId,
- 'paper_id' => $data['paper_id'],
- 'student_id' => $data['student_id'],
- 'total_score' => $totalScore,
- 'obtained_score' => $obtainedScore,
- 'accuracy_rate' => $accuracyRate,
- 'correct_count' => $correctCount,
- 'wrong_count' => $wrongCount,
- 'total_questions' => count($answers),
- ];
- }
- /**
- * 保存错题记录
- */
- private function saveMistakeRecord(array $data, array $answer): void
- {
- try {
- // 检查是否已存在相同的错题记录
- $existing = MistakeRecord::where('student_id', $data['student_id'])
- ->where('question_id', $answer['question_id'])
- ->first();
- if ($existing) {
- // 更新现有记录
- $existing->increment('review_count');
- $existing->update([
- 'student_answer' => $answer['student_answer'] ?? '',
- 'correct_answer' => $answer['correct_answer'] ?? '',
- 'updated_at' => now(),
- ]);
- } else {
- // 创建新记录
- MistakeRecord::create([
- 'student_id' => $data['student_id'],
- 'question_id' => $answer['question_id'],
- 'source' => MistakeRecord::SOURCE_EXAM,
- 'question_text' => json_encode([
- 'question_number' => $answer['question_number'] ?? null,
- 'paper_id' => $data['paper_id'],
- 'question_type' => $answer['question_type'] ?? null,
- ]),
- 'student_answer' => $answer['student_answer'] ?? '',
- 'correct_answer' => $answer['correct_answer'] ?? '',
- 'knowledge_point' => $answer['knowledge_point'] ?? null,
- 'error_type' => $this->guessErrorType($answer),
- 'review_status' => MistakeRecord::REVIEW_STATUS_PENDING,
- 'review_count' => 0,
- 'force_review' => false,
- 'is_favorite' => false,
- 'in_retry_list' => false,
- 'difficulty' => 0.5,
- 'mastery_level' => 0.0,
- ]);
- }
- } catch (\Exception $e) {
- Log::warning('保存错题记录失败', [
- 'student_id' => $data['student_id'],
- 'question_id' => $answer['question_id'],
- 'error' => $e->getMessage(),
- ]);
- }
- }
- /**
- * 猜测错误类型
- */
- private function guessErrorType(array $answer): string
- {
- // 根据题型和答案特征猜测错误类型
- $questionType = $answer['question_type'] ?? '';
- if ($questionType === 'choice') {
- return MistakeRecord::ERROR_TYPE_CARELESS;
- }
- if ($questionType === 'fill' || $questionType === 'answer') {
- // 检查是否部分正确
- if (isset($answer['step_scores']) && is_array($answer['step_scores'])) {
- $totalSteps = count($answer['step_scores']);
- $correctSteps = array_sum($answer['step_scores']);
- if ($correctSteps > 0 && $correctSteps < $totalSteps) {
- return MistakeRecord::ERROR_TYPE_PROCEDURE ?? MistakeRecord::ERROR_TYPE_CALCULATION;
- }
- }
- }
- return MistakeRecord::ERROR_TYPE_OTHER;
- }
- /**
- * 保存分析结果
- */
- public function saveAnalysisResults(array $answerRecord, array $analysisData, array $questionAnalyses): void
- {
- try {
- // 生成分析ID
- $analysisId = 'analysis_' . Str::uuid()->toString();
- // 保存分析记录到 PostgreSQL
- DB::connection('pgsql')->table('answer_analysis_records')->insert([
- 'analysis_id' => $analysisId,
- 'exam_id' => $answerRecord['paper_id'],
- 'student_id' => $answerRecord['student_id'],
- 'ocr_record_id' => 0, // 如果是系统试卷,没有OCR记录
- 'status' => 'completed',
- 'analysis_results' => json_encode($analysisData),
- 'completed_at' => now(),
- 'created_at' => now(),
- 'updated_at' => now(),
- ]);
- // 获取分析记录的ID
- $analysisRecordId = DB::connection('pgsql')
- ->table('answer_analysis_records')
- ->where('analysis_id', $analysisId)
- ->value('id');
- // 保存每道题的分析结果
- foreach ($questionAnalyses as $questionAnalysis) {
- DB::connection('pgsql')->table('question_analysis_results')->insert([
- 'analysis_record_id' => $analysisRecordId,
- 'question_id' => $questionAnalysis['question_id'],
- 'question_number' => $questionAnalysis['question_number'] ?? null,
- 'kp_code' => $questionAnalysis['kp_code'] ?? null,
- 'student_answer' => $questionAnalysis['student_answer'] ?? '',
- 'correct_answer' => $questionAnalysis['correct_answer'] ?? '',
- 'is_correct' => $questionAnalysis['is_correct'] ?? false,
- 'score_obtained' => $questionAnalysis['score_obtained'] ?? 0,
- 'max_score' => $questionAnalysis['max_score'] ?? 0,
- 'ai_analysis' => $questionAnalysis['ai_analysis'] ?? null,
- 'learning_suggestions' => json_encode($questionAnalysis['suggestions'] ?? []),
- 'created_at' => now(),
- 'updated_at' => now(),
- ]);
- // 更新掌握度
- if (!empty($questionAnalysis['kp_code'])) {
- $this->updateMasteryForQuestion(
- $answerRecord['student_id'],
- $questionAnalysis['kp_code'],
- $questionAnalysis['is_correct'],
- $questionAnalysis['difficulty'] ?? 0.5
- );
- }
- }
- Log::info('分析结果已保存', [
- 'student_id' => $answerRecord['student_id'],
- 'paper_id' => $answerRecord['paper_id'],
- 'analysis_id' => $analysisId,
- 'question_count' => count($questionAnalyses),
- ]);
- } catch (\Exception $e) {
- Log::error('保存分析结果失败', [
- 'student_id' => $answerRecord['student_id'],
- 'paper_id' => $answerRecord['paper_id'],
- 'error' => $e->getMessage(),
- ]);
- }
- }
- /**
- * 为单个题目更新掌握度
- */
- private function updateMasteryForQuestion(string $studentId, string $kpCode, bool $isCorrect, float $difficulty): void
- {
- try {
- // 获取当前掌握度
- $currentMastery = 0.5; // 默认值
- $existingMastery = DB::connection('pgsql')
- ->table('student_knowledge_mastery')
- ->where('student_id', $studentId)
- ->where('kp_code', $kpCode)
- ->first();
- if ($existingMastery) {
- $currentMastery = (float) $existingMastery->mastery_level;
- }
- // 使用AI分析服务更新掌握度
- $result = $this->aiAnalysisService->updateMastery(
- $studentId,
- $kpCode,
- $currentMastery,
- $isCorrect,
- $difficulty
- );
- Log::debug('掌握度已更新', [
- 'student_id' => $studentId,
- 'kp_code' => $kpCode,
- 'old_mastery' => $result['old_mastery'],
- 'new_mastery' => $result['new_mastery'],
- 'change' => $result['change'],
- ]);
- } catch (\Exception $e) {
- Log::warning('更新掌握度失败', [
- 'student_id' => $studentId,
- 'kp_code' => $kpCode,
- 'error' => $e->getMessage(),
- ]);
- }
- }
- /**
- * 创建掌握度快照
- */
- public function createMasterySnapshot(string $studentId, ?string $paperId = null, ?string $answerRecordId = null): ?array
- {
- // 使用AI分析服务创建快照
- return $this->aiAnalysisService->createMasterySnapshot($studentId, $paperId, $answerRecordId);
- }
- /**
- * 获取学生的学习历史
- */
- public function getStudentLearningHistory(string $studentId, int $limit = 10): array
- {
- try {
- $exercises = StudentExercise::where('student_id', $studentId)
- ->orderBy('created_at', 'desc')
- ->limit($limit)
- ->get()
- ->toArray();
- $mistakes = MistakeRecord::where('student_id', $studentId)
- ->orderBy('created_at', 'desc')
- ->limit($limit)
- ->get()
- ->toArray();
- // 使用AI分析服务获取掌握度数据
- $masteryData = $this->aiAnalysisService->getStudentMastery($studentId);
- // 获取掌握度快照历史
- $snapshots = DB::connection('pgsql')
- ->table('knowledge_point_mastery_snapshots')
- ->where('student_id', $studentId)
- ->orderBy('snapshot_time', 'desc')
- ->limit($limit)
- ->get()
- ->toArray();
- return [
- 'exercises' => $exercises,
- 'mistakes' => $mistakes,
- 'mastery_data' => $masteryData['data'] ?? [],
- 'mastery_snapshots' => $snapshots,
- 'summary' => [
- 'total_exercises' => StudentExercise::where('student_id', $studentId)->count(),
- 'total_mistakes' => MistakeRecord::where('student_id', $studentId)->count(),
- 'mastery_snapshots_count' => count($snapshots),
- 'total_mastery_items' => count($masteryData['data'] ?? []),
- ],
- ];
- } catch (\Exception $e) {
- Log::error('获取学习历史失败', [
- 'student_id' => $studentId,
- 'error' => $e->getMessage(),
- ]);
- return [];
- }
- }
- }
|