Files
lotteryLaravel/app/Services/Ticket/TicketPendingConfirmReconcileService.php
kang 1dcd4716c5 refactor: 更新权限管理与请求验证逻辑
- 在多个控制器中将权限检查从 hasAdminPermission 更新为 hasPermissionCode,以增强权限管理的灵活性。
- 引入 AdminScopePolicy,优化基于代理节点的权限和数据过滤逻辑,确保管理员能够更精确地控制访问权限。
- 在请求验证中添加 agent_node_id 字段,确保 API 接口支持代理节点的相关操作。
- 更新 AdminUser 模型,新增 hasPermissionCode 方法,以支持更细粒度的权限检查。
- 优化审计日志记录逻辑,确保在处理请求时能够准确记录管理员的操作。
2026-06-03 10:07:38 +08:00

179 lines
5.7 KiB
PHP

<?php
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,
) {}
/**
* @return array{scanned:int, confirmed:int, refunded:int}
*/
public function reconcile(int $staleMinutes, int $limit): array
{
$cutoff = now()->subMinutes($staleMinutes);
$orders = TicketOrder::query()
->whereIn('status', ['pending_confirm', 'partial_pending_confirm'])
->where('updated_at', '<=', $cutoff)
->orderBy('id')
->limit($limit)
->get();
$summary = ['scanned' => 0, 'confirmed' => 0, 'refunded' => 0];
foreach ($orders as $order) {
$result = DB::transaction(function () use ($order): string {
$lockedOrder = TicketOrder::query()
->whereKey($order->id)
->lockForUpdate()
->first();
if ($lockedOrder === null || ! in_array($lockedOrder->status, ['pending_confirm', 'partial_pending_confirm'], true)) {
return 'skipped';
}
$hasPostedDeduct = WalletTxn::query()
->where('biz_type', 'bet_deduct')
->where('biz_no', $lockedOrder->order_no)
->where('status', 'posted')
->exists();
if ($hasPostedDeduct) {
return $this->confirmOrder($lockedOrder);
}
return $this->refundOrderWithoutDeduct($lockedOrder);
});
if ($result === 'skipped') {
continue;
}
$summary['scanned']++;
if ($result === 'confirmed') {
$summary['confirmed']++;
}
if ($result === 'refunded') {
$summary['refunded']++;
}
}
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);
}
$this->ticketWallet->releaseReservedBetDeduct($lockedOrder, $reasonCode.'_release');
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';
}
}