Ver Fonte

飞书看课-修改使用个人账户的逻辑

cgp há 6 dias atrás
pai
commit
53a3a8f4ea

+ 32 - 2
fs-service/src/main/java/com/fs/feishu/service/FeiShuService.java

@@ -59,6 +59,9 @@ public class FeiShuService {
 
     private static final String FEISHU_CLIENT = "fei_shu_client:";
 
+    /** 个人账号轮询计数器 key 前缀 */
+    private static final String FEISHU_PERSONAL_RR_KEY = "feishu:personal_rr:";
+
     // ==================== 获取销售个人账号(带缓存) ====================
 
     public List<FeishuAccount> getFeishuAccountCompanyUser(Long companyId, Long companyUserId) {
@@ -85,6 +88,25 @@ public class FeiShuService {
         return feishuAccounts;
     }
 
+    /**
+     * 个人账号轮询:每次调用自动递增计数器,返回本轮起始索引。
+     * 多个请求进来时,各自从不同位置开始遍历,实现"公平调用"。
+     *
+     * @param companyId       公司 ID
+     * @param companyUserId   销售用户 ID
+     * @param accountSize     账号总数
+     * @return 本轮起始索引(0 ~ accountSize-1)
+     */
+    private int getPersonalRoundRobinIndex(Long companyId, Long companyUserId, int accountSize) {
+        if (accountSize <= 1) {
+            return 0;
+        }
+        String key = FEISHU_PERSONAL_RR_KEY + companyId + ":" + companyUserId;
+        Long counter = redisCache.incr(key, 1L);
+        redisCache.expire(key, 1, TimeUnit.DAYS);
+        return (int) (counter % accountSize);
+    }
+
 
     public String getNewFeishuRegisterLink(Long videoId, Long companyId, Long courseId,
                                         Long companyUserId, String shortLink) {
@@ -112,7 +134,11 @@ public class FeiShuService {
         // 1. 尝试个人账号
         List<FeishuAccount> personalAccounts = getFeishuAccountCompanyUser(param.getCompanyId(), param.getCompanyUserId());
         if (!personalAccounts.isEmpty()) {
-            for (FeishuAccount account : personalAccounts) {
+            int startIndex = getPersonalRoundRobinIndex(param.getCompanyId(), param.getCompanyUserId(), personalAccounts.size());
+            int size = personalAccounts.size();
+            for (int j = 0; j < size; j++) {
+                int i = (startIndex + j) % size;
+                FeishuAccount account = personalAccounts.get(i);
                 if (account.getStatus() != 1) continue;
                 Client client = buildClient(account);
                 try {
@@ -232,7 +258,11 @@ public class FeiShuService {
         // 1. 尝试个人账号
         List<FeishuAccount> personalAccounts = getFeishuAccountCompanyUser(param.getCompanyId(), param.getCompanyUserId());
         if (!personalAccounts.isEmpty()) {
-            for (FeishuAccount account : personalAccounts) {
+            int startIndex = getPersonalRoundRobinIndex(param.getCompanyId(), param.getCompanyUserId(), personalAccounts.size());
+            int size = personalAccounts.size();
+            for (int j = 0; j < size; j++) {
+                int i = (startIndex + j) % size;
+                FeishuAccount account = personalAccounts.get(i);
                 if (account.getStatus() != 1) continue;
                 Client client = buildClient(account);
                 try {