Browse Source

调整对话历史接口

tsurumure 6 tháng trước cách đây
mục cha
commit
b445a6c933

+ 2 - 1
src/main/java/com/backendsys/modules/ai/chat/controller/ChatHistoryController.java

@@ -24,7 +24,8 @@ public class ChatHistoryController {
     @PreAuthorize("@sr.hasPermission('31')")
     @GetMapping("/api/ai/chat/getChatHistoryList")
     public Result getChatHistoryList() {
-        return Result.success().put("data", chatHistoryService.selectChatHistoryList());
+        ChatHistory history = new ChatHistory();
+        return Result.success().put("data", chatHistoryService.selectChatHistoryList(history));
     }
 
     @Operation(summary = "删除我的对话历史及内容 (软删除)")

+ 6 - 0
src/main/java/com/backendsys/modules/ai/chat/dao/ChatHistoryDao.java

@@ -4,6 +4,12 @@ import com.backendsys.modules.ai.chat.entity.ChatHistory;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import org.apache.ibatis.annotations.Mapper;
 
+import java.util.List;
+import java.util.Map;
+
 @Mapper
 public interface ChatHistoryDao extends BaseMapper<ChatHistory> {
+
+    List<ChatHistory> selectChatHistoryList(ChatHistory chatHistory);
+
 }

+ 5 - 0
src/main/java/com/backendsys/modules/ai/chat/entity/ChatHistory.java

@@ -2,6 +2,7 @@ package com.backendsys.modules.ai.chat.entity;
 
 import com.backendsys.entity.validator.RangeStringArray;
 import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableName;
 import jakarta.validation.constraints.NotEmpty;
@@ -24,6 +25,10 @@ public class ChatHistory {
     private Long user_id;
     private String robot_code;
     private String last_prompt;
+
+    @TableField(exist = false)
+    private Integer chat_count;
+
     private Integer del_flag;
     private String create_time;
     private String update_time;

+ 1 - 1
src/main/java/com/backendsys/modules/ai/chat/service/ChatHistoryService.java

@@ -9,7 +9,7 @@ import java.util.Map;
 public interface ChatHistoryService {
 
     // 获取我的对话历史列表
-    PageEntity selectChatHistoryList();
+    PageEntity selectChatHistoryList(ChatHistory history);
 
     // 删除我的对话历史 (软删除)
     Map<String, Object> deleteChatHistory(ChatHistory chatHistory);

+ 2 - 5
src/main/java/com/backendsys/modules/ai/chat/service/impl/ChatHistoryServiceImpl.java

@@ -30,12 +30,9 @@ public class ChatHistoryServiceImpl implements ChatHistoryService {
      * 获取我的对话历史列表
      */
     @Override
-    public PageEntity selectChatHistoryList() {
+    public PageEntity selectChatHistoryList(ChatHistory history) {
         PageUtils.startPage();  // 分页
-        LambdaQueryWrapper<ChatHistory> wrapper = new LambdaQueryWrapper<>();
-        wrapper.eq(ChatHistory::getDel_flag, -1);
-        wrapper.orderByAsc(ChatHistory::getCreate_time);
-        List<ChatHistory> list = chatHistoryDao.selectList(wrapper);
+        List<ChatHistory> list = chatHistoryDao.selectChatHistoryList(history);
         return new PageInfoResult(list).toEntity();
     }
 

+ 48 - 0
src/main/resources/mapper/ai/chat/ChatHistoryDao.xml

@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
+<mapper namespace="com.backendsys.modules.ai.chat.dao.ChatHistoryDao">
+
+    <sql id="includeChatHistory">
+        ch.id,
+        ch.history_code,
+        ch.user_id,
+        COALESCE(ch.robot_code, '') robot_code,
+        ch.last_prompt,
+        ch.del_flag,
+        ch.create_time,
+        ch.update_time
+    </sql>
+<!--   java.util.LinkedHashMap -->
+    <resultMap id="resultMapChatHistory" type="com.backendsys.modules.ai.chat.entity.ChatHistory">
+        <id property="id" column="id" jdbcType="BIGINT" />
+        <result property="history_code" column="history_code" />
+        <result property="user_id" column="user_id" javaType="java.lang.Long" />
+        <result property="robot_code" column="robot_code" />
+        <result property="last_prompt" column="last_prompt" />
+        <result property="chat_count" column="chat_count" javaType="java.lang.Integer" />
+        <result property="del_flag" column="del_flag" javaType="java.lang.Integer" />
+        <result property="create_time" column="create_time" />
+        <result property="update_time" column="update_time" />
+    </resultMap>
+
+    <!-- 查 列表 -->
+    <select id="selectChatHistoryList" resultMap="resultMapChatHistory">
+        SELECT <include refid="includeChatHistory" />, COUNT(c.id) chat_count
+        FROM ai_chat_history ch
+        LEFT JOIN ai_chat c ON c.history_code = ch.history_code
+        <where>
+            <if test="user_id != null and user_id != ''">
+                AND ch.user_id = #{user_id}
+            </if>
+            <if test="history_code != null and history_code != ''">
+                AND ch.history_code = #{history_code}
+            </if>
+            <if test="del_flag != null and del_flag != ''">
+                AND ch.del_flag = #{del_flag}
+            </if>
+        </where>
+        GROUP BY ch.id, ch.history_code
+        ORDER BY ch.update_time DESC
+    </select>
+
+</mapper>