InjectItemForm.php 3.0 KB

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