FarmUpgradeLogRepository.php 2.5 KB

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