feat: 增强奖池与钱包管理功能
更新 AdminJackpotPoolUpdateController 校验规则,禁止传入 current_amount。 优化 AdminRiskPoolManualStatusController:更新奖池状态后同步 Redis 状态。 在 TransferOrderReconcileController 中新增 completeCredit 方法,用于处理卡住的转账订单对账。 调整 TransferOrderListController:优化转账订单处理条件。 在 TicketItemsIndexController 中实现支持时区的日期筛选,提升日期处理准确性。 扩展 JackpotPool 模型,新增 adjustments 关联关系。 改进票据与钱包相关服务中的错误处理和事务管理。
This commit is contained in:
@@ -172,6 +172,28 @@ final class RiskPoolService
|
||||
}
|
||||
}
|
||||
|
||||
/** 后台改池或释池后,将 Redis 风控快照与 DB 对齐。 */
|
||||
public function syncRedisStateFromPool(RiskPool $pool): void
|
||||
{
|
||||
if (! $this->shouldUseRedisAtomicLocks()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$total = (int) $pool->total_cap_amount;
|
||||
$locked = (int) $pool->locked_amount;
|
||||
$remaining = max(0, $total - $locked);
|
||||
|
||||
Redis::eval(
|
||||
$this->overwriteStateLua(),
|
||||
1,
|
||||
$this->redisPoolKey((int) $pool->draw_id, (string) $pool->normalized_number),
|
||||
$total,
|
||||
$locked,
|
||||
$remaining,
|
||||
(int) $pool->version,
|
||||
);
|
||||
}
|
||||
|
||||
private function shouldUseRedisAtomicLocks(): bool
|
||||
{
|
||||
if (App::environment('testing')) {
|
||||
@@ -196,6 +218,14 @@ return 1
|
||||
LUA;
|
||||
}
|
||||
|
||||
private function overwriteStateLua(): string
|
||||
{
|
||||
return <<<'LUA'
|
||||
redis.call('HMSET', KEYS[1], 'total', ARGV[1], 'locked', ARGV[2], 'remaining', ARGV[3], 'version', ARGV[4])
|
||||
return 1
|
||||
LUA;
|
||||
}
|
||||
|
||||
private function acquireLua(): string
|
||||
{
|
||||
return <<<'LUA'
|
||||
|
||||
@@ -2,15 +2,21 @@
|
||||
|
||||
namespace App\Services\Ticket;
|
||||
|
||||
use App\Models\Draw;
|
||||
use App\Models\WalletTxn;
|
||||
use App\Models\TicketItem;
|
||||
use App\Models\TicketOrder;
|
||||
use App\Services\Draw\DrawHallSnapshotBuilder;
|
||||
use App\Services\Jackpot\JackpotContributionService;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
final class TicketPendingConfirmReconcileService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly RiskPoolService $riskPool,
|
||||
private readonly JackpotContributionService $jackpotContribution,
|
||||
private readonly DrawHallSnapshotBuilder $drawHallSnapshot,
|
||||
private readonly TicketWalletService $ticketWallet,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -20,7 +26,7 @@ final class TicketPendingConfirmReconcileService
|
||||
{
|
||||
$cutoff = now()->subMinutes($staleMinutes);
|
||||
$orders = TicketOrder::query()
|
||||
->where('status', 'pending_confirm')
|
||||
->whereIn('status', ['pending_confirm', 'partial_pending_confirm'])
|
||||
->where('updated_at', '<=', $cutoff)
|
||||
->orderBy('id')
|
||||
->limit($limit)
|
||||
@@ -35,7 +41,7 @@ final class TicketPendingConfirmReconcileService
|
||||
->lockForUpdate()
|
||||
->first();
|
||||
|
||||
if ($lockedOrder === null || $lockedOrder->status !== 'pending_confirm') {
|
||||
if ($lockedOrder === null || ! in_array($lockedOrder->status, ['pending_confirm', 'partial_pending_confirm'], true)) {
|
||||
return 'skipped';
|
||||
}
|
||||
|
||||
@@ -46,52 +52,10 @@ final class TicketPendingConfirmReconcileService
|
||||
->exists();
|
||||
|
||||
if ($hasPostedDeduct) {
|
||||
TicketItem::query()
|
||||
->where('order_id', $lockedOrder->id)
|
||||
->where('status', 'pending_confirm')
|
||||
->update([
|
||||
'status' => 'pending_draw',
|
||||
'fail_reason_code' => null,
|
||||
'fail_reason_text' => null,
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
$lockedOrder->forceFill(['status' => 'placed'])->save();
|
||||
|
||||
return 'confirmed';
|
||||
return $this->confirmOrder($lockedOrder);
|
||||
}
|
||||
|
||||
$items = TicketItem::query()
|
||||
->where('order_id', $lockedOrder->id)
|
||||
->where('status', 'pending_confirm')
|
||||
->with('combinations')
|
||||
->lockForUpdate()
|
||||
->get();
|
||||
|
||||
foreach ($items as $item) {
|
||||
$locks = [];
|
||||
foreach ($item->combinations as $combo) {
|
||||
$locks[] = [
|
||||
'number_4d' => (string) $combo->number_4d,
|
||||
'amount' => (int) $combo->estimated_payout,
|
||||
];
|
||||
}
|
||||
|
||||
if ($locks !== []) {
|
||||
$this->riskPool->release((int) $lockedOrder->draw_id, $item, $locks);
|
||||
}
|
||||
|
||||
$item->forceFill([
|
||||
'status' => 'refunded',
|
||||
'fail_reason_code' => 'pending_confirm_timeout',
|
||||
'fail_reason_text' => 'pending_confirm_timeout_refund',
|
||||
'risk_locked_amount' => 0,
|
||||
])->save();
|
||||
}
|
||||
|
||||
$lockedOrder->forceFill(['status' => 'refunded'])->save();
|
||||
|
||||
return 'refunded';
|
||||
return $this->refundOrderWithoutDeduct($lockedOrder);
|
||||
});
|
||||
|
||||
if ($result === 'skipped') {
|
||||
@@ -109,4 +73,104 @@ final class TicketPendingConfirmReconcileService
|
||||
|
||||
return $summary;
|
||||
}
|
||||
|
||||
private function confirmOrder(TicketOrder $lockedOrder): string
|
||||
{
|
||||
$draw = Draw::query()->whereKey($lockedOrder->draw_id)->first();
|
||||
if ($draw === null || ! $this->drawHallSnapshot->isBettingOpen($draw)) {
|
||||
return $this->refundStalePendingOrder(
|
||||
$lockedOrder,
|
||||
$draw === null ? 'draw_missing' : 'draw_no_longer_open',
|
||||
);
|
||||
}
|
||||
|
||||
$items = TicketItem::query()
|
||||
->where('order_id', $lockedOrder->id)
|
||||
->where('status', 'pending_confirm')
|
||||
->lockForUpdate()
|
||||
->get();
|
||||
|
||||
foreach ($items as $item) {
|
||||
$item->forceFill([
|
||||
'status' => 'pending_draw',
|
||||
'fail_reason_code' => null,
|
||||
'fail_reason_text' => null,
|
||||
])->save();
|
||||
|
||||
$this->jackpotContribution->recordFromPlacedTicketItem($item, $draw, (string) $lockedOrder->currency_code);
|
||||
}
|
||||
|
||||
$hasFailures = TicketItem::query()
|
||||
->where('order_id', $lockedOrder->id)
|
||||
->where('status', 'failed')
|
||||
->exists();
|
||||
|
||||
$lockedOrder->forceFill([
|
||||
'status' => $hasFailures ? 'partial_failed' : 'placed',
|
||||
])->save();
|
||||
|
||||
return 'confirmed';
|
||||
}
|
||||
|
||||
private function refundStalePendingOrder(TicketOrder $lockedOrder, string $reasonCode): string
|
||||
{
|
||||
$hasPostedDeduct = WalletTxn::query()
|
||||
->where('biz_type', 'bet_deduct')
|
||||
->where('biz_no', $lockedOrder->order_no)
|
||||
->where('status', 'posted')
|
||||
->exists();
|
||||
|
||||
if ($hasPostedDeduct) {
|
||||
$this->ticketWallet->reverseBetDeduct($lockedOrder);
|
||||
}
|
||||
|
||||
return $this->refundPendingConfirmItems($lockedOrder, $reasonCode);
|
||||
}
|
||||
|
||||
private function refundOrderWithoutDeduct(TicketOrder $lockedOrder): string
|
||||
{
|
||||
return $this->refundPendingConfirmItems($lockedOrder, 'pending_confirm_timeout');
|
||||
}
|
||||
|
||||
private function refundPendingConfirmItems(TicketOrder $lockedOrder, string $reasonCode): string
|
||||
{
|
||||
$items = TicketItem::query()
|
||||
->where('order_id', $lockedOrder->id)
|
||||
->where('status', 'pending_confirm')
|
||||
->with('combinations')
|
||||
->lockForUpdate()
|
||||
->get();
|
||||
|
||||
foreach ($items as $item) {
|
||||
$locks = [];
|
||||
foreach ($item->combinations as $combo) {
|
||||
$locks[] = [
|
||||
'number_4d' => (string) $combo->number_4d,
|
||||
'amount' => (int) $combo->estimated_payout,
|
||||
];
|
||||
}
|
||||
|
||||
if ($locks !== []) {
|
||||
$this->riskPool->release((int) $lockedOrder->draw_id, $item, $locks);
|
||||
}
|
||||
|
||||
$item->forceFill([
|
||||
'status' => 'refunded',
|
||||
'fail_reason_code' => $reasonCode,
|
||||
'fail_reason_text' => $reasonCode.'_refund',
|
||||
'risk_locked_amount' => 0,
|
||||
])->save();
|
||||
}
|
||||
|
||||
$hasFailures = TicketItem::query()
|
||||
->where('order_id', $lockedOrder->id)
|
||||
->where('status', 'failed')
|
||||
->exists();
|
||||
|
||||
$lockedOrder->forceFill([
|
||||
'status' => $hasFailures ? 'partial_failed' : 'refunded',
|
||||
])->save();
|
||||
|
||||
return 'refunded';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,13 +7,13 @@ 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;
|
||||
use App\Models\TicketCombination;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Exceptions\TicketOperationException;
|
||||
use App\Services\Jackpot\JackpotContributionService;
|
||||
use App\Services\Draw\DrawHallSnapshotBuilder;
|
||||
|
||||
final class TicketPlacementService
|
||||
{
|
||||
@@ -23,6 +23,7 @@ final class TicketPlacementService
|
||||
private readonly RiskPoolService $riskPoolService,
|
||||
private readonly TicketWalletService $ticketWalletService,
|
||||
private readonly JackpotContributionService $jackpotContribution,
|
||||
private readonly DrawHallSnapshotBuilder $drawHallSnapshot,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -36,11 +37,14 @@ final class TicketPlacementService
|
||||
? (string) $payload['client_trace_id']
|
||||
: null;
|
||||
|
||||
if ($clientTraceId !== null) {
|
||||
$drawNo = (string) $payload['draw_id'];
|
||||
$drawIdForIdempotency = Draw::query()->where('draw_no', $drawNo)->value('id');
|
||||
|
||||
if ($clientTraceId !== null && $drawIdForIdempotency !== null) {
|
||||
$existing = TicketOrder::query()
|
||||
->where('player_id', $player->id)
|
||||
->where('draw_id', $drawIdForIdempotency)
|
||||
->where('client_trace_id', $clientTraceId)
|
||||
->whereIn('status', ['placed', 'partial_failed'])
|
||||
->first();
|
||||
|
||||
if ($existing !== null) {
|
||||
@@ -72,7 +76,7 @@ final class TicketPlacementService
|
||||
if ($draw === null) {
|
||||
throw new TicketOperationException('draw_not_found', ErrorCode::BetInvalidDraw->value);
|
||||
}
|
||||
if ($draw->status !== DrawStatus::Open->value || ($draw->close_time !== null && now()->greaterThanOrEqualTo($draw->close_time))) {
|
||||
if (! $this->drawHallSnapshot->isBettingOpen($draw)) {
|
||||
throw new TicketOperationException('draw_closed', ErrorCode::DrawClosed->value);
|
||||
}
|
||||
|
||||
@@ -132,13 +136,15 @@ final class TicketPlacementService
|
||||
);
|
||||
}
|
||||
|
||||
$walletBalance = (int) (PlayerWallet::query()
|
||||
$wallet = PlayerWallet::query()
|
||||
->where('player_id', $player->id)
|
||||
->where('wallet_type', 'lottery')
|
||||
->where('currency_code', $currencyCode)
|
||||
->lockForUpdate()
|
||||
->value('balance') ?? 0);
|
||||
if ($walletBalance < $totalActualDeduct) {
|
||||
->first();
|
||||
$walletBalance = $wallet !== null ? (int) $wallet->balance : 0;
|
||||
$walletAvailable = $walletBalance - ($wallet !== null ? (int) $wallet->frozen_balance : 0);
|
||||
if ($walletAvailable < $totalActualDeduct) {
|
||||
throw new TicketOperationException('bet_insufficient_balance', ErrorCode::BetInsufficientBalance->value);
|
||||
}
|
||||
|
||||
@@ -333,6 +339,10 @@ final class TicketPlacementService
|
||||
$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', 'pending_draw')->count();
|
||||
$pendingConfirmCount = TicketItem::query()
|
||||
->where('order_id', $order->id)
|
||||
->whereIn('status', ['pending_confirm', 'partial_pending_confirm'])
|
||||
->count();
|
||||
$failureCount = TicketItem::query()->where('order_id', $order->id)->where('status', 'failed')->count();
|
||||
if ($balanceAfter === null) {
|
||||
$walletTxn = WalletTxn::query()
|
||||
@@ -350,11 +360,13 @@ final class TicketPlacementService
|
||||
'status' => $draw->status,
|
||||
],
|
||||
'summary' => [
|
||||
'order_status' => $order->status,
|
||||
'total_bet_amount' => (int) $order->total_bet_amount,
|
||||
'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' => $successCount,
|
||||
'pending_confirm_count' => $pendingConfirmCount,
|
||||
'failure_count' => $failureCount,
|
||||
],
|
||||
'balance_after' => $balanceAfter,
|
||||
|
||||
@@ -4,8 +4,8 @@ namespace App\Services\Ticket;
|
||||
|
||||
use App\Models\Draw;
|
||||
use App\Lottery\ErrorCode;
|
||||
use App\Lottery\DrawStatus;
|
||||
use App\Exceptions\TicketOperationException;
|
||||
use App\Services\Draw\DrawHallSnapshotBuilder;
|
||||
|
||||
final class TicketPreviewService
|
||||
{
|
||||
@@ -13,6 +13,7 @@ final class TicketPreviewService
|
||||
private readonly PlayCatalogResolver $catalogResolver,
|
||||
private readonly PlayRuleEngine $ruleEngine,
|
||||
private readonly RiskPoolService $riskPoolService,
|
||||
private readonly DrawHallSnapshotBuilder $drawHallSnapshot,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -25,7 +26,7 @@ final class TicketPreviewService
|
||||
if ($draw === null) {
|
||||
throw new TicketOperationException('draw_not_found', ErrorCode::BetInvalidDraw->value);
|
||||
}
|
||||
if ($draw->status !== DrawStatus::Open->value || ($draw->close_time !== null && now()->greaterThanOrEqualTo($draw->close_time))) {
|
||||
if (! $this->drawHallSnapshot->isBettingOpen($draw)) {
|
||||
throw new TicketOperationException('draw_closed', ErrorCode::DrawClosed->value);
|
||||
}
|
||||
|
||||
@@ -108,10 +109,13 @@ final class TicketPreviewService
|
||||
);
|
||||
}
|
||||
|
||||
$nowUtc = now()->utc();
|
||||
|
||||
return [
|
||||
'draw' => [
|
||||
'draw_id' => $draw->draw_no,
|
||||
'status' => $draw->status,
|
||||
'status' => $this->drawHallSnapshot->effectiveHallDisplayStatus($draw, $nowUtc),
|
||||
'db_status' => $draw->status,
|
||||
],
|
||||
'config_versions' => $this->catalogResolver->currentActiveVersionStamp(),
|
||||
'summary' => [
|
||||
|
||||
@@ -39,8 +39,13 @@ final class TicketWalletService
|
||||
$wallet = PlayerWallet::query()->whereKey($wallet->id)->lockForUpdate()->firstOrFail();
|
||||
}
|
||||
|
||||
if ((int) $wallet->status !== 0) {
|
||||
throw new TicketOperationException('wallet_frozen', ErrorCode::WalletExternalRejected->value);
|
||||
}
|
||||
|
||||
$before = (int) $wallet->balance;
|
||||
if ($before < $amountMinor) {
|
||||
$available = $before - (int) $wallet->frozen_balance;
|
||||
if ($available < $amountMinor) {
|
||||
throw new TicketOperationException('bet_insufficient_balance', ErrorCode::BetInsufficientBalance->value);
|
||||
}
|
||||
|
||||
@@ -62,7 +67,7 @@ final class TicketWalletService
|
||||
'balance_after' => $after,
|
||||
'status' => self::TXN_POSTED,
|
||||
'external_ref_no' => null,
|
||||
'idempotent_key' => $order->client_trace_id,
|
||||
'idempotent_key' => 'bet_deduct:'.$order->order_no,
|
||||
'remark' => null,
|
||||
]);
|
||||
|
||||
@@ -191,6 +196,10 @@ final class TicketWalletService
|
||||
}
|
||||
|
||||
$currency = strtoupper($currencyCode);
|
||||
$idempotentKey = 'settle-payout:'.$settlementBatchId.':'.$player->id;
|
||||
if (WalletTxn::query()->where('idempotent_key', $idempotentKey)->where('biz_type', 'settle_payout')->exists()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$wallet = PlayerWallet::query()
|
||||
->where('player_id', $player->id)
|
||||
@@ -231,7 +240,7 @@ final class TicketWalletService
|
||||
'balance_after' => $after,
|
||||
'status' => self::TXN_POSTED,
|
||||
'external_ref_no' => null,
|
||||
'idempotent_key' => 'settle-payout:'.$settlementBatchId.':'.$player->id,
|
||||
'idempotent_key' => $idempotentKey,
|
||||
'remark' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user