Browse Source

消息已读未读设置

wukai 2 years ago
parent
commit
a0a038f99b

+ 3 - 1
doc-admin/src/main/java/com/doc/web/controller/system/SysLoginController.java

@@ -12,6 +12,7 @@ import com.doc.common.utils.SecurityUtils;
 import com.doc.framework.web.service.SysLoginService;
 import com.doc.framework.web.service.SysPermissionService;
 import com.doc.system.service.ISysMenuService;
+import org.springframework.data.redis.core.StringRedisTemplate;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
@@ -42,6 +43,8 @@ public class SysLoginController {
 
     @Resource
     private IChatMsgService msgService;
+    @Resource
+    private StringRedisTemplate stringRedisTemplate;
 
     /**
      * 登录方法
@@ -100,7 +103,6 @@ public class SysLoginController {
                 msg.setContent(s);
                 msgService.sendSysMsg(msg);
             });
-
         }
 
         return ajax;

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

@@ -19,6 +19,7 @@ import javax.websocket.Session;
 import javax.websocket.server.PathParam;
 import javax.websocket.server.ServerEndpoint;
 import java.io.IOException;
+import java.util.List;
 import java.util.concurrent.ConcurrentHashMap;
 
 /**
@@ -49,8 +50,22 @@ public class WebSocketServer {
         log.info("用户(ID:{})建立连接!", uid);
         try {
             session.getBasicRemote().sendText("连接成功");
+            //查询用户是否有未读消息,如果有则使用ws推送,延迟2秒
+            Thread.sleep(2000);
+            ChatMsg unRead = new ChatMsg();
+            unRead.setToId(uid);
+            unRead.setIsRead("N");
+            List<ChatMsg> list = msgService.selectChatMsgList(unRead);
+            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);
+        } catch (InterruptedException e) {
+            throw new RuntimeException(e);
         }
     }
 
@@ -74,6 +89,7 @@ public class WebSocketServer {
             ChatMsg msg = mapper.readValue(message, ChatMsg.class);
             msg.setFromId(uid);
             // 保存到数据库
+            msg.setIsRead("N");
             msgService.insertChatMsg(msg);
             //发布集群消息
             this.sendClusterMessage(message);
@@ -103,6 +119,10 @@ public class WebSocketServer {
             try {
                 Session session = currentMap.get(msg.getToId());
                 if (session != null) {
+                    ChatMsg readMsg = new ChatMsg();
+                    readMsg.setMsgId(msg.getMsgId());
+                    readMsg.setIsRead("Y");
+                    msgService.updateChatMsg(readMsg);
                     session.getBasicRemote().sendText(JSON.toJSONString(msg));
                 }
             } catch (Exception e) {

+ 7 - 15
doc-biz/src/main/java/com/doc/chat/domain/ChatMsg.java

@@ -8,8 +8,6 @@ import com.doc.common.core.domain.BaseEntity;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
-import org.apache.commons.lang3.builder.ToStringBuilder;
-import org.apache.commons.lang3.builder.ToStringStyle;
 
 /**
  * 聊天消息管理对象 chat_msg
@@ -89,22 +87,16 @@ public class ChatMsg extends BaseEntity {
     private String idIndex;
 
     /**
+     * ID索引
+     */
+    @ApiModelProperty("是否已读")
+    @Excel(name = "是否已读")
+    private String isRead;
+
+    /**
      * 文件列表
      */
     @ApiModelProperty("文件信息")
     @TableField(exist = false)
     private DocInfo file;
-
-    @Override
-    public String toString() {
-        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
-                .append("msgId", getMsgId())
-                .append("fromId", getFromId())
-                .append("toId", getToId())
-                .append("msgType", getMsgType())
-                .append("content", getContent())
-                .append("idIndex", getIdIndex())
-                .append("createTime", getCreateTime())
-                .toString();
-    }
 }

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

@@ -2,6 +2,7 @@ package com.doc.chat.mapper;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.doc.chat.domain.ChatMsg;
+import org.apache.ibatis.annotations.Param;
 
 import java.util.List;
 
