Auth::id()]; // 用户地址 $address = Address::query()->where('user_id', $data['user_id'])->find($params['addressId']); !$address && self::error('地址信息错误'); $data['address'] = $address['address']; $data['real_address'] = $address['addressInfo']; $data['address_lat'] = $address['lat']; $data['address_lng'] = $address['lng']; $data['area_id'] = 0; // 获取项目信息 $project = Project::query()->find($params['projectId']); $data['project_name'] = $project->title; $data['project_icon'] = $project->cover; // 优惠卷 // if ($params['couponId']) $data['coupon_id'] = $params['couponId']; // $coupon_record_model = new CouponRecord(); // 渠道 // $channel_model = new UserChannel(); // 用户渠道 // $input['channel_id'] = $channel_model->getChannelId($this->_user['id']); // 订单号 $order_id = $params['orderId'] ?? null; // 使用余额 $use_balance = $params['useBalance'] ?? 0; // 出行方式 $car_type = $params['carType'] ?? 0; // 技师 $coach_id = $params['coachId'] ?? null; if ($coach_id) { $coach_model = new User(); $coach_info = $coach_model->info($coach_id); !$coach_info && self::error('技师不存在'); !empty($coach_info) && $coach_info['is_work'] == 0 && self::error('该技师未上班'); !empty($coach_info) && ($coach_info['auth_status'] !== 2 || $coach_info['status'] !== 1) && self::error('该技师已下架'); } // 优惠卷ID $coupon_id = $params['couponId'] ?? 0; // 加钟订单 if ($order_id) { } else { // $address = $address_model->info($params['address_id']); // if(empty($address)){ // $this->errorMsg('请添加地址'); // } } $orderModel = new Order(); DB::beginTransaction(); try { $memberQuery = \App\Models\Member\User::query(); $member = $memberQuery->lockForUpdate()->find($data['user_id']); $orderPayInfo = $orderModel->buildPayInfo($data['user_id'], $params['projectId'], $address['city_code'], $address['lat'], $address['lng'], $use_balance, $coach_id, $car_type, $coupon_id, $order_id); //默认微信 $payType = $params['pay_type'] ?? 1; $use_balance && !$orderPayInfo['pay_price'] && ($payType = 0); // 订单状态 $orderStatus = $use_balance && !$orderPayInfo['pay_price'] ? 1 : 0; $orderData = [ 'order_sn' => $this->buildOrderSN(), 'pay_type' => $payType, 'status' => $orderStatus, ...$data, ...$orderPayInfo, //备注 // 'text' => $params['text'] ?: '', // 'car_type' => $car_type, // 'channel_id' => $params['channel_id'] ?: 0, //目的地地址 //加钟 // 'add_pid' => $order_id, // 'is_add' => $order_id ? 1 : 0, ]; // 创建订单 $orderId = $orderModel->newQuery()->create($orderData)->id; // 变更用户余额 if ($use_balance && $orderPayInfo['balance_price']) { Benefit::query()->create([ 'user_id' => $data['user_id'], 'order_id' => $orderId, 'type' => 1, 'benefit' => $orderPayInfo['balance_price'] * -1, 'balance' => $member['balance'] ]); $member->decrement('balance', $orderPayInfo['balance_price']); } DB::commit(); return $orderId; } catch (\Exception $e) { DB::rollBack(); Log::error('Transaction failed: ' . $e->getMessage()); self::error('执行错误!'); } } public function getOrderPage(array $data) { $user_id = Auth::id(); $status = intval($data['status'] ?? 0); $where = ['user_id' => $user_id, 'user_del' => 0]; $statusRange = match ($status) { 1 => [0, 1, 2, 3], 2 => [4, 5], 3 => [5], 4 => [6, 7], default => [0, 1, 2, 3, 4, 5, 6, 7] }; // $select = ['id', 'project_name', 'project_icon', 'duration', 'paid_type', 'price', 'real_address', 'status', 'created_at', 'refunded_at']; $select = ['id', 'project_name', 'project_icon', 'time_long', 'pay_type', 'pay_price', 'real_address', 'status', 'created_at']; $orderPage = Order::query()->where($where)->whereIn('status', $statusRange)->select($select)->latest()->paginate(); return ['list' => $orderPage->items(), 'total' => $orderPage->total()]; } public function getOrder(int $order_id) { $user_id = Auth::id(); return Order::query()->where('user_id', $user_id)->select($this->baseColumn)->find($order_id); } /** * @throws ApiException */ public function updateOrder(array $data, int $order_id): void { $order = Order::query()->find($order_id); $status = $data['status']; switch ($status) { // 结束订单 case 6: $order->status !== 5 && self::error('订单状态有误'); // 查询是否存在加钟订单 // 判断加钟服务是否完成 $order->user_end = 1; $order->end_time = time(); break; case 9: !in_array($order->status, [0, 1, 2, 3]) && self::error('订单状态有误'); break; } $order->status = $status; $order->save(); } public function delOrder(int $order_id): void { $order = Order::query()->find($order_id); $order->user_del = 1; $order->save(); } public function confirmOrder(array $params) { $user_id = Auth::id(); $projectId = $params['projectId']; $addressId = $params['addressId']; $coachId = $params['coachId'] ?? 0; // 查询地区项目 $projectSelect = ['title', 'sub_title', 'cover', 'price', 'init_price', 'total_sale', 'time_long', 'is_store', 'is_door']; $project = Project::query()->select($projectSelect)->find($projectId); // 查询用户余额 $balance = \App\Models\Member\User::query()->where('id', $user_id)->pluck('balance'); // 车费 // 判断技师是否收取车费 // $coach = User::query()->find($coachId); // 获取技师定位 $coachSite = null; if ($coachId) { $coachSelect = ['latitude', 'longitude']; $coachSite = Site::query()->select($coachSelect)->where('coach_id', $coachId)->first(); } return [ 'balance' => $balance, 'project' => $project, 'coach' => $coachSite ]; } public function computePrice(array $params): void { $user_id = Auth::id(); $address = $params['addressId']; $use_balance = $params['useBalance']; $coach_id = $params['coachId']; $memberQuery = \App\Models\Member\User::query(); $member = $memberQuery->find($user_id); $orderModel = new Order(); $orderPayInfo = $orderModel->buildPayInfo($user_id, $params['projectId'], $address['city_code'], $address['lat'], $address['lng'], $use_balance, $coach_id, $car_type, $coupon_id, $order_id); } }