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.member.dao.MemberSettingDao; 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 = "websocket-job", havingValue = "true") public class ChatConsumer { @Autowired private OtcMsgUserListDao otcMsgUserListDao; @Autowired private OtcMsgHistoryDao otcMsgHistoryDao; @Autowired private RedisUtils redisUtils; @Autowired private MemberSettingDao memberSettingDao; @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 = chat.getTargetId(); Long fromId = chat.getFromMemberId(); // 发送人是否存在聊天框 OtcMsgUserListEntity fromList = otcMsgUserListDao.selectChatListByToAndFrom(toId, fromId); if (fromList == null) { OtcMsgUserListEntity from = new OtcMsgUserListEntity(); from.setMemberId(fromId); from.setTargetId(toId); from.setIsRead(OtcMsgUserListEntity.ISREAD_TWO); from.setLastMsgTime(new Date()); otcMsgUserListDao.insert(from); } else { fromList.setLastMsgTime(new Date()); otcMsgUserListDao.updateById(fromList); } // 收件人是否存在聊天框 OtcMsgUserListEntity toList = otcMsgUserListDao.selectChatListByToAndFrom(fromId, toId); if (toList == null) { OtcMsgUserListEntity from = new OtcMsgUserListEntity(); from.setMemberId(toId); from.setTargetId(fromId); from.setIsRead(OtcMsgUserListEntity.ISREAD_ONE); from.setLastMsgTime(new Date()); otcMsgUserListDao.insert(from); } else { // 收件人正在聊的用户 String value = redisUtils.getString(AppContants.MSG_CHATTING + toId); if (StrUtil.isNotBlank(value) && value.equals(fromId.toString())) { toList.setLastMsgTime(new Date()); otcMsgUserListDao.updateById(toList); } else { toList.setIsRead(OtcMsgUserListEntity.ISREAD_ONE); 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.getMsg()); OtcMsgHistoryEntity fromHistory = new OtcMsgHistoryEntity(); BeanUtil.copyProperties(toHistory, fromHistory); fromHistory.setIsSelf(OtcMsgHistoryEntity.ISSELF_ONE); fromHistory.setMemberId(fromId); memberSettingDao.updateMessageReminderByMemberId(toId); otcMsgHistoryDao.insert(fromHistory); otcMsgHistoryDao.insert(toHistory); } }