From 943b447d99868a7d164b9dec705f251a4326b2a5 Mon Sep 17 00:00:00 2001 From: cb <275647614@qq.com> Date: Fri, 25 Jul 2025 16:13:55 +0800 Subject: [PATCH] no message --- .../system/ChatHistoryAppController.java | 181 ++++++++++++++++++ .../ruoyi/framework/config/ShiroConfig.java | 3 + .../com/ruoyi/system/domain/ChatHistory.java | 122 ++++++++++++ .../system/mapper/ChatHistoryMapper.java | 101 ++++++++++ .../system/service/IChatHistoryService.java | 101 ++++++++++ .../service/impl/ChatHistoryServiceImpl.java | 153 +++++++++++++++ .../mapper/system/ChatHistoryMapper.xml | 113 +++++++++++ 7 files changed, 774 insertions(+) create mode 100644 ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/ChatHistoryAppController.java create mode 100644 ruoyi-system/src/main/java/com/ruoyi/system/domain/ChatHistory.java create mode 100644 ruoyi-system/src/main/java/com/ruoyi/system/mapper/ChatHistoryMapper.java create mode 100644 ruoyi-system/src/main/java/com/ruoyi/system/service/IChatHistoryService.java create mode 100644 ruoyi-system/src/main/java/com/ruoyi/system/service/impl/ChatHistoryServiceImpl.java create mode 100644 ruoyi-system/src/main/resources/mapper/system/ChatHistoryMapper.xml diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/ChatHistoryAppController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/ChatHistoryAppController.java new file mode 100644 index 00000000..7d75afa4 --- /dev/null +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/ChatHistoryAppController.java @@ -0,0 +1,181 @@ +package com.ruoyi.web.controller.system; + +import java.util.Date; +import java.util.List; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import com.ruoyi.common.core.controller.BaseController; +import com.ruoyi.common.core.domain.AjaxResult; +import com.ruoyi.system.domain.ChatHistory; +import com.ruoyi.system.service.IChatHistoryService; +import org.springframework.web.bind.annotation.RequestParam; + +/** + * 聊天记录App接口Controller + * + * @author ruoyi + * @date 2024-01-01 + */ +@RestController +@RequestMapping("/system/chatHistory/app") +public class ChatHistoryAppController extends BaseController +{ + @Autowired + private IChatHistoryService chatHistoryService; + + /** + * 根据会话ID查询聊天记录 + */ + @GetMapping("/session/{sessionId}") + public AjaxResult getChatHistoryBySession(@PathVariable("sessionId") String sessionId) + { + try { + List list = chatHistoryService.selectChatHistoryBySessionId(sessionId); + return AjaxResult.success(list); + } catch (Exception e) { + logger.error("查询聊天记录失败", e); + return AjaxResult.error("查询失败:" + e.getMessage()); + } + } + + /** + * 根据用户ID查询聊天记录 + */ + @GetMapping("/user/{userId}") + public AjaxResult getChatHistoryByUser(@PathVariable("userId") String userId) + { + try { + List list = chatHistoryService.selectChatHistoryByUserId(userId); + return AjaxResult.success(list); + } catch (Exception e) { + logger.error("查询聊天记录失败", e); + return AjaxResult.error("查询失败:" + e.getMessage()); + } + } + + /** + * 新增聊天记录 + */ + @PostMapping + public AjaxResult add(@RequestBody ChatHistory chatHistory) + { + try { + int result = chatHistoryService.insertChatHistory(chatHistory); + chatHistory.setCreateTime(new Date()); + return toAjax(result); + } catch (Exception e) { + logger.error("新增聊天记录失败", e); + return AjaxResult.error("新增失败:" + e.getMessage()); + } + } + + /** + * 批量新增聊天记录 + */ + @PostMapping("/batch") + public AjaxResult batchAdd(@RequestBody List chatHistoryList) + { + try { + int result = chatHistoryService.batchInsertChatHistory(chatHistoryList); + return toAjax(result); + } catch (Exception e) { + logger.error("批量新增聊天记录失败", e); + return AjaxResult.error("批量新增失败:" + e.getMessage()); + } + } + + /** + * 修改聊天记录 + */ + @PutMapping + public AjaxResult edit(@RequestBody ChatHistory chatHistory) + { + try { + int result = chatHistoryService.updateChatHistory(chatHistory); + return toAjax(result); + } catch (Exception e) { + logger.error("修改聊天记录失败", e); + return AjaxResult.error("修改失败:" + e.getMessage()); + } + } + + /** + * 删除指定会话的聊天记录 + */ + @DeleteMapping("/session/{sessionId}") + public AjaxResult removeBySession(@PathVariable("sessionId") String sessionId) + { + try { + int result = chatHistoryService.deleteChatHistoryBySessionId(sessionId); + return toAjax(result); + } catch (Exception e) { + logger.error("删除聊天记录失败", e); + return AjaxResult.error("删除失败:" + e.getMessage()); + } + } + + /** + * 删除指定用户的聊天记录 + */ + @DeleteMapping("/user/{userId}") + public AjaxResult removeByUser(@PathVariable("userId") String userId) + { + try { + int result = chatHistoryService.deleteChatHistoryByUserId(userId); + return toAjax(result); + } catch (Exception e) { + logger.error("删除聊天记录失败", e); + return AjaxResult.error("删除失败:" + e.getMessage()); + } + } + + /** + * 保存用户聊天记录(包含用户消息和客服回复) + */ + @PostMapping("/saveChat") + public AjaxResult saveChat(@RequestBody List chatHistoryList) + { + try { + if (chatHistoryList == null || chatHistoryList.isEmpty()) { + return AjaxResult.error("聊天记录不能为空"); + } + + int result = chatHistoryService.batchInsertChatHistory(chatHistoryList); + return toAjax(result); + } catch (Exception e) { + logger.error("保存聊天记录失败", e); + return AjaxResult.error("保存失败:" + e.getMessage()); + } + } + + /** + * 获取用户最新的聊天记录 + */ + @GetMapping("/latest/{userId}") + public AjaxResult getLatestChatHistory(@PathVariable("userId") String userId, + @RequestParam(value = "limit", defaultValue = "20") Integer limit) + { + try { + ChatHistory query = new ChatHistory(); + query.setUserId(userId); + List list = chatHistoryService.selectChatHistoryList(query); + + // 限制返回数量 + if (limit != null && limit > 0 && list.size() > limit) { + list = list.subList(list.size() - limit, list.size()); + } + + return AjaxResult.success(list); + } catch (Exception e) { + logger.error("查询最新聊天记录失败", e); + return AjaxResult.error("查询失败:" + e.getMessage()); + } + } +} \ No newline at end of file diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/ShiroConfig.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/ShiroConfig.java index 1c435c2b..9043ca07 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/ShiroConfig.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/ShiroConfig.java @@ -306,6 +306,9 @@ public class ShiroConfig filterChainDefinitionMap.put("/system/material/app/**", "anon"); // 客服回复接口 filterChainDefinitionMap.put("/system/customerServiceReply/app/**", "anon"); + + filterChainDefinitionMap.put("/system/chatHistory/app/**", "anon"); + // 退出 logout地址,shiro去清除session filterChainDefinitionMap.put("/logout", "logout"); // 不需要拦截的访问 diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/domain/ChatHistory.java b/ruoyi-system/src/main/java/com/ruoyi/system/domain/ChatHistory.java new file mode 100644 index 00000000..235ac0bc --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/system/domain/ChatHistory.java @@ -0,0 +1,122 @@ +package com.ruoyi.system.domain; + +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; +import com.ruoyi.common.annotation.Excel; +import com.ruoyi.common.core.domain.BaseEntity; + +/** + * 聊天记录对象 chat_history + * + * @author ruoyi + * @date 2024-01-01 + */ +public class ChatHistory extends BaseEntity +{ + private static final long serialVersionUID = 1L; + + /** 主键ID */ + private Long id; + + /** 用户ID */ + @Excel(name = "用户ID") + private String userId; + + /** 会话ID */ + @Excel(name = "会话ID") + private String sessionId; + + /** 消息类型 */ + @Excel(name = "消息类型", readConverterExp = "user=用户消息,service=客服消息") + private String messageType; + + /** 消息内容 */ + @Excel(name = "消息内容") + private String content; + + /** 是否包含链接 */ + @Excel(name = "是否包含链接", readConverterExp = "0=否,1=是") + private Integer isLink; + + /** 是否为按钮消息 */ + @Excel(name = "是否为按钮消息", readConverterExp = "0=否,1=是") + private Integer isButton; + + public void setId(Long id) + { + this.id = id; + } + + public Long getId() + { + return id; + } + public void setUserId(String userId) + { + this.userId = userId; + } + + public String getUserId() + { + return userId; + } + public void setSessionId(String sessionId) + { + this.sessionId = sessionId; + } + + public String getSessionId() + { + return sessionId; + } + public void setMessageType(String messageType) + { + this.messageType = messageType; + } + + public String getMessageType() + { + return messageType; + } + public void setContent(String content) + { + this.content = content; + } + + public String getContent() + { + return content; + } + public void setIsLink(Integer isLink) + { + this.isLink = isLink; + } + + public Integer getIsLink() + { + return isLink; + } + public void setIsButton(Integer isButton) + { + this.isButton = isButton; + } + + public Integer getIsButton() + { + return isButton; + } + + @Override + public String toString() { + return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE) + .append("id", getId()) + .append("userId", getUserId()) + .append("sessionId", getSessionId()) + .append("messageType", getMessageType()) + .append("content", getContent()) + .append("isLink", getIsLink()) + .append("isButton", getIsButton()) + .append("createTime", getCreateTime()) + .toString(); + } +} \ No newline at end of file diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/mapper/ChatHistoryMapper.java b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/ChatHistoryMapper.java new file mode 100644 index 00000000..e097438b --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/system/mapper/ChatHistoryMapper.java @@ -0,0 +1,101 @@ +package com.ruoyi.system.mapper; + +import java.util.List; +import com.ruoyi.system.domain.ChatHistory; + +/** + * 聊天记录Mapper接口 + * + * @author ruoyi + * @date 2024-01-01 + */ +public interface ChatHistoryMapper +{ + /** + * 查询聊天记录 + * + * @param id 聊天记录主键 + * @return 聊天记录 + */ + public ChatHistory selectChatHistoryById(Long id); + + /** + * 查询聊天记录列表 + * + * @param chatHistory 聊天记录 + * @return 聊天记录集合 + */ + public List selectChatHistoryList(ChatHistory chatHistory); + + /** + * 根据会话ID查询聊天记录 + * + * @param sessionId 会话ID + * @return 聊天记录集合 + */ + public List selectChatHistoryBySessionId(String sessionId); + + /** + * 根据用户ID查询聊天记录 + * + * @param userId 用户ID + * @return 聊天记录集合 + */ + public List selectChatHistoryByUserId(String userId); + + /** + * 新增聊天记录 + * + * @param chatHistory 聊天记录 + * @return 结果 + */ + public int insertChatHistory(ChatHistory chatHistory); + + /** + * 批量新增聊天记录 + * + * @param chatHistoryList 聊天记录列表 + * @return 结果 + */ + public int batchInsertChatHistory(List chatHistoryList); + + /** + * 修改聊天记录 + * + * @param chatHistory 聊天记录 + * @return 结果 + */ + public int updateChatHistory(ChatHistory chatHistory); + + /** + * 删除聊天记录 + * + * @param id 聊天记录主键 + * @return 结果 + */ + public int deleteChatHistoryById(Long id); + + /** + * 批量删除聊天记录 + * + * @param ids 需要删除的数据主键集合 + * @return 结果 + */ + public int deleteChatHistoryByIds(Long[] ids); + + /** + * 删除指定会话的聊天记录 + * + * @param sessionId 会话ID + * @return 结果 + */ + public int deleteChatHistoryBySessionId(String sessionId); + + /** + * 删除指定用户的聊天记录 + * + * @param userId 用户ID + * @return 结果 + */ + public int deleteChatHistoryByUserId(String userId); +} \ No newline at end of file diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/IChatHistoryService.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/IChatHistoryService.java new file mode 100644 index 00000000..4704b721 --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/IChatHistoryService.java @@ -0,0 +1,101 @@ +package com.ruoyi.system.service; + +import java.util.List; +import com.ruoyi.system.domain.ChatHistory; + +/** + * 聊天记录Service接口 + * + * @author ruoyi + * @date 2024-01-01 + */ +public interface IChatHistoryService +{ + /** + * 查询聊天记录 + * + * @param id 聊天记录主键 + * @return 聊天记录 + */ + public ChatHistory selectChatHistoryById(Long id); + + /** + * 查询聊天记录列表 + * + * @param chatHistory 聊天记录 + * @return 聊天记录集合 + */ + public List selectChatHistoryList(ChatHistory chatHistory); + + /** + * 根据会话ID查询聊天记录 + * + * @param sessionId 会话ID + * @return 聊天记录集合 + */ + public List selectChatHistoryBySessionId(String sessionId); + + /** + * 根据用户ID查询聊天记录 + * + * @param userId 用户ID + * @return 聊天记录集合 + */ + public List selectChatHistoryByUserId(String userId); + + /** + * 新增聊天记录 + * + * @param chatHistory 聊天记录 + * @return 结果 + */ + public int insertChatHistory(ChatHistory chatHistory); + + /** + * 批量新增聊天记录 + * + * @param chatHistoryList 聊天记录列表 + * @return 结果 + */ + public int batchInsertChatHistory(List chatHistoryList); + + /** + * 修改聊天记录 + * + * @param chatHistory 聊天记录 + * @return 结果 + */ + public int updateChatHistory(ChatHistory chatHistory); + + /** + * 批量删除聊天记录 + * + * @param ids 需要删除的聊天记录主键集合 + * @return 结果 + */ + public int deleteChatHistoryByIds(Long[] ids); + + /** + * 删除聊天记录信息 + * + * @param id 聊天记录主键 + * @return 结果 + */ + public int deleteChatHistoryById(Long id); + + /** + * 删除指定会话的聊天记录 + * + * @param sessionId 会话ID + * @return 结果 + */ + public int deleteChatHistoryBySessionId(String sessionId); + + /** + * 删除指定用户的聊天记录 + * + * @param userId 用户ID + * @return 结果 + */ + public int deleteChatHistoryByUserId(String userId); +} \ No newline at end of file diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/ChatHistoryServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/ChatHistoryServiceImpl.java new file mode 100644 index 00000000..be6b8254 --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/ChatHistoryServiceImpl.java @@ -0,0 +1,153 @@ +package com.ruoyi.system.service.impl; + +import java.util.List; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import com.ruoyi.system.mapper.ChatHistoryMapper; +import com.ruoyi.system.domain.ChatHistory; +import com.ruoyi.system.service.IChatHistoryService; + +/** + * 聊天记录Service业务层处理 + * + * @author ruoyi + * @date 2024-01-01 + */ +@Service +public class ChatHistoryServiceImpl implements IChatHistoryService +{ + @Autowired + private ChatHistoryMapper chatHistoryMapper; + + /** + * 查询聊天记录 + * + * @param id 聊天记录主键 + * @return 聊天记录 + */ + @Override + public ChatHistory selectChatHistoryById(Long id) + { + return chatHistoryMapper.selectChatHistoryById(id); + } + + /** + * 查询聊天记录列表 + * + * @param chatHistory 聊天记录 + * @return 聊天记录 + */ + @Override + public List selectChatHistoryList(ChatHistory chatHistory) + { + return chatHistoryMapper.selectChatHistoryList(chatHistory); + } + + /** + * 根据会话ID查询聊天记录 + * + * @param sessionId 会话ID + * @return 聊天记录集合 + */ + @Override + public List selectChatHistoryBySessionId(String sessionId) + { + return chatHistoryMapper.selectChatHistoryBySessionId(sessionId); + } + + /** + * 根据用户ID查询聊天记录 + * + * @param userId 用户ID + * @return 聊天记录集合 + */ + @Override + public List selectChatHistoryByUserId(String userId) + { + return chatHistoryMapper.selectChatHistoryByUserId(userId); + } + + /** + * 新增聊天记录 + * + * @param chatHistory 聊天记录 + * @return 结果 + */ + @Override + public int insertChatHistory(ChatHistory chatHistory) + { + return chatHistoryMapper.insertChatHistory(chatHistory); + } + + /** + * 批量新增聊天记录 + * + * @param chatHistoryList 聊天记录列表 + * @return 结果 + */ + @Override + public int batchInsertChatHistory(List chatHistoryList) + { + return chatHistoryMapper.batchInsertChatHistory(chatHistoryList); + } + + /** + * 修改聊天记录 + * + * @param chatHistory 聊天记录 + * @return 结果 + */ + @Override + public int updateChatHistory(ChatHistory chatHistory) + { + return chatHistoryMapper.updateChatHistory(chatHistory); + } + + /** + * 批量删除聊天记录 + * + * @param ids 需要删除的聊天记录主键 + * @return 结果 + */ + @Override + public int deleteChatHistoryByIds(Long[] ids) + { + return chatHistoryMapper.deleteChatHistoryByIds(ids); + } + + /** + * 删除聊天记录信息 + * + * @param id 聊天记录主键 + * @return 结果 + */ + @Override + public int deleteChatHistoryById(Long id) + { + return chatHistoryMapper.deleteChatHistoryById(id); + } + + /** + * 删除指定会话的聊天记录 + * + * @param sessionId 会话ID + * @return 结果 + */ + @Override + public int deleteChatHistoryBySessionId(String sessionId) + { + return chatHistoryMapper.deleteChatHistoryBySessionId(sessionId); + } + + /** + * 删除指定用户的聊天记录 + * + * @param userId 用户ID + * @return 结果 + */ + @Override + public int deleteChatHistoryByUserId(String userId) + { + return chatHistoryMapper.deleteChatHistoryByUserId(userId); + } +} \ No newline at end of file diff --git a/ruoyi-system/src/main/resources/mapper/system/ChatHistoryMapper.xml b/ruoyi-system/src/main/resources/mapper/system/ChatHistoryMapper.xml new file mode 100644 index 00000000..a09cf4f9 --- /dev/null +++ b/ruoyi-system/src/main/resources/mapper/system/ChatHistoryMapper.xml @@ -0,0 +1,113 @@ + + + + + + + + + + + + + + + + + select id, user_id, session_id, message_type, content, is_link, is_button, create_time from chat_history + + + + + + + + + + + + insert into chat_history + + user_id, + session_id, + message_type, + content, + is_link, + is_button, + create_time, + + + #{userId}, + #{sessionId}, + #{messageType}, + #{content}, + #{isLink}, + #{isButton}, + #{createTime}, + + + + + insert into chat_history (user_id, session_id, message_type, content, is_link, is_button, create_time) values + + (#{item.userId}, #{item.sessionId}, #{item.messageType}, #{item.content}, #{item.isLink}, #{item.isButton}, #{item.createTime}) + + + + + update chat_history + + user_id = #{userId}, + session_id = #{sessionId}, + message_type = #{messageType}, + content = #{content}, + is_link = #{isLink}, + is_button = #{isButton}, + create_time = #{createTime}, + + where id = #{id} + + + + delete from chat_history where id = #{id} + + + + delete from chat_history where id in + + #{id} + + + + + delete from chat_history where session_id = #{sessionId} + + + + delete from chat_history where user_id = #{userId} + + \ No newline at end of file