소스 검색

feat:技师端-获取钱包流水记录

刘学玺 4 달 전
부모
커밋
f84998c34a
2개의 변경된 파일123개의 추가작업 그리고 11개의 파일을 삭제
  1. 10 0
      app/Http/Controllers/Coach/WalletController.php
  2. 113 11
      app/Services/Coach/WalletService.php

+ 10 - 0
app/Http/Controllers/Coach/WalletController.php

@@ -58,6 +58,11 @@ class WalletController extends Controller
      * @queryParam type integer 记录类型(1:收入 2:支出) Example: 1
      * @queryParam start_date date 开始日期 Example: 2024-01-01
      * @queryParam end_date date 结束日期 Example: 2024-03-21
+     * @queryParam min_amount numeric 最小金额 Example: 100
+     * @queryParam max_amount numeric 最大金额 Example: 1000
+     * @queryParam status integer 交易状态(1:成功 2:处理中 3:失败) Example: 1
+     * @queryParam sort_field string 排序字段(created_at, amount, balance) Example: created_at
+     * @queryParam sort_order string 排序顺序(asc, desc) Example: desc
      * @queryParam page integer 页码 Example: 1
      * @queryParam per_page integer 每页数量 Example: 10
      *
@@ -82,6 +87,11 @@ class WalletController extends Controller
             'type' => 'nullable|integer|in:1,2',
             'start_date' => 'nullable|date',
             'end_date' => 'nullable|date|after_or_equal:start_date',
+            'min_amount' => 'nullable|numeric|min:0',
+            'max_amount' => 'nullable|numeric|gt:min_amount',
+            'status' => 'nullable|integer|in:1,2,3',
+            'sort_field' => 'nullable|string|in:created_at,amount,balance',
+            'sort_order' => 'nullable|string|in:asc,desc',
             'page' => 'nullable|integer|min:1',
             'per_page' => 'nullable|integer|min:1|max:50',
         ]);

+ 113 - 11
app/Services/Coach/WalletService.php

@@ -56,34 +56,97 @@ class WalletService
     public function getWalletRecords(int $userId, array $params): array
     {
         try {
-            // 加载用户和钱包信息
-            $user = MemberUser::with(['wallet'])->findOrFail($userId);
-            abort_if(! $user->wallet, 404, '钱包信息不存在');
+            // 加载用户和技师信息(优化关联加载)
+            $user = MemberUser::with(['coach', 'coach.wallet'])->findOrFail($userId);
+            abort_if(! $user->coach, 404, '技师信息不存在');
+            abort_if(! $user->coach->wallet, 404, '钱包信息不存在');
 
             // 构建查询
-            $query = WalletTransRecord::where('wallet_id', $user->wallet->id)
+            $query = WalletTransRecord::where('wallet_id', $user->coach->wallet->id)
+                // 修复参数名称错误
                 ->when(isset($params['type']), function ($query) use ($params) {
-                    return $query->where('type', $params['type']);
+                    return $query->where('trans_type', $params['type']);
                 })
+                // 优化日期查询
                 ->when(isset($params['start_date']), function ($query) use ($params) {
-                    return $query->where('created_at', '>=', $params['start_date']);
+                    return $query->whereDate('created_at', '>=', $params['start_date']);
                 })
                 ->when(isset($params['end_date']), function ($query) use ($params) {
-                    return $query->where('created_at', '<=', $params['end_date']);
+                    return $query->whereDate('created_at', '<=', $params['end_date']);
+                })
+                // 添加金额范围筛选
+                ->when(isset($params['min_amount']), function ($query) use ($params) {
+                    return $query->where('amount', '>=', $params['min_amount']);
                 })
-                ->orderBy('created_at', 'desc');
+                ->when(isset($params['max_amount']), function ($query) use ($params) {
+                    return $query->where('amount', '<=', $params['max_amount']);
+                })
+                // 添加交易状态筛选
+                ->when(isset($params['status']), function ($query) use ($params) {
+                    return $query->where('status', $params['status']);
+                })
+                // 添加排序选项
+                ->when(
+                    isset($params['sort_field']) && isset($params['sort_order']),
+                    function ($query) use ($params) {
+                        return $query->orderBy(
+                            $params['sort_field'],
+                            $params['sort_order'] === 'desc' ? 'desc' : 'asc'
+                        );
+                    },
+                    function ($query) {
+                        return $query->orderBy('created_at', 'desc');
+                    }
+                );
 
-            // 分页获取数据
+            // 分页获取数据(添加字段选择)
             $records = $query->paginate(
                 $params['per_page'] ?? 10,
-                ['*'],
+                [
+                    'id',
+                    'trans_no',
+                    'trans_type',
+                    'amount',
+                    'balance',
+                    'owner_type',
+                    'owner_id',
+                    'remark',
+                    'status',
+                    'created_at',
+                ],
                 'page',
                 $params['page'] ?? 1
             );
 
+            // TODO: 处理格式化数据存在的枚举映射
+
+            // 格式化数据
+            $items = collect($records->items())->map(function ($record) {
+                return [
+                    'id' => $record->id,
+                    'trans_no' => $record->trans_no,
+                    'trans_type' => $this->formatTransType($record->trans_type),
+                    'amount' => number_format($record->amount, 2, '.', ''),
+                    'balance' => number_format($record->balance, 2, '.', ''),
+                    'owner_type' => $this->formatOwnerType($record->owner_type),
+                    'owner_id' => $record->owner_id,
+                    'remark' => $record->remark,
+                    'status' => $this->formatStatus($record->status),
+                    'created_at' => $record->created_at->format('Y-m-d H:i:s'),
+                ];
+            });
+
+            // 添加汇总信息
+            $summary = [
+                'total_income' => $query->where('amount', '>', 0)->sum('amount'),
+                'total_expense' => abs($query->where('amount', '<', 0)->sum('amount')),
+                'record_count' => $records->total(),
+            ];
+
             return [
-                'items' => $records->items(),
+                'items' => $items,
                 'total' => $records->total(),
+                'summary' => $summary,
             ];
 
         } catch (\Exception $e) {
@@ -98,6 +161,45 @@ class WalletService
         }
     }
 
+    /**
+     * 格式化交易类型
+     */
+    private function formatTransType(int $type): string
+    {
+        return match ($type) {
+            1 => '收入',
+            2 => '支出',
+            default => '未知',
+        };
+    }
+
+    /**
+     * 格式化来源类型
+     */
+    private function formatOwnerType(string $type): string
+    {
+        return match ($type) {
+            'order' => '订单',
+            'withdraw' => '提现',
+            'refund' => '退款',
+            'system' => '系统',
+            default => '其他',
+        };
+    }
+
+    /**
+     * 格式化状态
+     */
+    private function formatStatus(int $status): string
+    {
+        return match ($status) {
+            1 => '成功',
+            2 => '处理中',
+            3 => '失败',
+            default => '未知',
+        };
+    }
+
     /**
      * 获取钱包流水统计
      *