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; + } +}