feat: 添加新的错误码以支持投注功能,更新数据库填充器以增强玩法和赔率配置,扩展 API 路由以支持风险池管理
This commit is contained in:
159
app/Services/Ticket/PlayCatalogResolver.php
Normal file
159
app/Services/Ticket/PlayCatalogResolver.php
Normal file
@@ -0,0 +1,159 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Ticket;
|
||||
|
||||
use App\Exceptions\TicketOperationException;
|
||||
use App\Lottery\ConfigVersionStatus;
|
||||
use App\Lottery\ErrorCode;
|
||||
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 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
|
||||
*/
|
||||
public function lockActiveConfigVersionsForPlacement(?array $expectedFromPreview = null): void
|
||||
{
|
||||
$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 array{play_type: PlayType, play_config: PlayConfigItem, odds_items: Collection<int, OddsItem>}
|
||||
*/
|
||||
public function resolve(string $playCode, string $currencyCode): array
|
||||
{
|
||||
$playType = PlayType::query()->where('play_code', $playCode)->first();
|
||||
if ($playType === null) {
|
||||
throw new TicketOperationException('play_not_found', ErrorCode::BetPlayUnsupported->value);
|
||||
}
|
||||
if (! $playType->is_enabled) {
|
||||
throw new TicketOperationException('play_master_disabled', ErrorCode::PlayModeClosed->value);
|
||||
}
|
||||
|
||||
$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();
|
||||
|
||||
$oddsItems = OddsItem::query()
|
||||
->where('version_id', $oddsVersion->id)
|
||||
->where('play_code', $playCode)
|
||||
->where('currency_code', strtoupper($currencyCode))
|
||||
->get();
|
||||
|
||||
if ($oddsItems->isEmpty()) {
|
||||
throw new TicketOperationException('odds_missing', ErrorCode::BetPlayUnsupported->value);
|
||||
}
|
||||
|
||||
return [
|
||||
'play_type' => $playType,
|
||||
'play_config' => $playConfig,
|
||||
'odds_items' => $oddsItems,
|
||||
];
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
return 50_000_000_000;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user