2026-06-30 24681c81c09022f584a57006f2534b5f74723414
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
package cn.iocoder.yudao.module.im.service.conversation;
 
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.im.dal.dataobject.conversation.ImConversationReadDO;
import cn.iocoder.yudao.module.im.dal.mysql.conversation.ImConversationReadMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
 
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
 
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
 
/**
 * IM 会话读位置 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Slf4j
public class ImConversationReadServiceImpl implements ImConversationReadService {
 
    @Resource
    private ImConversationReadMapper conversationReadMapper;
 
    @Override
    public boolean updateConversationReadPosition(Long userId, Integer conversationType, Long conversationId, Long readMessageId) {
        LocalDateTime now = LocalDateTime.now();
        // 1. 不存在则插入;并发下唯一键冲突,降级为回查 + CAS 更新
        ImConversationReadDO existing = conversationReadMapper.selectByUserIdAndConversation(
                userId, conversationType, conversationId);
        if (existing == null) {
            try {
                conversationReadMapper.insert(new ImConversationReadDO().setUserId(userId)
                        .setConversationType(conversationType).setTargetId(conversationId)
                        .setMessageId(readMessageId).setReadTime(now));
                return true;
            } catch (DuplicateKeyException e) {
                log.warn("[updateConversationReadPosition][userId({}) type({}) conversationId({}) 并发插入冲突,回查更新]",
                        userId, conversationType, conversationId);
                existing = conversationReadMapper.selectByUserIdAndConversation(userId, conversationType, conversationId);
            }
        }
        if (existing == null) {
            return false;
        }
 
        // 2. CAS 单调更新:mapper 内 WHERE message_id < ? 保证乱序 / 并发不回退;影响行数 > 0 即读位置前进
        return conversationReadMapper.updateReadMessageIdToLarger(existing.getId(), readMessageId, now) > 0;
    }
 
    @Override
    public Long getConversationReadMessageId(Long userId, Integer conversationType, Long conversationId) {
        ImConversationReadDO read = conversationReadMapper.selectByUserIdAndConversation(
                userId, conversationType, conversationId);
        return read != null ? read.getMessageId() : null;
    }
 
    @Override
    public Map<Long, Long> getUserReadMessageIdMap(Integer conversationType, Long conversationId) {
        return convertMap(conversationReadMapper.selectListByConversation(conversationType, conversationId),
                ImConversationReadDO::getUserId, ImConversationReadDO::getMessageId);
    }
 
    @Override
    public Map<Long, Long> getConversationReadMessageIdMap(Long userId, Integer conversationType,
                                                           Collection<Long> conversationIds) {
        if (CollUtil.isEmpty(conversationIds)) {
            return Collections.emptyMap();
        }
        List<ImConversationReadDO> list =  conversationReadMapper.selectListByUserIdAndConversations(
                userId, conversationType, conversationIds);
        return convertMap(list, ImConversationReadDO::getTargetId, ImConversationReadDO::getMessageId);
    }
 
    @Override
    public List<ImConversationReadDO> pullConversationReadList(Long userId, Long lastUpdateTime, Long lastId, Integer limit) {
        return conversationReadMapper.selectPullListByUserId(userId, lastUpdateTime, lastId, limit);
    }
 
}