feat(credit): 实时同步代理已用额度,覆盖玩家下注/结算全流程
Some checks failed
lotterLaravel CI / test (push) Has been cancelled
lotterLaravel E2E / e2e-api (push) Has been cancelled

- PlayerCreditService 在下注占用、输赢结算、撤单释放等关键节点触发 AgentUsedCreditSyncService
- AgentProfileService::present 与 AgentDashboardOverviewBuilder 查询前主动同步 used_credit
- 确保代理链 used_credit(团队已用风险)实时反映下级玩家信用占用状态
This commit is contained in:
2026-06-30 15:15:15 +08:00
parent 200269196e
commit f1f68d09d3
6 changed files with 390 additions and 0 deletions

View File

@@ -3,6 +3,7 @@
namespace App\Services\Player;
use App\Models\Player;
use App\Services\Agent\AgentUsedCreditSyncService;
use App\Support\AgentOverdueGuard;
use App\Support\CreditAmountScale;
use App\Support\PlayerFundingMode;
@@ -12,6 +13,9 @@ use Illuminate\Validation\ValidationException;
final class PlayerCreditService
{
public function __construct(
private readonly AgentUsedCreditSyncService $usedCreditSync,
) {}
/**
* @param array{credit_limit?: int} $payload
*/
@@ -155,6 +159,8 @@ final class PlayerCreditService
'created_at' => $now,
'updated_at' => $now,
]);
$this->syncAgentUsedCredit($player);
}
public function applySettledLoss(Player $player, int $amountMinor, int $ticketItemId): void
@@ -208,6 +214,8 @@ final class PlayerCreditService
'used_credit' => (int) $row->used_credit + $majorDelta,
'updated_at' => $now,
]);
$this->syncAgentUsedCredit($player);
}
public function applySettledWin(Player $player, int $amountMinor, int $ticketItemId): void
@@ -243,6 +251,7 @@ final class PlayerCreditService
}
$this->decreaseUsedCredit($player, $amountMinor);
$this->syncAgentUsedCredit($player);
}
public function assertMayPlaceBet(Player $player, int $amountMinor): void
@@ -310,6 +319,7 @@ final class PlayerCreditService
}
$this->decreaseUsedCredit($player, $amountMinor);
$this->syncAgentUsedCredit($player);
}
public function reverseBetHold(Player $player, int $amountMinor, int $ticketOrderId): void
@@ -341,6 +351,7 @@ final class PlayerCreditService
}
$this->decreaseUsedCredit($player, $amountMinor);
$this->syncAgentUsedCredit($player);
}
public function reverseGameSettlement(Player $player, int $gameWinLossSigned, int $ticketItemId): void
@@ -395,6 +406,8 @@ final class PlayerCreditService
'used_credit' => (int) $row->used_credit + $majorDelta,
'updated_at' => $now,
]);
$this->syncAgentUsedCredit($player);
}
/**
@@ -414,6 +427,7 @@ final class PlayerCreditService
);
if ($delta > 0) {
$this->decreaseUsedCredit($player, $delta);
$this->syncAgentUsedCredit($player);
}
}
@@ -508,6 +522,17 @@ final class PlayerCreditService
]);
}
/** 玩家信用变动后同步直属代理及祖先链的 used_credit团队已用风险。 */
private function syncAgentUsedCredit(Player $player): void
{
$agentNodeId = (int) ($player->agent_node_id ?? 0);
if ($agentNodeId <= 0) {
return;
}
$this->usedCreditSync->syncForPlayerAgentChain($agentNodeId);
}
/**
* 识别数据库唯一约束冲突PostgreSQL SQLSTATE 23505SQLite SQLSTATE 23000/error code 19)。
* 兜底扫描消息中 unique 字样,兼容驱动返回的 SQLSTATE 缺失场景。