|
|
@@ -51,6 +51,9 @@ import java.time.LocalDateTime;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
+import static com.fs.common.constant.LiveKeysConstant.*;
|
|
|
+import static com.fs.common.constant.LiveKeysConstant.LIVE_COUPON_NUM;
|
|
|
+
|
|
|
@Component
|
|
|
@AllArgsConstructor
|
|
|
public class Task {
|
|
|
@@ -80,6 +83,8 @@ public class Task {
|
|
|
private LiveLotteryRegistrationMapper liveLotteryRegistrationMapper;
|
|
|
@Autowired
|
|
|
private ILiveRedConfService liveRedConfService;
|
|
|
+ @Autowired
|
|
|
+ private ILiveCouponIssueService liveCouponIssueService;
|
|
|
|
|
|
@Autowired
|
|
|
public FsJstAftersalePushService fsJstAftersalePushService;
|
|
|
@@ -402,6 +407,74 @@ public class Task {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ *定期将缓存的数据写入数据库
|
|
|
+ */
|
|
|
+ @Scheduled(fixedRate = 60000) // 每分钟执行一次
|
|
|
+ public void syncLiveDataToDB() {
|
|
|
+ List<LiveData> liveDatas = liveDataService.getAllLiveDatas(); // 获取所有正在直播的直播间数据
|
|
|
+ if(liveDatas == null)
|
|
|
+ return;
|
|
|
+ liveDatas.forEach(liveData ->{
|
|
|
+ Long likeCount = Long.valueOf(redisCache.getCacheObject("live:like:" + liveData.getLiveId()));
|
|
|
+ long resultLikeCount = liveData.getLikes() + (Objects.isNull(likeCount) ? 0L : likeCount);
|
|
|
+ redisCache.setCacheObject("live:like:" + liveData.getLiveId(), resultLikeCount);
|
|
|
+ liveData.setLikes(
|
|
|
+ resultLikeCount
|
|
|
+ );
|
|
|
+
|
|
|
+ /* for (Long liveId : liveIds) {
|
|
|
+ LiveData liveData = liveDataService.selectLiveDataByLiveId(liveId);
|
|
|
+ if (liveData == null) {
|
|
|
+ continue; // 防止空指针异常
|
|
|
+ }*/
|
|
|
+
|
|
|
+
|
|
|
+ // 从 redis 获取数据,并提供默认值,避免 NPE
|
|
|
+ liveData.setPageViews(
|
|
|
+ liveData.getPageViews() + Optional.ofNullable(redisCache.incrementCacheValue(PAGE_VIEWS_KEY + liveData.getLiveId(),0)).orElse(0L)
|
|
|
+ );
|
|
|
+ liveData.setTotalViews(
|
|
|
+ liveData.getTotalViews() + Optional.ofNullable(redisCache.incrementCacheValue(TOTAL_VIEWS_KEY + liveData.getLiveId(),0)).orElse(0L)
|
|
|
+ );
|
|
|
+ liveData.setUniqueVisitors(
|
|
|
+ /*Optional.ofNullable(redisCache.getCacheSet(UNIQUE_VISITORS_KEY + liveId))
|
|
|
+ .map(Set::size) // 获取集合大小
|
|
|
+ .map(Long::valueOf) // 转换为 Long 类型
|
|
|
+ .orElse(0L)*/
|
|
|
+ liveData.getUniqueVisitors() + Optional.ofNullable(redisCache.incrementCacheValue(UNIQUE_VISITORS_KEY + liveData.getLiveId(),0)).orElse(0L)
|
|
|
+ );
|
|
|
+ liveData.setUniqueViewers(
|
|
|
+ /*Optional.ofNullable(redisCache.getCacheSet(UNIQUE_VIEWERS_KEY + liveId))
|
|
|
+ .map(Set::size) // 获取集合大小
|
|
|
+ .map(Long::valueOf) // 转换为 Long 类型
|
|
|
+ .orElse(0L)*/
|
|
|
+ liveData.getUniqueViewers() + Optional.ofNullable(redisCache.incrementCacheValue(UNIQUE_VIEWERS_KEY + liveData.getLiveId(),0)).orElse(0L)
|
|
|
+ );
|
|
|
+ liveData.setPeakConcurrentViewers(
|
|
|
+ liveData.getPeakConcurrentViewers() + Optional.ofNullable(redisCache.incrementCacheValue(MAX_ONLINE_USERS_KEY + liveData.getLiveId(),0)).orElse(0L)
|
|
|
+ );
|
|
|
+ });
|
|
|
+ if(!liveDatas.isEmpty())
|
|
|
+ for (LiveData liveData : liveDatas) {
|
|
|
+ liveDataService.updateLiveData(liveData);
|
|
|
+ }
|
|
|
+ /*// 更新数据库
|
|
|
+ liveDataService.updateLiveData(liveData);*/
|
|
|
+ Set<String> keys = redisCache.redisTemplate.keys(String.format(LIVE_COUPON_NUM, "*"));
|
|
|
+ if (keys != null && !keys.isEmpty()) {
|
|
|
+ for (String key : keys) {
|
|
|
+ Object o = redisCache.redisTemplate.opsForValue().get(String.format(LIVE_COUPON_NUM, key));
|
|
|
+ if (o != null) {
|
|
|
+ LiveCouponIssue updateEntity = new LiveCouponIssue();
|
|
|
+ updateEntity.setId(Long.valueOf(key));
|
|
|
+ updateEntity.setRemainCount(Long.parseLong(o.toString()));
|
|
|
+ liveCouponIssueService.updateLiveCouponIssue(updateEntity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 更新红包领取数量
|
|
|
*/
|