|
|
@@ -95,6 +95,12 @@ public class LiveWatchUserServiceImpl implements ILiveWatchUserService {
|
|
|
} else if ("silenced".equals(liveWatchUser.getTabName())) {
|
|
|
liveWatchUser.setMsgStatus(1);
|
|
|
}
|
|
|
+ // 先查缓存,获取liveFlag和replayFlag
|
|
|
+ if (liveWatchUser != null && liveWatchUser.getLiveId() != null) {
|
|
|
+ Map<String, Integer> flagMap = getLiveFlagWithCache(liveWatchUser.getLiveId());
|
|
|
+ liveWatchUser.setLiveFlag(flagMap.get("liveFlag"));
|
|
|
+ liveWatchUser.setReplayFlag(flagMap.get("replayFlag"));
|
|
|
+ }
|
|
|
return baseMapper.selectLiveWatchUserList(liveWatchUser);
|
|
|
}
|
|
|
|
|
|
@@ -150,7 +156,47 @@ public class LiveWatchUserServiceImpl implements ILiveWatchUserService {
|
|
|
|
|
|
@Override
|
|
|
public List<LiveWatchUser> getByLiveIdAndUserId(long liveId, long userId) {
|
|
|
- return baseMapper.selectUserByLiveIdAndUserId(liveId, userId);
|
|
|
+ // 先查缓存,获取liveFlag和replayFlag
|
|
|
+ Map<String, Integer> flagMap = getLiveFlagWithCache(liveId);
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("liveId", liveId);
|
|
|
+ params.put("userId", userId);
|
|
|
+ params.put("liveFlag", flagMap.get("liveFlag"));
|
|
|
+ params.put("replayFlag", flagMap.get("replayFlag"));
|
|
|
+ return baseMapper.selectUserByLiveIdAndUserId(params);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取直播间的直播/回放状态(带缓存)
|
|
|
+ * @param liveId 直播间ID
|
|
|
+ * @return Map包含liveFlag和replayFlag
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Map<String, Integer> getLiveFlagWithCache(Long liveId) {
|
|
|
+ String cacheKey = String.format(LiveKeysConstant.LIVE_FLAG_CACHE, liveId);
|
|
|
+
|
|
|
+ // 先查缓存
|
|
|
+ Map<String, Integer> cached = redisCache.getCacheObject(cacheKey);
|
|
|
+ if (cached != null && cached.containsKey("liveFlag") && cached.containsKey("replayFlag")) {
|
|
|
+ return cached;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 缓存不存在,查询数据库
|
|
|
+ Integer liveFlag = liveMapper.selectLiveFlagByLiveId(liveId);
|
|
|
+ if (liveFlag == null) {
|
|
|
+ liveFlag = 0;
|
|
|
+ }
|
|
|
+ Integer replayFlag = 1 - liveFlag; // 反数
|
|
|
+
|
|
|
+ // 构建结果
|
|
|
+ Map<String, Integer> result = new HashMap<>();
|
|
|
+ result.put("liveFlag", liveFlag);
|
|
|
+ result.put("replayFlag", replayFlag);
|
|
|
+
|
|
|
+ // 缓存结果,过期时间1分钟
|
|
|
+ redisCache.setCacheObject(cacheKey, result, LiveKeysConstant.LIVE_FLAG_CACHE_EXPIRE, TimeUnit.SECONDS);
|
|
|
+
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -164,37 +210,10 @@ public class LiveWatchUserServiceImpl implements ILiveWatchUserService {
|
|
|
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;
|
|
|
- }
|
|
|
- }
|
|
|
+ // 获取直播/回放状态(带缓存)
|
|
|
+ Map<String, Integer> flagMap = getLiveFlagWithCache(liveId);
|
|
|
+ Integer liveFlag = flagMap.get("liveFlag");
|
|
|
+ Integer replayFlag = flagMap.get("replayFlag");
|
|
|
|
|
|
// 使用唯一索引查询:live_id, user_id, live_flag, replay_flag
|
|
|
LiveWatchUser liveWatchUser = baseMapper.selectByUniqueIndex(liveId, userId, liveFlag, replayFlag);
|
|
|
@@ -241,37 +260,10 @@ public class LiveWatchUserServiceImpl implements ILiveWatchUserService {
|
|
|
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;
|
|
|
- }
|
|
|
- }
|
|
|
+ // 获取直播/回放状态(带缓存)
|
|
|
+ Map<String, Integer> flagMap = getLiveFlagWithCache(liveId);
|
|
|
+ Integer liveFlag = flagMap.get("liveFlag");
|
|
|
+ Integer replayFlag = flagMap.get("replayFlag");
|
|
|
|
|
|
// 使用唯一索引查询:live_id, user_id, live_flag, replay_flag
|
|
|
LiveWatchUser liveWatchUser = baseMapper.selectByUniqueIndex(liveId, userId, liveFlag, replayFlag);
|
|
|
@@ -304,7 +296,6 @@ public class LiveWatchUserServiceImpl implements ILiveWatchUserService {
|
|
|
}
|
|
|
@Override
|
|
|
public LiveWatchUser close(long liveId, long userId) {
|
|
|
- Date now = DateUtils.getNowDate();
|
|
|
|
|
|
// 查询直播间信息
|
|
|
Live live = liveMapper.selectLiveByLiveId(liveId);
|
|
|
@@ -312,37 +303,10 @@ public class LiveWatchUserServiceImpl implements ILiveWatchUserService {
|
|
|
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;
|
|
|
- }
|
|
|
- }
|
|
|
+ // 获取直播/回放状态(带缓存)
|
|
|
+ Map<String, Integer> flagMap = getLiveFlagWithCache(liveId);
|
|
|
+ Integer liveFlag = flagMap.get("liveFlag");
|
|
|
+ Integer replayFlag = flagMap.get("replayFlag");
|
|
|
|
|
|
// 使用唯一索引查询:live_id, user_id, live_flag, replay_flag
|
|
|
LiveWatchUser liveWatchUser = baseMapper.selectByUniqueIndex(liveId, userId, liveFlag, replayFlag);
|
|
|
@@ -369,6 +333,13 @@ public class LiveWatchUserServiceImpl implements ILiveWatchUserService {
|
|
|
*/
|
|
|
@Override
|
|
|
public List<LiveWatchUserVO> selectWatchUserList(Map<String, Object> params) {
|
|
|
+ // 先查缓存,获取liveFlag和replayFlag
|
|
|
+ if (params != null && params.containsKey("liveId")) {
|
|
|
+ Long liveId = Long.valueOf(params.get("liveId").toString());
|
|
|
+ Map<String, Integer> flagMap = getLiveFlagWithCache(liveId);
|
|
|
+ params.put("liveFlag", flagMap.get("liveFlag"));
|
|
|
+ params.put("replayFlag", flagMap.get("replayFlag"));
|
|
|
+ }
|
|
|
return baseMapper.selectWatchUserListByLiveId(params);
|
|
|
}
|
|
|
|
|
|
@@ -400,17 +371,37 @@ public class LiveWatchUserServiceImpl implements ILiveWatchUserService {
|
|
|
*/
|
|
|
@Override
|
|
|
public LiveWatchUserVO selectWatchUserByLiveIdAndUserId(Long liveId, Long userId) {
|
|
|
- return baseMapper.selectWatchUserByLiveIdAndUserId(liveId, userId);
|
|
|
+ // 先查缓存,获取liveFlag和replayFlag
|
|
|
+ Map<String, Integer> flagMap = getLiveFlagWithCache(liveId);
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("liveId", liveId);
|
|
|
+ params.put("userId", userId);
|
|
|
+ params.put("liveFlag", flagMap.get("liveFlag"));
|
|
|
+ params.put("replayFlag", flagMap.get("replayFlag"));
|
|
|
+ return baseMapper.selectWatchUserByLiveIdAndUserId(params);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<LiveWatchUserVO> selectOnlineUserList(LiveWatchUser param) {
|
|
|
+ // 先查缓存,获取liveFlag和replayFlag
|
|
|
+ if (param != null && param.getLiveId() != null) {
|
|
|
+ Map<String, Integer> flagMap = getLiveFlagWithCache(param.getLiveId());
|
|
|
+ param.setLiveFlag(flagMap.get("liveFlag"));
|
|
|
+ param.setReplayFlag(flagMap.get("replayFlag"));
|
|
|
+ }
|
|
|
return baseMapper.selectOnlineUserList(param);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<LiveWatchUser> checkOnlineNoRewardUser(Long liveId, Date now) {
|
|
|
- return baseMapper.checkOnlineNoRewardUser(liveId,now);
|
|
|
+ // 先查缓存,获取liveFlag和replayFlag
|
|
|
+ Map<String, Integer> flagMap = getLiveFlagWithCache(liveId);
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("liveId", liveId);
|
|
|
+ params.put("now", now);
|
|
|
+ params.put("liveFlag", flagMap.get("liveFlag"));
|
|
|
+ params.put("replayFlag", flagMap.get("replayFlag"));
|
|
|
+ return baseMapper.checkOnlineNoRewardUser(params);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -423,7 +414,14 @@ public class LiveWatchUserServiceImpl implements ILiveWatchUserService {
|
|
|
|
|
|
@Override
|
|
|
public List<LiveWatchUser> selectLiveWatchAndRegisterUser(Long liveId, Long lotteryId) {
|
|
|
- return baseMapper.selectLiveWatchAndRegisterUser(liveId, lotteryId);
|
|
|
+ // 先查缓存,获取liveFlag和replayFlag
|
|
|
+ Map<String, Integer> flagMap = getLiveFlagWithCache(liveId);
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("liveId", liveId);
|
|
|
+ params.put("lotteryId", lotteryId);
|
|
|
+ params.put("liveFlag", flagMap.get("liveFlag"));
|
|
|
+ params.put("replayFlag", flagMap.get("replayFlag"));
|
|
|
+ return baseMapper.selectLiveWatchAndRegisterUser(params);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -480,6 +478,13 @@ public class LiveWatchUserServiceImpl implements ILiveWatchUserService {
|
|
|
|
|
|
@Override
|
|
|
public List<LiveWatchUserVO> dashBoardWatchUserList(Map<String, Object> param) {
|
|
|
+ // 先查缓存,获取liveFlag和replayFlag
|
|
|
+ if (param != null && param.containsKey("liveId")) {
|
|
|
+ Long liveId = Long.valueOf(param.get("liveId").toString());
|
|
|
+ Map<String, Integer> flagMap = getLiveFlagWithCache(liveId);
|
|
|
+ param.put("liveFlag", flagMap.get("liveFlag"));
|
|
|
+ param.put("replayFlag", flagMap.get("replayFlag"));
|
|
|
+ }
|
|
|
if (param != null && param.containsKey("all") && "1".equals(param.get("all"))) {
|
|
|
return baseMapper.selectWatchUserListAllByLiveId(param);
|
|
|
} else {
|
|
|
@@ -500,4 +505,9 @@ public class LiveWatchUserServiceImpl implements ILiveWatchUserService {
|
|
|
baseMapper.updateSingleVisible(liveId, status,userId);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public LiveWatchUser selectLiveWatchUserByFlag(Long liveId, Long userId, Integer liveFlag, Integer replayFlag) {
|
|
|
+ return baseMapper.selectByUniqueIndex(liveId, userId, liveFlag, replayFlag);
|
|
|
+ }
|
|
|
+
|
|
|
}
|