| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- namespace App\Module\GameItems\AdminControllers\Actions;
- use App\Module\GameItems\Logics\Item;
- use App\Module\GameItems\Models\ItemUser;
- use Dcat\Admin\Contracts\LazyRenderable;
- use Dcat\Admin\Traits\LazyWidget;
- use Dcat\Admin\Widgets\Form;
- use Illuminate\Support\Facades\DB;
- /**
- * 物品数量操作表单
- *
- * 用于快捷调整用户物品数量
- */
- class ItemQuantityForm extends Form implements LazyRenderable
- {
- use LazyWidget;
- /**
- * 处理表单提交
- *
- * @param array $input
- * @return \Dcat\Admin\Http\JsonResponse
- */
- public function handle(array $input)
- {
- try {
- $id = $this->payload['id'];
- $operationType = $input['operation_type'] ?? '';
- $quantity = (int)($input['quantity'] ?? 0);
- $reason = $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();
- // 在事务中执行操作
- return DB::transaction(function () use ($userItem, $operationType, $quantity, $reason, $adminUser) {
- // 根据操作类型处理
- 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()
- {
- $this->display('item_name', '物品名称')->value($this->payload['item_name']);
- $this->display('current_quantity', '当前数量')->value($this->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('请说明此次操作的原因(可选)');
- }
- }
|