HarvestLogLogic.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace App\Module\Farm\Logics;
  3. use App\Module\Farm\Models\FarmHarvestLog;
  4. use Illuminate\Database\Eloquent\Collection;
  5. use UCore\Helper\Logger;
  6. /**
  7. * 收获记录逻辑
  8. *
  9. * 提供收获记录数据的业务逻辑处理。
  10. */
  11. class HarvestLogLogic
  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 FarmHarvestLog::where('user_id', $userId)
  24. ->orderByDesc('harvest_time')
  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 $seedId
  38. * @param int $limit
  39. * @return Collection
  40. */
  41. public function findBySeedId(int $seedId, int $limit = 100): Collection
  42. {
  43. try {
  44. return FarmHarvestLog::where('seed_id', $seedId)
  45. ->orderByDesc('harvest_time')
  46. ->limit($limit)
  47. ->get();
  48. } catch (\Exception $e) {
  49. Logger::exception('获取种子收获记录失败', $e, [
  50. 'seed_id' => $seedId,
  51. ]);
  52. return collect();
  53. }
  54. }
  55. /**
  56. * 获取指定时间段内的收获记录
  57. *
  58. * @param string $startTime
  59. * @param string $endTime
  60. * @return Collection
  61. */
  62. public function findByTimeRange(string $startTime, string $endTime): Collection
  63. {
  64. try {
  65. return FarmHarvestLog::whereBetween('harvest_time', [$startTime, $endTime])
  66. ->orderByDesc('harvest_time')
  67. ->get();
  68. } catch (\Exception $e) {
  69. Logger::exception('获取时间段收获记录失败', $e, [
  70. 'start_time' => $startTime,
  71. 'end_time' => $endTime,
  72. ]);
  73. return collect();
  74. }
  75. }
  76. /**
  77. * 获取用户指定时间段内的收获记录
  78. *
  79. * @param int $userId
  80. * @param string $startTime
  81. * @param string $endTime
  82. * @return Collection
  83. */
  84. public function findByUserIdAndTimeRange(int $userId, string $startTime, string $endTime): Collection
  85. {
  86. try {
  87. return FarmHarvestLog::where('user_id', $userId)
  88. ->whereBetween('harvest_time', [$startTime, $endTime])
  89. ->orderByDesc('harvest_time')
  90. ->get();
  91. } catch (\Exception $e) {
  92. Logger::exception('获取用户时间段收获记录失败', $e, [
  93. 'user_id' => $userId,
  94. 'start_time' => $startTime,
  95. 'end_time' => $endTime,
  96. ]);
  97. return collect();
  98. }
  99. }
  100. /**
  101. * 清理过期的收获记录
  102. *
  103. * @param int $days 保留天数
  104. * @return int
  105. */
  106. public function cleanupOldLogs(int $days = 90): int
  107. {
  108. try {
  109. $date = now()->subDays($days);
  110. return FarmHarvestLog::where('harvest_time', '<', $date)->delete();
  111. } catch (\Exception $e) {
  112. Logger::exception('清理过期收获记录失败', $e, [
  113. 'days' => $days,
  114. ]);
  115. return 0;
  116. }
  117. }
  118. }