ItemQuantityForm.php 3.8 KB

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