v2board/app/Console/Commands/CheckCommission.php

126 lines
3.5 KiB
PHP
Raw Normal View History

2019-10-29 15:33:36 +08:00
<?php
namespace App\Console\Commands;
2021-09-18 20:04:35 +08:00
use App\Models\CommissionLog;
2019-10-29 15:33:36 +08:00
use Illuminate\Console\Command;
use App\Models\Order;
use App\Models\User;
2021-01-10 02:25:31 +08:00
use Illuminate\Support\Facades\DB;
2019-10-29 15:33:36 +08:00
class CheckCommission extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'check:commission';
/**
* The console command description.
*
* @var string
*/
protected $description = '返佣服务';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
2020-05-03 23:17:55 +08:00
{
$this->autoCheck();
$this->autoPayCommission();
}
public function autoCheck()
{
2020-05-04 20:08:43 +08:00
if ((int)config('v2board.commission_auto_check_enable', 1)) {
2020-05-03 23:17:55 +08:00
Order::where('commission_status', 0)
2020-11-26 15:07:47 +08:00
->where('invite_user_id', '!=', NULL)
2021-03-13 00:03:53 +08:00
->whereNotIn('status', [0, 2])
2020-05-07 18:54:18 +08:00
->where('updated_at', '<=', strtotime('-3 day', time()))
2020-05-03 23:17:55 +08:00
->update([
'commission_status' => 1
]);
}
}
public function autoPayCommission()
2019-10-29 15:33:36 +08:00
{
2021-09-18 19:53:34 +08:00
$orders = Order::where('commission_status', 1)
2020-11-26 15:07:47 +08:00
->where('invite_user_id', '!=', NULL)
2019-10-29 15:33:36 +08:00
->get();
2021-09-18 19:53:34 +08:00
foreach ($orders as $order) {
DB::beginTransaction();
if (!$this->payHandle($order->invite_user_id, $order)) {
DB::rollBack();
continue;
}
$order->commission_status = 2;
if (!$order->save()) {
DB::rollBack();
continue;
}
DB::commit();
}
}
public function payHandle($inviteUserId, Order $order)
{
if ((int)config('v2board.commission_distribution_enable', 0)) {
$level = 3;
$commissionShareLevels = [
0 => (int)config('v2board.commission_distribution_l1'),
1 => (int)config('v2board.commission_distribution_l2'),
2 => (int)config('v2board.commission_distribution_l3')
];
} else {
$level = 3;
$commissionShareLevels = [
0 => 100
];
}
for ($l = 0; $l < $level; $l++) {
$inviter = User::find($inviteUserId);
2020-11-26 15:07:47 +08:00
if (!$inviter) continue;
2021-09-22 17:04:16 +08:00
if (!isset($commissionShareLevels[$l])) continue;
2021-09-18 20:04:35 +08:00
$commissionBalance = $order->commission_balance * ($commissionShareLevels[$l] / 100);
2021-02-03 18:17:32 +08:00
if ((int)config('v2board.withdraw_close_enable', 0)) {
2021-09-18 19:53:34 +08:00
$inviter->balance = $inviter->balance + $commissionBalance;
2021-02-03 18:17:32 +08:00
} else {
2021-09-18 19:53:34 +08:00
$inviter->commission_balance = $inviter->commission_balance + $commissionBalance;
2021-02-03 18:17:32 +08:00
}
2021-09-18 19:53:34 +08:00
if (!$inviter->save()) {
DB::rollBack();
return false;
2021-01-10 02:25:31 +08:00
}
2021-09-18 20:04:35 +08:00
if (!CommissionLog::create([
'invite_user_id' => $inviteUserId,
'user_id' => $order->user_id,
'trade_no' => $order->trade_no,
'order_amount' => $order->total_amount,
'get_amount' => $commissionBalance
])) {
DB::rollBack();
return false;
}
2021-09-18 19:53:34 +08:00
$inviteUserId = $inviter->invite_user_id;
2019-10-29 15:33:36 +08:00
}
2021-09-18 19:53:34 +08:00
return true;
2019-10-29 15:33:36 +08:00
}
2020-01-11 13:36:52 +08:00
2019-10-29 15:33:36 +08:00
}