CropService.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. use UCore\Dto\Res;
  8. /**
  9. * 作物管理服务
  10. */
  11. class CropService
  12. {
  13. /**
  14. * 获取作物信息
  15. *
  16. * @param int $cropId
  17. * @return CropInfoDto|null
  18. */
  19. public static function getCropInfo(int $cropId): ?CropInfoDto
  20. {
  21. try {
  22. $cropLogic = new CropLogic();
  23. return $cropLogic->getCropInfo($cropId);
  24. } catch (\Exception $e) {
  25. Log::error('获取作物信息失败', [
  26. 'crop_id' => $cropId,
  27. 'error' => $e->getMessage(),
  28. 'trace' => $e->getTraceAsString()
  29. ]);
  30. return null;
  31. }
  32. }
  33. /**
  34. * 获取土地上的作物信息
  35. *
  36. * @param int $landId
  37. * @return CropInfoDto|null
  38. */
  39. public static function getCropByLandId(int $landId): ?CropInfoDto
  40. {
  41. try {
  42. $cropLogic = new CropLogic();
  43. return $cropLogic->getCropByLandId($landId);
  44. } catch (\Exception $e) {
  45. Log::error('获取土地作物信息失败', [
  46. 'land_id' => $landId,
  47. 'error' => $e->getMessage(),
  48. 'trace' => $e->getTraceAsString()
  49. ]);
  50. return null;
  51. }
  52. }
  53. /**
  54. * 种植作物
  55. *
  56. * @param int $userId
  57. * @param int $landId
  58. * @param int $seedId
  59. * @return array|null 返回包含CropInfoDto和日志ID的数组,格式为['crop' => CropInfoDto, 'log_id' => int]
  60. */
  61. public static function plantCrop(int $userId, int $landId, int $seedId): ?array
  62. {
  63. try {
  64. $cropLogic = new CropLogic();
  65. return $cropLogic->plantCrop($userId, $landId, $seedId);
  66. } catch (\Exception $e) {
  67. Log::error('种植作物失败', [
  68. 'user_id' => $userId,
  69. 'land_id' => $landId,
  70. 'seed_id' => $seedId,
  71. 'error' => $e->getMessage(),
  72. 'trace' => $e->getTraceAsString()
  73. ]);
  74. return null;
  75. }
  76. }
  77. /**
  78. * 收获作物
  79. *
  80. * @param int $userId
  81. * @param int $landId
  82. * @return HarvestResultDto|null
  83. */
  84. public static function harvestCrop(int $userId, int $landId): Res
  85. {
  86. try {
  87. $cropLogic = new CropLogic();
  88. return $cropLogic->harvestCrop($userId, $landId);
  89. } catch (\Exception $e) {
  90. Log::error('收获作物失败', [
  91. 'user_id' => $userId,
  92. 'land_id' => $landId,
  93. 'error' => $e->getMessage(),
  94. 'trace' => $e->getTraceAsString()
  95. ]);
  96. return Res::error();
  97. }
  98. }
  99. /**
  100. * 使用化肥
  101. *
  102. * @param int $userId
  103. * @param int $landId
  104. * @return Res
  105. */
  106. public static function useFertilizer(int $userId, int $landId,int $crop_growth_time): Res
  107. {
  108. try {
  109. $cropLogic = new CropLogic();
  110. return $cropLogic->useFertilizer($userId, $landId,$crop_growth_time);
  111. } catch (\Exception $e) {
  112. Log::error('使用化肥失败', [
  113. 'user_id' => $userId,
  114. 'land_id' => $landId,
  115. 'error' => $e->getMessage(),
  116. 'trace' => $e->getTraceAsString()
  117. ]);
  118. return Res::error('');
  119. }
  120. }
  121. /**
  122. * 更新作物生长阶段
  123. *
  124. * @param int $cropid
  125. * @return bool
  126. */
  127. public static function updateGrowthStage(int $cropid): bool
  128. {
  129. // 检查施肥后是否需要更新生长阶段
  130. $cropLogic = new CropLogic();
  131. return $cropLogic->updateGrowthStage($cropid);
  132. }
  133. /**
  134. * 清理灾害
  135. *
  136. * @param int $userId
  137. * @param int $landId
  138. * @param int $disasterType
  139. * @return bool
  140. */
  141. public static function clearDisaster(int $userId, int $landId, int $disasterType): bool
  142. {
  143. try {
  144. $cropLogic = new CropLogic();
  145. return $cropLogic->clearDisaster($userId, $landId, $disasterType);
  146. } catch (\Exception $e) {
  147. Log::error('清理灾害失败', [
  148. 'user_id' => $userId,
  149. 'land_id' => $landId,
  150. 'disaster_type' => $disasterType,
  151. 'error' => $e->getMessage(),
  152. 'trace' => $e->getTraceAsString()
  153. ]);
  154. return false;
  155. }
  156. }
  157. /**
  158. * 使用物品去除灾害(带概率判断)
  159. *
  160. * @param int $userId 用户ID
  161. * @param int $landId 土地ID
  162. * @param int $itemId 物品ID
  163. * @param int $disasterType 灾害类型
  164. * @param string $sourceType 消耗来源类型
  165. * @return array 操作结果
  166. * @throws \Exception
  167. */
  168. public static function removeDisasterWithItem(int $userId, int $landId, int $itemId, int $disasterType, string $sourceType): array
  169. {
  170. try {
  171. $disasterRemovalLogic = new \App\Module\Farm\Logics\DisasterRemovalLogic();
  172. return $disasterRemovalLogic->removeDisaster($userId, $landId, $itemId, $disasterType, $sourceType);
  173. } catch (\Exception $e) {
  174. Log::error('使用物品去除灾害失败', [
  175. 'user_id' => $userId,
  176. 'land_id' => $landId,
  177. 'item_id' => $itemId,
  178. 'disaster_type' => $disasterType,
  179. 'source_type' => $sourceType,
  180. 'error' => $e->getMessage(),
  181. 'trace' => $e->getTraceAsString()
  182. ]);
  183. throw $e;
  184. }
  185. }
  186. /**
  187. * 使用物品去除灾害(带验证和概率判断)
  188. *
  189. * @param int $userId 用户ID
  190. * @param int $landId 土地ID
  191. * @param int $itemId 物品ID
  192. * @param int $disasterType 灾害类型
  193. * @param string $sourceType 消耗来源类型
  194. * @return array 操作结果
  195. * @throws \Exception
  196. */
  197. public static function removeDisasterWithValidation(int $userId, int $landId, int $itemId, int $disasterType, string $sourceType): array
  198. {
  199. try {
  200. // 使用Validation进行数据验证
  201. $validation = new \App\Module\Farm\Validations\DisasterRemovalValidation([
  202. 'user_id' => $userId,
  203. 'land_id' => $landId,
  204. 'item_id' => $itemId,
  205. 'disaster_type' => $disasterType
  206. ]);
  207. // 验证数据
  208. $validation->validated();
  209. // 验证通过后,执行业务逻辑
  210. $disasterRemovalLogic = new \App\Module\Farm\Logics\DisasterRemovalLogic();
  211. return $disasterRemovalLogic->removeDisaster($userId, $landId, $itemId, $disasterType, $sourceType);
  212. } catch (\Exception $e) {
  213. Log::error('使用物品去除灾害失败', [
  214. 'user_id' => $userId,
  215. 'land_id' => $landId,
  216. 'item_id' => $itemId,
  217. 'disaster_type' => $disasterType,
  218. 'source_type' => $sourceType,
  219. 'error' => $e->getMessage(),
  220. 'trace' => $e->getTraceAsString()
  221. ]);
  222. throw $e;
  223. }
  224. }
  225. /**
  226. * 铲除作物
  227. *
  228. * @param int $userId
  229. * @param int $landId
  230. * @return bool
  231. */
  232. public static function removeCrop(int $userId, int $landId): bool
  233. {
  234. try {
  235. $cropLogic = new CropLogic();
  236. return $cropLogic->removeCrop($userId, $landId);
  237. } catch (\Exception $e) {
  238. Log::error('铲除作物失败', [
  239. 'user_id' => $userId,
  240. 'land_id' => $landId,
  241. 'error' => $e->getMessage(),
  242. 'trace' => $e->getTraceAsString()
  243. ]);
  244. return false;
  245. }
  246. }
  247. }