63 lines
1.5 KiB
PHP
63 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Exceptions\BizException;
|
|
use App\Models\Order;
|
|
use App\Services\OrderService;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Throwable;
|
|
|
|
class OrderCompleteCommand extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'order:complete';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = '将可自动完成的订单标记为已完成';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function handle(OrderService $orderService)
|
|
{
|
|
while (true) {
|
|
$page = 0;
|
|
|
|
Order::with('packages')->completable()->chunkById(200, function ($orders) use ($orderService, &$page) {
|
|
foreach ($orders as $order) {
|
|
try {
|
|
DB::beginTransaction();
|
|
$orderService->confirm($order, true);
|
|
DB::commit();
|
|
} catch (BizException $e) {
|
|
DB::rollBack();
|
|
report($e);
|
|
}
|
|
}
|
|
|
|
$page++;
|
|
});
|
|
|
|
if ($page === 0) {
|
|
sleep(60);
|
|
} elseif ($page === 1) {
|
|
sleep(30);
|
|
} else {
|
|
sleep(15);
|
|
}
|
|
}
|
|
}
|
|
}
|