Bladeren bron

1、调整完课答题领优惠卷需要留存

yys 4 dagen geleden
bovenliggende
commit
543deba2a8

+ 39 - 27
fs-live-app/src/main/java/com/fs/live/task/LiveCompletionPointsTask.java

@@ -104,25 +104,8 @@ public class LiveCompletionPointsTask {
                 return;
             }
 
-            processCompletionByWatchDuration(activeLives, (liveId, userId, duration) -> {
-                LiveCompletionCouponNotifyResult notifyResult =
-                        completionCouponService.prepareCompletionCouponNotify(liveId, userId, duration);
-                if (notifyResult == null || !notifyResult.isShouldNotify()) {
-                    return;
-                }
-                String bizName = resolveCompletionCouponBizName(notifyResult);
-                Long bizId = notifyResult.getCoupon() != null ? notifyResult.getCoupon().getCouponId() : null;
-                LiveConsoleOpLog opLog = liveConsoleOpLogService.saveLog(
-                        liveId,
-                        LiveConsoleOpLog.OP_COMPLETION_COUPON,
-                        LiveConsoleOpLog.HANDLE_AUTO,
-                        bizId,
-                        bizName
-                );
-                if (pushCompletionCouponQuestion(liveId, userId, notifyResult, opLog)) {
-                    completionCouponService.markCompletionCouponNotified(liveId, userId);
-                }
-            });
+            processCompletionByWatchDuration(activeLives, (liveId, userId, duration) ->
+                    dispatchCompletionCouponNotify(liveId, userId, duration, false));
 
         } catch (Exception e) {
             log.error("检查完课优惠券定时任务执行失败", e);
@@ -139,21 +122,50 @@ public class LiveCompletionPointsTask {
             if (notifyResult == null || !notifyResult.isShouldNotify()) {
                 return R.ok("当前无需推送弹窗").put("data", notifyResult);
             }
-            boolean pushed = pushCompletionCouponQuestion(liveId, userId, notifyResult);
-            if (pushed) {
-                completionCouponService.markCompletionCouponNotified(liveId, userId);
-            }
+            LiveConsoleOpLog opLog = saveAndPushCompletionCouponNotify(liveId, userId, notifyResult);
+            boolean pushed = opLog != null;
             return pushed
-                    ? R.ok("今日问题弹窗 WebSocket 推送成功").put("data", notifyResult).put("pushed", true)
-                    : R.error("用户未在线,WebSocket 推送失败").put("data", notifyResult).put("pushed", false);
+                    ? R.ok("今日问题弹窗 WebSocket 推送成功")
+                        .put("data", notifyResult)
+                        .put("pushed", true)
+                        .put("opLogId", opLog.getId())
+                    : R.error("用户未在线,WebSocket 推送失败")
+                        .put("data", notifyResult)
+                        .put("pushed", false);
         } catch (Exception e) {
             log.error("手动触发完课优惠券弹窗失败, liveId={}, userId={}", liveId, userId, e);
             return R.error("触发失败:" + e.getMessage());
         }
     }
 
