FarmHarvestLogRepository.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace App\Module\Farm\Repositories;
  3. use App\Module\Farm\Models\FarmHarvestLog;
  4. use Dcat\Admin\Repositories\EloquentRepository;
  5. use Illuminate\Database\Eloquent\Collection;
  6. /**
  7. * 收获记录仓库
  8. *
  9. * 提供收获记录数据的访问和操作功能。
  10. * 该类是收获记录模块与后台管理系统的桥梁,用于处理收获记录数据的CRUD操作。
  11. */
  12. class FarmHarvestLogRepository extends EloquentRepository
  13. {
  14. /**
  15. * 模型类名
  16. *
  17. * @var string
  18. */
  19. protected $eloquentClass = FarmHarvestLog::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 FarmHarvestLog::where('user_id', $userId)
  30. ->orderByDesc('harvest_time')
  31. ->limit($limit)
  32. ->get();
  33. }
  34. /**
  35. * 获取指定种子的收获记录
  36. *
  37. * @param int $seedId
  38. * @param int $limit
  39. * @return Collection
  40. */
  41. public function findBySeedId(int $seedId, int $limit = 100): Collection
  42. {
  43. return FarmHarvestLog::where('seed_id', $seedId)
  44. ->orderByDesc('harvest_time')
  45. ->limit($limit)
  46. ->get();
  47. }
  48. /**
  49. * 获取指定时间段内的收获记录
  50. *
  51. * @param string $startTime
  52. * @param string $endTime
  53. * @return Collection
  54. */
  55. public function findByTimeRange(string $startTime, string $endTime): Collection
  56. {
  57. return FarmHarvestLog::whereBetween('harvest_time', [$startTime, $endTime])
  58. ->orderByDesc('harvest_time')
  59. ->get();
  60. }
  61. /**
  62. * 获取用户指定时间段内的收获记录
  63. *
  64. * @param int $userId
  65. * @param string $startTime
  66. * @param string $endTime
  67. * @return Collection
  68. */
  69. public function findByUserIdAndTimeRange(int $userId, string $startTime, string $endTime): Collection
  70. {
  71. return FarmHarvestLog::where('user_id', $userId)
  72. ->whereBetween('harvest_time', [$startTime, $endTime])
  73. ->orderByDesc('harvest_time')
  74. ->get();
  75. }
  76. /**
  77. * 清理过期的收获记录
  78. *
  79. * @param int $days 保留天数
  80. * @return int
  81. */
  82. public function cleanupOldLogs(int $days = 90): int
  83. {
  84. $date = now()->subDays($days);
  85. return FarmHarvestLog::where('harvest_time', '<', $date)->delete();
  86. }
  87. }