海南旅游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.

75 lines
2.3 KiB

4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\AdminGuide\Controllers;
  3. use App\AdminGuide\Repositories\WithdrawalAlipay;
  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. use Illuminate\Support\Facades\DB;
  15. class WithdrawalAlipayController extends AdminController
  16. {
  17. /**
  18. * Make a form builder.
  19. *
  20. * @return Form
  21. */
  22. protected function form()
  23. {
  24. return Form::make(new WithdrawalAlipay(), function (Form $form) {
  25. $auto = Withdrawal::query()
  26. ->with('pay')
  27. ->where([
  28. 'user_id' => Admin::user()->id,
  29. 'user_type' => DemandTraits::$col[2],
  30. 'status' => WithdrawalTraits::$state[3],
  31. 'pay_type' => WithdrawalTraits::$userType[0],
  32. ])
  33. ->orderByDesc('updated_at')
  34. ->first();
  35. $form->display('id');
  36. $form->decimal('price','提现金额')->required()->type('number')->attribute('min', 0)->default($auto->price ?? 0);
  37. $form->text('account')->required()->maxLength(50)->default($auto->pay->account ?? '');
  38. $form->text('name')->required()->maxLength(50)->default($auto->pay->name ?? '');
  39. $form->image('qrcode');
  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[2];
  51. $withdrawal->price = request('price',0);
  52. //$withdrawal->pay_id = $form->getKey();
  53. $withdrawal->pay_type = WithdrawalTraits::$userType[0];
  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. }