From 3feb16379880d4365e15a9c8c7ea2495ea5075cc Mon Sep 17 00:00:00 2001 From: lemon <15040771@qq.com> Date: Thu, 16 Sep 2021 14:04:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Admin/Forms/SystemSetting.php | 29 ++--- .../WithdrawalAlipayController.php | 106 ++++++++++++------ .../Controllers/WithdrawalBankController.php | 97 +++++++++++----- app/Common/StatementType.php | 8 ++ app/Models/SystemSetting.php | 8 ++ app/Service/WithdrawalService.php | 23 ++++ app/Traits/WithdrawalTraits.php | 14 +++ ...1_09_16_114815_update_withdrawal_table.php | 32 ++++++ 8 files changed, 237 insertions(+), 80 deletions(-) create mode 100644 app/Service/WithdrawalService.php create mode 100644 database/migrations/2021_09_16_114815_update_withdrawal_table.php diff --git a/app/Admin/Forms/SystemSetting.php b/app/Admin/Forms/SystemSetting.php index e4d3993..bfcd183 100644 --- a/app/Admin/Forms/SystemSetting.php +++ b/app/Admin/Forms/SystemSetting.php @@ -2,6 +2,7 @@ namespace App\Admin\Forms; +use App\Traits\WithdrawalTraits; use Dcat\Admin\Widgets\Form; class SystemSetting extends Form @@ -20,11 +21,12 @@ class SystemSetting extends Form ]); $arr = [ - 'agent' => $input['agent'], - 'supllier' => $input['supllier'], - 'guide' => $input['guide'], + WithdrawalTraits::$adminType[0] => $input[WithdrawalTraits::$adminType[0]], + WithdrawalTraits::$adminType[1] => $input[WithdrawalTraits::$adminType[1]], + WithdrawalTraits::$adminType[2] => $input[WithdrawalTraits::$adminType[2]], + WithdrawalTraits::$adminType[3] => $input[WithdrawalTraits::$adminType[3]], ]; - $system->value = json_encode($arr); + $system->value = $arr; $system->save(); return $this ->response() @@ -37,9 +39,10 @@ class SystemSetting extends Form */ public function form() { - $this->number('agent','代理商')->min(0)->max(100)->required()->help('提现手续费 %'); - $this->number('supllier','供应商')->min(0)->max(100)->required()->help('提现手续费 %'); - $this->number('guide','地接')->min(0)->max(100)->required()->help('提现手续费 %'); + $this->number(WithdrawalTraits::$adminType[0],WithdrawalTraits::$adminTypeText[WithdrawalTraits::$adminType[0]])->min(0)->max(100)->required()->help('提现手续费 %'); + $this->number(WithdrawalTraits::$adminType[1],WithdrawalTraits::$adminTypeText[WithdrawalTraits::$adminType[1]])->min(0)->max(100)->required()->help('提现手续费 %'); + $this->number(WithdrawalTraits::$adminType[2],WithdrawalTraits::$adminTypeText[WithdrawalTraits::$adminType[2]])->min(0)->max(100)->required()->help('提现手续费 %'); + $this->number(WithdrawalTraits::$adminType[3],WithdrawalTraits::$adminTypeText[WithdrawalTraits::$adminType[3]])->min(0)->help('提现手续费 %'); } /** @@ -49,14 +52,12 @@ class SystemSetting extends Form */ public function default() { - $system = \App\Models\SystemSetting::query()->where('key','withdrawal')->first(); - if ($system) { - $key = json_decode($system->value, true); - } + $system = \App\Models\SystemSetting::query()->where('key','withdrawal')->value('value'); return [ - 'agent' => $key['agent'] ?? 0, - 'supllier' => $key['supllier'] ?? 0, - 'guide' => $key['guide'] ?? 0 + WithdrawalTraits::$adminType[0] => $system[WithdrawalTraits::$adminType[0]] ?? 0, + WithdrawalTraits::$adminType[1] => $system[WithdrawalTraits::$adminType[1]] ?? 0, + WithdrawalTraits::$adminType[2] => $system[WithdrawalTraits::$adminType[2]] ?? 0, + WithdrawalTraits::$adminType[3] => $system[WithdrawalTraits::$adminType[3]] ?? 0 ]; } } diff --git a/app/AdminAgent/Controllers/WithdrawalAlipayController.php b/app/AdminAgent/Controllers/WithdrawalAlipayController.php index e77c28b..d5dc3bd 100755 --- a/app/AdminAgent/Controllers/WithdrawalAlipayController.php +++ b/app/AdminAgent/Controllers/WithdrawalAlipayController.php @@ -3,8 +3,11 @@ 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; @@ -19,15 +22,15 @@ 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() + /** + * 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, @@ -37,40 +40,71 @@ class WithdrawalAlipayController extends AdminController ]) ->orderByDesc('updated_at') ->first(); - $form->display('id'); - $form->decimal('price','提现金额')->required()->type('number')->attribute('min', 0)->default($auto->price ?? 0); + $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) { - $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(); + $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->pay_id = $form->getKey(); - $withdrawal->pay_type = WithdrawalTraits::$userType[0]; - $withdrawal->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'); - }); + $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(); - }); - }); - } + $form->saved(function (Form $form) { + $withdrawal = Withdrawal::find($form->withdrawal_id); + $withdrawal->pay_id = $form->getKey(); + $withdrawal->save(); + }); + }); + } } diff --git a/app/AdminAgent/Controllers/WithdrawalBankController.php b/app/AdminAgent/Controllers/WithdrawalBankController.php index cef6984..28f21fd 100755 --- a/app/AdminAgent/Controllers/WithdrawalBankController.php +++ b/app/AdminAgent/Controllers/WithdrawalBankController.php @@ -3,8 +3,11 @@ namespace App\AdminAgent\Controllers; use App\AdminAgent\Repositories\WithdrawalBank; +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; @@ -13,18 +16,19 @@ use Dcat\Admin\Grid; use Dcat\Admin\Show; use Dcat\Admin\Http\Controllers\AdminController; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Facades\DB; class WithdrawalBankController extends AdminController { - /** - * Make a form builder. - * - * @return Form - */ - protected function form() - { - return Form::make(new WithdrawalBank(), function (Form $form) { + /** + * Make a form builder. + * + * @return Form + */ + protected function form() + { + return Form::make(new WithdrawalBank(), function (Form $form) { $auto = Withdrawal::query() ->with('pay') @@ -36,33 +40,66 @@ class WithdrawalBankController extends AdminController ]) ->orderByDesc('updated_at') ->first(); - $form->display('id'); - $form->decimal('price','提现金额')->required()->maxLength(50)->default($auto->price ?? 0); + $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('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->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); - } + 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,$form->price,6); - $user->save(); + $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->pay_id = $form->getKey(); - $withdrawal->pay_type = WithdrawalTraits::$userType[1]; - $withdrawal->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[1]; + $withdrawal->save(); - $form->withdrawal_id = $withdrawal->id; - $form->deleteInput('price'); + $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) { @@ -70,6 +107,6 @@ class WithdrawalBankController extends AdminController $withdrawal->pay_id = $form->getKey(); $withdrawal->save(); }); - }); - } + }); + } } diff --git a/app/Common/StatementType.php b/app/Common/StatementType.php index b6e6a93..66befab 100644 --- a/app/Common/StatementType.php +++ b/app/Common/StatementType.php @@ -14,12 +14,20 @@ class StatementType /** @var int 订单结算 */ const ORDER = 3; + /** @var int 提现 */ + const WITHDRAWAL = 4; + + /** @var int 提现手续费 */ + const WITHDRAWAL_CAT = 5; + public static function array(): array { return [ self::CHARGE => '微信支付手续费', self::CUT => '抽成', self::ORDER => '订单结算', + self::WITHDRAWAL => '提现', + self::WITHDRAWAL_CAT => '提现手续费', ]; } } diff --git a/app/Models/SystemSetting.php b/app/Models/SystemSetting.php index 5c06f4f..6940a7b 100755 --- a/app/Models/SystemSetting.php +++ b/app/Models/SystemSetting.php @@ -14,4 +14,12 @@ class SystemSetting extends Model protected $table = 'system_settings'; protected $guarded = ['id']; + + protected $casts = ['value' => 'json']; + + public static function val($key,$value) + { + $setting = static::query()->where('key',$key)->value('value'); + return $setting[$value] ?? null; + } } diff --git a/app/Service/WithdrawalService.php b/app/Service/WithdrawalService.php new file mode 100644 index 0000000..9254172 --- /dev/null +++ b/app/Service/WithdrawalService.php @@ -0,0 +1,23 @@ +create([ + 'price' => $price, + 'type' => $type, + 'user_id' => $userId, + 'user_type' => $userType, + 'order_id' => $orderId ?? '' + ]); + } +} diff --git a/app/Traits/WithdrawalTraits.php b/app/Traits/WithdrawalTraits.php index f2c43a3..71ebc6f 100644 --- a/app/Traits/WithdrawalTraits.php +++ b/app/Traits/WithdrawalTraits.php @@ -35,4 +35,18 @@ trait WithdrawalTraits 3 => '已通过', 4 => '已打款', ]; + + public static $adminType = [ + 'agent', + 'supplier', + 'guide', + 'min' + ]; + + public static $adminTypeText = [ + 'agent' => '代理商', + 'supplier' =>'供应商', + 'guide' =>'地接', + 'min' => '最小提现金额' + ]; } diff --git a/database/migrations/2021_09_16_114815_update_withdrawal_table.php b/database/migrations/2021_09_16_114815_update_withdrawal_table.php new file mode 100644 index 0000000..39a52bf --- /dev/null +++ b/database/migrations/2021_09_16_114815_update_withdrawal_table.php @@ -0,0 +1,32 @@ +decimal('cut_price') + ->default(0) + ->comment('提现手续费'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +}