6
0
Fork 0
jiqu-library-server/app/Console/Commands/OrderProfitCommand.php

105 lines
2.8 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Enums\PayWay;
use App\Exceptions\BizException;
use App\Models\Order;
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');
// 支付方式
$payWay = PayWay::WxpayTransfer->value;
while (true) {
$page = 0;
$orders = Order::with([
'profits' => fn($q) => $q->where('status', 0)
])
// 订单已完成
->where('status', Order::STATUS_COMPLETED)
// 没有售后订单
->whereDoesntHave('afterSales')
// 售后期过了
->where('completed_at', '<', now()->subDays($saleDays))
// 未支付提成
->whereNull('profit_paid')
->limit(200);
foreach ($orders as $order) {
foreach($order->profits as $profit) {
try {
DB::beginTransaction();
if ($payWay === PayWay::WxpayTransfer->value) {
if (!$profit->pay_no) {
$profit->update([
'pay_no' => serial_number(),
'pay_way' => $payWay
]);
}
$service->wechatTransfer($profit);
}
DB::commit();
} catch (BizException $e) {
DB::rollBack();
}
}
// 没有待付款的提成记录
if (!$order->profits()->where('status', 0)->exists()) {
// 更新订单
$order->update(['profit_paid' => now()]);
}
}
$page++;
if ($page === 0) {
sleep(60);
} elseif ($page === 1) {
sleep(30);
} else {
sleep(15);
}
}
}
}