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

<?php
namespace App\AdminGuide\Controllers;
use App\AdminGuide\Repositories\WithdrawalBank;
use App\Models\Agent;
use App\Models\Withdrawal;
use App\Traits\DemandTraits;
use App\Traits\WithdrawalTraits;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Database\Eloquent\Model;
class WithdrawalBankController extends AdminController
{
/**
* Make a form builder.
*
* @return Form
*/
protected function form()
{
return Form::make(new WithdrawalBank(), function (Form $form) {
$auto = Withdrawal::query()
->with('pay')
->where([
'user_id' => Admin::user()->id,
'user_type' => DemandTraits::$col[2],
'status' => WithdrawalTraits::$state[3],
'pay_type' => WithdrawalTraits::$userType[1],
])
->orderByDesc('updated_at')
->first();
$form->display('id');
$form->decimal('price','提现金额')->required()->maxLength(50)->default($auto->price ?? 0);
$form->text('name')->required()->maxLength(50)->default($auto->pay->name ?? '');
$form->text('card_number')->required()->maxLength(50)->type('number')->default($auto->pay->card_number ?? '');
$form->text('account_name')->required()->maxLength(50)->default($auto->pay->account_name ?? '');
$form->text('branch')->required()->maxLength(100)->default($auto->pay->branch ?? '');
$form->hidden('withdrawal_id');
$form->saving(function (Form $form) {
$user = Agent::query()->where('id', Admin::user()->id)->lockForUpdate()->first();
if ($form->price > $user->balance) {
return $form->response()->error('余额不足,当前可用余额为'.$user->balance);
}
$user->balance = bcsub($user->balance,$form->price,6);
$user->save();
$withdrawal = new Withdrawal();
$withdrawal->user_id = Admin::user()->id;
$withdrawal->user_type = DemandTraits::$col[2];
$withdrawal->price = request('price',0);
//$withdrawal->pay_id = $form->getKey();
$withdrawal->pay_type = WithdrawalTraits::$userType[1];
$withdrawal->save();
$form->withdrawal_id = $withdrawal->id;
$form->deleteInput('price');
});
$form->saved(function (Form $form) {
$withdrawal = Withdrawal::find($form->withdrawal_id);
$withdrawal->pay_id = $form->getKey();
$withdrawal->save();
});
});
}
}