1
0

3 Ревизии 295f2ecb9a ... 78ca515b80

Автор SHA1 Съобщение Дата
  wangzhijun 78ca515b80 syncLotteryCount接口抽取 преди 1 седмица
  wangzhijun a276b7bdcc 优化saveComment获得抽奖次数代码 преди 1 седмица
  wangzhijun 9dc9acc801 添加@EnableAsync注解 преди 1 седмица

+ 2 - 0
nightFragrance-admin/src/main/java/com/ylx/NightFragranceApplication.java

@@ -3,12 +3,14 @@ package com.ylx;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
+import org.springframework.scheduling.annotation.EnableAsync;
 
 /**
  * 启动程序
  *
  * @author ylx
  */
+@EnableAsync
 @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
 public class NightFragranceApplication {
     public static void main(String[] args) {

+ 89 - 94
nightFragrance-massage/src/main/java/com/ylx/massage/service/impl/TCommentUserServiceImpl.java

@@ -20,10 +20,12 @@ import com.ylx.massage.service.TCommentUserAuditService;
 import com.ylx.massage.service.TCommentUserService;
 import com.ylx.massage.service.TOrderService;
 import com.ylx.usercenter.domain.dto.UnifiedUserCenterDTO;
+import com.ylx.usercenter.service.UnifiedUserCenterService;
 import lombok.extern.slf4j.Slf4j;
-import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.transaction.support.TransactionSynchronization;
+import org.springframework.transaction.support.TransactionSynchronizationManager;
 
 import javax.annotation.Resource;
 import java.util.Date;
@@ -51,6 +53,8 @@ public class TCommentUserServiceImpl extends ServiceImpl<TCommentUserMapper, TCo
     private LotteryCountService lotteryCountService;
     @Resource
     private LotteryCountLogService lotteryCountLogService;
+    @Resource
+    private UnifiedUserCenterService unifiedUserCenterService;
 
     @Override
     @Transactional(rollbackFor = Exception.class)
@@ -97,112 +101,103 @@ public class TCommentUserServiceImpl extends ServiceImpl<TCommentUserMapper, TCo
         //修改订单状态(已完成)
         order.setnStatus(OrderStatusEnum.COMPLETE.getCode());
         order.setcId(comment.getOrderId());
-        boolean bool = orderService.updateById(order);
-        if (bool) {
-
-            Integer isBind = wxLoginUser.getIsBind();
-            String localLiveUserId = wxLoginUser.getLocalLiveUserId();
-            // 评价有礼
-            List<LotteryActivityVO> lotteryActivityVOS = lotteryCountService.queryActivityRules();
-            if (CollUtil.isNotEmpty(lotteryActivityVOS)) {
-                for (LotteryActivityVO activity : lotteryActivityVOS) {
-
-                    List<LocalActivityTableVO> gyyPointTables = activity.getGyyPointTables();
-                    if (CollUtil.isNotEmpty(gyyPointTables)) {
-                        for (LocalActivityTableVO localActivityTable : gyyPointTables) {
-
-                            LotteryCountLog lotteryCountLog = new LotteryCountLog();
-                            lotteryCountLog.setOpenId(wxLoginUser.getCOpenid());
-                            lotteryCountLog.setUserId(wxLoginUser.getId());
-                            lotteryCountLog.setUserPhone(wxLoginUser.getCPhone());
-                            // 活动类型:2.任务奖励
-                            lotteryCountLog.setActivityType(2);
-                            lotteryCountLog.setLocalActivityTableId(localActivityTable.getId());
-                            lotteryCountLog.setLotteryNum(localActivityTable.getPointNumber());
-                            lotteryCountLog.setReceiveTime(new Date());
-                            lotteryCountLog.setIsDelete("0");
-                            // 同步状态 0-未同步到本地生活
-                            lotteryCountLog.setStatus(0);
-
-                            lotteryCountLog.setIsLottery(0);
-                            lotteryCountLog.setType(3);
-                            lotteryCountLogService.save(lotteryCountLog);
-                        }
-                    } else {
+        boolean updateOrderSuccess = orderService.updateById(order);
+        if (!updateOrderSuccess) {
+            throw new ServiceException("订单状态更新失败");
+        }
 
-                        // 非任务奖励活动
-                        if (ObjectUtil.isNull(activity.getParticipationRules()) || ObjectUtil.notEqual(activity.getParticipationRules(), 3)) {
-                            continue;
-                        }
+        // 评价有礼 —— 发放抽奖次数日志
+        grantLotteryCountForComment(wxLoginUser);
 
-                        List<LocalActivityTableVO> localActivityTables = activity.getLocalActivityTables();
-                        if (CollUtil.isEmpty(localActivityTables)) {
-                            continue;
-                        }
+        // 已绑定一账通 → 异步同步抽奖次数
+        syncLotteryCountIfNeeded(wxLoginUser);
 
-                        for (LocalActivityTableVO localActivityTable : localActivityTables) {
-
-                            // 判断是否评价有礼
-                            if (ObjectUtil.isNull(localActivityTable.getType()) || ObjectUtil.notEqual(localActivityTable.getType(), 4)) {
-                                continue;
-                            }
-
-                            LotteryCountLog lotteryCountLog = new LotteryCountLog();
-                            lotteryCountLog.setOpenId(wxLoginUser.getCOpenid());
-                            lotteryCountLog.setUserId(wxLoginUser.getId());
-                            lotteryCountLog.setUserPhone(wxLoginUser.getCPhone());
-                            // 活动类型:2.任务奖励
-                            lotteryCountLog.setActivityType(2);
-                            lotteryCountLog.setLocalActivityTableId(localActivityTable.getId());
-                            lotteryCountLog.setLotteryNum(1);
-                            lotteryCountLog.setReceiveTime(new Date());
-                            lotteryCountLog.setIsDelete("0");
-                            // 同步状态 0-未同步到本地生活
-                            lotteryCountLog.setStatus(0);
-
-                            lotteryCountLog.setIsLottery(0);
-                            lotteryCountLog.setType(3);
-                            lotteryCountLogService.save(lotteryCountLog);
-                        }
-                    }
+        return true;
+    }
+
+    @Override
+    public List<TCommentUser> selectAll(TCommentUser tCommentUser) {
+        return tCommentUserMapper.selectAll(tCommentUser);
+    }
+
+    /**
+     * 评价有礼发放抽奖次数
+     */
+    private void grantLotteryCountForComment(WxLoginUser wxLoginUser) {
+        List<LotteryActivityVO> activityList = lotteryCountService.queryActivityRules();
+        if (CollUtil.isEmpty(activityList)) {
+            return;
+        }
+
+        for (LotteryActivityVO activity : activityList) {
+            // 处理积分表
+            List<LocalActivityTableVO> pointTables = activity.getGyyPointTables();
+            if (CollUtil.isNotEmpty(pointTables)) {
+                for (LocalActivityTableVO table : pointTables) {
+                    saveLotteryLog(wxLoginUser, table, table.getPointNumber());
                 }
+                continue;
+            }
+
+            // 非积分表、任务奖励类型(参与规则=3)
+            if (ObjectUtil.notEqual(activity.getParticipationRules(), 3)) {
+                continue;
+            }
+
+            List<LocalActivityTableVO> localTables = activity.getLocalActivityTables();
+            if (CollUtil.isEmpty(localTables)) {
+                continue;
             }
 
-            // todo  事务问题 当前用户绑定一账通
-            if (ObjectUtil.isNotNull(isBind) && (ObjectUtil.equals(isBind, 1) && ObjectUtil.isNotNull(localLiveUserId))) {
-                // 同步抽奖次数(异步处理)
-                UnifiedUserCenterDTO dto = new UnifiedUserCenterDTO();
-                dto.setTargetUserId(localLiveUserId);
-                dto.setSourceUserId(wxLoginUser.getId());
-                syncLotteryCount(dto);
+            // 类型=4 → 评价有礼
+            for (LocalActivityTableVO table : localTables) {
+                if (ObjectUtil.equals(table.getType(), 4)) {
+                    saveLotteryLog(wxLoginUser, table, 1);
+                }
             }
         }
-        return bool;
     }
 
-
-    @Override
-    public List<TCommentUser> selectAll(TCommentUser tCommentUser) {
-        return tCommentUserMapper.selectAll(tCommentUser);
+    /**
+     * 统一保存抽奖次数日志
+     */
+    private void saveLotteryLog(WxLoginUser wxLoginUser, LocalActivityTableVO table, Integer lotteryNum) {
+        LotteryCountLog log = new LotteryCountLog();
+        log.setOpenId(wxLoginUser.getCOpenid());
+        log.setUserId(wxLoginUser.getId());
+        log.setUserPhone(wxLoginUser.getCPhone());
+        log.setActivityType(2); // 任务奖励
+        log.setLocalActivityTableId(table.getId());
+        log.setLotteryNum(lotteryNum);
+        log.setReceiveTime(new Date());
+        log.setIsDelete("0");
+        log.setStatus(0); // 未同步
+        log.setIsLottery(0);
+        log.setType(3);
+        lotteryCountLogService.save(log);
     }
 
-    // 异步同步抽奖次数
-    @Async
-    public void syncLotteryCount(UnifiedUserCenterDTO dto) {
-        try {
-            boolean lotteryCountBool = this.lotteryCountService.batchAdd(dto);
-            if (lotteryCountBool) {
-                log.info("同步用户抽奖次数到本地生活服务成功, userId:{}", dto.getTargetUserId());
-            } else {
-                log.warn("同步用户抽奖次数到本地生活服务失败, userId:{}", dto.getTargetUserId());
-                // 同步失败,记录告警日志
-                log.error("【告警】同步用户抽奖次数到本地生活服务失败,需要人工处理, userId:{}", dto.getTargetUserId());
-            }
-        } catch (Exception e) {
-            log.error("同步用户抽奖次数到本地生活服务异常, userId:{}", dto.getTargetUserId(), e);
-            // 异常情况,记录告警日志
-            log.error("【告警】同步用户抽奖次数到本地生活服务异常,需要人工处理, userId:{}", dto.getTargetUserId(), e);
+    private void syncLotteryCountIfNeeded(WxLoginUser wxLoginUser) {
+        if (ObjectUtil.equals(wxLoginUser.getIsBind(), 1)
+                && 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);
+                        }
+                    }
+            );
+
         }
     }
+
 }
 

+ 2 - 0
nightFragrance-massage/src/main/java/com/ylx/usercenter/service/UnifiedUserCenterService.java

@@ -12,4 +12,6 @@ public interface UnifiedUserCenterService {
     R<?> unbind(UnifiedUserCenterDTO dto);
 
     OneAccountVO queryClients(String uuid);
+
+    void syncLotteryCount(UnifiedUserCenterDTO dto);
 }

+ 1 - 0
nightFragrance-massage/src/main/java/com/ylx/usercenter/service/impl/UnifiedUserCenterServiceImpl.java

@@ -218,6 +218,7 @@ public class UnifiedUserCenterServiceImpl implements UnifiedUserCenterService {
 
     // 异步同步抽奖次数
     @Async
+    @Override
     public void syncLotteryCount(UnifiedUserCenterDTO dto) {
         try {
             boolean lotteryCountBool = this.lotteryCountService.batchAdd(dto);