| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php
- namespace App\Http\Controllers;
- use App\Jobs\GenerateAnalysisPdfJob;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- class ExamAnalysisPdfController extends Controller
- {
- private const ENQUEUE_LOCK_TTL_SECONDS = 120;
- public function show(Request $request)
- {
- $paperId = $request->query('paperId');
- $studentId = $request->query('studentId');
- $recordId = $request->query('recordId'); // 可选的OCR记录ID
- if (!$paperId || !$studentId) {
- return response()->json([
- 'success' => false,
- 'message' => 'paperId 和 studentId 不能为空',
- ], 400);
- }
- // 已生成则直接返回链接
- $existingPdfUrl = DB::connection('mysql')
- ->table('exam_analysis_results')
- ->where('paper_id', $paperId)
- ->where('student_id', $studentId)
- ->whereNotNull('analysis_pdf_url')
- ->where('analysis_pdf_url', '!=', '')
- ->orderByDesc('updated_at')
- ->value('analysis_pdf_url');
- if ($existingPdfUrl) {
- return response()->json([
- 'success' => true,
- 'message' => '学情报告已生成',
- 'data' => [
- 'paper_id' => $paperId,
- 'student_id' => $studentId,
- 'record_id' => $recordId,
- 'status' => 'completed',
- 'pdf_url' => $existingPdfUrl,
- 'queued' => false,
- ],
- ]);
- }
- $lockKey = sprintf('analysis_pdf:enqueue:%s:%s', $paperId, $studentId);
- $acquired = Cache::add($lockKey, now()->timestamp, now()->addSeconds(self::ENQUEUE_LOCK_TTL_SECONDS));
- if (! $acquired) {
- return response()->json([
- 'success' => true,
- 'message' => '学情报告正在生成中,请稍后重试',
- 'data' => [
- 'paper_id' => $paperId,
- 'student_id' => $studentId,
- 'record_id' => $recordId,
- 'status' => 'processing',
- 'pdf_url' => null,
- 'queued' => false,
- ],
- ], 202);
- }
- dispatch(new GenerateAnalysisPdfJob($paperId, $studentId, $recordId));
- Log::info('ExamAnalysisPdfController: 学情报告生成任务已入队', [
- 'paper_id' => $paperId,
- 'student_id' => $studentId,
- 'record_id' => $recordId,
- ]);
- return response()->json([
- 'success' => true,
- 'message' => '学情报告任务已入队,正在后台生成',
- 'data' => [
- 'paper_id' => $paperId,
- 'student_id' => $studentId,
- 'record_id' => $recordId,
- 'status' => 'processing',
- 'pdf_url' => null,
- 'queued' => true,
- ],
- ], 202);
- }
- }
|