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