|
@@ -95,6 +95,12 @@ public class LiveWatchUserServiceImpl implements ILiveWatchUserService {
|
|
|
} else if ("silenced".equals(liveWatchUser.getTabName())) {
|
|
} else if ("silenced".equals(liveWatchUser.getTabName())) {
|
|
|
liveWatchUser.setMsgStatus(1);
|
|
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);
|
|
return baseMapper.selectLiveWatchUserList(liveWatchUser);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -149,8 +155,48 @@ public class LiveWatchUserServiceImpl implements ILiveWatchUserService {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
- public LiveWatchUser getByLiveIdAndUserId(long liveId, long userId) {
|
|
|
|
|
- return baseMapper.selectUserByLiveIdAndUserId(liveId, userId);
|
|
|
|
|
|
|
+ public List<LiveWatchUser> getByLiveIdAndUserId(long liveId, long 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
|
|
@Override
|
|
@@ -164,37 +210,10 @@ public class LiveWatchUserServiceImpl implements ILiveWatchUserService {
|
|
|
throw new RuntimeException("直播间不存在");
|
|
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
|
|
// 使用唯一索引查询:live_id, user_id, live_flag, replay_flag
|
|
|
LiveWatchUser liveWatchUser = baseMapper.selectByUniqueIndex(liveId, userId, liveFlag, replayFlag);
|
|
LiveWatchUser liveWatchUser = baseMapper.selectByUniqueIndex(liveId, userId, liveFlag, replayFlag);
|
|
@@ -232,62 +251,65 @@ 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);
|
|
|
-
|
|
|
|
|
- // 判断用户进入时间:如果进入时间大于直播结束时间,说明是回放
|
|
|
|
|
- boolean isReplay = false;
|
|
|
|
|
- if (live != null && live.getFinishTime() != null) {
|
|
|
|
|
- Date finishTime = java.sql.Timestamp.valueOf(live.getFinishTime());
|
|
|
|
|
- isReplay = now.after(finishTime);
|
|
|
|
|
|
|
+ if (live == null) {
|
|
|
|
|
+ throw new RuntimeException("直播间不存在");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if(liveWatchUser != null) {
|
|
|
|
|
- liveWatchUser.setUpdateTime(now);
|
|
|
|
|
- liveWatchUser.setOnline(0);
|
|
|
|
|
|
|
+ // 获取直播/回放状态(带缓存)
|
|
|
|
|
+ Map<String, Integer> flagMap = getLiveFlagWithCache(liveId);
|
|
|
|
|
+ Integer liveFlag = flagMap.get("liveFlag");
|
|
|
|
|
+ Integer replayFlag = flagMap.get("replayFlag");
|
|
|
|
|
|
|
|
- // 更新进入标记
|
|
|
|
|
- if (isReplay) {
|
|
|
|
|
- liveWatchUser.setReplayFlag(1);
|
|
|
|
|
- } else {
|
|
|
|
|
- liveWatchUser.setLiveFlag(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);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 查询直播间信息
|
|
|
|
|
+ Live live = liveMapper.selectLiveByLiveId(liveId);
|
|
|
|
|
+ if (live == null) {
|
|
|
|
|
+ throw new RuntimeException("直播间不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 获取直播/回放状态(带缓存)
|
|
|
|
|
+ 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);
|
|
|
// 设置在线时长
|
|
// 设置在线时长
|
|
|
try {
|
|
try {
|
|
|
Long onlineSeconds = liveWatchUser.getOnlineSeconds();
|
|
Long onlineSeconds = liveWatchUser.getOnlineSeconds();
|
|
@@ -311,6 +333,13 @@ public class LiveWatchUserServiceImpl implements ILiveWatchUserService {
|
|
|
*/
|
|
*/
|
|
|
@Override
|
|
@Override
|
|
|
public List<LiveWatchUserVO> selectWatchUserList(Map<String, Object> params) {
|
|
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);
|
|
return baseMapper.selectWatchUserListByLiveId(params);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -322,11 +351,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;
|
|
|
}
|
|
}
|
|
@@ -339,17 +371,37 @@ public class LiveWatchUserServiceImpl implements ILiveWatchUserService {
|
|
|
*/
|
|
*/
|
|
|
@Override
|
|
@Override
|
|
|
public LiveWatchUserVO selectWatchUserByLiveIdAndUserId(Long liveId, Long userId) {
|
|
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
|
|
@Override
|
|
|
public List<LiveWatchUserVO> selectOnlineUserList(LiveWatchUser param) {
|
|
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);
|
|
return baseMapper.selectOnlineUserList(param);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public List<LiveWatchUser> checkOnlineNoRewardUser(Long liveId, Date now) {
|
|
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
|
|
@Override
|
|
@@ -362,7 +414,14 @@ public class LiveWatchUserServiceImpl implements ILiveWatchUserService {
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public List<LiveWatchUser> selectLiveWatchAndRegisterUser(Long liveId, Long lotteryId) {
|
|
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
|
|
@Override
|
|
@@ -419,6 +478,13 @@ public class LiveWatchUserServiceImpl implements ILiveWatchUserService {
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public List<LiveWatchUserVO> dashBoardWatchUserList(Map<String, Object> param) {
|
|
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"))) {
|
|
if (param != null && param.containsKey("all") && "1".equals(param.get("all"))) {
|
|
|
return baseMapper.selectWatchUserListAllByLiveId(param);
|
|
return baseMapper.selectWatchUserListAllByLiveId(param);
|
|
|
} else {
|
|
} else {
|
|
@@ -439,4 +505,9 @@ public class LiveWatchUserServiceImpl implements ILiveWatchUserService {
|
|
|
baseMapper.updateSingleVisible(liveId, status,userId);
|
|
baseMapper.updateSingleVisible(liveId, status,userId);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public LiveWatchUser selectLiveWatchUserByFlag(Long liveId, Long userId, Integer liveFlag, Integer replayFlag) {
|
|
|
|
|
+ return baseMapper.selectByUniqueIndex(liveId, userId, liveFlag, replayFlag);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|