initialize(); use app\adminapi\logic\tcm\PrescriptionAiLogic; use think\facade\Db; $apply = in_array('--apply', $argv, true); $batchIds = array_values(array_filter(array_map('intval', array_slice($argv, 1)), static fn (int $id): bool => $id > 0)); $rows = Db::name('prescription_ai_task')->where('status', 'failed') ->field('id,batch_id,model_key,attempts,manual_retries,error_code')->order('id')->select()->toArray(); $out = ['apply' => $apply, 'candidates' => [], 'results' => []]; foreach ($rows as $row) { if ($batchIds !== [] && !in_array((int) $row['batch_id'], $batchIds, true)) { continue; } $batch = Db::name('prescription_ai_batch')->where('id', $row['batch_id']) ->field('id,prescription_id,actor_id,validity,status')->find(); $entry = [ 'task_id' => (int) $row['id'], 'batch_id' => (int) $row['batch_id'], 'prescription_id' => (int) ($batch['prescription_id'] ?? 0), 'model_key' => $row['model_key'], 'error_code' => $row['error_code'], 'manual_retries' => (int) $row['manual_retries'], 'validity' => $batch['validity'] ?? '', ]; $out['candidates'][] = $entry; if (!$apply || ($batch['validity'] ?? '') !== 'current') { continue; } $actor = (int) ($batch['actor_id'] ?? 0); $info = \app\common\service\prescriptionai\PrescriptionAiAccess::actor($actor); try { $out['results'][] = $entry + ['queued' => PrescriptionAiLogic::retry( (int) $row['batch_id'], (string) $row['model_key'], $actor, (array) $info)]; } catch (\Throwable $e) { $out['results'][] = $entry + ['refused' => $e->getMessage()]; } } echo json_encode($out, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";