fangzhen 6 месяцев назад
Родитель
Сommit
e3e9345cf8

+ 4 - 3
ruoyi-generator/src/main/java/com/ruoyi/generator/controller/CommunityChatMsgController.java

@@ -44,7 +44,7 @@ public class CommunityChatMsgController extends BaseController {
      */
     @ApiOperation("获取当前登录用户聊天记录")
     @GetMapping()
-    public AjaxResult chatRecord(Long otherUserId) {
+    public AjaxResult chatRecord(Long otherUserId, String type) {
         Long userId = SecurityUtils.getUserId();
         List<CommunityChatMsg> chatMsgList = communityChatMsgService.list(new QueryWrapper<CommunityChatMsg>()
                 .eq("sender_id", userId)
@@ -52,6 +52,7 @@ public class CommunityChatMsgController extends BaseController {
                 .or()
                 .eq("sender_id", otherUserId)
                 .eq("receiver_id", userId)
+                .eq("type", type)
                 .orderByDesc("create_time"));
         //更新该消息标记为已读
         List<Long> msgId = chatMsgList.stream()
@@ -85,9 +86,9 @@ public class CommunityChatMsgController extends BaseController {
      */
     @ApiOperation("获取当前登录用户聊天列表")
     @GetMapping("/list")
-    public AjaxResult chatList() {
+    public AjaxResult chatList(String type) {
         Long userId = SecurityUtils.getUserId();
-        List<SysUserVo> sysUserChatVos = communityChatMsgService.getChatListWithLatestMessage(userId);
+        List<SysUserVo> sysUserChatVos = communityChatMsgService.getChatListWithLatestMessage(userId, type);
         return AjaxResult.success(sysUserChatVos);
     }
 }

+ 2 - 1
ruoyi-system/src/main/java/com/ruoyi/system/mapper/CommunityChatMsgMapper.java

@@ -12,8 +12,9 @@ import java.util.List;
 public interface CommunityChatMsgMapper extends BaseMapper<CommunityChatMsg> {
     /**
      * 获取登录用户的聊天列表
+     *
      * @param userId
      * @return
      */
-    List<SysUserVo> getChatListWithLatestMessage(@Param("userId") Long userId);
+    List<SysUserVo> getChatListWithLatestMessage(@Param("userId") Long userId, @Param("type") String type);
 }

+ 2 - 1
ruoyi-system/src/main/java/com/ruoyi/system/service/ICommunityChatMsgService.java

@@ -10,8 +10,9 @@ import java.util.List;
 public interface ICommunityChatMsgService extends IService<CommunityChatMsg> {
     /**
      * 获取登录用户的聊天列表
+     *
      * @param userId 用户id
      * @return 用户聊天列表
      */
-    List<SysUserVo> getChatListWithLatestMessage(@Param("userId") Long userId);
+    List<SysUserVo> getChatListWithLatestMessage(@Param("userId") Long userId, @Param("type") String type);
 }

+ 3 - 2
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/CommunityChatMsgServiceImpl.java

@@ -18,11 +18,12 @@ public class CommunityChatMsgServiceImpl extends ServiceImpl<CommunityChatMsgMap
 
     /**
      * 获取登录用户的聊天列表
+     *
      * @param userId 用户id
      * @return 用户聊天列表
      */
     @Override
-    public List<SysUserVo> getChatListWithLatestMessage(Long userId) {
-        return communityChatMsgMapper.getChatListWithLatestMessage(userId);
+    public List<SysUserVo> getChatListWithLatestMessage(Long userId, String type) {
+        return communityChatMsgMapper.getChatListWithLatestMessage(userId, type);
     }
 }

+ 12 - 13
ruoyi-system/src/main/resources/mapper/system/CommunityChatMsgMapper.xml

@@ -3,13 +3,13 @@
 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.ruoyi.system.mapper.CommunityChatMsgMapper">
-	<select id="getChatListWithLatestMessage" parameterType="java.lang.Long" resultType="com.ruoyi.system.domain.vo.SysUserVo">
+	<select id="getChatListWithLatestMessage" resultType="com.ruoyi.system.domain.vo.SysUserVo">
         SELECT u.user_id                                                             AS id,
-               u.nick_name as nickName,
-               u.user_name as username,
-               u.avatar as avatar,
+               u.nick_name                                                           as nickName,
+               u.user_name                                                           as username,
+               u.avatar                                                              as avatar,
                (SELECT u2.nick_name from sys_user u2 where u2.user_id = r.sender_id) as senderNickName,
-               r.is_read                                                             AS isRead,
+               r.is_read                                                             as isRead,
                r.content                                                             AS msg,
                r.create_time                                                         AS msgTime
         FROM (SELECT CASE
@@ -18,18 +18,17 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
                          END          AS chat_user_id,
                      MAX(create_time) AS latest_time
               FROM community_chat_msg
-              WHERE sender_id = #{userId}
-                 OR receiver_id = #{userId}
+              WHERE (sender_id = #{userId}
+                  OR receiver_id = #{userId})
+                and type = #{type}
               GROUP BY chat_user_id) latest
                  JOIN community_chat_msg r ON
             (
-                (r.sender_id = #{userId}
-                     AND r.receiver_id = latest.chat_user_id
+                (r.sender_id = #{userId} AND r.receiver_id = latest.chat_user_id
                     OR
-                 r.sender_id = latest.chat_user_id
-                     AND r.receiver_id = #{userId})
-                    AND r.create_time = latest.latest_time
-                )
+                 r.sender_id = latest.chat_user_id AND r.receiver_id = #{userId})
+                    AND r.create_time = latest.latest_time and r.type = #{type}
+            )
                  JOIN sys_user u ON
             u.user_id = latest.chat_user_id
         ORDER BY latest.latest_time DESC