|
|
@@ -8,6 +8,7 @@ import com.fs.common.constant.LiveKeysConstant;
|
|
|
import com.fs.common.core.domain.R;
|
|
|
import com.fs.common.core.redis.RedisCache;
|
|
|
import com.fs.common.utils.StringUtils;
|
|
|
+import com.fs.common.utils.redis.LiveDelayedTaskRedisUtil;
|
|
|
import com.fs.framework.aspectj.lock.DistributeLock;
|
|
|
import com.fs.erp.service.FsJstAftersalePushService;
|
|
|
import com.fs.his.service.IFsUserService;
|
|
|
@@ -82,6 +83,8 @@ public class Task {
|
|
|
|
|
|
@Autowired
|
|
|
public FsJstAftersalePushService fsJstAftersalePushService;
|
|
|
+ @Autowired
|
|
|
+ private LiveDelayedTaskRedisUtil liveDelayedTaskRedisUtil;
|
|
|
|
|
|
@Scheduled(cron = "0 0/1 * * * ?")
|
|
|
@DistributeLock(key = "updateLiveStatusByTime", scene = "task")
|
|
|
@@ -155,7 +158,6 @@ public class Task {
|
|
|
liveService.updateLiveEntity(live);
|
|
|
}
|
|
|
}
|
|
|
- String key = "live:auto_task:";
|
|
|
if (!startLiveList.isEmpty()) {
|
|
|
for (Live live : startLiveList) {
|
|
|
SendMsgVo sendMsgVo = new SendMsgVo();
|
|
|
@@ -164,12 +166,14 @@ public class Task {
|
|
|
webSocketServer.broadcastMessage(live.getLiveId(), JSONObject.toJSONString(R.ok().put("data",sendMsgVo)));
|
|
|
List<LiveAutoTask> collect = liveAutoTasks.stream().filter(liveAutoTask -> liveAutoTask.getLiveId().equals(live.getLiveId())).collect(Collectors.toList());
|
|
|
if (!collect.isEmpty()) {
|
|
|
+ String autoTaskKey = liveDelayedTaskRedisUtil.autoTaskKey(live.getLiveId());
|
|
|
collect.forEach(liveAutoTask -> {
|
|
|
liveAutoTask.setCreateTime(null);
|
|
|
liveAutoTask.setUpdateTime(null);
|
|
|
- redisCache.zSetAdd(key + live.getLiveId(), JSON.toJSONString(liveAutoTask),liveAutoTask.getAbsValue().getTime());
|
|
|
- redisCache.expire(key+live.getLiveId(), 1, TimeUnit.DAYS);
|
|
|
+ redisCache.zSetAdd(autoTaskKey, JSON.toJSONString(liveAutoTask), liveAutoTask.getAbsValue().getTime());
|
|
|
+ redisCache.expire(autoTaskKey, 1, TimeUnit.DAYS);
|
|
|
});
|
|
|
+ liveDelayedTaskRedisUtil.trackAutoTask(live.getLiveId());
|
|
|
}
|
|
|
// 清理小程序缓存 和 直播标签缓存
|
|
|
String cacheKey = String.format(LiveKeysConstant.LIVE_DATA_CACHE, live.getLiveId());
|
|
|
@@ -214,12 +218,9 @@ public class Task {
|
|
|
webSocketServer.broadcastMessage(live.getLiveId(), JSONObject.toJSONString(R.ok().put("data",sendMsgVo)));
|
|
|
List<LiveAutoTask> collect = liveAutoTasks.stream().filter(liveAutoTask -> liveAutoTask.getLiveId().equals(live.getLiveId())).collect(Collectors.toList());
|
|
|
if (!collect.isEmpty()) {
|
|
|
- redisCache.deleteObject(key + live.getLiveId());
|
|
|
- collect.forEach(liveAutoTask -> {
|
|
|
- liveAutoTask.setCreateTime(null);
|
|
|
- liveAutoTask.setUpdateTime(null);
|
|
|
- redisCache.redisTemplate.opsForZSet().remove(key + live.getLiveId(), JSON.toJSONString(liveAutoTask),liveAutoTask.getAbsValue().getTime());
|
|
|
- });
|
|
|
+ String autoTaskKey = liveDelayedTaskRedisUtil.autoTaskKey(live.getLiveId());
|
|
|
+ redisCache.deleteObject(autoTaskKey);
|
|
|
+ liveDelayedTaskRedisUtil.untrackAutoTask(live.getLiveId());
|
|
|
}
|
|
|
String cacheKey = String.format(LiveKeysConstant.LIVE_DATA_CACHE, live.getLiveId());
|
|
|
redisCache.deleteObject(cacheKey);
|
|
|
@@ -243,42 +244,41 @@ public class Task {
|
|
|
@DistributeLock(key = "liveLotteryTask", scene = "task")
|
|
|
public void liveLotteryTask() {
|
|
|
long currentTime = Instant.now().toEpochMilli(); // 当前时间戳(毫秒)
|
|
|
- String lotteryKey = "live:lottery_task:*";
|
|
|
- Set<String> allLiveKeys = redisCache.redisTemplate.keys(lotteryKey);
|
|
|
- if (allLiveKeys != null && !allLiveKeys.isEmpty()) {
|
|
|
- for (String liveKey : allLiveKeys) {
|
|
|
- Set<String> range = redisCache.redisTemplate.opsForZSet().rangeByScore(liveKey, 0, currentTime);
|
|
|
- if (range == null || range.isEmpty()) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- processLotteryTask(range);
|
|
|
- redisCache.redisTemplate.opsForZSet()
|
|
|
- .removeRangeByScore(liveKey, 0, currentTime);
|
|
|
+ Set<String> lotteryZSetKeys = liveDelayedTaskRedisUtil.listLotteryTaskZSetKeys();
|
|
|
+ for (String liveKey : lotteryZSetKeys) {
|
|
|
+ Set<String> range = redisCache.redisTemplate.opsForZSet().rangeByScore(liveKey, 0, currentTime);
|
|
|
+ if (range == null || range.isEmpty()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ processLotteryTask(range);
|
|
|
+ redisCache.redisTemplate.opsForZSet().removeRangeByScore(liveKey, 0, currentTime);
|
|
|
+ Long liveId = LiveKeysConstant.parseLiveIdFromTaskZSetKey(liveKey);
|
|
|
+ if (liveId != null) {
|
|
|
+ liveDelayedTaskRedisUtil.refreshLotteryTaskIndex(liveId);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- String redKey = "live:red_task:*";
|
|
|
- allLiveKeys = redisCache.redisTemplate.keys(redKey);
|
|
|
- if (allLiveKeys == null || allLiveKeys.isEmpty()) {
|
|
|
- return;
|
|
|
- }
|
|
|
- for (String liveKey : allLiveKeys) {
|
|
|
+ Set<String> redZSetKeys = liveDelayedTaskRedisUtil.listRedTaskZSetKeys();
|
|
|
+ for (String liveKey : redZSetKeys) {
|
|
|
Set<String> range = redisCache.redisTemplate.opsForZSet().rangeByScore(liveKey, 0, currentTime);
|
|
|
if (range == null || range.isEmpty()) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
updateRedStatus(range);
|
|
|
- redisCache.redisTemplate.opsForZSet()
|
|
|
- .removeRangeByScore(liveKey, 0, currentTime);
|
|
|
+ redisCache.redisTemplate.opsForZSet().removeRangeByScore(liveKey, 0, currentTime);
|
|
|
+ Long liveId = LiveKeysConstant.parseLiveIdFromTaskZSetKey(liveKey);
|
|
|
+ if (liveId != null) {
|
|
|
+ liveDelayedTaskRedisUtil.refreshRedTaskIndex(liveId);
|
|
|
+ }
|
|
|
try {
|
|
|
// 广播红包关闭消息
|
|
|
SendMsgVo sendMsgVo = new SendMsgVo();
|
|
|
- sendMsgVo.setLiveId(Long.valueOf(liveKey));
|
|
|
+ sendMsgVo.setLiveId(liveId);
|
|
|
sendMsgVo.setCmd("red");
|
|
|
sendMsgVo.setStatus(-1);
|
|
|
- liveService.asyncToCacheLiveConfig(Long.parseLong(liveKey));
|
|
|
- webSocketServer.broadcastMessage(Long.valueOf(liveKey), JSONObject.toJSONString(R.ok().put("data", sendMsgVo)));
|
|
|
+ liveService.asyncToCacheLiveConfig(liveId);
|
|
|
+ webSocketServer.broadcastMessage(liveId, JSONObject.toJSONString(R.ok().put("data", sendMsgVo)));
|
|
|
} catch (Exception e) {
|
|
|
log.error("更新红包状态异常", e);
|
|
|
}
|
|
|
@@ -374,21 +374,18 @@ public class Task {
|
|
|
public void liveAutoTask() {
|
|
|
long currentTime = Instant.now().toEpochMilli(); // 当前时间戳(毫秒)
|
|
|
|
|
|
- Set<String> allLiveKeys = redisCache.redisTemplate.keys("live:auto_task:*");
|
|
|
- if (allLiveKeys == null || allLiveKeys.isEmpty()) {
|
|
|
- return; // 没有数据,直接返回
|
|
|
- }
|
|
|
- // 2. 遍历每个直播间的ZSet键
|
|
|
- for (String liveKey : allLiveKeys) {
|
|
|
- // 3. 获取当前直播间ZSet中所有元素(按score排序)
|
|
|
- // range方法:0表示第一个元素,-1表示最后一个元素,即获取全部
|
|
|
+ Set<String> autoZSetKeys = liveDelayedTaskRedisUtil.listAutoTaskZSetKeys();
|
|
|
+ for (String liveKey : autoZSetKeys) {
|
|
|
Set<String> range = redisCache.redisTemplate.opsForZSet().rangeByScore(liveKey, 0, currentTime);
|
|
|
if (range == null || range.isEmpty()) {
|
|
|
- continue; // 没有数据,直接返回
|
|
|
+ continue;
|
|
|
}
|
|
|
- redisCache.redisTemplate.opsForZSet()
|
|
|
- .removeRangeByScore(liveKey, 0, currentTime);
|
|
|
+ redisCache.redisTemplate.opsForZSet().removeRangeByScore(liveKey, 0, currentTime);
|
|
|
processAutoTask(range);
|
|
|
+ Long liveId = LiveKeysConstant.parseLiveIdFromTaskZSetKey(liveKey);
|
|
|
+ if (liveId != null) {
|
|
|
+ liveDelayedTaskRedisUtil.refreshAutoTaskIndex(liveId);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|