205 lines
7.3 KiB
PHP
205 lines
7.3 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, ?string $providerCode = null): int
|
|
{
|
|
$riskVersion = RiskCapVersion::query()
|
|
->where('status', ConfigVersionStatus::Active->value)
|
|
->firstOrFail();
|
|
|
|
$providerCode = strtoupper(trim((string) ($providerCode ?: 'GLOBAL')));
|
|
$providerScopes = array_values(array_unique(['GLOBAL', $providerCode]));
|
|
|
|
$specific = RiskCapItem::query()
|
|
->where('version_id', $riskVersion->id)
|
|
->whereIn('provider_code', $providerScopes)
|
|
->where('draw_id', $drawId)
|
|
->where('normalized_number', $number4d)
|
|
->orderByRaw('case when provider_code = ? then 0 else 1 end', [$providerCode])
|
|
->orderByDesc('id')
|
|
->first();
|
|
|
|
if ($specific !== null) {
|
|
return (int) $specific->cap_amount;
|
|
}
|
|
|
|
$generic = RiskCapItem::query()
|
|
->where('version_id', $riskVersion->id)
|
|
->whereIn('provider_code', $providerScopes)
|
|
->whereNull('draw_id')
|
|
->where('normalized_number', $number4d)
|
|
->orderByRaw('case when provider_code = ? then 0 else 1 end', [$providerCode])
|
|
->orderByDesc('id')
|
|
->first();
|
|
|
|
if ($generic !== null) {
|
|
return (int) $generic->cap_amount;
|
|
}
|
|
|
|
$default = RiskCapItem::query()
|
|
->where('version_id', $riskVersion->id)
|
|
->whereIn('provider_code', $providerScopes)
|
|
->whereNull('draw_id')
|
|
->where('cap_type', 'default')
|
|
->orderByRaw('case when provider_code = ? then 0 else 1 end', [$providerCode])
|
|
->orderByDesc('id')
|
|
->first();
|
|
|
|
if ($default !== null) {
|
|
return (int) $default->cap_amount;
|
|
}
|
|
|
|
return 50_000_000_000;
|
|
}
|
|
}
|