|
@@ -114,7 +114,7 @@ class OrderService
|
|
|
->firstOrFail();
|
|
|
|
|
|
$project = Project::where('id', $data['project_id'])
|
|
|
- ->where('state', 'enable')
|
|
|
+ ->where('state', ProjectStatus::OPEN->value())
|
|
|
->firstOrFail();
|
|
|
|
|
|
// 2. 订单类型判断
|
|
@@ -122,25 +122,27 @@ class OrderService
|
|
|
|
|
|
// 关键操作:验证必要参数
|
|
|
abort_if(empty($data['project_id']), 400, '项目ID不能为空');
|
|
|
- abort_if(empty($data['service_time']), 400, '服务时间不能为空');
|
|
|
abort_if($orderType == 'normal' && empty($data['coach_id']), 400, '技师ID不能为空');
|
|
|
abort_if($orderType == 'normal' && empty($data['address_id']), 400, '地址ID不能为空');
|
|
|
|
|
|
- // 3. 验证地址
|
|
|
- $address = $user->addresses()
|
|
|
- ->where('id', $data['address_id'])
|
|
|
- ->firstOrFail();
|
|
|
-
|
|
|
+ // 3. 验证技师
|
|
|
// 4. 根据订单类型处理
|
|
|
if ($orderType == 'normal') {
|
|
|
- $coach = $this->validateCoach($data['coach_id']);
|
|
|
+ $this->validateCoach($data['coach_id']);
|
|
|
} else {
|
|
|
$originalOrder = $this->getOriginalOrder($user, $data['order_id']);
|
|
|
- $coach = $this->validateCoach($originalOrder->coach_id);
|
|
|
+
|
|
|
+ $data['address_id'] = $originalOrder->address_id;
|
|
|
+
|
|
|
+ $this->validateCoach($originalOrder->coach_id);
|
|
|
abort_if(! in_array($originalOrder->state, ['service_ing', 'service_end']), 400, '原订单状态不允许加钟');
|
|
|
$data = $this->prepareAddTimeData($originalOrder, $data);
|
|
|
}
|
|
|
|
|
|
+ $address = $user->addresses()
|
|
|
+ ->where('id', $data['address_id'])
|
|
|
+ ->firstOrFail();
|
|
|
+
|
|
|
// 5. 计算订单金额
|
|
|
$amounts = $this->calculateOrderAmount(
|
|
|
$userId,
|
|
@@ -153,11 +155,11 @@ class OrderService
|
|
|
|
|
|
// 6. 验证金额和余额
|
|
|
abort_if($amounts['total_amount'] <= 0, 400, '订单金额异常');
|
|
|
+
|
|
|
if ($data['payment_type'] == 'balance') {
|
|
|
$wallet = $user->wallet;
|
|
|
abort_if($wallet->available_balance < $amounts['balance_amount'], 400, '可用余额不足');
|
|
|
}
|
|
|
-
|
|
|
// 7. 创建订单记录
|
|
|
$order = $this->createOrderRecord($userId, $data, $orderType, $address, $data['payment_type'], (object) $amounts);
|
|
|
|
|
@@ -178,9 +180,9 @@ class OrderService
|
|
|
{
|
|
|
$coach = CoachUser::where('id', $coachId)
|
|
|
->where('state', 'enable')
|
|
|
- ->whereHas('info', fn($q) => $q->where('state', 'approved'))
|
|
|
- ->whereHas('qual', fn($q) => $q->where('state', 'approved'))
|
|
|
- ->whereHas('real', fn($q) => $q->where('state', 'approved'))
|
|
|
+ ->whereHas('info', fn ($q) => $q->where('state', 'approved'))
|
|
|
+ ->whereHas('qual', fn ($q) => $q->where('state', 'approved'))
|
|
|
+ ->whereHas('real', fn ($q) => $q->where('state', 'approved'))
|
|
|
->firstOrFail();
|
|
|
|
|
|
return $coach;
|
|
@@ -733,10 +735,10 @@ class OrderService
|
|
|
try {
|
|
|
// 1. 校验技师
|
|
|
$coach = CoachUser::where('state', 'enable')
|
|
|
- ->whereHas('info', fn($q) => $q->where('state', 'approved'))
|
|
|
- ->whereHas('real', fn($q) => $q->where('state', 'approved'))
|
|
|
- ->whereHas('qual', fn($q) => $q->where('state', 'approved'))
|
|
|
- ->with(['projects' => fn($q) => $q->where('project_id', $projectId)])
|
|
|
+ ->whereHas('info', fn ($q) => $q->where('state', 'approved'))
|
|
|
+ ->whereHas('real', fn ($q) => $q->where('state', 'approved'))
|
|
|
+ ->whereHas('qual', fn ($q) => $q->where('state', 'approved'))
|
|
|
+ ->with(['projects' => fn ($q) => $q->where('project_id', $projectId)])
|
|
|
->find($coachId);
|
|
|
|
|
|
abort_if(! $coach, 404, '技师不存在或状态异常');
|
|
@@ -764,7 +766,7 @@ class OrderService
|
|
|
? bcmul($fee, '2', 2)
|
|
|
: $fee;
|
|
|
} catch (Exception $e) {
|
|
|
- Log::error(__CLASS__ . '->' . __FUNCTION__ . '计算路费失败:', [
|
|
|
+ Log::error(__CLASS__.'->'.__FUNCTION__.'计算路费失败:', [
|
|
|
'message' => $e->getMessage(),
|
|
|
'coach_id' => $coachId,
|
|
|
'project_id' => $projectId,
|
|
@@ -840,6 +842,7 @@ class OrderService
|
|
|
int $lng = 0
|
|
|
): array {
|
|
|
try {
|
|
|
+
|
|
|
// 1. 参数校验
|
|
|
$user = MemberUser::find($userId);
|
|
|
abort_if(! $user || $user->state != 'enable', 404, '用户不存在或状态异常');
|
|
@@ -848,7 +851,6 @@ class OrderService
|
|
|
$coach = $this->validateCoach($coachId);
|
|
|
|
|
|
abort_if(! $coach, 404, '技师不存在或状态异常');
|
|
|
-
|
|
|
$coachProject = $coach->projects()
|
|
|
->where('state', 'enable')
|
|
|
->where('project_id', $projectId)
|
|
@@ -856,16 +858,19 @@ class OrderService
|
|
|
|
|
|
abort_if(! $coachProject, 404, '技师项目不存在');
|
|
|
// 3. 查询基础项目
|
|
|
-
|
|
|
$project = Project::where('id', $projectId)
|
|
|
->where('state', ProjectStatus::OPEN->value())
|
|
|
->first();
|
|
|
|
|
|
abort_if(! $project, 404, '项目不存在或状态异常');
|
|
|
+
|
|
|
// 4. 计算距离
|
|
|
if (floatval($distance) <= 0) {
|
|
|
- $address = $addressId && $user->addresses()->find($addressId) ?? ['latitude' => $lat, 'longitude' => $lng];
|
|
|
+
|
|
|
+ $address = $user->addresses()->find($addressId) ?? ['latitude' => $lat, 'longitude' => $lng];
|
|
|
+
|
|
|
$coachService = app(CoachService::class);
|
|
|
+
|
|
|
$coachDetail = $coachService->getCoachDetail($coachId, $address['latitude'], $address['longitude']);
|
|
|
$distance = $coachDetail['distance'] ?? 0;
|
|
|
}
|
|
@@ -910,7 +915,7 @@ class OrderService
|
|
|
'delivery_fee' => $deliveryFee,
|
|
|
];
|
|
|
} catch (Exception $e) {
|
|
|
- Log::error(__CLASS__ . '->' . __FUNCTION__ . '计算订单金额失败:', [
|
|
|
+ Log::error(__CLASS__.'->'.__FUNCTION__.'计算订单金额失败:', [
|
|
|
'message' => $e->getMessage(),
|
|
|
'user_id' => $userId,
|
|
|
'project_id' => $projectId,
|
|
@@ -1091,7 +1096,7 @@ class OrderService
|
|
|
WalletPaymentRecord::create([
|
|
|
'order_id' => $order->id,
|
|
|
'wallet_id' => $wallet->id,
|
|
|
- 'payment_no' => 'balance_' . $order->id,
|
|
|
+ 'payment_no' => 'balance_'.$order->id,
|
|
|
'payment_method' => 'balance',
|
|
|
'total_amount' => $order->balance_amount,
|
|
|
'actual_amount' => 0,
|