coach, 403, '非教练用户无法创建投流订单'); // 验证时间段参数 abort_if(! in_array($params['time_slot'], [1, 2, 3]), 422, '无效的时间段'); // 验证天数参数 $validDays = [1, 7, 15, 30, 90, 180, 365]; abort_if(! in_array($params['days'], $validDays), 422, '无效的天数'); // 验证位置类型 abort_if(! in_array($params['position_type'], [1, 2]), 422, '无效的位置类型'); // 计算订单金额 $amount = $this->calculateAmount($params['time_slot'], $params['days']); // 生成订单号 $orderNo = $this->generateOrderNo(); // 创建订单 $order = $user->coach->flowOrders()->create([ 'order_no' => $orderNo, 'coach_id' => $user->id, 'position_type' => $params['position_type'], 'time_slot' => $params['time_slot'], 'days' => $params['days'], 'amount' => $amount, 'state' => 0, 'start_time' => null, 'end_time' => null, ]); Log::info('创建投流订单成功', [ 'order_id' => $order->id, 'order_no' => $orderNo, 'user_id' => $userId, 'amount' => $amount, 'params' => $params, ]); return [ 'order_id' => $order->id, 'order_no' => $orderNo, 'amount' => number_format($amount, 2), ]; }); } catch (\Exception $e) { Log::error('创建投流订单失败', [ 'message' => $e->getMessage(), 'user_id' => $userId, 'params' => $params, 'trace' => $e->getTraceAsString(), ]); throw new \Exception('创建订单失败:'.$e->getMessage()); } } /** * 生成订单号 */ private function generateOrderNo(): string { return 'FL'.date('YmdHis').mt_rand(1000, 9999); } /** * 获取价格配置 */ public function getPriceConfig() { return [ 'time_slots' => [ ['type' => 1, 'name' => '上午', 'time' => '6:00~12:00', 'price' => 4], ['type' => 2, 'name' => '下午', 'time' => '12:00~18:00', 'price' => 6], ['type' => 3, 'name' => '晚上', 'time' => '18:00~6:00', 'price' => 8], ], 'days' => [ ['days' => 1, 'price' => 12], ['days' => 7, 'price' => 53], ['days' => 15, 'price' => 99], ['days' => 30, 'price' => 160], ['days' => 90, 'price' => 430], ['days' => 180, 'price' => 760], ['days' => 365, 'price' => 1360], ], ]; } /** * 计算订单金额 */ private function calculateAmount($timeSlot, $days) { $dayPrice = 0; switch ($timeSlot) { case 1: $dayPrice = 4; break; case 2: $dayPrice = 6; break; case 3: $dayPrice = 8; break; } $totalPrice = 0; switch ($days) { case 1: $totalPrice = 12; break; case 7: $totalPrice = 53; break; case 15: $totalPrice = 99; break; case 30: $totalPrice = 160; break; case 90: $totalPrice = 430; break; case 180: $totalPrice = 760; break; case 365: $totalPrice = 1360; break; } return $totalPrice; } }