feat: 支持开奖重开与风险池原子扣减,完善投注部分成功流程

This commit is contained in:
2026-05-18 11:28:11 +08:00
parent 4f143c7cb1
commit 9157dcb6a1
14 changed files with 526 additions and 103 deletions

View File

@@ -23,7 +23,10 @@ final class DrawRngRunController extends Controller
$batch = DB::transaction(function () use ($draw) {
/** @var Draw $locked */
$locked = Draw::query()->whereKey($draw->id)->lockForUpdate()->firstOrFail();
if ($locked->status !== DrawStatus::Closed->value || $locked->resultBatches()->exists()) {
if ($locked->status !== DrawStatus::Closed->value || (int) $locked->settle_version > 0) {
throw new \RuntimeException('draw_not_runnable');
}
if (! (bool) $locked->is_reopened && $locked->resultBatches()->exists()) {
throw new \RuntimeException('draw_not_runnable');
}

View File

@@ -34,6 +34,7 @@ final class TicketDrawMyMatchController extends Controller
'hit_numbers_4d' => [],
'total_win_minor' => 0,
'total_jackpot_win_minor' => 0,
'winning_ticket_count' => 0,
'has_bets' => false,
]);
}
@@ -45,6 +46,7 @@ final class TicketDrawMyMatchController extends Controller
'hit_numbers_4d' => [],
'total_win_minor' => 0,
'total_jackpot_win_minor' => 0,
'winning_ticket_count' => 0,
'has_bets' => false,
]);
}
@@ -63,11 +65,20 @@ final class TicketDrawMyMatchController extends Controller
->pluck('id');
$hasBets = $itemIds->isNotEmpty();
$winningItemIds = TicketItem::query()
->where('draw_id', $draw->id)
->where('player_id', $player->id)
->where('status', 'settled_win')
->where(function ($q): void {
$q->where('win_amount', '>', 0)
->orWhere('jackpot_win_amount', '>', 0);
})
->pluck('id');
$hits = [];
if ($hasBets) {
if ($winningItemIds->isNotEmpty()) {
$hits = TicketCombination::query()
->whereIn('ticket_item_id', $itemIds)
->whereIn('ticket_item_id', $winningItemIds)
->pluck('number_4d')
->map(fn ($n) => self::norm4d((string) $n))
->filter(fn (string $n) => isset($board[$n]))
@@ -88,6 +99,7 @@ final class TicketDrawMyMatchController extends Controller
'hit_numbers_4d' => $hits,
'total_win_minor' => (int) ($sums->sum_win ?? 0),
'total_jackpot_win_minor' => (int) ($sums->sum_jackpot ?? 0),
'winning_ticket_count' => $winningItemIds->count(),
'has_bets' => $hasBets,
]);
}

View File

