feat(selector): filter send accounts by account type

This commit is contained in:
2026-07-14 17:21:55 +08:00
parent 0bef33bbb8
commit ed76915f56
2 changed files with 36 additions and 4 deletions

View File

@@ -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<Long> selectSendAccounts(String tenantCode, Long conversationId, ChannelType channelType, String preferredStrategy) {
public List<Long> selectSendAccounts(String tenantCode, Long conversationId,
ChannelType channelType, String preferredStrategy) {
return selectSendAccounts(tenantCode, conversationId, channelType, null, preferredStrategy);
}
public List<Long> selectSendAccounts(String tenantCode, Long conversationId,
ChannelType channelType, Set<Integer> accountTypes,
String preferredStrategy) {
if (conversationId == null || channelType == null) {
return Collections.emptyList();
}
@@ -64,9 +72,13 @@ public class AccountSelector {
List<Long> 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;
}

View File

@@ -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<Long> 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);