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.
		
		
		
		
		
			
		
			
				
					
					
						
							110 lines
						
					
					
						
							3.5 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							110 lines
						
					
					
						
							3.5 KiB
						
					
					
				
								<?php
							 | 
						|
								
							 | 
						|
								namespace App\AdminAgent\Controllers;
							 | 
						|
								
							 | 
						|
								use App\AdminAgent\Repositories\WithdrawalAlipay;
							 | 
						|
								use App\Common\StatementType;
							 | 
						|
								use App\Models\Agent;
							 | 
						|
								use App\Models\SystemSetting;
							 | 
						|
								use App\Models\Withdrawal;
							 | 
						|
								use App\Service\WithdrawalService;
							 | 
						|
								use App\Traits\DemandTraits;
							 | 
						|
								use App\Traits\WithdrawalTraits;
							 | 
						|
								use Dcat\Admin\Admin;
							 | 
						|
								use Dcat\Admin\Form;
							 | 
						|
								use Dcat\Admin\Grid;
							 | 
						|
								use Dcat\Admin\Repositories\EloquentRepository;
							 | 
						|
								use Dcat\Admin\Show;
							 | 
						|
								use Dcat\Admin\Http\Controllers\AdminController;
							 | 
						|
								use Illuminate\Database\Eloquent\Model;
							 | 
						|
								use Illuminate\Support\Facades\DB;
							 | 
						|
								
							 | 
						|
								class WithdrawalAlipayController extends AdminController
							 | 
						|
								{
							 | 
						|
								
							 | 
						|
									/**
							 | 
						|
									 * Make a form builder.
							 | 
						|
									 *
							 | 
						|
									 * @return Form
							 | 
						|
									 */
							 | 
						|
									protected function form()
							 | 
						|
									{
							 | 
						|
										return Form::make(new WithdrawalAlipay(), function (Form $form) {
							 | 
						|
											$auto = Withdrawal::query()
							 | 
						|
												->with('pay')
							 | 
						|
												->where([
							 | 
						|
													'user_id' => Admin::user()->id,
							 | 
						|
													'user_type' => DemandTraits::$col[0],
							 | 
						|
													'status' => WithdrawalTraits::$state[3],
							 | 
						|
													'pay_type' => WithdrawalTraits::$userType[0],
							 | 
						|
												])
							 | 
						|
												->orderByDesc('updated_at')
							 | 
						|
												->first();
							 | 
						|
											$form->display('id');
							 | 
						|
											$min = SystemSetting::val('withdrawal', WithdrawalTraits::$adminType[3]) ?? 0;
							 | 
						|
											$form->decimal('price','提现金额')->rules('required|numeric|min:'.$min.'|not_in:0',[
							 | 
						|
													'*' => '提现金额为必填字段且必须大于'.$min,
							 | 
						|
												])
							 | 
						|
												->default($auto->price ?? 0)
							 | 
						|
												->help('含手续费' . SystemSetting::val('withdrawal', WithdrawalTraits::$adminType[0]) . ' %,最小提现金额'.$min);
							 | 
						|
											$form->text('account')->required()->maxLength(50)->default($auto->pay->account ?? '');
							 | 
						|
											$form->text('name')->required()->maxLength(50)->default($auto->pay->name ?? '');
							 | 
						|
											$form->image('qrcode');
							 | 
						|
											$form->hidden('withdrawal_id');
							 | 
						|
								
							 | 
						|
											$form->saving(function (Form $form) {
							 | 
						|
												DB::beginTransaction();
							 | 
						|
												try {
							 | 
						|
													$user = Agent::query()->where('id', Admin::user()->id)->lockForUpdate()->first();
							 | 
						|
													//手续费
							 | 
						|
													$cutPrice = bcmul($form->price, SystemSetting::val('withdrawal', WithdrawalTraits::$adminType[0]), 6);
							 | 
						|
													$cutPrice = $cutPrice > 0 ? bcdiv($cutPrice, 100, 6) : 0;
							 | 
						|
													//总花费
							 | 
						|
													$total = bcadd($form->price, $cutPrice,6);
							 | 
						|
													if ($total > $user->balance) {
							 | 
						|
														return $form->response()->error('余额不足,本次提现需花费'.bcadd($total,0,2).'(含手续费),当前可用余额为' . $user->balance);
							 | 
						|
													}
							 | 
						|
													//提现扣钱流水
							 | 
						|
													$service = new WithdrawalService();
							 | 
						|
													$service->create(
							 | 
						|
														bcmul($form->price, -1, 6),
							 | 
						|
														StatementType::WITHDRAWAL,
							 | 
						|
														Admin::user()->id,
							 | 
						|
														DemandTraits::$col[0]
							 | 
						|
													);
							 | 
						|
													//提现手续费流水
							 | 
						|
													$service->create(
							 | 
						|
														bcmul($cutPrice, -1, 6),
							 | 
						|
														StatementType::WITHDRAWAL_CAT,
							 | 
						|
														Admin::user()->id,
							 | 
						|
														DemandTraits::$col[0]
							 | 
						|
													);
							 | 
						|
													$user->balance = bcsub($user->balance, $total, 6);
							 | 
						|
													$user->save();
							 | 
						|
								
							 | 
						|
													$withdrawal = new Withdrawal();
							 | 
						|
													$withdrawal->user_id = Admin::user()->id;
							 | 
						|
													$withdrawal->user_type = DemandTraits::$col[0];
							 | 
						|
													$withdrawal->price = request('price', 0);
							 | 
						|
													$withdrawal->cut_price = $cutPrice;
							 | 
						|
													//$withdrawal->pay_id = $form->getKey();
							 | 
						|
													$withdrawal->pay_type = WithdrawalTraits::$userType[0];
							 | 
						|
													$withdrawal->save();
							 | 
						|
								
							 | 
						|
													$form->withdrawal_id = $withdrawal->id;
							 | 
						|
													$form->deleteInput('price');
							 | 
						|
													DB::commit();
							 | 
						|
												} catch (\Exception $e) {
							 | 
						|
													DB::rollBack();
							 | 
						|
													return $form->response()->error($e->getMessage());
							 | 
						|
												}
							 | 
						|
											});
							 | 
						|
								
							 | 
						|
											$form->saved(function (Form $form) {
							 | 
						|
												$withdrawal = Withdrawal::find($form->withdrawal_id);
							 | 
						|
												$withdrawal->pay_id = $form->getKey();
							 | 
						|
												$withdrawal->save();
							 | 
						|
											});
							 | 
						|
										});
							 | 
						|
									}
							 | 
						|
								}
							 |