liusuyi
2024-08-10 5b017324148ea92d96f9f16ade215463d6c712e5
ard-work/src/main/java/com/ruoyi/call/service/impl/ArdCallHistoryServiceImpl.java
@@ -1,37 +1,23 @@
package com.ruoyi.call.service.impl;
import java.util.HashMap;
import java.util.List;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.call.domain.*;
import com.ruoyi.call.mapper.*;
import com.ruoyi.call.dto.MessageEvent;
import com.ruoyi.call.mapper.ArdCallGroupUserMapper;
import com.ruoyi.call.mapper.ArdCallHistoryMapper;
import com.ruoyi.call.mapper.ArdCallSessionMapper;
import com.ruoyi.call.mapper.ArdCallUnreadMessagesMapper;
import com.ruoyi.call.service.IArdCallGroupService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.call.service.IArdCallHistoryService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.utils.websocket.util.WebSocketUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.call.service.IArdCallHistoryService;
import javax.annotation.Resource;
import javax.websocket.Session;
import static com.ruoyi.utils.websocket.util.WebSocketUtils.ONLINE_USER_SESSIONS;
import java.util.List;
/**
 * 会话历史Service业务层处理
@@ -40,6 +26,7 @@
 * @date 2024-07-03
 */
@Service
@Slf4j(topic = "msgListener")
public class ArdCallHistoryServiceImpl implements IArdCallHistoryService {
    @Resource
    private ArdCallHistoryMapper ardCallHistoryMapper;
@@ -51,6 +38,8 @@
    private ArdCallGroupUserMapper ardCallGroupUserMapper;
    @Resource
    private IArdCallGroupService ardCallGroupService;
    @Autowired
    private ApplicationEventPublisher eventPublisher;
    /**
     * 查询会话历史
@@ -104,7 +93,6 @@
     */
    @Override
    public int insertArdCallHistory(ArdCallHistory ardCallHistory) {
        ardCallHistory.setId(IdUtils.simpleUUID());
        ardCallHistory.setCreateTime(DateUtils.getNowDate());
        ArdCallSession ardCallSession = ardCallSessionMapper.selectArdCallSessionById(ardCallHistory.getSessionId());
@@ -125,17 +113,8 @@
                ardCallUnreadMessagesMapper.updateArdCallUnreadMessages(ardCallUnreadMessages);
            }
            //websocket发送给targetId
            // 构建正则表达式模式
            String regex = "^" + Pattern.quote(ardCallHistory.getTargetId()) + "_\\d+$";
            Pattern pattern = Pattern.compile(regex);
            Map<String, Object> messageMap = new HashMap<>();
            messageMap.put("type", "message");
            messageMap.put("message", JSON.toJSONString(ardCallHistory));
            ONLINE_USER_SESSIONS.entrySet().stream().filter(entry -> {
                Matcher matcher = pattern.matcher(entry.getKey());
                return matcher.matches();
            }).map(Map.Entry::getValue).forEach(session -> WebSocketUtils.sendMessage(session, messageMap));
            MessageEvent messageEvent = new MessageEvent(ardCallHistory.getTargetId(), ardCallHistory);
            eventPublisher.publishEvent(messageEvent);
        } else {
            //更新群聊用户未读消息计数
            ArdCallGroupUser ardCallGroupUser = new ArdCallGroupUser();
@@ -159,20 +138,10 @@
                        ardCallUnreadMessagesMapper.updateArdCallUnreadMessages(ardCallUnreadMessages);
                    }
                    //websocket发送给targetId
                    // 构建正则表达式模式
                    String regex = "^" + Pattern.quote(groupUser.getUserId()) + "_\\d+$";
                    Pattern pattern = Pattern.compile(regex);
                    Map<String, Object> messageMap = new HashMap<>();
                    messageMap.put("type", "message");
                    messageMap.put("message", JSON.toJSONString(ardCallHistory));
                    ONLINE_USER_SESSIONS.entrySet().stream().filter(entry -> {
                        Matcher matcher = pattern.matcher(entry.getKey());
                        return matcher.matches();
                    }).map(Map.Entry::getValue).forEach(session -> WebSocketUtils.sendMessage(session, messageMap));
                    MessageEvent messageEvent = new MessageEvent(groupUser.getUserId(), ardCallHistory);
                    eventPublisher.publishEvent(messageEvent);
                }
            });
        }
        return ardCallHistoryMapper.insertArdCallHistory(ardCallHistory);
    }