CropService.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace App\Module\Farm\Services;
  3. use App\Module\Farm\Dtos\CropInfoDto;
  4. use App\Module\Farm\Dtos\HarvestResultDto;
  5. use App\Module\Farm\Logics\CropLogic;
  6. use Illuminate\Support\Facades\Log;
  7. /**
  8. * 作物管理服务
  9. */
  10. class CropService
  11. {
  12. /**
  13. * 获取作物信息
  14. *
  15. * @param int $cropId
  16. * @return CropInfoDto|null
  17. */
  18. public static function getCropInfo(int $cropId): ?CropInfoDto
  19. {
  20. try {
  21. $cropLogic = new CropLogic();
  22. return $cropLogic->getCropInfo($cropId);
  23. } catch (\Exception $e) {
  24. Log::error('获取作物信息失败', [
  25. 'crop_id' => $cropId,
  26. 'error' => $e->getMessage(),
  27. 'trace' => $e->getTraceAsString()
  28. ]);
  29. return null;
  30. }
  31. }
  32. /**
  33. * 获取土地上的作物信息
  34. *
  35. * @param int $landId
  36. * @return CropInfoDto|null
  37. */
  38. public static function getCropByLandId(int $landId): ?CropInfoDto
  39. {
  40. try {
  41. $cropLogic = new CropLogic();
  42. return $cropLogic->getCropByLandId($landId);
  43. } catch (\Exception $e) {
  44. Log::error('获取土地作物信息失败', [
  45. 'land_id' => $landId,
  46. 'error' => $e->getMessage(),
  47. 'trace' => $e->getTraceAsString()
  48. ]);
  49. return null;
  50. }
  51. }
  52. /**
  53. * 种植作物
  54. *
  55. * @param int $userId
  56. * @param int $landId
  57. * @param int $seedId
  58. * @return CropInfoDto|null
  59. */
  60. public static function plantCrop(int $userId, int $landId, int $seedId): ?CropInfoDto
  61. {
  62. try {
  63. $cropLogic = new CropLogic();
  64. return $cropLogic->plantCrop($userId, $landId, $seedId);
  65. } catch (\Exception $e) {
  66. Log::error('种植作物失败', [
  67. 'user_id' => $userId,
  68. 'land_id' => $landId,
  69. 'seed_id' => $seedId,
  70. 'error' => $e->getMessage(),
  71. 'trace' => $e->getTraceAsString()
  72. ]);
  73. return null;
  74. }
  75. }
  76. /**
  77. * 收获作物
  78. *
  79. * @param int $userId
  80. * @param int $landId
  81. * @return HarvestResultDto|null
  82. */
  83. public static function harvestCrop(int $userId, int $landId): ?HarvestResultDto
  84. {
  85. try {
  86. $cropLogic = new CropLogic();
  87. return $cropLogic->harvestCrop($userId, $landId);
  88. } catch (\Exception $e) {
  89. Log::error('收获作物失败', [
  90. 'user_id' => $userId,
  91. 'land_id' => $landId,
  92. 'error' => $e->getMessage(),
  93. 'trace' => $e->getTraceAsString()
  94. ]);
  95. return null;
  96. }
  97. }
  98. /**
  99. * 使用化肥
  100. *
  101. * @param int $userId
  102. * @param int $landId
  103. * @return bool
  104. */
  105. public static function useFertilizer(int $userId, int $landId): bool
  106. {
  107. try {
  108. $cropLogic = new CropLogic();
  109. return $cropLogic->useFertilizer($userId, $landId);
  110. } catch (\Exception $e) {
  111. Log::error('使用化肥失败', [
  112. 'user_id' => $userId,
  113. 'land_id' => $landId,
  114. 'error' => $e->getMessage(),
  115. 'trace' => $e->getTraceAsString()
  116. ]);
  117. return false;
  118. }
  119. }
  120. /**
  121. * 清理灾害
  122. *
  123. * @param int $userId
  124. * @param int $landId
  125. * @param int $disasterType
  126. * @return bool
  127. */
  128. public static function clearDisaster(int $userId, int $landId, int $disasterType): bool
  129. {
  130. try {
  131. $cropLogic = new CropLogic();
  132. return $cropLogic->clearDisaster($userId, $landId, $disasterType);
  133. } catch (\Exception $e) {
  134. Log::error('清理灾害失败', [
  135. 'user_id' => $userId,
  136. 'land_id' => $landId,
  137. 'disaster_type' => $disasterType,
  138. 'error' => $e->getMessage(),
  139. 'trace' => $e->getTraceAsString()
  140. ]);
  141. return false;
  142. }
  143. }
  144. /**
  145. * 铲除作物
  146. *
  147. * @param int $userId
  148. * @param int $landId
  149. * @return bool
  150. */
  151. public static function removeCrop(int $userId, int $landId): bool
  152. {
  153. try {
  154. $cropLogic = new CropLogic();
  155. return $cropLogic->removeCrop($userId, $landId);
  156. } catch (\Exception $e) {
  157. Log::error('铲除作物失败', [
  158. 'user_id' => $userId,
  159. 'land_id' => $landId,
  160. 'error' => $e->getMessage(),
  161. 'trace' => $e->getTraceAsString()
  162. ]);
  163. return false;
  164. }
  165. }
  166. }