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.core.domain.entity.SysUser; 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; @Resource private IArdCallGroupUserService ardCallGroupUserService; /** * 查询视频会话 * * @param id 视频会话主键 * @return 视频会话 */ @Override public ArdCallSession selectArdCallSessionById(String id) { return ardCallSessionMapper.selectArdCallSessionById(id); } /** * 查询视频会话列表 * * @param ardCallSession 视频会话 * @return 视频会话 */ @Override public List selectArdCallSessionList(ArdCallSession ardCallSession) { List 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.isNull(sessionId)) { //若不存在群聊session直接则分别创建所有群用户的session ArdCallSession ardCallSession = new ArdCallSession(); ardCallSession.setType(type); sessionId = insertArdCallSession(ardCallSession); //获取群用户列表 ArdCallGroup ardCallGroup = ardCallGroupService.selectArdCallGroupById(targetId); if (ardCallGroup != null) { List userList = ardCallGroupUserService.InGroupUsers(ardCallGroup.getId()); if (userList.size() > 0) { ardCallGroup.setSysUserList(userList); String finalSessionId = sessionId; ardCallGroup.getSysUserList().stream().forEach(groupUser -> { ArdCallSessionUser ardCallSessionUser = new ArdCallSessionUser(); ardCallSessionUser.setSessionId(finalSessionId); ardCallSessionUser.setType(type); ardCallSessionUser.setUserId(groupUser.getUserId()); ardCallSessionUser.setTargetId(targetId); ardCallSessionUserService.insertArdCallSessionUser(ardCallSessionUser); }); } } } } return sessionId; } }