InjectItemForm.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace App\Module\Mex\Forms;
  3. use App\Module\Mex\Services\MexAdminService;
  4. use Dcat\Admin\Admin;
  5. use Dcat\Admin\Widgets\Form;
  6. use Illuminate\Support\Facades\DB;
  7. /**
  8. * Mex模块物品注入表单
  9. */
  10. class InjectItemForm extends Form
  11. {
  12. /**
  13. * 处理表单提交请求
  14. */
  15. public function handle(array $input)
  16. {
  17. // 验证输入数据
  18. if (empty($input['item_id']) || $input['item_id'] <= 0) {
  19. return $this->response()->error('商品ID不能为空且必须大于0');
  20. }
  21. if (empty($input['quantity']) || $input['quantity'] <= 0) {
  22. return $this->response()->error('注入数量不能为空且必须大于0');
  23. }
  24. if (!isset($input['price']) || $input['price'] < 0) {
  25. return $this->response()->error('注入价格不能为空且不能为负数');
  26. }
  27. try {
  28. DB::beginTransaction();
  29. $result = MexAdminService::injectItem(
  30. adminUserId: Admin::user()->id,
  31. itemId: (int)$input['item_id'],
  32. quantity: (int)$input['quantity'],
  33. price: (float)$input['price'],
  34. remark: $input['remark'] ?? '后台管理员注入'
  35. );
  36. DB::commit();
  37. if ($result['success']) {
  38. return $this->response()
  39. ->success('注入成功!操作ID: ' . $result['operation_id'] . ', 成交ID: ' . $result['transaction_id'])
  40. ->refresh(); // 刷新当前页面
  41. } else {
  42. return $this->response()->error('注入失败: ' . $result['message']);
  43. }
  44. } catch (\Exception $e) {
  45. DB::rollBack();
  46. return $this->response()->error('注入失败: ' . $e->getMessage());
  47. }
  48. }
  49. /**
  50. * 构建表单
  51. */
  52. public function form()
  53. {
  54. $this->number('item_id', '商品ID')
  55. ->required()
  56. ->min(1)
  57. ->help('请输入要注入的商品ID');
  58. $this->number('quantity', '注入数量')
  59. ->required()
  60. ->min(1)
  61. ->help('请输入要注入的数量');
  62. $this->decimal('price', '注入价格')
  63. ->required()
  64. ->help('请输入注入价格(每个商品的价格)');
  65. $this->textarea('remark', '操作备注')
  66. ->rows(3)
  67. ->help('可选,记录本次操作的原因或说明');
  68. $this->html('<div class="alert alert-warning">
  69. <h6><i class="fa fa-exclamation-triangle"></i> 注意事项</h6>
  70. <ul class="mb-0">
  71. <li>注入操作相当于系统向仓库"卖出"物品</li>
  72. <li>会增加仓库的库存数量</li>
  73. <li>系统会获得相应的资金收入</li>
  74. <li>操作不可撤销,请谨慎操作</li>
  75. </ul>
  76. </div>');
  77. }
  78. /**
  79. * 返回表单默认数据
  80. */
  81. public function default()
  82. {
  83. return [
  84. 'item_id' => '',
  85. 'quantity' => '',
  86. 'price' => '',
  87. 'remark' => '',
  88. ];
  89. }
  90. }