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

113 lines
3.5 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
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\Common\StatementType;
  5. use App\Models\Guide;
  6. use App\Models\SystemSetting;
  7. use App\Models\Withdrawal;
  8. use App\Service\WithdrawalService;
  9. use App\Traits\DemandTraits;
  10. use App\Traits\StatementTraits;
  11. use App\Traits\WithdrawalTraits;
  12. use Dcat\Admin\Admin;
  13. use Dcat\Admin\Form;
  14. use Dcat\Admin\Http\Controllers\AdminController;
  15. use Illuminate\Support\Facades\DB;
  16. class WithdrawalAlipayController extends AdminController
  17. {
  18. /**
  19. * Make a form builder.
  20. *
  21. * @return Form
  22. */
  23. protected function form()
  24. {
  25. return Form::make(new WithdrawalAlipay(), function (Form $form) {
  26. $auto = Withdrawal::query()
  27. ->with('pay')
  28. ->where([
  29. 'user_id' => Admin::user()->id,
  30. 'user_type' => DemandTraits::$col[2],
  31. 'status' => WithdrawalTraits::$state[3],
  32. 'pay_type' => WithdrawalTraits::$userType[0],
  33. ])
  34. ->orderByDesc('updated_at')
  35. ->first();
  36. $form->display('id');
  37. $min = SystemSetting::val('withdrawal', WithdrawalTraits::$adminType[3]) ?? 0;
  38. $form->decimal('price','提现金额')->rules('required|numeric|min:'.$min.'|not_in:0',[
  39. '*' => '提现金额为必填字段且必须大于'.$min,
  40. ])
  41. ->default($auto->price ?? 0)
  42. ->help('含手续费' . SystemSetting::val('withdrawal', WithdrawalTraits::$adminType[2]) . ' %,最小提现金额'.$min);
  43. $form->text('account')->required()->maxLength(50)->default($auto->pay->account ?? '');
  44. $form->text('name')->required()->maxLength(50)->default($auto->pay->name ?? '');
  45. $form->image('qrcode');
  46. $form->hidden('withdrawal_id');
  47. $form->saving(function (Form $form) {
  48. DB::beginTransaction();
  49. try {
  50. $user = Guide::query()->where('id', Admin::user()->id)->lockForUpdate()->first();
  51. //手续费
  52. $cutPrice = bcmul($form->price, SystemSetting::val('withdrawal', WithdrawalTraits::$adminType[2]), 6);
  53. $cutPrice = $cutPrice > 0 ? bcdiv($cutPrice, 100, 6) : 0;
  54. //总花费
  55. $total = bcadd($form->price, $cutPrice,6);
  56. if ($total > $user->balance) {
  57. return $form->response()->error('余额不足,本次提现需花费'.bcadd($total,0,2).'(含手续费),当前可用余额为' . $user->balance);
  58. }
  59. $user->balance = bcsub($user->balance, $total, 6);
  60. $user->save();
  61. $withdrawal = new Withdrawal();
  62. $withdrawal->user_id = Admin::user()->id;
  63. $withdrawal->user_type = DemandTraits::$col[2];
  64. $withdrawal->price = request('price', 0);
  65. $withdrawal->cut_price = $cutPrice;
  66. //$withdrawal->pay_id = $form->getKey();
  67. $withdrawal->pay_type = WithdrawalTraits::$userType[0];
  68. $withdrawal->save();
  69. //提现扣钱流水
  70. $service = new WithdrawalService();
  71. $service->create(
  72. bcmul($form->price, -1, 6),
  73. StatementType::WITHDRAWAL,
  74. Admin::user()->id,
  75. DemandTraits::$col[2],
  76. $withdrawal->id,
  77. StatementTraits::$type[1]
  78. );
  79. //提现手续费流水
  80. $service->create(
  81. bcmul($cutPrice, -1, 6),
  82. StatementType::WITHDRAWAL_CAT,
  83. Admin::user()->id,
  84. DemandTraits::$col[2],
  85. $withdrawal->id,
  86. StatementTraits::$type[1]
  87. );
  88. $form->withdrawal_id = $withdrawal->id;
  89. $form->deleteInput('price');
  90. DB::commit();
  91. } catch (\Exception $e) {
  92. DB::rollBack();
  93. return $form->response()->error($e->getMessage());
  94. }
  95. });
  96. $form->saved(function (Form $form) {
  97. $withdrawal = Withdrawal::find($form->withdrawal_id);
  98. $withdrawal->pay_id = $form->getKey();
  99. $withdrawal->save();
  100. });
  101. });
  102. }
  103. }