no message

This commit is contained in:
cb 2025-07-25 16:13:55 +08:00
parent a25e34794f
commit 943b447d99
7 changed files with 774 additions and 0 deletions

View File

@ -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<ChatHistory> 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<ChatHistory> 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<ChatHistory> 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<ChatHistory> 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<ChatHistory> 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());
}
}
}

View File

@ -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");
// 不需要拦截的访问

View File

@ -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();
}
}

View File

@ -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<ChatHistory> selectChatHistoryList(ChatHistory chatHistory);
/**
* 根据会话ID查询聊天记录
*
* @param sessionId 会话ID
* @return 聊天记录集合
*/
public List<ChatHistory> selectChatHistoryBySessionId(String sessionId);
/**
* 根据用户ID查询聊天记录
*
* @param userId 用户ID
* @return 聊天记录集合
*/
public List<ChatHistory> selectChatHistoryByUserId(String userId);
/**
* 新增聊天记录
*
* @param chatHistory 聊天记录
* @return 结果
*/
public int insertChatHistory(ChatHistory chatHistory);
/**
* 批量新增聊天记录
*
* @param chatHistoryList 聊天记录列表
* @return 结果
*/
public int batchInsertChatHistory(List<ChatHistory> 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);
}

View File

@ -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<ChatHistory> selectChatHistoryList(ChatHistory chatHistory);
/**
* 根据会话ID查询聊天记录
*
* @param sessionId 会话ID
* @return 聊天记录集合
*/
public List<ChatHistory> selectChatHistoryBySessionId(String sessionId);
/**
* 根据用户ID查询聊天记录
*
* @param userId 用户ID
* @return 聊天记录集合
*/
public List<ChatHistory> selectChatHistoryByUserId(String userId);
/**
* 新增聊天记录
*
* @param chatHistory 聊天记录
* @return 结果
*/
public int insertChatHistory(ChatHistory chatHistory);
/**
* 批量新增聊天记录
*
* @param chatHistoryList 聊天记录列表
* @return 结果
*/
public int batchInsertChatHistory(List<ChatHistory> 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);
}

View File

@ -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<ChatHistory> selectChatHistoryList(ChatHistory chatHistory)
{
return chatHistoryMapper.selectChatHistoryList(chatHistory);
}
/**
* 根据会话ID查询聊天记录
*
* @param sessionId 会话ID
* @return 聊天记录集合
*/
@Override
public List<ChatHistory> selectChatHistoryBySessionId(String sessionId)
{
return chatHistoryMapper.selectChatHistoryBySessionId(sessionId);
}
/**
* 根据用户ID查询聊天记录
*
* @param userId 用户ID
* @return 聊天记录集合
*/
@Override
public List<ChatHistory> 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<ChatHistory> 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);
}
}

View File

@ -0,0 +1,113 @@
<?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.ruoyi.system.mapper.ChatHistoryMapper">
<resultMap type="ChatHistory" id="ChatHistoryResult">
<result property="id" column="id" />
<result property="userId" column="user_id" />
<result property="sessionId" column="session_id" />
<result property="messageType" column="message_type" />
<result property="content" column="content" />
<result property="isLink" column="is_link" />
<result property="isButton" column="is_button" />
<result property="createTime" column="create_time" />
</resultMap>
<sql id="selectChatHistoryVo">
select id, user_id, session_id, message_type, content, is_link, is_button, create_time from chat_history
</sql>
<select id="selectChatHistoryList" parameterType="ChatHistory" resultMap="ChatHistoryResult">
<include refid="selectChatHistoryVo"/>
<where>
<if test="userId != null and userId != ''"> and user_id = #{userId}</if>
<if test="sessionId != null and sessionId != ''"> and session_id = #{sessionId}</if>
<if test="messageType != null and messageType != ''"> and message_type = #{messageType}</if>
<if test="content != null and content != ''"> and content like concat('%', #{content}, '%')</if>
<if test="isLink != null "> and is_link = #{isLink}</if>
<if test="isButton != null "> and is_button = #{isButton}</if>
</where>
order by create_time asc
</select>
<select id="selectChatHistoryById" parameterType="Long" resultMap="ChatHistoryResult">
<include refid="selectChatHistoryVo"/>
where id = #{id}
</select>
<select id="selectChatHistoryBySessionId" parameterType="String" resultMap="ChatHistoryResult">
<include refid="selectChatHistoryVo"/>
where session_id = #{sessionId}
order by create_time asc
</select>
<select id="selectChatHistoryByUserId" parameterType="String" resultMap="ChatHistoryResult">
<include refid="selectChatHistoryVo"/>
where user_id = #{userId}
order by create_time asc
</select>
<insert id="insertChatHistory" parameterType="ChatHistory" useGeneratedKeys="true" keyProperty="id">
insert into chat_history
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="userId != null">user_id,</if>
<if test="sessionId != null">session_id,</if>
<if test="messageType != null">message_type,</if>
<if test="content != null">content,</if>
<if test="isLink != null">is_link,</if>
<if test="isButton != null">is_button,</if>
<if test="createTime != null">create_time,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="userId != null">#{userId},</if>
<if test="sessionId != null">#{sessionId},</if>
<if test="messageType != null">#{messageType},</if>
<if test="content != null">#{content},</if>
<if test="isLink != null">#{isLink},</if>
<if test="isButton != null">#{isButton},</if>
<if test="createTime != null">#{createTime},</if>
</trim>
</insert>
<insert id="batchInsertChatHistory" parameterType="java.util.List">
insert into chat_history (user_id, session_id, message_type, content, is_link, is_button, create_time) values
<foreach collection="list" item="item" separator=",">
(#{item.userId}, #{item.sessionId}, #{item.messageType}, #{item.content}, #{item.isLink}, #{item.isButton}, #{item.createTime})
</foreach>
</insert>
<update id="updateChatHistory" parameterType="ChatHistory">
update chat_history
<trim prefix="SET" suffixOverrides=",">
<if test="userId != null">user_id = #{userId},</if>
<if test="sessionId != null">session_id = #{sessionId},</if>
<if test="messageType != null">message_type = #{messageType},</if>
<if test="content != null">content = #{content},</if>
<if test="isLink != null">is_link = #{isLink},</if>
<if test="isButton != null">is_button = #{isButton},</if>
<if test="createTime != null">create_time = #{createTime},</if>
</trim>
where id = #{id}
</update>
<delete id="deleteChatHistoryById" parameterType="Long">
delete from chat_history where id = #{id}
</delete>
<delete id="deleteChatHistoryByIds" parameterType="String">
delete from chat_history where id in
<foreach item="id" collection="array" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
<delete id="deleteChatHistoryBySessionId" parameterType="String">
delete from chat_history where session_id = #{sessionId}
</delete>
<delete id="deleteChatHistoryByUserId" parameterType="String">
delete from chat_history where user_id = #{userId}
</delete>
</mapper>