getCropInfo($cropId); } catch (\Exception $e) { Log::error('获取作物信息失败', [ 'crop_id' => $cropId, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return null; } } /** * 获取土地上的作物信息 * * @param int $landId * @return CropInfoDto|null */ public static function getCropByLandId(int $landId): ?CropInfoDto { try { $cropLogic = new CropLogic(); return $cropLogic->getCropByLandId($landId); } catch (\Exception $e) { Log::error('获取土地作物信息失败', [ 'land_id' => $landId, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return null; } } /** * 种植作物 * * @param int $userId * @param int $landId * @param int $itemId 种子物品ID * @return array|null 返回包含CropInfoDto和日志ID的数组,格式为['crop' => CropInfoDto, 'log_id' => int] */ public static function plantCrop(int $userId, int $landId, int $itemId): ?array { try { $cropLogic = new CropLogic(); return $cropLogic->plantCrop($userId, $landId, $itemId); } catch (\Exception $e) { Log::error('种植作物失败', [ 'user_id' => $userId, 'land_id' => $landId, 'item_id' => $itemId, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return null; } } /** * 收获作物 * * @param int $userId * @param int $landId * @return HarvestResultDto|null */ public static function harvestCrop(int $userId, int $landId): Res { try { $cropLogic = new CropLogic(); return $cropLogic->harvestCrop($userId, $landId); } catch (\Exception $e) { Log::error('收获作物失败', [ 'user_id' => $userId, 'land_id' => $landId, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return Res::error(); } } /** * 使用化肥(通过作物ID) * * @param int $cropId 作物ID * @param int $cropGrowthTime 减少的生长时间(秒) * @return Res */ public static function useFertilizer(int $cropId, int $cropGrowthTime): Res { try { $cropLogic = new CropLogic(); return $cropLogic->useFertilizerByCropId($cropId, $cropGrowthTime); } catch (\Exception $e) { Log::error('使用化肥失败', [ 'crop_id' => $cropId, 'crop_growth_time' => $cropGrowthTime, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return Res::error('使用化肥失败'); } } /** * 使用化肥(通过土地ID,兼容旧接口) * * @param int $userId * @param int $landId * @param int $cropGrowthTime * @return Res * @deprecated 建议使用 useFertilizer($cropId, $cropGrowthTime) 方法 */ public static function useFertilizerByLandId(int $userId, int $landId, int $cropGrowthTime): Res { try { $cropLogic = new CropLogic(); return $cropLogic->useFertilizer($userId, $landId, $cropGrowthTime); } catch (\Exception $e) { Log::error('使用化肥失败', [ 'user_id' => $userId, 'land_id' => $landId, 'crop_growth_time' => $cropGrowthTime, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return Res::error('使用化肥失败'); } } /** * 更新作物生长阶段 * * @param int $cropid * @return bool */ public static function updateGrowthStage(int $cropid): bool { // 检查施肥后是否需要更新生长阶段 $cropLogic = new CropLogic(); return $cropLogic->updateGrowthStage($cropid); } /** * 清理灾害 * * @param int $userId * @param int $landId * @param int $disasterType * @return bool */ public static function clearDisaster(int $userId, int $landId, int $disasterType): bool { try { $cropLogic = new CropLogic(); return $cropLogic->clearDisaster($userId, $landId, $disasterType); } catch (\Exception $e) { Log::error('清理灾害失败', [ 'user_id' => $userId, 'land_id' => $landId, 'disaster_type' => $disasterType, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return false; } } /** * 使用物品去除灾害(带概率判断) * * @param int $userId 用户ID * @param int $landId 土地ID * @param int $itemId 物品ID * @param int $disasterType 灾害类型 * @param string $sourceType 消耗来源类型 * @return array 操作结果 * @throws \Exception */ public static function removeDisasterWithItem(int $userId, int $landId, int $itemId, int $disasterType, string $sourceType): array { try { $disasterRemovalLogic = new \App\Module\Farm\Logics\DisasterRemovalLogic(); return $disasterRemovalLogic->removeDisaster($userId, $landId, $itemId, $disasterType, $sourceType); } catch (\Exception $e) { Log::error('使用物品去除灾害失败', [ 'user_id' => $userId, 'land_id' => $landId, 'item_id' => $itemId, 'disaster_type' => $disasterType, 'source_type' => $sourceType, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); throw $e; } } /** * 使用物品去除灾害(带验证和概率判断) * * @param int $userId 用户ID * @param int $landId 土地ID * @param int $itemId 物品ID * @param int $disasterType 灾害类型 * @param string $sourceType 消耗来源类型 * @return array 操作结果 * @throws \Exception */ public static function removeDisasterWithValidation(int $userId, int $landId, int $itemId, int $disasterType, string $sourceType): array { try { // 使用Validation进行数据验证 $validation = new \App\Module\Farm\Validations\DisasterRemovalValidation([ 'user_id' => $userId, 'land_id' => $landId, 'item_id' => $itemId, 'disaster_type' => $disasterType ]); // 验证数据 $validation->validated(); // 验证通过后,执行业务逻辑 $disasterRemovalLogic = new \App\Module\Farm\Logics\DisasterRemovalLogic(); return $disasterRemovalLogic->removeDisaster($userId, $landId, $itemId, $disasterType, $sourceType); } catch (\Exception $e) { Log::error('使用物品去除灾害失败', [ 'user_id' => $userId, 'land_id' => $landId, 'item_id' => $itemId, 'disaster_type' => $disasterType, 'source_type' => $sourceType, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); throw $e; } } /** * 铲除作物 * * @param int $userId * @param int $landId * @param int $toolItemId 使用的工具物品ID,0表示手动铲除 * @return array 返回操作结果和状态变更信息 */ public static function removeCrop(int $userId, int $landId, int $toolItemId = 0): array { try { // 获取铲除前的土地状态 $land = \App\Module\Farm\Models\FarmLand::where('id', $landId) ->where('user_id', $userId) ->first(); if (!$land) { throw new \Exception('土地不存在'); } $oldStatus = $land->status; $cropLogic = new CropLogic(); $result = $cropLogic->removeCrop($userId, $landId, $toolItemId); if ($result) { // 获取铲除后的土地状态 $land->refresh(); $newStatus = $land->status; return [ 'success' => true, 'status_changed' => $oldStatus !== $newStatus, 'old_status' => $oldStatus, 'new_status' => $newStatus ]; } return ['success' => false]; } catch (\Exception $e) { Log::error('铲除作物失败', [ 'user_id' => $userId, 'land_id' => $landId, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); throw $e; // 重新抛出异常,由调用方处理 } } /** * 强制删除作物(物理删除,谨慎使用) * * 此方法会永久删除作物记录,主要用于: * 1. 数据清理和维护 * 2. 测试环境的数据重置 * 3. 特殊的管理员操作 * * @param int $userId 用户ID * @param int $landId 土地ID * @param string $reason 删除原因 * @return array 返回操作结果 */ public static function forceDeleteCrop(int $userId, int $landId, string $reason = '管理员操作'): array { try { $cropLogic = new CropLogic(); $result = $cropLogic->forceDeleteCrop($userId, $landId, $reason); return [ 'success' => $result, 'message' => $result ? '强制删除作物成功' : '强制删除作物失败' ]; } catch (\Exception $e) { Log::error('强制删除作物失败', [ 'user_id' => $userId, 'land_id' => $landId, 'reason' => $reason, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); throw $e; } } /** * 恢复软删除的作物 * * @param int $userId 用户ID * @param int $landId 土地ID * @return array 返回操作结果 */ public static function restoreCrop(int $userId, int $landId): array { try { $cropLogic = new CropLogic(); $result = $cropLogic->restoreCrop($userId, $landId); return [ 'success' => $result, 'message' => $result ? '恢复作物成功' : '恢复作物失败' ]; } catch (\Exception $e) { Log::error('恢复作物失败', [ 'user_id' => $userId, 'land_id' => $landId, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); throw $e; } } /** * 获取软删除的作物信息 * * @param int $landId 土地ID * @return CropInfoDto|null */ public static function getTrashedCropByLandId(int $landId): ?CropInfoDto { try { $cropLogic = new CropLogic(); return $cropLogic->getTrashedCropByLandId($landId); } catch (\Exception $e) { Log::error('获取软删除作物信息失败', [ 'land_id' => $landId, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return null; } } }