| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- namespace App\Module\Farm\Repositories;
- use App\Module\Farm\Models\FarmCrop;
- use Dcat\Admin\Repositories\EloquentRepository;
- use Illuminate\Database\Eloquent\Collection;
- /**
- * 作物信息仓库
- *
- * 提供作物信息数据的访问和操作功能。
- * 该类是作物信息模块与后台管理系统的桥梁,用于处理作物信息数据的CRUD操作。
- */
- class FarmCropRepository extends EloquentRepository
- {
- /**
- * 模型类名
- *
- * @var string
- */
- protected $eloquentClass = FarmCrop::class;
- /**
- * 根据土地ID查找作物
- *
- * @param int $landId
- * @return FarmCrop|null
- */
- public function findByLandId(int $landId): ?FarmCrop
- {
- return FarmCrop::where('land_id', $landId)->first();
- }
- /**
- * 获取用户的所有作物
- *
- * @param int $userId
- * @return Collection
- */
- public function findByUserId(int $userId): Collection
- {
- return FarmCrop::where('user_id', $userId)->get();
- }
- /**
- * 获取用户指定生长阶段的作物
- *
- * @param int $userId
- * @param int $growthStage
- * @return Collection
- */
- public function findByUserIdAndGrowthStage(int $userId, int $growthStage): Collection
- {
- return FarmCrop::where('user_id', $userId)
- ->where('growth_stage', $growthStage)
- ->get();
- }
- /**
- * 获取需要更新生长阶段的作物
- *
- * @return Collection
- */
- public function findNeedUpdateGrowthStage(): Collection
- {
- return FarmCrop::whereNotNull('stage_end_time')
- ->where('stage_end_time', '<=', now())
- ->where('growth_stage', '<', 5) // 不包括枯萎期
- ->get();
- }
- /**
- * 获取指定种子类型的作物
- *
- * @param int $seedId
- * @return Collection
- */
- public function findBySeedId(int $seedId): Collection
- {
- return FarmCrop::where('seed_id', $seedId)->get();
- }
- }
|