162 lines
4.8 KiB
PHP
162 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\DistributionPreIncome;
|
|
use App\Models\Order;
|
|
use App\Models\SalesValueLog;
|
|
use App\Models\UserInfo;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Throwable;
|
|
|
|
class OrderSettleCommand extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'order:settle';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = '订单结算';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function handle()
|
|
{
|
|
while (true) {
|
|
Order::where(
|
|
'completed_at',
|
|
'<=',
|
|
now()->subDays(app_settings('distribution.settle_days', 7))
|
|
)->where([
|
|
'status' => Order::STATUS_COMPLETED,
|
|
'is_settlable' => false,
|
|
'is_settle' => false,
|
|
])->chunkById(200, function ($orders) {
|
|
foreach ($orders as $order) {
|
|
$order->update([
|
|
'is_settlable' => true,
|
|
]);
|
|
}
|
|
});
|
|
|
|
Order::whereDoesntHave('afterSales', function ($query) {
|
|
return $query->processing();
|
|
})->whereDoesntHave('distributionPreIncomeJobs', function ($query) {
|
|
return $query->pending();
|
|
})->settlable()->chunkById(200, function ($orders) {
|
|
$orders->load(['user', 'afterSales']);
|
|
|
|
foreach ($orders as $order) {
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$this->settle($order);
|
|
|
|
DB::commit();
|
|
} catch (Throwable $e) {
|
|
DB::rollBack();
|
|
|
|
report($e);
|
|
}
|
|
}
|
|
});
|
|
|
|
sleep(60);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 结算成长值
|
|
*
|
|
* @param \App\Models\Order $order
|
|
* @return void
|
|
*/
|
|
protected function settle(Order $order)
|
|
{
|
|
// 用户可得销售值
|
|
$salesValue = $order->sales_value;
|
|
|
|
foreach ($order->afterSales as $afterSale) {
|
|
if ($afterSale->isCancelled()) {
|
|
continue;
|
|
}
|
|
|
|
$salesValue = bcsub($salesValue, $afterSale->sales_value, 2);
|
|
}
|
|
|
|
if (bccomp($salesValue, '0', 2) === 1) {
|
|
$salesValueLogs = [];
|
|
$ts = now()->toDateTimeString();
|
|
|
|
// 更新下单用户的成长值和预成长值
|
|
$order->user->userInfo()->update([
|
|
'growth_value' => DB::raw("growth_value+{$salesValue}"),
|
|
'pre_growth_value' => DB::raw("pre_growth_value-{$salesValue}"),
|
|
]);
|
|
|
|
// 下单用户的销售值日志
|
|
$salesValueLogs[] = [
|
|
'user_id' => $order->user->id,
|
|
'order_id' => $order->id,
|
|
'order_user_id' => $order->user->id,
|
|
'type' => SalesValueLog::TYPE_INDIVIDUAL,
|
|
'change_sales_value' => $salesValue,
|
|
'remarks' => '个人消费',
|
|
'created_at' => $ts,
|
|
'updated_at' => $ts,
|
|
];
|
|
|
|
// 提升下单用户的代理等级
|
|
$order->user->userInfo->attemptUpgradeAgentLevel();
|
|
|
|
if (count($pids = $order->user->userInfo->parent_ids) > 0) {
|
|
$ancestors = UserInfo::whereIn('user_id', $pids)->latest('depth')->get();
|
|
|
|
foreach ($ancestors as $ancestor) {
|
|
$ancestor->attemptUpgradeAgentLevel();
|
|
|
|
$salesValueLogs[] = [
|
|
'user_id' => $ancestor->user_id,
|
|
'order_id' => $order->id,
|
|
'order_user_id' => $order->user_id,
|
|
'type' => SalesValueLog::TYPE_TEAM,
|
|
'change_sales_value' => $salesValue,
|
|
'remarks' => '团队成员消费',
|
|
'created_at' => $ts,
|
|
'updated_at' => $ts,
|
|
];
|
|
}
|
|
|
|
// 更新上级的团队销售值
|
|
UserInfo::whereIn('user_id', $pids)->update([
|
|
'group_sales_value' => DB::raw("group_sales_value + {$salesValue}"),
|
|
]);
|
|
}
|
|
|
|
// 保存销售值日志
|
|
SalesValueLog::insert($salesValueLogs);
|
|
}
|
|
|
|
// 将订单标记未已结算
|
|
$order->update([
|
|
'is_settle' => true,
|
|
]);
|
|
|
|
// 将预收益标记为结算中
|
|
DistributionPreIncome::where('order_id', $order->id)->update([
|
|
'status' => DistributionPreIncome::STATUS_PROCESSING,
|
|
]);
|
|
}
|
|
}
|