feat: 添加新的错误码以支持配置版本管理,更新彩票配置以启用手动审核,增强 API 路由以支持玩法和赔率版本化管理

This commit is contained in:
2026-05-11 10:08:48 +08:00
parent aeaf124096
commit 067c2b39f5
41 changed files with 2578 additions and 1 deletions

View File

@@ -0,0 +1,183 @@
<?php
namespace App\Services\Config;
use App\Lottery\ConfigVersionStatus;
use App\Models\Currency;
use App\Models\OddsItem;
use App\Models\OddsVersion;
use App\Models\PlayConfigItem;
use App\Models\PlayConfigVersion;
use App\Models\PlayType;
use App\Models\RiskCapItem;
use App\Models\RiskCapVersion;
use Illuminate\Support\Collection;
/**
* 玩家端:当前生效的玩法目录 + 三套版本快照(只读)。
*/
final class EffectivePlayCatalogService
{
/**
* @return array<string, mixed>
*/
public function build(?string $currencyCode = null): array
{
$currency = $this->resolveBettableCurrency($currencyCode);
$playVersion = PlayConfigVersion::query()
->where('status', ConfigVersionStatus::Active->value)
->firstOrFail();
$oddsVersion = OddsVersion::query()
->where('status', ConfigVersionStatus::Active->value)
->firstOrFail();
$riskVersion = RiskCapVersion::query()
->where('status', ConfigVersionStatus::Active->value)
->firstOrFail();
$playTypes = PlayType::query()->orderBy('sort_order')->orderBy('play_code')->get();
/** @var Collection<string, PlayConfigItem> $configByCode */
$configByCode = PlayConfigItem::query()
->where('version_id', $playVersion->id)
->get()
->keyBy('play_code');
$oddsRows = OddsItem::query()
->where('version_id', $oddsVersion->id)
->where('currency_code', $currency->code)
->get();
/** @var Collection<string, Collection<int, OddsItem>> */
$oddsByPlay = $oddsRows->groupBy('play_code');
$riskItems = RiskCapItem::query()
->where('version_id', $riskVersion->id)
->orderBy('normalized_number')
->get();
$plays = $playTypes->map(function (PlayType $pt) use ($configByCode, $oddsByPlay): array {
$c = $configByCode->get($pt->play_code);
$items = $oddsByPlay->get($pt->play_code, collect());
$o = $this->pickPrimaryOddsItem($items);
return [
'play_code' => $pt->play_code,
'category' => $pt->category,
'dimension' => $pt->dimension,
'bet_mode' => $pt->bet_mode,
'display_name_zh' => $pt->display_name_zh,
'display_name_en' => $pt->display_name_en,
'display_name_ne' => $pt->display_name_ne,
'sort_order' => (int) $pt->sort_order,
'supports_multi_number' => (bool) $pt->supports_multi_number,
'master_enabled' => (bool) $pt->is_enabled,
'config' => $c === null ? null : $this->serializePlayConfigItem($c),
'odds' => $o === null ? null : $this->serializeOddsItem($o),
];
})->values()->all();
return [
'currency_code' => $currency->code,
'effective_versions' => [
'play_config' => $this->serializeVersionHead($playVersion),
'odds' => $this->serializeVersionHead($oddsVersion),
'risk_cap' => $this->serializeVersionHead($riskVersion),
],
'plays' => $plays,
'risk_cap_items' => $riskItems->map(fn (RiskCapItem $r) => $this->serializeRiskItem($r))->all(),
];
}
/**
* 大厅列表展示单档赔率:优先头奖档 {@see first},兼容历史 {@see default}
*
* @param Collection<int, OddsItem> $items
*/
private function pickPrimaryOddsItem(Collection $items): ?OddsItem
{
if ($items->isEmpty()) {
return null;
}
foreach (['first', 'default', 'second', 'third', 'starter', 'consolation'] as $scope) {
$hit = $items->firstWhere('prize_scope', $scope);
if ($hit !== null) {
return $hit;
}
}
return $items->first();
}
private function resolveBettableCurrency(?string $currencyCode): Currency
{
if ($currencyCode !== null && $currencyCode !== '') {
$row = Currency::query()->where('code', strtoupper($currencyCode))->first();
if ($row === null || ! $row->is_enabled || ! $row->is_bettable) {
throw new \InvalidArgumentException('currency');
}
return $row;
}
return Currency::query()
->where('is_enabled', true)
->where('is_bettable', true)
->orderBy('code')
->firstOrFail();
}
/** @return array<string, mixed> */
private function serializeVersionHead(PlayConfigVersion|OddsVersion|RiskCapVersion $v): array
{
return [
'id' => (int) $v->getKey(),
'version_no' => (int) $v->version_no,
'effective_at' => $v->effective_at?->toIso8601String(),
];
}
/** @return array<string, mixed> */
private function serializePlayConfigItem(PlayConfigItem $r): array
{
return [
'is_enabled' => (bool) $r->is_enabled,
'min_bet_amount' => (int) $r->min_bet_amount,
'max_bet_amount' => (int) $r->max_bet_amount,
'display_order' => (int) $r->display_order,
'rule_text_zh' => $r->rule_text_zh,
'rule_text_en' => $r->rule_text_en,
'rule_text_ne' => $r->rule_text_ne,
'extra_config_json' => $r->extra_config_json,
];
}
/** @return array<string, mixed> */
private function serializeOddsItem(OddsItem $r): array
{
return [
'prize_scope' => $r->prize_scope,
'odds_value' => (int) $r->odds_value,
'rebate_rate' => (string) $r->rebate_rate,
'commission_rate' => (string) $r->commission_rate,
'currency_code' => $r->currency_code,
'extra_config_json' => $r->extra_config_json,
/** 赔率乘数小数位 = odds_value / 10000 */
'odds_multiplier' => round($r->odds_value / 10000, 4),
];
}
/** @return array<string, mixed> */
private function serializeRiskItem(RiskCapItem $r): array
{
return [
'draw_id' => $r->draw_id,
'normalized_number' => $r->normalized_number,
'cap_amount' => (int) $r->cap_amount,
'cap_type' => $r->cap_type,
];
}
}

