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; } } }