KKSU
2024-09-30 36be00e0f3cbe0d559c646fd2977e6e3a74aa6f9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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 = "netty-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);
    }
}