自动支付提成记录
parent
1a2ae7e17b
commit
19254d084c
|
|
@ -0,0 +1,104 @@
|
||||||
|
<?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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -55,6 +55,7 @@ class Order extends Model
|
||||||
'pay_at' => 'datetime',
|
'pay_at' => 'datetime',
|
||||||
'completed_at' => 'datetime',
|
'completed_at' => 'datetime',
|
||||||
'auto_complete_at' => 'datetime',
|
'auto_complete_at' => 'datetime',
|
||||||
|
'profit_paid' => 'datetime',
|
||||||
'status' => 'int',
|
'status' => 'int',
|
||||||
'is_change' => 'bool',
|
'is_change' => 'bool',
|
||||||
];
|
];
|
||||||
|
|
@ -96,6 +97,7 @@ class Order extends Model
|
||||||
'inviter_id',
|
'inviter_id',
|
||||||
'market_price',
|
'market_price',
|
||||||
'cost_price',
|
'cost_price',
|
||||||
|
'profit_paid',
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -177,7 +177,7 @@ class DistributeService
|
||||||
* 返现记录支付成功
|
* 返现记录支付成功
|
||||||
*
|
*
|
||||||
* @param \App\Models\OrderProfit $profit
|
* @param \App\Models\OrderProfit $profit
|
||||||
* @param mixed $data
|
* @param mixed $data [paid_at, remarks, pay_way]
|
||||||
*/
|
*/
|
||||||
public function success(OrderProfit $profit, $data = null)
|
public function success(OrderProfit $profit, $data = null)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,32 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
class AddProfitPaidToOrdersTable extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::table('orders', function (Blueprint $table) {
|
||||||
|
$table->timestamp('profit_paid')->nullable()->comment('提成支付完成时间');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::table('orders', function (Blueprint $table) {
|
||||||
|
$table->dropColumn('profit_paid');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue