统计仪表板
* 功能: 展示种植点数系统的统计数据和分析图表
*/
class PointDashboardController extends AdminController
{
/**
* 页面标题
*/
protected $title = '种植点数统计仪表板';
/**
* 仪表板页面
*/
#[Get('point/dashboard', name: 'admin.point.dashboard.index')]
public function index(Content $content)
{
return $content
->title($this->title)
->description('积分系统数据统计')
->body($this->overview())
->body($this->pointTypeStats())
->body($this->recentActivity());
}
/**
* 概览统计
*/
protected function overview()
{
$pointRepo = new PointRepository();
$logRepo = new PointLogRepository();
$adminRepo = new PointAdminRepository();
$circulationRepo = new PointCirculationRepository();
$transferRepo = new PointTransferRepository();
$orderRepo = new PointOrderRepository();
// 基础统计
$totalAccounts = $pointRepo->model()->count();
$totalLogs = $logRepo->model()->count();
$totalAdminOps = $adminRepo->model()->count();
$totalCirculations = $circulationRepo->model()->count();
$totalTransfers = $transferRepo->model()->count();
$totalOrders = $orderRepo->model()->count();
// 今日统计
$todayStart = strtotime('today');
$todayLogs = $logRepo->model()->where('create_time', '>=', $todayStart)->count();
$todayAdminOps = $adminRepo->model()->where('create_time', '>=', $todayStart)->count();
$content = '
' . $totalAccounts . '
积分账户总数
' . $totalLogs . '
积分日志总数
' . $totalAdminOps . '
管理员操作
' . $totalCirculations . '
点数流转总数
' . $totalTransfers . '
积分转账总数
' . $totalOrders . '
积分订单总数
';
return new Card('Point模块统计仪表板', $content);
}
/**
* 积分类型统计
*/
protected function pointTypeStats()
{
$pointRepo = new PointRepository();
$stats = $pointRepo->getPointTypeStats();
$pointTypes = [
1 => '经验积分',
2 => '成就积分',
3 => '活动积分',
4 => '签到积分',
5 => '推荐积分',
];
$html = '';
foreach ($stats as $stat) {
$pointId = $stat['point_id'];
$typeName = $pointTypes[$pointId] ?? "积分{$pointId}";
$userCount = $stat['user_count'];
$totalBalance = number_format($stat['total_balance']);
$avgBalance = number_format($stat['avg_balance'], 2);
$html .= "
{$typeName}
{$userCount} 用户
总积分: {$totalBalance} | 平均: {$avgBalance}
";
}
$html .= '
';
return new Card('积分类型统计', $html);
}
/**
* 最近活动
*/
protected function recentActivity()
{
$logRepo = new PointLogRepository();
$recentLogs = $logRepo->model()
->orderBy('create_time', 'desc')
->limit(10)
->get();
$pointTypes = [
1 => '经验积分',
2 => '成就积分',
3 => '活动积分',
4 => '签到积分',
5 => '推荐积分',
];
$html = '';
$html .= '| 时间 | 用户 | 积分类型 | 变更 | 备注 |
';
$html .= '';
foreach ($recentLogs as $log) {
$time = date('m-d H:i', $log->create_time);
$pointType = $pointTypes[$log->point_id->value] ?? "积分{$log->point_id->value}";
$amount = $log->amount > 0 ? "+{$log->amount}" : $log->amount;
$amountClass = $log->amount > 0 ? 'text-success' : 'text-danger';
$remark = mb_substr($log->remark, 0, 20) . (mb_strlen($log->remark) > 20 ? '...' : '');
$html .= "
| {$time} |
用户{$log->user_id} |
{$pointType} |
{$amount} |
{$remark} |
";
}
$html .= '
';
return new Card('最近活动', $html);
}
}