LandService.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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和土地id获取土地信息
  36. *
  37. * @param int $userId 用户ID
  38. * @param int $landId 土地ID
  39. * @return LandInfoDto|null
  40. */
  41. public static function getUserLand(int $userId, int $landId): ?LandInfoDto
  42. {
  43. try {
  44. $land = FarmLand::where('user_id', $userId)
  45. ->where('id', $landId)
  46. ->first();
  47. if (!$land) {
  48. return null;
  49. }
  50. return LandInfoDto::fromModel($land);
  51. } catch (\Exception $e) {
  52. Log::error('获取用户土地信息失败', [
  53. 'user_id' => $userId,
  54. 'land_id' => $landId,
  55. 'error' => $e->getMessage(),
  56. 'trace' => $e->getTraceAsString()
  57. ]);
  58. return null;
  59. }
  60. }
  61. /**
  62. * 获取用户指定位置的土地
  63. *
  64. * @param int $userId
  65. * @param int $position
  66. * @return LandInfoDto|null
  67. */
  68. public static function getUserLandByPosition(int $userId, int $position): ?LandInfoDto
  69. {
  70. try {
  71. $landLogic = new LandLogic();
  72. return $landLogic->getUserLandByPosition($userId, $position);
  73. } catch (\Exception $e) {
  74. Log::error('获取用户指定位置土地失败', [
  75. 'user_id' => $userId,
  76. 'position' => $position,
  77. 'error' => $e->getMessage(),
  78. 'trace' => $e->getTraceAsString()
  79. ]);
  80. return null;
  81. }
  82. }
  83. /**
  84. * 升级土地
  85. *
  86. * @param int $userId
  87. * @param int $landId
  88. * @param int $targetType
  89. * @return bool
  90. */
  91. public static function upgradeLand(int $userId, int $landId, int $targetType ): bool
  92. {
  93. try {
  94. $landLogic = new LandLogic();
  95. return $landLogic->upgradeLand($userId, $landId, $targetType);
  96. } catch (\Exception $e) {
  97. Log::error('升级土地失败', [
  98. 'user_id' => $userId,
  99. 'land_id' => $landId,
  100. 'target_type' => $targetType,
  101. 'error' => $e->getMessage(),
  102. 'trace' => $e->getTraceAsString()
  103. ]);
  104. return false;
  105. }
  106. }
  107. /**
  108. * 获取可用的升级路径
  109. *
  110. * @param int $userId 用户ID
  111. * @param int $landId 土地ID
  112. * @param int|null $toType 目标土地类型ID(可选)
  113. * @return FarmLandUpgradeConfig[]
  114. */
  115. public static function getAvailableUpgradePaths(int $userId, int $landId, ?int $toType = null): array
  116. {
  117. try {
  118. $landLogic = new LandLogic();
  119. return $landLogic->getAvailableUpgradePaths($userId, $landId, $toType);
  120. } catch (\Exception $e) {
  121. Log::error('获取可用升级路径失败', [
  122. 'user_id' => $userId,
  123. 'land_id' => $landId,
  124. 'to_type' => $toType,
  125. 'error' => $e->getMessage(),
  126. 'trace' => $e->getTraceAsString()
  127. ]);
  128. return [];
  129. }
  130. }
  131. }