WalletService.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace App\Services\Coach;
  3. use App\Models\MemberUser;
  4. use App\Models\WalletTransRecord;
  5. use Illuminate\Support\Facades\Log;
  6. class WalletService
  7. {
  8. /**
  9. * 获取技师钱包信息
  10. *
  11. * @param int $userId 技师用户ID
  12. */
  13. public function getWallet(int $userId): array
  14. {
  15. try {
  16. // 加载用户和技师信息
  17. $user = MemberUser::with(['coach', 'coach.wallet'])->findOrFail($userId);
  18. abort_if(! $user->coach, 404, '技师信息不存在');
  19. // 获取技师钱包
  20. $wallet = $user->coach->wallet;
  21. // 获取钱包流水统计
  22. $statistics = $this->getWalletStatistics($wallet->id);
  23. return [
  24. 'available_balance' => number_format($wallet->available_balance, 2, '.', ''), // 可用余额
  25. 'frozen_amount' => number_format($wallet->frozen_amount, 2, '.', ''), // 冻结金额
  26. 'total_income' => number_format($statistics['total_income'], 2, '.', ''), // 累计收入
  27. 'total_withdraw' => number_format($statistics['total_withdraw'], 2, '.', ''), // 累计支出
  28. 'today_income' => number_format($statistics['today_income'], 2, '.', ''), // 今日收入
  29. 'month_income' => number_format($statistics['month_income'], 2, '.', ''), // 本月收入
  30. 'last_month_income' => number_format($statistics['last_month_income'], 2, '.', ''), // 上月收入
  31. ];
  32. } catch (\Exception $e) {
  33. Log::error('获取技师钱包信息失败', [
  34. 'user_id' => $userId,
  35. 'error' => $e->getMessage(),
  36. 'file' => $e->getFile(),
  37. 'line' => $e->getLine(),
  38. ]);
  39. throw $e;
  40. }
  41. }
  42. /**
  43. * 获取钱包流水记录
  44. *
  45. * @param int $userId 技师用户ID
  46. * @param array $params 查询参数
  47. */
  48. public function getWalletRecords(int $userId, array $params): array
  49. {
  50. try {
  51. // 加载用户和钱包信息
  52. $user = MemberUser::with(['wallet'])->findOrFail($userId);
  53. abort_if(! $user->wallet, 404, '钱包信息不存在');
  54. // 构建查询
  55. $query = WalletTransRecord::where('wallet_id', $user->wallet->id)
  56. ->when(isset($params['type']), function ($query) use ($params) {
  57. return $query->where('type', $params['type']);
  58. })
  59. ->when(isset($params['start_date']), function ($query) use ($params) {
  60. return $query->where('created_at', '>=', $params['start_date']);
  61. })
  62. ->when(isset($params['end_date']), function ($query) use ($params) {
  63. return $query->where('created_at', '<=', $params['end_date']);
  64. })
  65. ->orderBy('created_at', 'desc');
  66. // 分页获取数据
  67. $records = $query->paginate(
  68. $params['per_page'] ?? 10,
  69. ['*'],
  70. 'page',
  71. $params['page'] ?? 1
  72. );
  73. return [
  74. 'items' => $records->items(),
  75. 'total' => $records->total(),
  76. ];
  77. } catch (\Exception $e) {
  78. Log::error('获取钱包流水记录失败', [
  79. 'user_id' => $userId,
  80. 'params' => $params,
  81. 'error' => $e->getMessage(),
  82. 'file' => $e->getFile(),
  83. 'line' => $e->getLine(),
  84. ]);
  85. throw $e;
  86. }
  87. }
  88. /**
  89. * 获取钱包流水统计
  90. *
  91. * @param int $walletId 钱包ID
  92. */
  93. private function getWalletStatistics(int $walletId): array
  94. {
  95. try {
  96. // 计算总收入和总支出
  97. $totals = WalletTransRecord::where('wallet_id', $walletId)
  98. ->selectRaw('
  99. SUM(CASE WHEN amount > 0 THEN amount ELSE 0 END) as total_income,
  100. ABS(SUM(CASE WHEN amount < 0 THEN amount ELSE 0 END)) as total_withdraw
  101. ')
  102. ->first();
  103. // 计算今日收入
  104. $todayIncome = WalletTransRecord::where('wallet_id', $walletId)
  105. ->where('amount', '>', 0)
  106. ->whereDate('created_at', today())
  107. ->sum('amount');
  108. // 计算本月收入
  109. $monthIncome = WalletTransRecord::where('wallet_id', $walletId)
  110. ->where('amount', '>', 0)
  111. ->whereYear('created_at', now()->year)
  112. ->whereMonth('created_at', now()->month)
  113. ->sum('amount');
  114. // 计算上月收入
  115. $lastMonthIncome = WalletTransRecord::where('wallet_id', $walletId)
  116. ->where('amount', '>', 0)
  117. ->whereYear('created_at', now()->subMonth()->year)
  118. ->whereMonth('created_at', now()->subMonth()->month)
  119. ->sum('amount');
  120. return [
  121. 'total_income' => $totals->total_income ?? 0,
  122. 'total_withdraw' => $totals->total_withdraw ?? 0,
  123. 'today_income' => $todayIncome,
  124. 'month_income' => $monthIncome,
  125. 'last_month_income' => $lastMonthIncome,
  126. ];
  127. } catch (\Exception $e) {
  128. Log::error('获取钱包统计信息失败', [
  129. 'wallet_id' => $walletId,
  130. 'error' => $e->getMessage(),
  131. ]);
  132. return [
  133. 'total_income' => 0,
  134. 'total_withdraw' => 0,
  135. 'today_income' => 0,
  136. 'month_income' => 0,
  137. 'last_month_income' => 0,
  138. ];
  139. }
  140. }
  141. }