liusuyi
2024-08-01 60211f59d2c85053533ed151adb2bdc5348dd342
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.ruoyi.call.service.impl;
 
import com.ruoyi.call.domain.ArdCallGroup;
import com.ruoyi.call.domain.ArdCallSession;
import com.ruoyi.call.domain.ArdCallSessionUser;
import com.ruoyi.call.mapper.ArdCallSessionMapper;
import com.ruoyi.call.service.IArdCallGroupService;
import com.ruoyi.call.service.IArdCallGroupUserService;
import com.ruoyi.call.service.IArdCallSessionService;
import com.ruoyi.call.service.IArdCallSessionUserService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
 
/**
 * 视频会话Service业务层处理
 *
 * @author ard
 * @date 2024-07-03
 */
@Service
public class ArdCallSessionServiceImpl implements IArdCallSessionService {
    @Resource
    private ArdCallSessionMapper ardCallSessionMapper;
    @Resource
    private IArdCallSessionUserService ardCallSessionUserService;
    @Resource
    private IArdCallGroupService ardCallGroupService;
 
    /**
     * 查询视频会话
     *
     * @param id 视频会话主键
     * @return 视频会话
     */
    @Override
    public ArdCallSession selectArdCallSessionById(String id) {
        return ardCallSessionMapper.selectArdCallSessionById(id);
    }
 
    /**
     * 查询视频会话列表
     *
     * @param ardCallSession 视频会话
     * @return 视频会话
     */
    @Override
    public List<ArdCallSession> selectArdCallSessionList(ArdCallSession ardCallSession) {
        List<ArdCallSession> ardCallSessions = ardCallSessionMapper.selectArdCallSessionList(ardCallSession);
        return ardCallSessions;
    }
 
    /**
     * 新增视频会话
     *
     * @param ardCallSession 视频会话
     * @return 结果
     */
    @Override
    public String insertArdCallSession(ArdCallSession ardCallSession) {
        ardCallSession.setId(IdUtils.simpleUUID());
        ardCallSession.setCreateTime(DateUtils.getNowDate());
        ardCallSessionMapper.insertArdCallSession(ardCallSession);
        return ardCallSession.getId();
    }
 
    /**
     * 修改视频会话
     *
     * @param ardCallSession 视频会话
     * @return 结果
     */
    @Override
    public int updateArdCallSession(ArdCallSession ardCallSession) {
        ardCallSession.setUpdateBy(SecurityUtils.getUsername());
        ardCallSession.setUpdateTime(DateUtils.getNowDate());
        return ardCallSessionMapper.updateArdCallSession(ardCallSession);
    }
 
    /**
     * 批量删除视频会话
     *
     * @param ids 需要删除的视频会话主键
     * @return 结果
     */
    @Transactional
    @Override
    public int
    deleteArdCallSessionByIds(String[] ids) {
        Arrays.stream(ids).forEach(id -> {
            ardCallSessionUserService.deleteArdCallSessionUserBySessionId(id);
        });
        return ardCallSessionMapper.deleteArdCallSessionByIds(ids);
    }
 
    /**
     * 删除视频会话信息
     *
     * @param id 视频会话主键
     * @return 结果
     */
    @Override
    public int deleteArdCallSessionById(String id) {
        return ardCallSessionMapper.deleteArdCallSessionById(id);
    }
 
 
    /**
     * 获取会话
     *
     * @param type     会话类型
     * @param userId   会话发起方
     * @param targetId 会话接收方
     * @return 结果
     */
    @Override
    public String getSession(String type, String userId, String targetId) {
        String sessionId = "";
        if (type.equals("0")) {
            //单聊
            sessionId = ardCallSessionUserService.getSessionId(type, userId, targetId);
            if (StringUtils.isEmpty(sessionId)) {
                //创建单聊双方的会话
                ArdCallSession ardCallSession = new ArdCallSession();
                ardCallSession.setType(type);
                sessionId = insertArdCallSession(ardCallSession);
                ArdCallSessionUser ardCallSessionUser = new ArdCallSessionUser();
                ardCallSessionUser.setSessionId(sessionId);
                ardCallSessionUser.setUserId(userId);
                ardCallSessionUser.setTargetId(targetId);
                ardCallSessionUser.setType(type);
                ardCallSessionUserService.insertArdCallSessionUser(ardCallSessionUser);//创建主叫关联
                ardCallSessionUser.setUserId(targetId);
                ardCallSessionUser.setTargetId(userId);
                ardCallSessionUserService.insertArdCallSessionUser(ardCallSessionUser);//创建被叫关联
            }
        } else {
            //群聊
            sessionId = ardCallSessionUserService.getGroupSessionId(type, targetId);
            if (StringUtils.isEmpty(sessionId)) {
                //若不存在群聊session直接则分别创建所有群用户的session
                ArdCallSession ardCallSession = new ArdCallSession();
                ardCallSession.setType(type);
                sessionId = insertArdCallSession(ardCallSession);
 
                //获取群用户列表
                ArdCallGroup ardCallGroup = ardCallGroupService.selectArdCallGroupById(targetId);
                if(ardCallGroup!=null)
                {
                    String finalSessionId = sessionId;
                    ardCallGroup.getArdCallGroupUsers().stream().forEach(groupUser -> {
                        ArdCallSessionUser ardCallSessionUser = new ArdCallSessionUser();
                        ardCallSessionUser.setSessionId(finalSessionId);
                        ardCallSessionUser.setType(type);
                        ardCallSessionUser.setUserId(groupUser.getUserId());
                        ardCallSessionUser.setTargetId(targetId);
                        ardCallSessionUserService.insertArdCallSessionUser(ardCallSessionUser);
                    });
                }
            } else {
                //获取自己的session
                 sessionId = ardCallSessionUserService.getSessionId(type, userId, targetId);
            }
        }
        return sessionId;
    }
}