| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
- namespace App\Module\Mex\Forms;
- use App\Module\Mex\Services\MexAdminService;
- use Dcat\Admin\Admin;
- use Dcat\Admin\Widgets\Form;
- /**
- * Mex模块物品注入表单
- */
- class InjectItemForm extends Form
- {
- /**
- * 处理表单提交请求
- */
- public function handle(array $input)
- {
- // 验证输入数据
- if (empty($input['item_id']) || $input['item_id'] <= 0) {
- return $this->response()->error('商品ID不能为空且必须大于0');
- }
-
- if (empty($input['quantity']) || $input['quantity'] <= 0) {
- return $this->response()->error('注入数量不能为空且必须大于0');
- }
-
- if (!isset($input['price']) || $input['price'] < 0) {
- return $this->response()->error('注入价格不能为空且不能为负数');
- }
- try {
- $result = MexAdminService::injectItem(
- adminUserId: Admin::user()->id,
- itemId: (int)$input['item_id'],
- quantity: (int)$input['quantity'],
- price: (float)$input['price'],
- remark: $input['remark'] ?? '后台管理员注入'
- );
- if ($result['success']) {
- return $this->response()
- ->success('注入成功!操作ID: ' . $result['operation_id'] . ', 成交ID: ' . $result['transaction_id'])
- ->refresh(); // 刷新当前页面
- } else {
- return $this->response()->error('注入失败: ' . $result['message']);
- }
- } catch (\Exception $e) {
- return $this->response()->error('注入失败: ' . $e->getMessage());
- }
- }
- /**
- * 构建表单
- */
- public function form()
- {
- $this->number('item_id', '商品ID')
- ->required()
- ->min(1)
- ->help('请输入要注入的商品ID');
-
- $this->number('quantity', '注入数量')
- ->required()
- ->min(1)
- ->help('请输入要注入的数量');
-
- $this->decimal('price', '注入价格')
- ->required()
- ->help('请输入注入价格(每个商品的价格)');
-
- $this->textarea('remark', '操作备注')
- ->rows(3)
- ->help('可选,记录本次操作的原因或说明');
- $this->html('<div class="alert alert-warning">
- <h6><i class="fa fa-exclamation-triangle"></i> 注意事项</h6>
- <ul class="mb-0">
- <li>注入操作相当于系统向仓库"卖出"物品</li>
- <li>会增加仓库的库存数量</li>
- <li>系统会获得相应的资金收入</li>
- <li>操作不可撤销,请谨慎操作</li>
- </ul>
- </div>');
- }
- /**
- * 返回表单默认数据
- */
- public function default()
- {
- return [
- 'item_id' => '',
- 'quantity' => '',
- 'price' => '',
- 'remark' => '',
- ];
- }
- }
|