瀏覽代碼

fix 车队计算次数

chenbiao 2 年之前
父節點
當前提交
e80494614f

+ 0 - 1
netflix-service/src/main/java/com/cyksj/redis/RedisService.java

@@ -821,7 +821,6 @@ public class RedisService {
         CLOSE_ORDER_TIME_KEY("close_order_time_key:", "关闭订单key", 3 * 60),
         CHATGPT_CONVERSATION_LIMIT("chatgpt:conversation:limit:", "chatGpt 对话限制", 3 * 60 * 60L),
 
-        CHATGPT_CAR_CONVERSATION_LIMIT("chatgpt:conversation:car:", "chatGpt 车队对话数量", 3 * 60 * 60L),
         CHATGPT_CAR_SCORES("chatgpt:car:scores","chatGpt 车队分数", 48 * 60 * 60L),
         CHATGPT_CAR_HIGH_CHAT("chatgpt:car:high:chat:","chatGpt gpt4 对话次数", 48 * 60 * 60L),
         CHATGPT_CAR_LOW_CHAT("chatgpt:car:low:chat:","chatGpt 3.5 对话次数", 48 * 60 * 60L),

+ 10 - 41
netflix-service/src/main/java/com/cyksj/service/chatgpt/impl/ChatGptAccountServiceImpl.java

@@ -383,6 +383,10 @@ public class ChatGptAccountServiceImpl implements ChatGptAccountService {
             }
         }
         ChatgptSession chatgptSession = chatgptSessionMapper.selectOne(wrapper);
+        if(conversationRequest.getCarId() == null && chatgptSession.getIsCar() && chatgptSession.getCarId() != null){
+            conversationRequest.setCarId(chatgptSession.getCarId());
+        }
+        conversationRequest.setCarId(chatgptSession.getCarId());
         ChatgptUserConversationRecord chatgptUserConversationRecord = new ChatgptUserConversationRecord();
         chatgptUserConversationRecord.setUserToken(userToken);
         chatgptUserConversationRecord.setCarId(chatgptSession.getCarId());
@@ -412,21 +416,6 @@ public class ChatGptAccountServiceImpl implements ChatGptAccountService {
             if (chatgptUser.getIsCar()) {
                 conversationLimitResponse = isConversationAllowed(userToken, chatgptUser.getLimitNum(), chatgptUser.getLimitTime());
             }
-            if(carId == null){
-                if (StringUtils.isNotBlank(userToken)) {
-                    if (chatgptUser.getSessionId() != null) {
-                        ChatgptSession chatgptSession = chatgptSessionMapper.selectById(chatgptUser.getSessionId());
-                        if(chatgptSession != null){
-                            carId = chatgptSession.getCarId();
-                        }
-                    }
-                }
-            }
-            //车队次数记录
-            String finalCarId = carId;
-            TASK_EXECUTOR.execute(() -> {
-                cardConversationRecord(finalCarId);
-            });
         }
 
         return conversationLimitResponse;
@@ -470,29 +459,6 @@ public class ChatGptAccountServiceImpl implements ChatGptAccountService {
     }
 
 
-    /**
-     * 记录车队的提问次数
-     */
-    public boolean cardConversationRecord(String carid) {
-        String key = RedisService.key.CHATGPT_CAR_CONVERSATION_LIMIT.getName() + ":" + carid;
-        long currentTimeMillis = System.currentTimeMillis();
-        long windowStartMillis = currentTimeMillis - (WINDOW_SIZE) * 1000;
-
-        // 清除时间窗口之前的请求记录
-        redisService.zRemoveRangeByScore(key, 0, windowStartMillis);
-
-        Long currentSize = redisService.zCard(key);
-        if (currentSize != null && currentSize >= MAX_REQUESTS) {
-            // 如果当前请求次数超过限制,则拒绝请求
-            return false;
-        } else {
-            // 如果未超过限制,记录当前请求的时间戳
-            redisService.zAdd(key, currentTimeMillis, currentTimeMillis);
-            // 设置ZSet的过期时间,窗口大小加上一段冗余时间
-            redisService.expire(key, (WINDOW_SIZE * 60 * 60) + 20);
-            return true;
-        }
-    }
 
     /**
      * 获取指定用户ID在滑动窗口内的提问次数
@@ -521,9 +487,12 @@ public class ChatGptAccountServiceImpl implements ChatGptAccountService {
      * @return 滑动窗口内的请求次数
      */
     private Long getCarConversationCount(String carId, Long limitTime) {
-        String key = RedisService.key.CHATGPT_CAR_CONVERSATION_LIMIT.getName() + ":" + carId;
-        long currentTimeMillis = System.currentTimeMillis();
-        long windowStartMillis = currentTimeMillis - (limitTime * 60 * 60) * 1000;
+        // 定义键名
+        String key = RedisService.key.CHATGPT_CAR_HIGH_CHAT.getName() + carId;
+        long timestamp = System.currentTimeMillis();
+        // 更新体验数据
+        redisService.zAdd(key, timestamp, String.valueOf(timestamp));
+        long windowStartMillis = timestamp - (limitTime * 60 * 60 * 1000);
 
         // 清除时间窗口之前的请求记录(可选,根据需要决定是否在此处清理过期记录)
         redisService.zRemoveRangeByScore(key, 0, windowStartMillis);