- 在多个控制器中更新权限检查逻辑,确保管理员能够更灵活地管理代理和玩家。 - 在 AdminPlayerStoreController 中引入对玩家创建能力的验证,确保只有具备相应权限的管理员能够创建玩家。 - 更新请求验证逻辑,新增 credit_limit、rebate_rate 和 extra_rebate_rate 字段,以支持更细粒度的玩家管理。 - 在 AgentNodeProfileController 中添加对父代理能力授予的验证,确保子代理的权限在父代理范围内。 - 引入 AgentProfileFieldRules 以简化代理资料更新请求的规则定义,提升代码复用性。
56 lines
1.6 KiB
PHP
56 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Agent;
|
|
|
|
use App\Models\AgentNode;
|
|
use App\Models\AgentProfile;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* 按「下发即占用」真理源重算代理已下发额度:
|
|
* 直属玩家 credit_limit 之和 + 直属下级代理 credit_limit 之和。
|
|
*/
|
|
final class AgentCreditAllocatedSyncService
|
|
{
|
|
public function syncForAgent(AgentNode $agent): void
|
|
{
|
|
$profile = AgentProfile::query()->where('agent_node_id', $agent->id)->first();
|
|
if ($profile === null) {
|
|
return;
|
|
}
|
|
|
|
$expected = $this->calculateAllocatedCredit($agent);
|
|
if ((int) $profile->allocated_credit === $expected) {
|
|
return;
|
|
}
|
|
|
|
$profile->allocated_credit = $expected;
|
|
$profile->save();
|
|
}
|
|
|
|
public function syncForAgentId(int $agentNodeId): void
|
|
{
|
|
$agent = AgentNode::query()->find($agentNodeId);
|
|
if ($agent === null) {
|
|
return;
|
|
}
|
|
|
|
$this->syncForAgent($agent);
|
|
}
|
|
|
|
public function calculateAllocatedCredit(AgentNode $agent): int
|
|
{
|
|
$playerTotal = (int) DB::table('player_credit_accounts as pca')
|
|
->join('players as p', 'p.id', '=', 'pca.player_id')
|
|
->where('p.agent_node_id', $agent->id)
|
|
->sum('pca.credit_limit');
|
|
|
|
$childIds = AgentNode::query()->where('parent_id', $agent->id)->pluck('id');
|
|
$childAgentTotal = $childIds->isEmpty()
|
|
? 0
|
|
: (int) AgentProfile::query()->whereIn('agent_node_id', $childIds)->sum('credit_limit');
|
|
|
|
return $playerTotal + $childAgentTotal;
|
|
}
|
|
}
|