FarmTeamProfitRepository.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace App\Module\Farm\Repositories;
  3. use App\Module\Farm\Models\FarmTeamProfit;
  4. use Dcat\Admin\Repositories\EloquentRepository;
  5. use Illuminate\Database\Eloquent\Collection;
  6. /**
  7. * 团队收益记录仓库
  8. *
  9. * 提供团队收益记录数据的访问和操作功能。
  10. * 该类是团队收益记录模块与后台管理系统的桥梁,用于处理团队收益记录数据的CRUD操作。
  11. */
  12. class FarmTeamProfitRepository extends EloquentRepository
  13. {
  14. /**
  15. * 模型类名
  16. *
  17. * @var string
  18. */
  19. protected $eloquentClass = FarmTeamProfit::class;
  20. /**
  21. * 获取用户的团队收益记录
  22. *
  23. * @param int $userId
  24. * @param int $limit
  25. * @return Collection
  26. */
  27. public function findByUserId(int $userId, int $limit = 100): Collection
  28. {
  29. return FarmTeamProfit::where('user_id', $userId)
  30. ->orderByDesc('created_at')
  31. ->limit($limit)
  32. ->get();
  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. return FarmTeamProfit::where('team_member_id', $teamMemberId)
  44. ->orderByDesc('created_at')
  45. ->limit($limit)
  46. ->get();
  47. }
  48. /**
  49. * 获取指定收获记录产生的团队收益
  50. *
  51. * @param int $harvestId
  52. * @return Collection
  53. */
  54. public function findByHarvestId(int $harvestId): Collection
  55. {
  56. return FarmTeamProfit::where('harvest_id', $harvestId)
  57. ->orderByDesc('created_at')
  58. ->get();
  59. }
  60. /**
  61. * 获取指定时间段内的团队收益记录
  62. *
  63. * @param string $startTime
  64. * @param string $endTime
  65. * @return Collection
  66. */
  67. public function findByTimeRange(string $startTime, string $endTime): Collection
  68. {
  69. return FarmTeamProfit::whereBetween('created_at', [$startTime, $endTime])
  70. ->orderByDesc('created_at')
  71. ->get();
  72. }
  73. /**
  74. * 获取用户指定时间段内的团队收益记录
  75. *
  76. * @param int $userId
  77. * @param string $startTime
  78. * @param string $endTime
  79. * @return Collection
  80. */
  81. public function findByUserIdAndTimeRange(int $userId, string $startTime, string $endTime): Collection
  82. {
  83. return FarmTeamProfit::where('user_id', $userId)
  84. ->whereBetween('created_at', [$startTime, $endTime])
  85. ->orderByDesc('created_at')
  86. ->get();
  87. }
  88. /**
  89. * 清理过期的团队收益记录
  90. *
  91. * @param int $days 保留天数
  92. * @return int
  93. */
  94. public function cleanupOldLogs(int $days = 90): int
  95. {
  96. $date = now()->subDays($days);
  97. return FarmTeamProfit::where('created_at', '<', $date)->delete();
  98. }
  99. }