|
@@ -0,0 +1,111 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+
|
|
|
|
|
+namespace App\Module\GameItems\AdminControllers\Metrics;
|
|
|
|
|
+
|
|
|
|
|
+use UCore\DcatAdmin\Metrics\Examples\Ranking;
|
|
|
|
|
+use Illuminate\Http\Request;
|
|
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
|
|
+use App\Module\GameItems\Models\ItemUser;
|
|
|
|
|
+use App\Module\User\Models\User;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 材料持有排名图表
|
|
|
|
|
+ * 显示三大材料(木材/石材/钢材)的用户持有排名
|
|
|
|
|
+ */
|
|
|
|
|
+class MaterialRanking extends Ranking
|
|
|
|
|
+{
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 材料ID映射
|
|
|
|
|
+ */
|
|
|
|
|
+ protected $materialIds = [
|
|
|
|
|
+ 'wood' => 33, // 木材
|
|
|
|
|
+ 'stone' => 34, // 石材
|
|
|
|
|
+ 'steel' => 35, // 钢材
|
|
|
|
|
+ ];
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 材料名称映射
|
|
|
|
|
+ */
|
|
|
|
|
+ protected $materialNames = [
|
|
|
|
|
+ 'wood' => '木材',
|
|
|
|
|
+ 'stone' => '石材',
|
|
|
|
|
+ 'steel' => '钢材',
|
|
|
|
|
+ ];
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 初始化卡片内容
|
|
|
|
|
+ */
|
|
|
|
|
+ protected function init()
|
|
|
|
|
+ {
|
|
|
|
|
+ parent::init();
|
|
|
|
|
+
|
|
|
|
|
+ $this->title('材料持有排名');
|
|
|
|
|
+ $this->dropdown([
|
|
|
|
|
+ 'wood' => '木材排行榜',
|
|
|
|
|
+ 'stone' => '石材排行榜',
|
|
|
|
|
+ 'steel' => '钢材排行榜',
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 处理请求
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param Request $request
|
|
|
|
|
+ * @return mixed|void
|
|
|
|
|
+ */
|
|
|
|
|
+ public function handle(Request $request)
|
|
|
|
|
+ {
|
|
|
|
|
+ $materialType = $request->get('option', 'wood');
|
|
|
|
|
+ $data = $this->getMaterialRankingData($materialType);
|
|
|
|
|
+
|
|
|
|
|
+ // 卡片内容
|
|
|
|
|
+ $this->withContent($data);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取材料排名数据
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param string $materialType
|
|
|
|
|
+ * @return array
|
|
|
|
|
+ */
|
|
|
|
|
+ protected function getMaterialRankingData($materialType)
|
|
|
|
|
+ {
|
|
|
|
|
+ $itemId = $this->materialIds[$materialType] ?? $this->materialIds['wood'];
|
|
|
|
|
+ $materialName = $this->materialNames[$materialType] ?? $this->materialNames['wood'];
|
|
|
|
|
+
|
|
|
|
|
+ // 查询用户材料持有量排名(前20名)
|
|
|
|
|
+ $rankings = ItemUser::with('user')
|
|
|
|
|
+ ->select([
|
|
|
|
|
+ 'user_id',
|
|
|
|
|
+ DB::raw('SUM(quantity) as total_quantity')
|
|
|
|
|
+ ])
|
|
|
|
|
+ ->where('item_id', $itemId)
|
|
|
|
|
+ ->where('is_frozen', 0) // 排除冻结的物品
|
|
|
|
|
+ ->whereNull('expire_at') // 排除已过期的物品
|
|
|
|
|
+ ->groupBy('user_id')
|
|
|
|
|
+ ->orderByDesc('total_quantity')
|
|
|
|
|
+ ->limit(20)
|
|
|
|
|
+ ->get();
|
|
|
|
|
+
|
|
|
|
|
+ $result = [];
|
|
|
|
|
+ foreach ($rankings as $index => $ranking) {
|
|
|
|
|
+ $username = $ranking->user ? $ranking->user->username : null;
|
|
|
|
|
+ $result[] = [
|
|
|
|
|
+ 'rank' => $index + 1,
|
|
|
|
|
+ 'title' => $username ?: "用户{$ranking->user_id}",
|
|
|
|
|
+ 'number' => number_format($ranking->total_quantity) . ' 个',
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 如果没有数据,返回空排名提示
|
|
|
|
|
+ if (empty($result)) {
|
|
|
|
|
+ $result[] = [
|
|
|
|
|
+ 'rank' => '-',
|
|
|
|
|
+ 'title' => '暂无' . $materialName . '持有数据',
|
|
|
|
|
+ 'number' => '0 个',
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return $result;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|