tsurumure 6 месяцев назад
Родитель
Сommit
54ccb0f6a5

+ 1 - 0
db/ai_chat.sql

@@ -19,6 +19,7 @@ CREATE TABLE `ai_chat` (
 #     `user_nickname` VARCHAR(255) COMMENT '用户名',
 #     `user_avatar` VARCHAR(255) COMMENT '用户头像',
     `robot_code` VARCHAR(255) COMMENT '机器人CODE',
+    `del_flag` TINYINT(1) DEFAULT '-1' COMMENT '删除标志 (-1未删除, 1删除)',
     `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
     `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
     INDEX `idx_history_code` (`history_code`),

+ 1 - 0
db/ai_chat_history.sql

@@ -12,6 +12,7 @@ CREATE TABLE `ai_chat_history` (
     `user_id` BIGINT NOT NULL COMMENT '用户ID',
     `robot_code` VARCHAR(255) COMMENT '机器人CODE',
     `last_prompt` VARCHAR(255) NOT NULL COMMENT '最后一次的对话内容',
+    `del_flag` TINYINT(1) DEFAULT '-1' COMMENT '删除标志 (-1未删除, 1删除)',
     `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
     `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
     UNIQUE KEY `uk_history_code` (`history_code`),

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

@@ -26,7 +26,7 @@ public class ChatController {
         return Result.success().put("data", chatService.sendChat(chat));
     }
 
-    @Operation(summary = "获取我的对话列表")
+    @Operation(summary = "获取我的对话")
     @PreAuthorize("@sr.hasPermission('31')")
     @GetMapping("/api/ai/chat/getChatList")
     public Result getChatList(@Validated(Chat.Detail.class) Chat chat) {

+ 29 - 0
src/main/java/com/backendsys/modules/ai/chat/controller/ChatHistoryController.java

@@ -1,9 +1,38 @@
 package com.backendsys.modules.ai.chat.controller;
 
+import com.backendsys.modules.ai.chat.entity.Chat;
+import com.backendsys.modules.ai.chat.entity.ChatHistory;
+import com.backendsys.modules.ai.chat.service.ChatHistoryService;
+import com.backendsys.modules.common.utils.Result;
+import io.swagger.v3.oas.annotations.Operation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.DeleteMapping;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RestController;
 
 @Validated
 @RestController
 public class ChatHistoryController {
+
+    @Autowired
+    private ChatHistoryService chatHistoryService;
+
+    @Operation(summary = "获取我的对话历史列表")
+    @PreAuthorize("@sr.hasPermission('31')")
+    @GetMapping("/api/ai/chat/getChatHistoryList")
+    public Result getChatHistoryList() {
+        return Result.success().put("data", chatHistoryService.selectChatHistoryList());
+    }
+
+    @Operation(summary = "删除我的对话历史")
+    @PreAuthorize("@sr.hasPermission('31')")
+    @DeleteMapping("/api/ai/chat/deleteChatHistory")
+    public Result deleteChatHistory(@Validated(ChatHistory.Delete.class) @RequestBody ChatHistory chatHistory) {
+        return Result.success().put("data", chatHistoryService.deleteChatHistory(chatHistory));
+    }
+
+
 }

+ 1 - 0
src/main/java/com/backendsys/modules/ai/chat/entity/Chat.java

@@ -40,6 +40,7 @@ public class Chat {
     private String content_type;    // 对话内容类型 (THINK: 思考, REPLY: 回复)
     private Long duration;
 
+    private Integer del_flag;
     private String create_time;
     private String update_time;
 

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

@@ -18,10 +18,13 @@ public class ChatHistory {
 
     @TableId(type = IdType.AUTO)
     private Long id;
+
+    @NotEmpty(message = "history_code 不能为空", groups = { Delete.class })
     private String history_code;
     private Long user_id;
     private String robot_code;
     private String last_prompt;
+    private Integer del_flag;
     private String create_time;
     private String update_time;
 

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

@@ -1,6 +1,7 @@
 package com.backendsys.modules.ai.chat.service;
 
 import com.backendsys.modules.ai.chat.entity.Chat;
+import com.backendsys.modules.ai.chat.entity.ChatHistory;
 import com.backendsys.utils.response.PageEntity;
 
 import java.util.Map;
@@ -8,6 +9,9 @@ import java.util.Map;
 public interface ChatHistoryService {
 
     // 获取我的对话历史列表
-    PageEntity selectChatHistoryist(String history_code);
+    PageEntity selectChatHistoryList();
+
+    // 删除我的对话历史
+    Map<String, Object> deleteChatHistory(ChatHistory chatHistory);
 
 }

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

@@ -18,6 +18,7 @@ import com.backendsys.utils.v2.PageUtils;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 import java.util.List;
 import java.util.Map;
@@ -27,9 +28,60 @@ import java.util.concurrent.CompletableFuture;
 @Service
 public class ChatHistoryServiceImpl implements ChatHistoryService {
 
+    @Autowired
+    private ChatDao chatDao;
 
+    @Autowired
+    private ChatHistoryDao chatHistoryDao;
+
+    /**
+     * 获取我的对话历史列表
+     */
     @Override
-    public PageEntity selectChatHistoryist(String history_code) {
-        return null;
+    public PageEntity selectChatHistoryList() {
+        PageUtils.startPage();  // 分页
+        LambdaQueryWrapper<ChatHistory> wrapper = new LambdaQueryWrapper<>();
+        wrapper.eq(ChatHistory::getDel_flag, -1);
+        wrapper.orderByAsc(ChatHistory::getCreate_time);
+        List<ChatHistory> list = chatHistoryDao.selectList(wrapper);
+        return new PageInfoResult(list).toEntity();
+    }
+
+    /**
+     * 删除我的对话历史
+     */
+    @Override
+    @Transactional
+    public Map<String, Object> deleteChatHistory(ChatHistory chatHistory) {
+
+        String history_code = chatHistory.getHistory_code();
+
+        ChatHistory chatHistoryDetail = chatHistoryDao.selectOne(new LambdaQueryWrapper<ChatHistory>()
+                .eq(ChatHistory::getDel_flag, -1)
+                .eq(ChatHistory::getHistory_code, history_code));
+        if (chatHistoryDetail == null) throw new CustException("history_code 不存在");
+        chatHistoryDetail.setDel_flag(1);
+        // [软删除] 删除对话记录
+        chatHistoryDao.updateById(chatHistoryDetail);
+
+        List<Chat> chatList = chatDao.selectList(new LambdaQueryWrapper<Chat>()
+                .eq(Chat::getDel_flag, -1)
+                .eq(Chat::getHistory_code, history_code));
+        if (chatList != null && !chatList.isEmpty()) {
+            chatList.stream().forEach(chat -> {
+                // [软删除] 删除对话
+                chat.setDel_flag(1);
+                System.out.println(chat);
+                chatDao.updateById(chat);
+            });
+        }
+
+//        // 删除对话记录
+//        chatHistoryDao.delete(new LambdaQueryWrapper<ChatHistory>().eq(ChatHistory::getHistory_code, history_code));
+
+//        // 删除对话
+//        chatDao.delete(new LambdaQueryWrapper<Chat>().eq(Chat::getHistory_code, history_code));
+
+        return Map.of("history_code", chatHistory.getHistory_code());
     }
 }

+ 6 - 1
src/main/java/com/backendsys/modules/ai/chat/service/impl/ChatServiceImpl.java

@@ -74,6 +74,7 @@ public class ChatServiceImpl implements ChatService {
         } else {
             // 查询历史对话是否存在
             LambdaQueryWrapper<ChatHistory> wrapper = new LambdaQueryWrapper<>();
+            wrapper.eq(ChatHistory::getDel_flag, -1);
             wrapper.eq(ChatHistory::getHistory_code, history_code);
             ChatHistory historyDetail = chatHistoryDao.selectOne(wrapper);
             if (historyDetail == null) throw new CustException("history_code 不存在,请重新创建");
@@ -97,6 +98,7 @@ public class ChatServiceImpl implements ChatService {
 
                     // [DB] 查询对话历史记录 (按时间升序排序,最新的在最后面) (用于对话上下文)
                     LambdaQueryWrapper<Chat> wrapper = new LambdaQueryWrapper<>();
+                    wrapper.eq(Chat::getDel_flag, -1);
                     wrapper.eq(Chat::getHistory_code, finalHistory_code);
                     wrapper.orderByDesc(Chat::getCreate_time);
                     List<Chat> chatList = chatDao.selectList(wrapper);
@@ -156,12 +158,15 @@ public class ChatServiceImpl implements ChatService {
     @Override
     public PageEntity selectChatList(String history_code) {
 
-        ChatHistory chatHistory = chatHistoryDao.selectOne(new LambdaQueryWrapper<ChatHistory>().eq(ChatHistory::getHistory_code, history_code));
+        ChatHistory chatHistory = chatHistoryDao.selectOne(new LambdaQueryWrapper<ChatHistory>()
+                .eq(ChatHistory::getDel_flag, -1)
+                .eq(ChatHistory::getHistory_code, history_code));
         if (chatHistory == null) throw new CustException("history_code 不存在");
 
         PageUtils.startPage();  // 分页
         LambdaQueryWrapper<Chat> wrapper = new LambdaQueryWrapper<>();
         wrapper.eq(Chat::getHistory_code, history_code);
+        wrapper.eq(Chat::getDel_flag, -1);
         wrapper.orderByAsc(Chat::getCreate_time);
         List<Chat> list = chatDao.selectList(wrapper);