LandService.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace App\Module\Farm\Services;
  3. use App\Module\Farm\Dtos\LandInfoDto;
  4. use App\Module\Farm\Logics\LandLogic;
  5. use App\Module\Farm\Models\FarmLand;
  6. use App\Module\Farm\Models\FarmLandUpgradeConfig;
  7. use Illuminate\Support\Collection;
  8. use Illuminate\Support\Facades\Log;
  9. /**
  10. * 土地管理服务
  11. */
  12. class LandService
  13. {
  14. /**
  15. * 获取用户的所有土地
  16. *
  17. * @param int $userId
  18. * @return Collection|LandInfoDto[]
  19. */
  20. public static function getUserLands(int $userId): Collection
  21. {
  22. try {
  23. $landLogic = new LandLogic();
  24. return $landLogic->getUserLands($userId);
  25. } catch (\Exception $e) {
  26. Log::error('获取用户土地失败', [
  27. 'user_id' => $userId,
  28. 'error' => $e->getMessage(),
  29. 'trace' => $e->getTraceAsString()
  30. ]);
  31. return collect();
  32. }
  33. }
  34. /**
  35. * 根据土地ID获取土地信息
  36. *
  37. * @param int $landId 土地ID
  38. * @return LandInfoDto|null
  39. */
  40. public static function getLandById(int $landId): ?LandInfoDto
  41. {
  42. try {
  43. $land = FarmLand::find($landId);
  44. if (!$land) {
  45. return null;
  46. }
  47. return LandInfoDto::fromModel($land);
  48. } catch (\Exception $e) {
  49. Log::error('根据ID获取土地信息失败', [
  50. 'land_id' => $landId,
  51. 'error' => $e->getMessage(),
  52. 'trace' => $e->getTraceAsString()
  53. ]);
  54. return null;
  55. }
  56. }
  57. /**
  58. * 根据用户id和土地id获取土地信息
  59. *
  60. * @param int $userId 用户ID
  61. * @param int $landId 土地ID
  62. * @return LandInfoDto|null
  63. */
  64. public static function getUserLand(int $userId, int $landId): ?LandInfoDto
  65. {
  66. try {
  67. $land = FarmLand::where('user_id', $userId)
  68. ->where('id', $landId)
  69. ->first();
  70. if (!$land) {
  71. return null;
  72. }
  73. return LandInfoDto::fromModel($land);
  74. } catch (\Exception $e) {
  75. Log::error('获取用户土地信息失败', [
  76. 'user_id' => $userId,
  77. 'land_id' => $landId,
  78. 'error' => $e->getMessage(),
  79. 'trace' => $e->getTraceAsString()
  80. ]);
  81. return null;
  82. }
  83. }
  84. /**
  85. * 获取用户指定位置的土地
  86. *
  87. * @param int $userId
  88. * @param int $position
  89. * @return LandInfoDto|null
  90. */
  91. public static function getUserLandByPosition(int $userId, int $position): ?LandInfoDto
  92. {
  93. try {
  94. $landLogic = new LandLogic();
  95. return $landLogic->getUserLandByPosition($userId, $position);
  96. } catch (\Exception $e) {
  97. Log::error('获取用户指定位置土地失败', [
  98. 'user_id' => $userId,
  99. 'position' => $position,
  100. 'error' => $e->getMessage(),
  101. 'trace' => $e->getTraceAsString()
  102. ]);
  103. return null;
  104. }
  105. }
  106. /**
  107. * 升级土地
  108. *
  109. * @param int $userId
  110. * @param int $landId
  111. * @param int $targetType
  112. * @return bool
  113. */
  114. public static function upgradeLand(int $userId, int $landId, int $targetType ): bool
  115. {
  116. try {
  117. $landLogic = new LandLogic();
  118. return $landLogic->upgradeLand($userId, $landId, $targetType);
  119. } catch (\Exception $e) {
  120. Log::error('升级土地失败', [
  121. 'user_id' => $userId,
  122. 'land_id' => $landId,
  123. 'target_type' => $targetType,
  124. 'error' => $e->getMessage(),
  125. 'trace' => $e->getTraceAsString()
  126. ]);
  127. return false;
  128. }
  129. }
  130. /**
  131. * 获取可用的升级路径
  132. *
  133. * @param int $userId 用户ID
  134. * @param int $landId 土地ID
  135. * @param int|null $toType 目标土地类型ID(可选)
  136. * @return FarmLandUpgradeConfig[]
  137. */
  138. public static function getAvailableUpgradePaths(int $userId, int $landId, ?int $toType = null): array
  139. {
  140. try {
  141. $landLogic = new LandLogic();
  142. return $landLogic->getAvailableUpgradePaths($userId, $landId, $toType);
  143. } catch (\Exception $e) {
  144. Log::error('获取可用升级路径失败', [
  145. 'user_id' => $userId,
  146. 'land_id' => $landId,
  147. 'to_type' => $toType,
  148. 'error' => $e->getMessage(),
  149. 'trace' => $e->getTraceAsString()
  150. ]);
  151. return [];
  152. }
  153. }
  154. /**
  155. * 获取用户所有可收获的土地
  156. *
  157. * @param int $userId 用户ID
  158. * @return Collection|LandInfoDto[]
  159. */
  160. public static function getHarvestableLands(int $userId): Collection
  161. {
  162. try {
  163. $landLogic = new LandLogic();
  164. return $landLogic->getHarvestableLands($userId);
  165. } catch (\Exception $e) {
  166. Log::error('获取可收获土地失败', [
  167. 'user_id' => $userId,
  168. 'error' => $e->getMessage(),
  169. 'trace' => $e->getTraceAsString()
  170. ]);
  171. return collect();
  172. }
  173. }
  174. /**
  175. * 获取用户所有空闲的土地
  176. *
  177. * @param int $userId 用户ID
  178. * @return Collection|LandInfoDto[]
  179. */
  180. public static function getIdleLands(int $userId): Collection
  181. {
  182. try {
  183. $landLogic = new LandLogic();
  184. return $landLogic->getIdleLands($userId);
  185. } catch (\Exception $e) {
  186. Log::error('获取空闲土地失败', [
  187. 'user_id' => $userId,
  188. 'error' => $e->getMessage(),
  189. 'trace' => $e->getTraceAsString()
  190. ]);
  191. return collect();
  192. }
  193. }
  194. /**
  195. * 获取用户所有有作物的土地
  196. *
  197. * @param int $userId 用户ID
  198. * @return Collection|LandInfoDto[]
  199. */
  200. public static function getLandsWithCrops(int $userId): Collection
  201. {
  202. try {
  203. $landLogic = new LandLogic();
  204. return $landLogic->getLandsWithCrops($userId);
  205. } catch (\Exception $e) {
  206. Log::error('获取有作物的土地失败', [
  207. 'user_id' => $userId,
  208. 'error' => $e->getMessage(),
  209. 'trace' => $e->getTraceAsString()
  210. ]);
  211. return collect();
  212. }
  213. }
  214. }