feat(admin): 统一后台 API 资源鉴权并完善投注风控快照与回补
This commit is contained in:
@@ -48,8 +48,9 @@ final class PlayCatalogResolver
|
||||
* 下注事务内:按固定顺序锁住当前生效的三套配置版本,与后台切版互斥;可选与预览戳比对。
|
||||
*
|
||||
* @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): void
|
||||
public function lockActiveConfigVersionsForPlacement(?array $expectedFromPreview = null): array
|
||||
{
|
||||
$playV = PlayConfigVersion::query()
|
||||
->where('status', ConfigVersionStatus::Active->value)
|
||||
@@ -76,6 +77,12 @@ final class PlayCatalogResolver
|
||||
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,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -136,10 +136,10 @@ final class RiskPoolService
|
||||
$pool = $this->firstOrMakePool($drawId, $number4d);
|
||||
$key = $this->redisPoolKey($drawId, $number4d);
|
||||
|
||||
Redis::eval($this->initLua(), 1, $key, (int) $pool->total_cap_amount, (int) $pool->locked_amount);
|
||||
Redis::eval($this->initLua(), 1, $key, (int) $pool->total_cap_amount, (int) $pool->locked_amount, (int) $pool->version);
|
||||
|
||||
$result = (int) Redis::eval($this->acquireLua(), 1, $key, $amount);
|
||||
if ($result !== 1) {
|
||||
$result = $this->normalizeLuaResult(Redis::eval($this->acquireLua(), 1, $key, $amount, (int) $pool->version));
|
||||
if (($result['code'] ?? null) !== 'OK') {
|
||||
throw new TicketOperationException('risk_sold_out', ErrorCode::RiskPoolSoldOut->value);
|
||||
}
|
||||
|
||||
@@ -190,7 +190,7 @@ final class RiskPoolService
|
||||
{
|
||||
return <<<'LUA'
|
||||
if redis.call('EXISTS', KEYS[1]) == 0 then
|
||||
redis.call('HMSET', KEYS[1], 'total', ARGV[1], 'locked', ARGV[2], 'remaining', ARGV[1] - ARGV[2])
|
||||
redis.call('HMSET', KEYS[1], 'total', ARGV[1], 'locked', ARGV[2], 'remaining', ARGV[1] - ARGV[2], 'version', ARGV[3])
|
||||
end
|
||||
return 1
|
||||
LUA;
|
||||
@@ -200,13 +200,25 @@ LUA;
|
||||
{
|
||||
return <<<'LUA'
|
||||
local amount = tonumber(ARGV[1])
|
||||
local expectedVersion = tonumber(ARGV[2])
|
||||
if amount == nil or amount <= 0 then
|
||||
return {'INVALID_ARGUMENT', 0, 0, 0}
|
||||
end
|
||||
if redis.call('EXISTS', KEYS[1]) == 0 then
|
||||
return {'POOL_NOT_INITIALIZED', 0, 0, 0}
|
||||
end
|
||||
local version = tonumber(redis.call('HGET', KEYS[1], 'version') or '0')
|
||||
if expectedVersion ~= nil and version ~= expectedVersion then
|
||||
return {'VERSION_CONFLICT', tonumber(redis.call('HGET', KEYS[1], 'remaining') or '0'), tonumber(redis.call('HGET', KEYS[1], 'locked') or '0'), version}
|
||||
end
|
||||
local remaining = tonumber(redis.call('HGET', KEYS[1], 'remaining') or '0')
|
||||
if remaining < amount then
|
||||
return 0
|
||||
return {'INSUFFICIENT_CAP', remaining, tonumber(redis.call('HGET', KEYS[1], 'locked') or '0'), version}
|
||||
end
|
||||
redis.call('HINCRBY', KEYS[1], 'locked', amount)
|
||||
redis.call('HINCRBY', KEYS[1], 'remaining', -amount)
|
||||
return 1
|
||||
local locked = redis.call('HINCRBY', KEYS[1], 'locked', amount)
|
||||
remaining = redis.call('HINCRBY', KEYS[1], 'remaining', -amount)
|
||||
version = redis.call('HINCRBY', KEYS[1], 'version', 1)
|
||||
return {'OK', remaining, locked, version}
|
||||
LUA;
|
||||
}
|
||||
|
||||
@@ -265,6 +277,28 @@ LUA;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{code:string, remaining:int, locked:int, version:int}
|
||||
*/
|
||||
private function normalizeLuaResult(mixed $result): array
|
||||
{
|
||||
if (! is_array($result)) {
|
||||
return [
|
||||
'code' => (int) $result === 1 ? 'OK' : 'INSUFFICIENT_CAP',
|
||||
'remaining' => 0,
|
||||
'locked' => 0,
|
||||
'version' => 0,
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'code' => (string) ($result[0] ?? 'INSUFFICIENT_CAP'),
|
||||
'remaining' => (int) ($result[1] ?? 0),
|
||||
'locked' => (int) ($result[2] ?? 0),
|
||||
'version' => (int) ($result[3] ?? 0),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<array{number_4d:string, amount:int}> $locks
|
||||
*/
|
||||
|
||||
@@ -6,6 +6,7 @@ use App\Models\Draw;
|
||||
use App\Models\Player;
|
||||
use App\Lottery\ErrorCode;
|
||||
use App\Models\TicketItem;
|
||||
use App\Models\WalletTxn;
|
||||
use App\Lottery\DrawStatus;
|
||||
use App\Models\TicketOrder;
|
||||
use App\Models\PlayerWallet;
|
||||
@@ -31,6 +32,22 @@ final class TicketPlacementService
|
||||
public function place(Player $player, array $payload): array
|
||||
{
|
||||
$currencyCode = strtoupper((string) $payload['currency_code']);
|
||||
$clientTraceId = isset($payload['client_trace_id']) && $payload['client_trace_id'] !== ''
|
||||
? (string) $payload['client_trace_id']
|
||||
: null;
|
||||
|
||||
if ($clientTraceId !== null) {
|
||||
$existing = TicketOrder::query()
|
||||
->where('player_id', $player->id)
|
||||
->where('client_trace_id', $clientTraceId)
|
||||
->whereIn('status', ['placed', 'partial_failed'])
|
||||
->first();
|
||||
|
||||
if ($existing !== null) {
|
||||
return $this->responseForOrder($existing, null);
|
||||
}
|
||||
}
|
||||
|
||||
$expectedVersions = $payload['expected_config_versions'] ?? null;
|
||||
if (is_array($expectedVersions)) {
|
||||
$expectedVersions = [
|
||||
@@ -59,7 +76,7 @@ final class TicketPlacementService
|
||||
throw new TicketOperationException('draw_closed', ErrorCode::DrawClosed->value);
|
||||
}
|
||||
|
||||
$this->catalogResolver->lockActiveConfigVersionsForPlacement($expectedVersions);
|
||||
$configVersions = $this->catalogResolver->lockActiveConfigVersionsForPlacement($expectedVersions);
|
||||
|
||||
$evaluatedLines = [];
|
||||
$totalBet = 0;
|
||||
@@ -137,6 +154,9 @@ final class TicketPlacementService
|
||||
'status' => 'pending',
|
||||
'submit_source' => 'h5',
|
||||
'client_trace_id' => $payload['client_trace_id'] ?? null,
|
||||
'play_config_version_no' => $configVersions['play_config_version_no'],
|
||||
'odds_version_no' => $configVersions['odds_version_no'],
|
||||
'risk_cap_version_no' => $configVersions['risk_cap_version_no'],
|
||||
]);
|
||||
|
||||
$successfulItems = [];
|
||||
@@ -297,6 +317,7 @@ final class TicketPlacementService
|
||||
}
|
||||
|
||||
$order->forceFill(['status' => 'refunded'])->save();
|
||||
$this->ticketWalletService->reverseBetDeduct($order);
|
||||
});
|
||||
|
||||
throw $e;
|
||||
@@ -304,6 +325,24 @@ final class TicketPlacementService
|
||||
|
||||
$order = TicketOrder::query()->whereKey($order->id)->firstOrFail();
|
||||
|
||||
return $this->responseForOrder($order, $balanceAfter);
|
||||
}
|
||||
|
||||
private function responseForOrder(TicketOrder $order, ?int $balanceAfter): array
|
||||
{
|
||||
$order = TicketOrder::query()->whereKey($order->id)->firstOrFail();
|
||||
$draw = Draw::query()->whereKey((int) $order->draw_id)->firstOrFail();
|
||||
$successCount = TicketItem::query()->where('order_id', $order->id)->where('status', 'success')->count();
|
||||
$failureCount = TicketItem::query()->where('order_id', $order->id)->where('status', 'failed')->count();
|
||||
if ($balanceAfter === null) {
|
||||
$walletTxn = WalletTxn::query()
|
||||
->where('biz_type', 'bet_deduct')
|
||||
->where('biz_no', $order->order_no)
|
||||
->latest('id')
|
||||
->first();
|
||||
$balanceAfter = $walletTxn === null ? null : (int) $walletTxn->balance_after;
|
||||
}
|
||||
|
||||
return [
|
||||
'order_no' => $order->order_no,
|
||||
'draw' => [
|
||||
@@ -315,8 +354,8 @@ final class TicketPlacementService
|
||||
'total_rebate_amount' => (int) $order->total_rebate_amount,
|
||||
'total_actual_deduct' => (int) $order->total_actual_deduct,
|
||||
'total_estimated_payout' => (int) $order->total_estimated_payout,
|
||||
'success_count' => TicketItem::query()->where('order_id', $order->id)->where('status', 'success')->count(),
|
||||
'failure_count' => TicketItem::query()->where('order_id', $order->id)->where('status', 'failed')->count(),
|
||||
'success_count' => $successCount,
|
||||
'failure_count' => $failureCount,
|
||||
],
|
||||
'balance_after' => $balanceAfter,
|
||||
'items' => TicketItem::query()
|
||||
|
||||
@@ -69,6 +69,53 @@ final class TicketWalletService
|
||||
return $after;
|
||||
}
|
||||
|
||||
public function reverseBetDeduct(TicketOrder $order): void
|
||||
{
|
||||
$deductTxn = WalletTxn::query()
|
||||
->where('biz_type', 'bet_deduct')
|
||||
->where('biz_no', $order->order_no)
|
||||
->where('status', self::TXN_POSTED)
|
||||
->first();
|
||||
|
||||
if ($deductTxn === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$idempotentKey = 'bet-reverse:'.$order->order_no;
|
||||
if (WalletTxn::query()->where('biz_type', 'bet_reverse')->where('idempotent_key', $idempotentKey)->exists()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$wallet = PlayerWallet::query()
|
||||
->whereKey($deductTxn->wallet_id)
|
||||
->lockForUpdate()
|
||||
->firstOrFail();
|
||||
|
||||
$amount = (int) $deductTxn->amount;
|
||||
$before = (int) $wallet->balance;
|
||||
$after = $before + $amount;
|
||||
$wallet->forceFill([
|
||||
'balance' => $after,
|
||||
'version' => (int) $wallet->version + 1,
|
||||
])->save();
|
||||
|
||||
WalletTxn::query()->create([
|
||||
'txn_no' => $this->newTxnNo(),
|
||||
'player_id' => (int) $deductTxn->player_id,
|
||||
'wallet_id' => (int) $deductTxn->wallet_id,
|
||||
'biz_type' => 'bet_reverse',
|
||||
'biz_no' => $order->order_no,
|
||||
'direction' => self::TXN_DIR_IN,
|
||||
'amount' => $amount,
|
||||
'balance_before' => $before,
|
||||
'balance_after' => $after,
|
||||
'status' => self::TXN_POSTED,
|
||||
'external_ref_no' => null,
|
||||
'idempotent_key' => $idempotentKey,
|
||||
'remark' => 'post_deduct_confirmation_failed',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 结算派彩入账(产品文档:派彩写入钱包流水;幂等键按结算批次 + 玩家)。
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user