|  | @@ -1,6 +1,8 @@
 | 
	
		
			
				|  |  |  package com.ym.mec.biz.service.impl;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +import com.alibaba.fastjson.JSON;
 | 
	
		
			
				|  |  |  import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 | 
	
		
			
				|  |  |  import com.baomidou.mybatisplus.core.metadata.IPage;
 | 
	
		
			
				|  |  |  import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 | 
	
		
			
				|  |  |  import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 | 
	
	
		
			
				|  | @@ -9,9 +11,13 @@ 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.entity.ImLiveBroadcastRoomData;
 | 
	
		
			
				|  |  | +import com.ym.mec.biz.dal.entity.ImLiveBroadcastRoomMember;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.enums.MessageTypeEnum;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.vo.ImLiveBroadcastRoomVo;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.vo.RoomUserInfoVo;
 | 
	
		
			
				|  |  | +import com.ym.mec.biz.service.ImLiveBroadcastRoomDataService;
 | 
	
		
			
				|  |  | +import com.ym.mec.biz.service.ImLiveBroadcastRoomMemberService;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.service.ImLiveBroadcastRoomService;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.service.SysMessageService;
 | 
	
		
			
				|  |  |  import com.ym.mec.common.entity.ImRoomMessage;
 | 
	
	
		
			
				|  | @@ -36,6 +42,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.*;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  /**
 | 
	
	
		
			
				|  | @@ -57,6 +64,10 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
 | 
	
		
			
				|  |  |      private RedissonClient redissonClient;
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  |      private SysMessageService sysMessageService;
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private ImLiveBroadcastRoomMemberService liveBroadcastRoomMemberService;
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private ImLiveBroadcastRoomDataService liveBroadcastRoomDataService;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      public static final String USER_ID = "${userId}";
 | 
	
		
			
				|  |  |      public static final String ROOM_UID = "${roomUid}";
 | 
	
	
		
			
				|  | @@ -65,6 +76,8 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
 | 
	
		
			
				|  |  |      public static final String LIVE_ROOM_USER_LIST = "IM:LIVE_ROOM_USER_LIST:" + ROOM_UID;
 | 
	
		
			
				|  |  |      //直播间累计用户信息-指只要进入到该房间的用户都要记录
 | 
	
		
			
				|  |  |      public static final String LIVE_ROOM_TOTAL_USER_LIST = "IM:LIVE_ROOM_TOTAL_USER_LIST:" + ROOM_UID;
 | 
	
		
			
				|  |  | +    //主讲人信息
 | 
	
		
			
				|  |  | +    public static final String LIVE_SPEAKER_INFO = "IM:LIVE_SPEAKER_INFO:" + USER_ID;
 | 
	
		
			
				|  |  |      //用户对应的直播间Uid
 | 
	
		
			
				|  |  |      public static final String LIVE_USER_ROOM = "IM:LIVE_ROOM_USER:" + USER_ID;
 | 
	
		
			
				|  |  |      //房间点赞数
 | 
	
	
		
			
				|  | @@ -163,9 +176,12 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
 | 
	
		
			
				|  |  |      public void update(ImLiveBroadcastRoomDto dto) {
 | 
	
		
			
				|  |  |          ImLiveBroadcastRoom obj = this.getById(dto.getId());
 | 
	
		
			
				|  |  |          if (obj.getLiveState() == 1) {
 | 
	
		
			
				|  |  | -            throw new BizException("直播已经开始,无法修改");
 | 
	
		
			
				|  |  | +            obj.setRoomTitle(dto.getRoomTitle());
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  | +            BeanUtils.copyProperties(dto, obj);
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        BeanUtils.copyProperties(dto, obj);
 | 
	
		
			
				|  |  | +        obj.setUpdatedBy(getSysUser().getId());
 | 
	
		
			
				|  |  | +        obj.setUpdatedTime(new Date());
 | 
	
		
			
				|  |  |          log.info("update room  >>>  :{}", JSONObject.toJSONString(obj));
 | 
	
		
			
				|  |  |          this.updateById(obj);
 | 
	
		
			
				|  |  |      }
 | 
	
	
		
			
				|  | @@ -183,6 +199,8 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |          obj.setId(id);
 | 
	
		
			
				|  |  |          obj.setRoomState(1);
 | 
	
		
			
				|  |  | +        obj.setUpdatedBy(getSysUser().getId());
 | 
	
		
			
				|  |  | +        obj.setUpdatedTime(new Date());
 | 
	
		
			
				|  |  |          this.updateById(obj);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -206,12 +224,13 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
 | 
	
		
			
				|  |  |              log.error(e.getMessage(), e.getCause());
 | 
	
		
			
				|  |  |              throw new BizException(e.getMessage());
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -        //todo 将房间redis的数据写入数据库后在删除缓存
 | 
	
		
			
				|  |  | +        //获取所有直播间缓存数据并写入数据库
 | 
	
		
			
				|  |  | +        insertLiveAllData(room.getRoomUid(), room.getSpeakerId());
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          //将房间状态改为已销毁
 | 
	
		
			
				|  |  |          Date date = new Date();
 | 
	
		
			
				|  |  |          room.setRoomState(2);
 | 
	
		
			
				|  |  | +        room.setLiveState(2);
 | 
	
		
			
				|  |  |          room.setUpdatedBy(getSysUser().getId());
 | 
	
		
			
				|  |  |          room.setUpdatedTime(date);
 | 
	
		
			
				|  |  |          room.setLiveEndTime(date);
 | 
	
	
		
			
				|  | @@ -225,12 +244,46 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
 | 
	
		
			
				|  |  |          imFeignService.publishRoomMsg(message);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    //获取该直播间所有数据
 | 
	
		
			
				|  |  | +    private void insertLiveAllData(String roomUid, Integer userId) {
 | 
	
		
			
				|  |  | +        //获取直播间所有人数据写入 im_live_broadcast_room_member
 | 
	
		
			
				|  |  | +        RMap<Integer, RoomUserInfoVo> roomTotalUser = redissonClient.getMap(LIVE_ROOM_TOTAL_USER_LIST.replace(ROOM_UID, roomUid));
 | 
	
		
			
				|  |  | +        Integer tenantId = roomTotalUser.get(0).getTenantId();
 | 
	
		
			
				|  |  | +        List<ImLiveBroadcastRoomMember> memberList = new ArrayList<>();
 | 
	
		
			
				|  |  | +        roomTotalUser.forEach((k, v) -> {
 | 
	
		
			
				|  |  | +            ImLiveBroadcastRoomMember member = new ImLiveBroadcastRoomMember();
 | 
	
		
			
				|  |  | +            member.setTenantId(tenantId);
 | 
	
		
			
				|  |  | +            member.setRoomUid(roomUid);
 | 
	
		
			
				|  |  | +            member.setUserId(userId);
 | 
	
		
			
				|  |  | +            member.setJoinTime(v.getFirstJoinTime());
 | 
	
		
			
				|  |  | +            member.setTotalTime(v.getTotalViewTime());
 | 
	
		
			
				|  |  | +            memberList.add(member);
 | 
	
		
			
				|  |  | +        });
 | 
	
		
			
				|  |  | +        liveBroadcastRoomMemberService.getDao().insertBatch(memberList);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        //获取直播间数据
 | 
	
		
			
				|  |  | +        ImLiveBroadcastRoomVo roomVo = queryRoomInfo(roomUid);
 | 
	
		
			
				|  |  | +        //获取直播间主讲人信息
 | 
	
		
			
				|  |  | +        RBucket<RoomSpeakerInfo> speakerCache = redissonClient.getBucket(LIVE_SPEAKER_INFO.replace(USER_ID, roomVo.getSpeakerId().toString()));
 | 
	
		
			
				|  |  | +        RoomSpeakerInfo speakerInfo = speakerCache.get();
 | 
	
		
			
				|  |  | +        //获取直播间数据写入im_live_broadcast_room_data
 | 
	
		
			
				|  |  | +        ImLiveBroadcastRoomData liveData = new ImLiveBroadcastRoomData();
 | 
	
		
			
				|  |  | +        liveData.setTenantId(tenantId);
 | 
	
		
			
				|  |  | +        liveData.setRoomUid(roomUid);
 | 
	
		
			
				|  |  | +        liveData.setLikeNum(roomVo.getLikeNum());
 | 
	
		
			
				|  |  | +        liveData.setTotalUserNum(roomVo.getTotalLookNum());
 | 
	
		
			
				|  |  | +        liveData.setUpdatedTime(new Date());
 | 
	
		
			
				|  |  | +        liveData.setLiveTime(speakerInfo.getTotalLiveTime());
 | 
	
		
			
				|  |  | +        liveBroadcastRoomDataService.save(liveData);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  |       * 同步点赞数量
 | 
	
		
			
				|  |  |       *
 | 
	
		
			
				|  |  |       * @param roomUid 房间uid
 | 
	
		
			
				|  |  |       * @param likeNum 点赞数
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  |      public void syncLike(String roomUid, Integer likeNum) {
 | 
	
		
			
				|  |  |          redissonClient.getBucket(LIVE_ROOM_LIKE.replace(ROOM_UID, roomUid)).set(likeNum);
 | 
	
		
			
				|  |  |      }
 | 
	
	
		
			
				|  | @@ -240,31 +293,38 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
 | 
	
		
			
				|  |  |       *
 | 
	
		
			
				|  |  |       * @param userState 用户状态数据
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  |      public void quitRoom(List<ImUserState> userState) {
 | 
	
		
			
				|  |  |          userState.forEach(user -> {
 | 
	
		
			
				|  |  | +            log.info("quitRoom>>>> {}", JSONObject.toJSONString(user));
 | 
	
		
			
				|  |  |              if (StringUtils.isBlank(user.getStatus()) || user.getStatus().equals("0")) {
 | 
	
		
			
				|  |  |                  return;
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  | +            Integer userId = Integer.valueOf(user.getUserid());
 | 
	
		
			
				|  |  | +            Date now = new Date();
 | 
	
		
			
				|  |  | +            //查询userId,如果是主讲人退出房间,那么不做后面操作
 | 
	
		
			
				|  |  | +            RBucket<RoomSpeakerInfo> speakerCache = redissonClient.getBucket(LIVE_SPEAKER_INFO.replace(USER_ID, user.getUserid()));
 | 
	
		
			
				|  |  | +            if (speakerCache.isExists()) {
 | 
	
		
			
				|  |  | +                RoomSpeakerInfo speakerInfo = speakerCache.get();
 | 
	
		
			
				|  |  | +                speakerInfo.setState(2);//主讲人退出房间后房间为暂停直播状态(超过半小时还未开播则自动清算)
 | 
	
		
			
				|  |  | +                speakerInfo.setExitRoomTime(now);
 | 
	
		
			
				|  |  | +                speakerCache.set(speakerInfo);
 | 
	
		
			
				|  |  | +                //主讲人退出房间关闭录像
 | 
	
		
			
				|  |  | +                closeLive(speakerInfo.getRoomUid(), userId);
 | 
	
		
			
				|  |  | +                log.info("quitRoom>>>> speakerCache {}", JSONObject.toJSONString(speakerInfo));
 | 
	
		
			
				|  |  | +                return;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |              //获取当前用户所在房间的uid
 | 
	
		
			
				|  |  |              RBucket<String> userRoom = redissonClient.getBucket(LIVE_USER_ROOM.replace(USER_ID, user.getUserid()));
 | 
	
		
			
				|  |  |              if (!userRoom.isExists()) {
 | 
	
		
			
				|  |  |                  return;
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |              String roomUid = userRoom.get();
 | 
	
		
			
				|  |  | -            Integer userId = Integer.valueOf(user.getUserid());
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -            //只有离线-发送退出房间消息给主讲人
 | 
	
		
			
				|  |  | -            if (user.getStatus().equals("1")) {
 | 
	
		
			
				|  |  | -                ImRoomMessage message = new ImRoomMessage();
 | 
	
		
			
				|  |  | -                message.setFromUserId(userId.toString());
 | 
	
		
			
				|  |  | -                message.setToChatroomId(roomUid);
 | 
	
		
			
				|  |  | -                message.setObjectName(ImRoomMessage.LOOKER_LOGIN_OUT);
 | 
	
		
			
				|  |  | -                imFeignService.publishRoomMsg(message);
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |              //从房间累计用户信息中查询该用户的信息
 | 
	
		
			
				|  |  |              RMap<Integer, RoomUserInfoVo> roomTotalUser = redissonClient.getMap(LIVE_ROOM_TOTAL_USER_LIST.replace(ROOM_UID, roomUid));
 | 
	
		
			
				|  |  | -            Date now = new Date();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |              RoomUserInfoVo userInfo;
 | 
	
		
			
				|  |  |              //该房间未查询到用户数据则不处理
 | 
	
		
			
				|  |  |              if (!roomTotalUser.containsKey(userId)) {
 | 
	
	
		
			
				|  | @@ -272,8 +332,20 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |              //查询到用户数据
 | 
	
		
			
				|  |  |              userInfo = roomTotalUser.get(userId);
 | 
	
		
			
				|  |  | -            //将用户从该房间当前直播间用户列表移除
 | 
	
		
			
				|  |  | -            redissonClient.getList(LIVE_ROOM_USER_LIST.replace(ROOM_UID, roomUid)).remove(userInfo);
 | 
	
		
			
				|  |  | +            //查询该房间当前直播间用户列表
 | 
	
		
			
				|  |  | +            RList<Object> roomUserCache = redissonClient.getList(LIVE_ROOM_USER_LIST.replace(ROOM_UID, roomUid));
 | 
	
		
			
				|  |  | +            if (roomUserCache.contains(userInfo)) {
 | 
	
		
			
				|  |  | +                //只有在房间的时候并且是杀进程那么融云会发送用户离线消息-此刻就发送退出房间消息给主讲人
 | 
	
		
			
				|  |  | +                if (user.getStatus().equals("1")) {
 | 
	
		
			
				|  |  | +                    ImRoomMessage message = new ImRoomMessage();
 | 
	
		
			
				|  |  | +                    message.setFromUserId(userId.toString());
 | 
	
		
			
				|  |  | +                    message.setToChatroomId(roomUid);
 | 
	
		
			
				|  |  | +                    message.setObjectName(ImRoomMessage.LOOKER_LOGIN_OUT);
 | 
	
		
			
				|  |  | +                    imFeignService.publishRoomMsg(message);
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                //从该房间当前直播间用户列表中移除
 | 
	
		
			
				|  |  | +                roomUserCache.remove(userInfo);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  |              //每次退出房间计算当前用户观看时长
 | 
	
		
			
				|  |  |              int minutesBetween = getMinutesBetween(userInfo.getDynamicJoinTime(), now);
 | 
	
		
			
				|  |  |              userInfo.setTotalViewTime(userInfo.getTotalViewTime() + minutesBetween);
 | 
	
	
		
			
				|  | @@ -291,16 +363,31 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
 | 
	
		
			
				|  |  |       * @param userId  用户id
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  |      public void joinRoom(String roomUid, Integer userId) {
 | 
	
		
			
				|  |  | +        Date now = new Date();
 | 
	
		
			
				|  |  | +        //获取直播间信息
 | 
	
		
			
				|  |  | +        RBucket<RoomSpeakerInfo> speakerCache = redissonClient.getBucket(LIVE_SPEAKER_INFO.replace(USER_ID, userId.toString()));
 | 
	
		
			
				|  |  | +        if (speakerCache.isExists()) {
 | 
	
		
			
				|  |  | +            //判断有没有是房间号的主讲人
 | 
	
		
			
				|  |  | +            RoomSpeakerInfo speakerInfo = speakerCache.get();
 | 
	
		
			
				|  |  | +            if (roomUid.equals(speakerInfo.getRoomUid())) {
 | 
	
		
			
				|  |  | +                speakerInfo.setJoinRoomTime(now);
 | 
	
		
			
				|  |  | +                speakerCache.set(speakerInfo);
 | 
	
		
			
				|  |  | +                return;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          //记录用户当前房间uid
 | 
	
		
			
				|  |  |          redissonClient.getBucket(LIVE_USER_ROOM.replace(USER_ID, userId.toString())).set(roomUid);
 | 
	
		
			
				|  |  |          //房间累计用户信息-指只要进入到该房间的用户都要记录
 | 
	
		
			
				|  |  |          RMap<Integer, RoomUserInfoVo> roomTotalUser = redissonClient.getMap(LIVE_ROOM_TOTAL_USER_LIST.replace(ROOM_UID, roomUid));
 | 
	
		
			
				|  |  | +        //查询直播间当前用户数据缓存
 | 
	
		
			
				|  |  | +        RList<RoomUserInfoVo> roomUserCacheList = redissonClient.getList(LIVE_ROOM_USER_LIST.replace(ROOM_UID, roomUid));
 | 
	
		
			
				|  |  |          //判断是否第一次进房间
 | 
	
		
			
				|  |  | -        Date now = new Date();
 | 
	
		
			
				|  |  |          RoomUserInfoVo userInfo;
 | 
	
		
			
				|  |  |          if (roomTotalUser.containsKey(userId)) {
 | 
	
		
			
				|  |  |              //多次进入更新动态进入时间
 | 
	
		
			
				|  |  |              userInfo = roomTotalUser.get(userId);
 | 
	
		
			
				|  |  | +            roomUserCacheList.remove(userInfo);
 | 
	
		
			
				|  |  |          } else {
 | 
	
		
			
				|  |  |              //第一次进该房间 写入用户首次进入时间
 | 
	
		
			
				|  |  |              userInfo = getUserInfo(userId);
 | 
	
	
		
			
				|  | @@ -309,28 +396,48 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |          userInfo.setDynamicJoinTime(now);
 | 
	
		
			
				|  |  |          roomTotalUser.put(userId, userInfo);
 | 
	
		
			
				|  |  | -        //添加到直播间当前用户数据缓存中
 | 
	
		
			
				|  |  | -        redissonClient.getList(LIVE_ROOM_USER_LIST.replace(ROOM_UID, roomUid)).add(userInfo);
 | 
	
		
			
				|  |  | +        //将数据添加到当前直播间
 | 
	
		
			
				|  |  | +        roomUserCacheList.add(userInfo);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  | -     * 开始直播
 | 
	
		
			
				|  |  | +     * 开始直播-录像
 | 
	
		
			
				|  |  |       *
 | 
	
		
			
				|  |  | -     * @param roomUid
 | 
	
		
			
				|  |  | +     * @param roomUid 房间uid
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  | -    public void startLive(String roomUid) {
 | 
	
		
			
				|  |  | -        //开始录制视频
 | 
	
		
			
				|  |  | -        imFeignService.startRecord(roomUid);
 | 
	
		
			
				|  |  | +    public void startLive(String roomUid, Integer userId) {
 | 
	
		
			
				|  |  | +        //查询房间信息是否允许录像
 | 
	
		
			
				|  |  | +        RBucket<RoomSpeakerInfo> speakerCache = redissonClient.getBucket(LIVE_SPEAKER_INFO.replace(USER_ID, userId.toString()));
 | 
	
		
			
				|  |  | +        RoomSpeakerInfo roomSpeakerInfo = speakerCache.get();
 | 
	
		
			
				|  |  | +        if (roomSpeakerInfo.getWhetherVideo() == 0) {
 | 
	
		
			
				|  |  | +            //开始录制视频
 | 
	
		
			
				|  |  | +            imFeignService.startRecord(roomUid);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        //开始直播
 | 
	
		
			
				|  |  | +        roomSpeakerInfo.setState(0);
 | 
	
		
			
				|  |  | +        roomSpeakerInfo.setStartLiveTime(new Date());
 | 
	
		
			
				|  |  | +        speakerCache.set(roomSpeakerInfo);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  | -     * 关闭直播
 | 
	
		
			
				|  |  | +     * 关闭直播-录像
 | 
	
		
			
				|  |  |       *
 | 
	
		
			
				|  |  | -     * @param roomUid
 | 
	
		
			
				|  |  | +     * @param roomUid 房间uid
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  | -    public void closeLive(String roomUid) {
 | 
	
		
			
				|  |  | -        //停止录制视频
 | 
	
		
			
				|  |  | -        imFeignService.stopRecord(roomUid);
 | 
	
		
			
				|  |  | +    public void closeLive(String roomUid, Integer userId) {
 | 
	
		
			
				|  |  | +        //关闭直播
 | 
	
		
			
				|  |  | +        RBucket<RoomSpeakerInfo> speakerCache = redissonClient.getBucket(LIVE_SPEAKER_INFO.replace(USER_ID, userId.toString()));
 | 
	
		
			
				|  |  | +        RoomSpeakerInfo roomSpeakerInfo = speakerCache.get();
 | 
	
		
			
				|  |  | +        roomSpeakerInfo.setState(1);
 | 
	
		
			
				|  |  | +        if (roomSpeakerInfo.getWhetherVideo() == 0) {
 | 
	
		
			
				|  |  | +            //停止录制视频
 | 
	
		
			
				|  |  | +            imFeignService.stopRecord(roomUid);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        //计算时长
 | 
	
		
			
				|  |  | +        int minutesBetween = getMinutesBetween(roomSpeakerInfo.getStartLiveTime(), new Date());
 | 
	
		
			
				|  |  | +        int i = Objects.isNull(roomSpeakerInfo.getTotalLiveTime()) ? 0 : roomSpeakerInfo.getTotalLiveTime();
 | 
	
		
			
				|  |  | +        roomSpeakerInfo.setTotalLiveTime(i + minutesBetween);
 | 
	
		
			
				|  |  | +        speakerCache.set(roomSpeakerInfo);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      /**
 | 
	
	
		
			
				|  | @@ -353,55 +460,44 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
 | 
	
		
			
				|  |  |          list.forEach(this::createLiveRoom);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    //移除
 | 
	
		
			
				|  |  | -    private void removeUserCache(Integer userId, String lodRoomUid) {
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    private RoomUserInfoVo getUserInfo(Integer userId) {
 | 
	
		
			
				|  |  | -        RoomUserInfoVo userInfo = new RoomUserInfoVo();
 | 
	
		
			
				|  |  | -        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;
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    //计算时间差-分钟数不满一分钟为0
 | 
	
		
			
				|  |  | -    private int getMinutesBetween(Date startDT, Date endDT) {
 | 
	
		
			
				|  |  | -        if (Objects.isNull(startDT) || Objects.isNull(endDT)) {
 | 
	
		
			
				|  |  | -            return 0;
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        //课程结束时间-课程开始时间
 | 
	
		
			
				|  |  | -        long durationTime = endDT.getTime() - startDT.getTime();
 | 
	
		
			
				|  |  | -        //相差多少分钟
 | 
	
		
			
				|  |  | -        return new Long(durationTime / 1000 / 60).intValue();
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  |       * 去融云创建房间
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  |      private void createLiveRoom(ImLiveBroadcastRoom room) {
 | 
	
		
			
				|  |  | +        Date now = new Date();
 | 
	
		
			
				|  |  |          try {
 | 
	
		
			
				|  |  | +            //生成主讲人信息到缓存
 | 
	
		
			
				|  |  | +            SysUser sysUser = getSysUser(room.getSpeakerId());
 | 
	
		
			
				|  |  | +            RoomSpeakerInfo speakerInfo = new RoomSpeakerInfo();
 | 
	
		
			
				|  |  | +            speakerInfo.setSpeakerId(sysUser.getId());
 | 
	
		
			
				|  |  | +            speakerInfo.setSpeakerName(sysUser.getRealName());
 | 
	
		
			
				|  |  | +            speakerInfo.setCreateRoomTime(now);
 | 
	
		
			
				|  |  | +            speakerInfo.setRoomUid(room.getRoomUid());
 | 
	
		
			
				|  |  | +            speakerInfo.setTenantId(sysUser.getTenantId());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            //查询房间信息是否允许录像
 | 
	
		
			
				|  |  | +            ImLiveBroadcastRoom one = this.getOne(new QueryWrapper<ImLiveBroadcastRoom>().eq("room_uid_", room.getRoomUid()));
 | 
	
		
			
				|  |  | +            Optional.ofNullable(one.getRoomConfig())
 | 
	
		
			
				|  |  | +                    .map(c -> JSON.parseObject(c, ImLiveBroadcastRoomDto.RoomConfig.class))
 | 
	
		
			
				|  |  | +                    .filter(c -> Objects.nonNull(c.getWhether_video()))
 | 
	
		
			
				|  |  | +                    .filter(c -> c.getWhether_video() == 0)
 | 
	
		
			
				|  |  | +                    .ifPresent(c -> speakerInfo.setWhetherVideo(0));//可以录制视频
 | 
	
		
			
				|  |  | +            redissonClient.getBucket(LIVE_SPEAKER_INFO.replace(USER_ID, room.getSpeakerId().toString())).set(speakerInfo);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            //生成0点赞
 | 
	
		
			
				|  |  | +            redissonClient.getBucket(LIVE_ROOM_LIKE.replace(ROOM_UID, room.getRoomUid())).set(0);
 | 
	
		
			
				|  |  | +            //修改房间状态为开始
 | 
	
		
			
				|  |  | +            room.setLiveState(1);
 | 
	
		
			
				|  |  | +            room.setUpdatedTime(now);
 | 
	
		
			
				|  |  | +            room.setUpdatedBy(-1);
 | 
	
		
			
				|  |  | +            baseMapper.updateById(room);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |              //去融云创建房间
 | 
	
		
			
				|  |  | -            Object liveRoom = imFeignService.createLiveRoom(room.getRoomUid(), room.getRoomTitle());
 | 
	
		
			
				|  |  | +            imFeignService.createLiveRoom(room.getRoomUid(), room.getRoomTitle());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          } catch (Exception e) {
 | 
	
		
			
				|  |  | -            throw new BizException(e.getMessage());
 | 
	
		
			
				|  |  | +            log.error(">>>>>>>>>> createLiveRoom error roomUid:{} msg:{}", room.getRoomUid(), e.getMessage());
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  | -        //生成0点赞
 | 
	
		
			
				|  |  | -        redissonClient.getBucket(LIVE_ROOM_LIKE.replace(ROOM_UID, room.getRoomUid())).set(0);
 | 
	
		
			
				|  |  | -        //todo 修改房间状态
 | 
	
		
			
				|  |  | -        room.setLiveState(1);
 | 
	
		
			
				|  |  | -        room.setUpdatedTime(new Date());
 | 
	
		
			
				|  |  | -        room.setUpdatedBy(-1);
 | 
	
		
			
				|  |  | -        baseMapper.updateById(room);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      private void getRoomData(ImLiveBroadcastRoomVo roomVo) {
 | 
	
	
		
			
				|  | @@ -433,9 +529,10 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  | -     * 分享直播间
 | 
	
		
			
				|  |  | +     * 测试
 | 
	
		
			
				|  |  |       */
 | 
	
		
			
				|  |  |      public void test() {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Override
 | 
	
	
		
			
				|  | @@ -460,5 +557,149 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
 | 
	
		
			
				|  |  |          return redissonClient.getList(LIVE_ROOM_USER_LIST.replace(ROOM_UID, roomUid));
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    private RoomUserInfoVo getUserInfo(Integer userId) {
 | 
	
		
			
				|  |  | +        RoomUserInfoVo userInfo = new RoomUserInfoVo();
 | 
	
		
			
				|  |  | +        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);
 | 
	
		
			
				|  |  | +        userInfo.setTenantId(sysUser.getTenantId());
 | 
	
		
			
				|  |  | +        return userInfo;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    //计算时间差-分钟数不满一分钟为0
 | 
	
		
			
				|  |  | +    private int getMinutesBetween(Date startDT, Date endDT) {
 | 
	
		
			
				|  |  | +        if (Objects.isNull(startDT) || Objects.isNull(endDT)) {
 | 
	
		
			
				|  |  | +            return 0;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        //课程结束时间-课程开始时间
 | 
	
		
			
				|  |  | +        long durationTime = endDT.getTime() - startDT.getTime();
 | 
	
		
			
				|  |  | +        //相差多少分钟
 | 
	
		
			
				|  |  | +        return new Long(durationTime / 1000 / 60).intValue();
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 主讲人信息
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public static class RoomSpeakerInfo implements Serializable {
 | 
	
		
			
				|  |  | +        //主讲人id
 | 
	
		
			
				|  |  | +        private Integer speakerId;
 | 
	
		
			
				|  |  | +        //主讲人名称
 | 
	
		
			
				|  |  | +        private String speakerName;
 | 
	
		
			
				|  |  | +        //直播状态 0 直播中 1关闭直播 2暂停直播(直接断开退出直播间后超过半小时还未开播则自动清算)
 | 
	
		
			
				|  |  | +        private Integer state;
 | 
	
		
			
				|  |  | +        //房间uid
 | 
	
		
			
				|  |  | +        private String roomUid;
 | 
	
		
			
				|  |  | +        //房间创建时间
 | 
	
		
			
				|  |  | +        private Date createRoomTime;
 | 
	
		
			
				|  |  | +        //进入房间时间
 | 
	
		
			
				|  |  | +        private Date joinRoomTime;
 | 
	
		
			
				|  |  | +        //开始直播时间
 | 
	
		
			
				|  |  | +        private Date startLiveTime;
 | 
	
		
			
				|  |  | +        //退出房间时间
 | 
	
		
			
				|  |  | +        private Date exitRoomTime;
 | 
	
		
			
				|  |  | +        //总直播时间
 | 
	
		
			
				|  |  | +        private Integer totalLiveTime = 0;
 | 
	
		
			
				|  |  | +        //是否录像 0允许 1不允许
 | 
	
		
			
				|  |  | +        private Integer whetherVideo;
 | 
	
		
			
				|  |  | +        //机构
 | 
	
		
			
				|  |  | +        private Integer tenantId;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        public Integer getSpeakerId() {
 | 
	
		
			
				|  |  | +            return speakerId;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        public void setSpeakerId(Integer speakerId) {
 | 
	
		
			
				|  |  | +            this.speakerId = speakerId;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        public String getSpeakerName() {
 | 
	
		
			
				|  |  | +            return speakerName;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        public void setSpeakerName(String speakerName) {
 | 
	
		
			
				|  |  | +            this.speakerName = speakerName;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        public Integer getState() {
 | 
	
		
			
				|  |  | +            return state;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        public void setState(Integer state) {
 | 
	
		
			
				|  |  | +            this.state = state;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        public String getRoomUid() {
 | 
	
		
			
				|  |  | +            return roomUid;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        public void setRoomUid(String roomUid) {
 | 
	
		
			
				|  |  | +            this.roomUid = roomUid;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        public Date getJoinRoomTime() {
 | 
	
		
			
				|  |  | +            return joinRoomTime;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        public void setJoinRoomTime(Date joinRoomTime) {
 | 
	
		
			
				|  |  | +            this.joinRoomTime = joinRoomTime;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        public Date getStartLiveTime() {
 | 
	
		
			
				|  |  | +            return startLiveTime;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        public void setStartLiveTime(Date startLiveTime) {
 | 
	
		
			
				|  |  | +            this.startLiveTime = startLiveTime;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        public Integer getTotalLiveTime() {
 | 
	
		
			
				|  |  | +            return totalLiveTime;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        public void setTotalLiveTime(Integer totalLiveTime) {
 | 
	
		
			
				|  |  | +            this.totalLiveTime = totalLiveTime;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        public Date getCreateRoomTime() {
 | 
	
		
			
				|  |  | +            return createRoomTime;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        public void setCreateRoomTime(Date createRoomTime) {
 | 
	
		
			
				|  |  | +            this.createRoomTime = createRoomTime;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        public Date getExitRoomTime() {
 | 
	
		
			
				|  |  | +            return exitRoomTime;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        public void setExitRoomTime(Date exitRoomTime) {
 | 
	
		
			
				|  |  | +            this.exitRoomTime = exitRoomTime;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        public Integer getWhetherVideo() {
 | 
	
		
			
				|  |  | +            return whetherVideo;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        public void setWhetherVideo(Integer whetherVideo) {
 | 
	
		
			
				|  |  | +            this.whetherVideo = whetherVideo;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        public Integer getTenantId() {
 | 
	
		
			
				|  |  | +            return tenantId;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        public void setTenantId(Integer tenantId) {
 | 
	
		
			
				|  |  | +            this.tenantId = tenantId;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 |