- 在 `AGENTS.md` 中新增后台 RBAC 相关说明,强调 `php artisan lottery:admin-auth-sync --audit` 的使用。 - 更新 `README.md`,明确本地重置演示数据的命令,并补充 `AdminAuthorizationRegistry` 的同步要求。 - 精简 `AdminDashboardAnalyticsBuilder` 中的权限检查逻辑,确保与 `AdminAuthorizationRegistry` 一致。 - 在 `admin-rbac.md` 中添加仪表盘 API 权限要求的详细信息,优化维护命令的描述。
79 lines
2.5 KiB
PHP
79 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Admin;
|
|
|
|
use App\Models\AdminUser;
|
|
|
|
/**
|
|
* 仪表盘可筛选分析数据:区间汇总、日趋势、玩法拆解。
|
|
*/
|
|
final class AdminDashboardAnalyticsBuilder
|
|
{
|
|
public function __construct(
|
|
private readonly AdminReportQueryService $reportQuery,
|
|
) {}
|
|
|
|
/**
|
|
* @param array{
|
|
* period?: string,
|
|
* date_from?: string|null,
|
|
* date_to?: string|null,
|
|
* metric?: string,
|
|
* play_code?: string|null
|
|
* } $filters
|
|
*
|
|
* @return array<string, mixed>|null
|
|
*/
|
|
public function build(AdminUser $admin, array $filters): ?array
|
|
{
|
|
if (! $this->canView($admin)) {
|
|
return null;
|
|
}
|
|
|
|
$period = (string) ($filters['period'] ?? 'last_7_days');
|
|
$metric = (string) ($filters['metric'] ?? 'overview');
|
|
$playCode = isset($filters['play_code']) && $filters['play_code'] !== ''
|
|
? (string) $filters['play_code']
|
|
: null;
|
|
|
|
$range = $this->reportQuery->resolveDashboardPeriod(
|
|
$period,
|
|
isset($filters['date_from']) ? (string) $filters['date_from'] : null,
|
|
isset($filters['date_to']) ? (string) $filters['date_to'] : null,
|
|
);
|
|
|
|
$dateFrom = $range['date_from'];
|
|
$dateTo = $range['date_to'];
|
|
|
|
$trend = $this->reportQuery->dailyProfitSeriesFilled($dateFrom, $dateTo);
|
|
|
|
return [
|
|
'period' => $period,
|
|
'metric' => $metric,
|
|
'play_code' => $playCode,
|
|
'date_from' => $dateFrom,
|
|
'date_to' => $dateTo,
|
|
'currency_code' => $this->reportQuery->resolvePeriodCurrencyCode($dateFrom, $dateTo),
|
|
'summary' => $this->reportQuery->periodFinanceTotals($dateFrom, $dateTo),
|
|
'daily_series' => $trend['series'],
|
|
'chart_meta' => [
|
|
'chart_date_from' => $trend['chart_date_from'],
|
|
'chart_date_to' => $trend['chart_date_to'],
|
|
'truncated' => $trend['truncated'],
|
|
'span_days' => $trend['span_days'],
|
|
],
|
|
'play_breakdown' => $this->reportQuery->playDimensionBreakdownRows(
|
|
$dateFrom,
|
|
$dateTo,
|
|
$playCode,
|
|
),
|
|
];
|
|
}
|
|
|
|
/** 与 {@see AdminAuthorizationRegistry} 中 dashboard 类 API 资源的 `dashboard.view` 绑定一致。 */
|
|
private function canView(AdminUser $admin): bool
|
|
{
|
|
return $admin->hasAdminPermission('prd.dashboard.view');
|
|
}
|
|
}
|