chenbiao пре 2 година
родитељ
комит
fb893c0c92

+ 5 - 2
netflix-dao/src/main/java/com/cyksj/model/views/ChatGptUserConversationRecordHistoryView.java

@@ -12,8 +12,8 @@ import java.util.Date;
  */
 @Data
 @SearchBean(tables = "(SELECT MAX(id) id from chatgpt_user_conversation_record as r\n" +
-        "GROUP BY r.conversation_id) t left join chatgpt_user_conversation_record as r on t.id = r.id , chatgpt_user_conversation as c"
-        ,where = "r.conversation_id = c.conversationId"
+        "GROUP BY r.conversation_id) t left join chatgpt_user_conversation_record as r on t.id = r.id , chatgpt_user_conversation as c, chatgpt_sesion s "
+        ,where = "r.conversation_id = c.conversationId AND s.carId = r.car_id "
         ,orderBy = "r.created_time desc"
 )
 public class ChatGptUserConversationRecordHistoryView {
@@ -50,4 +50,7 @@ public class ChatGptUserConversationRecordHistoryView {
     @DbField("r.car_name")
     private String carName;
 
+    @DbField("s.isPlus")
+    private Integer isPLus;
+
 }

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

@@ -823,7 +823,6 @@ public class RedisService {
         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),
 
-        CHATGPT_CAR_SEESSION("sesstion:","chatGpt 车队 session", 48 * 60 * 60L),
         ;
 
         private String name;

+ 5 - 0
netflix-service/src/main/java/com/cyksj/service/chatgpt/ChatGptAccountService.java

@@ -55,4 +55,9 @@ public interface ChatGptAccountService {
     ChatGptUserView getUserInfo(long userId, Long relationId);
 
     void genTitleSync(String messageId, String carid, String userToken);
+
+    /**
+     * oai 触发车队限制
+     */
+    void carLimited(String carId, Integer expTime);
 }

+ 17 - 34
netflix-service/src/main/java/com/cyksj/service/chatgpt/impl/ChatGptAccountServiceImpl.java

@@ -373,7 +373,7 @@ public class ChatGptAccountServiceImpl implements ChatGptAccountService {
     public ConversationLimitResponse conversationLimit(String userToken, String model, String carId, ChatgptUser chatgptUser) {
         ConversationLimitResponse conversationLimitResponse = new ConversationLimitResponse();
         conversationLimitResponse.setLimited(false);
-        if ("gpt-4".equals(model)) {
+        if (!"text-davinci-002-render-sha".equals(model)) {
             //如果不为车队 直接返回
             if (!chatgptUser.getIsCar()) {
                 return conversationLimitResponse;
@@ -500,8 +500,8 @@ public class ChatGptAccountServiceImpl implements ChatGptAccountService {
 
 
     private Long carOaiLimit(String carId) {
-        if (redisService.hasKey("clears_in:" + carId)) {
-            return redisService.getExpire("clears_in:" + carId);
+        if (redisService.hasKey("chatgpt:clears_in:" + carId)) {
+            return redisService.getExpire("chatgpt:clears_in:" + carId);
         }
         return 0L;
     }
@@ -542,19 +542,9 @@ public class ChatGptAccountServiceImpl implements ChatGptAccountService {
         SearchResult<ChatGptUserConversationRecordHistoryView> search = beanSearcher.search(ChatGptUserConversationRecordHistoryView.class, MapUtils.builder().field("userToken", userToken).limit(0, 2).build());
         Set<ChatgptCarInfoView> res = search.getDataList().stream()
                 .map((historyView) -> {
-                    ChatgptCarInfoView chatgptCarInfoView = buildChatgptCarInfoView(historyView.getCarId(), redisService.zScore(RedisService.key.CHATGPT_CAR_SCORES.getName(), historyView.getCarId()));
+                    ChatgptCarInfoView chatgptCarInfoView = buildChatgptCarInfoView(historyView.getCarId(), historyView.getCarName(), historyView.getIsPLus(), redisService.zScore(RedisService.key.CHATGPT_CAR_SCORES.getName(), historyView.getCarId()));
                     chatgptCarInfoView.setIsHistory(true);
                     chatgptCarInfoView.setScore(redisService.zScore(RedisService.key.CHATGPT_CAR_SCORES.getName(), historyView.getCarId()));
-                    String sessionVar = redisService.getStr(RedisService.key.CHATGPT_CAR_SEESSION.getName() + historyView.getCarId());
-                    if (JSONUtil.isJson(sessionVar)) {
-                        JSONObject sessionJson = new JSONObject(sessionVar);
-                        JSONArray models = sessionJson.getJSONArray("models");
-                        if (models.size() > 1) {
-                            chatgptCarInfoView.setType("plus");
-                        } else {
-                            chatgptCarInfoView.setType("3.5");
-                        }
-                    }
 
                     return chatgptCarInfoView;
                 }).collect(Collectors.toCollection(LinkedHashSet::new));
@@ -621,15 +611,10 @@ public class ChatGptAccountServiceImpl implements ChatGptAccountService {
             initFleets(false);
         }
         lowestScorecarIds = redisService.zRangeWithScores(RedisService.key.CHATGPT_CAR_SCORES.getName(), 0L, count - 1);
-
-        return lowestScorecarIds.stream()
-                .map(typedTuple -> {
-                    String value = StringUtil.getString(typedTuple.getValue());
-                    Double score = typedTuple.getScore(); // 获取分数
-                    // 根据需要处理value和score
-                    return buildChatgptCarInfoView(value, score); // 假设这个方法现在也接收分数
-                })
-                .collect(Collectors.toSet());
+        Map<Object, Double> collect = lowestScorecarIds.stream().collect(Collectors.toMap(ZSetOperations.TypedTuple::getValue, ZSetOperations.TypedTuple::getScore));
+        List<ChatgptSession> chatgptSessions = chatgptSessionMapper.selectList(Wrappers.lambdaQuery(ChatgptSession.class).in(ChatgptSession::getCarId, collect.keySet()));
+        return chatgptSessions.stream().map((chatgptSession)-> buildChatgptCarInfoView(chatgptSession.getCarId(),chatgptSession.getCarName(), chatgptSession.getIsPlus(), collect.get(chatgptSession.getCarId())))
+                .sorted(Comparator.comparing(ChatgptCarInfoView::getScore)).collect(Collectors.toCollection(LinkedHashSet::new));
     }
 
     public void initFleets(Boolean flag) {
@@ -652,22 +637,15 @@ public class ChatGptAccountServiceImpl implements ChatGptAccountService {
 
 
     // 构建ChatgptCarInfoView对象
-    private ChatgptCarInfoView buildChatgptCarInfoView(String carId, Double score) {
+    private ChatgptCarInfoView buildChatgptCarInfoView(String carId, String carName, Integer isPlus, Double score) {
         // 在这里根据carId获取相关信息并填充到ChatgptCarInfoView对象中
         ChatgptCarInfoView view = new ChatgptCarInfoView();
         view.setCarId(carId);
         // 假设以下方法从Redis或其他服务获取数据
         view.setScore(score);
         view.setStatus(score >= 50 ? "繁忙" : "空闲"); // 或“繁忙”
-        String sessionVar = redisService.getStr(RedisService.key.CHATGPT_CAR_SEESSION.getName() + carId);
-        if (JSONUtil.isJson(sessionVar)) {
-            JSONArray models = new JSONObject(sessionVar).getJSONArray("models");
-            if (models != null && models.size() > 1) {
-                view.setType("plus");
-            } else {
-                view.setType("3.5");
-            }
-        }
+
+        view.setType(isPlus == 1 ? "plus" : "3.5");
         view.setGptLimit(40); // 假设值
         int use = getCarConversationCount(carId, 3L).intValue();
         view.setUse(Math.min(use, 40));
@@ -676,7 +654,7 @@ public class ChatGptAccountServiceImpl implements ChatGptAccountService {
             view.setStatus("停运");
             view.setExpTime(new DateTime(System.currentTimeMillis() + aLong * 1000));
         }
-        // 设置其他必要的属性...
+        view.setCarName(carName);
         return view;
     }
 
@@ -713,6 +691,11 @@ public class ChatGptAccountServiceImpl implements ChatGptAccountService {
         }
     }
 
+    @Override
+    public void carLimited(String carId, Integer expTime) {
+        redisService.set("chatgpt:clears_in:" + carId, expTime);
+    }
+
     @PostConstruct
     public void init() {
         //如果存在未存在redis中的车辆则同步进 chatgpt:car:scores

+ 12 - 0
netflix-web/src/main/java/com/cyksj/web/controller/mirror/MirrorController.java

@@ -1,6 +1,7 @@
 package com.cyksj.web.controller.mirror;
 
 import cn.hutool.json.JSONObject;
+import com.baomidou.mybatisplus.extension.api.R;
 import com.cyksj.common.exception.BusinessRuntimeException;
 import com.cyksj.common.task.GlobalThreadPoolTaskExecutor;
 import com.cyksj.common.util.IoKit;
@@ -211,4 +212,15 @@ public class MirrorController {
         return GatewayResponse.SUCCESS.newBuilder().toResult(search);
     }
 
+    /**
+     * oai触发车队限制
+     */
+    @GetMapping("/gpt/car/limited")
+    public Result<String> carLimited(String carId, Integer expTime) {
+        TASK_EXECUTOR.execute(()->{
+            chatGptAccountService.carLimited(carId, expTime);
+        });
+        return GatewayResponse.SUCCESS.newBuilder().toResult();
+    }
+
 }