find($this->recordId); } public function mount(string $recordId): void { $this->recordId = $recordId; $record = $this->record(); if ($record) { foreach ($record->questions as $question) { if ($question->manual_answer) { $this->manualAnswers[$question->id] = $question->manual_answer; } } // 检查是否已有AI分析结果 $this->checkAnalysisResults($record); } } /** * Check if record already has AI analysis results */ private function checkAnalysisResults(OCRRecord $record): void { $this->hasAnalysisResults = $record->questions() ->whereNotNull('ai_score') ->orWhereNotNull('ai_feedback') ->exists(); } /** * Submit all questions for AI analysis. * Updates manual answers in batch, then sends data to LearningAnalytics. */ public function submitForAnalysis(): void { $record = $this->record(); if (! $record) { Notification::make() ->title('记录不存在') ->danger() ->send(); return; } $updatedCount = 0; foreach ($record->questions as $question) { $manualAnswer = $this->manualAnswers[$question->id] ?? null; if ($manualAnswer && trim($manualAnswer) !== '') { $question->update([ 'manual_answer' => trim($manualAnswer), 'answer_verified' => true, ]); $updatedCount++; } } // Call LearningAnalytics API $client = new \App\Services\LearningAnalyticsClient(); $results = $client->analyze($record); $aiUpdated = 0; // LearningAnalyticsClient已经处理了数据更新,这里只需要更新记录状态 if (is_array($results) && count($results) > 0) { $aiUpdated = count($results); // 更新记录状态为已分析 $record->update([ 'ai_analyzed_at' => now(), 'ai_analysis_count' => $aiUpdated, ]); // 重新检查分析结果状态 $this->checkAnalysisResults($record); } Notification::make() ->title('分析完成') ->body("已更新 {$updatedCount} 道题的答案,AI 分析完成 {$aiUpdated} 道题目") ->success() ->send(); } public function startRecognition(): void { $record = $this->record(); if (! $record) { Notification::make() ->title('记录不存在') ->danger() ->send(); return; } if ($record->status === 'processing') { Notification::make() ->title('正在处理中') ->warning() ->send(); return; } ProcessOCRRecord::dispatch($record); $record->update(['status' => 'processing']); Notification::make() ->title('开始识别') ->body('OCR识别任务已启动,请稍后刷新查看结果') ->success() ->send(); } public function getStatusBadgeConfig(string $status): array { return match ($status) { 'pending' => ['class' => 'badge-warning', 'text' => '待处理'], 'processing' => ['class' => 'badge-info', 'text' => '处理中'], 'completed' => ['class' => 'badge-success', 'text' => '已完成'], 'failed' => ['class' => 'badge-error', 'text' => '失败'], default => ['class' => 'badge-ghost', 'text' => $status], }; } }