feat: 更新玩法配置管理,简化字段并增强功能

- 将玩法相关的显示名称字段统一为 `display_name`,移除多语言字段。
- 在 `PlayTypePatchController` 中新增即时切换玩法开关的功能,并推送大厅更新。
- 优化多个控制器和服务中的权限检查与数据处理逻辑,提升代码可读性与维护性。
This commit is contained in:
2026-05-25 14:34:24 +08:00
parent 270d2e9af1
commit e27a00f260
74 changed files with 4469 additions and 280 deletions

View File

@@ -10,7 +10,7 @@ use App\Models\JackpotPayoutLog;
use Illuminate\Support\Collection;
/**
* 产品文档 §5.11.25.11.3:中头奖且满足阈值或连续未爆期数 按比例释放奖池,按注项 `total_bet_amount` 比例分配。
* 产品文档 §5.11.25.11.3:中头奖且满足阈值或连续未爆期数 按比例/全额释放奖池,按注项 `total_bet_amount` 比例分配。
*/
final class JackpotBurstAllocator
{
@@ -36,36 +36,72 @@ final class JackpotBurstAllocator
}
$trigger = $thresholdOk ? 'threshold' : ($gapOk ? 'forced_gap' : 'play_combo');
$releaseFullPool = $trigger === 'forced_gap';
$winnerItems = $winners->map(fn (array $r): TicketItem => $r['item'])->values();
return $this->burstToWinners(
$draw,
$pool,
$winnerItems,
$trigger,
$releaseFullPool,
[
'threshold_ok' => $thresholdOk,
'gap_ok' => $gapOk,
'combo_ok' => $comboOk,
'combo_trigger_play_codes' => $this->comboTriggerPlayCodes($pool),
],
);
}
/**
* 超管手动爆池:跳过头奖触发条件校验,仍要求存在头奖中奖注单,并按配置派彩比例释放奖池。
*
* @param Collection<int, TicketItem> $winnerItems
* @return array{allocations: array<int, int>, pool_payout: int, trigger: string, log_id: int}
*/
public function burstManual(Draw $draw, JackpotPool $pool, Collection $winnerItems): array
{
if ($winnerItems->isEmpty()) {
throw new \RuntimeException('jackpot_manual_no_first_prize_winners');
}
$out = $this->burstToWinners($draw, $pool, $winnerItems, 'manual', false, ['manual' => true]);
return [
'allocations' => $out['allocations'],
'pool_payout' => $out['pool_payout'],
'trigger' => 'manual',
'log_id' => (int) ($out['log_id'] ?? 0),
];
}
/**
* @param Collection<int, TicketItem> $winnerItems
* @param array<string, mixed> $snapshotExtra
* @return array{allocations: array<int, int>, pool_payout: int, trigger: string, log_id: int}
*/
private function burstToWinners(
Draw $draw,
JackpotPool $pool,
Collection $winnerItems,
string $trigger,
bool $releaseFullPool,
array $snapshotExtra,
): array {
$poolBefore = (int) $pool->current_amount;
$poolPayout = (int) floor($poolBefore * (float) $pool->payout_rate);
$poolPayout = $releaseFullPool
? $poolBefore
: (int) floor($poolBefore * (float) $pool->payout_rate);
if ($poolPayout <= 0) {
return ['allocations' => [], 'pool_payout' => 0, 'trigger' => null];
return ['allocations' => [], 'pool_payout' => 0, 'trigger' => $trigger, 'log_id' => 0];
}
$list = $winners->values()->all();
$weightTotal = 0;
foreach ($list as $r) {
$weightTotal += (int) $r['item']->total_bet_amount;
}
if ($weightTotal <= 0) {
return ['allocations' => [], 'pool_payout' => 0, 'trigger' => null];
}
$allocations = [];
$remaining = $poolPayout;
$n = count($list);
foreach ($list as $idx => $r) {
/** @var TicketItem $item */
$item = $r['item'];
$w = (int) $item->total_bet_amount;
if ($idx === $n - 1) {
$share = max(0, $remaining);
} else {
$share = (int) floor($poolPayout * $w / $weightTotal);
$remaining -= $share;
}
$allocations[(int) $item->id] = $share;
$allocations = $this->distributeByBetWeight($winnerItems, $poolPayout);
if ($allocations === []) {
return ['allocations' => [], 'pool_payout' => 0, 'trigger' => $trigger, 'log_id' => 0];
}
$pool->forceFill([
@@ -73,23 +109,59 @@ final class JackpotBurstAllocator
'last_trigger_draw_id' => $draw->id,
])->save();
JackpotPayoutLog::query()->create([
$log = JackpotPayoutLog::query()->create([
'draw_id' => $draw->id,
'jackpot_pool_id' => $pool->id,
'trigger_type' => $trigger,
'total_payout_amount' => $poolPayout,
'winner_count' => count($allocations),
'trigger_snapshot_json' => [
'threshold_ok' => $thresholdOk,
'gap_ok' => $gapOk,
'combo_ok' => $comboOk,
'combo_trigger_play_codes' => $this->comboTriggerPlayCodes($pool),
'trigger_snapshot_json' => array_merge($snapshotExtra, [
'pool_amount_before' => $poolBefore,
'payout_rate' => (string) $pool->payout_rate,
],
'release_full_pool' => $releaseFullPool,
]),
]);
return ['allocations' => $allocations, 'pool_payout' => $poolPayout, 'trigger' => $trigger];
return [
'allocations' => $allocations,
'pool_payout' => $poolPayout,
'trigger' => $trigger,
'log_id' => (int) $log->id,
];
}
/**
* @param Collection<int, TicketItem> $winnerItems
* @return array<int, int>
*/
private function distributeByBetWeight(Collection $winnerItems, int $poolPayout): array
{
$list = $winnerItems->values()->all();
$weightTotal = 0;
foreach ($list as $item) {
$weightTotal += (int) $item->total_bet_amount;
}
if ($weightTotal <= 0) {
return [];
}
$allocations = [];
$remaining = $poolPayout;
$n = count($list);
foreach ($list as $idx => $item) {
$w = (int) $item->total_bet_amount;
if ($idx === $n - 1) {
$share = max(0, $remaining);
} else {
$share = (int) floor($poolPayout * $w / $weightTotal);
$remaining -= $share;
}
if ($share > 0) {
$allocations[(int) $item->id] = $share;
}
}
return $allocations;
}
private function gapTriggerMet(JackpotPool $pool): bool

View File

@@ -0,0 +1,264 @@
<?php
namespace App\Services\Jackpot;
use App\Models\Draw;
use App\Models\Player;
use App\Models\TicketItem;
use App\Lottery\DrawStatus;
use App\Models\JackpotPool;
use App\Models\JackpotPayoutLog;
use App\Models\SettlementBatch;
use App\Models\TicketSettlementDetail;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use App\Lottery\SettlementBatchStatus;
use App\Lottery\DrawResultBatchStatus;
use App\Models\DrawResultBatch;
use App\Services\Draw\DrawHallSnapshotBuilder;
use App\Services\Draw\DrawResultViewService;
use App\Services\Draw\LotteryHallRealtimeBroadcaster;
use App\Services\Ticket\TicketWalletService;
/**
* 产品文档:超管紧急手动爆池 —— 对已结算期号的头奖中奖者按奖池派彩比例分配,合并入账并广播动画。
*/
final class JackpotManualBurstService
{
public function __construct(
private readonly JackpotBurstAllocator $allocator,
private readonly TicketWalletService $wallet,
private readonly LotteryHallRealtimeBroadcaster $hallRealtime,
private readonly DrawHallSnapshotBuilder $hallSnapshot,
private readonly DrawResultViewService $drawResults,
) {}
/**
* @return array{
* current_amount: int,
* burst_amount: int,
* log_id: int|null,
* winner_count: int,
* draw_no: string,
* wallet_credited: bool
* }
*/
public function execute(JackpotPool $pool, int $drawId): array
{
return DB::transaction(function () use ($pool, $drawId): array {
/** @var JackpotPool $locked */
$locked = JackpotPool::query()->whereKey($pool->id)->lockForUpdate()->firstOrFail();
if ((int) $locked->status !== 1) {
throw new \RuntimeException('jackpot_disabled');
}
if ((int) $locked->current_amount <= 0) {
throw new \RuntimeException('jackpot_pool_empty');
}
$draw = Draw::query()->whereKey($drawId)->firstOrFail();
$this->assertDrawReady($draw);
if (JackpotPayoutLog::query()
->where('jackpot_pool_id', $locked->id)
->where('draw_id', $drawId)
->exists()) {
throw new \RuntimeException('jackpot_already_burst_for_draw');
}
$batch = $this->resolveSettlementBatch($draw);
$winnerItems = $this->firstPrizeWinnerItems($batch);
if ($winnerItems->isEmpty()) {
throw new \RuntimeException('jackpot_manual_no_first_prize_winners');
}
$existingJackpot = (int) $batch->total_jackpot_payout_amount;
if ($existingJackpot > 0) {
throw new \RuntimeException('jackpot_already_allocated_for_draw');
}
$burst = $this->allocator->burstManual($draw, $locked, $winnerItems);
$poolPayout = (int) $burst['pool_payout'];
if ($poolPayout <= 0) {
return [
'current_amount' => (int) $locked->current_amount,
'burst_amount' => 0,
'log_id' => null,
'winner_count' => 0,
'draw_no' => (string) $draw->draw_no,
'wallet_credited' => false,
];
}
$allocations = $burst['allocations'];
$this->applyAllocationsToSettlement($batch, $allocations);
$walletCredited = $this->creditWalletsIfAlreadyPaid($batch, $allocations, (int) $burst['log_id'], $locked->currency_code);
$locked->refresh();
$firstPrizeNumber = $this->drawResults->firstPrizeNumber4dForDraw($draw);
if ($firstPrizeNumber === '') {
$firstPrizeNumber = '----';
}
$this->hallRealtime->notifyJackpotBurst(
(int) $draw->id,
(string) $draw->draw_no,
$firstPrizeNumber,
(string) $locked->currency_code,
$poolPayout,
count($allocations),
'manual',
(int) $locked->current_amount,
);
$this->hallRealtime->notifyStatusChange($this->hallSnapshot->build());
return [
'current_amount' => (int) $locked->current_amount,
'burst_amount' => $poolPayout,
'log_id' => (int) $burst['log_id'],
'winner_count' => count($allocations),
'draw_no' => (string) $draw->draw_no,
'wallet_credited' => $walletCredited,
];
});
}
private function assertDrawReady(Draw $draw): void
{
$allowed = [
DrawStatus::Settling->value,
DrawStatus::Settled->value,
];
if (! in_array($draw->status, $allowed, true)) {
throw new \RuntimeException('draw_not_ready_for_jackpot_burst');
}
$hasPublished = DrawResultBatch::query()
->where('draw_id', $draw->id)
->where('status', DrawResultBatchStatus::Published->value)
->where('result_version', (int) $draw->current_result_version)
->exists();
if (! $hasPublished) {
throw new \RuntimeException('draw_result_not_published');
}
}
private function resolveSettlementBatch(Draw $draw): SettlementBatch
{
$batch = SettlementBatch::query()
->where('draw_id', $draw->id)
->whereIn('status', [
SettlementBatchStatus::PendingReview->value,
SettlementBatchStatus::Approved->value,
SettlementBatchStatus::Paid->value,
SettlementBatchStatus::Completed->value,
])
->orderByDesc('id')
->first();
if ($batch === null) {
throw new \RuntimeException('settlement_batch_not_found');
}
return $batch;
}
/**
* @return Collection<int, TicketItem>
*/
private function firstPrizeWinnerItems(SettlementBatch $batch): Collection
{
$details = TicketSettlementDetail::query()
->where('settlement_batch_id', $batch->id)
->where('matched_prize_tier', 'first')
->where('win_amount', '>', 0)
->with('ticketItem')
->get();
return $details
->map(fn (TicketSettlementDetail $d) => $d->ticketItem)
->filter(fn (?TicketItem $item): bool => $item instanceof TicketItem)
->values();
}
/**
* @param array<int, int> $allocations
*/
private function applyAllocationsToSettlement(SettlementBatch $batch, array $allocations): void
{
$addedJackpot = 0;
foreach ($allocations as $ticketItemId => $share) {
$detail = TicketSettlementDetail::query()
->where('settlement_batch_id', $batch->id)
->where('ticket_item_id', $ticketItemId)
->first();
if ($detail === null) {
continue;
}
$detail->forceFill(['jackpot_allocation_amount' => $share])->save();
$item = $detail->ticketItem;
if ($item !== null) {
$item->forceFill(['jackpot_win_amount' => $share])->save();
}
$addedJackpot += $share;
}
if ($addedJackpot > 0) {
$batch->forceFill([
'total_jackpot_payout_amount' => (int) $batch->total_jackpot_payout_amount + $addedJackpot,
'total_payout_amount' => (int) $batch->total_payout_amount + $addedJackpot,
])->save();
}
}
/**
* 若结算批次已派彩,则补发 Jackpot 份额到玩家钱包。
*
* @param array<int, int> $allocations
*/
private function creditWalletsIfAlreadyPaid(
SettlementBatch $batch,
array $allocations,
int $jackpotLogId,
string $currencyCode,
): bool {
if (! in_array($batch->status, [SettlementBatchStatus::Paid->value, SettlementBatchStatus::Completed->value], true)) {
return false;
}
$playerTotals = [];
foreach ($allocations as $ticketItemId => $share) {
if ($share <= 0) {
continue;
}
$item = TicketItem::query()->whereKey($ticketItemId)->first();
if ($item === null) {
continue;
}
$pid = (int) $item->player_id;
$playerTotals[$pid] = ($playerTotals[$pid] ?? 0) + $share;
}
foreach ($playerTotals as $playerId => $amount) {
$player = Player::query()->whereKey($playerId)->firstOrFail();
$this->wallet->creditJackpotManualPayout(
$player,
$currencyCode,
$amount,
(int) $batch->id,
$jackpotLogId,
);
}
return $playerTotals !== [];
}
}