Explorar el Código

feat(game-items): 添加宝箱内容和消耗配置的详细视图并优化物品管理

- 在 ChestContentController 中添加了查看宝箱、物品和物品组详情的快捷操作
- 在 ItemController 中添加了宝箱内容和消耗配置的详细视图
- 修复了 ChestManageAction 中的重定向 URL- 新增 SqlLogServiceProvider 用于记录 SQL 查询日志
- 在 logging 配置中添加了 sql 日志通道
Your Name hace 8 meses
padre
commit
cd4008cf19

+ 1 - 1
app/Module/GameItems/AdminControllers/Actions/ChestManageAction.php

@@ -28,7 +28,7 @@ class ChestManageAction extends RowActionHandler
     {
         $id = $this->getKey();
 
-        return $this->response()->redirect(admin_url("game-items-chest-contents?item_id={$id}"));
+        return $this->response()->redirect(admin_url("game-items-chest-contents?chest_id={$id}"));
     }
 
 

+ 43 - 0
app/Module/GameItems/AdminControllers/Actions/ViewChestDetailAction.php

@@ -0,0 +1,43 @@
+<?php
+
+namespace App\Module\GameItems\AdminControllers\Actions;
+
+use App\Module\GameItems\Models\ItemChestContent;
+use Illuminate\Http\Request;
+use UCore\DcatAdmin\RowActionHandler;
+
+/**
+ * 查看宝箱详情
+ * 
+ * 在宝箱内容管理行中添加查看宝箱详情的快捷操作
+ */
+class ViewChestDetailAction extends RowActionHandler
+{
+    /**
+     * 操作按钮标题
+     *
+     * @var string
+     */
+    public $title = '宝箱详情';
+
+    /**
+     * 处理请求
+     *
+     * @param Request $request
+     * @return mixed
+     */
+    public function handle(Request $request)
+    {
+        $id = $this->getKey();
+        $content = ItemChestContent::with('chest')->find($id);
+        
+        if (!$content || !$content->chest_id) {
+            return $this->response()->error('宝箱不存在');
+        }
+        
+        // 跳转到宝箱详情页面
+        return $this->response()->redirect(
+            admin_url("game-items/{$content->chest_id}")
+        );
+    }
+}

+ 54 - 0
app/Module/GameItems/AdminControllers/Actions/ViewGroupDetailAction.php

@@ -0,0 +1,54 @@
+<?php
+
+namespace App\Module\GameItems\AdminControllers\Actions;
+
+use App\Module\GameItems\Models\ItemChestContent;
+use Illuminate\Http\Request;
+use UCore\DcatAdmin\RowActionHandler;
+
+/**
+ * 查看物品组详情
+ * 
+ * 在宝箱内容管理行中添加查看物品组详情的快捷操作
+ */
+class ViewGroupDetailAction extends RowActionHandler
+{
+    /**
+     * 操作按钮标题
+     *
+     * @var string
+     */
+    public $title = '物品组详情';
+
+    /**
+     * 判断是否允许显示此操作
+     * 
+     * @return bool
+     */
+    public function allowed()
+    {
+        // 只有当内容是物品组而不是物品时才显示此操作
+        return !empty($this->row->group_id) && empty($this->row->item_id);
+    }
+
+    /**
+     * 处理请求
+     *
+     * @param Request $request
+     * @return mixed
+     */
+    public function handle(Request $request)
+    {
+        $id = $this->getKey();
+        $content = ItemChestContent::with('group')->find($id);
+        
+        if (!$content || !$content->group_id) {
+            return $this->response()->error('物品组不存在或此内容为单个物品');
+        }
+        
+        // 跳转到物品组详情页面
+        return $this->response()->redirect(
+            admin_url("game-items-groups/{$content->group_id}")
+        );
+    }
+}

+ 54 - 0
app/Module/GameItems/AdminControllers/Actions/ViewItemDetailAction.php