View File

@@ -0,0 +1,163 @@
<?php
namespace App\Services\Config;
use App\Lottery\ConfigVersionStatus;
use App\Models\AdminUser;
use App\Models\Currency;
use App\Models\OddsItem;
use App\Models\OddsVersion;
use App\Models\PlayType;
use App\Services\AuditLogger;
use App\Support\OddsStandardScopes;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
/** 后台:赔率版本({@see odds_versions} / {@see odds_items} */
final class OddsStreamService
{
/** @return LengthAwarePaginator<int, OddsVersion> */
public function paginate(?string $status, int $perPage): LengthAwarePaginator
{
$q = OddsVersion::query()->orderByDesc('id');
if ($status !== null && $status !== '') {
$q->where('status', $status);
}
return $q->paginate($perPage);
}
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<int, array<string, mixed>> $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,
);
}
/** @return array<string, mixed> */
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(),
];
}
}

View File

@@ -0,0 +1,161 @@
<?php
namespace App\Services\Config;
use App\Lottery\ConfigVersionStatus;
use App\Models\AdminUser;
use App\Models\PlayConfigItem;
use App\Models\PlayConfigVersion;
use App\Models\PlayType;
use App\Services\AuditLogger;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
/** 后台:玩法配置版本({@see play_config_versions} / {@see play_config_items} */
final class PlayConfigStreamService
{
/** @return LengthAwarePaginator<int, PlayConfigVersion> */
public function paginate(?string $status, int $perPage): LengthAwarePaginator
{
$q = PlayConfigVersion::query()->orderByDesc('id');
if ($status !== null && $status !== '') {
$q->where('status', $status);
}
return $q->paginate($perPage);
}
public function createDraft(AdminUser $admin, ?string $reason, ?int $cloneFromVersionId): PlayConfigVersion
{
$nextNo = (int) (PlayConfigVersion::query()->max('version_no') ?? 0) + 1;
return DB::transaction(function () use ($admin, $reason, $cloneFromVersionId, $nextNo): PlayConfigVersion {
$draft = PlayConfigVersion::query()->create([
'version_no' => $nextNo,
'status' => ConfigVersionStatus::Draft->value,
'effective_at' => null,
'updated_by' => $admin->id,
'reason' => $reason,
]);
$source = null;
if ($cloneFromVersionId !== null) {
$source = PlayConfigVersion::query()->whereKey($cloneFromVersionId)->firstOrFail();
} else {
$source = PlayConfigVersion::query()
->where('status', ConfigVersionStatus::Active->value)
->first();
}
if ($source !== null) {
foreach ($source->items()->orderBy('play_code')->get() as $row) {
PlayConfigItem::query()->create([
'version_id' => $draft->id,
'play_code' => $row->play_code,
'is_enabled' => $row->is_enabled,
'min_bet_amount' => $row->min_bet_amount,
'max_bet_amount' => $row->max_bet_amount,
'display_order' => $row->display_order,
'rule_text_zh' => $row->rule_text_zh,
'rule_text_en' => $row->rule_text_en,
'rule_text_ne' => $row->rule_text_ne,
'extra_config_json' => $row->extra_config_json,
]);
}
} else {
foreach (PlayType::query()->orderBy('sort_order')->orderBy('play_code')->get() as $pt) {
PlayConfigItem::query()->create([
'version_id' => $draft->id,
'play_code' => $pt->play_code,
'is_enabled' => (bool) $pt->is_enabled,
'min_bet_amount' => 100,
'max_bet_amount' => 500_000_000,
'display_order' => (int) $pt->sort_order,
'rule_text_zh' => null,
'rule_text_en' => null,
'rule_text_ne' => null,
'extra_config_json' => null,
]);
}
}
return $draft->fresh(['items']);
});
}
/**
* @param array<int, array<string, mixed>> $items
*/
public function replaceItems(PlayConfigVersion $draft, array $items, AdminUser $admin): void
{
DB::transaction(function () use ($draft, $items, $admin): void {
PlayConfigItem::query()->where('version_id', $draft->id)->delete();
foreach ($items as $row) {
PlayConfigItem::query()->create([
'version_id' => $draft->id,
'play_code' => (string) $row['play_code'],
'is_enabled' => (bool) ($row['is_enabled'] ?? true),
'min_bet_amount' => (int) ($row['min_bet_amount'] ?? 0),
'max_bet_amount' => (int) ($row['max_bet_amount'] ?? 0),
'display_order' => (int) ($row['display_order'] ?? 0),
'rule_text_zh' => isset($row['rule_text_zh']) ? (string) $row['rule_text_zh'] : null,
'rule_text_en' => isset($row['rule_text_en']) ? (string) $row['rule_text_en'] : null,
'rule_text_ne' => isset($row['rule_text_ne']) ? (string) $row['rule_text_ne'] : null,
'extra_config_json' => $row['extra_config_json'] ?? null,
]);
}
$draft->forceFill(['updated_by' => $admin->id])->save();
});
}
public function publish(PlayConfigVersion $draft, AdminUser $admin, ?Request $request = null): void
{
$before = $this->snapshotVersion($draft);
DB::transaction(function () use ($draft, $admin): void {
/** @var PlayConfigVersion|null $current */
$current = PlayConfigVersion::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: 'play_config',
actionCode: 'publish',
targetType: 'play_config_version',
targetId: (string) $draft->id,
beforeJson: $before,
afterJson: $after,
);
}
/** @return array<string, mixed> */
private function snapshotVersion(PlayConfigVersion $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(),
];
}
}

