whereIn('status', $status_arr) ->whereNotNull('timeout') ->min('id');*/ $min_id = 0; while (true) { Order::with('orderProductItem') // ->where('id', '>=', $min_id) ->whereIn('status', $status_arr) ->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); //代理商产品加库存 AgentProduct::where('id', $v->agent_product_id)->increment('stock', $v->num); if (!$v->orderProductItem->isEmpty()) { $item = $v->orderProductItem->toArray(); //供应商产品规格加库存 foreach ($item as $v2) { if ($v2['product_spec_id']) { if (is_array($v2['product_spec_id'])) { ProductSpec::whereIn('id', $v2['product_spec_id'])->increment('stock', $v->num); } else { ProductSpec::where('id', $v2['product_spec_id'])->increment('stock', $v->num); } } } //代理商产品规格加库存 foreach ($item as $v2) { if ($v2['agent_product_spec_id']) { if (is_array($v2['agent_product_spec_id'])) { AgentProductSpec::whereIn('id', $v2['agent_product_spec_id'])->increment('stock', $v->num); } else { AgentProductSpec::where('id', $v2['agent_product_spec_id'])->increment('stock', $v->num); } } } } DB::commit(); $min_id = $v->id; } catch (\Exception $exception) { DB::rollBack(); $this->line("订单ID {$v->id} 错误:" . $exception->getMessage()); } } } }); //TODO 产品规格表加库存 $this->line('[' . date('Y-m-d H:i:s') . "] ID游标:$min_id ,等待下一个任务"); sleep(5); } return 0; } }