setName('order_close') ->setDescription('系统关闭超时未付款订单'); } protected function execute(Input $input, Output $output) { $now = time(); $ableClose = ConfigService::get('transaction', 'cancel_unpaid_orders'); $cancelTime = ConfigService::get('transaction', 'cancel_unpaid_orders_times') * 60; if ($ableClose == YesNoEnum::NO) { return true; } $orders = Order::with('order_goods') ->whereRaw("create_time+$cancelTime < $now") ->where([ 'order_status' => OrderEnum::STATUS_WAIT_PAY, 'pay_status' => PayEnum::UNPAID, ])->select(); if (empty($orders)) { return true; } try{ foreach ($orders as $order) { //回退订单商品库存 $this->rollbackGoods($order); //更新订单状态 $this->updateOrderSattus($order); //如有使用优惠券, 返回优惠券 AfterSaleService::returnCoupon($order); } } catch(\Exception $e) { Log::write('订单自动关闭失败,失败原因:' . $e->getMessage()); } } /** * @notes 回退库存 * @param $order * @author 段誉 * @date 2021/9/15 14:32 */ protected function rollbackGoods($order) { foreach ($order['order_goods'] as $good) { Goods::update([ 'total_stock' => Db::raw('total_stock+' . $good['goods_num']) ], ['id' => $good['goods_id']]); //补充规格表库存 GoodsItem::update([ 'stock' => Db::raw('stock+' . $good['goods_num']) ], ['id' => $good['item_id']]); } } /** * @notes 更新订单状态 * @param $order * @author 段誉 * @date 2021/9/15 14:32 */ protected function updateOrderSattus($order) { //更新订单状态 Order::update(['order_status' => OrderEnum::STATUS_CLOSE], ['id' => $order['id']]); // 订单日志 (new OrderLog())->record([ 'type' => OrderLogEnum::TYPE_SYSTEM, 'channel' => OrderLogEnum::SYSTEM_CANCEL_ORDER, 'order_id' => $order['id'], 'operator_id' => $order['user_id'], ]); } }