fix: 增强配置发布校验与关闭玩法清理提示

1. 发布赔率、玩法配置和风控封顶草稿前校验空配置、重复项、金额范围和合法性
2. 限制赔率返水与佣金比例在 0 到 1 之间
3. 投注预览和下单遇到已关闭玩法时返回需清理注项明细
This commit is contained in:
2026-05-16 09:54:47 +08:00
parent 87637f2e4a
commit f7f6c58b02
8 changed files with 299 additions and 6 deletions

View File

@@ -12,6 +12,7 @@ use App\Services\AuditLogger;
use Illuminate\Support\Facades\DB;
use App\Support\OddsStandardScopes;
use App\Lottery\ConfigVersionStatus;
use Illuminate\Validation\ValidationException;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
/** 后台:赔率版本({@see odds_versions} / {@see odds_items} */
@@ -115,6 +116,7 @@ final class OddsStreamService
public function publish(OddsVersion $draft, AdminUser $admin, ?Request $request = null): void
{
$this->validatePublishableDraft($draft);
$before = $this->snapshotVersion($draft);
DB::transaction(function () use ($draft, $admin): void {
@@ -180,4 +182,73 @@ final class OddsStreamService
'items_count' => $v->items()->count(),
];
}
private function validatePublishableDraft(OddsVersion $draft): void
{
$items = $draft->items()->orderBy('currency_code')->orderBy('play_code')->orderBy('prize_scope')->get();
$allowedPlayCodes = array_fill_keys(
PlayType::query()->pluck('play_code')->all(),
true,
);
$allowedCurrencyCodes = array_fill_keys(
Currency::query()
->where('is_bettable', true)
->where('is_enabled', true)
->pluck('code')
->map(fn (string $code) => strtoupper($code))
->all(),
true,
);
$allowedScopes = array_fill_keys(OddsStandardScopes::SCOPE_KEYS, true);
$errors = [];
$seenKeys = [];
if ($items->isEmpty()) {
$errors['items'][] = '草稿至少需要一条赔率配置';
}
foreach ($items as $index => $row) {
$playCode = (string) $row->play_code;
$scope = (string) $row->prize_scope;
$currencyCode = strtoupper((string) $row->currency_code);
$oddsValue = (int) $row->odds_value;
$rebateRate = (float) $row->rebate_rate;
$commissionRate = (float) $row->commission_rate;
$key = $playCode.'|'.$scope.'|'.$currencyCode;
if (! isset($allowedPlayCodes[$playCode])) {
$errors["items.$index.play_code"][] = '玩法不存在';
}
if (! isset($allowedScopes[$scope])) {
$errors["items.$index.prize_scope"][] = '奖项档位不合法';
}
if (! isset($allowedCurrencyCodes[$currencyCode])) {
$errors["items.$index.currency_code"][] = '币种不可下注';
}
if (isset($seenKeys[$key])) {
$errors["items.$index"][] = '同一玩法、档位、币种存在重复赔率项';
}
$seenKeys[$key] = true;
if ($oddsValue <= 0) {
$errors["items.$index.odds_value"][] = '赔率值必须大于 0';
}
if ($rebateRate < 0 || $rebateRate > 1) {
$errors["items.$index.rebate_rate"][] = '返水比例必须在 0 到 1 之间';
}
if ($commissionRate < 0 || $commissionRate > 1) {
$errors["items.$index.commission_rate"][] = '佣金比例必须在 0 到 1 之间';
}
}
if ($errors !== []) {
throw ValidationException::withMessages($errors);
}
}
}