CropService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 $itemId 种子物品ID
  59. * @return array|null 返回包含CropInfoDto和日志ID的数组,格式为['crop' => CropInfoDto, 'log_id' => int]
  60. */
  61. public static function plantCrop(int $userId, int $landId, int $itemId): ?array
  62. {
  63. try {
  64. $cropLogic = new CropLogic();
  65. return $cropLogic->plantCrop($userId, $landId, $itemId);
  66. } catch (\Exception $e) {
  67. Log::error('种植作物失败', [
  68. 'user_id' => $userId,
  69. 'land_id' => $landId,
  70. 'item_id' => $itemId,
  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. * 使用化肥(通过作物ID)
  101. *
  102. * @param int $cropId 作物ID
  103. * @param int $cropGrowthTime 减少的生长时间(秒)
  104. * @return Res
  105. */
  106. public static function useFertilizer(int $cropId, int $cropGrowthTime): Res
  107. {
  108. try {
  109. $cropLogic = new CropLogic();
  110. return $cropLogic->useFertilizerByCropId($cropId, $cropGrowthTime);
  111. } catch (\Exception $e) {
  112. Log::error('使用化肥失败', [
  113. 'crop_id' => $cropId,
  114. 'crop_growth_time' => $cropGrowthTime,
  115. 'error' => $e->getMessage(),
  116. 'trace' => $e->getTraceAsString()
  117. ]);
  118. return Res::error('使用化肥失败');
  119. }
  120. }
  121. /**
  122. * 使用化肥(通过土地ID,兼容旧接口)
  123. *
  124. * @param int $userId
  125. * @param int $landId
  126. * @param int $cropGrowthTime
  127. * @return Res
  128. * @deprecated 建议使用 useFertilizer($cropId, $cropGrowthTime) 方法
  129. */
  130. public static function useFertilizerByLandId(int $userId, int $landId, int $cropGrowthTime): Res
  131. {
  132. try {
  133. $cropLogic = new CropLogic();
  134. return $cropLogic->useFertilizer($userId, $landId, $cropGrowthTime);
  135. } catch (\Exception $e) {
  136. Log::error('使用化肥失败', [
  137. 'user_id' => $userId,
  138. 'land_id' => $landId,
  139. 'crop_growth_time' => $cropGrowthTime,
  140. 'error' => $e->getMessage(),
  141. 'trace' => $e->getTraceAsString()
  142. ]);
  143. return Res::error('使用化肥失败');
  144. }
  145. }
  146. /**
  147. * 更新作物生长阶段
  148. *
  149. * @param int $cropid
  150. * @return bool
  151. */
  152. public static function updateGrowthStage(int $cropid): bool
  153. {
  154. // 检查施肥后是否需要更新生长阶段
  155. $cropLogic = new CropLogic();
  156. return $cropLogic->updateGrowthStage($cropid);
  157. }
  158. /**
  159. * 清理灾害
  160. *
  161. * @param int $userId
  162. * @param int $landId
  163. * @param int $disasterType
  164. * @return bool
  165. */
  166. public static function clearDisaster(int $userId, int $landId, int $disasterType): bool
  167. {
  168. try {
  169. $cropLogic = new CropLogic();
  170. return $cropLogic->clearDisaster($userId, $landId, $disasterType);
  171. } catch (\Exception $e) {
  172. Log::error('清理灾害失败', [
  173. 'user_id' => $userId,
  174. 'land_id' => $landId,
  175. 'disaster_type' => $disasterType,
  176. 'error' => $e->getMessage(),
  177. 'trace' => $e->getTraceAsString()
  178. ]);
  179. return false;
  180. }
  181. }
  182. /**
  183. * 使用物品去除灾害(带概率判断)
  184. *
  185. * @param int $userId 用户ID
  186. * @param int $landId 土地ID
  187. * @param int $itemId 物品ID
  188. * @param int $disasterType 灾害类型
  189. * @param string $sourceType 消耗来源类型
  190. * @return array 操作结果
  191. * @throws \Exception
  192. */
  193. public static function removeDisasterWithItem(int $userId, int $landId, int $itemId, int $disasterType, string $sourceType): array
  194. {
  195. try {
  196. $disasterRemovalLogic = new \App\Module\Farm\Logics\DisasterRemovalLogic();
  197. return $disasterRemovalLogic->removeDisaster($userId, $landId, $itemId, $disasterType, $sourceType);
  198. } catch (\Exception $e) {
  199. Log::error('使用物品去除灾害失败', [
  200. 'user_id' => $userId,
  201. 'land_id' => $landId,
  202. 'item_id' => $itemId,
  203. 'disaster_type' => $disasterType,
  204. 'source_type' => $sourceType,
  205. 'error' => $e->getMessage(),
  206. 'trace' => $e->getTraceAsString()
  207. ]);
  208. throw $e;
  209. }
  210. }
  211. /**
  212. * 使用物品去除灾害(带验证和概率判断)
  213. *
  214. * @param int $userId 用户ID
  215. * @param int $landId 土地ID
  216. * @param int $itemId 物品ID
  217. * @param int $disasterType 灾害类型
  218. * @param string $sourceType 消耗来源类型
  219. * @return array 操作结果
  220. * @throws \Exception
  221. */
  222. public static function removeDisasterWithValidation(int $userId, int $landId, int $itemId, int $disasterType, string $sourceType): array
  223. {
  224. try {
  225. // 使用Validation进行数据验证
  226. $validation = new \App\Module\Farm\Validations\DisasterRemovalValidation([
  227. 'user_id' => $userId,
  228. 'land_id' => $landId,
  229. 'item_id' => $itemId,
  230. 'disaster_type' => $disasterType
  231. ]);
  232. // 验证数据
  233. $validation->validated();
  234. // 验证通过后,执行业务逻辑
  235. $disasterRemovalLogic = new \App\Module\Farm\Logics\DisasterRemovalLogic();
  236. return $disasterRemovalLogic->removeDisaster($userId, $landId, $itemId, $disasterType, $sourceType);
  237. } catch (\Exception $e) {
  238. Log::error('使用物品去除灾害失败', [
  239. 'user_id' => $userId,
  240. 'land_id' => $landId,
  241. 'item_id' => $itemId,
  242. 'disaster_type' => $disasterType,
  243. 'source_type' => $sourceType,
  244. 'error' => $e->getMessage(),
  245. 'trace' => $e->getTraceAsString()
  246. ]);
  247. throw $e;
  248. }
  249. }
  250. /**
  251. * 铲除作物
  252. *
  253. * @param int $userId
  254. * @param int $landId
  255. * @param int $toolItemId 使用的工具物品ID,0表示手动铲除
  256. * @return array 返回操作结果和状态变更信息
  257. */
  258. public static function removeCrop(int $userId, int $landId, int $toolItemId = 0): array
  259. {
  260. try {
  261. // 获取铲除前的土地状态
  262. $land = \App\Module\Farm\Models\FarmLand::where('id', $landId)
  263. ->where('user_id', $userId)
  264. ->first();
  265. if (!$land) {
  266. throw new \Exception('土地不存在');
  267. }
  268. $oldStatus = $land->status;
  269. $cropLogic = new CropLogic();
  270. $result = $cropLogic->removeCrop($userId, $landId, $toolItemId);
  271. if ($result) {
  272. // 获取铲除后的土地状态
  273. $land->refresh();
  274. $newStatus = $land->status;
  275. return [
  276. 'success' => true,
  277. 'status_changed' => $oldStatus !== $newStatus,
  278. 'old_status' => $oldStatus,
  279. 'new_status' => $newStatus
  280. ];
  281. }
  282. return ['success' => false];
  283. } catch (\Exception $e) {
  284. Log::error('铲除作物失败', [
  285. 'user_id' => $userId,
  286. 'land_id' => $landId,
  287. 'error' => $e->getMessage(),
  288. 'trace' => $e->getTraceAsString()
  289. ]);
  290. throw $e; // 重新抛出异常,由调用方处理
  291. }
  292. }
  293. /**
  294. * 强制删除作物(物理删除,谨慎使用)
  295. *
  296. * 此方法会永久删除作物记录,主要用于:
  297. * 1. 数据清理和维护
  298. * 2. 测试环境的数据重置
  299. * 3. 特殊的管理员操作
  300. *
  301. * @param int $userId 用户ID
  302. * @param int $landId 土地ID
  303. * @param string $reason 删除原因
  304. * @return array 返回操作结果
  305. */
  306. public static function forceDeleteCrop(int $userId, int $landId, string $reason = '管理员操作'): array
  307. {
  308. try {
  309. $cropLogic = new CropLogic();
  310. $result = $cropLogic->forceDeleteCrop($userId, $landId, $reason);
  311. return [
  312. 'success' => $result,
  313. 'message' => $result ? '强制删除作物成功' : '强制删除作物失败'
  314. ];
  315. } catch (\Exception $e) {
  316. Log::error('强制删除作物失败', [
  317. 'user_id' => $userId,
  318. 'land_id' => $landId,
  319. 'reason' => $reason,
  320. 'error' => $e->getMessage(),
  321. 'trace' => $e->getTraceAsString()
  322. ]);
  323. throw $e;
  324. }
  325. }
  326. /**
  327. * 恢复软删除的作物
  328. *
  329. * @param int $userId 用户ID
  330. * @param int $landId 土地ID
  331. * @return array 返回操作结果
  332. */
  333. public static function restoreCrop(int $userId, int $landId): array
  334. {
  335. try {
  336. $cropLogic = new CropLogic();
  337. $result = $cropLogic->restoreCrop($userId, $landId);
  338. return [
  339. 'success' => $result,
  340. 'message' => $result ? '恢复作物成功' : '恢复作物失败'
  341. ];
  342. } catch (\Exception $e) {
  343. Log::error('恢复作物失败', [
  344. 'user_id' => $userId,
  345. 'land_id' => $landId,
  346. 'error' => $e->getMessage(),
  347. 'trace' => $e->getTraceAsString()
  348. ]);
  349. throw $e;
  350. }
  351. }
  352. /**
  353. * 获取软删除的作物信息
  354. *
  355. * @param int $landId 土地ID
  356. * @return CropInfoDto|null
  357. */
  358. public static function getTrashedCropByLandId(int $landId): ?CropInfoDto
  359. {
  360. try {
  361. $cropLogic = new CropLogic();
  362. return $cropLogic->getTrashedCropByLandId($landId);
  363. } catch (\Exception $e) {
  364. Log::error('获取软删除作物信息失败', [
  365. 'land_id' => $landId,
  366. 'error' => $e->getMessage(),
  367. 'trace' => $e->getTraceAsString()
  368. ]);
  369. return null;
  370. }
  371. }
  372. }