fix(other): prefer exact country region match

This commit is contained in:
hy001 2026-05-06 20:18:39 +08:00
parent 7d73423199
commit 194fe0cb0a
2 changed files with 47 additions and 0 deletions

View File

@ -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);
}
/**
* 检查两个用户在业务分区是否一致.

View File

@ -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<Long, String> 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)