From 9a968ba186af5280fcc29cc3eb04412eff204ba7 Mon Sep 17 00:00:00 2001
From: Helius <wangdoubleone@gmail.com>
Date: Thu, 27 May 2021 16:37:33 +0800
Subject: [PATCH] modify

---
 src/main/java/com/xcong/excoin/rabbit/consumer/ChatConsumer.java |  100 +++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 95 insertions(+), 5 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/rabbit/consumer/ChatConsumer.java b/src/main/java/com/xcong/excoin/rabbit/consumer/ChatConsumer.java
index fccfd6e..4902736 100644
--- a/src/main/java/com/xcong/excoin/rabbit/consumer/ChatConsumer.java
+++ b/src/main/java/com/xcong/excoin/rabbit/consumer/ChatConsumer.java
@@ -1,7 +1,97 @@
-package com.xcong.excoin.rabbit.consumer;/**
-*
-* @author wzy
-* @date 2021-05-27
-**/
+package com.xcong.excoin.rabbit.consumer;
+
+
+import cn.hutool.core.bean.BeanUtil;
+import cn.hutool.core.util.StrUtil;
+import com.alibaba.fastjson.JSONObject;
+import com.xcong.excoin.common.contants.AppContants;
+import com.xcong.excoin.configurations.RabbitMqConfig;
+import com.xcong.excoin.modules.otc.dao.OtcMsgHistoryDao;
+import com.xcong.excoin.modules.otc.dao.OtcMsgUserListDao;
+import com.xcong.excoin.modules.otc.entity.OtcMsgHistoryEntity;
+import com.xcong.excoin.modules.otc.entity.OtcMsgUserListEntity;
+import com.xcong.excoin.netty.bean.ChatRequest;
+import com.xcong.excoin.utils.RedisUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.amqp.rabbit.annotation.RabbitListener;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.stereotype.Component;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.Date;
+
+@Slf4j
+@Component
+//@ConditionalOnProperty(prefix = "app", name = "rabbit-consumer", havingValue = "true")
 public class ChatConsumer {
+
+    @Autowired
+    private OtcMsgUserListDao otcMsgUserListDao;
+
+    @Autowired
+    private OtcMsgHistoryDao otcMsgHistoryDao;
+
+    @Autowired
+    private RedisUtils redisUtils;
+
+    @RabbitListener(queues = RabbitMqConfig.QUEUE_MSG_HISTORY)
+    @Transactional(rollbackFor = Exception.class)
+    public void msgHistoryConsumer(String content) {
+        log.info("收到历史消息处理:{}", content);
+        ChatRequest chat = JSONObject.parseObject(content, ChatRequest.class);
+
+        Long toId = Long.parseLong(chat.getTo());
+        Long fromId = Long.parseLong(chat.getFrom());
+
+        // 发送人是否存在聊天框
+        OtcMsgUserListEntity fromList = otcMsgUserListDao.selectChatListByToAndFrom(Long.parseLong(chat.getTo()), Long.parseLong(chat.getFrom()));
+        if (fromList == null) {
+            OtcMsgUserListEntity from = new OtcMsgUserListEntity();
+            from.setMemberId(Long.parseLong(chat.getFrom()));
+            from.setTargetId(Long.parseLong(chat.getTo()));
+            from.setIsRead(OtcMsgUserListEntity.ISREAD_TWO);
+            from.setLastMsgTime(new Date());
+            otcMsgUserListDao.insert(from);
+        }
+
+        // 收件人是否存在聊天框
+        OtcMsgUserListEntity toList = otcMsgUserListDao.selectChatListByToAndFrom(Long.parseLong(chat.getFrom()), Long.parseLong(chat.getTo()));
+        if (toList == null) {
+            OtcMsgUserListEntity from = new OtcMsgUserListEntity();
+            from.setMemberId(Long.parseLong(chat.getTo()));
+            from.setTargetId(Long.parseLong(chat.getFrom()));
+            from.setIsRead(OtcMsgUserListEntity.ISREAD_ONE);
+            from.setLastMsgTime(new Date());
+            otcMsgUserListDao.insert(from);
+        } else {
+            // 收件人正在聊的用户
+            String value = redisUtils.getString(AppContants.MSG_CHATTING + chat.getTo());
+            if (StrUtil.isNotBlank(value) && value.equals(chat.getFrom())) {
+                toList.setLastMsgTime(new Date());
+                otcMsgUserListDao.updateById(toList);
+            } else {
+                toList.setIsRead(OtcMsgUserListEntity.ISREAD_TWO);
+                toList.setLastMsgTime(new Date());
+                otcMsgUserListDao.updateById(toList);
+            }
+        }
+
+
+        OtcMsgHistoryEntity toHistory = new OtcMsgHistoryEntity();
+        toHistory.setMemberId(toId);
+        toHistory.setFromMemberId(fromId);
+        toHistory.setTargetId(toId);
+        toHistory.setIsSelf(OtcMsgHistoryEntity.ISSELF_TWO);
+        toHistory.setMsgType(chat.getMsgType());
+        toHistory.setMsg(chat.getContent());
+
+        OtcMsgHistoryEntity fromHistory = new OtcMsgHistoryEntity();
+        BeanUtil.copyProperties(toHistory, fromHistory);
+        fromHistory.setIsSelf(OtcMsgHistoryEntity.ISSELF_ONE);
+        fromHistory.setMemberId(fromId);
+
+        otcMsgHistoryDao.insert(fromHistory);
+        otcMsgHistoryDao.insert(toHistory);
+    }
 }

--
Gitblit v1.9.1