feat: 增强代理和玩家管理功能
- 在多个控制器中更新权限检查逻辑,确保管理员能够更灵活地管理代理和玩家。 - 在 AdminPlayerStoreController 中引入对玩家创建能力的验证,确保只有具备相应权限的管理员能够创建玩家。 - 更新请求验证逻辑,新增 credit_limit、rebate_rate 和 extra_rebate_rate 字段,以支持更细粒度的玩家管理。 - 在 AgentNodeProfileController 中添加对父代理能力授予的验证,确保子代理的权限在父代理范围内。 - 引入 AgentProfileFieldRules 以简化代理资料更新请求的规则定义,提升代码复用性。
This commit is contained in:
@@ -3,6 +3,9 @@
|
||||
namespace App\Services\Player;
|
||||
|
||||
use App\Models\Player;
|
||||
use App\Support\AgentOverdueGuard;
|
||||
use App\Support\CreditAmountScale;
|
||||
use App\Support\PlayerFundingMode;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
@@ -14,19 +17,33 @@ final class PlayerCreditService
|
||||
public function upsertAccount(Player $player, array $payload): void
|
||||
{
|
||||
$limit = max(0, (int) ($payload['credit_limit'] ?? 0));
|
||||
$now = now();
|
||||
$exists = DB::table('player_credit_accounts')
|
||||
->where('player_id', $player->id)
|
||||
->exists();
|
||||
|
||||
DB::table('player_credit_accounts')->updateOrInsert(
|
||||
['player_id' => $player->id],
|
||||
[
|
||||
'credit_limit' => $limit,
|
||||
'used_credit' => DB::raw('COALESCE(used_credit, 0)'),
|
||||
'frozen_credit' => DB::raw('COALESCE(frozen_credit, 0)'),
|
||||
'updated_at' => now(),
|
||||
'created_at' => now(),
|
||||
],
|
||||
);
|
||||
if ($exists) {
|
||||
DB::table('player_credit_accounts')
|
||||
->where('player_id', $player->id)
|
||||
->update([
|
||||
'credit_limit' => $limit,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
DB::table('player_credit_accounts')->insert([
|
||||
'player_id' => $player->id,
|
||||
'credit_limit' => $limit,
|
||||
'used_credit' => 0,
|
||||
'frozen_credit' => 0,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
]);
|
||||
}
|
||||
|
||||
/** 可用授信(主货币整数,与后台「授信额度」一致)。 */
|
||||
public function availableCredit(Player $player): int
|
||||
{
|
||||
$row = DB::table('player_credit_accounts')->where('player_id', $player->id)->first();
|
||||
@@ -37,34 +54,45 @@ final class PlayerCreditService
|
||||
return max(0, (int) $row->credit_limit - (int) $row->used_credit - (int) $row->frozen_credit);
|
||||
}
|
||||
|
||||
public function holdForBet(Player $player, int $amount): void
|
||||
/** 可用授信(最小货币单位,供玩家端钱包/下注与钱包余额 API 对齐)。 */
|
||||
public function availableCreditMinor(Player $player, ?string $currencyCode = null): int
|
||||
{
|
||||
if ($amount <= 0) {
|
||||
$currency = $currencyCode ?? (string) $player->default_currency;
|
||||
|
||||
return CreditAmountScale::majorToMinor($this->availableCredit($player), $currency);
|
||||
}
|
||||
|
||||
public function holdForBet(Player $player, int $amountMinor): void
|
||||
{
|
||||
if ($amountMinor <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! \App\Support\CreditLineMode::isEnabledForSiteCode((string) $player->site_code)) {
|
||||
if (! PlayerFundingMode::usesCredit($player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$available = $this->availableCredit($player);
|
||||
if ($amount > $available) {
|
||||
$currency = (string) $player->default_currency;
|
||||
$availableMinor = $this->availableCreditMinor($player, $currency);
|
||||
if ($amountMinor > $availableMinor) {
|
||||
throw ValidationException::withMessages([
|
||||
'credit' => ['insufficient'],
|
||||
]);
|
||||
}
|
||||
|
||||
$majorDelta = CreditAmountScale::minorToMajor($amountMinor, $currency);
|
||||
|
||||
DB::table('player_credit_accounts')
|
||||
->where('player_id', $player->id)
|
||||
->update([
|
||||
'used_credit' => DB::raw('used_credit + '.$amount),
|
||||
'used_credit' => DB::raw('used_credit + '.$majorDelta),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
DB::table('credit_ledger')->insert([
|
||||
'owner_type' => 'player',
|
||||
'owner_id' => $player->id,
|
||||
'amount' => -$amount,
|
||||
'amount' => -$amountMinor,
|
||||
'reason' => 'bet_hold',
|
||||
'ref_type' => 'bet',
|
||||
'ref_id' => null,
|
||||
@@ -73,23 +101,97 @@ final class PlayerCreditService
|
||||
]);
|
||||
}
|
||||
|
||||
public function releaseFromSettlement(Player $player, int $amount, int $billId): void
|
||||
public function applySettledLoss(Player $player, int $amountMinor, int $ticketItemId): void
|
||||
{
|
||||
if ($amount <= 0) {
|
||||
if ($amountMinor <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (! PlayerFundingMode::usesCredit($player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$currency = (string) $player->default_currency;
|
||||
$majorDelta = CreditAmountScale::minorToMajor($amountMinor, $currency);
|
||||
|
||||
DB::table('player_credit_accounts')
|
||||
->where('player_id', $player->id)
|
||||
->update([
|
||||
'used_credit' => DB::raw('GREATEST(0, used_credit - '.$amount.')'),
|
||||
'used_credit' => DB::raw('used_credit + '.$majorDelta),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
DB::table('credit_ledger')->insert([
|
||||
'owner_type' => 'player',
|
||||
'owner_id' => $player->id,
|
||||
'amount' => $amount,
|
||||
'amount' => -$amountMinor,
|
||||
'reason' => 'game_settlement_loss',
|
||||
'ref_type' => 'ticket_item',
|
||||
'ref_id' => $ticketItemId,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function assertMayPlaceBet(Player $player, int $amountMinor): void
|
||||
{
|
||||
if (! PlayerFundingMode::usesCredit($player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$overdue = DB::table('settlement_bills')
|
||||
->where('owner_type', 'player')
|
||||
->where('owner_id', $player->id)
|
||||
->where('status', 'overdue')
|
||||
->where('unpaid_amount', '>', 0)
|
||||
->exists();
|
||||
|
||||
if ($overdue) {
|
||||
throw ValidationException::withMessages([
|
||||
'credit' => ['overdue'],
|
||||
]);
|
||||
}
|
||||
|
||||
$agentNodeId = (int) ($player->agent_node_id ?? 0);
|
||||
if ($agentNodeId > 0) {
|
||||
AgentOverdueGuard::assertAgentMayGrantCredit($agentNodeId);
|
||||
}
|
||||
|
||||
$this->holdForBet($player, $amountMinor);
|
||||
}
|
||||
|
||||
public function releaseBetHold(Player $player, int $amountMinor, int $ticketItemId): void
|
||||
{
|
||||
if ($amountMinor <= 0 || ! PlayerFundingMode::usesCredit($player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->decreaseUsedCredit($player, $amountMinor);
|
||||
|
||||
DB::table('credit_ledger')->insert([
|
||||
'owner_type' => 'player',
|
||||
'owner_id' => $player->id,
|
||||
'amount' => $amountMinor,
|
||||
'reason' => 'bet_hold_release',
|
||||
'ref_type' => 'ticket_item',
|
||||
'ref_id' => $ticketItemId,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function releaseFromSettlement(Player $player, int $amountMinor, int $billId): void
|
||||
{
|
||||
if ($amountMinor <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->decreaseUsedCredit($player, $amountMinor);
|
||||
|
||||
DB::table('credit_ledger')->insert([
|
||||
'owner_type' => 'player',
|
||||
'owner_id' => $player->id,
|
||||
'amount' => $amountMinor,
|
||||
'reason' => 'settlement_confirm',
|
||||
'ref_type' => 'settlement_bill',
|
||||
'ref_id' => $billId,
|
||||
@@ -97,4 +199,26 @@ final class PlayerCreditService
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
private function decreaseUsedCredit(Player $player, int $amountMinor): void
|
||||
{
|
||||
if ($amountMinor <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$playerId = (int) $player->id;
|
||||
$row = DB::table('player_credit_accounts')->where('player_id', $playerId)->first();
|
||||
if ($row === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$majorDelta = CreditAmountScale::minorToMajor($amountMinor, (string) $player->default_currency);
|
||||
$next = max(0, (int) $row->used_credit - $majorDelta);
|
||||
DB::table('player_credit_accounts')
|
||||
->where('player_id', $playerId)
|
||||
->update([
|
||||
'used_credit' => $next,
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user