*/ public function paginate(?string $status, int $perPage, int $page = 1): LengthAwarePaginator { $q = OddsVersion::query()->orderByDesc('id'); if ($status !== null && $status !== '') { $q->where('status', $status); } return $q->paginate($perPage, ['*'], 'page', max(1, $page)); } public function createDraft(AdminUser $admin, ?string $reason, ?int $cloneFromVersionId): OddsVersion { $nextNo = (int) (OddsVersion::query()->max('version_no') ?? 0) + 1; return DB::transaction(function () use ($admin, $reason, $cloneFromVersionId, $nextNo): OddsVersion { $draft = OddsVersion::query()->create([ 'version_no' => $nextNo, 'status' => ConfigVersionStatus::Draft->value, 'effective_at' => null, 'updated_by' => $admin->id, 'reason' => $reason, ]); $source = null; if ($cloneFromVersionId !== null) { $source = OddsVersion::query()->whereKey($cloneFromVersionId)->firstOrFail(); } else { $source = OddsVersion::query() ->where('status', ConfigVersionStatus::Active->value) ->first(); } if ($source !== null) { foreach ($source->items()->orderBy('currency_code')->orderBy('play_code')->get() as $row) { OddsItem::query()->create([ 'version_id' => $draft->id, 'play_code' => $row->play_code, 'prize_scope' => $row->prize_scope, 'odds_value' => $row->odds_value, 'rebate_rate' => $row->rebate_rate, 'commission_rate' => $row->commission_rate, 'currency_code' => $row->currency_code, 'extra_config_json' => $row->extra_config_json, ]); } } else { $currency = Currency::query()->where('is_bettable', true)->where('is_enabled', true)->orderBy('code')->firstOrFail(); foreach (PlayType::query()->orderBy('sort_order')->orderBy('play_code')->get() as $pt) { foreach (OddsStandardScopes::PRESET_ODDS_BY_SCOPE as $scope => $oddsValue) { OddsItem::query()->create([ 'version_id' => $draft->id, 'play_code' => $pt->play_code, 'prize_scope' => $scope, 'odds_value' => $oddsValue, 'rebate_rate' => 0, 'commission_rate' => 0, 'currency_code' => $currency->code, 'extra_config_json' => null, ]); } } } $draft->refresh(); OddsStandardScopes::syncMissingForVersion($draft); return $draft->fresh(['items']); }); } /** * @param array> $items */ public function replaceItems(OddsVersion $draft, array $items, AdminUser $admin): void { DB::transaction(function () use ($draft, $items, $admin): void { OddsItem::query()->where('version_id', $draft->id)->delete(); foreach ($items as $row) { OddsItem::query()->create([ 'version_id' => $draft->id, 'play_code' => (string) $row['play_code'], 'prize_scope' => (string) $row['prize_scope'], 'odds_value' => (int) $row['odds_value'], 'rebate_rate' => (float) ($row['rebate_rate'] ?? 0), 'commission_rate' => (float) ($row['commission_rate'] ?? 0), 'currency_code' => strtoupper((string) $row['currency_code']), 'extra_config_json' => $row['extra_config_json'] ?? null, ]); } $draft->forceFill(['updated_by' => $admin->id])->save(); }); } public function publish(OddsVersion $draft, AdminUser $admin, ?Request $request = null): void { $before = $this->snapshotVersion($draft); DB::transaction(function () use ($draft, $admin): void { /** @var OddsVersion|null $current */ $current = OddsVersion::query() ->where('status', ConfigVersionStatus::Active->value) ->lockForUpdate() ->first(); if ($current !== null) { $current->forceFill(['status' => ConfigVersionStatus::Archived->value])->save(); } $draft->forceFill([ 'status' => ConfigVersionStatus::Active->value, 'effective_at' => now(), 'updated_by' => $admin->id, ])->save(); }); $after = $this->snapshotVersion($draft->fresh(['items'])); AuditLogger::recordForAdmin( $admin, $request, moduleCode: 'odds', actionCode: 'publish', targetType: 'odds_version', targetId: (string) $draft->id, beforeJson: $before, afterJson: $after, ); } public function deleteVersion(OddsVersion $version, AdminUser $admin, ?Request $request = null): void { $before = $this->snapshotVersion($version); DB::transaction(function () use ($version): void { $version->delete(); }); AuditLogger::recordForAdmin( $admin, $request, moduleCode: 'odds', actionCode: 'delete', targetType: 'odds_version', targetId: (string) $version->id, beforeJson: $before, afterJson: null, ); } /** @return array */ private function snapshotVersion(OddsVersion $v): array { return [ 'id' => $v->id, 'version_no' => $v->version_no, 'status' => $v->status, 'effective_at' => $v->effective_at?->toIso8601String(), 'items_count' => $v->items()->count(), ]; } }