CropService.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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,int $crop_growth_time): bool
  106. {
  107. try {
  108. $cropLogic = new CropLogic();
  109. return $cropLogic->useFertilizer($userId, $landId,$crop_growth_time);
  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 $cropid
  124. * @return bool
  125. */
  126. public static function updateGrowthStage(int $cropid): bool
  127. {
  128. // 检查施肥后是否需要更新生长阶段
  129. $cropLogic = new CropLogic();
  130. return $cropLogic->updateGrowthStage($cropid);
  131. }
  132. /**
  133. * 清理灾害
  134. *
  135. * @param int $userId
  136. * @param int $landId
  137. * @param int $disasterType
  138. * @return bool
  139. */
  140. public static function clearDisaster(int $userId, int $landId, int $disasterType): bool
  141. {
  142. try {
  143. $cropLogic = new CropLogic();
  144. return $cropLogic->clearDisaster($userId, $landId, $disasterType);
  145. } catch (\Exception $e) {
  146. Log::error('清理灾害失败', [
  147. 'user_id' => $userId,
  148. 'land_id' => $landId,
  149. 'disaster_type' => $disasterType,
  150. 'error' => $e->getMessage(),
  151. 'trace' => $e->getTraceAsString()
  152. ]);
  153. return false;
  154. }
  155. }
  156. /**
  157. * 铲除作物
  158. *
  159. * @param int $userId
  160. * @param int $landId
  161. * @return bool
  162. */
  163. public static function removeCrop(int $userId, int $landId): bool
  164. {
  165. try {
  166. $cropLogic = new CropLogic();
  167. return $cropLogic->removeCrop($userId, $landId);
  168. } catch (\Exception $e) {
  169. Log::error('铲除作物失败', [
  170. 'user_id' => $userId,
  171. 'land_id' => $landId,
  172. 'error' => $e->getMessage(),
  173. 'trace' => $e->getTraceAsString()
  174. ]);
  175. return false;
  176. }
  177. }
  178. }