|
@@ -149,7 +149,7 @@ public class LiveWatchUserServiceImpl implements ILiveWatchUserService {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
- public LiveWatchUser getByLiveIdAndUserId(long liveId, long userId) {
|
|
|
|
|
|
|
+ public List<LiveWatchUser> getByLiveIdAndUserId(long liveId, long userId) {
|
|
|
return baseMapper.selectUserByLiveIdAndUserId(liveId, userId);
|
|
return baseMapper.selectUserByLiveIdAndUserId(liveId, userId);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -232,62 +232,120 @@ public class LiveWatchUserServiceImpl implements ILiveWatchUserService {
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public LiveWatchUser joinWithoutLocation(long liveId, long userId) {
|
|
public LiveWatchUser joinWithoutLocation(long liveId, long userId) {
|
|
|
- LiveWatchUser liveWatchUser = getByLiveIdAndUserId(liveId, userId);
|
|
|
|
|
FsUserScrm fsUserVO = fsUserService.selectFsUserByUserId(userId);
|
|
FsUserScrm fsUserVO = fsUserService.selectFsUserByUserId(userId);
|
|
|
Date now = DateUtils.getNowDate();
|
|
Date now = DateUtils.getNowDate();
|
|
|
|
|
|
|
|
// 查询直播间信息
|
|
// 查询直播间信息
|
|
|
Live live = liveMapper.selectLiveByLiveId(liveId);
|
|
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 {
|
|
} 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);
|
|
baseMapper.updateLiveWatchUser(liveWatchUser);
|
|
|
- }else{
|
|
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 不存在则插入
|
|
|
liveWatchUser = new LiveWatchUser();
|
|
liveWatchUser = new LiveWatchUser();
|
|
|
liveWatchUser.setLiveId(liveId);
|
|
liveWatchUser.setLiveId(liveId);
|
|
|
liveWatchUser.setUserId(userId);
|
|
liveWatchUser.setUserId(userId);
|
|
|
liveWatchUser.setAvatar(fsUserVO.getAvatar());
|
|
liveWatchUser.setAvatar(fsUserVO.getAvatar());
|
|
|
liveWatchUser.setMsgStatus(0);
|
|
liveWatchUser.setMsgStatus(0);
|
|
|
liveWatchUser.setOnline(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.setCreateTime(now);
|
|
|
liveWatchUser.setUpdateTime(now);
|
|
liveWatchUser.setUpdateTime(now);
|
|
|
baseMapper.insertLiveWatchUser(liveWatchUser);
|
|
baseMapper.insertLiveWatchUser(liveWatchUser);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
liveWatchUser.setAvatar(fsUserVO.getAvatar());
|
|
liveWatchUser.setAvatar(fsUserVO.getAvatar());
|
|
|
liveWatchUser.setNickName(fsUserVO.getNickname());
|
|
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));
|
|
redisCache.hashPut(hashKey, String.valueOf(userId), JSON.toJSONString(liveWatchUser));
|
|
|
return liveWatchUser;
|
|
return liveWatchUser;
|
|
|
}
|
|
}
|
|
|
@Override
|
|
@Override
|
|
|
public LiveWatchUser close(long liveId, long userId) {
|
|
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 {
|
|
try {
|
|
|
Long onlineSeconds = liveWatchUser.getOnlineSeconds();
|
|
Long onlineSeconds = liveWatchUser.getOnlineSeconds();
|
|
@@ -322,11 +380,14 @@ public class LiveWatchUserServiceImpl implements ILiveWatchUserService {
|
|
|
*/
|
|
*/
|
|
|
@Override
|
|
@Override
|
|
|
public int changeUserState(Long liveId, Long userId) {
|
|
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;
|
|
return 0;
|
|
|
}
|
|
}
|