| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- <?php
- namespace App\Module\SocialFarm\Services;
- use App\Module\SocialFarm\Logics\StealLogic;
- use App\Module\SocialFarm\Logics\VisitLogic;
- use App\Module\SocialFarm\Logics\HelpLogic;
- use App\Module\SocialFarm\Logics\PermissionLogic;
- use App\Module\SocialFarm\Models\SocialFarmSetting;
- use App\Module\SocialFarm\Enums\STEAL_STATUS;
- use App\Module\SocialFarm\Enums\VISIT_TYPE;
- use App\Module\SocialFarm\Enums\HELP_TYPE;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- /**
- * 社交农场主服务类
- *
- * 提供社交农场功能的统一对外接口
- */
- class SocialFarmService
- {
- /**
- * 访问好友农场
- *
- * @param int $visitorId 访问者ID
- * @param int $ownerId 农场主ID
- * @param int $visitType 访问类型
- * @return array 访问结果
- */
- public static function visitFriendFarm(int $visitorId, int $ownerId, int $visitType = VISIT_TYPE::NORMAL->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 [];
- }
- }
- }
|