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 $seedId * @return array|null 返回包含CropInfoDto和日志ID的数组,格式为['crop' => CropInfoDto, 'log_id' => int] */ public static function plantCrop(int $userId, int $landId, int $seedId): ?array { try { $cropLogic = new CropLogic(); return $cropLogic->plantCrop($userId, $landId, $seedId); } catch (\Exception $e) { Log::error('种植作物失败', [ 'user_id' => $userId, 'land_id' => $landId, 'seed_id' => $seedId, '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(); } } /** * 使用化肥 * * @param int $userId * @param int $landId * @return Res */ public static function useFertilizer(int $userId, int $landId,int $crop_growth_time): Res { try { $cropLogic = new CropLogic(); return $cropLogic->useFertilizer($userId, $landId,$crop_growth_time); } catch (\Exception $e) { Log::error('使用化肥失败', [ 'user_id' => $userId, 'land_id' => $landId, '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 * @return bool */ public static function removeCrop(int $userId, int $landId): bool { try { $cropLogic = new CropLogic(); return $cropLogic->removeCrop($userId, $landId); } catch (\Exception $e) { Log::error('铲除作物失败', [ 'user_id' => $userId, 'land_id' => $landId, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return false; } } }