|
@@ -649,8 +649,9 @@ public class Task {
|
|
|
return 0;
|
|
return 0;
|
|
|
}
|
|
}
|
|
|
Long actionCouponId = Long.parseLong(actionCouponIdStr);
|
|
Long actionCouponId = Long.parseLong(actionCouponIdStr);
|
|
|
|
|
+ int actionCouponCount = resolveActionCouponCount(config);
|
|
|
LiveCoupon watchRewardCoupon = liveCouponService.selectLiveCouponById(actionCouponId);
|
|
LiveCoupon watchRewardCoupon = liveCouponService.selectLiveCouponById(actionCouponId);
|
|
|
- List<LiveConsoleOpLogUser> couponRelations = bindCouponToUsers(live, userIds, actionCouponId, false);
|
|
|
|
|
|
|
+ List<LiveConsoleOpLogUser> couponRelations = bindCouponToUsers(live, userIds, actionCouponId, false, actionCouponCount);
|
|
|
if (!couponRelations.isEmpty()) {
|
|
if (!couponRelations.isEmpty()) {
|
|
|
LiveConsoleOpLog watchCouponOpLog = liveConsoleOpLogService.saveLog(
|
|
LiveConsoleOpLog watchCouponOpLog = liveConsoleOpLogService.saveLog(
|
|
|
liveId,
|
|
liveId,
|
|
@@ -660,11 +661,15 @@ public class Task {
|
|
|
resolveWatchRewardCouponBizName(watchRewardCoupon, actionCouponId, couponRelations.size())
|
|
resolveWatchRewardCouponBizName(watchRewardCoupon, actionCouponId, couponRelations.size())
|
|
|
);
|
|
);
|
|
|
liveConsoleOpLogService.bindOpLogUsers(watchCouponOpLog.getId(), liveId, couponRelations);
|
|
liveConsoleOpLogService.bindOpLogUsers(watchCouponOpLog.getId(), liveId, couponRelations);
|
|
|
- couponRelations.forEach(relation -> sendCouponRewardMessage(
|
|
|
|
|
- liveId, relation.getUserId(), watchRewardCoupon, watchCouponOpLog));
|
|
|
|
|
- log.info("{} 观看奖励优惠券发放完成: liveId={}, 用户数={}",
|
|
|
|
|
- LOG_PREFIX, liveId, couponRelations.size());
|
|
|
|
|
- return couponRelations.size();
|
|
|
|
|
|
|
+ Set<Long> notifiedUserIds = new HashSet<>();
|
|
|
|
|
+ couponRelations.forEach(relation -> {
|
|
|
|
|
+ if (relation.getUserId() != null && notifiedUserIds.add(relation.getUserId())) {
|
|
|
|
|
+ sendCouponRewardMessage(liveId, relation.getUserId(), watchRewardCoupon, watchCouponOpLog);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ log.info("{} 观看奖励优惠券发放完成: liveId={}, 用户数={}, 每人张数={}",
|
|
|
|
|
+ LOG_PREFIX, liveId, notifiedUserIds.size(), actionCouponCount);
|
|
|
|
|
+ return notifiedUserIds.size();
|
|
|
}
|
|
}
|
|
|
log.warn("{} 观看奖励优惠券发放无成功用户: liveId={}, couponId={}",
|
|
log.warn("{} 观看奖励优惠券发放无成功用户: liveId={}, couponId={}",
|
|
|
LOG_PREFIX, liveId, actionCouponId);
|
|
LOG_PREFIX, liveId, actionCouponId);
|
|
@@ -685,10 +690,12 @@ public class Task {
|
|
|
* @param userIds 待发放用户ID列表
|
|
* @param userIds 待发放用户ID列表
|
|
|
* @param couponId 优惠券ID
|
|
* @param couponId 优惠券ID
|
|
|
* @param sendWebSocket 是否立即发送 WebSocket(定时任务由外层统一发送并附带 opLog)
|
|
* @param sendWebSocket 是否立即发送 WebSocket(定时任务由外层统一发送并附带 opLog)
|
|
|
|
|
+ * @param couponCount 每人领取张数
|
|
|
* @return 发放成功的用户关联列表(含 userId、couponUserId,供写入 live_console_op_log_user)
|
|
* @return 发放成功的用户关联列表(含 userId、couponUserId,供写入 live_console_op_log_user)
|
|
|
*/
|
|
*/
|
|
|
- private List<LiveConsoleOpLogUser> bindCouponToUsers(Live live, List<Long> userIds, Long couponId, boolean sendWebSocket) {
|
|
|
|
|
|
|
+ private List<LiveConsoleOpLogUser> bindCouponToUsers(Live live, List<Long> userIds, Long couponId, boolean sendWebSocket, int couponCount) {
|
|
|
List<LiveConsoleOpLogUser> relations = new ArrayList<>();
|
|
List<LiveConsoleOpLogUser> relations = new ArrayList<>();
|
|
|
|
|
+ int grantCount = couponCount > 0 ? couponCount : 1;
|
|
|
try {
|
|
try {
|
|
|
// 查询优惠券信息
|
|
// 查询优惠券信息
|
|
|
LiveCoupon coupon = liveCouponService.selectLiveCouponById(couponId);
|
|
LiveCoupon coupon = liveCouponService.selectLiveCouponById(couponId);
|
|
@@ -714,40 +721,46 @@ public class Task {
|
|
|
|
|
|
|
|
for (Long userId : userIds) {
|
|
for (Long userId : userIds) {
|
|
|
try {
|
|
try {
|
|
|
- // 创建用户优惠券记录
|
|
|
|
|
- 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());
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ boolean userGranted = false;
|
|
|
|
|
+ for (int i = 0; i < grantCount; i++) {
|
|
|
|
|
+ // 创建用户优惠券记录
|
|
|
|
|
+ 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);
|
|
|
|
|
|
|
+ couponUser.setType("3"); // 获取方式:3-观看奖励
|
|
|
|
|
+ couponUser.setStatus(0); // 状态:0-未核销
|
|
|
|
|
+ couponUser.setIsFail(0); // 是否有效:0-有效
|
|
|
|
|
+ couponUser.setIsDel(0);
|
|
|
|
|
+ couponUser.setCreateTime(now);
|
|
|
|
|
|
|
|
- liveCouponUserService.insertLiveCouponUser(couponUser);
|
|
|
|
|
|
|
+ liveCouponUserService.insertLiveCouponUser(couponUser);
|
|
|
|
|
|
|
|
- // 保存奖励记录 - 优惠券类型的sourceId为couponId
|
|
|
|
|
- saveUserRewardRecord(live, Collections.singletonList(userId), coupon.getCouponPrice(), 3, couponId);
|
|
|
|
|
|
|
+ relations.add(new LiveConsoleOpLogUser(null, userId, live.getLiveId(), couponUser.getId()));
|
|
|
|
|
+ userGranted = true;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- relations.add(new LiveConsoleOpLogUser(null, userId, live.getLiveId(), couponUser.getId()));
|
|
|
|
|
|
|
+ if (userGranted) {
|
|
|
|
|
+ // 保存奖励记录 - 优惠券类型的sourceId为couponId(按用户记一条)
|
|
|
|
|
+ saveUserRewardRecord(live, Collections.singletonList(userId), coupon.getCouponPrice(), 3, couponId);
|
|
|
|
|
|
|
|
- if (sendWebSocket) {
|
|
|
|
|
- sendCouponRewardMessage(live.getLiveId(), userId, coupon);
|
|
|
|
|
|
|
+ if (sendWebSocket) {
|
|
|
|
|
+ sendCouponRewardMessage(live.getLiveId(), userId, coupon);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
@@ -755,8 +768,8 @@ public class Task {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- log.info("直播间观看奖励-优惠券发放完成,liveId={}, couponId={}, 成功发放 {} 个用户",
|
|
|
|
|
- live.getLiveId(), couponId, relations.size());
|
|
|
|
|
|
|
+ log.info("直播间观看奖励-优惠券发放完成,liveId={}, couponId={}, 每人张数={}, 成功发放 {} 条记录",
|
|
|
|
|
+ live.getLiveId(), couponId, grantCount, relations.size());
|
|
|
|
|
|
|
|
return relations;
|
|
return relations;
|
|
|
|
|
|
|
@@ -766,6 +779,13 @@ public class Task {
|
|
|
return relations;
|
|
return relations;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private int resolveActionCouponCount(LiveWatchConfig config) {
|
|
|
|
|
+ if (config == null || config.getActionCouponCount() == null || config.getActionCouponCount() <= 0) {
|
|
|
|
|
+ return 1;
|
|
|
|
|
+ }
|
|
|
|
|
+ return config.getActionCouponCount().intValue();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 手动触发观看奖励优惠券发放(单用户,供接口调用)
|
|
* 手动触发观看奖励优惠券发放(单用户,供接口调用)
|
|
|
*
|
|
*
|
|
@@ -781,13 +801,17 @@ public class Task {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
Long targetCouponId = couponId;
|
|
Long targetCouponId = couponId;
|
|
|
|
|
+ int actionCouponCount = 1;
|
|
|
|
|
+ String configJson = live.getConfigJson();
|
|
|
|
|
+ LiveWatchConfig config = null;
|
|
|
|
|
+ if (StringUtils.isNotEmpty(configJson)) {
|
|
|
|
|
+ config = JSON.parseObject(configJson, LiveWatchConfig.class);
|
|
|
|
|
+ }
|
|
|
if (targetCouponId == null) {
|
|
if (targetCouponId == null) {
|
|
|
- String configJson = live.getConfigJson();
|
|
|
|
|
- if (StringUtils.isEmpty(configJson)) {
|
|
|
|
|
|
|
+ if (config == null) {
|
|
|
return R.error("直播间未配置观看奖励");
|
|
return R.error("直播间未配置观看奖励");
|
|
|
}
|
|
}
|
|
|
- LiveWatchConfig config = JSON.parseObject(configJson, LiveWatchConfig.class);
|
|
|
|
|
- if (config == null || !Boolean.TRUE.equals(config.getEnabled())) {
|
|
|
|
|
|
|
+ if (!Boolean.TRUE.equals(config.getEnabled())) {
|
|
|
return R.error("观看奖励未开启");
|
|
return R.error("观看奖励未开启");
|
|
|
}
|
|
}
|
|
|
if (config.getAction() == null || config.getAction().intValue() != 3) {
|
|
if (config.getAction() == null || config.getAction().intValue() != 3) {
|
|
@@ -799,8 +823,9 @@ public class Task {
|
|
|
}
|
|
}
|
|
|
targetCouponId = Long.parseLong(actionCouponIdStr);
|
|
targetCouponId = Long.parseLong(actionCouponIdStr);
|
|
|
}
|
|
}
|
|
|
|
|
+ actionCouponCount = resolveActionCouponCount(config);
|
|
|
|
|
|
|
|
- List<LiveConsoleOpLogUser> couponRelations = bindCouponToUsers(live, Collections.singletonList(userId), targetCouponId, false);
|
|
|
|
|
|
|
+ List<LiveConsoleOpLogUser> couponRelations = bindCouponToUsers(live, Collections.singletonList(userId), targetCouponId, false, actionCouponCount);
|
|
|
if (couponRelations.isEmpty()) {
|
|
if (couponRelations.isEmpty()) {
|
|
|
return R.error("优惠券发放失败,请检查优惠券配置及库存");
|
|
return R.error("优惠券发放失败,请检查优惠券配置及库存");
|
|
|
}
|
|
}
|
|
@@ -813,10 +838,10 @@ public class Task {
|
|
|
resolveWatchRewardCouponBizName(watchRewardCoupon, targetCouponId, couponRelations.size())
|
|
resolveWatchRewardCouponBizName(watchRewardCoupon, targetCouponId, couponRelations.size())
|
|
|
);
|
|
);
|
|
|
liveConsoleOpLogService.bindOpLogUsers(watchCouponOpLog.getId(), liveId, couponRelations);
|
|
liveConsoleOpLogService.bindOpLogUsers(watchCouponOpLog.getId(), liveId, couponRelations);
|
|
|
- couponRelations.forEach(relation -> sendCouponRewardMessage(
|
|
|
|
|
- liveId, relation.getUserId(), watchRewardCoupon, watchCouponOpLog));
|
|
|
|
|
|
|
+ sendCouponRewardMessage(liveId, userId, watchRewardCoupon, watchCouponOpLog);
|
|
|
return R.ok("观看奖励优惠券发放完成")
|
|
return R.ok("观看奖励优惠券发放完成")
|
|
|
.put("couponId", targetCouponId)
|
|
.put("couponId", targetCouponId)
|
|
|
|
|
+ .put("couponCount", actionCouponCount)
|
|
|
.put("opLogId", watchCouponOpLog.getId());
|
|
.put("opLogId", watchCouponOpLog.getId());
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
log.error("手动触发观看奖励优惠券失败, liveId={}, userId={}", liveId, userId, e);
|
|
log.error("手动触发观看奖励优惠券失败, liveId={}, userId={}", liveId, userId, e);
|