Kaynağa Gözat

实现物品数量为0时不删除记录,并在后台用户物品管理中添加数量操作功能

- 修改物品消耗逻辑:当物品数量为0时保留记录而不删除
- 新增ItemQuantityAction行操作类,提供快捷的数量增减功能
- 新增ItemQuantityForm表单类,支持物品数量的增加和减少操作
- 在用户物品管理列表中集成数量操作按钮
- 添加操作日志记录,包含管理员信息和操作原因
notfff 7 ay önce
ebeveyn
işleme
7500ad281f

+ 59 - 0
app/Module/GameItems/AdminControllers/Actions/ItemQuantityAction.php

@@ -0,0 +1,59 @@
+<?php
+
+namespace App\Module\GameItems\AdminControllers\Actions;
+
+use App\Module\GameItems\AdminControllers\Actions\ItemQuantityForm;
+use Dcat\Admin\Widgets\Modal;
+use UCore\DcatAdmin\RowAction;
+
+/**
+ * 物品数量操作行动作
+ *
+ * 提供快捷的物品数量增加/减少操作
+ */
+class ItemQuantityAction extends RowAction
+{
+    /**
+     * 操作按钮标题
+     *
+     * @var string
+     */
+    protected $title = '数量操作';
+
+    /**
+     * 检查是否允许显示此操作
+     *
+     * @return bool
+     */
+    public function allowed()
+    {
+        // 只对普通物品(非单独属性物品)显示
+        $row = $this->getRow();
+        return empty($row->instance_id);
+    }
+
+    /**
+     * 渲染操作按钮
+     *
+     * @return string
+     */
+    public function render2()
+    {
+        $row = $this->getRow();
+
+        // 实例化表单类并传递自定义参数
+        $form = ItemQuantityForm::make()->payload([
+            'id' => $this->getKey(),
+            'user_id' => $row->user_id,
+            'item_id' => $row->item_id,
+            'current_quantity' => $row->quantity,
+            'item_name' => $row->item->name ?? '未知物品'
+        ]);
+
+        return Modal::make()
+            ->lg()
+            ->title($this->title())
+            ->body($form)
+            ->button('<i class="feather icon-edit-3"></i> ' . $this->title());
+    }
+}

+ 118 - 0
app/Module/GameItems/AdminControllers/Actions/ItemQuantityForm.php

