|
|
@@ -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('请说明此次操作的原因(可选)');
|
|
|
+ }
|
|
|
+}
|