@@ -0,0 +1,54 @@
+<?php
+
+namespace App\Module\GameItems\AdminControllers\Actions;
+
+use App\Module\GameItems\Models\ItemChestContent;
+use Illuminate\Http\Request;
+use UCore\DcatAdmin\RowActionHandler;
+
+/**
+ * 查看物品详情
+ * 
+ * 在宝箱内容管理行中添加查看物品详情的快捷操作
+ */
+class ViewItemDetailAction extends RowActionHandler
+{
+    /**
+     * 操作按钮标题
+     *
+     * @var string
+     */
+    public $title = '物品详情';
+
+    /**
+     * 判断是否允许显示此操作
+     * 
+     * @return bool
+     */
+    public function allowed()
+    {
+        // 只有当内容是物品而不是物品组时才显示此操作
+        return !empty($this->row->item_id) && empty($this->row->group_id);
+    }
+
+    /**
+     * 处理请求
+     *
+     * @param Request $request
+     * @return mixed
+     */
+    public function handle(Request $request)
+    {
+        $id = $this->getKey();
+        $content = ItemChestContent::with('item')->find($id);
+        
+        if (!$content || !$content->item_id) {
+            return $this->response()->error('物品不存在或此内容为物品组');
+        }
+        
+        // 跳转到物品详情页面
+        return $this->response()->redirect(
+            admin_url("game-items/{$content->item_id}")
+        );
+    }
+}

+ 9 - 0
app/Module/GameItems/AdminControllers/ChestContentController.php

@@ -3,6 +3,9 @@
 namespace App\Module\GameItems\AdminControllers;
 
 use App\Module\GameItems\AdminControllers\Actions\DuplicateChestContentAction;
+use App\Module\GameItems\AdminControllers\Actions\ViewItemDetailAction;
+use App\Module\GameItems\AdminControllers\Actions\ViewGroupDetailAction;
+use App\Module\GameItems\AdminControllers\Actions\ViewChestDetailAction;
 use App\Module\GameItems\Repositorys\ItemChestContentRepository;
 use Dcat\Admin\Form;
 use Dcat\Admin\Grid;
@@ -55,6 +58,12 @@ class ChestContentController extends AdminController
             $grid->actions(function (Grid\Displayers\Actions $actions) {
                 // 添加复制按钮
                 $actions->append(new DuplicateChestContentAction());
+                // 添加查看宝箱详情按钮
+                $actions->append(new ViewChestDetailAction());
+                // 添加查看物品详情按钮
+                $actions->append(new ViewItemDetailAction());
+                // 添加查看物品组详情按钮
+                $actions->append(new ViewGroupDetailAction());
             });
 
             // 筛选

+ 72 - 36
app/Module/GameItems/AdminControllers/ItemController.php

@@ -5,6 +5,9 @@ namespace App\Module\GameItems\AdminControllers;
 use App\Module\Game\DCache\ItemJsonConfig;
 use App\Module\GameItems\Enums\ITEM_TYPE;
 use App\Module\GameItems\Models\ItemCategory;
+use App\Module\GameItems\Models\ItemChestOpenCost;
+use App\Module\GameItems\Repositorys\ItemChestContentRepository;
+use App\Module\GameItems\Repositorys\ItemChestOpenCostRepository;
 use App\Module\GameItems\Repositorys\ItemRepository;
 use Dcat\Admin\Form;
 use Dcat\Admin\Grid;
@@ -28,18 +31,19 @@ use App\Module\GameItems\AdminControllers\Actions\DuplicateRowAction;
 #[Resource('game-items', names: 'dcat.admin.game-items')]
 class ItemController extends AdminController
 {
+
     /**
      * 生成物品JSON数据
      */
     #[Get('game-items/generate-json')]
     public function generateJson()
     {
-        $success = ItemJsonConfig::getData([],true);
+        $success = ItemJsonConfig::getData([], true);
 
         return response()->json([
-            'status' => $success ? 'success' : 'error',
-            'message' => $success ? 'JSON生成成功' : 'JSON生成失败'
-        ]);
+                                    'status'  => $success ? 'success' : 'error',
+                                    'message' => $success ? 'JSON生成成功' : 'JSON生成失败'
+                                ]);
     }
 
 
