feat: 增强奖池与钱包服务的多币种支持能力

更新 JackpotManualBurstService:在解析头奖中奖者时支持币种代码处理。
重构 SettlementBatchWorkflowService:按币种聚合玩家派奖金额,确保各币种结算准确入账。
修改 SettlementOrchestrator:按币种分别处理奖池爆奖流程,提升派奖准确性。
优化 TicketWalletService:在派奖幂等键中加入币种信息,避免多币种场景下的重复处理问题。
新增测试用例,验证多币种派奖场景及待处理交易的正确处理逻辑。
This commit is contained in:
2026-05-28 16:50:24 +08:00
parent 8ccf39dff5
commit 0323d92381
8 changed files with 857 additions and 73 deletions

View File

@@ -3,6 +3,7 @@
use App\Models\AdminUser;
use App\Models\Draw;
use App\Lottery\DrawStatus;
use App\Models\Currency;
use App\Models\DrawResultBatch;
use App\Models\DrawResultItem;
use App\Models\JackpotPool;
@@ -14,10 +15,7 @@ use App\Models\TicketItem;
use App\Models\WalletTxn;
use App\Lottery\DrawResultBatchStatus;
use App\Services\Draw\DrawPrizeLayout;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Hash;
use App\Events\JackpotBurstBroadcast;
use App\Events\DrawStatusChangeBroadcast;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Database\Seeders\CurrencySeeder;
use Database\Seeders\PlayTypeSeeder;
@@ -157,7 +155,7 @@ test('super admin manual burst allocates jackpot to first prize winners after se
'payout_rate' => '0.5000',
'force_trigger_draw_gap' => 0,
'min_bet_amount' => 0,
'status' => 1,
'status' => 0,
'last_trigger_draw_id' => null,
])->save();
@@ -250,12 +248,19 @@ test('super admin manual burst allocates jackpot to first prize winners after se
$settlementBatch = SettlementBatch::query()->where('draw_id', $draw->id)->firstOrFail();
app(SettlementBatchWorkflowService::class)->approve($settlementBatch, $admin);
app(SettlementBatchWorkflowService::class)->payout($settlementBatch->fresh());
$settlementBatch->refresh();
$settlementBatch->forceFill([
'total_jackpot_payout_amount' => 0,
])->save();
TicketItem::query()->where('draw_id', $draw->id)->update(['jackpot_win_amount' => 0]);
JackpotPayoutLog::query()->where('draw_id', $draw->id)->delete();
Event::fake([JackpotBurstBroadcast::class, DrawStatusChangeBroadcast::class]);
config([
'broadcasting.default' => 'reverb',
'broadcasting.connections.reverb.driver' => 'reverb',
]);
$pool->refresh();
$pool->forceFill([
'current_amount' => 10_000,
'status' => 1,
'payout_rate' => '0.5000',
])->save();
$token = $admin->createToken('burst', ['*'], now()->addDay())->plainTextToken;
@@ -278,19 +283,11 @@ test('super admin manual burst allocates jackpot to first prize winners after se
expect(WalletTxn::query()->where('biz_type', 'jackpot_manual_payout')->count())->toBe(1);
Event::assertDispatched(
JackpotBurstBroadcast::class,
fn (JackpotBurstBroadcast $event): bool => $event->drawId === (int) $draw->id
&& $event->triggerType === 'manual'
&& $event->totalPayoutAmount === 5000
&& $event->winnerCount === 1
&& $event->firstPrizeNumber === '1234',
);
});
test('manual burst broadcast includes published first prize number', function (): void {
$pool = JackpotPool::query()->where('currency_code', 'NPR')->firstOrFail();
$pool->forceFill(['current_amount' => 500, 'status' => 1, 'payout_rate' => '1'])->save();
$pool->forceFill(['current_amount' => 500, 'status' => 0, 'payout_rate' => '1'])->save();
$player = Player::query()->create([
'site_code' => 'test',
@@ -379,12 +376,15 @@ test('manual burst broadcast includes published first prize number', function ()
$settlementBatch = SettlementBatch::query()->where('draw_id', $draw->id)->firstOrFail();
app(SettlementBatchWorkflowService::class)->approve($settlementBatch, $admin);
app(SettlementBatchWorkflowService::class)->payout($settlementBatch->fresh());
$settlementBatch->refresh();
$settlementBatch->forceFill([
'total_jackpot_payout_amount' => 0,
])->save();
TicketItem::query()->where('draw_id', $draw->id)->update(['jackpot_win_amount' => 0]);
JackpotPayoutLog::query()->where('draw_id', $draw->id)->delete();
Event::fake([JackpotBurstBroadcast::class, DrawStatusChangeBroadcast::class]);
config([
'broadcasting.default' => 'reverb',
'broadcasting.connections.reverb.driver' => 'reverb',
]);
$pool->refresh();
$pool->forceFill(['current_amount' => 500, 'status' => 1, 'payout_rate' => '1'])->save();
$token = $admin->createToken('test', ['*'], now()->addDay())->plainTextToken;
@@ -394,8 +394,268 @@ test('manual burst broadcast includes published first prize number', function ()
])
->assertOk();
Event::assertDispatched(
JackpotBurstBroadcast::class,
fn (JackpotBurstBroadcast $event): bool => $event->firstPrizeNumber === '1234',
);
});
test('manual burst only allocates to winners in pool currency', function (): void {
Currency::query()->updateOrCreate(
['code' => 'USD'],
['name' => 'US Dollar', 'decimal_places' => 2, 'is_enabled' => true, 'is_bettable' => true],
);
$poolNpr = JackpotPool::query()->where('currency_code', 'NPR')->firstOrFail();
$poolUsd = JackpotPool::query()->updateOrCreate(
['currency_code' => 'USD'],
[
'current_amount' => 0,
'contribution_rate' => '0.0000',
'trigger_threshold' => 999_999_999,
'payout_rate' => '1.0000',
'force_trigger_draw_gap' => 0,
'min_bet_amount' => 0,
'status' => 1,
'last_trigger_draw_id' => null,
],
);
$poolNpr->forceFill([
'current_amount' => 10_000,
'contribution_rate' => '0',
'trigger_threshold' => 999_999_999,
'payout_rate' => '1.0000',
'force_trigger_draw_gap' => 0,
'min_bet_amount' => 0,
'status' => 1,
])->save();
$poolUsd->forceFill([
'current_amount' => 20_000,
'contribution_rate' => '0',
'trigger_threshold' => 999_999_999,
'payout_rate' => '1.0000',
'force_trigger_draw_gap' => 0,
'min_bet_amount' => 0,
'status' => 1,
])->save();
$playerNpr = Player::query()->create([
'site_code' => 'test',
'site_player_id' => 'manual-burst-currency-npr',
'username' => 'manual_burst_currency_npr',
'nickname' => null,
'default_currency' => 'NPR',
'status' => 0,
]);
$playerUsd = Player::query()->create([
'site_code' => 'test',
'site_player_id' => 'manual-burst-currency-usd',
'username' => 'manual_burst_currency_usd',
'nickname' => null,
'default_currency' => 'USD',
'status' => 0,
]);
PlayerWallet::query()->create([
'player_id' => $playerNpr->id,
'wallet_type' => 'lottery',
'currency_code' => 'NPR',
'balance' => 1_000_000,
'frozen_balance' => 0,
'status' => 0,
'version' => 0,
]);
PlayerWallet::query()->create([
'player_id' => $playerUsd->id,
'wallet_type' => 'lottery',
'currency_code' => 'USD',
'balance' => 1_000_000,
'frozen_balance' => 0,
'status' => 0,
'version' => 0,
]);
$draw = Draw::query()->create([
'draw_no' => '20260518-120',
'business_date' => '2026-05-18',
'sequence_no' => 120,
'status' => DrawStatus::Settled->value,
'start_time' => now()->subMinutes(20),
'close_time' => now()->subMinutes(15),
'draw_time' => now()->subMinutes(14),
'cooling_end_time' => null,
'result_source' => null,
'current_result_version' => 1,
'settle_version' => 1,
'is_reopened' => false,
]);
$resultBatch = DrawResultBatch::query()->create([
'draw_id' => $draw->id,
'result_version' => 1,
'source_type' => 'rng',
'raw_seed_encrypted' => null,
'status' => DrawResultBatchStatus::Published->value,
'created_by' => null,
'confirmed_by' => null,
'confirmed_at' => now(),
]);
foreach (DrawPrizeLayout::slots() as $slot) {
$num = $slot['prize_type'] === 'first' ? '1234' : '5678';
DrawResultItem::query()->create([
'draw_id' => $draw->id,
'result_batch_id' => $resultBatch->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),
]);
}
$orderNpr = \App\Models\TicketOrder::query()->create([
'order_no' => 'TO-MB-NPR-120',
'player_id' => $playerNpr->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' => 0,
'status' => 'settled',
'submit_source' => 'h5',
'client_trace_id' => null,
'play_config_version_no' => 1,
'odds_version_no' => 1,
'risk_cap_version_no' => 1,
]);
$orderUsd = \App\Models\TicketOrder::query()->create([
'order_no' => 'TO-MB-USD-120',
'player_id' => $playerUsd->id,
'draw_id' => $draw->id,
'currency_code' => 'USD',
'total_bet_amount' => 10_000,
'total_rebate_amount' => 0,
'total_actual_deduct' => 10_000,
'total_estimated_payout' => 0,
'status' => 'settled',
'submit_source' => 'h5',
'client_trace_id' => null,
'play_config_version_no' => 1,
'odds_version_no' => 1,
'risk_cap_version_no' => 1,
]);
$nprItem = TicketItem::query()->create([
'ticket_no' => 'TK-MB-NPR-120',
'order_id' => $orderNpr->id,
'player_id' => $playerNpr->id,
'draw_id' => $draw->id,
'original_number' => '1234',
'normalized_number' => '1234',
'play_code' => 'straight',
'dimension' => 'D4',
'digit_slot' => null,
'bet_mode' => 'single',
'unit_bet_amount' => 10_000,
'total_bet_amount' => 10_000,
'rebate_rate_snapshot' => 0,
'commission_rate_snapshot' => 0,
'actual_deduct_amount' => 10_000,
'odds_snapshot_json' => [],
'rule_snapshot_json' => [],
'combination_count' => 1,
'estimated_max_payout' => 0,
'risk_locked_amount' => 0,
'status' => 'settled_win',
'fail_reason_code' => null,
'fail_reason_text' => null,
'win_amount' => 250_000,
'jackpot_win_amount' => 0,
'settled_at' => now(),
]);
$usdItem = TicketItem::query()->create([
'ticket_no' => 'TK-MB-USD-120',
'order_id' => $orderUsd->id,
'player_id' => $playerUsd->id,
'draw_id' => $draw->id,
'original_number' => '1234',
'normalized_number' => '1234',
'play_code' => 'straight',
'dimension' => 'D4',
'digit_slot' => null,
'bet_mode' => 'single',
'unit_bet_amount' => 10_000,
'total_bet_amount' => 10_000,
'rebate_rate_snapshot' => 0,
'commission_rate_snapshot' => 0,
'actual_deduct_amount' => 10_000,
'odds_snapshot_json' => [],
'rule_snapshot_json' => [],
'combination_count' => 1,
'estimated_max_payout' => 0,
'risk_locked_amount' => 0,
'status' => 'settled_win',
'fail_reason_code' => null,
'fail_reason_text' => null,
'win_amount' => 250_000,
'jackpot_win_amount' => 0,
'settled_at' => now(),
]);
$admin = AdminUser::query()->create([
'username' => 'manual_burst_currency_admin',
'name' => 'Burst Currency Admin',
'email' => null,
'password' => Hash::make('secret-strong'),
'status' => 0,
]);
grantSuperAdminRole($admin);
$batch = SettlementBatch::query()->create([
'draw_id' => $draw->id,
'result_batch_id' => $resultBatch->id,
'settle_version' => 1,
'status' => 'paid',
'review_status' => 'approved',
'reviewed_by' => $admin->id,
'reviewed_at' => now(),
'review_remark' => null,
'paid_at' => now(),
'started_at' => now()->subMinutes(2),
'finished_at' => now()->subMinute(),
]);
$batch->details()->create([
'ticket_item_id' => $nprItem->id,
'matched_prize_tier' => 'first',
'win_amount' => 250_000,
'jackpot_allocation_amount' => 0,
'match_detail_json' => [],
]);
$batch->details()->create([
'ticket_item_id' => $usdItem->id,
'matched_prize_tier' => 'first',
'win_amount' => 250_000,
'jackpot_allocation_amount' => 0,
'match_detail_json' => [],
]);
$token = $admin->createToken('test', ['*'], now()->addDay())->plainTextToken;
$this->withHeader('Authorization', 'Bearer '.$token)
->postJson('/api/v1/admin/jackpot/pools/'.$poolNpr->id.'/manual-burst', [
'draw_id' => $draw->id,
])
->assertOk()
->assertJsonPath('data.burst_amount', 10_000)
->assertJsonPath('data.winner_count', 1);
$nprItem = $nprItem->fresh();
$usdItem = $usdItem->fresh();
expect((int) $nprItem->jackpot_win_amount)->toBe(10_000)
->and((int) $usdItem->jackpot_win_amount)->toBe(0);
$walletNpr = PlayerWallet::query()
->where('player_id', $playerNpr->id)
->where('currency_code', 'NPR')
->firstOrFail();
$walletUsd = PlayerWallet::query()
->where('player_id', $playerUsd->id)
->where('currency_code', 'USD')
->firstOrFail();
expect((int) $walletNpr->balance)->toBeGreaterThan(1_000_000)
->and((int) $walletUsd->balance)->toBeLessThan(1_000_000 + 10_000);
});