소스 검색

fixed:订单重构

刘学玺 4 달 전
부모
커밋
73350d0aaf

+ 4 - 3
app/Http/Controllers/Client/OrderController.php

@@ -163,7 +163,7 @@ class OrderController extends Controller
      *
      * @authenticated
      *
-     * @urlParam id required 订单ID. Example: 1
+     * @urlParam id required 订单ID. Example: 6
      *
      * @response {
      *   "status": "success",
@@ -283,7 +283,8 @@ class OrderController extends Controller
      *
      * @bodyParam project_id int required 项目ID. Example: 1
      * @bodyParam use_balance boolean 使用余额. Example: false
-     * @bodyParam order_id int 订单ID. Example: 1
+     * @bodyParam order_id int 订单ID. Example: 15
+     * @bodyParam payment_type string 支付类型. Example: balance
      *
      * @response {
      *   "status": "success",
@@ -292,7 +293,7 @@ class OrderController extends Controller
      */
     public function addTime(Request $request)
     {
-        $data = $request->only(['project_id', 'use_balance', 'order_id']);
+        $data = $request->only(['project_id', 'use_balance', 'order_id', 'payment_type']);
 
         return $this->service->createOrder(Auth::user()->id, $data);
     }

+ 63 - 0
app/Http/Controllers/Coach/OrderController

@@ -0,0 +1,63 @@
+<?php
+
+namespace App\Http\Controllers\Client;
+
+use App\Http\Controllers\Controller;
+use App\Services\Client\OrderService;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Auth;
+
+/**
+ * @group 技师端
+ *
+ * 订单相关的API接口
+ */
+class OrderController extends Controller
+{
+    // protected OrderService $service;
+
+    // public function __construct(OrderService $service)
+    // {
+    //     $this->service = $service;
+    // }
+
+    /**
+     * [订单]获取可抢订单列表
+     *
+     * 获取可抢订单列表
+     *
+     * @authenticated
+     *
+     * @queryParam area_code string required 区划代码. Example: 370602
+     * @queryParam page int 页码. Example: 1
+     * @queryParam per_page int 每页数量. Example: 10
+     *
+     * @response {
+     *   "data": [
+     *     {
+     *       "id": 1,
+     *       "order_no": "202403210001",
+     *       "project_name": "项目名称",
+     *       "project_duration": 60,
+     *       "project_price": "188.00",
+     *       "address": "山东省烟台市芝罘区",
+     *       "distance": 2.5,
+     *       "service_time": "2024-03-21 10:00:00",
+     *       "created_at": "2024-03-21 09:30:00"
+     *     }
+     *   ],
+     *   "meta": {
+     *     "total": 100,
+     *     "per_page": 10,
+     *     "current_page": 1,
+     *     "last_page": 10
+     *   }
+     * }
+     */
+    public function getGrabList(Request $request)
+    {
+        $params = $request->only(['area_code', 'page', 'per_page']);
+
+        return $this->service->getGrabList(Auth::user()->id, $params);
+    }
+}

+ 28 - 23
app/Services/Client/OrderService.php

@@ -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,

+ 18 - 0
app/Services/Coach/OrderService.php

@@ -0,0 +1,18 @@
+<?php
+
+namespace App\Services\Client;
+
+class OrderService
+{
+    // protected AgentService $agentService;
+
+    // protected ProjectService $projectService;
+
+    // public function __construct(
+    //     AgentService $agentService,
+    //     ProjectService $projectService
+    // ) {
+    //     $this->agentService = $agentService;
+    //     $this->projectService = $projectService;
+    // }
+}