From 6ea6a34ed6594b9ecbc6d2149dd7aa6f0a6a7270 Mon Sep 17 00:00:00 2001 From: lemon <15040771@qq.com> Date: Thu, 16 Sep 2021 16:13:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E8=B4=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Api/VerificationController.php | 163 +++++++++--------- app/Models/Order.php | 2 +- app/Models/Statement.php | 5 + app/Models/Withdrawal.php | 5 + app/Service/WithdrawalService.php | 30 +++- app/Traits/StatementTraits.php | 18 ++ ...21_09_16_151024_update_statement_table.php | 32 ++++ 7 files changed, 175 insertions(+), 80 deletions(-) create mode 100644 app/Traits/StatementTraits.php create mode 100644 database/migrations/2021_09_16_151024_update_statement_table.php diff --git a/app/Http/Controllers/Api/VerificationController.php b/app/Http/Controllers/Api/VerificationController.php index b84ca48..1341562 100644 --- a/app/Http/Controllers/Api/VerificationController.php +++ b/app/Http/Controllers/Api/VerificationController.php @@ -14,8 +14,10 @@ use App\Models\OrderProductItem; use App\Models\User; use App\Common\OrderStatus; use App\Service\SmsService; +use App\Service\WithdrawalService; use App\Traits\DemandTraits; use App\Traits\SmsTraits; +use App\Traits\StatementTraits; use EasyWeChat\Factory; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Storage; @@ -40,12 +42,12 @@ class VerificationController extends Controller return $this->error('订单不存在或订单状态不允许核销'); } - //$mobile = User::where('id', $this->user_id)->value('mobile'); - // - //$checkMobile = Product::query()->whereIn('id', explode(',', $order->product_ids))->where('verify_mobile', $mobile)->doesntExist(); - //if ($checkMobile) { - // return $this->error('对不起,你没有核销权限,请联系管理员'); - //} + $mobile = User::where('id', $this->user_id)->value('mobile'); + + $checkMobile = Product::query()->whereIn('id', explode(',', $order->product_ids))->where('verify_mobile', $mobile)->doesntExist(); + if ($checkMobile) { + return $this->error('对不起,你没有核销权限,请联系管理员'); + } $order->status = OrderStatus::SUCCESS; if ($order->save()) { @@ -72,6 +74,7 @@ class VerificationController extends Controller public function fund($order) { + $service = new WithdrawalService(); DB::beginTransaction(); try { //最后批量插入 @@ -82,13 +85,14 @@ class VerificationController extends Controller $guidePrice = $order->guide_price; $cost = bcadd($cost, $order->guide_price, 6); //成本价 加上地接价格 - $statementCreate[] = [ - 'price' => $order->guide_price, - 'type' => StatementType::ORDER, - 'user_id' => $order->guide->id, - 'user_type' => DemandTraits::$col[2], - 'order_id' => $order->id - ]; + $statementCreate[] = $service->createByOrder( + $order->guide_price, + StatementType::ORDER, + $order->guide->id, + DemandTraits::$col[2], + $order->id, + StatementTraits::$type[0] + ); //抽成 if ($order->guide->rate > 0) { //计算抽成金额 @@ -96,21 +100,22 @@ class VerificationController extends Controller $cutPrice = $guideCut > 0 ? bcdiv($guideCut, 100, 6) : 0; //总后台抽成流水 if ($cutPrice > 0) { - $adminCreate[] = [ - 'price' => $cutPrice, - 'type' => StatementType::CUT, - 'cut_user_id' => $order->guide->id, - 'cut_user_type' => DemandTraits::$col[2], - 'order_id' => $order->id - ]; + $adminCreate[] = $service->createByOrderFormAdmin( + $cutPrice, + StatementType::CUT, + $order->guide->id, + DemandTraits::$col[2], + $order->id, + ); //地接被抽成流水 - $statementCreate[] = [ - 'price' => bcmul($cutPrice, -1, 2), - 'type' => StatementType::CUT, - 'user_id' => $order->guide->id, - 'user_type' => DemandTraits::$col[2], - 'order_id' => $order->id - ]; + $statementCreate[] = $service->createByOrder( + bcmul($cutPrice, -1, 2), + StatementType::CUT, + $order->guide->id, + DemandTraits::$col[2], + $order->id, + StatementTraits::$type[0] + ); $guidePrice = bcsub($order->guide_price, $cutPrice, 6); $guide = Guide::query()->where('id', $order->guide->id)->lockForUpdate()->first(); $guide->balance = bcadd($guide->balance, $guidePrice, 6); @@ -131,13 +136,14 @@ class VerificationController extends Controller $cost = bcadd($cost, $v->sum_price, 6); $supplierPrice = $v->sum_price; - $statementCreate[] = [ - 'price' => $v->sum_price, - 'type' => StatementType::ORDER, - 'user_id' => $v->supplier_id, - 'user_type' => DemandTraits::$col[1], - 'order_id' => $order->id - ]; + $statementCreate[] = $service->createByOrder( + $v->sum_price, + StatementType::ORDER, + $v->supplier_id, + DemandTraits::$col[1], + $order->id, + StatementTraits::$type[0] + ); if ($v->supplier->rate > 0) { //计算抽成金额 @@ -145,21 +151,22 @@ class VerificationController extends Controller $cutPrice = $supplierCut > 0 ? bcdiv($supplierCut, 100, 6) : 0; if ($cutPrice > 0) { //总后台抽成流水 - $adminCreate[] = [ - 'price' => $cutPrice, - 'type' => StatementType::CUT, - 'cut_user_id' => $v->supplier_id, - 'cut_user_type' => DemandTraits::$col[1], - 'order_id' => $order->id - ]; + $adminCreate[] = $service->createByOrderFormAdmin( + $cutPrice, + StatementType::CUT, + $v->supplier_id, + DemandTraits::$col[1], + $order->id, + ); //供应商被抽成流水 - $statementCreate[] = [ - 'price' => bcmul($cutPrice, -1, 6), - 'type' => StatementType::CUT, - 'user_id' => $v->supplier_id, - 'user_type' => DemandTraits::$col[1], - 'order_id' => $order->id - ]; + $statementCreate[] = $service->createByOrder( + bcmul($cutPrice, -1, 6), + StatementType::CUT, + $v->supplier_id, + DemandTraits::$col[1], + $order->id, + StatementTraits::$type[0] + ); $supplierPrice = bcsub($supplierPrice, $cutPrice, 6); } @@ -174,13 +181,14 @@ class VerificationController extends Controller $agentPrice = bcsub($order->price, $cost, 2); if ($agentPrice > 0) { - $statementCreate[] = [ - 'price' => $agentPrice, - 'type' => StatementType::ORDER, - 'user_id' => $order->agent_id, - 'user_type' => DemandTraits::$col[0], - 'order_id' => $order->id - ]; + $statementCreate[] = $service->createByOrder( + $agentPrice, + StatementType::ORDER, + $order->agent_id, + DemandTraits::$col[0], + $order->id, + StatementTraits::$type[0] + ); //抽成 if ($order->agent->rate > 0) { @@ -190,40 +198,41 @@ class VerificationController extends Controller //总后台抽成流水 if ($cutPrice > 0) { - $adminCreate[] = [ - 'price' => $cutPrice, - 'type' => StatementType::CUT, - 'cut_user_id' => $order->agent->id, - 'cut_user_type' => DemandTraits::$col[0], - 'order_id' => $order->id - ]; + $adminCreate[] = $service->createByOrderFormAdmin( + $cutPrice, + StatementType::CUT, + $order->agent->id, + DemandTraits::$col[0], + $order->id, + ); //代理商被抽成流水 - $statementCreate[] = [ - 'price' => bcmul($cutPrice, -1, 6), - 'type' => StatementType::CUT, - 'user_id' => $order->agent->id, - 'user_type' => DemandTraits::$col[0], - 'order_id' => $order->id - ]; + $statementCreate[] = $service->createByOrder( + bcmul($cutPrice, -1, 6), + StatementType::CUT, + $order->agent->id, + DemandTraits::$col[0], + $order->id, + StatementTraits::$type[0] + ); $agentPrice = bcsub($agentPrice, $cutPrice, 6); } } //扣除微信支付手续费 $chargePrice = bcmul($order->price, 0.006, 6); - $statementCreate[] = [ - 'price' => bcmul($chargePrice, -1, 6), - 'type' => StatementType::CHARGE, - 'user_id' => $order->agent_id, - 'user_type' => DemandTraits::$col[0], - 'order_id' => $order->id - ]; + $statementCreate[] = $service->createByOrder( + bcmul($chargePrice, -1, 6), + StatementType::CHARGE, + $order->agent_id, + DemandTraits::$col[0], + $order->id, + StatementTraits::$type[0] + ); $agentPrice = bcsub($agentPrice, $chargePrice, 6); $agent = Agent::query()->where('id', $order->agent->id)->lockForUpdate()->first(); $agent->balance = bcadd($agent->balance, $agentPrice, 6); $agent->save(); } - //dd($adminCreate,$guideCreate); if (!empty($adminCreate)) { $order->statementAdmin()->createMany($adminCreate); } diff --git a/app/Models/Order.php b/app/Models/Order.php index 3d7d56a..87d4b13 100644 --- a/app/Models/Order.php +++ b/app/Models/Order.php @@ -131,6 +131,6 @@ class Order extends BaseModel public function statement() { - return $this->hasMany(Statement::class,'order_id','id'); + return $this->morphMany(Statement::class,'access'); } } diff --git a/app/Models/Statement.php b/app/Models/Statement.php index ed1a060..a7db421 100755 --- a/app/Models/Statement.php +++ b/app/Models/Statement.php @@ -17,4 +17,9 @@ class Statement extends Model { return $this->morphTo(); } + + public function access() + { + return $this->morphTo(); + } } diff --git a/app/Models/Withdrawal.php b/app/Models/Withdrawal.php index 60aa45c..22a06b9 100755 --- a/app/Models/Withdrawal.php +++ b/app/Models/Withdrawal.php @@ -22,4 +22,9 @@ class Withdrawal extends Model { return $this->morphTo(); } + + public function statement() + { + return $this->morphMany(Statement::class,'access'); + } } diff --git a/app/Service/WithdrawalService.php b/app/Service/WithdrawalService.php index 9254172..71b21b6 100644 --- a/app/Service/WithdrawalService.php +++ b/app/Service/WithdrawalService.php @@ -10,14 +10,40 @@ use App\Traits\DemandTraits; class WithdrawalService { - public function create($price,$type,$userId,$userType,$orderId = '') + //提现流水 + public function create($price,$type,$userId,$userType,$accessId,$accessType) { Statement::query()->create([ 'price' => $price, 'type' => $type, 'user_id' => $userId, 'user_type' => $userType, - 'order_id' => $orderId ?? '' + 'access_id' => $accessId, + 'access_type' => $accessType, ]); } + + //订单流水 暂时放一起 + public function createByOrder($price,$type,$userId,$userType,$accessId,$accessType) + { + return [ + 'price' => $price, + 'type' => $type, + 'user_id' => $userId, + 'user_type' => $userType, + 'access_id' => $accessId, + 'access_type' => $accessType, + ]; + } + + public function createByOrderFormAdmin($price,$type,$userId,$userType,$orderId) + { + return [ + 'price' => $price, + 'type' => $type, + 'cut_user_id' => $userId, + 'cut_user_type' => $userType, + 'order_id' => $orderId, + ]; + } } diff --git a/app/Traits/StatementTraits.php b/app/Traits/StatementTraits.php new file mode 100644 index 0000000..899aaf0 --- /dev/null +++ b/app/Traits/StatementTraits.php @@ -0,0 +1,18 @@ + '订单', + 'App\Models\Withdrawal' => '提现', + ]; + +} diff --git a/database/migrations/2021_09_16_151024_update_statement_table.php b/database/migrations/2021_09_16_151024_update_statement_table.php new file mode 100644 index 0000000..ffd445e --- /dev/null +++ b/database/migrations/2021_09_16_151024_update_statement_table.php @@ -0,0 +1,32 @@ +string('access_type')->index()->default('')->comment('关联类型 目前有订单 和提现')->after('user_type'); + $table->integer('access_id')->index()->default(0)->comment('关联')->after('user_type'); + $table->dropColumn('order_id'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +}