| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- <?php
- namespace App\Jobs;
- use App\Models\KnowledgeExplanation;
- use App\Models\Paper;
- use App\Services\ExamPdfExportService;
- use App\Services\TaskManager;
- use Illuminate\Bus\Queueable;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Illuminate\Foundation\Bus\Dispatchable;
- use Illuminate\Queue\InteractsWithQueue;
- use Illuminate\Queue\SerializesModels;
- use Illuminate\Support\Facades\Log;
- use Throwable;
- class GenerateKnowledgeExplanationPdfJob implements ShouldQueue
- {
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- public function __construct(
- public string $taskId,
- public string $knowledgeId,
- public array $knowledgePoints
- ) {
- $this->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',
- ];
- }
- }
|