- 在赔率数据表中新增 provider_code 字段,默认值为 GLOBAL - 赔率接口与服务中新增 provider_code 参数支持,实现开注商特定赔率加载 - 解析赔率集合时,根据 provider_code 优先级进行排序与筛选,保证特定开注商赔率优先 - 优化赔率唯一索引,新增 provider_code 列以防止不同开注商赔率重复冲突 - 调整赔率相关验证规则,支持开注商代码的合法性与可选性检查 - 玩法解析与出票相关逻辑改造,支持多开注商环境下的投注处理 - 管理界面赔率替换接口增加 provider_code 字段支持 - 同步和测试用例覆盖 provider_code 相关功能,验证开注商赔率隔离与优先切换 - PlayEffectiveCatalogController 支持按 provider_code 返回赔率目录数据 - TicketPlacementService 与 TicketPreviewService 修改为支持 provider_code 多开注商玩法解析 - 维护 OddsStandardScopes 同步逻辑,保证开注商维度佣金与赔率正确同步 - AdminConfigPresenter 增加了对应数据的 provider_code 与 dimension 输出字段 - SDK接口添加 odds_snapshot_json 中开注商字段,完善赔率快照信息 - 路由配置调整管理文档地址,无关改动细微修改
196 lines
6.6 KiB
PHP
196 lines
6.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Ticket;
|
|
|
|
use App\Models\OddsItem;
|
|
use App\Lottery\ErrorCode;
|
|
use App\Models\OddsVersion;
|
|
use App\Models\RiskCapItem;
|
|
use App\Models\PlayConfigItem;
|
|
use App\Models\RiskCapVersion;
|
|
use App\Models\PlayConfigVersion;
|
|
use Illuminate\Support\Collection;
|
|
use App\Lottery\ConfigVersionStatus;
|
|
use App\Exceptions\TicketOperationException;
|
|
|
|
final class PlayCatalogResolver
|
|
{
|
|
/**
|
|
* 当前生效的三套配置版本号(无锁,供预览展示;与 {@see lockActiveConfigVersionsForPlacement} 配对使用)。
|
|
*
|
|
* @return array{play_config_version_no: int, odds_version_no: int, risk_cap_version_no: int}
|
|
*/
|
|
public function currentActiveVersionStamp(): array
|
|
{
|
|
$playV = PlayConfigVersion::query()
|
|
->where('status', ConfigVersionStatus::Active->value)
|
|
->orderBy('id')
|
|
->firstOrFail();
|
|
|
|
$oddsV = OddsVersion::query()
|
|
->where('status', ConfigVersionStatus::Active->value)
|
|
->orderBy('id')
|
|
->firstOrFail();
|
|
|
|
$riskV = RiskCapVersion::query()
|
|
->where('status', ConfigVersionStatus::Active->value)
|
|
->orderBy('id')
|
|
->firstOrFail();
|
|
|
|
return [
|
|
'play_config_version_no' => (int) $playV->version_no,
|
|
'odds_version_no' => (int) $oddsV->version_no,
|
|
'risk_cap_version_no' => (int) $riskV->version_no,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* 下注事务内:按固定顺序锁住当前生效的三套配置版本,与后台切版互斥;可选与预览戳比对。
|
|
*
|
|
* @param array{play_config_version_no: int, odds_version_no: int, risk_cap_version_no: int}|null $expectedFromPreview
|
|
* @return array{play_config_version_no: int, odds_version_no: int, risk_cap_version_no: int}
|
|
*/
|
|
public function lockActiveConfigVersionsForPlacement(?array $expectedFromPreview = null): array
|
|
{
|
|
$playV = PlayConfigVersion::query()
|
|
->where('status', ConfigVersionStatus::Active->value)
|
|
->orderBy('id')
|
|
->lockForUpdate()
|
|
->firstOrFail();
|
|
|
|
$oddsV = OddsVersion::query()
|
|
->where('status', ConfigVersionStatus::Active->value)
|
|
->orderBy('id')
|
|
->lockForUpdate()
|
|
->firstOrFail();
|
|
|
|
$riskV = RiskCapVersion::query()
|
|
->where('status', ConfigVersionStatus::Active->value)
|
|
->orderBy('id')
|
|
->lockForUpdate()
|
|
->firstOrFail();
|
|
|
|
if ($expectedFromPreview !== null) {
|
|
if ((int) $playV->version_no !== (int) $expectedFromPreview['play_config_version_no']
|
|
|| (int) $oddsV->version_no !== (int) $expectedFromPreview['odds_version_no']
|
|
|| (int) $riskV->version_no !== (int) $expectedFromPreview['risk_cap_version_no']) {
|
|
throw new TicketOperationException('config_version_stale', ErrorCode::BetConfigStale->value);
|
|
}
|
|
}
|
|
|
|
return [
|
|
'play_config_version_no' => (int) $playV->version_no,
|
|
'odds_version_no' => (int) $oddsV->version_no,
|
|
'risk_cap_version_no' => (int) $riskV->version_no,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array{play_config: PlayConfigItem, odds_items: Collection<int, OddsItem>}
|
|
*/
|
|
public function resolve(string $playCode, string $currencyCode, ?string $providerCode = null): array
|
|
{
|
|
$playVersion = PlayConfigVersion::query()
|
|
->where('status', ConfigVersionStatus::Active->value)
|
|
->firstOrFail();
|
|
|
|
$playConfig = PlayConfigItem::query()
|
|
->where('version_id', $playVersion->id)
|
|
->where('play_code', $playCode)
|
|
->first();
|
|
|
|
if ($playConfig === null || ! $playConfig->is_enabled) {
|
|
throw new TicketOperationException('play_config_disabled', ErrorCode::PlayModeClosed->value);
|
|
}
|
|
|
|
$oddsVersion = OddsVersion::query()
|
|
->where('status', ConfigVersionStatus::Active->value)
|
|
->firstOrFail();
|
|
|
|
$providerCode = strtoupper((string) ($providerCode ?: 'GLOBAL'));
|
|
$rawOddsItems = OddsItem::query()
|
|
->where('version_id', $oddsVersion->id)
|
|
->where('play_code', $playCode)
|
|
->where('currency_code', strtoupper($currencyCode))
|
|
->whereIn('provider_code', ['GLOBAL', $providerCode])
|
|
->get();
|
|
|
|
$oddsItems = $this->resolveProviderOddsItems($rawOddsItems, $providerCode);
|
|
|
|
if ($oddsItems->isEmpty()) {
|
|
throw new TicketOperationException('odds_missing', ErrorCode::BetPlayUnsupported->value);
|
|
}
|
|
|
|
return [
|
|
'play_config' => $playConfig,
|
|
'odds_items' => $oddsItems,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param Collection<int, OddsItem> $items
|
|
* @return Collection<int, OddsItem>
|
|
*/
|
|
private function resolveProviderOddsItems(Collection $items, string $providerCode): Collection
|
|
{
|
|
$selected = [];
|
|
|
|
foreach ($items->sortBy(fn (OddsItem $row): int => (string) $row->provider_code === $providerCode ? 0 : 1) as $row) {
|
|
$dimension = $row->dimension === null ? 'null' : (string) $row->dimension;
|
|
$key = implode('|', [
|
|
(string) $row->prize_scope,
|
|
strtoupper((string) $row->currency_code),
|
|
$dimension,
|
|
]);
|
|
|
|
if (! isset($selected[$key])) {
|
|
$selected[$key] = $row;
|
|
}
|
|
}
|
|
|
|
return collect(array_values($selected));
|
|
}
|
|
|
|
public function resolveCapAmount(int $drawId, string $number4d): int
|
|
{
|
|
$riskVersion = RiskCapVersion::query()
|
|
->where('status', ConfigVersionStatus::Active->value)
|
|
->firstOrFail();
|
|
|
|
$specific = RiskCapItem::query()
|
|
->where('version_id', $riskVersion->id)
|
|
->where('draw_id', $drawId)
|
|
->where('normalized_number', $number4d)
|
|
->orderByDesc('id')
|
|
->first();
|
|
|
|
if ($specific !== null) {
|
|
return (int) $specific->cap_amount;
|
|
}
|
|
|
|
$generic = RiskCapItem::query()
|
|
->where('version_id', $riskVersion->id)
|
|
->whereNull('draw_id')
|
|
->where('normalized_number', $number4d)
|
|
->orderByDesc('id')
|
|
->first();
|
|
|
|
if ($generic !== null) {
|
|
return (int) $generic->cap_amount;
|
|
}
|
|
|
|
$default = RiskCapItem::query()
|
|
->where('version_id', $riskVersion->id)
|
|
->whereNull('draw_id')
|
|
->where('cap_type', 'default')
|
|
->orderByDesc('id')
|
|
->first();
|
|
|
|
if ($default !== null) {
|
|
return (int) $default->cap_amount;
|
|
}
|
|
|
|
return 50_000_000_000;
|
|
}
|
|
}
|