问答社区-私信列表、发送列表

1.私信列表

  • 查询当前用户的会话列表
    每个会话只显示一条最新的私信

  • 支持分页显示
    messagemapper

    @Mapper
    public interface MessageMapper {
    
      //查询当前用户会话列表,针对每一个会话只返回一条最新的私信
      List<Message> selectConversations(@Param("userId") int userId, @Param("offset") int offset, @Param("limit") int limit);
    
      //查询当前用户会话数量
      int selectConversationCount(@Param("userId") int userId);
    
      //查询某个会话所包含的私信列表
      List<Message> selectLetters(@Param("conversationId") String conversationId, @Param("offset") int offset, @Param("limit") int limit);
    
      //查询某个会话所包含的私信数量
      int selectLetterCount(@Param("conversationId") String conversationId);
    
      //查询未读私信数量
      int selectLetterUnreadCount(@Param("userId") int userId, @Param("conversationId") String conversationId);
    }
    <?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.gpnu.community.dao.MessageMapper">
      <sql id="selectFields">
          id, from_id, to_id, conversation_id, content, status, create_time
      </sql>
    
      <sql id="insertFields">
          from_id, to_id, conversation_id, content, status, create_time
      </sql>
    
      <select id="selectConversations" resultType="Message">
          select
          <include refid="selectFields"></include>
          from message where id in
          (select max(id) FROM message
          WHERE status != 2
          and from_id != 1
          and (from_id = #{userId} or to_id = #{userId})
          group by conversation_id)
          order by id desc
          LIMIT #{offset},#{limit}
      </select>
    
      <select id="selectConversationCount" resultType="int">
          SELECT count(m.maxid)
          FROM (
                   SELECT max(id) AS maxid
                   FROM message
                   WHERE status != 2
                         AND from_id != 1
                         AND (from_id = #{userId} OR to_id = #{userId})
                   GROUP BY conversation_id
               ) AS m
      </select>
    
      <select id="selectLetters" resultType="Message">
          select
          <include refid="selectFields"></include>
          from message
          where status != 2
          and from_id != 1
          and conversation_id = #{conversationId}
          order by id DESC
          limit #{offset},#{limit}
      </select>
    
      <select id="selectLetterCount" resultType="int">
          SELECT count(id)
          FROM message
          WHERE status != 2
                AND from_id != 1
                AND conversation_id = #{conversationId}
      </select>
    
      <select id="selectLetterUnreadCount" resultType="int">
          select count(id)
          from message
          where status = 0
          and from_id != 1
          and to_id = #{userId}
          <if test="conversationId!=null">
              and conversation_id = #{conversationId}
          </if>
      </select>
    </mapper>

    2.私信详情

  • 查询某个会话所包含的私信

  • 支持分页显示
    controller

    @RequestMapping(value = "/letter/detail/{conversationId}", method = RequestMethod.GET)
      public String getLetterDetail(Model model, Page page, @PathVariable("conversationId") String conversationId) {
          User user = hostHolder.getUser();
          //分页
          page.setLimit(5);
          page.setPath("/letter/detail/" + conversationId);
          page.setRows(messageService.findLetterCount(conversationId));
    
          List<Message> letterList = messageService.findLetters(conversationId, page.getOffset(), page.getLimit());
          List<Map<String, Object>> letters = new ArrayList<>();
          if (letterList != null) {
              for (Message letter : letterList) {
                  Map<String, Object> map = new HashMap<>();
                  map.put("letter", letter);
                  map.put("fromUser", userService.findUserById(letter.getFromId()));
                  letters.add(map);
              }
          }
          model.addAttribute("letters", letters);
    
          //私信目标
          model.addAttribute("target", getLetterTarget(conversationId));
    
          return "/site/letter-detail";
      }
    
      private User getLetterTarget(String conversationId) {
          String[] ids = conversationId.split("_");
          int id0 = Integer.parseInt(ids[0]);
          int id1 = Integer.parseInt(ids[1]);
    
          if (hostHolder.getUser().getId() == id0) {
              return userService.findUserById(id1);
          } else {
              return userService.findUserById(id0);
          }
      }
全部评论

相关推荐

不愿透露姓名的神秘牛友
11-26 15:46
已编辑
字节国际 电商后端 24k-35k
点赞 评论 收藏
分享
沉淀一会:**圣经 1.同学你面试评价不错,概率很大,请耐心等待;2.你的排名比较靠前,不要担心,耐心等待;3.问题不大,正在审批,不要着急签其他公司,等等我们!4.预计9月中下旬,安心过节;5.下周会有结果,请耐心等待下;6.可能国庆节前后,一有结果我马上通知你;7.预计10月中旬,再坚持一下;8.正在走流程,就这两天了;9.同学,结果我也不知道,你如果查到了也告诉我一声;10.同学你出线不明朗,建议签其他公司保底!11.同学你找了哪些公司,我也在找工作。
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务