PointDashboardController.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. namespace App\Module\Point\AdminControllers;
  3. use App\Module\Point\Repositorys\PointRepository;
  4. use App\Module\Point\Repositorys\PointLogRepository;
  5. use App\Module\Point\Repositorys\PointAdminRepository;
  6. use App\Module\Point\Repositorys\PointCirculationRepository;
  7. use App\Module\Point\Repositorys\PointTransferRepository;
  8. use App\Module\Point\Repositorys\PointOrderRepository;
  9. use Dcat\Admin\Layout\Content;
  10. use Dcat\Admin\Widgets\Card;
  11. use Spatie\RouteAttributes\Attributes\Get;
  12. use UCore\DcatAdmin\AdminController;
  13. /**
  14. * 种植点数统计仪表板控制器
  15. *
  16. * 路由: /admin/point/dashboard
  17. * 菜单: 积分管理 -> 统计仪表板
  18. * 功能: 展示种植点数系统的统计数据和分析图表
  19. */
  20. class PointDashboardController extends AdminController
  21. {
  22. /**
  23. * 页面标题
  24. */
  25. protected $title = '种植点数统计仪表板';
  26. /**
  27. * 仪表板页面
  28. */
  29. #[Get('point/dashboard', name: 'admin.point.dashboard.index')]
  30. public function index(Content $content)
  31. {
  32. return $content
  33. ->title($this->title)
  34. ->description('积分系统数据统计')
  35. ->body($this->overview())
  36. ->body($this->pointTypeStats())
  37. ->body($this->recentActivity());
  38. }
  39. /**
  40. * 概览统计
  41. */
  42. protected function overview()
  43. {
  44. $pointRepo = new PointRepository();
  45. $logRepo = new PointLogRepository();
  46. $adminRepo = new PointAdminRepository();
  47. $circulationRepo = new PointCirculationRepository();
  48. $transferRepo = new PointTransferRepository();
  49. $orderRepo = new PointOrderRepository();
  50. // 基础统计
  51. $totalAccounts = $pointRepo->model()->count();
  52. $totalLogs = $logRepo->model()->count();
  53. $totalAdminOps = $adminRepo->model()->count();
  54. $totalCirculations = $circulationRepo->model()->count();
  55. $totalTransfers = $transferRepo->model()->count();
  56. $totalOrders = $orderRepo->model()->count();
  57. // 今日统计
  58. $todayStart = strtotime('today');
  59. $todayLogs = $logRepo->model()->where('create_time', '>=', $todayStart)->count();
  60. $todayAdminOps = $adminRepo->model()->where('create_time', '>=', $todayStart)->count();
  61. $content = '
  62. <div class="row">
  63. <div class="col-lg-3 col-6">
  64. <div class="small-box bg-info">
  65. <div class="inner">
  66. <h3>' . $totalAccounts . '</h3>
  67. <p>积分账户总数</p>
  68. </div>
  69. <div class="icon">
  70. <i class="fa fa-users"></i>
  71. </div>
  72. <a href="' . route('dcat.admin.point.index') . '" class="small-box-footer">
  73. 查看详情 <i class="fa fa-arrow-circle-right"></i>
  74. </a>
  75. </div>
  76. </div>
  77. <div class="col-lg-3 col-6">
  78. <div class="small-box bg-success">
  79. <div class="inner">
  80. <h3>' . $totalLogs . '</h3>
  81. <p>积分日志总数</p>
  82. </div>
  83. <div class="icon">
  84. <i class="fa fa-file-text-o"></i>
  85. </div>
  86. <a href="' . route('dcat.admin.point-log.index') . '" class="small-box-footer">
  87. 查看详情 <i class="fa fa-arrow-circle-right"></i>
  88. </a>
  89. </div>
  90. </div>
  91. <div class="col-lg-3 col-6">
  92. <div class="small-box bg-warning">
  93. <div class="inner">
  94. <h3>' . $totalAdminOps . '</h3>
  95. <p>管理员操作</p>
  96. </div>
  97. <div class="icon">
  98. <i class="fa fa-cog"></i>
  99. </div>
  100. <a href="' . route('dcat.admin.point-admin.index') . '" class="small-box-footer">
  101. 查看详情 <i class="fa fa-arrow-circle-right"></i>
  102. </a>
  103. </div>
  104. </div>
  105. <div class="col-lg-3 col-6">
  106. <div class="small-box bg-danger">
  107. <div class="inner">
  108. <h3>' . $todayLogs . '</h3>
  109. <p>今日日志</p>
  110. </div>
  111. <div class="icon">
  112. <i class="fa fa-calendar"></i>
  113. </div>
  114. <a href="' . route('dcat.admin.point-log.index') . '" class="small-box-footer">
  115. 查看详情 <i class="fa fa-arrow-circle-right"></i>
  116. </a>
  117. </div>
  118. </div>
  119. </div>
  120. <div class="row">
  121. <div class="col-lg-4 col-6">
  122. <div class="small-box bg-primary">
  123. <div class="inner">
  124. <h3>' . $totalCirculations . '</h3>
  125. <p>点数流转总数</p>
  126. </div>
  127. <div class="icon">
  128. <i class="fa fa-exchange"></i>
  129. </div>
  130. <a href="' . route('dcat.admin.point-circulation.index') . '" class="small-box-footer">
  131. 查看详情 <i class="fa fa-arrow-circle-right"></i>
  132. </a>
  133. </div>
  134. </div>
  135. <div class="col-lg-4 col-6">
  136. <div class="small-box bg-secondary">
  137. <div class="inner">
  138. <h3>' . $totalTransfers . '</h3>
  139. <p>积分转账总数</p>
  140. </div>
  141. <div class="icon">
  142. <i class="fa fa-send"></i>
  143. </div>
  144. <a href="' . route('dcat.admin.point-transfer.index') . '" class="small-box-footer">
  145. 查看详情 <i class="fa fa-arrow-circle-right"></i>
  146. </a>
  147. </div>
  148. </div>
  149. <div class="col-lg-4 col-6">
  150. <div class="small-box bg-dark">
  151. <div class="inner">
  152. <h3>' . $totalOrders . '</h3>
  153. <p>积分订单总数</p>
  154. </div>
  155. <div class="icon">
  156. <i class="fa fa-shopping-cart"></i>
  157. </div>
  158. <a href="' . route('dcat.admin.point-order.index') . '" class="small-box-footer">
  159. 查看详情 <i class="fa fa-arrow-circle-right"></i>
  160. </a>
  161. </div>
  162. </div>
  163. </div>';
  164. return new Card('Point模块统计仪表板', $content);
  165. }
  166. /**
  167. * 积分类型统计
  168. */
  169. protected function pointTypeStats()
  170. {
  171. $pointRepo = new PointRepository();
  172. $stats = $pointRepo->getPointTypeStats();
  173. $pointTypes = [
  174. 1 => '经验积分',
  175. 2 => '成就积分',
  176. 3 => '活动积分',
  177. 4 => '签到积分',
  178. 5 => '推荐积分',
  179. ];
  180. $html = '<div class="row">';
  181. foreach ($stats as $stat) {
  182. $pointId = $stat['point_id'];
  183. $typeName = $pointTypes[$pointId] ?? "积分{$pointId}";
  184. $userCount = $stat['user_count'];
  185. $totalBalance = number_format($stat['total_balance']);
  186. $avgBalance = number_format($stat['avg_balance'], 2);
  187. $html .= "
  188. <div class='col-md-4'>
  189. <div class='info-box'>
  190. <span class='info-box-icon bg-blue'><i class='fa fa-star'></i></span>
  191. <div class='info-box-content'>
  192. <span class='info-box-text'>{$typeName}</span>
  193. <span class='info-box-number'>{$userCount} 用户</span>
  194. <div class='progress'>
  195. <div class='progress-bar' style='width: 70%'></div>
  196. </div>
  197. <span class='progress-description'>
  198. 总积分: {$totalBalance} | 平均: {$avgBalance}
  199. </span>
  200. </div>
  201. </div>
  202. </div>
  203. ";
  204. }
  205. $html .= '</div>';
  206. return new Card('积分类型统计', $html);
  207. }
  208. /**
  209. * 最近活动
  210. */
  211. protected function recentActivity()
  212. {
  213. $logRepo = new PointLogRepository();
  214. $recentLogs = $logRepo->model()
  215. ->orderBy('create_time', 'desc')
  216. ->limit(10)
  217. ->get();
  218. $pointTypes = [
  219. 1 => '经验积分',
  220. 2 => '成就积分',
  221. 3 => '活动积分',
  222. 4 => '签到积分',
  223. 5 => '推荐积分',
  224. ];
  225. $html = '<div class="table-responsive"><table class="table table-striped">';
  226. $html .= '<thead><tr><th>时间</th><th>用户</th><th>积分类型</th><th>变更</th><th>备注</th></tr></thead>';
  227. $html .= '<tbody>';
  228. foreach ($recentLogs as $log) {
  229. $time = date('m-d H:i', $log->create_time);
  230. $pointType = $pointTypes[$log->point_id->value] ?? "积分{$log->point_id->value}";
  231. $amount = $log->amount > 0 ? "+{$log->amount}" : $log->amount;
  232. $amountClass = $log->amount > 0 ? 'text-success' : 'text-danger';
  233. $remark = mb_substr($log->remark, 0, 20) . (mb_strlen($log->remark) > 20 ? '...' : '');
  234. $html .= "
  235. <tr>
  236. <td>{$time}</td>
  237. <td>用户{$log->user_id}</td>
  238. <td>{$pointType}</td>
  239. <td class='{$amountClass}'>{$amount}</td>
  240. <td>{$remark}</td>
  241. </tr>
  242. ";
  243. }
  244. $html .= '</tbody></table></div>';
  245. return new Card('最近活动', $html);
  246. }
  247. }