From 2d6694c4f8d34a604734cdd1827cd28b6b26ff85 Mon Sep 17 00:00:00 2001 From: liapples Date: Wed, 1 Sep 2021 15:07:05 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A2=E5=8D=95=E8=B6=85=E6=97=B6=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E5=9B=9E=E5=BA=93=E5=AD=98=E5=AE=9A=E6=97=B6=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/OrderTimeout.php | 79 +++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 app/Console/Commands/OrderTimeout.php diff --git a/app/Console/Commands/OrderTimeout.php b/app/Console/Commands/OrderTimeout.php new file mode 100644 index 0000000..b1b0adc --- /dev/null +++ b/app/Console/Commands/OrderTimeout.php @@ -0,0 +1,79 @@ +where('status', OrderStatus::UNPAID)->min('id'); + while (true) { + Order::query() + ->where([ + ['status', '=', OrderStatus::UNPAID], + ['id', '>=', $min_id], + ]) + ->whereNotNull('timeout') + ->orderBy('id') + ->chunk(100, function ($order) use (&$min_id) { + foreach ($order as $v) { + if (!is_null($v->timeout) && strtotime($v->timeout) < time()) { + DB::beginTransaction(); + try { + //取消订单 + $v->status = OrderStatus::CANCEL; + $v->save(); + + //加回库存 + Product::whereIn('id', explode(',', $v->product_ids))->increment('stock', $v->num); + + DB::commit(); + } catch (\Exception $exception) { + DB::rollBack(); + } + } + $min_id = $v->id; + } + }); + $this->line('[' . date('Y-m-d H:i:s') . "] ID游标:$min_id ,等待下一个任务"); + sleep(3); + } + return 0; + } +}