| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
- namespace App\Module\Farm\Repositories;
- use App\Module\Farm\Models\FarmHarvestLog;
- use Dcat\Admin\Repositories\EloquentRepository;
- use Illuminate\Database\Eloquent\Collection;
- /**
- * 收获记录仓库
- *
- * 提供收获记录数据的访问和操作功能。
- * 该类是收获记录模块与后台管理系统的桥梁,用于处理收获记录数据的CRUD操作。
- */
- class FarmHarvestLogRepository extends EloquentRepository
- {
- /**
- * 模型类名
- *
- * @var string
- */
- protected $eloquentClass = FarmHarvestLog::class;
- /**
- * 获取用户的收获记录
- *
- * @param int $userId
- * @param int $limit
- * @return Collection
- */
- public function findByUserId(int $userId, int $limit = 100): Collection
- {
- return FarmHarvestLog::where('user_id', $userId)
- ->orderByDesc('harvest_time')
- ->limit($limit)
- ->get();
- }
- /**
- * 获取指定种子的收获记录
- *
- * @param int $seedId
- * @param int $limit
- * @return Collection
- */
- public function findBySeedId(int $seedId, int $limit = 100): Collection
- {
- return FarmHarvestLog::where('seed_id', $seedId)
- ->orderByDesc('harvest_time')
- ->limit($limit)
- ->get();
- }
- /**
- * 获取指定时间段内的收获记录
- *
- * @param string $startTime
- * @param string $endTime
- * @return Collection
- */
- public function findByTimeRange(string $startTime, string $endTime): Collection
- {
- return FarmHarvestLog::whereBetween('harvest_time', [$startTime, $endTime])
- ->orderByDesc('harvest_time')
- ->get();
- }
- /**
- * 获取用户指定时间段内的收获记录
- *
- * @param int $userId
- * @param string $startTime
- * @param string $endTime
- * @return Collection
- */
- public function findByUserIdAndTimeRange(int $userId, string $startTime, string $endTime): Collection
- {
- return FarmHarvestLog::where('user_id', $userId)
- ->whereBetween('harvest_time', [$startTime, $endTime])
- ->orderByDesc('harvest_time')
- ->get();
- }
- /**
- * 清理过期的收获记录
- *
- * @param int $days 保留天数
- * @return int
- */
- public function cleanupOldLogs(int $days = 90): int
- {
- $date = now()->subDays($days);
- return FarmHarvestLog::where('harvest_time', '<', $date)->delete();
- }
- }
|