Ver Fonte

直播间在线用户 直播还是录播判断

yuhongqi há 1 semana atrás
pai
commit
be80817847

+ 7 - 8
fs-live-app/src/main/java/com/fs/live/websocket/handle/LiveChatHandler.java

@@ -71,7 +71,7 @@ public class LiveChatHandler extends SimpleChannelInboundHandler<TextWebSocketFr
 
             if (userType == 0) {
                 // 加入房间
-                liveWatchUserService.joinWithoutLocation(liveId, userId);
+                LiveWatchUser liveWatchUser = liveWatchUserService.joinWithoutLocation(liveId, userId);
                 room.put(userId, ctx.channel());
 
                 FsUser fsUser = fsUserService.selectFsUserByUserId(userId);
@@ -80,7 +80,6 @@ public class LiveChatHandler extends SimpleChannelInboundHandler<TextWebSocketFr
                     return;
                 }
 
-                LiveWatchUserVO liveWatchUserVO = liveWatchUserService.selectWatchUserByLiveIdAndUserId(liveId, userId);
 
                 SendMsgVo sendMsgVo = new SendMsgVo();
                 sendMsgVo.setLiveId(liveId);
@@ -88,7 +87,7 @@ public class LiveChatHandler extends SimpleChannelInboundHandler<TextWebSocketFr
                 sendMsgVo.setUserType(userType);
                 sendMsgVo.setCmd("entry");
                 sendMsgVo.setMsg("用户进入");
-                sendMsgVo.setData(JSONObject.toJSONString(liveWatchUserVO));
+                sendMsgVo.setData(JSONObject.toJSONString(liveWatchUser));
                 sendMsgVo.setNickName(fsUser.getNickname());
                 sendMsgVo.setAvatar(fsUser.getAvatar());
 