View File

@@ -0,0 +1,145 @@
<?php
namespace App\Services\Config;
use App\Lottery\ConfigVersionStatus;
use App\Models\AdminUser;
use App\Models\RiskCapItem;
use App\Models\RiskCapVersion;
use App\Services\AuditLogger;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
/** 后台:风控封顶版本({@see risk_cap_versions} / {@see risk_cap_items} */
final class RiskCapStreamService
{
/** @return LengthAwarePaginator<int, RiskCapVersion> */
public function paginate(?string $status, int $perPage): LengthAwarePaginator
{
$q = RiskCapVersion::query()->orderByDesc('id');
if ($status !== null && $status !== '') {
$q->where('status', $status);
}
return $q->paginate($perPage);
}
public function createDraft(AdminUser $admin, ?string $reason, ?int $cloneFromVersionId): RiskCapVersion
{
$nextNo = (int) (RiskCapVersion::query()->max('version_no') ?? 0) + 1;
return DB::transaction(function () use ($admin, $reason, $cloneFromVersionId, $nextNo): RiskCapVersion {
$draft = RiskCapVersion::query()->create([
'version_no' => $nextNo,
'status' => ConfigVersionStatus::Draft->value,
'effective_at' => null,
'updated_by' => $admin->id,
'reason' => $reason,
]);
$source = null;
if ($cloneFromVersionId !== null) {
$source = RiskCapVersion::query()->whereKey($cloneFromVersionId)->firstOrFail();
} else {
$source = RiskCapVersion::query()
->where('status', ConfigVersionStatus::Active->value)
->first();
}
if ($source !== null) {
foreach ($source->items()->orderBy('normalized_number')->get() as $row) {
RiskCapItem::query()->create([
'version_id' => $draft->id,
'draw_id' => $row->draw_id,
'normalized_number' => $row->normalized_number,
'cap_amount' => $row->cap_amount,
'cap_type' => $row->cap_type,
]);
}
} else {
foreach (['0000', '1234', '9999'] as $num) {
RiskCapItem::query()->create([
'version_id' => $draft->id,
'draw_id' => null,
'normalized_number' => $num,
'cap_amount' => 50_000_000_000,
'cap_type' => 'per_number',
]);
}
}
return $draft->fresh(['items']);
});
}
/**
* @param array<int, array<string, mixed>> $items
*/
public function replaceItems(RiskCapVersion $draft, array $items, AdminUser $admin): void
{
DB::transaction(function () use ($draft, $items, $admin): void {
RiskCapItem::query()->where('version_id', $draft->id)->delete();
foreach ($items as $row) {
RiskCapItem::query()->create([
'version_id' => $draft->id,
'draw_id' => isset($row['draw_id']) ? (int) $row['draw_id'] : null,
'normalized_number' => (string) $row['normalized_number'],
'cap_amount' => (int) $row['cap_amount'],
'cap_type' => (string) $row['cap_type'],
]);
}
$draft->forceFill(['updated_by' => $admin->id])->save();
});
}
public function publish(RiskCapVersion $draft, AdminUser $admin, ?Request $request = null): void
{
$before = $this->snapshotVersion($draft);
DB::transaction(function () use ($draft, $admin): void {
/** @var RiskCapVersion|null $current */
$current = RiskCapVersion::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: 'risk_cap',
actionCode: 'publish',
targetType: 'risk_cap_version',
targetId: (string) $draft->id,
beforeJson: $before,
afterJson: $after,
);
}
/** @return array<string, mixed> */
private function snapshotVersion(RiskCapVersion $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(),
];
}
}