193 lines
5.6 KiB
PHP
193 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands\Dealer;
|
|
|
|
use App\Admin\Services\DealerEarningService;
|
|
use App\Enums\DealerOrderSettleState;
|
|
use App\Enums\DealerSalesValueLogType;
|
|
use App\Models\Dealer;
|
|
use App\Models\DealerChannelSubsidyLog;
|
|
use App\Models\DealerEarning;
|
|
use App\Models\DealerManagerSalesLog;
|
|
use App\Models\DealerManageSubsidyLog;
|
|
use App\Models\DealerOrder;
|
|
use App\Models\DealerPurchaseLog;
|
|
use App\Models\DealerSalesValueLog;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Throwable;
|
|
|
|
class ChannelSubsidySettleCommand extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'dealer:channel-subsidy-settle';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = '结算签约经销商的渠道补贴';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function handle()
|
|
{
|
|
while (true) {
|
|
$page = 0;
|
|
|
|
DealerEarning::channelSubsidy()->withoutPayer()->onlyPending()->chunkById(200, function ($earnings) use (&$page) {
|
|
$earnings->load(['earningable', 'user']);
|
|
|
|
foreach ($earnings as $earning) {
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$earning->update([
|
|
'settle_at' => now(),
|
|
]);
|
|
|
|
(new DealerEarningService())->pay($earning);
|
|
|
|
DB::commit();
|
|
} catch (Throwable $e) {
|
|
DB::rollBack();
|
|
|
|
report($e);
|
|
}
|
|
}
|
|
|
|
$page++;
|
|
});
|
|
|
|
if ($page === 0) {
|
|
sleep(60);
|
|
} elseif ($page === 1) {
|
|
sleep(30);
|
|
} else {
|
|
sleep(15);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 处理经销商订单
|
|
*
|
|
* @param \App\Models\DealerOrder $order
|
|
* @return void
|
|
*/
|
|
protected function handleDealerOrder(DealerOrder $order)
|
|
{
|
|
$this->handleManagerSalesLogs($order);
|
|
|
|
$this->handleManageSubsidyLogs($order);
|
|
|
|
$this->handleChannelSubsidyLogs($order);
|
|
|
|
$this->handlePurchaseLogs($order);
|
|
|
|
$this->handleOrder($order);
|
|
}
|
|
|
|
protected function handleManagerSalesLogs(DealerOrder $order)
|
|
{
|
|
DealerManagerSalesLog::where('order_id', $order->id)->update([
|
|
'order_completed_at' => $order->shippinged_time,
|
|
]);
|
|
}
|
|
|
|
protected function handleManageSubsidyLogs(DealerOrder $order)
|
|
{
|
|
DealerManageSubsidyLog::where('order_id', $order->id)->update([
|
|
'order_completed_at' => $order->shippinged_time,
|
|
]);
|
|
}
|
|
|
|
protected function handleChannelSubsidyLogs(DealerOrder $order)
|
|
{
|
|
$channelSubsidyLogIds = DealerChannelSubsidyLog::where('order_id', $order->id)->get('id')->toArray();
|
|
|
|
DealerChannelSubsidyLog::whereIn('id', $channelSubsidyLogIds)->update([
|
|
'order_completed_at' => $order->shippinged_time,
|
|
]);
|
|
|
|
if ($order->consignor_id !== null) {
|
|
DealerEarning::where('earningable_type', 'dealer_channel_subsidy_log')->whereIn('earningable_id', $channelSubsidyLogIds)->update([
|
|
'settle_at' => now(),
|
|
]);
|
|
}
|
|
}
|
|
|
|
protected function handlePurchaseLogs(DealerOrder $order)
|
|
{
|
|
DealerPurchaseLog::where('order_id', $order->id)->update([
|
|
'order_completed_at' => $order->shippinged_time,
|
|
]);
|
|
}
|
|
|
|
protected function handleOrder(DealerOrder $order)
|
|
{
|
|
$salesValue = $order->total_amount;
|
|
|
|
if (bccomp($salesValue, '0', 2) < 1) {
|
|
return;
|
|
}
|
|
|
|
$salesValueLogs = [];
|
|
|
|
if (bccomp($salesValue, '0', 2) === 1) {
|
|
$ts = now()->toDateTimeString();
|
|
|
|
$order->dealer->update([
|
|
'self_sales_value' => DB::raw("self_sales_value+{$salesValue}"),
|
|
]);
|
|
|
|
$salesValueLogs[] = [
|
|
'user_id' => $order->user_id,
|
|
'loggable_type' => $order->getMorphClass(),
|
|
'loggable_id' => $order->id,
|
|
'type' => DealerSalesValueLogType::Personal,
|
|
'change_sales_value' => $salesValue,
|
|
'remark' => '个人进货',
|
|
'created_at' => $ts,
|
|
'updated_at' => $ts,
|
|
];
|
|
|
|
if (count($pids = $order->dealer->userInfo->parent_ids) > 0) {
|
|
foreach ($order->dealer->userInfo->parent_ids as $pid) {
|
|
$salesValueLogs[] = [
|
|
'user_id' => $pid,
|
|
'loggable_type' => $order->getMorphClass(),
|
|
'loggable_id' => $order->id,
|
|
'type' => DealerSalesValueLogType::Team,
|
|
'change_sales_value' => $salesValue,
|
|
'remarks' => '团队成员进货',
|
|
'created_at' => $ts,
|
|
'updated_at' => $ts,
|
|
];
|
|
}
|
|
|
|
// 更新上级的团队团队业绩
|
|
Dealer::whereIn('user_id', $pids)->update([
|
|
'team_sales_value' => DB::raw("team_sales_value + {$salesValue}"),
|
|
]);
|
|
}
|
|
}
|
|
|
|
// 保存销售值日志
|
|
DealerSalesValueLog::insert($salesValueLogs);
|
|
|
|
// 将订单标记未已结算
|
|
$order->forceFill([
|
|
'settle_state' => DealerOrderSettleState::Completed,
|
|
])->save();
|
|
}
|
|
}
|