-    private boolean pushCompletionCouponQuestion(Long liveId, Long userId, LiveCompletionCouponNotifyResult notifyResult) {
-        return pushCompletionCouponQuestion(liveId, userId, notifyResult, null);
+    /**
+     * 完课优惠券弹窗:与定时任务一致,先写留存再推送 WebSocket
+     */
+    private LiveConsoleOpLog dispatchCompletionCouponNotify(Long liveId, Long userId, Long watchDuration, boolean forcePush) {
+        LiveCompletionCouponNotifyResult notifyResult =
+                completionCouponService.prepareCompletionCouponNotify(liveId, userId, watchDuration, forcePush);
+        if (notifyResult == null || !notifyResult.isShouldNotify()) {
+            return null;
+        }
+        return saveAndPushCompletionCouponNotify(liveId, userId, notifyResult);
+    }
+
+    private LiveConsoleOpLog saveAndPushCompletionCouponNotify(Long liveId, Long userId,
+                                                                 LiveCompletionCouponNotifyResult notifyResult) {
+        String bizName = resolveCompletionCouponBizName(notifyResult);
+        Long bizId = notifyResult.getCoupon() != null ? notifyResult.getCoupon().getCouponId() : null;
+        LiveConsoleOpLog opLog = liveConsoleOpLogService.saveLog(
+                liveId,
+                LiveConsoleOpLog.OP_COMPLETION_COUPON,
+                LiveConsoleOpLog.HANDLE_AUTO,
+                bizId,
+                bizName
+        );
+        if (pushCompletionCouponQuestion(liveId, userId, notifyResult, opLog)) {
+            completionCouponService.markCompletionCouponNotified(liveId, userId);
+            return opLog;
+        }
+        return null;
     }
 
     private boolean pushCompletionCouponQuestion(Long liveId, Long userId, LiveCompletionCouponNotifyResult notifyResult,

+ 23 - 4
fs-live-app/src/main/java/com/fs/live/task/Task.java

@@ -542,8 +542,11 @@ public class Task {
                             openRewardLive.getLiveId(),
                             resolveWatchRewardPointsBizName(config, userIds.size())
                     );
-                    userIds.forEach(userId -> webSocketServer.sendIntegralMessage(
-                            openRewardLive.getLiveId(), userId, config.getScoreAmount(), watchPointsOpLog));
+                    userIds.forEach(userId -> {
+                        liveConsoleOpLogService.bindOpLogUser(watchPointsOpLog.getId(), openRewardLive.getLiveId(), userId);
+                        webSocketServer.sendIntegralMessage(
+                                openRewardLive.getLiveId(), userId, config.getScoreAmount(), watchPointsOpLog);
+                    });
                     rewardLiveCount++;
                     rewardedUserCount += userIds.size();
                     log.info("{} autoUpdateWatchReward 积分发放完成: liveId={}, 用户数={}",
@@ -718,8 +721,24 @@ public class Task {
                 targetCouponId = Long.parseLong(actionCouponIdStr);
             }
 
-            bindCouponToUsers(live, Collections.singletonList(userId), targetCouponId, true);
-            return R.ok("观看奖励优惠券发放完成").put("couponId", targetCouponId);
+            List<LiveConsoleOpLogUser> couponRelations = bindCouponToUsers(live, Collections.singletonList(userId), targetCouponId, false);
+            if (couponRelations.isEmpty()) {
+                return R.error("优惠券发放失败,请检查优惠券配置及库存");
+            }
+            LiveCoupon watchRewardCoupon = liveCouponService.selectLiveCouponById(targetCouponId);
+            LiveConsoleOpLog watchCouponOpLog = liveConsoleOpLogService.saveLog(
+                    liveId,
+                    LiveConsoleOpLog.OP_WATCH_REWARD_COUPON,
+                    LiveConsoleOpLog.HANDLE_AUTO,
+                    targetCouponId,
+                    resolveWatchRewardCouponBizName(watchRewardCoupon, targetCouponId, couponRelations.size())
+            );
+            liveConsoleOpLogService.bindOpLogUsers(watchCouponOpLog.getId(), liveId, couponRelations);
+            couponRelations.forEach(relation -> sendCouponRewardMessage(
+                    liveId, relation.getUserId(), watchRewardCoupon, watchCouponOpLog));
+            return R.ok("观看奖励优惠券发放完成")
+                    .put("couponId", targetCouponId)
+                    .put("opLogId", watchCouponOpLog.getId());
         } catch (Exception e) {
             log.error("手动触发观看奖励优惠券失败, liveId={}, userId={}", liveId, userId, e);
             return R.error("触发失败:" + e.getMessage());

+ 1 - 0
fs-live-app/src/main/java/com/fs/live/websocket/service/WebSocketServer.java

@@ -1138,6 +1138,7 @@ public class WebSocketServer {
             return false;
         }
         try {
+            embedOpLogIdInMessageData(sendMsgVo);
             sendMessage(session, JSONObject.toJSONString(R.ok().put("data", sendMsgVo)));
             return true;
         } catch (Exception e) {