$domain, 'subtype' => $subtype, 'source_id' => $sourceId, ])->whereIn('status', [AiKbSyncJob::STATUS_PENDING, AiKbSyncJob::STATUS_PROCESSING]) ->order('id', 'desc') ->findOrEmpty(); if (!$pending->isEmpty() && (int) $pending->status === AiKbSyncJob::STATUS_PENDING) { $pending->action = $action; $pending->priority = $priority; $pending->scheduled_at = $scheduledAt; $pending->error_msg = ''; $pending->update_time = time(); $pending->save(); return; } AiKbSyncJob::create([ 'domain' => $domain, 'subtype' => $subtype, 'source_id' => $sourceId, 'action' => $action, 'priority' => $priority, 'status' => AiKbSyncJob::STATUS_PENDING, 'retry_count' => 0, 'error_msg' => '', 'scheduled_at' => $scheduledAt, 'processed_at' => 0, 'create_time' => time(), 'update_time' => time(), ]); } public static function markLotteryBackfillJobs(int $limit = 200): void { self::enqueueMissingLotteryRows('draw_result', 'lottery_draw_result', $limit); self::enqueueMissingLotteryRows('ai_history', 'lottery_ai_analysis', $limit); } public static function markMatchBackfillJobs(int $limit = 200): void { self::enqueueMissingRows('match', 'event', 'match', $limit); } private static function enqueueMissingLotteryRows(string $subtype, string $table, int $limit): void { self::enqueueMissingRows('lottery', $subtype, $table, $limit); } private static function enqueueMissingRows(string $domain, string $subtype, string $table, int $limit): void { try { $rows = Db::name($table)->alias('s') ->leftJoin('ai_kb_document d', "d.domain = '{$domain}' AND d.subtype = '{$subtype}' AND d.source_id = s.id") ->whereNull('d.id') ->where('s.id', '>', 0) ->order('s.id', 'desc') ->limit($limit) ->column('s.id'); foreach ($rows as $sourceId) { self::enqueue($domain, $subtype, (int) $sourceId, AiKbSyncJob::ACTION_UPSERT, 80); } } catch (\Throwable $e) { // Ignore in environments where KB tables are not ready yet. } } public static function markDocumentInactive(string $domain, string $subtype, int $sourceId): void { AiKbDocument::where([ 'domain' => $domain, 'subtype' => $subtype, 'source_id' => $sourceId, ])->update([ 'is_active' => 0, 'update_time' => time(), ]); } }