- 在多个控制器中更新权限检查逻辑,确保管理员能够更灵活地管理代理和玩家。 - 在 AdminPlayerStoreController 中引入对玩家创建能力的验证,确保只有具备相应权限的管理员能够创建玩家。 - 更新请求验证逻辑,新增 credit_limit、rebate_rate 和 extra_rebate_rate 字段,以支持更细粒度的玩家管理。 - 在 AgentNodeProfileController 中添加对父代理能力授予的验证,确保子代理的权限在父代理范围内。 - 引入 AgentProfileFieldRules 以简化代理资料更新请求的规则定义,提升代码复用性。
98 lines
3.4 KiB
PHP
98 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Admin;
|
|
|
|
use App\Models\AdminUser;
|
|
use App\Models\AgentNode;
|
|
use App\Models\AgentProfile;
|
|
use App\Models\Player;
|
|
use App\Support\AdminAgentSettlementScope;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
|
|
/** 代理账号仪表盘:授信、团队规模、待结账单摘要。 */
|
|
final class AgentDashboardOverviewBuilder
|
|
{
|
|
/**
|
|
* @return array<string, mixed>|null
|
|
*/
|
|
public function build(AdminUser $admin): ?array
|
|
{
|
|
if (! $admin->hasPermissionCode('dashboard.view')) {
|
|
return null;
|
|
}
|
|
|
|
$node = $admin->primaryAgentNode();
|
|
if ($node === null) {
|
|
return null;
|
|
}
|
|
|
|
$profile = AgentProfile::query()->where('agent_node_id', $node->id)->first();
|
|
$subtreeIds = AgentNode::query()
|
|
->where('path', 'like', $node->path.'%')
|
|
->pluck('id')
|
|
->map(static fn ($id): int => (int) $id)
|
|
->all();
|
|
|
|
$directChildCount = AgentNode::query()
|
|
->where('parent_id', $node->id)
|
|
->count();
|
|
|
|
$directPlayerCount = 0;
|
|
if (Schema::hasColumn('players', 'agent_node_id')) {
|
|
$directPlayerCount = Player::query()
|
|
->where('agent_node_id', $node->id)
|
|
->count();
|
|
}
|
|
|
|
$pendingBillStats = $this->pendingBillStats($admin, $subtreeIds);
|
|
|
|
return [
|
|
'agent_node_id' => (int) $node->id,
|
|
'agent_code' => (string) $node->code,
|
|
'agent_name' => (string) $node->name,
|
|
'depth' => (int) $node->depth,
|
|
'credit_limit' => (int) ($profile?->credit_limit ?? 0),
|
|
'allocated_credit' => (int) ($profile?->allocated_credit ?? 0),
|
|
'used_credit' => (int) ($profile?->used_credit ?? 0),
|
|
'available_credit' => max(
|
|
0,
|
|
(int) ($profile?->credit_limit ?? 0) - (int) ($profile?->allocated_credit ?? 0),
|
|
),
|
|
'total_share_rate' => (float) ($profile?->total_share_rate ?? 0),
|
|
'settlement_cycle' => (string) ($profile?->settlement_cycle ?? 'weekly'),
|
|
'can_create_child_agent' => $profile === null || $profile->can_create_child_agent,
|
|
'can_create_player' => $profile === null || $profile->can_create_player,
|
|
'direct_child_count' => $directChildCount,
|
|
'subtree_agent_count' => count($subtreeIds),
|
|
'direct_player_count' => $directPlayerCount,
|
|
'pending_bill_count' => $pendingBillStats['count'],
|
|
'pending_unpaid_minor' => $pendingBillStats['unpaid_minor'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param list<int> $subtreeIds
|
|
* @return array{count: int, unpaid_minor: int}
|
|
*/
|
|
private function pendingBillStats(AdminUser $admin, array $subtreeIds): array
|
|
{
|
|
if ($subtreeIds === []) {
|
|
return ['count' => 0, 'unpaid_minor' => 0];
|
|
}
|
|
|
|
$query = DB::table('settlement_bills')
|
|
->where('bill_type', 'agent')
|
|
->where('owner_type', 'agent')
|
|
->whereIn('owner_id', $subtreeIds)
|
|
->whereIn('status', ['pending', 'pending_confirm', 'partial']);
|
|
|
|
AdminAgentSettlementScope::applyToBillsQuery($query, $admin);
|
|
|
|
return [
|
|
'count' => (int) $query->count(),
|
|
'unpaid_minor' => (int) $query->sum('unpaid_amount'),
|
|
];
|
|
}
|
|
}
|