FarmLandRepository.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace App\Module\Farm\Repositories;
  3. use App\Module\Farm\Models\FarmLand;
  4. use Dcat\Admin\Repositories\EloquentRepository;
  5. use Illuminate\Database\Eloquent\Collection;
  6. /**
  7. * 土地信息仓库
  8. *
  9. * 提供土地信息数据的访问和操作功能。
  10. * 该类是土地信息模块与后台管理系统的桥梁,用于处理土地信息数据的CRUD操作。
  11. */
  12. class FarmLandRepository extends EloquentRepository
  13. {
  14. /**
  15. * 模型类名
  16. *
  17. * @var string
  18. */
  19. protected $eloquentClass = FarmLand::class;
  20. /**
  21. * 获取用户的所有土地
  22. *
  23. * @param int $userId
  24. * @return Collection
  25. */
  26. public function findByUserId(int $userId): Collection
  27. {
  28. return FarmLand::where('user_id', $userId)->get();
  29. }
  30. /**
  31. * 获取用户指定位置的土地
  32. *
  33. * @param int $userId
  34. * @param int $position
  35. * @return FarmLand|null
  36. */
  37. public function findByUserIdAndPosition(int $userId, int $position): ?FarmLand
  38. {
  39. return FarmLand::where('user_id', $userId)
  40. ->where('position', $position)
  41. ->first();
  42. }
  43. /**
  44. * 获取用户指定状态的土地
  45. *
  46. * @param int $userId
  47. * @param int $status
  48. * @return Collection
  49. */
  50. public function findByUserIdAndStatus(int $userId, int $status): Collection
  51. {
  52. return FarmLand::where('user_id', $userId)
  53. ->where('status', $status)
  54. ->get();
  55. }
  56. /**
  57. * 获取用户指定类型的土地
  58. *
  59. * @param int $userId
  60. * @param int $landType
  61. * @return Collection
  62. */
  63. public function findByUserIdAndType(int $userId, int $landType): Collection
  64. {
  65. return FarmLand::where('user_id', $userId)
  66. ->where('land_type', $landType)
  67. ->get();
  68. }
  69. /**
  70. * 获取用户的特殊土地数量
  71. *
  72. * @param int $userId
  73. * @return int
  74. */
  75. public function countSpecialLands(int $userId): int
  76. {
  77. return FarmLand::where('user_id', $userId)
  78. ->whereIn('land_type', [4, 5, 6]) // 金、蓝、紫特殊土地
  79. ->count();
  80. }
  81. }