|
@@ -78,6 +78,10 @@ public class Task {
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private ILiveWatchLogService liveWatchLogService;
|
|
private ILiveWatchLogService liveWatchLogService;
|
|
|
@Autowired
|
|
@Autowired
|
|
|
|
|
+ private ILiveCouponUserService liveCouponUserService;
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private ILiveCouponService liveCouponService;
|
|
|
|
|
+ @Autowired
|
|
|
private ILiveUserFirstEntryService liveUserFirstEntryService;
|
|
private ILiveUserFirstEntryService liveUserFirstEntryService;
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
@@ -428,44 +432,198 @@ public class Task {
|
|
|
for (Live openRewardLive : openRewardLives) {
|
|
for (Live openRewardLive : openRewardLives) {
|
|
|
String configJson = openRewardLive.getConfigJson();
|
|
String configJson = openRewardLive.getConfigJson();
|
|
|
LiveWatchConfig config = JSON.parseObject(configJson, LiveWatchConfig.class);
|
|
LiveWatchConfig config = JSON.parseObject(configJson, LiveWatchConfig.class);
|
|
|
- if (config.getEnabled() && 1 == config.getParticipateCondition()) {
|
|
|
|
|
- List<LiveWatchUser> liveWatchUsers = liveWatchUserService.checkOnlineNoRewardUser(openRewardLive.getLiveId(), now);
|
|
|
|
|
- if (liveWatchUsers == null || liveWatchUsers.isEmpty()) {
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
- // 3.检查当前直播间的在线用户(可以传入一个时间,然后查出来当天没领取奖励的用户)
|
|
|
|
|
- List<LiveWatchUser> onlineUser = liveWatchUsers
|
|
|
|
|
- .stream().filter(user -> (now.getTime() - user.getUpdateTime().getTime() + ( user.getOnlineSeconds() == null ? 0L : user.getOnlineSeconds())) > config.getWatchDuration() * 60 * 1000)
|
|
|
|
|
- .collect(Collectors.toList());
|
|
|
|
|
- if(onlineUser.isEmpty()) continue;
|
|
|
|
|
|
|
+ if (!config.getEnabled() || config.getParticipateCondition() == null || config.getAction() == null) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ // 只处理 "达到指定观看时长" 的参与条件
|
|
|
|
|
+ if (1 != config.getParticipateCondition()) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ List<LiveWatchUser> liveWatchUsers = liveWatchUserService.checkOnlineNoRewardUser(openRewardLive.getLiveId(), now);
|
|
|
|
|
+ if (liveWatchUsers == null || liveWatchUsers.isEmpty()) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ // 3.检查当前直播间的在线用户(可以传入一个时间,然后查出来当天没领取奖励的用户)
|
|
|
|
|
+ List<LiveWatchUser> onlineUser = liveWatchUsers
|
|
|
|
|
+ .stream().filter(user -> (now.getTime() - user.getUpdateTime().getTime() + (user.getOnlineSeconds() == null ? 0L : user.getOnlineSeconds())) > config.getWatchDuration() * 60 * 1000)
|
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
+ if (onlineUser.isEmpty()) continue;
|
|
|
|
|
|
|
|
- List<Long> userIds = onlineUser.stream().map(LiveWatchUser::getUserId).collect(Collectors.toList());
|
|
|
|
|
- // 4.保存用户领取记录
|
|
|
|
|
- saveUserRewardRecord(openRewardLive, userIds,config.getScoreAmount());
|
|
|
|
|
- // 5.更新用户积分(积分
|
|
|
|
|
- fsUserService.increaseIntegral(userIds,config.getScoreAmount());
|
|
|
|
|
- // 6.发送websocket事件消息 通知用户自动领取成功
|
|
|
|
|
- userIds.forEach(userId -> webSocketServer.sendIntegralMessage(openRewardLive.getLiveId(),userId,config.getScoreAmount()));
|
|
|
|
|
|
|
+ List<Long> userIds = onlineUser.stream().map(LiveWatchUser::getUserId).collect(Collectors.toList());
|
|
|
|
|
|
|
|
|
|
+ // 根据 action 类型处理不同的奖励
|
|
|
|
|
+ Long action = config.getAction();
|
|
|
|
|
+ if (action == null) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ switch (action.intValue()) {
|
|
|
|
|
+ case 2: // 积分红包
|
|
|
|
|
+ // 4.保存用户领取记录
|
|
|
|
|
+ saveUserRewardRecord(openRewardLive, userIds, BigDecimal.valueOf(config.getScoreAmount()), 2);
|
|
|
|
|
+ // 5.更新用户积分
|
|
|
|
|
+ fsUserService.increaseIntegral(userIds, config.getScoreAmount());
|
|
|
|
|
+ // 6.发送websocket事件消息 通知用户自动领取成功
|
|
|
|
|
+ userIds.forEach(userId -> webSocketServer.sendIntegralMessage(openRewardLive.getLiveId(), userId, config.getScoreAmount()));
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ case 3: // 优惠券
|
|
|
|
|
+ // 获取配置的优惠券ID
|
|
|
|
|
+ String actionCouponIdStr = config.getActionCouponId();
|
|
|
|
|
+ if (StringUtils.isBlank(actionCouponIdStr)) {
|
|
|
|
|
+ log.warn("直播间观看奖励配置为优惠券,但未配置优惠券ID,liveId={}", openRewardLive.getLiveId());
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ Long actionCouponId = Long.parseLong(actionCouponIdStr);
|
|
|
|
|
+ bindCouponToUsers(openRewardLive, userIds, actionCouponId);
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ case 1: // 现金红包 - 暂不处理(现有逻辑)
|
|
|
|
|
+ default:
|
|
|
|
|
+ log.info("观看奖励类型 {} 暂不处理,liveId={}", action, openRewardLive.getLiveId());
|
|
|
|
|
+ break;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- private void saveUserRewardRecord(Live live, List<Long> userIds,Long scoreAmount) {
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 将优惠券绑定到用户
|
|
|
|
|
+ * @param live 直播间
|
|
|
|
|
+ * @param userIds 用户ID列表
|
|
|
|
|
+ * @param couponId 优惠券ID
|
|
|
|
|
+ */
|
|
|
|
|
+ private void bindCouponToUsers(Live live, List<Long> userIds, Long couponId) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 查询优惠券信息
|
|
|
|
|
+ LiveCoupon coupon = liveCouponService.selectLiveCouponById(couponId);
|
|
|
|
|
+ if (coupon == null) {
|
|
|
|
|
+ log.error("优惠券不存在,couponId={}", couponId);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 查询优惠券领取信息
|
|
|
|
|
+ LiveCouponIssue couponIssue = liveCouponIssueService.selectLiveCouponIssueByCouponId(couponId);
|
|
|
|
|
+ if (couponIssue == null) {
|
|
|
|
|
+ log.error("优惠券领取信息不存在,couponId={}", couponId);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 检查优惠券状态
|
|
|
|
|
+ if (couponIssue.getStatus() == null || couponIssue.getStatus() != 1) {
|
|
|
|
|
+ log.error("优惠券状态不正常,couponId={}, status={}", couponId, couponIssue.getStatus());
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Date now = new Date();
|
|
|
|
|
+ int successCount = 0;
|
|
|
|
|
+
|
|
|
|
|
+ for (Long userId : userIds) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 检查用户是否已领取过该优惠券
|
|
|
|
|
+ LiveCouponUser query = new LiveCouponUser();
|
|
|
|
|
+ query.setCouponId(couponId);
|
|
|
|
|
+ query.setUserId(userId.intValue());
|
|
|
|
|
+ List<LiveCouponUser> existingList = liveCouponUserService.selectLiveCouponUserList(query);
|
|
|
|
|
+ if (existingList != null && !existingList.isEmpty()) {
|
|
|
|
|
+ log.info("用户已领取过该优惠券,跳过,userId={}, couponId={}", userId, couponId);
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 创建用户优惠券记录
|
|
|
|
|
+ LiveCouponUser couponUser = new LiveCouponUser();
|
|
|
|
|
+ couponUser.setCouponId(couponId);
|
|
|
|
|
+ couponUser.setUserId(userId.intValue());
|
|
|
|
|
+ couponUser.setCouponTitle(coupon.getTitle());
|
|
|
|
|
+ couponUser.setCouponPrice(coupon.getCouponPrice());
|
|
|
|
|
+ couponUser.setUseMinPrice(coupon.getUseMinPrice());
|
|
|
|
|
+
|
|
|
|
|
+ // 计算优惠券过期时间
|
|
|
|
|
+ if (couponIssue.getLimitTime() != null) {
|
|
|
|
|
+ couponUser.setLimitTime(couponIssue.getLimitTime());
|
|
|
|
|
+ } else if (coupon.getCouponTime() != null) {
|
|
|
|
|
+ // 如果没有设置领取结束时间,使用优惠券有效期限计算
|
|
|
|
|
+ java.util.Calendar cal = java.util.Calendar.getInstance();
|
|
|
|
|
+ cal.setTime(now);
|
|
|
|
|
+ cal.add(java.util.Calendar.DATE, coupon.getCouponTime().intValue());
|
|
|
|
|
+ couponUser.setLimitTime(cal.getTime());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ couponUser.setType("3"); // 获取方式:3-观看奖励
|
|
|
|
|
+ couponUser.setStatus(0); // 状态:0-未核销
|
|
|
|
|
+ couponUser.setIsFail(0); // 是否有效:0-有效
|
|
|
|
|
+ couponUser.setIsDel(0);
|
|
|
|
|
+ couponUser.setCreateTime(now);
|
|
|
|
|
+
|
|
|
|
|
+ liveCouponUserService.insertLiveCouponUser(couponUser);
|
|
|
|
|
+
|
|
|
|
|
+ // 保存奖励记录
|
|
|
|
|
+ saveUserRewardRecord(live, Collections.singletonList(userId), coupon.getCouponPrice(), 3);
|
|
|
|
|
+
|
|
|
|
|
+ successCount++;
|
|
|
|
|
+
|
|
|
|
|
+ // 发送WebSocket消息通知用户
|
|
|
|
|
+ sendCouponRewardMessage(live.getLiveId(), userId, coupon);
|
|
|
|
|
+
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("绑定优惠券到用户失败,userId={}, couponId={}", userId, couponId, e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ log.info("直播间观看奖励-优惠券发放完成,liveId={}, couponId={}, 成功发放 {} 个用户",
|
|
|
|
|
+ live.getLiveId(), couponId, successCount);
|
|
|
|
|
+
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("绑定优惠券到用户异常,liveId={}, couponId={}", live.getLiveId(), couponId, e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 发送优惠券奖励消息给前端
|
|
|
|
|
+ */
|
|
|
|
|
+ private void sendCouponRewardMessage(Long liveId, Long userId, LiveCoupon coupon) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ SendMsgVo sendMsgVo = new SendMsgVo();
|
|
|
|
|
+ sendMsgVo.setLiveId(liveId);
|
|
|
|
|
+ sendMsgVo.setUserId(userId);
|
|
|
|
|
+ sendMsgVo.setUserType(0L);
|
|
|
|
|
+ sendMsgVo.setCmd("watchRewardCoupon");
|
|
|
|
|
+ sendMsgVo.setMsg("恭喜你获得观看奖励优惠券:" + coupon.getTitle());
|
|
|
|
|
+
|
|
|
|
|
+ // 构建优惠券信息
|
|
|
|
|
+ JSONObject couponData = new JSONObject();
|
|
|
|
|
+ couponData.put("couponId", coupon.getCouponId());
|
|
|
|
|
+ couponData.put("title", coupon.getTitle());
|
|
|
|
|
+ couponData.put("couponPrice", coupon.getCouponPrice());
|
|
|
|
|
+ couponData.put("useMinPrice", coupon.getUseMinPrice());
|
|
|
|
|
+ couponData.put("couponTime", coupon.getCouponTime());
|
|
|
|
|
+ sendMsgVo.setData(couponData.toJSONString());
|
|
|
|
|
+
|
|
|
|
|
+ webSocketServer.sendCompletionPointsMessage(liveId, userId, sendMsgVo);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("发送优惠券奖励消息失败,liveId={}, userId={}", liveId, userId, e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ private void saveUserRewardRecord(Live live, List<Long> userIds, BigDecimal amount, int rewardType) {
|
|
|
for (Long userId : userIds) {
|
|
for (Long userId : userIds) {
|
|
|
LiveRewardRecord record = new LiveRewardRecord();
|
|
LiveRewardRecord record = new LiveRewardRecord();
|
|
|
record.setLiveId(live.getLiveId());
|
|
record.setLiveId(live.getLiveId());
|
|
|
record.setUserId(userId);
|
|
record.setUserId(userId);
|
|
|
- record.setIncomeType(1L);
|
|
|
|
|
- record.setSourceType(3L);
|
|
|
|
|
|
|
+ record.setIncomeType(1L); // 收入
|
|
|
|
|
+ record.setSourceType(3L); // 观看奖励
|
|
|
record.setSourceId(live.getCompanyId() == null ? 0L : live.getCompanyId());
|
|
record.setSourceId(live.getCompanyId() == null ? 0L : live.getCompanyId());
|
|
|
- record.setRewardType(2L);
|
|
|
|
|
- record.setNum(BigDecimal.valueOf(scoreAmount));
|
|
|
|
|
- record.setRewardType(2L);
|
|
|
|
|
|
|
+ record.setRewardType((long) rewardType); // 1-现金 2-积分 3-优惠券
|
|
|
|
|
+ record.setNum(amount);
|
|
|
record.setCreateTime(new Date());
|
|
record.setCreateTime(new Date());
|
|
|
record.setCreateBy(String.valueOf(userId));
|
|
record.setCreateBy(String.valueOf(userId));
|
|
|
liveRewardRecordService.insertLiveRewardRecord(record);
|
|
liveRewardRecordService.insertLiveRewardRecord(record);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ // 保留原有方法签名以兼容其他调用
|
|
|
|
|
+ private void saveUserRewardRecord(Live live, List<Long> userIds, Long scoreAmount) {
|
|
|
|
|
+ saveUserRewardRecord(live, userIds, BigDecimal.valueOf(scoreAmount), 2);
|
|
|
|
|
+ }
|
|
|
/**
|
|
/**
|
|
|
* 从Redis获取对象并转换为Long类型
|
|
* 从Redis获取对象并转换为Long类型
|
|
|
* @param redisCache Redis缓存操作对象
|
|
* @param redisCache Redis缓存操作对象
|