findOrFail($userId); abort_if(! $user->coach, 404, '技师信息不存在'); // 获取技师钱包 $wallet = $user->coach->wallet; // 获取钱包流水统计 $statistics = $this->getWalletStatistics($wallet->id); return [ 'available_balance' => number_format($wallet->available_balance, 2, '.', ''), // 可用余额 'frozen_amount' => number_format($wallet->frozen_amount, 2, '.', ''), // 冻结金额 'total_income' => number_format($statistics['total_income'], 2, '.', ''), // 累计收入 'total_withdraw' => number_format($statistics['total_withdraw'], 2, '.', ''), // 累计支出 'today_income' => number_format($statistics['today_income'], 2, '.', ''), // 今日收入 'month_income' => number_format($statistics['month_income'], 2, '.', ''), // 本月收入 'last_month_income' => number_format($statistics['last_month_income'], 2, '.', ''), // 上月收入 ]; } catch (\Exception $e) { Log::error('获取技师钱包信息失败', [ 'user_id' => $userId, 'error' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine(), ]); throw $e; } } /** * 获取钱包流水记录 * * @param int $userId 技师用户ID * @param array $params 查询参数 */ public function getWalletRecords(int $userId, array $params): array { try { // 加载用户和钱包信息 $user = MemberUser::with(['wallet'])->findOrFail($userId); abort_if(! $user->wallet, 404, '钱包信息不存在'); // 构建查询 $query = WalletTransRecord::where('wallet_id', $user->wallet->id) ->when(isset($params['type']), function ($query) use ($params) { return $query->where('type', $params['type']); }) ->when(isset($params['start_date']), function ($query) use ($params) { return $query->where('created_at', '>=', $params['start_date']); }) ->when(isset($params['end_date']), function ($query) use ($params) { return $query->where('created_at', '<=', $params['end_date']); }) ->orderBy('created_at', 'desc'); // 分页获取数据 $records = $query->paginate( $params['per_page'] ?? 10, ['*'], 'page', $params['page'] ?? 1 ); return [ 'items' => $records->items(), 'total' => $records->total(), ]; } catch (\Exception $e) { Log::error('获取钱包流水记录失败', [ 'user_id' => $userId, 'params' => $params, 'error' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine(), ]); throw $e; } } /** * 获取钱包流水统计 * * @param int $walletId 钱包ID */ private function getWalletStatistics(int $walletId): array { try { // 计算总收入和总支出 $totals = WalletTransRecord::where('wallet_id', $walletId) ->selectRaw(' SUM(CASE WHEN amount > 0 THEN amount ELSE 0 END) as total_income, ABS(SUM(CASE WHEN amount < 0 THEN amount ELSE 0 END)) as total_withdraw ') ->first(); // 计算今日收入 $todayIncome = WalletTransRecord::where('wallet_id', $walletId) ->where('amount', '>', 0) ->whereDate('created_at', today()) ->sum('amount'); // 计算本月收入 $monthIncome = WalletTransRecord::where('wallet_id', $walletId) ->where('amount', '>', 0) ->whereYear('created_at', now()->year) ->whereMonth('created_at', now()->month) ->sum('amount'); // 计算上月收入 $lastMonthIncome = WalletTransRecord::where('wallet_id', $walletId) ->where('amount', '>', 0) ->whereYear('created_at', now()->subMonth()->year) ->whereMonth('created_at', now()->subMonth()->month) ->sum('amount'); return [ 'total_income' => $totals->total_income ?? 0, 'total_withdraw' => $totals->total_withdraw ?? 0, 'today_income' => $todayIncome, 'month_income' => $monthIncome, 'last_month_income' => $lastMonthIncome, ]; } catch (\Exception $e) { Log::error('获取钱包统计信息失败', [ 'wallet_id' => $walletId, 'error' => $e->getMessage(), ]); return [ 'total_income' => 0, 'total_withdraw' => 0, 'today_income' => 0, 'month_income' => 0, 'last_month_income' => 0, ]; } } }