feat: 增强后台设置校验、代理权限控制与财务审计能力

This commit is contained in:
2026-06-09 13:44:08 +08:00
parent 8d5d7f5b17
commit 41b964a606
25 changed files with 894 additions and 49 deletions

View File

@@ -133,7 +133,6 @@ final class AdminDashboardSnapshotBuilder
{
return $admin->hasPermissionCode('service.reconcile.manage')
|| $admin->hasPermissionCode('service.reconcile.view')
|| $admin->hasPermissionCode('service.wallet.view')
|| $admin->hasPermissionCode('service.wallet.manage');
}

View File

@@ -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;
}
}

View File

@@ -274,14 +274,14 @@ final class AgentProfileService
{
$profile = $this->profileForNode($agentNodeId);
return $profile === null || $profile->can_create_child_agent;
return (bool) ($profile?->can_create_child_agent ?? false);
}
public function nodeMayCreatePlayer(int $agentNodeId): bool
{
$profile = $this->profileForNode($agentNodeId);
return $profile === null || $profile->can_create_player;
return (bool) ($profile?->can_create_player ?? false);
}
private function assertAgentProfileExists(AgentNode $agent): void

View File

@@ -520,6 +520,15 @@ final class SettlementCenterLedgerService
array_values($creditById),
), static fn (int $id): bool => $id > 0)),
);
$creditBillRefs = $this->settlementBillsByIds(
$admin,
array_values(array_filter(array_map(
static fn (object $row): int => (string) ($row->ref_type ?? '') === 'settlement_bill'
? (int) ($row->ref_id ?? 0)
: 0,
array_values($creditById),
), static fn (int $id): bool => $id > 0)),
);
$paymentById = [];
if ($paymentIds !== []) {
@@ -556,7 +565,10 @@ final class SettlementCenterLedgerService
if ($kind === 'credit' && isset($creditById[$id])) {
$row = $creditById[$id];
$pid = (int) $row->player_id;
$items[] = $this->formatCreditEntry($row, $playerBills[$pid] ?? null, $ticketRefs);
$bill = (string) ($row->ref_type ?? '') === 'settlement_bill'
? ($creditBillRefs[(int) ($row->ref_id ?? 0)] ?? null)
: ($playerBills[$pid] ?? null);
$items[] = $this->formatCreditEntry($row, $bill, $ticketRefs);
} elseif ($kind === 'payment' && isset($paymentById[$id])) {
$items[] = $this->formatPaymentEntry($paymentById[$id]);
} elseif ($kind === 'adjustment' && isset($adjustmentById[$id])) {
@@ -569,6 +581,40 @@ final class SettlementCenterLedgerService
return $items;
}
/**
* @param list<int> $ids
* @return array<int, object>
*/
private function settlementBillsByIds(AdminUser $admin, array $ids): array
{
$ids = array_values(array_unique(array_filter($ids, static fn (int $id): bool => $id > 0)));
if ($ids === []) {
return [];
}
$query = DB::table('settlement_bills as sb')
->whereIn('sb.id', $ids)
->select([
'sb.id',
'sb.owner_id as player_id',
'sb.status',
'sb.bill_type',
'sb.net_amount',
'sb.unpaid_amount',
'sb.paid_amount',
'sb.settlement_period_id',
]);
AdminAgentSettlementScope::applySubtreeToBillsQuery($query, $admin, 'sb');
$map = [];
foreach ($query->get() as $bill) {
$map[(int) $bill->id] = $bill;
}
return $map;
}
/**
* @return array{0: Carbon|null, 1: Carbon|null}
*/
@@ -1273,6 +1319,8 @@ final class SettlementCenterLedgerService
private function formatPaymentEntry(object $row): array
{
$amount = (int) $row->amount;
$method = trim((string) ($row->method ?? ''));
$methodLabel = $method !== '' && ! ctype_digit($method) ? ' · '.$method : '';
return array_merge(
$this->baseRow(
@@ -1289,7 +1337,7 @@ final class SettlementCenterLedgerService
billStatus: (string) ($row->bill_status ?? ''),
billType: (string) ($row->bill_type ?? ''),
billUnpaid: isset($row->unpaid_amount) ? (int) $row->unpaid_amount : null,
refLabel: 'bill#'.$row->settlement_bill_id.($row->method ? ' · '.$row->method : ''),
refLabel: 'bill#'.$row->settlement_bill_id.$methodLabel,
),
$this->partyFieldsFromRow($row),
);