feat: 重构注单控制器以复用共享筛选逻辑
新增 TicketItemListFilters trait,用于封装注单列表的通用筛选逻辑。 更新 AdminPlayerTicketItemsIndexController、AdminTicketItemIndexController 与 TicketItemsIndexController,统一使用新的注单编号搜索与订单日期范围筛选方法,提升代码复用性与可读性。 增强 AdminRiskPoolManualStatusController:支持发布手动停售状态变更通知。 优化 RiskPoolService 与 TicketWalletService:钱包资金变动后实时通知余额更新。 更新测试用例,确保重构后功能行为保持一致。
This commit is contained in:
79
app/Services/Ticket/RiskPoolRealtimePublisher.php
Normal file
79
app/Services/Ticket/RiskPoolRealtimePublisher.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Ticket;
|
||||
|
||||
use App\Models\Draw;
|
||||
use App\Models\RiskPool;
|
||||
use App\Services\Draw\LotteryHallRealtimeBroadcaster;
|
||||
|
||||
/**
|
||||
* 风险池占用变化后推送 `risk.warning` / `risk.sold_out`(频道 `lottery-hall`)。
|
||||
*/
|
||||
final class RiskPoolRealtimePublisher
|
||||
{
|
||||
private const WARNING_RATIO = 0.8;
|
||||
|
||||
/** @var array<int, string> */
|
||||
private array $drawNoById = [];
|
||||
|
||||
public function __construct(
|
||||
private readonly LotteryHallRealtimeBroadcaster $hallRealtime,
|
||||
) {}
|
||||
|
||||
public function publishAfterLock(
|
||||
int $drawId,
|
||||
string $normalizedNumber,
|
||||
int $soldOutStatusBefore,
|
||||
int $lockedAmountBefore,
|
||||
int $totalCapBefore,
|
||||
RiskPool $poolAfter,
|
||||
): void {
|
||||
$drawNo = $this->resolveDrawNo($drawId);
|
||||
$normalizedNumber = strtoupper(trim($normalizedNumber));
|
||||
|
||||
$totalCap = (int) $poolAfter->total_cap_amount;
|
||||
if ($totalCap < 1) {
|
||||
$totalCap = max(1, $totalCapBefore);
|
||||
}
|
||||
|
||||
$soldOutAfter = (int) $poolAfter->sold_out_status;
|
||||
if ($soldOutAfter === 1 && $soldOutStatusBefore !== 1) {
|
||||
$this->hallRealtime->notifyRiskSoldOut($drawId, $drawNo, $normalizedNumber);
|
||||
}
|
||||
|
||||
$lockedAfter = (int) $poolAfter->locked_amount;
|
||||
$usageBefore = $totalCapBefore > 0 ? $lockedAmountBefore / $totalCapBefore : 0.0;
|
||||
$usageAfter = $totalCap > 0 ? $lockedAfter / $totalCap : 1.0;
|
||||
|
||||
if ($usageAfter >= self::WARNING_RATIO && $usageBefore < self::WARNING_RATIO) {
|
||||
$this->hallRealtime->notifyRiskWarning(
|
||||
$drawId,
|
||||
$drawNo,
|
||||
$normalizedNumber,
|
||||
$usageAfter,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function publishManualSoldOut(Draw $draw, string $normalizedNumber): void
|
||||
{
|
||||
$this->hallRealtime->notifyRiskSoldOut(
|
||||
(int) $draw->id,
|
||||
(string) $draw->draw_no,
|
||||
strtoupper(trim($normalizedNumber)),
|
||||
);
|
||||
}
|
||||
|
||||
private function resolveDrawNo(int $drawId): string
|
||||
{
|
||||
if (isset($this->drawNoById[$drawId])) {
|
||||
return $this->drawNoById[$drawId];
|
||||
}
|
||||
|
||||
$drawNo = Draw::query()->whereKey($drawId)->value('draw_no');
|
||||
$resolved = is_string($drawNo) && $drawNo !== '' ? $drawNo : (string) $drawId;
|
||||
$this->drawNoById[$drawId] = $resolved;
|
||||
|
||||
return $resolved;
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Services\Ticket;
|
||||
|
||||
use App\Models\Draw;
|
||||
use App\Models\RiskPool;
|
||||
use App\Lottery\ErrorCode;
|
||||
use App\Models\TicketItem;
|
||||
@@ -14,6 +15,7 @@ final class RiskPoolService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly PlayCatalogResolver $catalogResolver,
|
||||
private readonly RiskPoolRealtimePublisher $riskRealtime,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -90,6 +92,10 @@ final class RiskPoolService
|
||||
throw new TicketOperationException('risk_sold_out', ErrorCode::RiskPoolSoldOut->value);
|
||||
}
|
||||
|
||||
$soldOutBefore = (int) $pool->sold_out_status;
|
||||
$lockedBefore = (int) $pool->locked_amount;
|
||||
$totalBefore = (int) $pool->total_cap_amount;
|
||||
|
||||
$pool->forceFill([
|
||||
'locked_amount' => (int) $pool->locked_amount + $amount,
|
||||
'remaining_amount' => (int) $pool->remaining_amount - $amount,
|
||||
@@ -97,6 +103,15 @@ final class RiskPoolService
|
||||
'version' => (int) $pool->version + 1,
|
||||
])->save();
|
||||
|
||||
$this->riskRealtime->publishAfterLock(
|
||||
$drawId,
|
||||
$lock['number_4d'],
|
||||
$soldOutBefore,
|
||||
$lockedBefore,
|
||||
$totalBefore,
|
||||
$pool,
|
||||
);
|
||||
|
||||
RiskPoolLockLog::query()->create([
|
||||
'draw_id' => $drawId,
|
||||
'normalized_number' => $lock['number_4d'],
|
||||
@@ -172,6 +187,11 @@ final class RiskPoolService
|
||||
}
|
||||
}
|
||||
|
||||
public function publishManualSoldOut(Draw $draw, string $normalizedNumber): void
|
||||
{
|
||||
$this->riskRealtime->publishManualSoldOut($draw, $normalizedNumber);
|
||||
}
|
||||
|
||||
/** 后台改池或释池后,将 Redis 风控快照与 DB 对齐。 */
|
||||
public function syncRedisStateFromPool(RiskPool $pool): void
|
||||
{
|
||||
@@ -279,6 +299,10 @@ LUA;
|
||||
throw new TicketOperationException('risk_sold_out', ErrorCode::RiskPoolSoldOut->value);
|
||||
}
|
||||
|
||||
$soldOutBefore = (int) $pool->sold_out_status;
|
||||
$lockedBefore = (int) $pool->locked_amount;
|
||||
$totalBefore = (int) $pool->total_cap_amount;
|
||||
|
||||
$pool->forceFill([
|
||||
'locked_amount' => (int) $pool->locked_amount + $amount,
|
||||
'remaining_amount' => (int) $pool->remaining_amount - $amount,
|
||||
@@ -286,6 +310,15 @@ LUA;
|
||||
'version' => (int) $pool->version + 1,
|
||||
])->save();
|
||||
|
||||
$this->riskRealtime->publishAfterLock(
|
||||
$drawId,
|
||||
$number4d,
|
||||
$soldOutBefore,
|
||||
$lockedBefore,
|
||||
$totalBefore,
|
||||
$pool,
|
||||
);
|
||||
|
||||
RiskPoolLockLog::query()->create([
|
||||
'draw_id' => $drawId,
|
||||
'normalized_number' => $number4d,
|
||||
|
||||
@@ -8,9 +8,13 @@ use App\Lottery\ErrorCode;
|
||||
use App\Models\TicketOrder;
|
||||
use App\Models\PlayerWallet;
|
||||
use App\Exceptions\TicketOperationException;
|
||||
use App\Services\Wallet\WalletBalanceRealtimeNotifier;
|
||||
|
||||
final class TicketWalletService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly WalletBalanceRealtimeNotifier $balanceRealtime,
|
||||
) {}
|
||||
private const TXN_POSTED = 'posted';
|
||||
|
||||
private const TXN_DIR_OUT = 2;
|
||||
@@ -71,6 +75,9 @@ final class TicketWalletService
|
||||
'remark' => null,
|
||||
]);
|
||||
|
||||
$wallet->refresh();
|
||||
$this->balanceRealtime->notifyAfterMovement($wallet, -$amountMinor, 'bet_deduct');
|
||||
|
||||
return $after;
|
||||
}
|
||||
|
||||
@@ -119,6 +126,9 @@ final class TicketWalletService
|
||||
'idempotent_key' => $idempotentKey,
|
||||
'remark' => 'post_deduct_confirmation_failed',
|
||||
]);
|
||||
|
||||
$wallet->refresh();
|
||||
$this->balanceRealtime->notifyAfterMovement($wallet, $amount, 'bet_reverse');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -187,6 +197,9 @@ final class TicketWalletService
|
||||
'idempotent_key' => $idempotentKey,
|
||||
'remark' => 'manual_jackpot_burst',
|
||||
]);
|
||||
|
||||
$wallet->refresh();
|
||||
$this->balanceRealtime->notifyAfterMovement($wallet, $amountMinor, 'jackpot_manual_payout');
|
||||
}
|
||||
|
||||
public function creditSettlementPayout(Player $player, string $currencyCode, int $amountMinor, int $settlementBatchId): void
|
||||
@@ -243,6 +256,9 @@ final class TicketWalletService
|
||||
'idempotent_key' => $idempotentKey,
|
||||
'remark' => null,
|
||||
]);
|
||||
|
||||
$wallet->refresh();
|
||||
$this->balanceRealtime->notifyAfterMovement($wallet, $amountMinor, 'settle_payout');
|
||||
}
|
||||
|
||||
private function newTxnNo(): string
|
||||
|
||||
Reference in New Issue
Block a user