Browse Source

提现

master
lemon 4 years ago
parent
commit
3feb163798
  1. 29
      app/Admin/Forms/SystemSetting.php
  2. 106
      app/AdminAgent/Controllers/WithdrawalAlipayController.php
  3. 97
      app/AdminAgent/Controllers/WithdrawalBankController.php
  4. 8
      app/Common/StatementType.php
  5. 8
      app/Models/SystemSetting.php
  6. 23
      app/Service/WithdrawalService.php
  7. 14
      app/Traits/WithdrawalTraits.php
  8. 32
      database/migrations/2021_09_16_114815_update_withdrawal_table.php

29
app/Admin/Forms/SystemSetting.php

@ -2,6 +2,7 @@
namespace App\Admin\Forms; namespace App\Admin\Forms;
use App\Traits\WithdrawalTraits;
use Dcat\Admin\Widgets\Form; use Dcat\Admin\Widgets\Form;
class SystemSetting extends Form class SystemSetting extends Form
@ -20,11 +21,12 @@ class SystemSetting extends Form
]); ]);
$arr = [ $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(); $system->save();
return $this return $this
->response() ->response()
@ -37,9 +39,10 @@ class SystemSetting extends Form
*/ */
public function 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() 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 [ 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
]; ];
} }
} }

106
app/AdminAgent/Controllers/WithdrawalAlipayController.php

@ -3,8 +3,11 @@
namespace App\AdminAgent\Controllers; namespace App\AdminAgent\Controllers;
use App\AdminAgent\Repositories\WithdrawalAlipay; use App\AdminAgent\Repositories\WithdrawalAlipay;
use App\Common\StatementType;
use App\Models\Agent; use App\Models\Agent;
use App\Models\SystemSetting;
use App\Models\Withdrawal; use App\Models\Withdrawal;
use App\Service\WithdrawalService;
use App\Traits\DemandTraits; use App\Traits\DemandTraits;
use App\Traits\WithdrawalTraits; use App\Traits\WithdrawalTraits;
use Dcat\Admin\Admin; use Dcat\Admin\Admin;
@ -19,15 +22,15 @@ use Illuminate\Support\Facades\DB;
class WithdrawalAlipayController extends AdminController 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') ->with('pay')
->where([ ->where([
'user_id' => Admin::user()->id, 'user_id' => Admin::user()->id,
@ -37,40 +40,71 @@ class WithdrawalAlipayController extends AdminController
]) ])
->orderByDesc('updated_at') ->orderByDesc('updated_at')
->first(); ->first();
$form->display('id'); $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('account')->required()->maxLength(50)->default($auto->pay->account ?? '');
$form->text('name')->required()->maxLength(50)->default($auto->pay->name ?? ''); $form->text('name')->required()->maxLength(50)->default($auto->pay->name ?? '');
$form->image('qrcode'); $form->image('qrcode');
$form->hidden('withdrawal_id'); $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();
});
});
}
} }

97
app/AdminAgent/Controllers/WithdrawalBankController.php

@ -3,8 +3,11 @@
namespace App\AdminAgent\Controllers; namespace App\AdminAgent\Controllers;
use App\AdminAgent\Repositories\WithdrawalBank; use App\AdminAgent\Repositories\WithdrawalBank;
use App\Common\StatementType;
use App\Models\Agent; use App\Models\Agent;
use App\Models\SystemSetting;
use App\Models\Withdrawal; use App\Models\Withdrawal;
use App\Service\WithdrawalService;
use App\Traits\DemandTraits; use App\Traits\DemandTraits;
use App\Traits\WithdrawalTraits; use App\Traits\WithdrawalTraits;
use Dcat\Admin\Admin; use Dcat\Admin\Admin;
@ -13,18 +16,19 @@ use Dcat\Admin\Grid;
use Dcat\Admin\Show; use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController; use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class WithdrawalBankController extends AdminController 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() $auto = Withdrawal::query()
->with('pay') ->with('pay')
@ -36,33 +40,66 @@ class WithdrawalBankController extends AdminController
]) ])
->orderByDesc('updated_at') ->orderByDesc('updated_at')
->first(); ->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('name')->required()->maxLength(50)->default($auto->pay->name ?? '');
$form->text('card_number')->required()->maxLength(50)->type('number')->default($auto->pay->card_number ?? ''); $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) { $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) { $form->saved(function (Form $form) {
@ -70,6 +107,6 @@ class WithdrawalBankController extends AdminController
$withdrawal->pay_id = $form->getKey(); $withdrawal->pay_id = $form->getKey();
$withdrawal->save(); $withdrawal->save();
}); });
});
}
});
}
} }

8
app/Common/StatementType.php

@ -14,12 +14,20 @@ class StatementType
/** @var int 订单结算 */ /** @var int 订单结算 */
const ORDER = 3; const ORDER = 3;
/** @var int 提现 */
const WITHDRAWAL = 4;
/** @var int 提现手续费 */
const WITHDRAWAL_CAT = 5;
public static function array(): array public static function array(): array
{ {
return [ return [
self::CHARGE => '微信支付手续费', self::CHARGE => '微信支付手续费',
self::CUT => '抽成', self::CUT => '抽成',
self::ORDER => '订单结算', self::ORDER => '订单结算',
self::WITHDRAWAL => '提现',
self::WITHDRAWAL_CAT => '提现手续费',
]; ];
} }
} }

8
app/Models/SystemSetting.php

@ -14,4 +14,12 @@ class SystemSetting extends Model
protected $table = 'system_settings'; protected $table = 'system_settings';
protected $guarded = ['id']; 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;
}
} }

23
app/Service/WithdrawalService.php

@ -0,0 +1,23 @@
<?php
namespace App\Service;
use App\Common\StatementType;
use App\Models\Statement;
use App\Traits\DemandTraits;
class WithdrawalService
{
public function create($price,$type,$userId,$userType,$orderId = '')
{
Statement::query()->create([
'price' => $price,
'type' => $type,
'user_id' => $userId,
'user_type' => $userType,
'order_id' => $orderId ?? ''
]);
}
}

14
app/Traits/WithdrawalTraits.php

@ -35,4 +35,18 @@ trait WithdrawalTraits
3 => '已通过', 3 => '已通过',
4 => '已打款', 4 => '已打款',
]; ];
public static $adminType = [
'agent',
'supplier',
'guide',
'min'
];
public static $adminTypeText = [
'agent' => '代理商',
'supplier' =>'供应商',
'guide' =>'地接',
'min' => '最小提现金额'
];
} }

32
database/migrations/2021_09_16_114815_update_withdrawal_table.php

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class UpdateWithdrawalTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('withdrawal', function (Blueprint $table) {
$table->decimal('cut_price')
->default(0)
->comment('提现手续费');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
Loading…
Cancel
Save