|
@@ -2,32 +2,89 @@
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
-use App\Services\ExamPdfExportService;
|
|
|
|
|
|
|
+use App\Jobs\GenerateAnalysisPdfJob;
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Request;
|
|
|
|
|
+use Illuminate\Support\Facades\Cache;
|
|
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
|
|
class ExamAnalysisPdfController extends Controller
|
|
class ExamAnalysisPdfController extends Controller
|
|
|
{
|
|
{
|
|
|
- public function show(Request $request, ExamPdfExportService $pdfExportService)
|
|
|
|
|
|
|
+ private const ENQUEUE_LOCK_TTL_SECONDS = 120;
|
|
|
|
|
+
|
|
|
|
|
+ public function show(Request $request)
|
|
|
{
|
|
{
|
|
|
$paperId = $request->query('paperId');
|
|
$paperId = $request->query('paperId');
|
|
|
$studentId = $request->query('studentId');
|
|
$studentId = $request->query('studentId');
|
|
|
$recordId = $request->query('recordId'); // 可选的OCR记录ID
|
|
$recordId = $request->query('recordId'); // 可选的OCR记录ID
|
|
|
|
|
|
|
|
if (!$paperId || !$studentId) {
|
|
if (!$paperId || !$studentId) {
|
|
|
- return response('paperId 和 studentId 不能为空', 400);
|
|
|
|
|
|
|
+ return response()->json([
|
|
|
|
|
+ 'success' => false,
|
|
|
|
|
+ 'message' => 'paperId 和 studentId 不能为空',
|
|
|
|
|
+ ], 400);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $pdfUrl = $pdfExportService->generateAnalysisReportPdf($paperId, $studentId, $recordId);
|
|
|
|
|
- if (!$pdfUrl) {
|
|
|
|
|
- Log::error('ExamAnalysisPdfController: 学情报告生成失败', [
|
|
|
|
|
- 'paper_id' => $paperId,
|
|
|
|
|
- 'student_id' => $studentId,
|
|
|
|
|
- 'record_id' => $recordId,
|
|
|
|
|
|
|
+ // 已生成则直接返回链接
|
|
|
|
|
+ $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,
|
|
|
|
|
+ ],
|
|
|
]);
|
|
]);
|
|
|
- return response('生成学情报告失败,请稍后重试', 500);
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return redirect($pdfUrl);
|
|
|
|
|
|
|
+ $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);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|