|
|
@@ -1425,4 +1425,78 @@ class ExamPdfController extends Controller
|
|
|
], 500);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按卷子 ID 数组批量重新生成 PDF,投递到队列异步处理
|
|
|
+ *
|
|
|
+ * @return \Illuminate\Http\JsonResponse
|
|
|
+ */
|
|
|
+ public function regeneratePdfBatchByIds(Request $request)
|
|
|
+ {
|
|
|
+ $paperIds = $request->input('paper_ids');
|
|
|
+
|
|
|
+ if (empty($paperIds) || ! is_array($paperIds)) {
|
|
|
+ return response()->json([
|
|
|
+ 'success' => false,
|
|
|
+ 'message' => '参数 paper_ids 必填,且为数组(如 ["paper_123","paper_456"])',
|
|
|
+ ], 400);
|
|
|
+ }
|
|
|
+
|
|
|
+ $paperIds = array_values(array_unique(array_filter(array_map('trim', $paperIds))));
|
|
|
+ $validPattern = '/^paper_\d+$/';
|
|
|
+ $invalid = array_filter($paperIds, fn ($id) => ! preg_match($validPattern, $id));
|
|
|
+ if (! empty($invalid)) {
|
|
|
+ return response()->json([
|
|
|
+ 'success' => false,
|
|
|
+ 'message' => 'paper_ids 格式错误,需为 paper_ 开头的数字',
|
|
|
+ 'invalid' => array_values($invalid),
|
|
|
+ ], 400);
|
|
|
+ }
|
|
|
+
|
|
|
+ $includeKpExplain = $request->has('include_kp_explain')
|
|
|
+ ? filter_var($request->input('include_kp_explain'), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)
|
|
|
+ : null;
|
|
|
+
|
|
|
+ Log::info('RegeneratePdfBatchByIds: 投递队列', ['paper_ids' => $paperIds, 'count' => count($paperIds)]);
|
|
|
+
|
|
|
+ try {
|
|
|
+ $papers = Paper::with('questions')
|
|
|
+ ->whereIn('paper_id', $paperIds)
|
|
|
+ ->get()
|
|
|
+ ->keyBy('paper_id');
|
|
|
+
|
|
|
+ $queued = [];
|
|
|
+ $skipped = 0;
|
|
|
+ foreach ($paperIds as $paperId) {
|
|
|
+ $paper = $papers->get($paperId);
|
|
|
+ if (! $paper || $paper->questions->isEmpty()) {
|
|
|
+ $skipped++;
|
|
|
+
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ RegeneratePdfJob::dispatch($paperId, $includeKpExplain);
|
|
|
+ $queued[] = $paperId;
|
|
|
+ }
|
|
|
+
|
|
|
+ Log::info('RegeneratePdfBatchByIds: 已投递', [
|
|
|
+ 'queued' => count($queued),
|
|
|
+ 'skipped' => $skipped,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ return response()->json([
|
|
|
+ 'success' => true,
|
|
|
+ 'message' => '已投递到 pdf 队列,请确保 queue worker 正在运行',
|
|
|
+ 'queued' => count($queued),
|
|
|
+ 'skipped' => $skipped,
|
|
|
+ 'paper_ids' => $queued,
|
|
|
+ ]);
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('RegeneratePdfBatchByIds: 批量异常', ['error' => $e->getMessage()]);
|
|
|
+
|
|
|
+ return response()->json([
|
|
|
+ 'success' => false,
|
|
|
+ 'message' => '批量投递异常:'.$e->getMessage(),
|
|
|
+ ], 500);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|