Files
lotteryLaravel/app/Services/Settlement/SettlementBatchWorkflowService.php
kang e27a00f260 feat: 更新玩法配置管理,简化字段并增强功能
- 将玩法相关的显示名称字段统一为 `display_name`,移除多语言字段。
- 在 `PlayTypePatchController` 中新增即时切换玩法开关的功能,并推送大厅更新。
- 优化多个控制器和服务中的权限检查与数据处理逻辑,提升代码可读性与维护性。
2026-05-25 14:34:24 +08:00

145 lines
5.7 KiB
PHP

<?php
namespace App\Services\Settlement;
use App\Models\Draw;
use App\Models\Player;
use App\Models\AdminUser;
use App\Models\TicketItem;
use App\Lottery\DrawStatus;
use App\Models\TicketOrder;
use App\Models\SettlementBatch;
use Illuminate\Support\Facades\DB;
use App\Lottery\SettlementBatchStatus;
use App\Services\Ticket\TicketWalletService;
final class SettlementBatchWorkflowService
{
public function __construct(
private readonly TicketWalletService $wallet,
) {}
public function approve(SettlementBatch $batch, AdminUser $admin, ?string $remark = null): SettlementBatch
{
return $this->approveInternal($batch, $admin->id, $remark);
}
public function approveBySystem(SettlementBatch $batch, ?string $remark = null): SettlementBatch
{
return $this->approveInternal($batch, null, $remark);
}
private function approveInternal(SettlementBatch $batch, ?int $reviewedBy, ?string $remark): SettlementBatch
{
return DB::transaction(function () use ($batch, $reviewedBy, $remark): SettlementBatch {
/** @var SettlementBatch $locked */
$locked = SettlementBatch::query()->whereKey($batch->id)->lockForUpdate()->firstOrFail();
if ($locked->status !== SettlementBatchStatus::PendingReview->value) {
throw new \RuntimeException('settlement_not_pending_review');
}
$locked->forceFill([
'status' => SettlementBatchStatus::Approved->value,
'review_status' => 'approved',
'reviewed_by' => $reviewedBy,
'reviewed_at' => now(),
'review_remark' => $remark,
])->save();
return $locked->refresh();
});
}
public function reject(SettlementBatch $batch, AdminUser $admin, ?string $remark = null): SettlementBatch
{
return DB::transaction(function () use ($batch, $admin, $remark): SettlementBatch {
/** @var SettlementBatch $locked */
$locked = SettlementBatch::query()->whereKey($batch->id)->lockForUpdate()->firstOrFail();
if ($locked->status !== SettlementBatchStatus::PendingReview->value) {
throw new \RuntimeException('settlement_not_pending_review');
}
TicketItem::query()
->whereIn('id', $locked->details()->pluck('ticket_item_id'))
->where('status', 'pending_payout')
->update(['status' => 'success', 'win_amount' => 0, 'jackpot_win_amount' => 0]);
$locked->forceFill([
'status' => SettlementBatchStatus::Rejected->value,
'review_status' => 'rejected',
'reviewed_by' => $admin->id,
'reviewed_at' => now(),
'review_remark' => $remark,
])->save();
return $locked->refresh();
});
}
public function payout(SettlementBatch $batch): SettlementBatch
{
return DB::transaction(function () use ($batch): SettlementBatch {
/** @var SettlementBatch $locked */
$locked = SettlementBatch::query()->whereKey($batch->id)->lockForUpdate()->firstOrFail();
if ($locked->status !== SettlementBatchStatus::Approved->value || $locked->review_status !== 'approved') {
throw new \RuntimeException('settlement_not_approved');
}
$details = $locked->details()->with(['ticketItem.order'])->get();
$playerTotals = [];
$currencyByPlayer = [];
foreach ($details as $detail) {
$item = $detail->ticketItem;
if ($item === null) {
continue;
}
$finalCredit = (int) $detail->win_amount + (int) $detail->jackpot_allocation_amount;
if ($finalCredit > 0) {
$pid = (int) $item->player_id;
$playerTotals[$pid] = ($playerTotals[$pid] ?? 0) + $finalCredit;
$currencyByPlayer[$pid] = strtoupper((string) ($item->order?->currency_code ?? 'NPR'));
$item->forceFill(['status' => 'settled_win', 'settled_at' => now()])->save();
} elseif ($item->status !== 'settled_lose') {
$item->forceFill(['status' => 'settled_lose', 'settled_at' => now()])->save();
}
}
foreach ($playerTotals as $playerId => $amount) {
if ($amount <= 0) {
continue;
}
$player = Player::query()->whereKey($playerId)->firstOrFail();
$this->wallet->creditSettlementPayout($player, $currencyByPlayer[$playerId] ?? 'NPR', $amount, (int) $locked->id);
}
$orderIds = TicketItem::query()
->whereIn('id', $locked->details()->pluck('ticket_item_id'))
->pluck('order_id')
->unique()
->all();
foreach ($orderIds as $orderId) {
$pending = TicketItem::query()
->where('order_id', $orderId)
->whereNotIn('status', ['settled_win', 'settled_lose'])
->exists();
if (! $pending) {
TicketOrder::query()->whereKey($orderId)->update(['status' => 'settled']);
}
}
$locked->forceFill([
'status' => SettlementBatchStatus::Paid->value,
'paid_at' => now(),
])->save();
Draw::query()->whereKey($locked->draw_id)->update([
'status' => DrawStatus::Settled->value,
'settle_version' => (int) $locked->settle_version,
]);
return $locked->refresh();
});
}
}