Browse Source

fix:用户端-订单-列表-添加tab筛选

刘学玺 3 months ago
parent
commit
c3bf9c8418

+ 27 - 0
app/Enums/OrderStatus.php

@@ -102,6 +102,24 @@ enum OrderStatus: int
      */
     case REJECTED = 15;    // 技师拒单
 
+    /**
+     * 订单状态:支付失败
+     * 支付失败
+     */
+    case PAYMENT_FAILED = 16;  // 支付失败
+
+    /**
+     * 订单状态:退款中
+     * 退款进行中
+     */
+    case REFUNDING = 17;       // 退款中
+
+    /**
+     * 订单状态:退款失败
+     * 退款失败
+     */
+    case REFUND_FAILED = 18;   // 退款失败
+
     /**
      * 获取状态的显示文本
      * 将状态值转换为对应的中文描述
@@ -126,6 +144,9 @@ enum OrderStatus: int
             self::CANCELLED => '已取消',      // 订单取消
             self::REFUNDED => '已退款',       // 订单已退款
             self::REJECTED => '已拒单',      // 技师拒单
+            self::PAYMENT_FAILED => '支付失败',  // 支付失败
+            self::REFUNDING => '退款中',       // 退款中
+            self::REFUND_FAILED => '退款失败',   // 退款失败
         };
     }
 
@@ -174,6 +195,9 @@ enum OrderStatus: int
             self::CANCELLED->value => self::CANCELLED,
             self::REFUNDED->value => self::REFUNDED,
             self::REJECTED->value => self::REJECTED,
+            self::PAYMENT_FAILED->value => self::PAYMENT_FAILED,
+            self::REFUNDING->value => self::REFUNDING,
+            self::REFUND_FAILED->value => self::REFUND_FAILED,
             default => null
         };
     }
@@ -211,6 +235,9 @@ enum OrderStatus: int
             self::CANCELLED->value => self::CANCELLED->label(),
             self::REFUNDED->value => self::REFUNDED->label(),
             self::REJECTED->value => self::REJECTED->label(),
+            self::PAYMENT_FAILED->value => self::PAYMENT_FAILED->label(),
+            self::REFUNDING->value => self::REFUNDING->label(),
+            self::REFUND_FAILED->value => self::REFUND_FAILED->label(),
         ];
     }
 }

+ 12 - 2
app/Http/Controllers/Client/OrderController.php

@@ -164,14 +164,24 @@ class OrderController extends Controller
      *
      * @authenticated
      *
+     * @queryParam tab int 订单标签页 Example: 0
+     * 标签页说明:
+     * 0: 全部订单
+     * 1: 待付款 (状态: 1,16)
+     * 2: 进行中 (状态: 2,3,4,5,6,7,8,9,10)
+     * 3: 已完成 (状态: 12)
+     * 4: 待评价 (状态: 11)
+     * 5: 已取消 (状态: 13,14,15,17,18)
+     *
      * @response {
      *   "status": "success",
      *   "data": []
      * }
      */
-    public function list()
+    public function list(Request $request)
     {
-        return $this->success($this->service->getOrderList(Auth::user()->id));
+        $tab = $request->input('tab', 0);
+        return $this->success($this->service->getOrderList(Auth::user()->id, $tab));
     }
 
     /**

+ 82 - 78
app/Services/Client/OrderService.php

@@ -1046,43 +1046,88 @@ readonly class OrderService
 
     /**
      * 获取订单列表
-     *
-     * @param  int  $userId  用户ID
-     * @param array{
-     *     state: ?string,
-     *     start_date: ?string,
-     *     end_date: ?string,
-     *     project_id: ?int,
-     *     coach_id: ?int,
-     *     order_no: ?string,
-     *     per_page: ?int,
-     *     page: ?int
-     * } $filters 过滤条件
-     *
-     * @throws \Exception
      */
