diff --git a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/user/region/impl/SysRegionConfigServiceImpl.java b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/user/region/impl/SysRegionConfigServiceImpl.java index 4636ba76..13bdc1cb 100644 --- a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/user/region/impl/SysRegionConfigServiceImpl.java +++ b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/mongo/service/user/region/impl/SysRegionConfigServiceImpl.java @@ -99,7 +99,8 @@ public class SysRegionConfigServiceImpl implements SysRegionConfigService { SysRegionConfig.class)) .map(region -> region.stream().collect( Collectors.toMap(SysRegionConfig::getId, - v -> StringUtils.isBlankOrElse(v.getRegionName(), "")))) + v -> StringUtils.isBlankOrElse(v.getRegionName(), ""), + (first, second) -> first))) .orElseGet(Maps::newHashMap); } @@ -133,7 +134,8 @@ public class SysRegionConfigServiceImpl implements SysRegionConfigService { SysRegionConfig.class)) .map(region -> region.stream().collect( Collectors.toMap(SysRegionConfig::getId, - v -> StringUtils.isBlankOrElse(v.getRegionName(), "")))) + v -> StringUtils.isBlankOrElse(v.getRegionName(), ""), + (first, second) -> first))) .orElseGet(Maps::newHashMap); } @@ -177,7 +179,8 @@ public class SysRegionConfigServiceImpl implements SysRegionConfigService { return CollectionUtils.newHashMap(); } - return regionConfigs.stream().collect(Collectors.toMap(SysRegionConfig::getId, v -> v)); + return regionConfigs.stream().collect( + Collectors.toMap(SysRegionConfig::getId, v -> v, (first, second) -> first)); } @Override 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 83f27ca0..966e6994 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 @@ -408,7 +408,7 @@ public class UserRegionGatewayImpl implements UserRegionGateway { } Map regionMap = sysRegionConfigs.stream() - .collect(Collectors.toMap(SysRegionConfig::getId, v -> v)); + .collect(Collectors.toMap(SysRegionConfig::getId, v -> v, (first, second) -> first)); if (CollectionUtils.isEmpty(regionMap)) { return Boolean.FALSE; } @@ -775,7 +775,7 @@ public class UserRegionGatewayImpl implements UserRegionGateway { } Map sysRegionConfigMap = sysRegionConfigs.stream() - .collect(Collectors.toMap(SysRegionConfig::getId, v -> v)); + .collect(Collectors.toMap(SysRegionConfig::getId, v -> v, (first, second) -> first)); return teamProfiles.stream() .collect( diff --git a/rc-service/rc-service-other/other-infrastructure/src/test/java/com/red/circle/other/infra/database/mongo/service/user/region/impl/SysRegionConfigServiceImplTest.java b/rc-service/rc-service-other/other-infrastructure/src/test/java/com/red/circle/other/infra/database/mongo/service/user/region/impl/SysRegionConfigServiceImplTest.java new file mode 100644 index 00000000..f53f1650 --- /dev/null +++ b/rc-service/rc-service-other/other-infrastructure/src/test/java/com/red/circle/other/infra/database/mongo/service/user/region/impl/SysRegionConfigServiceImplTest.java @@ -0,0 +1,64 @@ +package com.red.circle.other.infra.database.mongo.service.user.region.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.red.circle.other.infra.database.mongo.entity.user.region.SysRegionConfig; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.query.Query; + +class SysRegionConfigServiceImplTest { + + private static final String REGION_ID = "2051777551323426818"; + private static final String REGION_NAME = "东南亚区"; + + private MongoTemplate mongoTemplate; + private SysRegionConfigServiceImpl service; + + @BeforeEach + void setUp() { + mongoTemplate = mock(MongoTemplate.class); + service = new SysRegionConfigServiceImpl(mongoTemplate); + } + + @Test + void mapRegionName_shouldKeepFirstWhenMongoReturnsDuplicateIds() { + when(mongoTemplate.find(any(Query.class), eq(SysRegionConfig.class))).thenReturn(List.of( + region(REGION_ID, "SEA", REGION_NAME), + region(REGION_ID, "SEA", REGION_NAME) + )); + + Map result = service.mapRegionName("LIKEI"); + + assertEquals(Map.of(REGION_ID, REGION_NAME), result); + } + + @Test + void mapAvailable_shouldKeepFirstWhenMongoReturnsDuplicateIds() { + when(mongoTemplate.find(any(Query.class), eq(SysRegionConfig.class))).thenReturn(List.of( + region(REGION_ID, "SEA", REGION_NAME), + region(REGION_ID, "SEA2", REGION_NAME) + )); + + Map result = service.mapAvailable(Set.of(REGION_ID)); + + assertEquals(1, result.size()); + assertEquals("SEA", result.get(REGION_ID).getRegionCode()); + } + + private SysRegionConfig region(String id, String regionCode, String regionName) { + return new SysRegionConfig() + .setId(id) + .setRegionCode(regionCode) + .setRegionName(regionName) + .setDel(Boolean.FALSE); + } +} 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 da73aeff..a20e4c9e 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,7 @@ 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 SEA_REGION_ID = "2051777551323426818"; private static final String SOUTH_ASIA_REGION_CODE = "南亚"; private static final String PAK_ALEX_REGION_CODE = "Pakistan - rolex"; @@ -158,6 +159,57 @@ class UserRegionGatewayImplTest { verify(countryCodeAliasSupport, never()).containsCode("PK,IN,BD,NP", "P2"); } + @Test + void checkOpenDailyAutoSalary_shouldKeepFirstWhenCacheHasDuplicateRegionIds() { + when(sysRegionCacheService.getCache(SYS_ORIGIN)).thenReturn(List.of( + new SysRegionConfig() + .setId(SEA_REGION_ID) + .setRegionName("东南亚区") + .setMetadata(Map.of("openDailyAutoSalary", "true")), + new SysRegionConfig() + .setId(SEA_REGION_ID) + .setRegionName("东南亚区") + .setMetadata(Map.of("openDailyAutoSalary", "false")) + )); + + Boolean result = userRegionGateway.checkOpenDailyAutoSalary(SEA_REGION_ID, SYS_ORIGIN); + + assertEquals(Boolean.TRUE, result); + } + + @Test + void mapUserRegionConfig_shouldKeepFirstWhenTeamRegionConfigHasDuplicateIds() { + SysRegionConfig firstRegion = new SysRegionConfig() + .setId(SEA_REGION_ID) + .setRegionCode("SEA") + .setRegionName("东南亚区"); + when(teamMemberService.listByMemberIds(Set.of(TEAM_USER_ID))).thenReturn(List.of( + new TeamMember() + .setMemberId(TEAM_USER_ID) + .setTeamId(TEAM_ID) + .setSysOrigin(SYS_ORIGIN) + .setRole(TeamMemberRole.OWN) + )); + when(teamProfileService.listByIds(Set.of(TEAM_ID))).thenReturn(List.of( + new TeamProfile().setId(TEAM_ID).setRegion(SEA_REGION_ID) + )); + when(sysRegionConfigService.listAvailable(Set.of(SEA_REGION_ID))).thenReturn(List.of( + firstRegion, + new SysRegionConfig() + .setId(SEA_REGION_ID) + .setRegionCode("SEA2") + .setRegionName("东南亚区") + )); + when(userRegionInfraConvertor.toRegionConfig(firstRegion)).thenReturn( + new RegionConfig().setId(SEA_REGION_ID).setRegionCode("SEA") + ); + + Map result = userRegionGateway.mapUserRegionConfig( + SYS_ORIGIN, Set.of(TEAM_USER_ID)); + + assertEquals("SEA", result.get(TEAM_USER_ID).getRegionCode()); + } + private SysRegionConfig region(String regionCode, String countryCodes, String langeCodes) { return new SysRegionConfig() .setId(regionCode)