onQueue('pdf'); $this->afterCommit(); } public int $tries = 3; public int $timeout = 300; public function handle( ExamPdfExportService $examPdfExportService, TaskManager $taskManager ): void { try { $record = KnowledgeExplanation::query() ->where('knowledge_id', $this->knowledgeId) ->first(); if (! $record) { $taskManager->markTaskFailed($this->taskId, '知识点讲解记录不存在'); return; } $taskManager->updateTaskProgress($this->taskId, 70, '开始渲染知识点讲解PDF...'); $pdfUrl = $examPdfExportService->generateKnowledgeExplanationStandalonePdf($record, $this->knowledgePoints); if (! $pdfUrl) { $record->update(['status' => 'failed']); $taskManager->markTaskFailed($this->taskId, '知识点讲解PDF生成失败'); return; } $record->update([ 'status' => 'completed', 'pdf_url' => $pdfUrl, 'generated_at' => now(), ]); $this->syncPaperRecord($record, $pdfUrl); $taskManager->markTaskCompleted($this->taskId, [ 'paper_id' => $this->knowledgeId, 'knowledge_id' => $this->knowledgeId, 'pdfs' => [ 'all_pdf' => $pdfUrl, ], 'exam_content' => $this->buildKnowledgeExamContent($record, $pdfUrl), ]); $taskManager->sendCallback($this->taskId); } catch (\Throwable $e) { Log::error('GenerateKnowledgeExplanationPdfJob 失败', [ 'task_id' => $this->taskId, 'knowledge_id' => $this->knowledgeId, 'error' => $e->getMessage(), ]); KnowledgeExplanation::query() ->where('knowledge_id', $this->knowledgeId) ->update(['status' => 'failed']); $taskManager->markTaskFailed($this->taskId, $e->getMessage()); } } public function failed(Throwable $exception): void { KnowledgeExplanation::query() ->where('knowledge_id', $this->knowledgeId) ->update(['status' => 'failed']); app(TaskManager::class)->markTaskFailed($this->taskId, $exception->getMessage()); } private function syncPaperRecord(KnowledgeExplanation $record, string $pdfUrl): void { $paperId = (string) ($record->knowledge_id ?? ''); if ($paperId === '') { return; } $displayCode = (string) preg_replace('/^(paper_|knowledge_)/', '', $paperId); if ($displayCode === '') { $displayCode = $paperId; } Paper::query()->updateOrCreate( ['paper_id' => $paperId], [ 'student_id' => (string) ($record->student_id ?? ''), 'teacher_id' => (string) ($record->teacher_id ?? ''), 'params' => [ 'source' => 'knowledge_explanation', 'knowledge_id' => $paperId, 'kp_codes' => is_array($record->kp_codes) ? $record->kp_codes : [], ], 'paper_name' => '知识点讲解_' . $displayCode, 'paper_type' => 22, 'total_questions' => 0, 'total_score' => 0, 'status' => 'completed', 'difficulty_category' => null, 'exam_pdf_url' => $pdfUrl, 'grading_pdf_url' => null, 'all_pdf_url' => $pdfUrl, 'completed_at' => now(), ] ); } private function buildKnowledgeExamContent(KnowledgeExplanation $record, string $pdfUrl): array { $paperId = (string) ($record->knowledge_id ?? ''); $displayCode = (string) preg_replace('/^(paper_|knowledge_)/', '', $paperId); if ($displayCode === '') { $displayCode = $paperId; } $kpCodes = is_array($record->kp_codes) ? array_values($record->kp_codes) : []; return [ 'paper_info' => [ 'paper_id' => $paperId, 'paper_name' => '知识点讲解_' . $displayCode, 'student_id' => (string) ($record->student_id ?? ''), 'teacher_id' => (string) ($record->teacher_id ?? ''), 'total_questions' => 0, 'total_score' => 0, 'difficulty_category' => null, 'exam_code' => $displayCode, 'grading_code' => null, 'paper_id_num' => $displayCode, ], 'knowledge_points' => $kpCodes, 'statistics' => [ 'type_distribution' => [ 'choice' => 0, 'fill' => 0, 'answer' => 0, ], 'difficulty_distribution' => [], 'knowledge_point_distribution' => array_fill_keys($kpCodes, 0), 'average_difficulty' => null, 'total_estimated_time' => 0, ], 'pdfs' => [ 'all_pdf' => $pdfUrl, ], 'source' => 'knowledge_explanation', ]; } }