|
@@ -9,19 +9,21 @@ import com.ym.mec.auth.api.entity.SysUser;
|
|
|
import com.ym.mec.biz.dal.dao.ImLiveBroadcastRoomDao;
|
|
|
import com.ym.mec.biz.dal.dto.ImLiveBroadcastRoomDto;
|
|
|
import com.ym.mec.biz.dal.entity.ImLiveBroadcastRoom;
|
|
|
-import com.ym.mec.biz.dal.enums.ImSendTypeEnum;
|
|
|
import com.ym.mec.biz.dal.enums.MessageTypeEnum;
|
|
|
import com.ym.mec.biz.dal.vo.ImLiveBroadcastRoomVo;
|
|
|
import com.ym.mec.biz.service.ImLiveBroadcastRoomService;
|
|
|
-import com.ym.mec.biz.service.SysConfigService;
|
|
|
import com.ym.mec.biz.service.SysMessageService;
|
|
|
import com.ym.mec.common.exception.BizException;
|
|
|
import com.ym.mec.common.page.PageInfo;
|
|
|
import com.ym.mec.common.page.PageUtil;
|
|
|
+import com.ym.mec.common.page.WrapperUtil;
|
|
|
import com.ym.mec.common.tenant.TenantContextHolder;
|
|
|
import com.ym.mec.im.ImFeignService;
|
|
|
import com.ym.mec.util.date.DateUtil;
|
|
|
import com.ym.mec.util.http.HttpUtil;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.redisson.api.RBucket;
|
|
|
+import org.redisson.api.RList;
|
|
|
import org.redisson.api.RedissonClient;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
@@ -30,6 +32,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
+import java.io.Serializable;
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
@@ -52,12 +55,33 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
|
|
|
@Autowired
|
|
|
private SysMessageService sysMessageService;
|
|
|
|
|
|
+ public static final String USER_ID = "${userId}";
|
|
|
+ public static final String ROOM_UID = "${roomUid}";
|
|
|
+
|
|
|
+ //直播间用户列表
|
|
|
+ public static final String LIVE_ROOM_USER_LIST = "IM:LIVE_ROOM_USER_LIST:" + ROOM_UID;
|
|
|
+ //用户对应的直播间Uid
|
|
|
+ public static final String LIVE_USER_ROOM = "IM:LIVE_ROOM_USER:" + USER_ID;
|
|
|
+ //用户进入房间时间
|
|
|
+ public static final String LIVE_ROOM_USER_JOIN_TIME = "IM:LIVE_ROOM_USER_JOIN_TIME:" + USER_ID;
|
|
|
+ //用户退出房间时间
|
|
|
+ public static final String LIVE_ROOM_USER_QUIT_TIME = "IM:LIVE_ROOM_USER_QUIT_TIME:" + USER_ID;
|
|
|
+ //房间信息
|
|
|
+ public static final String LIVE_ROOM_INFO = "IM:LIVE_ROOM_INFO:" + ROOM_UID;
|
|
|
+ //房间当前人数
|
|
|
+ public static final String LIVE_ROOM_LOOK = "IM:LIVE_ROOM_LOOK:" + ROOM_UID;
|
|
|
+ //房间累计人数
|
|
|
+ public static final String LIVE_ROOM_TOTAL_LOOK = "IM:LIVE_ROOM_TOTAL_LOOK:" + ROOM_UID;
|
|
|
+ //房间点赞数
|
|
|
+ public static final String LIVE_ROOM_LIKE = "IM:LIVE_ROOM_LIKE:" + ROOM_UID;
|
|
|
+
|
|
|
/**
|
|
|
* 进入直播间检查数据
|
|
|
*
|
|
|
* @param roomUid 房间uid
|
|
|
* @param userId 用户id
|
|
|
*/
|
|
|
+ @Override
|
|
|
public ImLiveBroadcastRoomVo queryRoom(String roomUid, Integer userId) {
|
|
|
SysUser sysUser;
|
|
|
if (Objects.isNull(userId)) {
|
|
@@ -73,7 +97,7 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
|
|
|
.orElseThrow(() -> new BizException("您不是该直播机构学员,不可观看!"));
|
|
|
optional.filter(r -> r.getRoomState() != 1).orElseThrow(() -> new BizException("直播间不存在"));
|
|
|
ImLiveBroadcastRoomVo room = optional.get();
|
|
|
- if (room.getLiveState() == 0 || room.getLiveStartTime().getTime() < new Date().getTime()) {
|
|
|
+ if (room.getLiveState() == 0) {
|
|
|
throw new BizException("直播未开始,直播开启的时间是 "
|
|
|
+ DateUtil.format(room.getLiveStartTime(), DateUtil.EXPANDED_DATE_TIME_FORMAT));
|
|
|
}
|
|
@@ -88,7 +112,8 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
|
|
|
*
|
|
|
* @param roomUid 直播间uid
|
|
|
*/
|
|
|
- private ImLiveBroadcastRoomVo queryRoomInfo(String roomUid) {
|
|
|
+ @Override
|
|
|
+ public ImLiveBroadcastRoomVo queryRoomInfo(String roomUid) {
|
|
|
List<ImLiveBroadcastRoomVo> list = baseMapper.queryPage(new HashMap<String, Object>() {{
|
|
|
put("roomUid", roomUid);
|
|
|
}});
|
|
@@ -146,6 +171,39 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 关闭房间
|
|
|
+ *
|
|
|
+ * @param id 直播房间表id
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void roomDestroy(Integer id) {
|
|
|
+ ImLiveBroadcastRoom room = this.getById(id);
|
|
|
+ if (Objects.isNull(room)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (room.getLiveState() == 0) {
|
|
|
+ throw new BizException("直播未开始");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ imFeignService.destroyLiveRoom(room.getRoomUid());
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.getMessage(), e.getCause());
|
|
|
+ throw new BizException(e.getMessage());
|
|
|
+ }
|
|
|
+ //todo 向聊天室发自定义消息提出所有学生
|
|
|
+
|
|
|
+ //todo 将房间redis的数据写入数据库后在删除缓存
|
|
|
+
|
|
|
+ //将房间状态改为已销毁
|
|
|
+ Date date = new Date();
|
|
|
+ room.setRoomState(2);
|
|
|
+ room.setUpdatedBy(getSysUser().getId());
|
|
|
+ room.setUpdatedTime(date);
|
|
|
+ room.setLiveEndTime(date);
|
|
|
+ this.updateById(room);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 删除直播间
|
|
|
*
|
|
|
* @param id 直播间id
|
|
@@ -153,6 +211,9 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
|
|
|
@Override
|
|
|
public void delete(Integer id) {
|
|
|
ImLiveBroadcastRoom obj = new ImLiveBroadcastRoom();
|
|
|
+ if (obj.getLiveState() == 1) {
|
|
|
+ throw new BizException("直播已经开始,无法删除");
|
|
|
+ }
|
|
|
obj.setId(id);
|
|
|
obj.setRoomState(1);
|
|
|
this.updateById(obj);
|
|
@@ -165,16 +226,118 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
|
|
|
* @param likeNum 点赞数
|
|
|
*/
|
|
|
public void syncLike(String roomUid, Integer likeNum) {
|
|
|
+ redissonClient.getBucket(LIVE_ROOM_LIKE.replace(ROOM_UID, roomUid)).set(likeNum);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 进入房间-修改用户对应的房间号信息
|
|
|
+ *
|
|
|
+ * @param roomUid 房间uid
|
|
|
+ * @param userId 用户id
|
|
|
+ */
|
|
|
+ public void joinRoom(String roomUid, Integer userId) {
|
|
|
+ //用户对应的直播间信息
|
|
|
+ RBucket<String> roomUserCache = redissonClient.getBucket(LIVE_USER_ROOM.replace(USER_ID, userId.toString()));
|
|
|
+ //获取原有房间信息
|
|
|
+ if (roomUserCache.isExists()) {
|
|
|
+ //原有的房间uid
|
|
|
+ String lodRoomUid = roomUserCache.get();
|
|
|
+ //如果房间相同直接返回
|
|
|
+ if (roomUid.equals(lodRoomUid)) {
|
|
|
+ return;
|
|
|
+ } else {
|
|
|
+ //如果不同就把原房间的当前人员数量-1
|
|
|
+ redissonClient.getAtomicLong(LIVE_ROOM_LOOK.replace(ROOM_UID, lodRoomUid)).decrementAndGet();
|
|
|
+ //将用户数据从老直播间用户列表删除
|
|
|
+ RList<UserInfo> list = redissonClient.getList(LIVE_ROOM_USER_LIST.replace(ROOM_UID, lodRoomUid));
|
|
|
+ UserInfo userInfo = getUserInfo(userId);
|
|
|
+ list.remove(userInfo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //写入用户房间uid
|
|
|
+ roomUserCache.set(roomUid);
|
|
|
+ //写入用户进入房间时间缓存
|
|
|
+ redissonClient.getBucket(LIVE_ROOM_USER_JOIN_TIME.replace(USER_ID, userId.toString())).set(new Date());
|
|
|
+ //添加房间中用户数量
|
|
|
+ redissonClient.getAtomicLong(LIVE_ROOM_LOOK.replace(ROOM_UID, roomUid)).incrementAndGet();
|
|
|
+ //添加房间中累计总用户数量
|
|
|
+ redissonClient.getAtomicLong(LIVE_ROOM_TOTAL_LOOK.replace(ROOM_UID, roomUid)).incrementAndGet();
|
|
|
+ //将用户数据写入直播间
|
|
|
+ RList<UserInfo> list = redissonClient.getList(LIVE_ROOM_USER_LIST.replace(ROOM_UID, roomUid));
|
|
|
+ UserInfo userInfo = getUserInfo(userId);
|
|
|
+ list.add(userInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ private UserInfo getUserInfo(Integer userId) {
|
|
|
+ UserInfo userInfo = new UserInfo();
|
|
|
+ userInfo.setUserId(userId);
|
|
|
+ SysUser sysUser = sysUserFeignService.queryUserById(userId);
|
|
|
+ String name = userId.toString();
|
|
|
+ if (Objects.nonNull(sysUser)) {
|
|
|
+ if (StringUtils.isNotBlank(sysUser.getRealName())) {
|
|
|
+ name = sysUser.getRealName();
|
|
|
+ } else {
|
|
|
+ name = sysUser.getPhone();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ userInfo.setUserName(name);
|
|
|
+ return userInfo;
|
|
|
+ }
|
|
|
+
|
|
|
+ static class UserInfo implements Serializable {
|
|
|
+ private Integer userId;
|
|
|
+ private String userName;
|
|
|
+
|
|
|
+ public Integer getUserId() {
|
|
|
+ return userId;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setUserId(Integer userId) {
|
|
|
+ this.userId = userId;
|
|
|
+ }
|
|
|
|
|
|
+ public String getUserName() {
|
|
|
+ return userName;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setUserName(String userName) {
|
|
|
+ this.userName = userName;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 定时任务,每分钟执行
|
|
|
* 提前30分钟主动去融云注册并创建房间
|
|
|
*/
|
|
|
- public void createRoom() {
|
|
|
- List<ImLiveBroadcastRoomVo> imLiveBroadcastRoomVos = baseMapper.queryPage(new HashMap<>());
|
|
|
- imFeignService.createLiveRoom("", "");
|
|
|
+ public void createLiveRoom() {
|
|
|
+ Date now = new Date();
|
|
|
+ Date startTime = DateUtil.addMinutes(now, 29);
|
|
|
+ Date endTime = DateUtil.addMinutes(now, 31);
|
|
|
+ List<ImLiveBroadcastRoom> list = this.list(new WrapperUtil<ImLiveBroadcastRoom>()
|
|
|
+ .hasEq("live_state_", 0)
|
|
|
+ .hasEq("room_state_", 0)
|
|
|
+ .hasGe("live_start_time_", startTime)
|
|
|
+ .hasLe("live_start_time_", endTime)
|
|
|
+ .queryWrapper());
|
|
|
+ if (CollectionUtils.isEmpty(list)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ list.forEach(this::createLiveRoom);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 去融云创建房间
|
|
|
+ */
|
|
|
+ private void createLiveRoom(ImLiveBroadcastRoom room) {
|
|
|
+ try {
|
|
|
+ //去融云创建房间
|
|
|
+ Object liveRoom = imFeignService.createLiveRoom(room.getRoomUid(), room.getRoomTitle());
|
|
|
+ } catch (Exception e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ //初始化房间数据 点赞 人数 等 为0
|
|
|
+ //修改房间状态
|
|
|
+
|
|
|
}
|
|
|
|
|
|
private SysUser getSysUser(Integer userId) {
|
|
@@ -193,19 +356,27 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
|
|
|
* 分享直播间
|
|
|
*/
|
|
|
public void test() {
|
|
|
-
|
|
|
+ //将用户数据写入直播间
|
|
|
+ RList<UserInfo> list = redissonClient.getList(LIVE_ROOM_USER_LIST.replace(ROOM_UID, "roomUid"));
|
|
|
+ UserInfo userInfo = new UserInfo();
|
|
|
+ userInfo.setUserId(1);
|
|
|
+ userInfo.setUserName("1");
|
|
|
+ boolean contains = list.contains(userInfo);
|
|
|
+ if (!contains) {
|
|
|
+ list.add(userInfo);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void shareGroup(String roomUid,String groupIds) {
|
|
|
+ public void shareGroup(String roomUid, String groupIds) {
|
|
|
ImLiveBroadcastRoomVo imLiveBroadcastRoomVo = queryRoomInfo(roomUid);
|
|
|
if (imLiveBroadcastRoomVo == null) {
|
|
|
throw new BizException("直播间信息异常!");
|
|
|
}
|
|
|
SysUser sysUser = sysUserFeignService.queryUserInfo();
|
|
|
- sysMessageService.batchSendImMessage(MessageTypeEnum.IM_SHARE_LIVE_URL,sysUser.getId().toString(),null,groupIds.split(","),null,
|
|
|
- imLiveBroadcastRoomVo.getTenantName(),imLiveBroadcastRoomVo.getRoomTitle(),imLiveBroadcastRoomVo.getSpeakerName(),
|
|
|
- DateUtil.format(imLiveBroadcastRoomVo.getLiveStartTime(),DateUtil.CHINESE_DATA_FORMAT_1),
|
|
|
+ sysMessageService.batchSendImMessage(MessageTypeEnum.IM_SHARE_LIVE_URL, sysUser.getId().toString(), null, groupIds.split(","), null,
|
|
|
+ imLiveBroadcastRoomVo.getTenantName(), imLiveBroadcastRoomVo.getRoomTitle(), imLiveBroadcastRoomVo.getSpeakerName(),
|
|
|
+ DateUtil.format(imLiveBroadcastRoomVo.getLiveStartTime(), DateUtil.CHINESE_DATA_FORMAT_1),
|
|
|
imLiveBroadcastRoomVo.getLiveRemark(), HttpUtil.getSortUrl("https://test.dayaedu.com/"));
|
|
|
}
|
|
|
}
|