diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/room/query/RoomVoiceDiscoverQryExe.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/room/query/RoomVoiceDiscoverQryExe.java index 010dc437..2035bdff 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/room/query/RoomVoiceDiscoverQryExe.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/command/room/query/RoomVoiceDiscoverQryExe.java @@ -68,12 +68,6 @@ public class RoomVoiceDiscoverQryExe { private final MicUserCacheService micUserCacheService; private static final String EMPTY_ROOM_CACHE_KEY = "empty_room_cache:*"; - - // 区域隔离常量 - 与ActiveVoiceRoomServiceImpl保持一致 - private static final Set TR_REGIONS = Set.of("TR"); - private static final Set SA_REGIONS = Set.of("BD","SA", "IN", "PK"); - private static final Set AR_REGIONS = Set.of("AE", "AR", "BH", "DJ", "DZ", "EG", "EY", "ER", "IL", "IQ", "JO", "KM", "KW", "LB", "LY", "MA", "MR", "OM", "PS", "QA", "SD", "SO", "SS", "SY", "TD", "TN", "YE"); - private static final Set OTHER_REGIONS = Set.of("OTHER", "PH", "ID", "GH", "IR", "AF", "NG", "DE", "MYS", "TM", "AZ", "NP"); public List execute(AppExtCommand cmd) { String region = cmd.isAllRegion() ? "ALL" : userRegionGateway.getRegionCode(cmd.requiredReqUserId()); @@ -86,13 +80,12 @@ public class RoomVoiceDiscoverQryExe { return roomList; } - String userCountryCode = userProfile != null ? userProfile.getCountryCode() : null; - List activeVoiceRooms = activeVoiceRoomService.listDiscover(cmd.requireReqSysOrigin(), cmd.isAllRegion(), region, userCountryCode, 50); + List activeVoiceRooms = activeVoiceRoomService.listDiscover(cmd.requireReqSysOrigin(), cmd.isAllRegion(), region, 50); // 查询没人的房间缓存,加入进去 List emptyRooms = getEmptyRoomsFromCache(); if (CollectionUtils.isNotEmpty(emptyRooms)) { - List matchedEmptyRooms = filterEmptyRoomsByRegion(emptyRooms, cmd.requireReqSysOrigin(), cmd.isAllRegion(), region, userCountryCode); + List matchedEmptyRooms = filterEmptyRoomsByRegion(emptyRooms, cmd.requireReqSysOrigin(), cmd.isAllRegion(), region); if (CollectionUtils.isNotEmpty(matchedEmptyRooms)) { List allRooms = new ArrayList<>(activeVoiceRooms); Set existingRoomIds = activeVoiceRooms.stream() @@ -287,95 +280,14 @@ public class RoomVoiceDiscoverQryExe { } } - private List filterEmptyRoomsByRegion(List emptyRooms, String sysOrigin, boolean isAllRegion, String region, String userCountryCode) { + private List filterEmptyRoomsByRegion(List emptyRooms, String sysOrigin, boolean isAllRegion, String region) { return emptyRooms.stream() - .filter(room -> Objects.equals(sysOrigin, room.getSysOrigin()) && - (isAllRegion || shouldShowRoom(region, room.getRegion())) && - (!SA_REGIONS.contains(region) || userCountryCode == null || userCountryCode.equals(room.getCountryCode()))) + .filter(room -> Objects.equals(sysOrigin, room.getSysOrigin())) + .filter(room -> isAllRegion || Objects.equals(region, room.getRegion())) .collect(Collectors.toList()); } - /** - * 判断是否应该显示该房间 - */ - private boolean shouldShowRoom(String userRegion, String roomRegion) { - // 获取用户所属区域组 - Set userRegionGroup = getRegionGroup(userRegion); - Set roomRegionGroup = getRegionGroup(roomRegion); - - // 如果用户在特殊区域组,只显示同组房间 - if (userRegionGroup != null) { - return userRegionGroup.contains(roomRegion); - } - - // 如果用户不在特殊区域组,排除所有特殊区域组房间 - return roomRegionGroup == null; - } - - /** - * 获取区域所属的特殊区域组(如果不属于任何特殊组则返回null) - */ - private Set getRegionGroup(String region) { - if (SA_REGIONS.contains(region)) { - return SA_REGIONS; - } - if (AR_REGIONS.contains(region)) { - return AR_REGIONS; - } - if (OTHER_REGIONS.contains(region)) { - return OTHER_REGIONS; - } - if (TR_REGIONS.contains(region)) { - return TR_REGIONS; - } - return null; - } - - - /** - * 获取需要排除的区域列表 - */ - private List getRegions(String region) { - List excludeRegions = new ArrayList<>(); - - if (SA_REGIONS.contains(region)) { - excludeRegions.addAll(AR_REGIONS); - excludeRegions.addAll(OTHER_REGIONS); - } else if (AR_REGIONS.contains(region)) { - excludeRegions.addAll(SA_REGIONS); - excludeRegions.addAll(OTHER_REGIONS); - } else if (OTHER_REGIONS.contains(region)) { - excludeRegions.addAll(SA_REGIONS); - excludeRegions.addAll(AR_REGIONS); - } else { - excludeRegions.addAll(SA_REGIONS); - excludeRegions.addAll(AR_REGIONS); - excludeRegions.addAll(OTHER_REGIONS); - } - - return excludeRegions; - } - - /** - * 获取特殊区域组列表 - */ - private List getSpecialRegions(String region) { - if (SA_REGIONS.contains(region)) { - return new ArrayList<>(SA_REGIONS); - } - - if (AR_REGIONS.contains(region)) { - return new ArrayList<>(AR_REGIONS); - } - - if (OTHER_REGIONS.contains(region)) { - return new ArrayList<>(OTHER_REGIONS); - } - - return new ArrayList<>(); - } - /** * 从缓存中获取没人的房间 * diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/scheduler/EmptyRoomCleanTask.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/scheduler/EmptyRoomCleanTask.java index 4ccd10d1..774d7d19 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/scheduler/EmptyRoomCleanTask.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/scheduler/EmptyRoomCleanTask.java @@ -37,7 +37,7 @@ public class EmptyRoomCleanTask { public void cleanEmptyRooms() { try { // 获取所有活跃房间 - List allRooms = activeVoiceRoomService.listDiscover(SysOriginPlatformEnum.ATYOU.name(),true,"",null,200); + List allRooms = activeVoiceRoomService.listDiscover(SysOriginPlatformEnum.ATYOU.name(), true, "", 200); if (CollectionUtils.isEmpty(allRooms)) { return; } diff --git a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/live/ActiveVoiceRoomService.java b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/live/ActiveVoiceRoomService.java index 5d5383d4..02a2878c 100644 --- a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/live/ActiveVoiceRoomService.java +++ b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/live/ActiveVoiceRoomService.java @@ -65,7 +65,7 @@ public interface ActiveVoiceRoomService { /** * 获取发现页数据. */ - List listDiscover(String sysOrigin, boolean isAllRegion, String region, String countryCode, Integer limit); + List listDiscover(String sysOrigin, boolean isAllRegion, String region, Integer limit); /** * 获取区域房间. diff --git a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/live/impl/ActiveVoiceRoomServiceImpl.java b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/live/impl/ActiveVoiceRoomServiceImpl.java index b8a8d413..5c00dce4 100644 --- a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/live/impl/ActiveVoiceRoomServiceImpl.java +++ b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/live/impl/ActiveVoiceRoomServiceImpl.java @@ -1,6 +1,5 @@ package com.red.circle.other.infra.database.mongo.service.live.impl; -import com.alibaba.fastjson.JSON; import com.red.circle.common.business.core.enums.SysOriginPlatformEnum; import com.red.circle.framework.mybatis.constant.PageConstant; import com.red.circle.other.infra.database.mongo.dto.query.live.ActiveVoiceRoomQuery; @@ -19,8 +18,6 @@ import org.springframework.dao.DuplicateKeyException; import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Order; import org.springframework.data.mongodb.core.MongoTemplate; -import org.springframework.data.mongodb.core.aggregation.Aggregation; -import org.springframework.data.mongodb.core.aggregation.ConditionalOperators; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Update; @@ -36,21 +33,6 @@ public class ActiveVoiceRoomServiceImpl implements ActiveVoiceRoomService { private final MongoTemplate mongoTemplate; - private static final Set TR_REGIONS = Set.of("TR"); - private static final Set SA_REGIONS = Set.of("BD","SA", "IN", "PK"); - private static final Set AR_REGIONS = Set.of("AE", "AR", "BH", "DJ", "DZ", "EG", "EY", "ER", "IL", "IQ", "JO", "KM", "KW", "LB", "LY", "MA", "MR", "OM", "PS", "QA", "SD", "SO", "SS", "SY", "TD", "TN", "YE"); - private static final Set OTHER_REGIONS = Set.of("OTHER", "PH", "ID", "GH", "IR", "AF", "NG", "DE", "MYS", "TM", "AZ", "NP"); - private static final Set ALL_SPECIAL_REGIONS; - - static { - Set temp = new HashSet<>(); - temp.addAll(SA_REGIONS); - temp.addAll(AR_REGIONS); - temp.addAll(TR_REGIONS); - temp.addAll(OTHER_REGIONS); - ALL_SPECIAL_REGIONS = Collections.unmodifiableSet(temp); - } - @Override public void saveOrUpdate(ActiveVoiceRoom param) { try { @@ -133,103 +115,22 @@ public class ActiveVoiceRoomServiceImpl implements ActiveVoiceRoomService { @Override - public List listDiscover(String sysOrigin, boolean isAllRegion, String region, String countryCode, Integer limit) { - // 区域隔离 - Set userRegionGroup = getRegionGroup(region); - Set excludeRegions = userRegionGroup == null ? ALL_SPECIAL_REGIONS : getExcludeRegionsForGroup(userRegionGroup); - - log.info("被隔离区域 regions:{}", JSON.toJSONString(excludeRegions)); - - // 构建聚合管道 + public List listDiscover(String sysOrigin, boolean isAllRegion, String region, Integer limit) { Criteria criteria = Criteria.where("sysOrigin").is(sysOrigin); - if (userRegionGroup != null && !isAllRegion) { - criteria.and("region").in(userRegionGroup); + // 非全部区域时,仅展示用户当前 region 的房间 + if (!isAllRegion && StringUtils.isNotBlank(region)) { + criteria.and("region").is(region); } - if (userRegionGroup == null && !isAllRegion) { - criteria.and("region").nin(excludeRegions); - } - // SA_REGIONS 用户只能看到相同国家的房间 - if (SA_REGIONS.contains(region) && !isAllRegion && StringUtils.isNotBlank(countryCode)) { - criteria.and("countryCode").is(countryCode); - } - Aggregation aggregation = Aggregation.newAggregation( - // 1. 匹配国家(可选条件) - Aggregation.match(criteria), - // 2. 计算匹配分数和动态调整 fixedWeights - Aggregation.project("id", "timeId", "sysOrigin", "userId", "roomAccount", "familyProfile", "runGame", "superVipLevel", "hot", "fixedWeights", "region", "countryCode", "countryName", "onlineQuantity", "weights", "expiredTime", "createTime", "updateTime") - .and( - ConditionalOperators.when(Criteria.where("region").is(region)) - .then(1) - .otherwise(0) - ).as("regionMatchScore") - .and( - ConditionalOperators.when(Criteria.where("region").is(region)) - .then("$fixedWeights") - .otherwise(0) - ).as("fixedWeightsScore") - .and("weights").as("weights"), - - // 3. 排序逻辑 - Aggregation.sort(Sort.by( - Sort.Order.desc("regionMatchScore"), - Sort.Order.desc("fixedWeightsScore"), // 如果区域不匹配,fixedWeights 为 0,不影响排序 + Query query = Query.query(criteria) + .with(Sort.by( + Sort.Order.desc("fixedWeights"), Sort.Order.desc("weights"), Sort.Order.desc("onlineQuantity") - )), + )) + .limit(limit); - // 4. 分页 - Aggregation.skip(0), - Aggregation.limit(limit) - ); - - // 执行聚合操作并返回结果 - return mongoTemplate.aggregate(aggregation, ActiveVoiceRoom.class, ActiveVoiceRoom.class) - .getMappedResults(); - } - - /** - * 获取区域所属的特殊区域组(如果不属于任何特殊组则返回null) - */ - private Set getRegionGroup(String region) { - if (SA_REGIONS.contains(region)) { - return SA_REGIONS; - } - if (AR_REGIONS.contains(region)) { - return AR_REGIONS; - } - if (OTHER_REGIONS.contains(region)) { - return OTHER_REGIONS; - } - if (TR_REGIONS.contains(region)) { - return TR_REGIONS; - } - return null; - } - - /** - * 获取指定区域组需要排除的其他区域组 - */ - private Set getExcludeRegionsForGroup(Set regionGroup) { - Set excludes = new HashSet<>(); - if (regionGroup == SA_REGIONS) { - excludes.addAll(AR_REGIONS); - excludes.addAll(OTHER_REGIONS); - excludes.addAll(TR_REGIONS); - } else if (regionGroup == AR_REGIONS) { - excludes.addAll(SA_REGIONS); - excludes.addAll(OTHER_REGIONS); - excludes.addAll(TR_REGIONS); - } else if (regionGroup == OTHER_REGIONS) { - excludes.addAll(SA_REGIONS); - excludes.addAll(AR_REGIONS); - excludes.addAll(TR_REGIONS); - } else if (regionGroup == TR_REGIONS) { - excludes.addAll(SA_REGIONS); - excludes.addAll(AR_REGIONS); - excludes.addAll(OTHER_REGIONS); - } - return excludes; + return mongoTemplate.find(query, ActiveVoiceRoom.class); }