@@ -63,10 +64,11 @@ public interface ChatMsgMapper extends BaseMapper<ChatMsg> {
     /**
      * 获取聊天记录
      *
-     * @param idIndex ID索引
+     * @param fromId 发送方ID
+     * @param toId 接收方ID
      * @return 聊天记录列表
      */
-    List<ChatMsg> selectRecordList(String idIndex);
+    List<ChatMsg> selectRecordList(@Param("fromId") Long fromId, @Param("toId") Long toId, @Param("idIndex") String idIndex);
 
     /**
      * 查询好友列表

+ 4 - 2
doc-biz/src/main/java/com/doc/chat/service/impl/ChatMsgServiceImpl.java

@@ -124,7 +124,7 @@ public class ChatMsgServiceImpl implements IChatMsgService {
      */
     @Override
     public List<ChatMsg> selectRecordList(Long fromId, Long toId) {
-        List<ChatMsg> msgList = chatMsgMapper.selectRecordList(getIdIndex(fromId, toId));
+        List<ChatMsg> msgList = chatMsgMapper.selectRecordList(fromId, toId, getIdIndex(fromId, toId));
         process(msgList);
         return msgList;
     }
@@ -152,9 +152,11 @@ public class ChatMsgServiceImpl implements IChatMsgService {
         if (msg.getFromId().equals(msg.getToId())) {
             return 0;
         }
+        msg.setIsRead("N");
         msg.setCreateTime(new Date());
+        int i = insertChatMsg(msg);
         stringRedisTemplate.convertAndSend(CacheConstants.CHANNEL, JSON.toJSONString(msg));
-        return insertChatMsg(msg);
+        return i;
     }
 
     /**

+ 11 - 6
doc-biz/src/main/resources/mapper/chat/ChatMsgMapper.xml

@@ -8,6 +8,7 @@
         <result property="msgId" column="MSG_ID"/>
         <result property="fromId" column="FROM_ID"/>
         <result property="toId" column="TO_ID"/>
+        <result property="isRead" column="IS_READ"/>
         <result property="msgType" column="MSG_TYPE"/>
         <result property="content" column="CONTENT"/>
         <result property="idIndex" column="ID_INDEX"/>
@@ -24,6 +25,7 @@
         <where>
             <if test="fromId != null ">and FROM_ID = #{fromId}</if>
             <if test="toId != null ">and TO_ID = #{toId}</if>
+            <if test="isRead != null ">and IS_READ = #{isRead}</if>
             <if test="msgType != null  and msgType != ''">and MSG_TYPE = #{msgType}</if>
             <if test="content != null  and content != ''">and CONTENT = #{content}</if>
             <if test="idIndex != null  and idIndex != ''">and ID_INDEX = #{idIndex}</if>
@@ -36,25 +38,26 @@
         where MSG_ID = #{msgId}
     </select>
 
-    <select id="selectRecordList" resultType="com.doc.chat.domain.ChatMsg" parameterType="String">
-        <include refid="selectChatMsgVo"/>
-        where ID_INDEX = #{idIndex}
-        order by CREATE_TIME desc
-    </select>
     <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 = #{user_id} OR to_id = #{user_id}
+        WHERE from_id = #{user_id} OR (to_id = #{user_id} and is_read='Y')
         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} and(from_id=#{fromId} or (is_read='Y' and to_id=#{fromId}))
+        order by CREATE_TIME desc
+    </select>
 
     <insert id="insertChatMsg" parameterType="ChatMsg" useGeneratedKeys="true" keyProperty="msgId">
         insert into chat_msg
         <trim prefix="(" suffix=")" suffixOverrides=",">
             <if test="fromId != null">FROM_ID,</if>
             <if test="toId != null">TO_ID,</if>
+            <if test="isRead != null">IS_READ,</if>
             <if test="msgType != null">MSG_TYPE,</if>
             <if test="content != null">CONTENT,</if>
             <if test="idIndex != null">ID_INDEX,</if>
@@ -63,6 +66,7 @@
         <trim prefix="values (" suffix=")" suffixOverrides=",">
             <if test="fromId != null">#{fromId},</if>
             <if test="toId != null">#{toId},</if>
+            <if test="isRead != null">#{isRead},</if>
             <if test="msgType != null">#{msgType},</if>
             <if test="content != null">#{content},</if>
             <if test="idIndex != null">#{idIndex},</if>
@@ -75,6 +79,7 @@
         <trim prefix="SET" suffixOverrides=",">
             <if test="fromId != null">FROM_ID = #{fromId},</if>
             <if test="toId != null">TO_ID = #{toId},</if>
+            <if test="isRead != null">IS_READ=#{isRead},</if>
             <if test="msgType != null">MSG_TYPE = #{msgType},</if>
             <if test="content != null">CONTENT = #{content},</if>
             <if test="idIndex != null">ID_INDEX = #{idIndex},</if>