@@ -28,6 +28,9 @@ final class TicketItemsIndexController extends Controller
$page = $this->page($request);
$drawNo = $request->query('draw_no');
$statusInput = $request->query('status', []);
if (is_string($statusInput)) {
$statusInput = [$statusInput];
}
$statusValues = is_array($statusInput) ? array_values(array_filter(array_map(
fn ($status) => is_string($status) ? trim($status) : '',
$statusInput,

View File

@@ -88,7 +88,11 @@ final class DrawRngRunner
->where('status', DrawStatus::Closed->value)
->whereNotNull('draw_time')
->where('draw_time', '<=', $nowUtc)
->whereDoesntHave('resultBatches')
->where('settle_version', 0)
->where(function ($q): void {
$q->where('is_reopened', true)
->orWhereDoesntHave('resultBatches');
})
->orderBy('draw_time')
->pluck('id');
@@ -100,7 +104,10 @@ final class DrawRngRunner
if ($locked === null || $locked->status !== DrawStatus::Closed->value) {
return;
}
if ($locked->resultBatches()->exists()) {
if ((int) $locked->settle_version > 0) {
return;
}
if (! (bool) $locked->is_reopened && $locked->resultBatches()->exists()) {
return;
}
$this->executeLocked($locked);

View File

@@ -6,6 +6,8 @@ use App\Models\RiskPool;
use App\Lottery\ErrorCode;
use App\Models\TicketItem;
use App\Models\RiskPoolLockLog;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Redis;
use App\Exceptions\TicketOperationException;
final class RiskPoolService
@@ -47,46 +49,106 @@ final class RiskPoolService
*/
public function acquire(int $drawId, ?TicketItem $ticketItem, array $locks): int
{
$total = 0;
foreach ($locks as $lock) {
$pool = RiskPool::query()
->where('draw_id', $drawId)
->where('normalized_number', $lock['number_4d'])
->lockForUpdate()
->first();
if ($this->shouldUseRedisAtomicLocks()) {
return $this->acquireWithRedisLua($drawId, $ticketItem, $locks);
}
if ($pool === null) {
$pool = $this->createPool($drawId, $lock['number_4d']);
return $this->acquireWithDatabaseLocks($drawId, $ticketItem, $locks);
}
/**
* @param list<array{number_4d:string, amount:int}> $locks
*/
private function acquireWithDatabaseLocks(int $drawId, ?TicketItem $ticketItem, array $locks): int
{
$acquired = [];
$total = 0;
try {
foreach ($locks as $lock) {
$pool = RiskPool::query()
->where('draw_id', $drawId)
->where('normalized_number', $lock['number_4d'])
->lockForUpdate()
->firstOrFail();
->first();
if ($pool === null) {
$pool = $this->createPool($drawId, $lock['number_4d']);
$pool = RiskPool::query()
->where('draw_id', $drawId)
->where('normalized_number', $lock['number_4d'])
->lockForUpdate()
->firstOrFail();
}
$amount = (int) $lock['amount'];
if ((int) $pool->remaining_amount < $amount) {
throw new TicketOperationException('risk_sold_out', ErrorCode::RiskPoolSoldOut->value);
}
$pool->forceFill([
'locked_amount' => (int) $pool->locked_amount + $amount,
'remaining_amount' => (int) $pool->remaining_amount - $amount,
'sold_out_status' => ((int) $pool->remaining_amount - $amount) <= 0 ? 1 : 0,
'version' => (int) $pool->version + 1,
])->save();
RiskPoolLockLog::query()->create([
'draw_id' => $drawId,
'normalized_number' => $lock['number_4d'],
'ticket_item_id' => $ticketItem?->id,
'action_type' => 'lock',
'amount' => $amount,
'source_reason' => 'ticket_place',
'created_at' => now(),
]);
$acquired[] = ['number_4d' => $lock['number_4d'], 'amount' => $amount];
$total += $amount;
}
} catch (\Throwable $e) {
$this->releaseDatabaseLocks($drawId, $ticketItem, $acquired, 'ticket_failed_line');
$amount = (int) $lock['amount'];
if ((int) $pool->remaining_amount < $amount) {
throw new TicketOperationException('risk_sold_out', ErrorCode::RiskPoolSoldOut->value);
throw $e;
}
return $total;
}
/**
* Redis Lua 负责额度判断与扣减的原子性;数据库事实表随后同步,失败时释放 Redis 占用。
*
* @param list<array{number_4d:string, amount:int}> $locks
*/
private function acquireWithRedisLua(int $drawId, ?TicketItem $ticketItem, array $locks): int
{
$acquired = [];
$total = 0;
try {
foreach ($locks as $lock) {
$number4d = $lock['number_4d'];
$amount = (int) $lock['amount'];
$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);
$result = (int) Redis::eval($this->acquireLua(), 1, $key, $amount);
if ($result !== 1) {
throw new TicketOperationException('risk_sold_out', ErrorCode::RiskPoolSoldOut->value);
}
$acquired[] = ['number_4d' => $number4d, 'amount' => $amount];
$total += $amount;
$this->syncDatabaseAfterRedisAcquire($drawId, $ticketItem, $number4d, $amount);
}
} catch (\Throwable $e) {
$this->releaseRedisLocks($drawId, $acquired);
$this->releaseDatabaseLocks($drawId, $ticketItem, $acquired, 'ticket_failed_line');
$pool->forceFill([
'locked_amount' => (int) $pool->locked_amount + $amount,
'remaining_amount' => (int) $pool->remaining_amount - $amount,
'sold_out_status' => ((int) $pool->remaining_amount - $amount) <= 0 ? 1 : 0,
'version' => (int) $pool->version + 1,
])->save();
RiskPoolLockLog::query()->create([
'draw_id' => $drawId,
'normalized_number' => $lock['number_4d'],
'ticket_item_id' => $ticketItem?->id,
'action_type' => 'lock',
'amount' => $amount,
'source_reason' => 'ticket_place',
'created_at' => now(),
]);
$total += $amount;
throw $e;
}
return $total;
@@ -96,6 +158,109 @@ final class RiskPoolService
* @param list<array{number_4d:string, amount:int}> $locks
*/
public function release(int $drawId, ?TicketItem $ticketItem, array $locks): void
{
if ($this->shouldUseRedisAtomicLocks()) {
$this->releaseRedisLocks($drawId, $locks);
}
foreach ($locks as $lock) {
$this->releaseDatabaseLocks($drawId, $ticketItem, [$lock], 'ticket_rollback');
}
}
private function shouldUseRedisAtomicLocks(): bool
{
if (App::environment('testing')) {
return false;
}
return (bool) config('lottery.risk_pool.use_redis_lua', true);
}
private function redisPoolKey(int $drawId, string $number4d): string
{
return "risk_pool:draw:{$drawId}:number:{$number4d}";
}
private function initLua(): string
{
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])
end
return 1
LUA;
}
private function acquireLua(): string
{
return <<<'LUA'
local amount = tonumber(ARGV[1])
local remaining = tonumber(redis.call('HGET', KEYS[1], 'remaining') or '0')
if remaining < amount then
return 0
end
redis.call('HINCRBY', KEYS[1], 'locked', amount)
redis.call('HINCRBY', KEYS[1], 'remaining', -amount)
return 1
LUA;
}
private function releaseLua(): string
{
return <<<'LUA'
local amount = tonumber(ARGV[1])
local locked = tonumber(redis.call('HGET', KEYS[1], 'locked') or '0')
local releaseAmount = amount
if locked < releaseAmount then
releaseAmount = locked
end
redis.call('HINCRBY', KEYS[1], 'locked', -releaseAmount)
redis.call('HINCRBY', KEYS[1], 'remaining', releaseAmount)
return releaseAmount
LUA;
}
private function syncDatabaseAfterRedisAcquire(int $drawId, ?TicketItem $ticketItem, string $number4d, int $amount): void
{
$pool = RiskPool::query()
->where('draw_id', $drawId)
->where('normalized_number', $number4d)
->lockForUpdate()
->firstOrFail();
$pool->forceFill([
'locked_amount' => (int) $pool->locked_amount + $amount,
'remaining_amount' => (int) $pool->remaining_amount - $amount,
'sold_out_status' => ((int) $pool->remaining_amount - $amount) <= 0 ? 1 : 0,
'version' => (int) $pool->version + 1,
])->save();
RiskPoolLockLog::query()->create([
'draw_id' => $drawId,
'normalized_number' => $number4d,
'ticket_item_id' => $ticketItem?->id,
'action_type' => 'lock',
'amount' => $amount,
'source_reason' => 'ticket_place',
'created_at' => now(),
]);
}
/**
* @param list<array{number_4d:string, amount:int}> $locks
*/
private function releaseRedisLocks(int $drawId, array $locks): void
{
foreach ($locks as $lock) {
Redis::eval($this->releaseLua(), 1, $this->redisPoolKey($drawId, $lock['number_4d']), (int) $lock['amount']);
}
}
/**
* @param list<array{number_4d:string, amount:int}> $locks
*/
private function releaseDatabaseLocks(int $drawId, ?TicketItem $ticketItem, array $locks, string $sourceReason): void
{
foreach ($locks as $lock) {
$pool = RiskPool::query()
@@ -122,7 +287,7 @@ final class RiskPoolService
'ticket_item_id' => $ticketItem?->id,
'action_type' => 'release',
'amount' => $amount,
'source_reason' => 'ticket_rollback',
'source_reason' => $sourceReason,
'created_at' => now(),
]);
}

View File

@@ -54,7 +54,7 @@ final class TicketPlacementService
if ($draw === null) {
throw new TicketOperationException('draw_not_found', ErrorCode::BetInvalidDraw->value);
}
if ($draw->status !== DrawStatus::Open->value) {
if ($draw->status !== DrawStatus::Open->value || ($draw->close_time !== null && now()->greaterThanOrEqualTo($draw->close_time))) {
throw new TicketOperationException('draw_closed', ErrorCode::DrawClosed->value);
}
@@ -76,6 +76,7 @@ final class TicketPlacementService
'client_line_no' => $index + 1,
'play_code' => (string) ($line['play_code'] ?? ''),
];
continue;
}
@@ -91,7 +92,7 @@ final class TicketPlacementService
'number_4d' => $combo['number_4d'],
'amount' => $combo['estimated_payout'],
], $evaluated['combinations']);
$this->riskPoolService->preview((int) $draw->id, $locks);
// place 阶段以 acquire 的原子扣减结果为准,允许单行售罄后形成混合成功/失败结果。
$evaluatedLines[] = $evaluated;
$rebateAmount = (int) $evaluated['total_bet_amount'] - (int) $evaluated['actual_deduct_amount'];
@@ -127,8 +128,14 @@ final class TicketPlacementService
'client_trace_id' => $payload['client_trace_id'] ?? null,
]);
$balanceAfter = $this->ticketWalletService->deduct($player, $currencyCode, $totalActualDeduct, $order);
$successfulItems = [];
$failedItems = [];
$successfulEvaluatedLines = [];
$successTotalBet = 0;
$successTotalRebate = 0;
$successTotalActualDeduct = 0;
$successTotalEstimatedPayout = 0;
$firstFailure = null;
foreach ($evaluatedLines as $evaluated) {
$item = TicketItem::query()->create([
'ticket_no' => $this->newTicketNo(),
@@ -145,13 +152,13 @@ final class TicketPlacementService
'total_bet_amount' => $evaluated['total_bet_amount'],
'rebate_rate_snapshot' => $evaluated['rebate_rate_snapshot'],
'commission_rate_snapshot' => $evaluated['commission_rate_snapshot'],
'actual_deduct_amount' => $evaluated['actual_deduct_amount'],
'actual_deduct_amount' => 0,
'odds_snapshot_json' => $evaluated['odds_snapshot_json'],
'rule_snapshot_json' => $evaluated['rule_snapshot_json'],
'combination_count' => $evaluated['combination_count'],
'estimated_max_payout' => $evaluated['estimated_max_payout'],
'risk_locked_amount' => 0,
'status' => 'success',
'status' => 'pending',
'fail_reason_code' => null,
'fail_reason_text' => null,
'win_amount' => 0,
@@ -175,10 +182,67 @@ final class TicketPlacementService
];
}
$lockedAmount = $this->riskPoolService->acquire((int) $draw->id, $item, $locks);
$item->forceFill(['risk_locked_amount' => $lockedAmount])->save();
try {
$lockedAmount = $this->riskPoolService->acquire((int) $draw->id, $item, $locks);
} catch (TicketOperationException $e) {
if ($e->lotteryCode !== ErrorCode::RiskPoolSoldOut->value) {
throw $e;
}
$this->jackpotContribution->recordFromPlacedTicketItem($item, $draw, $currencyCode);
$firstFailure ??= $e;
$item->forceFill([
'status' => 'failed',
'fail_reason_code' => (string) $e->lotteryCode,
'fail_reason_text' => 'risk_sold_out',
])->save();
$failedItems[] = $item;
continue;
}
$rebateAmount = (int) $evaluated['total_bet_amount'] - (int) $evaluated['actual_deduct_amount'];
$item->forceFill([
'actual_deduct_amount' => (int) $evaluated['actual_deduct_amount'],
'risk_locked_amount' => $lockedAmount,
'status' => 'success',
])->save();
$successfulItems[] = $item;
$successfulEvaluatedLines[] = ['item' => $item, 'evaluated' => $evaluated];
$successTotalBet += (int) $evaluated['total_bet_amount'];
$successTotalRebate += $rebateAmount;
$successTotalActualDeduct += (int) $evaluated['actual_deduct_amount'];
$successTotalEstimatedPayout += (int) $evaluated['estimated_max_payout'];
}
if ($successfulItems === []) {
throw $firstFailure ?? new TicketOperationException('risk_sold_out', ErrorCode::RiskPoolSoldOut->value);
}
$order->forceFill([
'total_bet_amount' => $successTotalBet,
'total_rebate_amount' => $successTotalRebate,
'total_actual_deduct' => $successTotalActualDeduct,
'total_estimated_payout' => $successTotalEstimatedPayout,
'status' => $failedItems === [] ? 'placed' : 'partial_failed',
])->save();
try {
$balanceAfter = $this->ticketWalletService->deduct($player, $currencyCode, $successTotalActualDeduct, $order);
foreach ($successfulItems as $item) {
$this->jackpotContribution->recordFromPlacedTicketItem($item, $draw, $currencyCode);
}
} catch (\Throwable $e) {
foreach ($successfulEvaluatedLines as $row) {
$locks = array_map(fn (array $combo): array => [
'number_4d' => $combo['number_4d'],
'amount' => $combo['estimated_payout'],
], $row['evaluated']['combinations']);
$this->riskPoolService->release((int) $draw->id, $row['item'], $locks);
}
throw $e;
}
return [
@@ -203,6 +267,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(),
],
'balance_after' => $balanceAfter,
'items' => TicketItem::query()
@@ -217,6 +283,9 @@ final class TicketPlacementService
'actual_deduct_amount' => (int) $item->actual_deduct_amount,
'estimated_max_payout' => (int) $item->estimated_max_payout,
'combination_count' => (int) $item->combination_count,
'status' => $item->status,
'fail_reason_code' => $item->fail_reason_code,
'fail_reason_text' => $item->fail_reason_text,
])->values()->all(),
];
}

