ExamAnalysisPdfController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Jobs\GenerateAnalysisPdfJob;
  4. use App\Services\ExamAnalysisService;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Cache;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Log;
  9. class ExamAnalysisPdfController extends Controller
  10. {
  11. private const ENQUEUE_LOCK_TTL_SECONDS = 120;
  12. public function show(Request $request, ExamAnalysisService $examAnalysisService)
  13. {
  14. $paperId = $request->query('paperId');
  15. $studentId = $request->query('studentId');
  16. $recordId = $request->query('recordId'); // 可选的OCR记录ID
  17. if (!$paperId || !$studentId) {
  18. return response()->json([
  19. 'success' => false,
  20. 'message' => 'paperId 和 studentId 不能为空',
  21. ], 400);
  22. }
  23. // 已生成则直接返回链接
  24. $existingPdfUrl = DB::connection('mysql')
  25. ->table('exam_analysis_results')
  26. ->where('paper_id', $paperId)
  27. ->where('student_id', $studentId)
  28. ->whereNotNull('analysis_pdf_url')
  29. ->where('analysis_pdf_url', '!=', '')
  30. ->orderByDesc('updated_at')
  31. ->value('analysis_pdf_url');
  32. if ($existingPdfUrl) {
  33. return response()->json([
  34. 'success' => true,
  35. 'message' => '学情报告已生成',
  36. 'data' => [
  37. 'paper_id' => $paperId,
  38. 'student_id' => $studentId,
  39. 'record_id' => $recordId,
  40. 'status' => 'completed',
  41. 'pdf_url' => $existingPdfUrl,
  42. 'queued' => false,
  43. ],
  44. ]);
  45. }
  46. try {
  47. $examAnalysisService->validateAnalysisReportEnqueue($paperId, $studentId, $recordId);
  48. } catch (\InvalidArgumentException $e) {
  49. return response()->json([
  50. 'success' => false,
  51. 'message' => $e->getMessage(),
  52. ], 422);
  53. }
  54. $lockKey = sprintf('analysis_pdf:enqueue:%s:%s', $paperId, $studentId);
  55. $acquired = Cache::add($lockKey, now()->timestamp, now()->addSeconds(self::ENQUEUE_LOCK_TTL_SECONDS));
  56. if (! $acquired) {
  57. return response()->json([
  58. 'success' => true,
  59. 'message' => '学情报告正在生成中,请稍后重试',
  60. 'data' => [
  61. 'paper_id' => $paperId,
  62. 'student_id' => $studentId,
  63. 'record_id' => $recordId,
  64. 'status' => 'processing',
  65. 'pdf_url' => null,
  66. 'queued' => false,
  67. ],
  68. ], 202);
  69. }
  70. dispatch(new GenerateAnalysisPdfJob($paperId, $studentId, $recordId));
  71. Log::info('ExamAnalysisPdfController: 学情报告生成任务已入队', [
  72. 'paper_id' => $paperId,
  73. 'student_id' => $studentId,
  74. 'record_id' => $recordId,
  75. ]);
  76. return response()->json([
  77. 'success' => true,
  78. 'message' => '学情报告任务已入队,正在后台生成',
  79. 'data' => [
  80. 'paper_id' => $paperId,
  81. 'student_id' => $studentId,
  82. 'record_id' => $recordId,
  83. 'status' => 'processing',
  84. 'pdf_url' => null,
  85. 'queued' => true,
  86. ],
  87. ], 202);
  88. }
  89. }