feat: 增强奖池与钱包管理功能
更新 AdminJackpotPoolUpdateController 校验规则,禁止传入 current_amount。 优化 AdminRiskPoolManualStatusController:更新奖池状态后同步 Redis 状态。 在 TransferOrderReconcileController 中新增 completeCredit 方法,用于处理卡住的转账订单对账。 调整 TransferOrderListController:优化转账订单处理条件。 在 TicketItemsIndexController 中实现支持时区的日期筛选,提升日期处理准确性。 扩展 JackpotPool 模型,新增 adjustments 关联关系。 改进票据与钱包相关服务中的错误处理和事务管理。
This commit is contained in:
91
app/Services/Draw/DrawCancelBetRefundService.php
Normal file
91
app/Services/Draw/DrawCancelBetRefundService.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Draw;
|
||||
|
||||
use App\Models\Draw;
|
||||
use App\Models\TicketItem;
|
||||
use App\Models\TicketOrder;
|
||||
use App\Models\WalletTxn;
|
||||
use App\Services\Ticket\RiskPoolService;
|
||||
use App\Services\Ticket\TicketWalletService;
|
||||
|
||||
/**
|
||||
* 取消期号前:退本、释池,避免已扣款注单悬空。
|
||||
*/
|
||||
final class DrawCancelBetRefundService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly RiskPoolService $riskPool,
|
||||
private readonly TicketWalletService $ticketWallet,
|
||||
) {}
|
||||
|
||||
public function refundOpenBetsForDraw(Draw $draw): void
|
||||
{
|
||||
$hasBlockedItems = TicketItem::query()
|
||||
->where('draw_id', $draw->id)
|
||||
->whereIn('status', ['settled_win', 'settled_lose', 'pending_payout'])
|
||||
->exists();
|
||||
|
||||
if ($hasBlockedItems) {
|
||||
throw new \RuntimeException('draw_has_settled_tickets');
|
||||
}
|
||||
|
||||
$orders = TicketOrder::query()
|
||||
->where('draw_id', $draw->id)
|
||||
->whereNotIn('status', ['refunded', 'failed'])
|
||||
->orderBy('id')
|
||||
->get();
|
||||
|
||||
foreach ($orders as $order) {
|
||||
$this->refundOrder($draw, $order);
|
||||
}
|
||||
}
|
||||
|
||||
private function refundOrder(Draw $draw, TicketOrder $order): void
|
||||
{
|
||||
$lockedOrder = TicketOrder::query()->whereKey($order->id)->lockForUpdate()->first();
|
||||
if ($lockedOrder === null || in_array($lockedOrder->status, ['refunded', 'failed'], true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$items = TicketItem::query()
|
||||
->where('order_id', $lockedOrder->id)
|
||||
->whereIn('status', ['pending_confirm', 'pending_draw', 'pending_payout'])
|
||||
->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) $draw->id, $item, $locks);
|
||||
}
|
||||
|
||||
$item->forceFill([
|
||||
'status' => 'refunded',
|
||||
'fail_reason_code' => 'draw_cancelled',
|
||||
'fail_reason_text' => 'draw_cancelled_refund',
|
||||
'risk_locked_amount' => 0,
|
||||
])->save();
|
||||
}
|
||||
|
||||
$hasPostedDeduct = WalletTxn::query()
|
||||
->where('biz_type', 'bet_deduct')
|
||||
->where('biz_no', $lockedOrder->order_no)
|
||||
->where('status', 'posted')
|
||||
->exists();
|
||||
|
||||
if ($hasPostedDeduct) {
|
||||
$this->ticketWallet->reverseBetDeduct($lockedOrder);
|
||||
}
|
||||
|
||||
$lockedOrder->forceFill(['status' => 'refunded'])->save();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user