From 194fe0cb0abf22437e123d1f54924dcf2aa98d70 Mon Sep 17 00:00:00 2001 From: hy001 Date: Wed, 6 May 2026 20:18:39 +0800 Subject: [PATCH] fix(other): prefer exact country region match --- .../gateway/user/UserRegionGatewayImpl.java | 25 +++++++++++++++++++ .../user/UserRegionGatewayImplTest.java | 22 ++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/gateway/user/UserRegionGatewayImpl.java b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/gateway/user/UserRegionGatewayImpl.java index 2d4a43b2..83f27ca0 100644 --- a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/gateway/user/UserRegionGatewayImpl.java +++ b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/gateway/user/UserRegionGatewayImpl.java @@ -3,6 +3,7 @@ package com.red.circle.other.infra.gateway.user; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.collect.Sets; +import com.red.circle.common.business.core.util.CountryCodeAliasUtils; import com.red.circle.other.domain.gateway.user.UserProfileGateway; import com.red.circle.other.domain.gateway.user.ability.UserRegionGateway; import com.red.circle.other.domain.model.user.UserProfile; @@ -253,6 +254,14 @@ public class UserRegionGatewayImpl implements UserRegionGateway { } if (StringUtils.isNotBlank(countryCode)) { + SysRegionConfig exactMatchedRegion = configs.stream() + .filter(region -> containsExactCountryCode(region.getCountryCodes(), countryCode)) + .findFirst() + .orElse(null); + if (Objects.nonNull(exactMatchedRegion)) { + return exactMatchedRegion; + } + SysRegionConfig matchedRegion = configs.stream() .filter(region -> StringUtils.isNotBlank(region.getCountryCodes()) && countryCodeAliasSupport.containsCode(region.getCountryCodes(), countryCode)) @@ -265,6 +274,22 @@ public class UserRegionGatewayImpl implements UserRegionGateway { return getSysOriginDefaultRegion(configs); } + + private boolean containsExactCountryCode(String csvCodes, String countryCode) { + if (StringUtils.isBlank(csvCodes) || StringUtils.isBlank(countryCode)) { + return false; + } + + String normalizedCountryCode = CountryCodeAliasUtils.normalize(countryCode); + if (StringUtils.isBlank(normalizedCountryCode)) { + return false; + } + + return Arrays.stream(csvCodes.split(",")) + .map(CountryCodeAliasUtils::normalize) + .filter(StringUtils::isNotBlank) + .anyMatch(normalizedCountryCode::equals); + } /** * 检查两个用户在业务分区是否一致. diff --git a/rc-service/rc-service-other/other-infrastructure/src/test/java/com/red/circle/other/infra/gateway/user/UserRegionGatewayImplTest.java b/rc-service/rc-service-other/other-infrastructure/src/test/java/com/red/circle/other/infra/gateway/user/UserRegionGatewayImplTest.java index 0aaee1bb..da73aeff 100644 --- a/rc-service/rc-service-other/other-infrastructure/src/test/java/com/red/circle/other/infra/gateway/user/UserRegionGatewayImplTest.java +++ b/rc-service/rc-service-other/other-infrastructure/src/test/java/com/red/circle/other/infra/gateway/user/UserRegionGatewayImplTest.java @@ -37,6 +37,8 @@ class UserRegionGatewayImplTest { private static final String SYS_ORIGIN = "LIKEI"; private static final String TEAM_REGION_ID = "middle-east-region"; private static final String TEAM_REGION_CODE = "中东区"; + private static final String SOUTH_ASIA_REGION_CODE = "南亚"; + private static final String PAK_ALEX_REGION_CODE = "Pakistan - rolex"; private TeamMemberService teamMemberService; private TeamProfileService teamProfileService; @@ -136,6 +138,26 @@ class UserRegionGatewayImplTest { verify(userProfileGateway, never()).getLanguage(TEAM_USER_ID); } + @Test + void mapRegionCode_shouldPreferExactCountryRegionBeforeBroadAliasRegion() { + when(sysRegionCacheService.getCache(SYS_ORIGIN)).thenReturn(List.of( + region("OTHER", "", ""), + region(SOUTH_ASIA_REGION_CODE, "PK,IN,BD,NP", "en,bn"), + region(PAK_ALEX_REGION_CODE, "P2", "en") + )); + UserProfile userProfile = new UserProfile(); + userProfile.setId(USER_ID); + userProfile.setOriginSys(SYS_ORIGIN); + userProfile.setCountryCode("P2"); + when(userProfileGateway.listByUserIds(Set.of(USER_ID))).thenReturn(List.of(userProfile)); + when(countryCodeAliasSupport.containsCode("PK,IN,BD,NP", "P2")).thenReturn(true); + + Map result = userRegionGateway.mapRegionCode(Set.of(USER_ID)); + + assertEquals(PAK_ALEX_REGION_CODE, result.get(USER_ID)); + verify(countryCodeAliasSupport, never()).containsCode("PK,IN,BD,NP", "P2"); + } + private SysRegionConfig region(String regionCode, String countryCodes, String langeCodes) { return new SysRegionConfig() .setId(regionCode)