海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\AdminSupplier\Controllers;
  3. use App\AdminSupplier\Repositories\WithdrawalBank;
  4. use App\Models\Agent;
  5. use App\Models\Withdrawal;
  6. use App\Traits\DemandTraits;
  7. use App\Traits\WithdrawalTraits;
  8. use Dcat\Admin\Admin;
  9. use Dcat\Admin\Form;
  10. use Dcat\Admin\Grid;
  11. use Dcat\Admin\Show;
  12. use Dcat\Admin\Http\Controllers\AdminController;
  13. use Illuminate\Database\Eloquent\Model;
  14. class WithdrawalBankController extends AdminController
  15. {
  16. /**
  17. * Make a form builder.
  18. *
  19. * @return Form
  20. */
  21. protected function form()
  22. {
  23. return Form::make(new WithdrawalBank(), function (Form $form) {
  24. $auto = Withdrawal::query()
  25. ->with('pay')
  26. ->where([
  27. 'user_id' => Admin::user()->id,
  28. 'user_type' => DemandTraits::$col[1],
  29. 'status' => WithdrawalTraits::$state[3],
  30. 'pay_type' => WithdrawalTraits::$userType[1],
  31. ])
  32. ->orderByDesc('updated_at')
  33. ->first();
  34. $form->display('id');
  35. $form->decimal('price','提现金额')->required()->maxLength(50)->default($auto->price ?? 0);
  36. $form->text('name')->required()->maxLength(50)->default($auto->pay->name ?? '');
  37. $form->text('card_number')->required()->maxLength(50)->type('number')->default($auto->pay->card_number ?? '');
  38. $form->text('account_name')->required()->maxLength(50)->default($auto->pay->account_name ?? '');
  39. $form->text('branch')->required()->maxLength(100)->default($auto->pay->branch ?? '');
  40. $form->hidden('withdrawal_id');
  41. $form->saving(function (Form $form) {
  42. $user = Agent::query()->where('id', Admin::user()->id)->lockForUpdate()->first();
  43. if ($form->price > $user->balance) {
  44. return $form->response()->error('余额不足,当前可用余额为'.$user->balance);
  45. }
  46. $user->balance = bcsub($user->balance,$form->price,6);
  47. $user->save();
  48. $withdrawal = new Withdrawal();
  49. $withdrawal->user_id = Admin::user()->id;
  50. $withdrawal->user_type = DemandTraits::$col[1];
  51. $withdrawal->price = request('price',0);
  52. //$withdrawal->pay_id = $form->getKey();
  53. $withdrawal->pay_type = WithdrawalTraits::$userType[1];
  54. $withdrawal->save();
  55. $form->withdrawal_id = $withdrawal->id;
  56. $form->deleteInput('price');
  57. });
  58. $form->saved(function (Form $form) {
  59. $withdrawal = Withdrawal::find($form->withdrawal_id);
  60. $withdrawal->pay_id = $form->getKey();
  61. $withdrawal->save();
  62. });
  63. });
  64. }
  65. }