value): array { try { // 验证访问权限 $permissionCheck = PermissionLogic::checkVisitPermission($visitorId, $ownerId); if (!$permissionCheck['allowed']) { return [ 'success' => false, 'code' => 'NO_PERMISSION', 'message' => $permissionCheck['reason'] ]; } // 执行访问逻辑 $visitResult = VisitLogic::recordVisit($visitorId, $ownerId, $visitType); // 获取农场信息 $farmInfo = static::getFriendFarmInfo($ownerId, $visitorId); return [ 'success' => true, 'data' => [ 'visit_id' => $visitResult['visit_id'], 'farm_info' => $farmInfo, 'permissions' => $permissionCheck['permissions'] ] ]; } catch (\Exception $e) { Log::error('访问好友农场失败', [ 'visitor_id' => $visitorId, 'owner_id' => $ownerId, 'error' => $e->getMessage() ]); return [ 'success' => false, 'code' => 'VISIT_FAILED', 'message' => '访问失败,请稍后重试' ]; } } /** * 偷菜操作 * * @param int $stealerId 偷菜者ID * @param int $ownerId 农场主ID * @param int $landId 土地ID * @return array 偷菜结果 */ public static function stealCrop(int $stealerId, int $ownerId, int $landId): array { try { // 开启事务 return DB::transaction(function () use ($stealerId, $ownerId, $landId) { // 验证偷菜条件 $canSteal = StealLogic::canSteal($stealerId, $ownerId, $landId); if (!$canSteal['allowed']) { return [ 'success' => false, 'code' => $canSteal['code'], 'message' => $canSteal['reason'] ]; } // 执行偷菜 $stealResult = StealLogic::executeSteal($stealerId, $ownerId, $landId); // 记录访问(如果还没有记录) VisitLogic::recordVisit($stealerId, $ownerId, VISIT_TYPE::STEAL_VISIT->value); return [ 'success' => true, 'data' => $stealResult ]; }); } catch (\Exception $e) { Log::error('偷菜操作失败', [ 'stealer_id' => $stealerId, 'owner_id' => $ownerId, 'land_id' => $landId, 'error' => $e->getMessage() ]); return [ 'success' => false, 'code' => 'STEAL_FAILED', 'message' => '偷菜失败,请稍后重试' ]; } } /** * 互助操作 * * @param int $helperId 帮助者ID * @param int $ownerId 农场主ID * @param int $landId 土地ID * @param int $helpType 帮助类型 * @return array 互助结果 */ public static function helpFriend(int $helperId, int $ownerId, int $landId, int $helpType): array { try { // 开启事务 return DB::transaction(function () use ($helperId, $ownerId, $landId, $helpType) { // 验证互助条件 $canHelp = HelpLogic::canHelp($helperId, $ownerId, $landId, $helpType); if (!$canHelp['allowed']) { return [ 'success' => false, 'code' => $canHelp['code'], 'message' => $canHelp['reason'] ]; } // 执行互助 $helpResult = HelpLogic::executeHelp($helperId, $ownerId, $landId, $helpType); // 记录访问(如果还没有记录) VisitLogic::recordVisit($helperId, $ownerId, VISIT_TYPE::HELP_VISIT->value); return [ 'success' => true, 'data' => $helpResult ]; }); } catch (\Exception $e) { Log::error('互助操作失败', [ 'helper_id' => $helperId, 'owner_id' => $ownerId, 'land_id' => $landId, 'help_type' => $helpType, 'error' => $e->getMessage() ]); return [ 'success' => false, 'code' => 'HELP_FAILED', 'message' => '互助失败,请稍后重试' ]; } } /** * 获取可偷菜的好友列表 * * @param int $userId 用户ID * @return array 好友列表 */ public static function getStealableFriends(int $userId): array { try { // 获取好友列表 $friends = \App\Module\Friend\Services\FriendService::getFriendList($userId); $stealableFriends = []; foreach ($friends['items'] as $friend) { $friendId = $friend['user_id']; // 检查是否允许偷菜 $permission = PermissionLogic::checkStealPermission($userId, $friendId); if (!$permission['allowed']) { continue; } // 获取可偷菜的作物数量 $stealableCount = StealLogic::getStealableCropCount($friendId); if ($stealableCount > 0) { $friend['stealable_count'] = $stealableCount; $friend['last_visit_time'] = VisitLogic::getLastVisitTime($userId, $friendId); $stealableFriends[] = $friend; } } return [ 'success' => true, 'data' => $stealableFriends ]; } catch (\Exception $e) { Log::error('获取可偷菜好友列表失败', [ 'user_id' => $userId, 'error' => $e->getMessage() ]); return [ 'success' => false, 'message' => '获取好友列表失败' ]; } } /** * 获取好友农场信息 * * @param int $ownerId 农场主ID * @param int $visitorId 访问者ID * @return array 农场信息 */ public static function getFriendFarmInfo(int $ownerId, int $visitorId): array { try { // 获取农场基础信息 $farmInfo = \App\Module\Farm\Services\FarmService::getFarmInfo($ownerId); // 获取土地信息(包含作物状态) $lands = \App\Module\Farm\Services\LandService::getUserLands($ownerId); // 为每块土地添加社交相关信息 foreach ($lands as &$land) { if ($land['crop']) { // 检查是否可偷菜 $canSteal = StealLogic::canStealCrop($visitorId, $ownerId, $land['id']); $land['can_steal'] = $canSteal['allowed']; $land['steal_reason'] = $canSteal['reason'] ?? ''; // 检查是否可互助 $canHelp = HelpLogic::getAvailableHelpTypes($visitorId, $ownerId, $land['id']); $land['help_types'] = $canHelp; } } return [ 'farm_info' => $farmInfo, 'lands' => $lands, 'owner_settings' => static::getUserSocialSettings($ownerId) ]; } catch (\Exception $e) { Log::error('获取好友农场信息失败', [ 'owner_id' => $ownerId, 'visitor_id' => $visitorId, 'error' => $e->getMessage() ]); return []; } } /** * 获取用户社交设置 * * @param int $userId 用户ID * @return array 社交设置 */ public static function getUserSocialSettings(int $userId): array { $settings = SocialFarmSetting::where('user_id', $userId)->first(); if (!$settings) { // 创建默认设置 $settings = SocialFarmSetting::create(['user_id' => $userId]); } return [ 'allow_steal' => $settings->allow_steal, 'allow_help' => $settings->allow_help, 'allow_visit' => $settings->allow_visit, 'steal_protection_hours' => $settings->steal_protection_hours, 'daily_steal_limit' => $settings->daily_steal_limit, 'daily_help_limit' => $settings->daily_help_limit, 'notification_enabled' => $settings->notification_enabled, 'friend_only' => $settings->friend_only, ]; } /** * 更新用户社交设置 * * @param int $userId 用户ID * @param array $settings 设置数据 * @return array 更新结果 */ public static function updateUserSocialSettings(int $userId, array $settings): array { try { $userSettings = SocialFarmSetting::where('user_id', $userId)->first(); if (!$userSettings) { $userSettings = new SocialFarmSetting(['user_id' => $userId]); } // 更新设置 $allowedFields = [ 'allow_steal', 'allow_help', 'allow_visit', 'steal_protection_hours', 'daily_steal_limit', 'daily_help_limit', 'notification_enabled', 'auto_revenge', 'friend_only' ]; foreach ($allowedFields as $field) { if (isset($settings[$field])) { $userSettings->$field = $settings[$field]; } } $userSettings->save(); return [ 'success' => true, 'data' => $userSettings->toArray() ]; } catch (\Exception $e) { Log::error('更新用户社交设置失败', [ 'user_id' => $userId, 'settings' => $settings, 'error' => $e->getMessage() ]); return [ 'success' => false, 'message' => '设置更新失败' ]; } } /** * 获取用户社交统计 * * @param int $userId 用户ID * @param string $date 统计日期(可选,默认今天) * @return array 统计数据 */ public static function getUserSocialStats(int $userId, string $date = null): array { try { $date = $date ?: date('Y-m-d'); $stats = \App\Module\SocialFarm\Models\SocialFarmStats::where('user_id', $userId) ->where('stat_date', $date) ->first(); if (!$stats) { return [ 'steal_count' => 0, 'stolen_count' => 0, 'help_count' => 0, 'helped_count' => 0, 'visit_count' => 0, 'visited_count' => 0, 'steal_items_gained' => 0, 'steal_items_lost' => 0, 'help_rewards_gained' => 0, 'total_exp_gained' => 0, ]; } return $stats->toArray(); } catch (\Exception $e) { Log::error('获取用户社交统计失败', [ 'user_id' => $userId, 'date' => $date, 'error' => $e->getMessage() ]); return []; } } }