61 lines
1.3 KiB
PHP
61 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\Order;
|
|
use App\Services\OrderService;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Throwable;
|
|
|
|
class OrderCloseExpiredCommand extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'order:close-expired';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = '关闭过期的订单';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function handle(OrderService $orderService)
|
|
{
|
|
while (true) {
|
|
$ids = Order::expired()->oldest('id')->limit(500)->pluck('id');
|
|
|
|
foreach ($ids as $id) {
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
$order = Order::lockForUpdate()->findOrFail($id);
|
|
|
|
$orderService->cancel($order);
|
|
|
|
DB::commit();
|
|
} catch (Throwable $e) {
|
|
DB::rollBack();
|
|
|
|
report($e);
|
|
}
|
|
}
|
|
|
|
if ($ids->isEmpty()) {
|
|
sleep(60);
|
|
} else {
|
|
sleep(10);
|
|
}
|
|
}
|
|
}
|
|
}
|