@@ -0,0 +1,118 @@
+<?php
+
+namespace App\Module\GameItems\AdminControllers\Actions;
+
+use App\Module\GameItems\Logics\Item;
+use App\Module\GameItems\Models\ItemUser;
+use Dcat\Admin\Widgets\Form;
+use Illuminate\Http\Request;
+
+/**
+ * 物品数量操作表单
+ *
+ * 用于快捷调整用户物品数量
+ */
+class ItemQuantityForm extends Form
+{
+    /**
+     * 处理表单提交
+     *
+     * @param Request $request
+     * @return \Dcat\Admin\Http\JsonResponse
+     */
+    public function handle(Request $request)
+    {
+        try {
+            $payload = $this->payload();
+            $id = $payload['id'];
+            $operationType = $request->input('operation_type');
+            $quantity = (int)$request->input('quantity', 0);
+            $reason = $request->input('reason', '');
+
+            if ($quantity <= 0) {
+                return $this->response()->error('数量必须大于0');
+            }
+
+            // 获取用户物品记录
+            $userItem = ItemUser::find($id);
+            if (!$userItem) {
+                return $this->response()->error('用户物品记录不存在');
+            }
+
+            $adminUser = \Dcat\Admin\Admin::user();
+
+            // 根据操作类型处理
+            if ($operationType === 'add') {
+                // 增加物品数量
+                Item::addNormalItem($userItem->user_id, $userItem->item_id, $quantity, [
+                    'source_type' => 'admin_operation',
+                    'source_id' => $adminUser->id,
+                    'details' => [
+                        'operation' => 'add',
+                        'reason' => $reason,
+                        'admin_user_id' => $adminUser->id,
+                        'admin_user_name' => $adminUser->name
+                    ]
+                ]);
+
+                $message = "成功增加 {$quantity} 个物品";
+            } elseif ($operationType === 'reduce') {
+                // 减少物品数量
+                if ($userItem->quantity < $quantity) {
+                    return $this->response()->error("当前数量不足,最多只能减少 {$userItem->quantity} 个");
+                }
+
+                Item::consumeNormalItem($userItem->user_id, $userItem->item_id, $quantity, [
+                    'source_type' => 'admin_operation',
+                    'source_id' => $adminUser->id,
+                    'details' => [
+                        'operation' => 'reduce',
+                        'reason' => $reason,
+                        'admin_user_id' => $adminUser->id,
+                        'admin_user_name' => $adminUser->name
+                    ]
+                ]);
+
+                $message = "成功减少 {$quantity} 个物品";
+            } else {
+                return $this->response()->error('无效的操作类型');
+            }
+
+            return $this->response()
+                ->success($message)
+                ->refresh();
+
+        } catch (\Exception $e) {
+            return $this->response()->error('操作失败:' . $e->getMessage());
+        }
+    }
+
+    /**
+     * 构建表单
+     */
+    public function form()
+    {
+        $payload = $this->payload();
+
+        $this->display('item_name', '物品名称')->value($payload['item_name']);
+        $this->display('current_quantity', '当前数量')->value($payload['current_quantity']);
+
+        $this->radio('operation_type', '操作类型')
+            ->options([
+                'add' => '增加数量',
+                'reduce' => '减少数量'
+            ])
+            ->default('add')
+            ->required();
+
+        $this->number('quantity', '数量')
+            ->min(1)
+            ->default(1)
+            ->required()
+            ->help('请输入要增加或减少的数量');
+
+        $this->textarea('reason', '操作原因')
+            ->rows(3)
+            ->help('请说明此次操作的原因(可选)');
+    }
+}

+ 9 - 1
app/Module/GameItems/AdminControllers/UserItemController.php

@@ -3,6 +3,7 @@
 namespace App\Module\GameItems\AdminControllers;
 
 use App\Module\GameItems\AdminForms\AddItemForm;
+use App\Module\GameItems\AdminControllers\Actions\ItemQuantityAction;
 use App\Module\GameItems\Repositorys\ItemUserRepository;
 use App\Module\GameItems\Repositorys\ItemRepository;
 use App\Module\GameItems\Repositorys\ItemInstanceRepository;
@@ -43,9 +44,16 @@ class UserItemController extends AdminController
         return Grid::make(new ItemUserRepository(['item']), function (Grid $grid) {
             // 禁用创建、编辑、查看和删除按钮
             $grid->disableCreateButton();
-            $grid->disableActions();
+            $grid->disableEditButton();
+            $grid->disableViewButton();
+            $grid->disableDeleteButton();
             $grid->disableBatchDelete();
 
+            // 添加自定义行操作
+            $grid->actions(function (Grid\Displayers\Actions $actions) {
+                $actions->append(new ItemQuantityAction());
+            });
+
 
 
             $helper = new GridHelper($grid, $this);

+ 15 - 2
app/Module/GameItems/Logics/Item.php

@@ -350,6 +350,7 @@ class Item
                 // 当前堆叠数量不足,全部消耗
                 $consumedQuantity = $userItem->quantity;
                 $remainingQuantity -= $consumedQuantity;
+                $oldQuantity = $userItem->quantity;
 
                 // 记录交易日志
                 self::logTransaction(
@@ -366,8 +367,20 @@ class Item
                     $options['device_info'] ?? null
                 );
 
-                // 删除用户物品记录
-                $userItem->delete();
+                // 将数量设置为0,不删除记录
+                $userItem->quantity = 0;
+                $userItem->save();
+
+                // 触发物品数量变更事件
+                Event::dispatch(new ItemQuantityChanged(
+                    $userId,
+                    $itemId,
+                    null,
+                    $oldQuantity,
+                    0,
+                    $userItem->id,
+                    $options
+                ));
             } else {
                 // 当前堆叠数量足够,部分消耗
                 $consumedQuantity = $remainingQuantity;