105 lines
2.9 KiB
PHP
105 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Enums\PayWay;
|
|
use App\Models\Order;
|
|
use App\Models\OrderProfit;
|
|
use App\Services\DistributeService;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class OrderProfitCommand extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'order:profit';
|
|
|
|
/**
|
|
* 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 int
|
|
*/
|
|
public function handle()
|
|
{
|
|
|
|
$service = new DistributeService();
|
|
|
|
// 售后过期天数 7
|
|
$saleDays = app_settings('app.sale_after_expire_days');
|
|
while (true) {
|
|
$page = 0;
|
|
|
|
$orders = Order::query()
|
|
// 订单已完成
|
|
->where('status', Order::STATUS_COMPLETED)
|
|
// 没有售后订单
|
|
->whereDoesntHave('afterSales')
|
|
// 售后期过了
|
|
->where('completed_at', '<', now()->subDays($saleDays))
|
|
// 未支付提成
|
|
->whereNull('profit_paid')
|
|
->limit(10)
|
|
->get();
|
|
|
|
// 修改提成记录的状态为 等待结算
|
|
OrderProfit::whereIn('order_id', $orders->pluck('id'))->where('status', 0)->update(['status' => 4]);
|
|
|
|
// 获取 等待结算 的提成记录, 按照 user_id 分组
|
|
$list = OrderProfit::where('status', 4)->get()->groupBy('user_id');
|
|
foreach($list as $id => $items) {
|
|
try {
|
|
DB::beginTransaction();
|
|
$service->wechatTransfers($items);
|
|
DB::commit();
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
$this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
// 没有待付款的提成记录
|
|
Order::where(fn($q) => $q->whereHas('profits', fn($q) => $q->where('status', 2))->orWhereDoesntHave('profits'))
|
|
// 订单已完成
|
|
->where('status', Order::STATUS_COMPLETED)
|
|
// 没有售后订单
|
|
->whereDoesntHave('afterSales')
|
|
// 售后期过了
|
|
->where('completed_at', '<', now()->subDays($saleDays))
|
|
// 未支付提成
|
|
->whereNull('profit_paid')
|
|
->update(['profit_paid' => now()]);
|
|
|
|
$page++;
|
|
|
|
if ($page === 0) {
|
|
sleep(60);
|
|
} elseif ($page === 1) {
|
|
sleep(30);
|
|
} else {
|
|
sleep(15);
|
|
}
|
|
}
|
|
}
|
|
}
|