6
0
Fork 0
jiqu-library-server/app/Console/Commands/OfflineOrderCloseExpiredCom...

73 lines
1.8 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Enums\OfflineOrderStatus;
use App\Models\OfflineOrder;
use App\Services\OfflineOrderService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Throwable;
class OfflineOrderCloseExpiredCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'offline-order:close-expired';
/**
* The console command description.
*
* @var string
*/
protected $description = '关闭过期的线下订单';
/**
* Execute the console command.
*
* @return int
*/
public function handle(OfflineOrderService $offlineOrderService)
{
while (true) {
// 待付款的线下订单的有效期
$expirs = (int) app_settings('app.offline_order_payment_expires_at', 0);
if ($expirs > 0) {
$ids = OfflineOrder::where('status', OfflineOrderStatus::Pending)
->where('created_at', '<=', now()->subSeconds($expirs))
->oldest('id')
->limit(500)
->pluck('id');
foreach ($ids as $id) {
try {
DB::beginTransaction();
$order = OfflineOrder::lockForUpdate()->findOrFail($id);
$offlineOrderService->revoke($order);
DB::commit();
} catch (Throwable $e) {
DB::rollBack();
report($e);
}
}
if ($ids->isEmpty()) {
sleep(60);
} else {
sleep(10);
}
} else {
sleep(60);
}
}
}
}