|
@@ -1,27 +1,33 @@
|
|
package com.fs.app.task;
|
|
package com.fs.app.task;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
+import com.fs.app.vo.LotteryVo;
|
|
|
|
+import com.fs.app.websocket.bean.SendMsgVo;
|
|
import com.fs.app.websocket.service.WebSocketServer;
|
|
import com.fs.app.websocket.service.WebSocketServer;
|
|
|
|
+import com.fs.common.core.domain.R;
|
|
import com.fs.common.core.redis.RedisCache;
|
|
import com.fs.common.core.redis.RedisCache;
|
|
import com.fs.common.utils.StringUtils;
|
|
import com.fs.common.utils.StringUtils;
|
|
import com.fs.common.utils.spring.SpringUtils;
|
|
import com.fs.common.utils.spring.SpringUtils;
|
|
import com.fs.his.service.IFsUserService;
|
|
import com.fs.his.service.IFsUserService;
|
|
import com.fs.live.domain.*;
|
|
import com.fs.live.domain.*;
|
|
|
|
+import com.fs.live.mapper.LiveLotteryRegistrationMapper;
|
|
import com.fs.live.service.*;
|
|
import com.fs.live.service.*;
|
|
|
|
+import com.fs.live.vo.LiveLotteryConfVo;
|
|
|
|
+import com.fs.live.vo.LiveLotteryProductListVo;
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.AllArgsConstructor;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
+import javax.websocket.Session;
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
import java.time.Instant;
|
|
import java.time.Instant;
|
|
import java.time.LocalDateTime;
|
|
import java.time.LocalDateTime;
|
|
-import java.util.ArrayList;
|
|
|
|
-import java.util.Date;
|
|
|
|
-import java.util.List;
|
|
|
|
-import java.util.Set;
|
|
|
|
|
|
+import java.util.*;
|
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Component
|
|
@Component
|
|
@@ -44,6 +50,12 @@ public class Task {
|
|
private WebSocketServer webSocketServer;
|
|
private WebSocketServer webSocketServer;
|
|
@Autowired
|
|
@Autowired
|
|
private ILiveAutoTaskService liveAutoTaskService;
|
|
private ILiveAutoTaskService liveAutoTaskService;
|
|
|
|
+ @Autowired
|
|
|
|
+ private ILiveLotteryConfService liveLotteryConfService;
|
|
|
|
+ @Autowired
|
|
|
|
+ private ILiveUserLotteryRecordService liveUserLotteryRecordService;
|
|
|
|
+ @Autowired
|
|
|
|
+ private LiveLotteryRegistrationMapper liveLotteryRegistrationMapper;
|
|
|
|
|
|
@Scheduled(cron = "0 0/1 * * * ?")
|
|
@Scheduled(cron = "0 0/1 * * * ?")
|
|
//public void selectSopUserLogsListByTime() {
|
|
//public void selectSopUserLogsListByTime() {
|
|
@@ -92,6 +104,77 @@ public class Task {
|
|
liveService.updateBatchById(list);
|
|
liveService.updateBatchById(list);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ @Scheduled(cron = "0/1 * * * * ?")
|
|
|
|
+ public void liveLotteryTask() {
|
|
|
|
+ long currentTime = Instant.now().toEpochMilli(); // 当前时间戳(毫秒)
|
|
|
|
+ String key = "live:lottery_task";
|
|
|
|
+
|
|
|
|
+ Set<String> range = redisCache.redisTemplate.opsForZSet().rangeByScore(key, 0, currentTime);
|
|
|
|
+ if (range == null || range.isEmpty()) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ redisCache.redisTemplate.opsForZSet()
|
|
|
|
+ .removeRangeByScore(key, 0, currentTime);
|
|
|
|
+ processLotteryTask(range);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void processLotteryTask(Set<String> range) {
|
|
|
|
+ List<LiveLotteryConfVo> liveLotteries = liveLotteryConfService.selectVoListByLotteryIds(range);
|
|
|
|
+ if(liveLotteries.isEmpty()) return;
|
|
|
|
+ Date now = new Date();
|
|
|
|
+ for (LiveLotteryConfVo liveLottery : liveLotteries) {
|
|
|
|
+ // 查询抽奖数量
|
|
|
|
+ List<LiveLotteryProductListVo> products = liveLottery.getProducts();
|
|
|
|
+ Integer totalLots = products.stream().mapToInt(liveLotteryProductListVo -> Math.toIntExact(liveLotteryProductListVo.getTotalLots())).sum();
|
|
|
|
+ if(totalLots <= 0) continue;
|
|
|
|
+ // 查询在线用户 并且参与了抽奖的用户
|
|
|
|
+ List<LiveWatchUser> liveWatchUsers = liveWatchUserService.selectLiveWatchAndRegisterUser(liveLottery.getLiveId(),liveLottery.getLotteryId(), totalLots);
|
|
|
|
+ if(liveWatchUsers.isEmpty()) continue;
|
|
|
|
+ LiveLotteryRegistration liveLotteryRegistration;
|
|
|
|
+ // 收集中奖信息
|
|
|
|
+ List<LotteryVo> lotteryVos = new ArrayList<>();
|
|
|
|
+ for (LiveLotteryProductListVo liveLotteryProductListVo : products) {
|
|
|
|
+ // 随机抽奖一个用户获取奖品
|
|
|
|
+ Long totalLotsPerProduct = liveLotteryProductListVo.getTotalLots();
|
|
|
|
+ for (int i = 0; i < totalLotsPerProduct && !liveWatchUsers.isEmpty(); i++) {
|
|
|
|
+ // 随机选择一个用户
|
|
|
|
+ int randomIndex = new java.util.Random().nextInt(liveWatchUsers.size());
|
|
|
|
+ LiveWatchUser winningUser = liveWatchUsers.get(randomIndex);
|
|
|
|
+
|
|
|
|
+ // 创建中奖记录
|
|
|
|
+ LiveUserLotteryRecord record = new LiveUserLotteryRecord();
|
|
|
|
+ record.setLotteryId(liveLottery.getLotteryId());
|
|
|
|
+ record.setLiveId(liveLottery.getLiveId());
|
|
|
|
+ record.setUserId(winningUser.getUserId());
|
|
|
|
+ record.setProductId(liveLotteryProductListVo.getProductId());
|
|
|
|
+ record.setCreateTime(new Date());
|
|
|
|
+
|
|
|
|
+ // 保存中奖记录
|
|
|
|
+ liveUserLotteryRecordService.insertLiveUserLotteryRecord(record);
|
|
|
|
+ liveLotteryRegistration = new LiveLotteryRegistration();
|
|
|
|
+ liveLotteryRegistration.setLotteryId(liveLottery.getLotteryId());
|
|
|
|
+ liveLotteryRegistration.setLiveId(liveLottery.getLotteryId());
|
|
|
|
+ liveLotteryRegistration.setUserId(winningUser.getUserId());
|
|
|
|
+ liveLotteryRegistration.setIsWin(1L);
|
|
|
|
+ liveLotteryRegistration.setUpdateTime(now);
|
|
|
|
+ liveLotteryRegistration.setRizeLevel(liveLotteryProductListVo.getPrizeLevel());
|
|
|
|
+ liveLotteryRegistrationMapper.updateLiveLotteryRegistrationNoId(liveLotteryRegistration);
|
|
|
|
+ // 从候选列表中移除该用户,确保每人只能中奖一次
|
|
|
|
+ liveWatchUsers.remove(randomIndex);
|
|
|
|
+ LotteryVo lotteryVo = new LotteryVo();
|
|
|
|
+ lotteryVo.setUserId(winningUser.getUserId());
|
|
|
|
+ lotteryVo.setPrizeLevel(liveLotteryProductListVo.getPrizeLevel());
|
|
|
|
+ lotteryVo.setProductName(liveLotteryProductListVo.getProductName());
|
|
|
|
+ lotteryVos.add(lotteryVo);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ SendMsgVo sendMsgVo = new SendMsgVo();
|
|
|
|
+ sendMsgVo.setLiveId(liveLottery.getLiveId());
|
|
|
|
+ sendMsgVo.setCmd("LotteryDetail");
|
|
|
|
+ sendMsgVo.setData(JSON.toJSONString(lotteryVos));
|
|
|
|
+ webSocketServer.broadcastMessage(liveLottery.getLotteryId(), JSONObject.toJSONString(R.ok().put("data", sendMsgVo)));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
@Scheduled(cron = "0/1 * * * * ?")
|
|
@Scheduled(cron = "0/1 * * * * ?")
|
|
public void liveAutoTask() {
|
|
public void liveAutoTask() {
|