diff --git a/backend/mci-server/src/main/java/com/sino/mci/channel/service/AccountSelector.java b/backend/mci-server/src/main/java/com/sino/mci/channel/service/AccountSelector.java index e17a5a0..3dc31b8 100644 --- a/backend/mci-server/src/main/java/com/sino/mci/channel/service/AccountSelector.java +++ b/backend/mci-server/src/main/java/com/sino/mci/channel/service/AccountSelector.java @@ -16,6 +16,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; +import java.util.Set; @Slf4j @Service @@ -39,7 +40,14 @@ public class AccountSelector { return CollectionUtils.isEmpty(accounts) ? null : accounts.get(0); } - public List selectSendAccounts(String tenantCode, Long conversationId, ChannelType channelType, String preferredStrategy) { + public List selectSendAccounts(String tenantCode, Long conversationId, + ChannelType channelType, String preferredStrategy) { + return selectSendAccounts(tenantCode, conversationId, channelType, null, preferredStrategy); + } + + public List selectSendAccounts(String tenantCode, Long conversationId, + ChannelType channelType, Set accountTypes, + String preferredStrategy) { if (conversationId == null || channelType == null) { return Collections.emptyList(); } @@ -64,9 +72,13 @@ public class AccountSelector { List healthyAccountIds = new ArrayList<>(); for (ChannelAccountConversation binding : bindings) { ChannelAccount account = accountMapper.selectById(binding.getAccountId()); - if (isHealthy(account, tenantCode, channelType)) { - healthyAccountIds.add(account.getId()); + if (!isHealthy(account, tenantCode, channelType)) { + continue; } + if (accountTypes != null && !accountTypes.contains(account.getAccountType())) { + continue; + } + healthyAccountIds.add(account.getId()); } return healthyAccountIds; } diff --git a/backend/mci-server/src/test/java/com/sino/mci/channel/service/AccountSelectorTest.java b/backend/mci-server/src/test/java/com/sino/mci/channel/service/AccountSelectorTest.java index 87aa18b..1318794 100644 --- a/backend/mci-server/src/test/java/com/sino/mci/channel/service/AccountSelectorTest.java +++ b/backend/mci-server/src/test/java/com/sino/mci/channel/service/AccountSelectorTest.java @@ -14,6 +14,8 @@ import org.mockito.junit.jupiter.MockitoExtension; import java.time.LocalDateTime; import java.util.Arrays; import java.util.Collections; +import java.util.List; +import java.util.Set; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; @@ -86,6 +88,20 @@ class AccountSelectorTest { assertEquals(2L, accountId); } + @Test + void selectSendAccountsFiltersByAccountType() { + ChannelAccountConversation primary = createBinding(1L, 1, 1); + ChannelAccountConversation backup = createBinding(2L, 2, 1); + when(accountConversationMapper.selectList(any())).thenReturn(Arrays.asList(primary, backup)); + when(accountMapper.selectById(1L)).thenReturn(createHealthyAccount(1L, 1)); + when(accountMapper.selectById(2L)).thenReturn(createHealthyAccount(2L, 3)); + + List accountIds = accountSelector.selectSendAccounts( + "tenant", 100L, ChannelType.WECHAT_PERSONAL, Set.of(1), "primary"); + + assertEquals(Collections.singletonList(1L), accountIds); + } + private ChannelAccountConversation createBinding(Long accountId, int bindingType, int sortOrder) { ChannelAccountConversation binding = new ChannelAccountConversation(); binding.setAccountId(accountId); @@ -96,10 +112,14 @@ class AccountSelectorTest { } private ChannelAccount createHealthyAccount(Long id) { + return createHealthyAccount(id, 3); + } + + private ChannelAccount createHealthyAccount(Long id, int accountType) { ChannelAccount account = new ChannelAccount(); account.setId(id); account.setTenantCode("tenant"); - account.setAccountType(3); + account.setAccountType(accountType); account.setChannelType("wechat_personal"); account.setAccountId("wxid-" + id); account.setStatus(1);