Просмотр исходного кода

物品模块:增加图表控制器,实现三大材料持有排名功能

- 新增 app/Module/GameItems/AdminControllers/MetricsController.php 图表控制器
- 新增 app/Module/GameItems/AdminControllers/Metrics/MaterialRanking.php 材料排名图表组件
- 实现木材/石材/钢材的用户持有量排名展示(前20名)
- 支持下拉菜单切换不同材料类型的排名
- 修复 ItemUser 模型缺少 user 关系的问题
- 路由地址:/admin/game-items-metrics
AI Assistant 6 месяцев назад
Родитель
Сommit
1db7df93

+ 30 - 1
AiWork/now.md

@@ -1,5 +1,34 @@
 # 当前工作状态
-**最新更新**: 2025年06月22日 15:20
+
+物品模块,增加图表控制器,参考 app/Module/Admin/AdminControllers/MetricsController.php
+三大材料(木材/石材/钢材)的持有排名,用户排名
+
+
+## 🎉 农贸市场图表分析页面报错修复完成 (2025-06-22 15:30)
+
+### 🎯 最新完成任务
+✅ **农贸市场图表分析页面报错修复**
+- 时间:2025-06-22 15:30
+- 状态:已完成,/admin/mex-charts页面所有报错已修复
+- 问题:页面访问时出现500错误,图表数据无法正常加载
+
+### 🔧 修复内容
+1. **TransactionType枚举常量错误修复**
+   - 问题:VolumeTrendChart中使用了不存在的BUY/SELL常量
+   - 修复:改为正确的USER_BUY/USER_SELL枚举值
+   - 文件:`app/Module/Mex/Metrics/VolumeTrendChart.php`
+
+2. **trade_date字段类型转换问题修复**
+   - 问题:使用selectRaw查询时trade_date字段未正确转换为Carbon对象
+   - 修复:添加Carbon::parse()确保正确的日期对象转换
+   - 文件:`app/Module/Mex/Metrics/PriceTrendChart.php`、`VolumeTrendChart.php`
+
+### 📊 修复效果
+- **页面访问**: /admin/mex-charts页面正常加载,无500错误
+- **图表显示**: 所有农产品的价格趋势图和成交趋势图正常显示
+- **交互功能**: 时间范围切换(7天、14天、30天、90天)正常工作
+- **数据请求**: 所有/admin/dcat-api/value请求返回200状态码
+- **图表内容**: 价格趋势(最低价、最高价、收盘价)和成交趋势(买入量、卖出量)正确显示
 
 ## 🎉 农贸市场图表分析功能开发完成 (2025-06-22 15:20)
 

+ 111 - 0
app/Module/GameItems/AdminControllers/Metrics/MaterialRanking.php

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

+ 37 - 0
app/Module/GameItems/AdminControllers/MetricsController.php

@@ -0,0 +1,37 @@
+<?php
+
+namespace App\Module\GameItems\AdminControllers;
+
+use App\Module\GameItems\AdminControllers\Metrics\MaterialRanking;
+use Dcat\Admin\Layout\Column;
+use Dcat\Admin\Layout\Content;
+use Dcat\Admin\Layout\Row;
+use Spatie\RouteAttributes\Attributes\Get;
+use UCore\DcatAdmin\AdminController;
+
+/**
+ * 物品模块图表控制器
+ * 提供物品相关的数据统计和图表展示
+ */
+class MetricsController extends AdminController
+{
+    /**
+     * 物品统计图表页面
+     *
+     * @param Content $content
+     * @return Content
+     */
+    #[Get('game-items-metrics')]
+    public function index(Content $content)
+    {
+        return $content
+            ->header('物品统计图表')
+            ->description('物品模块数据统计与分析')
+            ->body(function (Row $row) {
+                $row->column(12, function (Column $column) {
+                    // 三大材料持有排名
+                    $column->row(new MaterialRanking());
+                });
+            });
+    }
+}

+ 10 - 0
app/Module/GameItems/Models/ItemUser.php

@@ -64,6 +64,16 @@ class ItemUser extends ModelCore
         'frozen_log_id' => 'integer',
     ];
 
+    /**
+     * 获取关联的用户
+     *
+     * @return BelongsTo
+     */
+    public function user(): BelongsTo
+    {
+        return $this->belongsTo(\App\Module\User\Models\User::class, 'user_id');
+    }
+
     /**
      * 获取关联的物品
      *