| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- namespace App\Module\Farm\Logics;
- use App\Module\Farm\Models\FarmHarvestLog;
- use Illuminate\Database\Eloquent\Collection;
- use UCore\Helper\Logger;
- /**
- * 收获记录逻辑
- *
- * 提供收获记录数据的业务逻辑处理。
- */
- class HarvestLogLogic
- {
- /**
- * 获取用户的收获记录
- *
- * @param int $userId
- * @param int $limit
- * @return Collection
- */
- public function findByUserId(int $userId, int $limit = 100): Collection
- {
- try {
- return FarmHarvestLog::where('user_id', $userId)
- ->orderByDesc('harvest_time')
- ->limit($limit)
- ->get();
- } catch (\Exception $e) {
- Logger::exception('获取用户收获记录失败', $e, [
- 'user_id' => $userId,
- ]);
-
- return collect();
- }
- }
- /**
- * 获取指定种子的收获记录
- *
- * @param int $seedId
- * @param int $limit
- * @return Collection
- */
- public function findBySeedId(int $seedId, int $limit = 100): Collection
- {
- try {
- return FarmHarvestLog::where('seed_id', $seedId)
- ->orderByDesc('harvest_time')
- ->limit($limit)
- ->get();
- } catch (\Exception $e) {
- Logger::exception('获取种子收获记录失败', $e, [
- 'seed_id' => $seedId,
- ]);
-
- return collect();
- }
- }
- /**
- * 获取指定时间段内的收获记录
- *
- * @param string $startTime
- * @param string $endTime
- * @return Collection
- */
- public function findByTimeRange(string $startTime, string $endTime): Collection
- {
- try {
- return FarmHarvestLog::whereBetween('harvest_time', [$startTime, $endTime])
- ->orderByDesc('harvest_time')
- ->get();
- } catch (\Exception $e) {
- Logger::exception('获取时间段收获记录失败', $e, [
- 'start_time' => $startTime,
- 'end_time' => $endTime,
- ]);
-
- return collect();
- }
- }
- /**
- * 获取用户指定时间段内的收获记录
- *
- * @param int $userId
- * @param string $startTime
- * @param string $endTime
- * @return Collection
- */
- public function findByUserIdAndTimeRange(int $userId, string $startTime, string $endTime): Collection
- {
- try {
- return FarmHarvestLog::where('user_id', $userId)
- ->whereBetween('harvest_time', [$startTime, $endTime])
- ->orderByDesc('harvest_time')
- ->get();
- } catch (\Exception $e) {
- Logger::exception('获取用户时间段收获记录失败', $e, [
- 'user_id' => $userId,
- 'start_time' => $startTime,
- 'end_time' => $endTime,
- ]);
-
- return collect();
- }
- }
- /**
- * 清理过期的收获记录
- *
- * @param int $days 保留天数
- * @return int
- */
- public function cleanupOldLogs(int $days = 90): int
- {
- try {
- $date = now()->subDays($days);
- return FarmHarvestLog::where('harvest_time', '<', $date)->delete();
- } catch (\Exception $e) {
- Logger::exception('清理过期收获记录失败', $e, [
- 'days' => $days,
- ]);
-
- return 0;
- }
- }
- }
|