0 ? $batchSize : self::DEFAULT_BATCH_SIZE; $pending = fn () => DB::table('gift_cards') ->where('giftcard_status', GiftCards::STATUS_UNUSED) ->whereNotNull('expirationdate') ->where('expirationdate', '<=', $at); if ($dryRun) { return (int) $pending()->count(); } $total = 0; // 按 id 分批,避免一次性锁定/更新过多行。 $pending() ->select('id') ->chunkById($batchSize, function ($rows) use (&$total, $at) { $total += DB::table('gift_cards') ->whereIn('id', $rows->pluck('id')->all()) ->update([ 'giftcard_status' => GiftCards::STATUS_EXPIRED, 'updated_at' => $at, ]); }); return $total; } }