疫情防控交流社区平台——5.2 Kafka构建TB级异步消息系统(显示系统通知)

Kafka构建TB级异步消息系统(显示系统通知)

在这里插入图片描述

1.MessageMapper

	//查询某个主题下最新的通知
    Message selectLatestNotice(int userId,String topic);
    //查询某个主题所包含的通知数量
    int selectNoticeCount(int userId,String topic);
    //查询未读的通知的数量
    int selectNoticeUnreadCount(int userId,String topic);

2.message-mapper.xml

	<!--查询某个主题下最新的通知-->
    <select id="selectLatestNotice" 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 to_id = #{userId}
            and conversation_id = #{topic}
        )
    </select>
    <!--查询某个主题所包含的通知数量-->
    <select id="selectNoticeCount" resultType="int">
        select count(id) from message
            where status != 2
            and from_id = 1
            and to_id = #{userId}
            and conversation_id = #{topic}
    </select>
    <!--查询未读的通知的数量-->
    <select id="selectNoticeUnreadCount" resultType="int">
        select count(id) from message
            where status = 0
            and from_id = 1
            and to_id = #{userId}
            <if test="topic!=null and topic!= ''">
                and conversation_id = #{topic}
            </if>
    </select>

3.MessageService

	//查询某个主题下最新的通知
    public Message findLatestNotice(int userId,String topic){
        Message message = messageMapper.selectLatestNotice(userId, topic);
        return message;
    }
    //查询某个主题所包含的通知数量
    public int findNoticeCount(int userId,String topic){
        return messageMapper.selectNoticeCount(userId, topic);
    }
    //查询未读的通知的数量
    public int findNoticeUnreadCount(int userId,String topic){
        return messageMapper.selectNoticeUnreadCount(userId, topic);
    }

4.MessageController

	//※查询通知列表
    @RequestMapping(value = "/notice/list",method = RequestMethod.GET)
    public String getNoticeList(Model model){
        User users = hostHolder.getUsers();
        //1.查询评论类的通知
        Message latestNotice = messageService.findLatestNotice(users.getId(), TOPIC_COMMENT);
        Map<String,Object> messageVo = new HashMap<>();//用来封装要聚合的数据
        if (latestNotice != null){
            messageVo.put("message",latestNotice);
            //通知的内容在数据库中是json字符串存储的,所以要转回map对象
            String content = HtmlUtils.htmlUnescape(latestNotice.getContent());
            Map<String,Object> date = JSONObject.parseObject(content, HashMap.class);
            messageVo.put("user",userService.findByUserId((Integer) date.get("userId")));
            messageVo.put("entityType",date.get("entityType"));
            messageVo.put("entityId",date.get("entityId"));
            messageVo.put("postId",date.get("postId"));
            int count = messageService.findNoticeCount(users.getId(), TOPIC_COMMENT);
            messageVo.put("count",count);
            int unreadCount = messageService.findNoticeUnreadCount(users.getId(), TOPIC_COMMENT);
            messageVo.put("unreadCount",unreadCount);
        }
        model.addAttribute("commentNotice",messageVo);
        //2.查询点赞类的通知(复用变量名和vo)
        latestNotice = messageService.findLatestNotice(users.getId(), TOPIC_LIKE);
        messageVo = new HashMap<>();//用来封装要聚合的数据
        if (latestNotice != null){
            messageVo.put("message",latestNotice);
            //通知的内容在数据库中是json字符串存储的,所以要转回map对象
            String content = HtmlUtils.htmlUnescape(latestNotice.getContent());
            Map<String,Object> date = JSONObject.parseObject(content, HashMap.class);
            messageVo.put("user",userService.findByUserId((Integer) date.get("userId")));
            messageVo.put("entityType",date.get("entityType"));
            messageVo.put("entityId",date.get("entityId"));
            messageVo.put("postId",date.get("postId"));
            int count = messageService.findNoticeCount(users.getId(), TOPIC_LIKE);
            messageVo.put("count",count);
            int unreadCount = messageService.findNoticeUnreadCount(users.getId(), TOPIC_LIKE);
            messageVo.put("unreadCount",unreadCount);
        }
        model.addAttribute("likeNotice",messageVo);
        //3.查询关注类的通知
        latestNotice = messageService.findLatestNotice(users.getId(), TOPIC_FOLLOW);
        messageVo = new HashMap<>();//用来封装要聚合的数据
        if (latestNotice != null){
            messageVo.put("message",latestNotice);
            //通知的内容在数据库中是json字符串存储的,所以要转回map对象
            String content = HtmlUtils.htmlUnescape(latestNotice.getContent());
            Map<String,Object> date = JSONObject.parseObject(content, HashMap.class);
            messageVo.put("user",userService.findByUserId((Integer) date.get("userId")));
            messageVo.put("entityType",date.get("entityType"));
            messageVo.put("entityId",date.get("entityId"));
            messageVo.put("postId",date.get("postId"));//可要可不要
            int count = messageService.findNoticeCount(users.getId(), TOPIC_FOLLOW);
            messageVo.put("count",count);
            int unreadCount = messageService.findNoticeUnreadCount(users.getId(), TOPIC_FOLLOW);
            messageVo.put("unreadCount",unreadCount);
        }
        model.addAttribute("followNotice",messageVo);

        //4.查询未读数量
        //私信未读数量
        int letterUnreadCount = messageService.findLetterUnreadCount(users.getId(), null);//传null,不是要某个会话的数量,而是要全部的未读数量
        model.addAttribute("letterUnreadCount",letterUnreadCount);
        //未读通知数量
        int noticeUnreadCount = messageService.findNoticeUnreadCount(users.getId(), null);//要全部主题的未读通知
        model.addAttribute("noticeUnreadCount",noticeUnreadCount);

        return "/site/notice";
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

11_1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值