db->beginTransaction(); try { if (!LockHelper::lock($close_lock_key, $close_identification, 30, 60)) { throw new Exception('正在关闭订单'); } $order = CloudStockAgentOrder::find() ->where([ 'id' => $id, 'send_status' => [CloudStockEnum::PICK_ORDER_STATUS0, CloudStockEnum::PICK_ORDER_STATUS3], 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id ]) ->one(); if (empty($order)) { throw new RuntimeException('自提订单异常'); } $isCallRefund = $order->send_status == CloudStockEnum::PICK_ORDER_STATUS0; $details = CloudStockAgentOrderDetails::find() ->where(['status' => StatusEnum::ENABLED, 'order_id' => $order->id, 'mall_id' => $order->mall_id]) ->select('num, goods_id') ->asArray() ->all(); if (empty($details)) { throw new RuntimeException('自提订单异常'); } foreach ($details as $detail) { $params_add = [ 'goods_id' => $detail['goods_id'], 'changes_dir' => CloudStockEnum::STOCK_CHANGE_ADD, 'user_id' => $order['user_id'], 'order_id' => $order['id'], 'order_type' => 0, 'changes_type' => CloudStockEnum::CHANGE_TYPE_PICKOFFLINE_RETURN, 'num' => $detail['num'], 'status' => StatusEnum::ENABLED, ]; $res_save_log = CloudStockGoodsStockLog::setData($this->mall_id, $params_add); if (false === $res_save_log) { throw new RuntimeException(CloudStockGoodsStockLog::getStaticError()); } $add_params = [ 'user_id' => $order['user_id'], 'num' => $detail['num'], 'mall_id' => $this->mall_id, 'goods_id' => $detail['goods_id'], 'order_id' => $order['id'], 'order_type' => 0, 'changes_type' => CloudStockEnum::CHANGE_TYPE_PICKOFFLINE_RETURN, 'stock_log_id' => $res_save_log->id, 'status' => StatusEnum::ENABLED, ]; $res_add = CloudStockIncreaseDetail::setData($add_params); if (false === $res_add) { throw new RuntimeException(CloudStockIncreaseDetail::getStaticError()); } $cloud_stock_agent_goods_model = CloudStockAgentGoods::findOne(['mall_id' => $order['mall_id'], 'user_id' => $order['user_id'], 'goods_id' => $detail['goods_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]); if (!empty($cloud_stock_agent_goods_model)) { $cloud_stock_agent_goods_model->num += $detail['num']; $res = $cloud_stock_agent_goods_model->save(); if (!$res) { throw new RuntimeException($cloud_stock_agent_goods_model->getErrorMessage()); } } $cloud_stock_agent_model = CloudStockAgent::findOne(['mall_id' => $order['mall_id'], 'user_id' => $order['user_id'], 'status' => StatusEnum::ENABLED]); if (!empty($cloud_stock_agent_model)) { $cloud_stock_agent_model->stock += $detail['num']; $res = $cloud_stock_agent_model->save(); if (!$res) { throw new RuntimeException($cloud_stock_agent_model->getErrorMessage()); } } $order->cancel_admin_id = $this->admin_id; $order->send_status = CloudStockEnum::PICK_ORDER_STATUS4; if (!$order->save()) { throw new RuntimeException($order->getErrorMessage()); } } $t->commit(); } catch (Exception $e) { $t->rollBack(); $this->setError($e->getMessage()); return false; } finally { LockHelper::uLock($close_lock_key, $close_identification); } if (!$isCallRefund) { return true; } // 执行退款 return $this->shippingRefunds($order); } }