70 lines
1.9 KiB
PHP
70 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands\Dealer;
|
|
|
|
use App\Enums\DealerOrderStatus;
|
|
use App\Models\DealerOrder;
|
|
use App\Services\Dealer\OrderService;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Throwable;
|
|
|
|
class OrderAutoAllocate extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'dealer:order-auto-allocate';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = '自动分配订单';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function handle()
|
|
{
|
|
while (true) {
|
|
$page = 0;
|
|
|
|
DealerOrder::where('status', DealerOrderStatus::Pending)
|
|
->where('consignor_id', '>', 1) // 到1用户或者公司的订单不需要再分配
|
|
->where('allocated_at', '<', now()->subMinutes(app_settings('dealer.order_auto_allocate_times')))
|
|
->chunkById(200, function ($orders) use (&$page) {
|
|
$orders->load([
|
|
'consignor',
|
|
]);
|
|
$orderService = new OrderService();
|
|
foreach ($orders as $order) {
|
|
try {
|
|
DB::beginTransaction();
|
|
$orderService->updateOrderConsignor($order);
|
|
DB::commit();
|
|
} catch (Throwable $th) {
|
|
DB::rollBack();
|
|
report($th);
|
|
}
|
|
}
|
|
|
|
$page++;
|
|
});
|
|
|
|
if ($page === 0) {
|
|
sleep(60);
|
|
} elseif ($page === 1) {
|
|
sleep(30);
|
|
} else {
|
|
sleep(15);
|
|
}
|
|
}
|
|
}
|
|
}
|