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

121 lines
3.3 KiB
PHP

<?php
namespace App\Console\Commands\Dealer;
use App\Enums\DealerOrderSettleState;
use App\Enums\DealerOrderStatus;
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 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 = 'dealer:order-settle';
/**
* The console command description.
*
* @var string
*/
protected $description = '将可自动完成的订单标记为已完成';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
DealerOrder::where(
'status',
DealerOrderStatus::Completed
)->where(
'settle_state',
DealerOrderSettleState::Processed
)->whereNotNull('shippinged_time')->chunkById(200, function ($orders) {
foreach ($orders as $order) {
try {
DB::beginTransaction();
$this->handleDealerOrder($order);
DB::commit();
} catch (Throwable $e) {
DB::rollBack();
report($e);
}
}
});
return 0;
}
/**
* 处理经销商订单
*
* @param \App\Models\DealerOrder $dealerOrder
* @return void
*/
protected function handleDealerOrder(DealerOrder $dealerOrder)
{
$this->handleManagerSalesLogs($dealerOrder);
$this->handleManageSubsidyLogs($dealerOrder);
$this->handleChannelSubsidyLogs($dealerOrder);
$this->handlePurchaseLogs($dealerOrder);
// 将订单标记为已完成
$dealerOrder->forceFill([
'settle_state' => DealerOrderSettleState::Completed,
])->save();
}
protected function handleManagerSalesLogs(DealerOrder $dealerOrder)
{
DealerManagerSalesLog::where('order_id', $dealerOrder->id)->update([
'order_completed_at' => $dealerOrder->shippinged_time,
]);
}
protected function handleManageSubsidyLogs(DealerOrder $dealerOrder)
{
DealerManageSubsidyLog::where('order_id', $dealerOrder->id)->update([
'order_completed_at' => $dealerOrder->shippinged_time,
]);
}
protected function handleChannelSubsidyLogs(DealerOrder $dealerOrder)
{
$channelSubsidyLogIds = DealerChannelSubsidyLog::where('order_id', $dealerOrder->id)->get('id')->toArray();
DealerChannelSubsidyLog::whereIn('id', $channelSubsidyLogIds)->update([
'order_completed_at' => $dealerOrder->shippinged_time,
]);
DealerEarning::where('earningable_type', 'dealer_channel_subsidy_log')->whereIn('earningable_id', $channelSubsidyLogIds)->update([
'settle_at' => now(),
]);
}
protected function handlePurchaseLogs(DealerOrder $dealerOrder)
{
DealerPurchaseLog::where('order_id', $dealerOrder->id)->update([
'order_completed_at' => $dealerOrder->shippinged_time,
]);
}
}