request->isCli()) { $this->error('Autotask script only work at client!'); } parent::_initialize(); // 清除错误 error_reporting(0); // 设置永不超时 set_time_limit(0); } /** * 执行定时任务 */ public function index() { $time = time(); try { $config = get_addon_config('shop'); //确认收货时间 $receive = $time - $config['auto_delivery_time'] * 24 * 60 * 60; //失效订单 $order = Order::where(function ($query) use ($time) { $query->where('paystate', 0)->where('expiretime', '<=', $time)->where('orderstate', 0); })->whereOr(function ($query) use ($receive) { $query->where('shippingstate', 1)->where('paystate', 1)->where('shippingtime', '<=', $receive)->where('orderstate', 0); })->select(); //超过设置订单状态为过期 if ($order) { foreach ($order as $item) { // 启动事务 Db::startTrans(); try { //失效的 if (!$item->paystate) { $item->save(['orderstate' => 2]); //库存恢复 OrderGoods::setGoodsStocksInc($item->order_sn); //恢复优惠券 UserCoupon::resetUserCoupon($item->user_coupon_id, $item->order_sn); } else { //待收货的 $item->save(['shippingstate' => 2, 'receivetime' => time()]); OrderAction::push($item->order_sn, '系统', '订单确认收货成功'); } // 提交事务 Db::commit(); } catch (\Exception $e) { // 回滚事务 Db::rollback(); continue; } } } } catch (\Exception $e) { echo $e->getMessage(); } echo "done"; } }