get(IdGeneratorInterface::class); $globalOrderId = $generator->generate(); $dataMain = [ 'market_id' => $store->market_id, 'order_num' => $globalOrderId, 'global_order_id' => $globalOrderId, 'user_id' => $userId, 'type' => OrderType::OFFLINE, 'money' => $money, 'total_money' => $money, 'services_money' => 0, 'coupon_money' => 0, 'delivery_money' => 0, 'state' => OrderState::UNPAID, 'tel' => '', 'address' => '', 'lat' => '', 'lng' => '', 'name' => '', 'plat' => $plat, 'delivery_time_note' => '' ]; $orderMain = OrderMain::query()->create($dataMain); $orderMainId = $orderMain->id; // 店铺今天的订单数 $count = Order::query() ->where(['store_id' => $storeId, 'type' => OrderType::OFFLINE]) ->whereBetween('created_at', [strtotime(date('Y-m-d 00:00:00')), strtotime(date('Y-m-d 23:59:59'))]) ->count(); // 子订单数据 $dataChildren = [ 'order_main_id' => $orderMainId, 'user_id' => $userId, 'store_id' => $storeId, 'money' => $money, 'order_num' => date('YmdHis').mt_rand(1000, 9999), 'note' => '', 'oid' => $count + 1 ]; $orderChild = Order::query()->create($dataChildren); $orderChildId = $orderChild->id; Db::commit(); // 支付 return $this->paymentService->do($globalOrderId, $money, $userId, config('wechat.notify_url.offline')); } catch (\Exception $e) { Db::rollBack(); $this->log->event(LogLabel::ORDER_OFFLINE_LOG, ['exception_msg' => $e->getMessage()]); throw new ErrorCodeException(ErrorCode::ORDER_ONLINE_FAIL); } } public function check() { // TODO: Implement check() method. } public function undo() { // TODO: Implement undo() method. } public function doPaid($orderMainId) { Db::beginTransaction(); try { // 主订单状态更新 $orderMain = OrderMain::query() ->where(['id' => $orderMainId, 'type' => OrderType::OFFLINE]) ->first(); if (empty($orderMain)) { $this->log->event( LogLabel::ORDER_OFFLINE_PAID_LOG, ['order_not_found' => $orderMain->global_order_id] ); Db::rollBack(); return false; } $currentTime = time(); $orderMain->state = OrderState::COMPLETED; $orderMain->pay_time = $currentTime; $orderMain->save(); Db::commit(); return true; } catch (\Exception $e) { $this->log->event(LogLabel::ORDER_OFFLINE_PAID_LOG, ['exception' => $e->getMessage()]); Db::rollBack(); return false; } } }