feat: 增强后台设置校验、代理权限控制与财务审计能力
This commit is contained in:
@@ -6,13 +6,20 @@ use App\Models\AdminUser;
|
||||
use App\Models\AgentNode;
|
||||
use App\Models\AgentProfile;
|
||||
use App\Models\Player;
|
||||
use App\Support\AdminScopeContext;
|
||||
use App\Support\AdminScopeContextResolver;
|
||||
use App\Support\AdminAgentSettlementScope;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
/** 代理账号仪表盘:授信、团队规模、待结账单摘要。 */
|
||||
final class AgentDashboardOverviewBuilder
|
||||
{
|
||||
public function __construct(
|
||||
private readonly AdminReportQueryService $reportQuery,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>|null
|
||||
*/
|
||||
@@ -46,6 +53,16 @@ final class AgentDashboardOverviewBuilder
|
||||
}
|
||||
|
||||
$pendingBillStats = $this->pendingBillStats($admin, $subtreeIds);
|
||||
$scope = AdminScopeContextResolver::fromValues($admin, requestedAgentNodeId: (int) $node->id);
|
||||
$today = now()->toDateString();
|
||||
$sevenDayFrom = now()->subDays(6)->toDateString();
|
||||
$todayTotals = $this->reportQuery->periodFinanceTotals($today, $today, $scope);
|
||||
$sevenDayTotals = $this->reportQuery->periodFinanceTotals($sevenDayFrom, $today, $scope);
|
||||
$currencyCode = $this->reportQuery->resolvePeriodCurrencyCode($today, $today, $scope)
|
||||
?? $this->reportQuery->resolvePeriodCurrencyCode($sevenDayFrom, $today, $scope);
|
||||
$teamPlayerStats = $this->teamPlayerStats($subtreeIds);
|
||||
$todayActivityStats = $this->todayActivityStats($subtreeIds, $today);
|
||||
$topAgentToday = $this->topAgentToday($scope, $today, (int) $node->id);
|
||||
|
||||
return [
|
||||
'agent_node_id' => (int) $node->id,
|
||||
@@ -61,13 +78,25 @@ final class AgentDashboardOverviewBuilder
|
||||
),
|
||||
'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,
|
||||
'can_create_child_agent' => (bool) ($profile?->can_create_child_agent ?? false),
|
||||
'can_create_player' => (bool) ($profile?->can_create_player ?? false),
|
||||
'direct_child_count' => $directChildCount,
|
||||
'subtree_agent_count' => count($subtreeIds),
|
||||
'direct_player_count' => $directPlayerCount,
|
||||
'team_player_count' => $teamPlayerStats['count'],
|
||||
'active_player_count_today' => $todayActivityStats['player_count'],
|
||||
'bet_order_count_today' => $todayActivityStats['order_count'],
|
||||
'today_bet_minor' => $todayTotals['total_bet_minor'],
|
||||
'today_payout_minor' => $todayTotals['total_payout_minor'],
|
||||
'today_profit_minor' => $todayTotals['approx_house_gross_minor'],
|
||||
'seven_day_bet_minor' => $sevenDayTotals['total_bet_minor'],
|
||||
'seven_day_payout_minor' => $sevenDayTotals['total_payout_minor'],
|
||||
'seven_day_profit_minor' => $sevenDayTotals['approx_house_gross_minor'],
|
||||
'currency_code' => $currencyCode,
|
||||
'pending_bill_count' => $pendingBillStats['count'],
|
||||
'pending_unpaid_minor' => $pendingBillStats['unpaid_minor'],
|
||||
'latest_bet_at' => $todayActivityStats['latest_bet_at'],
|
||||
'top_agent_today' => $topAgentToday,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -94,4 +123,64 @@ final class AgentDashboardOverviewBuilder
|
||||
'unpaid_minor' => (int) $query->sum('unpaid_amount'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<int> $subtreeIds
|
||||
* @return array{count: int}
|
||||
*/
|
||||
private function teamPlayerStats(array $subtreeIds): array
|
||||
{
|
||||
if ($subtreeIds === [] || ! Schema::hasColumn('players', 'agent_node_id')) {
|
||||
return ['count' => 0];
|
||||
}
|
||||
|
||||
return [
|
||||
'count' => (int) Player::query()
|
||||
->whereIn('agent_node_id', $subtreeIds)
|
||||
->count(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<int> $subtreeIds
|
||||
* @return array{player_count: int, order_count: int, latest_bet_at: ?string}
|
||||
*/
|
||||
private function todayActivityStats(array $subtreeIds, string $today): array
|
||||
{
|
||||
if ($subtreeIds === []) {
|
||||
return [
|
||||
'player_count' => 0,
|
||||
'order_count' => 0,
|
||||
'latest_bet_at' => null,
|
||||
];
|
||||
}
|
||||
|
||||
$base = DB::table('ticket_orders as o')
|
||||
->join('players as p', 'p.id', '=', 'o.player_id')
|
||||
->whereIn('p.agent_node_id', $subtreeIds)
|
||||
->whereDate('o.created_at', $today);
|
||||
|
||||
$latestBetAt = (clone $base)->max('o.created_at');
|
||||
|
||||
return [
|
||||
'player_count' => (int) (clone $base)->distinct('o.player_id')->count('o.player_id'),
|
||||
'order_count' => (int) (clone $base)->count(),
|
||||
'latest_bet_at' => $latestBetAt !== null ? Carbon::parse((string) $latestBetAt)->toIso8601String() : null,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>|null
|
||||
*/
|
||||
private function topAgentToday(AdminScopeContext $scope, string $today, int $currentAgentId): ?array
|
||||
{
|
||||
$rows = $this->reportQuery->agentRankingRows($today, $today, null, 5, $scope);
|
||||
foreach ($rows as $row) {
|
||||
if ((int) ($row['agent_node_id'] ?? 0) === $currentAgentId) {
|
||||
return $row;
|
||||
}
|
||||
}
|
||||
|
||||
return $rows[0] ?? null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user