View File

@@ -25,7 +25,7 @@ final class TicketPreviewService
if ($draw === null) {
throw new TicketOperationException('draw_not_found', ErrorCode::BetInvalidDraw->value);
}
if ($draw->status !== DrawStatus::Open->value) {
if ($draw->status !== DrawStatus::Open->value || ($draw->close_time !== null && now()->greaterThanOrEqualTo($draw->close_time))) {
throw new TicketOperationException('draw_closed', ErrorCode::DrawClosed->value);
}
@@ -47,6 +47,7 @@ final class TicketPreviewService
'client_line_no' => $index + 1,
'play_code' => (string) ($line['play_code'] ?? ''),
];
continue;
}

View File

@@ -23,6 +23,11 @@ return [
'cache_ttl_seconds' => (int) env('LOTTERY_SETTINGS_CACHE_TTL', 60),
],
'risk_pool' => [
/** 生产默认使用 Redis Lua 做号码赔付池原子扣减testing 自动回退 DB便于无 Redis 跑测试。 */
'use_redis_lua' => filter_var(env('LOTTERY_RISK_POOL_USE_REDIS_LUA', true), FILTER_VALIDATE_BOOLEAN),
],
'main_site' => [
'base_url' => env('MAIN_SITE_BASE_URL'),
'sso_jwt_secret' => env('MAIN_SITE_SSO_JWT_SECRET'),

View File

@@ -19,7 +19,7 @@ return [
'stateful' => explode(',', env('SANCTUM_STATEFUL_DOMAINS', sprintf(
'%s%s',
'localhost,localhost:3000,127.0.0.1,127.0.0.1:8000,::1',
'localhost,localhost:3800,localhost:3801,127.0.0.1,127.0.0.1:3800,127.0.0.1:3801,127.0.0.1:8000,::1',
Sanctum::currentApplicationUrlWithPort(),
// Sanctum::currentRequestHost(),
))),

View File

@@ -13,36 +13,24 @@ Route::prefix('v1')->group(function (): void {
// 玩家端路由(需 lottery.player
require __DIR__.'/api/v1/player.php';
// 管理端路由(需 auth:sanctum + lottery.admin
// 管理端路由
Route::prefix('admin')
->name('api.v1.admin.')
->middleware(['auth:sanctum', 'lottery.admin'])
->group(function (): void {
// 认证路由(无需 Token单独限流
// 认证(无需 Token单独限流
require __DIR__.'/api/v1/admin/auth.php';
// 核心路由
require __DIR__.'/api/v1/admin/core.php';
// 钱包/对账
require __DIR__.'/api/v1/admin/wallet.php';
// 玩家管理
require __DIR__.'/api/v1/admin/player.php';
// 开奖/风控/结算
require __DIR__.'/api/v1/admin/draw.php';
// 奖池
require __DIR__.'/api/v1/admin/jackpot.php';
// 配置
require __DIR__.'/api/v1/admin/config.php';
// 报表
require __DIR__.'/api/v1/admin/report.php';
// 管理员账号
require __DIR__.'/api/v1/admin/user.php';
// 以下需 auth:sanctum + lottery.admin
Route::middleware(['auth:sanctum', 'lottery.admin'])
->group(function (): void {
require __DIR__.'/api/v1/admin/core.php';
require __DIR__.'/api/v1/admin/wallet.php';
require __DIR__.'/api/v1/admin/player.php';
require __DIR__.'/api/v1/admin/draw.php';
require __DIR__.'/api/v1/admin/jackpot.php';
require __DIR__.'/api/v1/admin/config.php';
require __DIR__.'/api/v1/admin/report.php';
require __DIR__.'/api/v1/admin/user.php';
});
});
});

View File

@@ -436,6 +436,16 @@ test('admin can reopen cooldown draw for a replacement result batch', function (
expect($draw->is_reopened)->toBeTrue();
expect($draw->cooling_end_time)->toBeNull();
$this->withHeader('Authorization', 'Bearer '.$token)
->postJson("/api/v1/admin/draws/{$draw->id}/rng")
->assertOk()
->assertJsonPath('data.batch.result_version', 2)
->assertJsonPath('data.batch.items_count', 23);
$draw->refresh();
expect($draw->current_result_version)->toBe(2);
expect(DrawResultBatch::query()->where('draw_id', $draw->id)->count())->toBe(2);
Carbon::setTestNow();
});

View File

@@ -471,7 +471,7 @@ test('§14.5 jackpot contributes on place and stays in pool when no first-prize
expect((int) $item->jackpot_win_amount)->toBe(0);
});
test('§14.5 placement rollback returns stake when mid-order risk acquire fails (退本)', function (): void {
test('§14.5 placement partial failure only deducts successful lines when mid-order risk acquire fails', function (): void {
$player = p145_player(500_000);
$drawNo = p145_next_draw_no();
$draw = p145_draw($drawNo, random_int(1, 99_999));
@@ -495,14 +495,24 @@ test('§14.5 placement rollback returns stake when mid-order risk acquire fails
['number' => '1234', 'play_code' => 'big', 'amount' => 120],
],
])
->assertStatus(400)
->assertJsonPath('code', ErrorCode::RiskPoolSoldOut->value);
->assertOk()
->assertJsonPath('data.summary.success_count', 1)
->assertJsonPath('data.summary.failure_count', 1)
->assertJsonPath('data.items.1.fail_reason_code', (string) ErrorCode::RiskPoolSoldOut->value);
expect(TicketOrder::query()->count())->toBe(0);
expect(WalletTxn::query()->where('biz_type', 'bet_deduct')->count())->toBe(0);
expect(TicketOrder::query()->count())->toBe(1);
expect(TicketOrder::query()->firstOrFail()->status)->toBe('partial_failed');
expect(WalletTxn::query()->where('biz_type', 'bet_deduct')->count())->toBe(1);
$wallet = PlayerWallet::query()->where('player_id', $player->id)->firstOrFail();
expect((int) $wallet->balance)->toBe(500_000);
expect((int) $wallet->balance)->toBe(500_000 - 120);
$pool = RiskPool::query()
->where('draw_id', $draw->id)
->where('normalized_number', '1234')
->firstOrFail();
expect((int) $pool->remaining_amount)->toBe(2000);
expect((int) $pool->locked_amount)->toBe(3000);
});
/**

View File

@@ -236,6 +236,24 @@ test('ticket place rejects closed draw', function (): void {
->assertJsonPath('code', ErrorCode::DrawClosed->value);
});
test('ticket preview and place reject open draw after server close time', function (): void {
$player = ticketPlayerWithWallet();
$draw = ticketOpenDraw();
$draw->update(['close_time' => now()->subSecond()]);
$this->withHeader('Authorization', 'Bearer dev:'.$player->id)
->postJson('/api/v1/ticket/preview', ticketPreviewPayload())
->assertStatus(400)
->assertJsonPath('code', ErrorCode::DrawClosed->value);
$this->withHeader('Authorization', 'Bearer dev:'.$player->id)
->postJson('/api/v1/ticket/place', ticketPreviewPayload())
->assertStatus(400)
->assertJsonPath('code', ErrorCode::DrawClosed->value);
expect(TicketOrder::query()->count())->toBe(0);
});
test('ticket place rejects insufficient balance', function (): void {
$player = ticketPlayerWithWallet(1_000);
ticketOpenDraw();
@@ -324,6 +342,58 @@ test('ticket place rejects sold out risk pool', function (): void {
expect(TicketOrder::query()->count())->toBe(0);
});
test('ticket place can return mixed success and failed risk results', function (): void {
$player = ticketPlayerWithWallet(500_000);
$draw = ticketOpenDraw();
RiskPool::query()->create([
'draw_id' => $draw->id,
'normalized_number' => '1234',
'total_cap_amount' => 5000,
'locked_amount' => 0,
'remaining_amount' => 5000,
'sold_out_status' => 0,
'version' => 0,
]);
RiskPool::query()->create([
'draw_id' => $draw->id,
'normalized_number' => '5678',
'total_cap_amount' => 100,
'locked_amount' => 100,
'remaining_amount' => 0,
'sold_out_status' => 1,
'version' => 1,
]);
$this->withHeader('Authorization', 'Bearer dev:'.$player->id)
->postJson('/api/v1/ticket/place', [
'draw_id' => '20260511-001',
'currency_code' => 'NPR',
'client_trace_id' => 'trace-mixed-risk',
'lines' => [
['number' => '1234', 'play_code' => 'big', 'amount' => 120],
['number' => '5678', 'play_code' => 'big', 'amount' => 120],
],
])
->assertOk()
->assertJsonPath('code', ErrorCode::Success->value)
->assertJsonPath('data.summary.success_count', 1)
->assertJsonPath('data.summary.failure_count', 1)
->assertJsonPath('data.items.0.status', 'success')
->assertJsonPath('data.items.1.status', 'failed')
->assertJsonPath('data.items.1.fail_reason_code', (string) ErrorCode::RiskPoolSoldOut->value);
$order = TicketOrder::query()->firstOrFail();
expect($order->status)->toBe('partial_failed')
->and((int) $order->total_actual_deduct)->toBe(120)
->and(TicketItem::query()->where('status', 'success')->count())->toBe(1)
->and(TicketItem::query()->where('status', 'failed')->count())->toBe(1);
$wallet = PlayerWallet::query()->where('player_id', $player->id)->firstOrFail();
expect((int) $wallet->balance)->toBe(500_000 - 120);
});
test('ticket place rejects disabled play from active catalog', function (): void {
$player = ticketPlayerWithWallet();
ticketOpenDraw();
@@ -424,10 +494,10 @@ test('ticket preview rejects invalid line amount per validation rules', function
});
/**
* §13.5 风险占用回滚同一订单两行共享号码时preview 各自通过;首条 acquire 后剩余不足则第二条失败,整单事务回滚
* §10.1.2 混合成功失败:同一订单两行共享号码时,首条占用成功,第二条额度不足则该注项失败
* big + amount 120 estimated_payout 3000maxOdds 250000 / 10000 = 25)。
*/
test('ticket place rolls back order wallet and risk locks when mid-order acquire fails', function (): void {
test('ticket place records partial failed when mid-order acquire fails', function (): void {
$player = ticketPlayerWithWallet(500_000);
$draw = ticketOpenDraw();
@@ -453,21 +523,24 @@ test('ticket place rolls back order wallet and risk locks when mid-order acquire
$this->withHeader('Authorization', 'Bearer dev:'.$player->id)
->postJson('/api/v1/ticket/place', $payload)
->assertStatus(400)
->assertJsonPath('code', ErrorCode::RiskPoolSoldOut->value);
->assertOk()
->assertJsonPath('code', ErrorCode::Success->value)
->assertJsonPath('data.summary.success_count', 1)
->assertJsonPath('data.summary.failure_count', 1);
expect(TicketOrder::query()->count())->toBe(0);
expect(WalletTxn::query()->where('biz_type', 'bet_deduct')->count())->toBe(0);
expect(TicketOrder::query()->count())->toBe(1);
expect(TicketOrder::query()->firstOrFail()->status)->toBe('partial_failed');
expect(WalletTxn::query()->where('biz_type', 'bet_deduct')->count())->toBe(1);
$wallet = PlayerWallet::query()->where('player_id', $player->id)->firstOrFail();
expect((int) $wallet->balance)->toBe(500_000);
expect((int) $wallet->balance)->toBe(500_000 - 120);
$pool = RiskPool::query()
->where('draw_id', $draw->id)
->where('normalized_number', '1234')
->firstOrFail();
expect((int) $pool->remaining_amount)->toBe(5000);
expect((int) $pool->locked_amount)->toBe(0);
expect((int) $pool->remaining_amount)->toBe(2000);
expect((int) $pool->locked_amount)->toBe(3000);
});
/** §13.5 并发下注(顺序挤出):先成功者占用额度,后一盘同一号码收到售罄。 */

View File

@@ -251,6 +251,12 @@ test('ticket items index filters by status number and date range', function ():
->assertJsonPath('data.total', 1)
->assertJsonPath('data.items.0.draw_no', '20260512-780');
$this->withHeader('Authorization', 'Bearer dev:'.$player->id)
->getJson('/api/v1/ticket/items?status=settled_win')
->assertOk()
->assertJsonPath('data.total', 1)
->assertJsonPath('data.items.0.draw_no', '20260512-780');
$this->withHeader('Authorization', 'Bearer dev:'.$player->id)
->getJson('/api/v1/ticket/items?number=1234')
->assertOk()
@@ -309,7 +315,7 @@ test('ticket item show returns match result and timeline', function (): void {
->assertJsonPath('data.timeline.4.code', 'settled');
});
test('my-match returns hit numbers when draw published', function (): void {
test('my-match returns hit numbers when draw settled with winning ticket', function (): void {
$uniq = bin2hex(random_bytes(4));
$player = Player::query()->create([
'site_code' => 'test',
@@ -355,11 +361,39 @@ test('my-match returns hit numbers when draw published', function (): void {
])
->assertOk();
ticketItemsPublishAndSettle($draw, '1234');
$this->withHeader('Authorization', 'Bearer dev:'.$player->id)
->getJson('/api/v1/ticket/draws/20260511-778/my-match')
->assertOk()
->assertJsonPath('data.has_bets', true)
->assertJsonPath('data.winning_ticket_count', 1)
->assertJsonPath('data.hit_numbers_4d', ['1234']);
});
test('my-match only highlights settled winning tickets', function (): void {
$player = ticketItemsPlayer();
$draw = Draw::query()->create([
'draw_no' => '20260514-779',
'business_date' => '2026-05-14',
'sequence_no' => 779,
'status' => DrawStatus::Cooldown->value,
'start_time' => now()->subMinutes(20),
'close_time' => now()->subMinutes(10),
'draw_time' => now()->subMinutes(5),
'cooling_end_time' => now()->addMinutes(5),
'result_source' => 'rng',
'current_result_version' => 1,
'settle_version' => 0,
'is_reopened' => false,
]);
$batch = DrawResultBatch::query()->create([
'draw_id' => $draw->id,
'result_version' => 1,
'source_type' => 'rng',
'rng_seed_hash' => 'test',
'rng_seed_hash' => 'pending-match',
'raw_seed_encrypted' => null,
'status' => DrawResultBatchStatus::Published->value,
'created_by' => null,
@@ -368,28 +402,71 @@ test('my-match returns hit numbers when draw published', function (): void {
]);
foreach (DrawPrizeLayout::slots() as $slot) {
$num = $slot['prize_type'] === 'first' ? '1234' : '5678';
DrawResultItem::query()->create([
'draw_id' => $draw->id,
'result_batch_id' => $batch->id,
'prize_type' => $slot['prize_type'],
'prize_index' => $slot['prize_index'],
'number_4d' => $num,
'suffix_3d' => substr($num, -3),
'suffix_2d' => substr($num, -2),
'head_digit' => (int) substr($num, 0, 1),
'tail_digit' => (int) substr($num, 3, 1),
'number_4d' => '1234',
'suffix_3d' => '234',
'suffix_2d' => '34',
'head_digit' => 1,
'tail_digit' => 4,
]);
}
$draw->forceFill([
'status' => DrawStatus::Cooldown->value,
'current_result_version' => 1,
])->save();
$order = TicketOrder::query()->create([
'order_no' => 'ORD-PENDING-MATCH',
'player_id' => $player->id,
'draw_id' => $draw->id,
'currency_code' => 'NPR',
'total_bet_amount' => 10_000,
'total_rebate_amount' => 0,
'total_actual_deduct' => 10_000,
'total_estimated_payout' => 20_000,
'status' => 'placed',
'submit_source' => 'h5',
'client_trace_id' => 'pending-match',
]);
$item = \App\Models\TicketItem::query()->create([
'ticket_no' => 'TKPENDINGMATCH',
'order_id' => $order->id,
'player_id' => $player->id,
'draw_id' => $draw->id,
'original_number' => '1234',
'normalized_number' => '1234',
'play_code' => 'big',
'dimension' => 4,
'digit_slot' => null,
'bet_mode' => 'single',
'unit_bet_amount' => 10_000,
'total_bet_amount' => 10_000,
'rebate_rate_snapshot' => '0.0000',
'commission_rate_snapshot' => '0.0000',
'actual_deduct_amount' => 10_000,
'odds_snapshot_json' => [],
'rule_snapshot_json' => [],
'combination_count' => 1,
'estimated_max_payout' => 20_000,
'risk_locked_amount' => 20_000,
'status' => 'success',
'win_amount' => 0,
'jackpot_win_amount' => 0,
]);
\App\Models\TicketCombination::query()->create([
'ticket_item_id' => $item->id,
'combination_no' => 0,
'number_4d' => '1234',
'bet_amount' => 10_000,
'estimated_payout' => 20_000,
]);
$this->withHeader('Authorization', 'Bearer dev:'.$player->id)
->getJson('/api/v1/ticket/draws/20260511-778/my-match')
->getJson('/api/v1/ticket/draws/20260514-779/my-match')
->assertOk()
->assertJsonPath('data.has_bets', true)
->assertJsonPath('data.hit_numbers_4d', ['1234']);
->assertJsonPath('data.winning_ticket_count', 0)
->assertJsonPath('data.hit_numbers_4d', []);
});