@@ -57,7 +61,7 @@ class ItemController extends AdminController
      */
     protected function grid()
     {
-        return Grid::make(new ItemRepository(['category']), function (Grid $grid) {
+        return Grid::make(new ItemRepository([ 'category' ]), function (Grid $grid) {
             $status = \App\Module\GameItems\AdminControllers\Tools\RefreshCheckTool::checkSyncStatus();
 
             if ($status['is_synced']) {
@@ -67,9 +71,9 @@ class ItemController extends AdminController
             }
 
             $grid->tools([
-                new \App\Module\GameItems\AdminControllers\Tools\RefreshCheckTool($status['should_display']),
-                new \App\Module\GameItems\AdminControllers\Tools\SyncItemsJsonTool($status['should_display'])
-            ]);
+                             new \App\Module\GameItems\AdminControllers\Tools\RefreshCheckTool($status['should_display']),
+                             new \App\Module\GameItems\AdminControllers\Tools\SyncItemsJsonTool($status['should_display'])
+                         ]);
 
             $helper = new GridHelper($grid, $this);
             $grid->column('id', 'ID')->sortable();
@@ -98,33 +102,34 @@ class ItemController extends AdminController
             // 筛选
             $grid->filter(function ($filter) {
                 $helper = new FilterHelper($filter, $this);
-                $helper->equal('id','ID');
+                $helper->equal('id', 'ID');
 
                 $filter->like('name', '名称');
                 $filter->equal('category_id', '分类')->select(
                     ItemCategory::pluck('name', 'id')
                 );
-                $helper->equalRadioModelCats('type','类型');
+                $helper->equalRadioModelCats('type', '类型');
 
                 $filter->equal('is_unique', '单独属性')->radio([
+                                                                   1 => '是',
+                                                                   0 => '否',
+                                                               ]);
+                $filter->equal('tradable', '可交易')->radio([
+                                                                1 => '是',
+                                                                0 => '否',
+                                                            ]);
+                $filter->equal('dismantlable', '可分解')->radio([
                                                                     1 => '是',
                                                                     0 => '否',
                                                                 ]);
-                $filter->equal('tradable', '可交易')->radio([
-                                                                 1 => '是',
-                                                                 0 => '否',
-                                                             ]);
-                $filter->equal('dismantlable', '可分解')->radio([
-                                                                     1 => '是',
-                                                                     0 => '否',
-                                                                 ]);
             });
 
             return $grid;
         });
 
 
-        }
+    }
+
     /**
      * 详情页
      *
@@ -135,7 +140,7 @@ class ItemController extends AdminController
     {
         return Show::make($id, new ItemRepository(), function (Show $show) {
             $helper = new ShowHelper($show, $this);
-            $helper->field('id','ID');
+            $helper->field('id', 'ID');
             $show->field('name', '名称');
             $show->field('description', '描述');
             $show->field('category.name', '分类');
@@ -157,23 +162,54 @@ class ItemController extends AdminController
             $show->field('global_expire_at', '全局过期时间');
             $show->field('created_at', '创建时间');
             $show->field('updated_at', '更新时间');
-
+            $show->divider();
             // 如果是宝箱类型,显示宝箱内容
-            if ($show->getModel()->type == ITEM_TYPE::CHEST) {
-                $show->chestContents('宝箱内容', function ($chestContents) {
-                    $chestContents->resource('/admin/game-items-chest-contents');
-                    $chestContents->id('ID');
-                    $chestContents->item()->name('物品名称');
-                    $chestContents->group()->name('物品组名称');
-                    $chestContents->min_quantity('最小数量');
-                    $chestContents->max_quantity('最大数量');
-                    $chestContents->weight('权重');
-                    $chestContents->allow_duplicate('允许重复')->bool();
-                    $chestContents->pity_count('保底次数');
-                    $chestContents->pity_weight_factor('保底权重因子');
+
+            $show->relation('chest_contents', '宝箱内容',
+                function (\App\Module\GameItems\Models\Item $item) {
+//                dd($item);
+                    $grid = new Grid(new ItemChestContentRepository([ 'chest', 'item', 'group' ]));
+
+                    $grid->model()->where('chest_id', $item->id);
+
+                    // 设置路由
+                    $grid->setResource('game-items-chest-contents');
+
+                    $grid->id();
+                    $grid->column('chest.name', '宝箱名称');
+                    $grid->column('item.name', '物品名称');
+                    $grid->column('group.name', '物品组名称');
+                    $grid->column('min_quantity', '最小数量');
+                    $grid->column('max_quantity', '最大数量');
+                    $grid->column('weight', '权重');
+
+                    $grid->disableActions();
+
+                    return $grid;
+
                 });
-            }
 
+            $show->relation('chest_costs', '宝箱消耗',
+                function (\App\Module\GameItems\Models\Item $item) {
+//                dd($item);
+                    $grid = new Grid(new ItemChestOpenCostRepository(['cost_item']));
+
+                    $grid->model()->where('chest_id', $item->id);
+
+                    // 设置路由
+                    $grid->setResource('game-items-chest-costs');
+
+                    $grid->id();
+                    $grid->column('cost_item.name', '物品名称');
+
+
+
+
+                    $grid->disableActions();
+
+                    return $grid;
+
+                });
             return $show;
         });
 
@@ -188,14 +224,14 @@ class ItemController extends AdminController
     protected function form()
     {
         return Form::make(new ItemRepository(), function (Form $form) {
-            $helper = new \App\Module\GameItems\AdminControllers\Helper\FormHelper($form,$this);
+            $helper = new \App\Module\GameItems\AdminControllers\Helper\FormHelper($form, $this);
             $helper->text('name')->required();
 
             $form->textarea('description', '描述');
             $form->select('category_id', '分类')
                 ->options(ItemCategory::pluck('name', 'id'))
                 ->required();
-            $helper->selectOptionCast('type','类型');
+            $helper->selectOptionCast('type', '类型');
 
             $form->switch('is_unique', '单独属性')
                 ->default(false);

+ 14 - 4
app/Module/GameItems/Models/Item.php

@@ -10,7 +10,7 @@ use UCore\ModelCore;
 /**
  * 物品基础信息
  *
- * field start 
+ * field start
  * @property   int  $id  物品ID,主键
  * @property   string  $name  物品名称
  * @property   string  $description  物品描述
@@ -39,7 +39,7 @@ class Item extends ModelCore
      */
     protected $table = 'item_items';
 
