Jelajahi Sumber

评价有礼同步本地生活获奖次数bug修复

wangzhijun 6 hari lalu
induk
melakukan
a51b8c93d6

+ 3 - 2
nightFragrance-massage/src/main/java/com/ylx/massage/service/TCommentUserService.java

@@ -1,9 +1,8 @@
 package com.ylx.massage.service;
 
-import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.ylx.common.core.domain.model.WxLoginUser;
-import com.ylx.massage.domain.TComment;
+import com.ylx.lottery.domain.vo.LocalActivityTableVO;
 import com.ylx.massage.domain.TCommentUser;
 
 import java.util.List;
@@ -33,5 +32,7 @@ public interface TCommentUserService extends IService<TCommentUser> {
      */
      List<TCommentUser> selectAll(TCommentUser tCommentUser);
 
+    void saveLotteryLogAndCommit(WxLoginUser wxLoginUser, LocalActivityTableVO table, Integer lotteryNum);
+
 }
 

+ 24 - 14
nightFragrance-massage/src/main/java/com/ylx/massage/service/impl/TCommentUserServiceImpl.java

@@ -24,6 +24,7 @@ import com.ylx.point.service.IPointAccountService;
 import com.ylx.usercenter.domain.dto.UnifiedUserCenterDTO;
 import com.ylx.usercenter.service.UnifiedUserCenterService;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.context.annotation.Lazy;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.transaction.support.TransactionSynchronization;
@@ -59,6 +60,9 @@ public class TCommentUserServiceImpl extends ServiceImpl<TCommentUserMapper, TCo
     private UnifiedUserCenterService unifiedUserCenterService;
     @Resource
     private IPointAccountService pointAccountService;
+    @Lazy
+    @Resource
+    private TCommentUserService self;
 
     @Override
     @Transactional(rollbackFor = Exception.class)
@@ -162,7 +166,7 @@ public class TCommentUserServiceImpl extends ServiceImpl<TCommentUserMapper, TCo
                 if (ObjectUtil.equals(table.getType(), "4")) {
                     table.setActivityUrl(activityUrl);
                     table.setActivityId(activity.getId());
-                    saveLotteryLog(wxLoginUser, table, 1);
+                    self.saveLotteryLogAndCommit(wxLoginUser, table, 1);
                 }
             }
         }
@@ -171,7 +175,9 @@ public class TCommentUserServiceImpl extends ServiceImpl<TCommentUserMapper, TCo
     /**
      * 统一保存抽奖次数日志
      */
-    private void saveLotteryLog(WxLoginUser wxLoginUser, LocalActivityTableVO table, Integer lotteryNum) {
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void saveLotteryLogAndCommit(WxLoginUser wxLoginUser, LocalActivityTableVO table, Integer lotteryNum) {
         LotteryCountLog log = new LotteryCountLog();
         log.setOpenId(wxLoginUser.getCOpenid());
         log.setUserId(wxLoginUser.getId());
@@ -193,19 +199,23 @@ public class TCommentUserServiceImpl extends ServiceImpl<TCommentUserMapper, TCo
                 && StringUtils.isNotBlank(wxLoginUser.getLocalLiveUserId())) {
 
             UnifiedUserCenterDTO dto = new UnifiedUserCenterDTO();
-            dto.setTargetUserId(wxLoginUser.getLocalLiveUserId());
-            dto.setSourceUserId(wxLoginUser.getId());
-
-            // 👇 事务提交后再执行异步
-            TransactionSynchronizationManager.registerSynchronization(
-                    new TransactionSynchronization() {
-                        @Override
-                        public void afterCommit() {
-                            // 事务提交完成!现在调用异步,一定能查到数据
-                            unifiedUserCenterService.syncLotteryCount(dto);
-                        }
+            dto.setSourceUserId(wxLoginUser.getLocalLiveUserId());
+            dto.setTargetUserId(wxLoginUser.getId());
+
+            // 👇 加上这个 if 判断,只有当当前存在活跃事务时,才去注册 afterCommit 回调
+            if (TransactionSynchronizationManager.isSynchronizationActive()) {
+                TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
+                    @Override
+                    public void afterCommit() {
+                        // 事务提交完成!现在调用异步,一定能查到数据
+                        unifiedUserCenterService.syncLotteryCount(dto);
                     }
-            );
+                });
+            } else {
+                // 👇 兜底逻辑:如果事务没开启(比如同类自调用导致失效),就直接同步执行,避免报错
+                log.warn("当前没有活跃事务,无法注册 afterCommit 回调,将直接同步执行抽奖次数同步!");
+                unifiedUserCenterService.syncLotteryCount(dto);
+            }
 
         }
     }