GenerateKnowledgeExplanationPdfJob.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\KnowledgeExplanation;
  4. use App\Models\Paper;
  5. use App\Services\ExamPdfExportService;
  6. use App\Services\TaskManager;
  7. use Illuminate\Bus\Queueable;
  8. use Illuminate\Contracts\Queue\ShouldQueue;
  9. use Illuminate\Foundation\Bus\Dispatchable;
  10. use Illuminate\Queue\InteractsWithQueue;
  11. use Illuminate\Queue\SerializesModels;
  12. use Illuminate\Support\Facades\Log;
  13. use Throwable;
  14. class GenerateKnowledgeExplanationPdfJob implements ShouldQueue
  15. {
  16. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  17. public function __construct(
  18. public string $taskId,
  19. public string $knowledgeId,
  20. public array $knowledgePoints
  21. ) {
  22. $this->onQueue('pdf');
  23. $this->afterCommit();
  24. }
  25. public int $tries = 3;
  26. public int $timeout = 300;
  27. public function handle(
  28. ExamPdfExportService $examPdfExportService,
  29. TaskManager $taskManager
  30. ): void {
  31. try {
  32. $record = KnowledgeExplanation::query()
  33. ->where('knowledge_id', $this->knowledgeId)
  34. ->first();
  35. if (! $record) {
  36. $taskManager->markTaskFailed($this->taskId, '知识点讲解记录不存在');
  37. return;
  38. }
  39. $taskManager->updateTaskProgress($this->taskId, 70, '开始渲染知识点讲解PDF...');
  40. $pdfUrl = $examPdfExportService->generateKnowledgeExplanationStandalonePdf($record, $this->knowledgePoints);
  41. if (! $pdfUrl) {
  42. $record->update(['status' => 'failed']);
  43. $taskManager->markTaskFailed($this->taskId, '知识点讲解PDF生成失败');
  44. return;
  45. }
  46. $record->update([
  47. 'status' => 'completed',
  48. 'pdf_url' => $pdfUrl,
  49. 'generated_at' => now(),
  50. ]);
  51. $this->syncPaperRecord($record, $pdfUrl);
  52. $taskManager->markTaskCompleted($this->taskId, [
  53. 'paper_id' => $this->knowledgeId,
  54. 'knowledge_id' => $this->knowledgeId,
  55. 'pdfs' => [
  56. 'all_pdf' => $pdfUrl,
  57. ],
  58. 'exam_content' => [],
  59. ]);
  60. $taskManager->sendCallback($this->taskId);
  61. } catch (\Throwable $e) {
  62. Log::error('GenerateKnowledgeExplanationPdfJob 失败', [
  63. 'task_id' => $this->taskId,
  64. 'knowledge_id' => $this->knowledgeId,
  65. 'error' => $e->getMessage(),
  66. ]);
  67. KnowledgeExplanation::query()
  68. ->where('knowledge_id', $this->knowledgeId)
  69. ->update(['status' => 'failed']);
  70. $taskManager->markTaskFailed($this->taskId, $e->getMessage());
  71. }
  72. }
  73. public function failed(Throwable $exception): void
  74. {
  75. KnowledgeExplanation::query()
  76. ->where('knowledge_id', $this->knowledgeId)
  77. ->update(['status' => 'failed']);
  78. app(TaskManager::class)->markTaskFailed($this->taskId, $exception->getMessage());
  79. }
  80. private function syncPaperRecord(KnowledgeExplanation $record, string $pdfUrl): void
  81. {
  82. $paperId = (string) ($record->knowledge_id ?? '');
  83. if ($paperId === '') {
  84. return;
  85. }
  86. $displayCode = (string) preg_replace('/^(paper_|knowledge_)/', '', $paperId);
  87. if ($displayCode === '') {
  88. $displayCode = $paperId;
  89. }
  90. Paper::query()->updateOrCreate(
  91. ['paper_id' => $paperId],
  92. [
  93. 'student_id' => (string) ($record->student_id ?? ''),
  94. 'teacher_id' => (string) ($record->teacher_id ?? ''),
  95. 'params' => [
  96. 'source' => 'knowledge_explanation',
  97. 'knowledge_id' => $paperId,
  98. 'kp_codes' => is_array($record->kp_codes) ? $record->kp_codes : [],
  99. ],
  100. 'paper_name' => '知识点讲解_' . $displayCode,
  101. 'paper_type' => 22,
  102. 'total_questions' => 0,
  103. 'total_score' => 0,
  104. 'status' => 'completed',
  105. 'difficulty_category' => null,
  106. 'exam_pdf_url' => $pdfUrl,
  107. 'grading_pdf_url' => null,
  108. 'all_pdf_url' => $pdfUrl,
  109. 'completed_at' => now(),
  110. ]
  111. );
  112. }
  113. }