-    // attrlist start 
+    // attrlist start
     protected $fillable = [
         'id',
         'name',
@@ -118,9 +118,19 @@ class Item extends ModelCore
      *
      * @return HasMany
      */
-    public function chestContents(): HasMany
+    public function chest_contents(): HasMany
     {
-        return $this->hasMany(ItemChestContent::class, 'chest_id');
+        return $this->hasMany(ItemChestContent::class, 'chest_id','item_id');
+    }
+
+    /**
+     * 宝箱开启消耗配置
+     *
+     * @return HasMany
+     */
+    public function chest_costs(): HasMany
+    {
+        return $this->hasMany(ItemChestOpenCost::class, 'chest_id','item_id');
     }
 
 }

+ 45 - 0
app/Providers/SqlLogServiceProvider.php

@@ -0,0 +1,45 @@
+<?php
+
+namespace App\Providers;
+
+use Illuminate\Database\Events\QueryExecuted;
+use Illuminate\Support\Facades\DB;
+use UCore\Helper\Logger;
+use Illuminate\Support\ServiceProvider;
+
+class SqlLogServiceProvider extends ServiceProvider
+{
+    /**
+     * 注册服务
+     */
+    public function register(): void
+    {
+        //
+    }
+
+    /**
+     * 启动服务
+     */
+    public function boot(): void
+    {
+        // 只在非生产环境下记录SQL查询
+        if (app()->environment('local', 'development', 'testing')) {
+            DB::listen(function (QueryExecuted $query) {
+                $sql = $query->sql;
+
+                // 替换绑定参数
+                foreach ($query->bindings as $binding) {
+                    $value = is_numeric($binding) ? $binding : "'{$binding}'";
+                    $sql = preg_replace('/\?/', $value, $sql, 1);
+                }
+
+                // 使用UCore\Helper\Logger记录SQL查询、执行时间和连接名称
+                Logger::debug('SQL', [
+                    'sql' => $sql,
+                    'time' => "{$query->time}ms",
+                    'connection' => $query->connectionName
+                ]);
+            });
+        }
+    }
+}

+ 3 - 0
config/app.php

@@ -205,6 +205,9 @@ return [
 
         // Farm 模块
         \App\Module\Farm\Providers\FarmServiceProvider::class,
+
+        // SQL日志服务提供者
+        App\Providers\SqlLogServiceProvider::class,
     ],
 
     /*

+ 8 - 0
config/logging.php

@@ -127,6 +127,14 @@ return [
             'path' => storage_path('logs/laravel.log'),
         ],
 
+        'sql' => [
+            'driver' => 'daily',
+            'path' => storage_path('logs/sql.log'),
+            'level' => 'debug',
+            'days' => 14,
+            'replace_placeholders' => true,
+        ],
+
     ],
 
 ];