-    public function getOrderList(int $userId, array $filters = [])
+    public function getOrderList(int $userId, int $tab = 0)
     {
-        try {
+        // 1. 验证用户
+        $user = $this->validateUserForQuery($userId);
 
-            // 1. 验证用户
-            $user = $this->validateUserForQuery($userId);
+        // 2. 构建基础查询
+        $query = $this->buildOrderListQuery($user);
 
-            // 2. 构建基础查询
-            $query = $this->buildOrderListQuery($user);
+        // 3. 获取状态过滤条件
+        $states = $this->getOrderStatesByTab($tab);
 
-            // 3. 应用过滤条件
-            $this->applyOrderFilters($query, $filters);
+        // 4. 加载关联数据并分页
+        return $this->paginateOrderList($query, [
+            'states' => $states
+        ]);
+    }
 
-            // 4. 加载关联数据并分页
-            return $this->paginateOrderList($query, $filters);
-        } catch (Exception $e) {
-            $this->logError('获取订单列表失败', $e, [
-                'user_id' => $userId,
-                'filters' => $filters,
-            ]);
-            throw $e;
+    /**
+     * 分页获取订单列表
+     *
+     * @param \Illuminate\Database\Eloquent\Builder $query
+     * @param array $filters 过滤条件
+     */
+    private function paginateOrderList($query, array $filters = [])
+    {
+        // 从请求中获取分页参数
+        $perPage = request('per_page', 10);
+        $page = request('page', 1);
+
+        // 应用状态过滤
+        if (!empty($filters['states'])) {
+            $query->whereIn('state', $filters['states']);
         }
+
+        // 按创建时间降序排序并分页
+        $paginatedOrders = $query->orderBy('created_at', 'desc')
+            ->paginate(
+                perPage: $perPage,
+                page: $page
+            );
+
+        // 格式化订单数据
+        $formattedOrders = $paginatedOrders->map(function ($order) {
+            return $this->formatOrderListItem($order);
+        });
+
+        return [
+            'items' => $formattedOrders,
+            'total' => $paginatedOrders->total(),
+        ];
+    }
+
+    /**
+     * 根据标签页获取订单状态
+     */
+    private function getOrderStatesByTab(int $tab): array
+    {
+        return match ($tab) {
+            1 => [OrderStatus::CREATED->value, OrderStatus::PAYMENT_FAILED->value], // 待付款
+            2 => [ // 进行中
+                OrderStatus::PAID->value,
+                OrderStatus::ASSIGNED->value,
+                OrderStatus::ACCEPTED->value,
+                OrderStatus::DEPARTED->value,
+                OrderStatus::ARRIVED->value,
+                OrderStatus::SERVICE_START->value,
+                OrderStatus::SERVICING->value,
+                OrderStatus::SERVICE_END->value,
+                OrderStatus::LEAVING->value,
+            ],
+            3 => [OrderStatus::COMPLETED->value], // 已完成
+            4 => [OrderStatus::LEFT->value], // 待评价
+            5 => [ // 已取消
+                OrderStatus::CANCELLED->value,
+                OrderStatus::REFUNDED->value,
+                OrderStatus::REJECTED->value,
+                OrderStatus::REFUNDING->value,
+                OrderStatus::REFUND_FAILED->value,
+            ],
+            default => [], // 全部订单
+        };
     }
 
     /**
@@ -1116,28 +1161,6 @@ readonly class OrderService
             ]);
     }
 
-    /**
-     * 分页获取订���列表
-     */
-    private function paginateOrderList($query, array $filters)
-    {
-
-        $paginatedOrders = $query->orderBy('created_at', 'desc')
-            ->paginate(
-                perPage: $filters['per_page'] ?? 10,
-                page: $filters['page'] ?? 1
-            );
-
-        $formattedOrders = $paginatedOrders->map(function ($order) {
-            return $this->formatOrderListItem($order);
-        });
-
-        return [
-            'items' => $formattedOrders,
-            'total' => $paginatedOrders->total(),
-        ];
-    }
-
     /**
      * 格式化订单列表项
      */
@@ -1147,7 +1170,7 @@ readonly class OrderService
             'order_id' => $order->id,
             'order_no' => $order->order_no,
             'state' => $order->state,
-            'state_description' => $this->getOrderStateDescription($order->state),
+            'state_text' => OrderStatus::fromValue($order->state)->label(),
             'total_amount' => $order->total_amount,
             'service_time' => $order->service_time,
             'created_at' => $order->created_at->toDateTimeString(),
@@ -1239,7 +1262,7 @@ readonly class OrderService
             'order_id' => $order->id,
             'order_no' => $order->order_no,
             'state' => $order->state,
-            'state_description' => $this->getOrderStateDescription($order->state),
+            'state_text' => OrderStatus::fromValue($order->state)->label(),
             'total_amount' => $order->total_amount,
             'balance_amount' => $order->balance_amount,
             'pay_amount' => $order->pay_amount,
@@ -1290,9 +1313,9 @@ readonly class OrderService
      */
     private function applyOrderFilters($query, array $filters): void
     {
-        // 按状态筛选
-        if (! empty($filters['state'])) {
-            $query->where('state', $filters['state']);
+        // 按状态数组筛选
+        if (!empty($filters['states'])) {
+            $query->whereIn('state', $filters['states']);
         }
 
         // 按时间范围筛选
@@ -1319,25 +1342,6 @@ readonly class OrderService
         }
     }
 
-    /**
-     * 获取订单状态描述
-     */
-    private function getOrderStateDescription(string $state): string
-    {
-        return match ($state) {
-            OrderStatus::CREATED->value => '待支付',
-            OrderStatus::PAID->value => '待接单',
-            OrderStatus::ACCEPTED->value => '已接单',
-            OrderStatus::DEPARTED->value => '已出发',
-            OrderStatus::ARRIVED->value => '已到达',
-            OrderStatus::SERVICING->value => '服务中',
-            OrderStatus::SERVICE_END->value => '已完成',
-            OrderStatus::CANCELLED->value => '已取消',
-            OrderStatus::REFUNDED->value => '已退款',
-            default => '未知状态',
-        };
-    }
-
     /**
      * 订单退款
      *
@@ -1602,7 +1606,7 @@ readonly class OrderService
             // 2. 获取计费规则
             $feeRules = $this->getDeliveryFeeRules($coachId, $agentId);
 
-            // 3. ��算路费
+            // 3. 算路费
             return $this->calculateFeeByRules($distance, $feeRules);
         } catch (Exception $e) {
             $this->logError('计算路费失败', $e, [
@@ -2122,7 +2126,7 @@ readonly class OrderService
             $order = Order::where('id', $orderId)
                 ->whereIn('state', [OrderStatus::CREATED->value])
                 ->firstOrFail();
-            // 查询抢单池���
+            // 查询抢单池表
             $grabList = $order->grabRecords()->with(['coach.info'])->get();
 
             // 格式化返回数据