Explorar o código

增加删除会话消息功能。

wukai hai 1 ano
pai
achega
44140e3a0b

+ 0 - 3
doc-biz/src/main/java/com/doc/biz/ws/WebSocketServer.java

@@ -57,10 +57,7 @@ public class WebSocketServer {
             List<ChatMsg> list = msgService.selectChatMsgList(unRead);
             Thread.sleep(2000);
             for (ChatMsg msg : list) {
-                msg.setRemark("unRead");
                 session.getBasicRemote().sendText(JSON.toJSONString(msg));
-//                msg.setIsRead("Y");
-//                msgService.updateChatMsg(msg);
             }
         } catch (IOException e) {
             throw new RuntimeException(e);

+ 13 - 1
doc-biz/src/main/java/com/doc/chat/controller/ChatMsgController.java

@@ -55,7 +55,7 @@ public class ChatMsgController extends BaseController {
         List<ChatMsg> list = chatMsgService.selectRecordList(SecurityUtils.getUserId(), toId);
         list.forEach(msg -> {
             if (msg.getToId().equals(SecurityUtils.getUserId()) && !"Y".equals(msg.getIsRead())) {
-                ChatMsg chatMsg=new ChatMsg();
+                ChatMsg chatMsg = new ChatMsg();
                 chatMsg.setMsgId(msg.getMsgId());
                 chatMsg.setIsRead("Y");
                 chatMsgService.updateChatMsg(chatMsg);
@@ -65,6 +65,18 @@ public class ChatMsgController extends BaseController {
     }
 
     /**
+     * 消息记录
+     *
+     * @param toId 用户ID
+     * @return
+     */
+    @ApiOperation("删除会话消息")
+    @DeleteMapping("/del/{toId}")
+    public AjaxResult del(@PathVariable Long toId) {
+        return toAjax(chatMsgService.delRecord(SecurityUtils.getUserId(), toId));
+    }
+
+    /**
      * 好友列表功能
      */
     @ApiOperation("好友列表")

+ 59 - 0
doc-biz/src/main/java/com/doc/chat/domain/ChatMsgRecord.java

@@ -0,0 +1,59 @@
+package com.doc.chat.domain;
+
+import com.baomidou.mybatisplus.annotation.FieldStrategy;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.doc.common.core.domain.BaseEntity;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import org.apache.commons.lang3.builder.ToStringBuilder;
+import org.apache.commons.lang3.builder.ToStringStyle;
+
+/**
+ * 聊天记录管理对象 chat_msg_record
+ *
+ * @author wukai
+ * @date 2023-11-12
+ */
+@ApiModel(value = "ChatMsgRecord", description = "聊天记录管理")
+public class ChatMsgRecord extends BaseEntity {
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * 用户ID
+     */
+    @ApiModelProperty("用户ID")
+    @TableId
+    private Long userId;
+
+    /**
+     * 消息ID
+     */
+    @ApiModelProperty("消息ID")
+    @TableField(value = "MSG_ID", insertStrategy = FieldStrategy.IGNORED)
+    private Long msgId;
+
+    public void setUserId(Long userId) {
+        this.userId = userId;
+    }
+
+    public Long getUserId() {
+        return userId;
+    }
+
+    public void setMsgId(Long msgId) {
+        this.msgId = msgId;
+    }
+
+    public Long getMsgId() {
+        return msgId;
+    }
+
+    @Override
+    public String toString() {
+        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
+                .append("userId", getUserId())
+                .append("msgId", getMsgId())
+                .toString();
+    }
+}

+ 3 - 2
doc-biz/src/main/java/com/doc/chat/mapper/ChatMsgMapper.java

@@ -64,8 +64,9 @@ public interface ChatMsgMapper extends BaseMapper<ChatMsg> {
     /**
      * 获取聊天记录
      *
-     * @param fromId 发送方ID
-     * @param toId 接收方ID
+     * @param fromId  发送方ID
+     * @param toId    接收方ID
+     * @param idIndex id
      * @return 聊天记录列表
      */
     List<ChatMsg> selectRecordList(@Param("fromId") Long fromId, @Param("toId") Long toId, @Param("idIndex") String idIndex);

+ 72 - 0
doc-biz/src/main/java/com/doc/chat/mapper/ChatMsgRecordMapper.java

@@ -0,0 +1,72 @@
+package com.doc.chat.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.doc.chat.domain.ChatMsgRecord;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
+
+/**
+ * 聊天记录管理Mapper接口
+ *
+ * @author wukai
+ * @date 2023-11-12
+ */
+public interface ChatMsgRecordMapper extends BaseMapper<ChatMsgRecord> {
+    /**
+     * 查询聊天记录管理
+     *
+     * @param userId 聊天记录管理主键
+     * @return 聊天记录管理
+     */
+    public ChatMsgRecord selectChatMsgRecordByUserId(Long userId);
+
+    /**
+     * 查询聊天记录管理列表
+     *
+     * @param chatMsgRecord 聊天记录管理
+     * @return 聊天记录管理集合
+     */
+    public List<ChatMsgRecord> selectChatMsgRecordList(ChatMsgRecord chatMsgRecord);
+
+    /**
+     * 新增聊天记录管理
+     *
+     * @param chatMsgRecord 聊天记录管理
+     * @return 结果
+     */
+    public int insertChatMsgRecord(ChatMsgRecord chatMsgRecord);
+
+    /**
+     * 修改聊天记录管理
+     *
+     * @param chatMsgRecord 聊天记录管理
+     * @return 结果
+     */
+    public int updateChatMsgRecord(ChatMsgRecord chatMsgRecord);
+
+    /**
+     * 删除聊天记录管理
+     *
+     * @param userId 聊天记录管理主键
+     * @return 结果
+     */
+    public int deleteChatMsgRecordByUserId(Long userId);
+
+    /**
+     * 批量删除聊天记录管理
+     *
+     * @param userIds 需要删除的数据主键集合
+     * @return 结果
+     */
+    public int deleteChatMsgRecordByUserIds(Long[] userIds);
+
+    /**
+     * 删除聊天记录
+     *
+     * @param userId  用户ID
+     * @param idIndex id组合
+     * @return 结果
+     */
+    int delRecord(@Param("userId") Long userId, @Param("idIndex") String idIndex);
+}

+ 70 - 0
doc-biz/src/main/java/com/doc/chat/service/IChatMsgRecordService.java

@@ -0,0 +1,70 @@
+package com.doc.chat.service;
+
+import com.doc.chat.domain.ChatMsgRecord;
+
+import java.util.List;
+
+/**
+ * 聊天记录管理Service接口
+ *
+ * @author wukai
+ * @date 2023-11-12
+ */
+public interface IChatMsgRecordService {
+    /**
+     * 查询聊天记录管理
+     *
+     * @param userId 聊天记录管理主键
+     * @return 聊天记录管理
+     */
+    public ChatMsgRecord selectChatMsgRecordByUserId(Long userId);
+
+    /**
+     * 查询聊天记录管理列表
+     *
+     * @param chatMsgRecord 聊天记录管理
+     * @return 聊天记录管理集合
+     */
+    public List<ChatMsgRecord> selectChatMsgRecordList(ChatMsgRecord chatMsgRecord);
+
+    /**
+     * 新增聊天记录管理
+     *
+     * @param chatMsgRecord 聊天记录管理
+     * @return 结果
+     */
+    public int insertChatMsgRecord(ChatMsgRecord chatMsgRecord);
+
+    /**
+     * 修改聊天记录管理
+     *
+     * @param chatMsgRecord 聊天记录管理
+     * @return 结果
+     */
+    public int updateChatMsgRecord(ChatMsgRecord chatMsgRecord);
+
+    /**
+     * 批量删除聊天记录管理
+     *
+     * @param userIds 需要删除的聊天记录管理主键集合
+     * @return 结果
+     */
+    public int deleteChatMsgRecordByUserIds(Long[] userIds);
+
+    /**
+     * 删除聊天记录管理信息
+     *
+     * @param userId 聊天记录管理主键
+     * @return 结果
+     */
+    public int deleteChatMsgRecordByUserId(Long userId);
+
+    /**
+     * 删除聊天记录
+     *
+     * @param userId  用户ID
+     * @param idIndex id组合
+     * @return 结果
+     */
+    int delRecord(Long userId, String idIndex);
+}

+ 10 - 0
doc-biz/src/main/java/com/doc/chat/service/IChatMsgService.java

@@ -86,8 +86,18 @@ public interface IChatMsgService {
 
     /**
      * 发送系统消息
+     *
      * @param msg 消息
      * @return 结果
      */
     int sendSysMsg(ChatMsg msg);
+
+    /**
+     * 删除聊天记录
+     *
+     * @param userId 用户ID
+     * @param toId   对方ID
+     * @return 结果
+     */
+    int delRecord(Long userId, Long toId);
 }

+ 99 - 0
doc-biz/src/main/java/com/doc/chat/service/impl/ChatMsgRecordServiceImpl.java

@@ -0,0 +1,99 @@
+package com.doc.chat.service.impl;
+
+import com.doc.chat.domain.ChatMsgRecord;
+import com.doc.chat.mapper.ChatMsgRecordMapper;
+import com.doc.chat.service.IChatMsgRecordService;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.util.List;
+
+/**
+ * 聊天记录管理Service业务层处理
+ *
+ * @author wukai
+ * @date 2023-11-12
+ */
+@Service
+public class ChatMsgRecordServiceImpl implements IChatMsgRecordService {
+    @Resource
+    private ChatMsgRecordMapper chatMsgRecordMapper;
+
+    /**
+     * 查询聊天记录管理
+     *
+     * @param userId 聊天记录管理主键
+     * @return 聊天记录管理
+     */
+    @Override
+    public ChatMsgRecord selectChatMsgRecordByUserId(Long userId) {
+        return chatMsgRecordMapper.selectChatMsgRecordByUserId(userId);
+    }
+
+    /**
+     * 查询聊天记录管理列表
+     *
+     * @param chatMsgRecord 聊天记录管理
+     * @return 聊天记录管理
+     */
+    @Override
+    public List<ChatMsgRecord> selectChatMsgRecordList(ChatMsgRecord chatMsgRecord) {
+        return chatMsgRecordMapper.selectChatMsgRecordList(chatMsgRecord);
+    }
+
+    /**
+     * 新增聊天记录管理
+     *
+     * @param chatMsgRecord 聊天记录管理
+     * @return 结果
+     */
+    @Override
+    public int insertChatMsgRecord(ChatMsgRecord chatMsgRecord) {
+        return chatMsgRecordMapper.insertChatMsgRecord(chatMsgRecord);
+    }
+
+    /**
+     * 修改聊天记录管理
+     *
+     * @param chatMsgRecord 聊天记录管理
+     * @return 结果
+     */
+    @Override
+    public int updateChatMsgRecord(ChatMsgRecord chatMsgRecord) {
+        return chatMsgRecordMapper.updateChatMsgRecord(chatMsgRecord);
+    }
+
+    /**
+     * 批量删除聊天记录管理
+     *
+     * @param userIds 需要删除的聊天记录管理主键
+     * @return 结果
+     */
+    @Override
+    public int deleteChatMsgRecordByUserIds(Long[] userIds) {
+        return chatMsgRecordMapper.deleteChatMsgRecordByUserIds(userIds);
+    }
+
+    /**
+     * 删除聊天记录管理信息
+     *
+     * @param userId 聊天记录管理主键
+     * @return 结果
+     */
+    @Override
+    public int deleteChatMsgRecordByUserId(Long userId) {
+        return chatMsgRecordMapper.deleteChatMsgRecordByUserId(userId);
+    }
+
+    /**
+     * 删除聊天记录
+     *
+     * @param userId  用户ID
+     * @param idIndex id组合
+     * @return 结果
+     */
+    @Override
+    public int delRecord(Long userId, String idIndex) {
+        return chatMsgRecordMapper.delRecord(userId, idIndex);
+    }
+}

+ 30 - 1
doc-biz/src/main/java/com/doc/chat/service/impl/ChatMsgServiceImpl.java

@@ -3,7 +3,9 @@ package com.doc.chat.service.impl;
 import com.alibaba.fastjson2.JSON;
 import com.doc.biz.service.IDocInfoService;
 import com.doc.chat.domain.ChatMsg;
+import com.doc.chat.domain.ChatMsgRecord;
 import com.doc.chat.mapper.ChatMsgMapper;
+import com.doc.chat.service.IChatMsgRecordService;
 import com.doc.chat.service.IChatMsgService;
 import com.doc.common.constant.CacheConstants;
 import com.doc.common.core.domain.entity.SysUser;
@@ -36,6 +38,8 @@ public class ChatMsgServiceImpl implements IChatMsgService {
     private StringRedisTemplate stringRedisTemplate;
     @Resource
     private IDocInfoService docInfoService;
+    @Resource
+    private IChatMsgRecordService recordService;
 
 
     /**
@@ -71,7 +75,20 @@ public class ChatMsgServiceImpl implements IChatMsgService {
         String idIndex = getIdIndex(chatMsg.getFromId(), chatMsg.getToId());
         chatMsg.setIdIndex(idIndex);
         chatMsg.setCreateTime(DateUtils.getNowDate());
-        return chatMsgMapper.insertChatMsg(chatMsg);
+        int i = chatMsgMapper.insertChatMsg(chatMsg);
+        if (chatMsg.getFromId() != -1) {
+            //如果是系统消息则不用管
+            ChatMsgRecord from = new ChatMsgRecord();
+            from.setMsgId(chatMsg.getMsgId());
+            from.setUserId(chatMsg.getFromId());
+            recordService.insertChatMsgRecord(from);
+        }
+        ChatMsgRecord to = new ChatMsgRecord();
+        to.setMsgId(chatMsg.getMsgId());
+        to.setUserId(chatMsg.getToId());
+        recordService.insertChatMsgRecord(to);
+
+        return i;
     }
 
     private String getIdIndex(Long fromId, Long toId) {
@@ -176,6 +193,18 @@ public class ChatMsgServiceImpl implements IChatMsgService {
     }
 
     /**
+     * 删除聊天记录
+     *
+     * @param userId 用户ID
+     * @param toId   对方ID
+     * @return 结果
+     */
+    @Override
+    public int delRecord(Long userId, Long toId) {
+        return recordService.delRecord(userId, getIdIndex(userId, toId));
+    }
+
+    /**
      * 设置用户及文件信息
      *
      * @param msgList 消息列表

+ 18 - 5
doc-biz/src/main/resources/mapper/chat/ChatMsgMapper.xml

@@ -47,15 +47,28 @@
 
     <select id="selectFriendList" resultType="com.doc.chat.domain.ChatMsg">
         <include refid="selectChatMsgVo"/>
-        where msg_id in (SELECT MAX(msg_id) msg_id
-        FROM chat_msg
-        WHERE from_id = #{userId} OR to_id = #{userId}
+        where msg_id in (SELECT MAX(a.msg_id) msg_id
+        FROM chat_msg a
+        WHERE (from_id = #{userId} OR to_id = #{userId}) and
+        exists (select 1 from chat_msg_record where user_id = #{userId} and msg_id=a.msg_id)
         GROUP BY id_index)
         ORDER BY create_time desc,msg_id desc
     </select>
     <select id="selectRecordList" resultType="com.doc.chat.domain.ChatMsg">
-        <include refid="selectChatMsgVo"/>
-        where id_index=#{idIndex}
+        select a.MSG_ID,
+               FROM_ID,
+               TO_ID,
+               MSG_TYPE,
+               CONTENT,
+               IS_READ,
+               ID_INDEX,
+               CREATE_TIME
+        from chat_msg a,
+             (select *
+              from chat_msg_record
+              where user_id = #{fromId}) b
+        where a.msg_id = b.msg_id
+          and a.id_index = #{idIndex}
         order by CREATE_TIME desc
     </select>
 

+ 65 - 0
doc-biz/src/main/resources/mapper/chat/ChatMsgRecordMapper.xml

@@ -0,0 +1,65 @@
+<?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.doc.chat.mapper.ChatMsgRecordMapper">
+
+    <resultMap type="ChatMsgRecord" id="ChatMsgRecordResult">
+        <result property="userId" column="USER_ID"/>
+        <result property="msgId" column="MSG_ID"/>
+    </resultMap>
+
+    <sql id="selectChatMsgRecordVo">
+        select USER_ID, MSG_ID
+        from chat_msg_record
+    </sql>
+
+    <select id="selectChatMsgRecordList" parameterType="ChatMsgRecord" resultMap="ChatMsgRecordResult">
+        <include refid="selectChatMsgRecordVo"/>
+        <where>
+        </where>
+    </select>
+
+    <select id="selectChatMsgRecordByUserId" parameterType="Long" resultMap="ChatMsgRecordResult">
+        <include refid="selectChatMsgRecordVo"/>
+        where USER_ID = #{userId}
+    </select>
+
+    <insert id="insertChatMsgRecord" parameterType="ChatMsgRecord">
+        insert into chat_msg_record
+        <trim prefix="(" suffix=")" suffixOverrides=",">
+            <if test="userId != null">USER_ID,</if>
+            <if test="msgId != null">MSG_ID,</if>
+        </trim>
+        <trim prefix="values (" suffix=")" suffixOverrides=",">
+            <if test="userId != null">#{userId},</if>
+            <if test="msgId != null">#{msgId},</if>
+        </trim>
+    </insert>
+
+    <update id="updateChatMsgRecord" parameterType="ChatMsgRecord">
+        update chat_msg_record
+        <trim prefix="SET" suffixOverrides=",">
+            <if test="msgId != null">MSG_ID = #{msgId},</if>
+        </trim>
+        where USER_ID = #{userId}
+    </update>
+
+    <delete id="deleteChatMsgRecordByUserId" parameterType="Long">
+        delete
+        from chat_msg_record
+        where USER_ID = #{userId}
+    </delete>
+
+    <delete id="deleteChatMsgRecordByUserIds" parameterType="String">
+        delete from chat_msg_record where USER_ID in
+        <foreach item="userId" collection="array" open="(" separator="," close=")">
+            #{userId}
+        </foreach>
+    </delete>
+    <delete id="delRecord">
+        delete
+        from chat_msg_record
+        where USER_ID = #{userId} and msg_id in(  select msg_id from chat_msg where id_index = #{idIndex} )
+    </delete>
+</mapper>

+ 7 - 0
pom.xml

@@ -77,6 +77,13 @@
                 <artifactId>mybatis-plus-boot-starter</artifactId>
                 <version>${mybatis-plus.version}</version>
             </dependency>
+
+            <dependency>
+                <groupId>io.github.penggle</groupId>
+                <artifactId>mybatis-tiny-core</artifactId>
+                <!-- 版本说明:3.5指的是基于Mybatis 3.5.x版本的意思 -->
+                <version>3.5.1</version>
+            </dependency>
             <!-- 获取系统信息 -->
             <dependency>
                 <groupId>com.github.oshi</groupId>