FarmCropRepository.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace App\Module\Farm\Repositories;
  3. use App\Module\Farm\Models\FarmCrop;
  4. use Dcat\Admin\Repositories\EloquentRepository;
  5. use Illuminate\Database\Eloquent\Collection;
  6. /**
  7. * 作物信息仓库
  8. *
  9. * 提供作物信息数据的访问和操作功能。
  10. * 该类是作物信息模块与后台管理系统的桥梁,用于处理作物信息数据的CRUD操作。
  11. */
  12. class FarmCropRepository extends EloquentRepository
  13. {
  14. /**
  15. * 模型类名
  16. *
  17. * @var string
  18. */
  19. protected $eloquentClass = FarmCrop::class;
  20. /**
  21. * 根据土地ID查找作物
  22. *
  23. * @param int $landId
  24. * @return FarmCrop|null
  25. */
  26. public function findByLandId(int $landId): ?FarmCrop
  27. {
  28. return FarmCrop::where('land_id', $landId)->first();
  29. }
  30. /**
  31. * 获取用户的所有作物
  32. *
  33. * @param int $userId
  34. * @return Collection
  35. */
  36. public function findByUserId(int $userId): Collection
  37. {
  38. return FarmCrop::where('user_id', $userId)->get();
  39. }
  40. /**
  41. * 获取用户指定生长阶段的作物
  42. *
  43. * @param int $userId
  44. * @param int $growthStage
  45. * @return Collection
  46. */
  47. public function findByUserIdAndGrowthStage(int $userId, int $growthStage): Collection
  48. {
  49. return FarmCrop::where('user_id', $userId)
  50. ->where('growth_stage', $growthStage)
  51. ->get();
  52. }
  53. /**
  54. * 获取需要更新生长阶段的作物
  55. *
  56. * @return Collection
  57. */
  58. public function findNeedUpdateGrowthStage(): Collection
  59. {
  60. return FarmCrop::whereNotNull('stage_end_time')
  61. ->where('stage_end_time', '<=', now())
  62. ->where('growth_stage', '<', 5) // 不包括枯萎期
  63. ->get();
  64. }
  65. /**
  66. * 获取指定种子类型的作物
  67. *
  68. * @param int $seedId
  69. * @return Collection
  70. */
  71. public function findBySeedId(int $seedId): Collection
  72. {
  73. return FarmCrop::where('seed_id', $seedId)->get();
  74. }
  75. }