TeamProfitLogic.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Module\Farm\Logics;
  3. use App\Module\Farm\Models\FarmTeamProfit;
  4. use Illuminate\Database\Eloquent\Collection;
  5. use UCore\Helper\Logger;
  6. /**
  7. * 团队收益记录逻辑
  8. *
  9. * 提供团队收益记录数据的业务逻辑处理。
  10. */
  11. class TeamProfitLogic
  12. {
  13. /**
  14. * 获取用户的团队收益记录
  15. *
  16. * @param int $userId
  17. * @param int $limit
  18. * @return Collection
  19. */
  20. public function findByUserId(int $userId, int $limit = 100): Collection
  21. {
  22. try {
  23. return FarmTeamProfit::where('user_id', $userId)
  24. ->orderByDesc('created_at')
  25. ->limit($limit)
  26. ->get();
  27. } catch (\Exception $e) {
  28. Logger::exception('获取用户团队收益记录失败', $e, [
  29. 'user_id' => $userId,
  30. ]);
  31. return collect();
  32. }
  33. }
  34. /**
  35. * 获取团队成员产生的收益记录
  36. *
  37. * @param int $teamMemberId
  38. * @param int $limit
  39. * @return Collection
  40. */
  41. public function findByTeamMemberId(int $teamMemberId, int $limit = 100): Collection
  42. {
  43. try {
  44. return FarmTeamProfit::where('team_member_id', $teamMemberId)
  45. ->orderByDesc('created_at')
  46. ->limit($limit)
  47. ->get();
  48. } catch (\Exception $e) {
  49. Logger::exception('获取团队成员收益记录失败', $e, [
  50. 'team_member_id' => $teamMemberId,
  51. ]);
  52. return collect();
  53. }
  54. }
  55. /**
  56. * 获取指定收获记录产生的团队收益
  57. *
  58. * @param int $harvestId
  59. * @return Collection
  60. */
  61. public function findByHarvestId(int $harvestId): Collection
  62. {
  63. try {
  64. return FarmTeamProfit::where('harvest_id', $harvestId)
  65. ->orderByDesc('created_at')
  66. ->get();
  67. } catch (\Exception $e) {
  68. Logger::exception('获取收获记录团队收益失败', $e, [
  69. 'harvest_id' => $harvestId,
  70. ]);
  71. return collect();
  72. }
  73. }
  74. /**
  75. * 获取指定时间段内的团队收益记录
  76. *
  77. * @param string $startTime
  78. * @param string $endTime
  79. * @return Collection
  80. */
  81. public function findByTimeRange(string $startTime, string $endTime): Collection
  82. {
  83. try {
  84. return FarmTeamProfit::whereBetween('created_at', [$startTime, $endTime])
  85. ->orderByDesc('created_at')
  86. ->get();
  87. } catch (\Exception $e) {
  88. Logger::exception('获取时间段团队收益记录失败', $e, [
  89. 'start_time' => $startTime,
  90. 'end_time' => $endTime,
  91. ]);
  92. return collect();
  93. }
  94. }
  95. /**
  96. * 获取用户指定时间段内的团队收益记录
  97. *
  98. * @param int $userId
  99. * @param string $startTime
  100. * @param string $endTime
  101. * @return Collection
  102. */
  103. public function findByUserIdAndTimeRange(int $userId, string $startTime, string $endTime): Collection
  104. {
  105. try {
  106. return FarmTeamProfit::where('user_id', $userId)
  107. ->whereBetween('created_at', [$startTime, $endTime])
  108. ->orderByDesc('created_at')
  109. ->get();
  110. } catch (\Exception $e) {
  111. Logger::exception('获取用户时间段团队收益记录失败', $e, [
  112. 'user_id' => $userId,
  113. 'start_time' => $startTime,
  114. 'end_time' => $endTime,
  115. ]);
  116. return collect();
  117. }
  118. }
  119. /**
  120. * 清理过期的团队收益记录
  121. *
  122. * @param int $days 保留天数
  123. * @return int
  124. */
  125. public function cleanupOldLogs(int $days = 90): int
  126. {
  127. try {
  128. $date = now()->subDays($days);
  129. return FarmTeamProfit::where('created_at', '<', $date)->delete();
  130. } catch (\Exception $e) {
  131. Logger::exception('清理过期团队收益记录失败', $e, [
  132. 'days' => $days,
  133. ]);
  134. return 0;
  135. }
  136. }
  137. }