ItemQuantityForm.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace App\Module\GameItems\AdminControllers\Actions;
  3. use App\Module\GameItems\Logics\Item;
  4. use App\Module\GameItems\Models\ItemUser;
  5. use Dcat\Admin\Contracts\LazyRenderable;
  6. use Dcat\Admin\Traits\LazyWidget;
  7. use Dcat\Admin\Widgets\Form;
  8. use Illuminate\Support\Facades\DB;
  9. /**
  10. * 物品数量操作表单
  11. *
  12. * 用于快捷调整用户物品数量
  13. */
  14. class ItemQuantityForm extends Form implements LazyRenderable
  15. {
  16. use LazyWidget;
  17. /**
  18. * 处理表单提交
  19. *
  20. * @param array $input
  21. * @return \Dcat\Admin\Http\JsonResponse
  22. */
  23. public function handle(array $input)
  24. {
  25. try {
  26. $id = $this->payload['id'];
  27. $operationType = $input['operation_type'] ?? '';
  28. $quantity = (int)($input['quantity'] ?? 0);
  29. $reason = $input['reason'] ?? '';
  30. if ($quantity <= 0) {
  31. return $this->response()->error('数量必须大于0');
  32. }
  33. // 获取用户物品记录
  34. $userItem = ItemUser::find($id);
  35. if (!$userItem) {
  36. return $this->response()->error('用户物品记录不存在');
  37. }
  38. $adminUser = \Dcat\Admin\Admin::user();
  39. // 在事务中执行操作
  40. return DB::transaction(function () use ($userItem, $operationType, $quantity, $reason, $adminUser) {
  41. // 根据操作类型处理
  42. if ($operationType === 'add') {
  43. // 增加物品数量
  44. Item::addNormalItem($userItem->user_id, $userItem->item_id, $quantity, [
  45. 'source_type' => 'admin_operation',
  46. 'source_id' => $adminUser->id,
  47. 'details' => [
  48. 'operation' => 'add',
  49. 'reason' => $reason,
  50. 'admin_user_id' => $adminUser->id,
  51. 'admin_user_name' => $adminUser->name
  52. ]
  53. ]);
  54. $message = "成功增加 {$quantity} 个物品";
  55. } elseif ($operationType === 'reduce') {
  56. // 减少物品数量
  57. if ($userItem->quantity < $quantity) {
  58. return $this->response()->error("当前数量不足,最多只能减少 {$userItem->quantity} 个");
  59. }
  60. Item::consumeNormalItem($userItem->user_id, $userItem->item_id, $quantity, [
  61. 'source_type' => 'admin_operation',
  62. 'source_id' => $adminUser->id,
  63. 'details' => [
  64. 'operation' => 'reduce',
  65. 'reason' => $reason,
  66. 'admin_user_id' => $adminUser->id,
  67. 'admin_user_name' => $adminUser->name
  68. ]
  69. ]);
  70. $message = "成功减少 {$quantity} 个物品";
  71. } else {
  72. return $this->response()->error('无效的操作类型');
  73. }
  74. return $this->response()
  75. ->success($message)
  76. ->refresh();
  77. });
  78. } catch (\Exception $e) {
  79. return $this->response()->error('操作失败:' . $e->getMessage());
  80. }
  81. }
  82. /**
  83. * 构建表单
  84. */
  85. public function form()
  86. {
  87. $this->display('item_name', '物品名称')->value($this->payload['item_name']);
  88. $this->display('current_quantity', '当前数量')->value($this->payload['current_quantity']);
  89. $this->radio('operation_type', '操作类型')
  90. ->options([
  91. 'add' => '增加数量',
  92. 'reduce' => '减少数量'
  93. ])
  94. ->default('add')
  95. ->required();
  96. $this->number('quantity', '数量')
  97. ->min(1)
  98. ->default(1)
  99. ->required()
  100. ->help('请输入要增加或减少的数量');
  101. $this->textarea('reason', '操作原因')
  102. ->rows(3)
  103. ->help('请说明此次操作的原因(可选)');
  104. }
  105. }