@@ -176,8 +175,8 @@ public class LiveChatHandler extends SimpleChannelInboundHandler<TextWebSocketFr
                     liveMsg.setCreateTime(new Date());
 
                     if (userType == 0) {
-                        LiveWatchUser liveWatchUser = liveWatchUserService.getByLiveIdAndUserId(msg.getLiveId(), msg.getUserId());
-                        if(liveWatchUser.getMsgStatus() == 1){
+                        List<LiveWatchUser> liveWatchUser = liveWatchUserService.getByLiveIdAndUserId(msg.getLiveId(), msg.getUserId());
+                        if(!liveWatchUser.isEmpty() && liveWatchUser.get(0).getMsgStatus() == 1){
                             sendMessage(channelHandlerContext.channel(), JSONObject.toJSONString(R.error("你以被禁言")));
                             return;
                         }
@@ -219,14 +218,14 @@ public class LiveChatHandler extends SimpleChannelInboundHandler<TextWebSocketFr
 
         if (userType == 0) {
             FsUser fsUser = fsUserService.selectFsUserByUserId(userId);
-            liveWatchUserService.close(liveId, userId);
+            LiveWatchUser close = liveWatchUserService.close(liveId, userId);
             room.remove(userId);
 
             if (room.isEmpty()) {
                 rooms.remove(liveId);
             }
 
-            LiveWatchUserVO liveWatchUserVO = liveWatchUserService.selectWatchUserByLiveIdAndUserId(liveId, userId);
+
 
             SendMsgVo sendMsgVo = new SendMsgVo();
             sendMsgVo.setLiveId(liveId);
@@ -234,7 +233,7 @@ public class LiveChatHandler extends SimpleChannelInboundHandler<TextWebSocketFr
             sendMsgVo.setUserType(userType);
             sendMsgVo.setCmd("out");
             sendMsgVo.setMsg("用户离开");
-            sendMsgVo.setData(JSONObject.toJSONString(liveWatchUserVO));
+            sendMsgVo.setData(JSONObject.toJSONString(close));
             sendMsgVo.setNickName(fsUser.getNickname());
             sendMsgVo.setAvatar(fsUser.getAvatar());
 

+ 7 - 8
fs-live-app/src/main/java/com/fs/live/websocket/service/WebSocketServer.java

@@ -134,8 +134,7 @@ public class WebSocketServer {
             if (isFirstViewer) {
                 redisCache.incr(UNIQUE_VIEWERS_KEY + liveId, 1);
             }
-            LiveWatchUser liveWatchUser = liveWatchUserService.getByLiveIdAndUserId(liveId, userId);
-            liveWatchUserVO.setMsgStatus(liveWatchUser.getMsgStatus());
+            liveWatchUserVO.setMsgStatus(liveWatchUserVO.getMsgStatus());
             SendMsgVo sendMsgVo = new SendMsgVo();
             sendMsgVo.setLiveId(liveId);
             sendMsgVo.setUserId(userId);
@@ -199,21 +198,21 @@ public class WebSocketServer {
             if (Objects.isNull(fsUser)) {
                 throw new BaseException("用户信息错误");
             }
-
-            LiveWatchUser liveWatchUserVO = liveWatchUserService.close(liveId, userId);
             room.remove(userId);
-
             if (room.isEmpty()) {
                 rooms.remove(liveId);
             }
 
 
+
+
+
             // 直播间在线人数 -1
             redisCache.incr(ONLINE_USERS_KEY + liveId, -1);
             // 从在线用户Set中移除用户ID
             String onlineUsersSetKey = ONLINE_USERS_SET_KEY + liveId;
             redisCache.redisTemplate.opsForSet().remove(onlineUsersSetKey, String.valueOf(userId));
-
+            LiveWatchUser liveWatchUserVO = liveWatchUserService.close(liveId, userId);
             SendMsgVo sendMsgVo = new SendMsgVo();
             sendMsgVo.setLiveId(liveId);
             sendMsgVo.setUserId(userId);
@@ -261,8 +260,8 @@ public class WebSocketServer {
                     liveMsg.setCreateTime(new Date());
 
                     if (userType == 0) {
-                        LiveWatchUser liveWatchUser = liveWatchUserService.getByLiveIdAndUserId(msg.getLiveId(), msg.getUserId());
-                        if(liveWatchUser.getMsgStatus() == 1){
+                        List<LiveWatchUser> liveWatchUser = liveWatchUserService.getByLiveIdAndUserId(msg.getLiveId(), msg.getUserId());
+                        if(!liveWatchUser.isEmpty() && liveWatchUser.get(0).getMsgStatus() == 1){
                             sendMessage(session, JSONObject.toJSONString(R.error("你已被禁言")));
                             return;
                         }

+ 2 - 2
fs-service/src/main/java/com/fs/live/mapper/LiveWatchUserMapper.java

@@ -105,12 +105,12 @@ public interface LiveWatchUserMapper {
     List<LiveWatchUser> selectLiveWatchAndRegisterUser(@Param("liveId") Long liveId,@Param("lotteryId") Long lotteryId);
 
     @Select("select * from live_watch_user where live_id = #{liveId} and user_id = #{userId}")
-    LiveWatchUser selectUserByLiveIdAndUserId(@Param("liveId") long liveId,@Param("userId")  long userId);
+    List<LiveWatchUser> selectUserByLiveIdAndUserId(@Param("liveId") long liveId,@Param("userId")  long userId);
 
     /**
      * 根据唯一索引查询:live_id, user_id, live_flag, replay_flag
      */
-    LiveWatchUser selectByUniqueIndex(@Param("liveId") Long liveId, @Param("userId") Long userId, 
+    LiveWatchUser selectByUniqueIndex(@Param("liveId") Long liveId, @Param("userId") Long userId,
                                       @Param("liveFlag") Integer liveFlag, @Param("replayFlag") Integer replayFlag);
 
     /**

+ 1 - 1
fs-service/src/main/java/com/fs/live/service/ILiveWatchUserService.java

@@ -73,7 +73,7 @@ public interface ILiveWatchUserService {
      */
     int deleteLiveWatchUserById(Long id);
 
-    LiveWatchUser getByLiveIdAndUserId(long liveId, long userId);
+    List<LiveWatchUser> getByLiveIdAndUserId(long liveId, long userId);
 
     LiveWatchUser join(long liveId, long userId, String location);
     LiveWatchUser joinWithoutLocation(long liveId, long userId);

+ 93 - 32
fs-service/src/main/java/com/fs/live/service/impl/LiveWatchUserServiceImpl.java

@@ -149,7 +149,7 @@ public class LiveWatchUserServiceImpl implements ILiveWatchUserService {
     }
 
     @Override
-    public LiveWatchUser getByLiveIdAndUserId(long liveId, long userId) {
+    public List<LiveWatchUser> getByLiveIdAndUserId(long liveId, long userId) {
         return baseMapper.selectUserByLiveIdAndUserId(liveId, userId);
     }
 
@@ -232,62 +232,120 @@ public class LiveWatchUserServiceImpl implements ILiveWatchUserService {
 
     @Override
     public LiveWatchUser joinWithoutLocation(long liveId, long userId) {
-        LiveWatchUser liveWatchUser = getByLiveIdAndUserId(liveId, userId);
         FsUserScrm fsUserVO = fsUserService.selectFsUserByUserId(userId);
         Date now = DateUtils.getNowDate();
 
         // 查询直播间信息
         Live live = liveMapper.selectLiveByLiveId(liveId);
+        if (live == null) {
+            throw new RuntimeException("直播间不存在");
+        }
 
-        // 判断用户进入时间:如果进入时间大于直播结束时间,说明是回放
-        boolean isReplay = false;
-        if (live != null && live.getFinishTime() != null) {
-            Date finishTime = java.sql.Timestamp.valueOf(live.getFinishTime());
-            isReplay = now.after(finishTime);
+        // 查询直播间录播视频时长(video_type IN (1, 2))
+        List<LiveVideo> videos = liveVideoMapper.selectByLiveIdAndType(liveId,1);
+        long totalDuration = 0L;
+        if (videos != null && !videos.isEmpty()) {
+            totalDuration = videos.stream()
+                    .filter(v -> v.getVideoType() != null && (v.getVideoType() == 1 || v.getVideoType() == 2))
+                    .filter(v -> v.getDuration() != null)
+                    .mapToLong(LiveVideo::getDuration)
+                    .sum();
         }
 
-        if(liveWatchUser != null) {
-            liveWatchUser.setUpdateTime(now);
-            liveWatchUser.setOnline(0);
+        // 判断是直播还是回放:开播时间 + 视频时长
+        Integer liveFlag = 0;
+        Integer replayFlag = 0;
+
+        if (live.getStartTime() != null) {
+            // 将 LocalDateTime 转换为 Date
+            Date startTime = java.sql.Timestamp.valueOf(live.getStartTime());
+            // 计算结束时间:开播时间 + 视频时长(秒)
+            Date endTime = new Date(startTime.getTime() + totalDuration * 1000);
 
-            // 更新进入标记
-            if (isReplay) {
-                liveWatchUser.setReplayFlag(1);
+            if (now.before(endTime)) {
+                // 当前时间 < 开播时间 + 视频时长,说明是直播
+                liveFlag = 1;
+                replayFlag = 0;
             } else {
-                liveWatchUser.setLiveFlag(1);
+                // 当前时间 >= 开播时间 + 视频时长,说明是回放
+                liveFlag = 0;
+                replayFlag = 1;
             }
+        }
+
+        // 使用唯一索引查询:live_id, user_id, live_flag, replay_flag
+        LiveWatchUser liveWatchUser = baseMapper.selectByUniqueIndex(liveId, userId, liveFlag, replayFlag);
 
+        if (liveWatchUser != null) {
+            // 存在则更新
+            liveWatchUser.setUpdateTime(now);
+            liveWatchUser.setOnline(0);
             baseMapper.updateLiveWatchUser(liveWatchUser);
-        }else{
+        } else {
+            // 不存在则插入
             liveWatchUser = new LiveWatchUser();
             liveWatchUser.setLiveId(liveId);
             liveWatchUser.setUserId(userId);
             liveWatchUser.setAvatar(fsUserVO.getAvatar());
             liveWatchUser.setMsgStatus(0);
             liveWatchUser.setOnline(0);
-
-            // 设置进入标记
-            if (isReplay) {
-                liveWatchUser.setReplayFlag(1);
-                liveWatchUser.setLiveFlag(0);
-            } else {
-                liveWatchUser.setLiveFlag(1);
-                liveWatchUser.setReplayFlag(0);
-            }
-
+            liveWatchUser.setLiveFlag(liveFlag);
+            liveWatchUser.setReplayFlag(replayFlag);
             liveWatchUser.setCreateTime(now);
             liveWatchUser.setUpdateTime(now);
             baseMapper.insertLiveWatchUser(liveWatchUser);
         }
+
         liveWatchUser.setAvatar(fsUserVO.getAvatar());
         liveWatchUser.setNickName(fsUserVO.getNickname());
-        String hashKey  = String.format(LiveKeysConstant.LIVE_WATCH_USERS, liveId);
+        String hashKey = String.format(LiveKeysConstant.LIVE_WATCH_USERS, liveId);
         redisCache.hashPut(hashKey, String.valueOf(userId), JSON.toJSONString(liveWatchUser));
         return liveWatchUser;
     }
     @Override
     public LiveWatchUser close(long liveId, long userId) {
-        LiveWatchUser liveWatchUser = getByLiveIdAndUserId(liveId, userId);
+        Date now = DateUtils.getNowDate();
+
+        // 查询直播间信息
+        Live live = liveMapper.selectLiveByLiveId(liveId);
+        if (live == null) {
+            throw new RuntimeException("直播间不存在");
+        }
+
+        // 查询直播间录播视频时长(video_type IN (1, 2))
+        List<LiveVideo> videos = liveVideoMapper.selectByLiveIdAndType(liveId,1);
+        long totalDuration = 0L;
+        if (videos != null && !videos.isEmpty()) {
+            totalDuration = videos.stream()
+                    .filter(v -> v.getVideoType() != null && (v.getVideoType() == 1 || v.getVideoType() == 2))
+                    .filter(v -> v.getDuration() != null)
+                    .mapToLong(LiveVideo::getDuration)
+                    .sum();
+        }
+
+        // 判断是直播还是回放:开播时间 + 视频时长
+        Integer liveFlag = 0;
+        Integer replayFlag = 0;
+
+        if (live.getStartTime() != null) {
+            // 将 LocalDateTime 转换为 Date
+            Date startTime = java.sql.Timestamp.valueOf(live.getStartTime());
+            // 计算结束时间:开播时间 + 视频时长(秒)
+            Date endTime = new Date(startTime.getTime() + totalDuration * 1000);
+
+            if (now.before(endTime)) {
+                // 当前时间 < 开播时间 + 视频时长,说明是直播
+                liveFlag = 1;
+                replayFlag = 0;
+            } else {
+                // 当前时间 >= 开播时间 + 视频时长,说明是回放
+                liveFlag = 0;
+                replayFlag = 1;
+            }
+        }
+
+        // 使用唯一索引查询:live_id, user_id, live_flag, replay_flag
+        LiveWatchUser liveWatchUser = baseMapper.selectByUniqueIndex(liveId, userId, liveFlag, replayFlag);
         // 设置在线时长
         try {
             Long onlineSeconds = liveWatchUser.getOnlineSeconds();
@@ -322,11 +380,14 @@ public class LiveWatchUserServiceImpl implements ILiveWatchUserService {
      */
     @Override
     public int changeUserState(Long liveId, Long userId) {
-        LiveWatchUser liveWatchUser = getByLiveIdAndUserId(liveId, userId);
-        if (Objects.nonNull(liveWatchUser)) {
-            liveWatchUser.setMsgStatus(Math.abs(1 - liveWatchUser.getMsgStatus()));
-            liveWatchUser.setUpdateTime(DateUtils.getNowDate());
-            return baseMapper.updateLiveWatchUser(liveWatchUser);
+        List<LiveWatchUser> liveWatchUser = getByLiveIdAndUserId(liveId, userId);
+        if (!liveWatchUser.isEmpty()) {
+            for (LiveWatchUser watchUser : liveWatchUser) {
+                watchUser.setMsgStatus(Math.abs(1 - watchUser.getMsgStatus()));
+                watchUser.setUpdateTime(DateUtils.getNowDate());
+                baseMapper.updateLiveWatchUser(watchUser);
+            }
+            return 1;
         }
         return 0;
     }