Quellcode durchsuchen

Merge branch 'sit' of http://8.152.195.41:3000/alien/alien_cloud into second-trade

qrs vor 3 Wochen
Ursprung
Commit
c49f278f19

+ 52 - 0
alien-gateway/src/main/java/shop/alien/gateway/service/LifeUserLogTransactionService.java

@@ -0,0 +1,52 @@
+package shop.alien.gateway.service;
+
+import lombok.RequiredArgsConstructor;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Propagation;
+import org.springframework.transaction.annotation.Transactional;
+import shop.alien.entity.second.LifeUserLog;
+import shop.alien.gateway.mapper.LifeUserLogGatewayMapper;
+
+import java.util.List;
+
+/**
+ * 用户登录日志事务处理服务
+ * 独立的事务服务类,用于处理需要新事务的数据库操作
+ * 解决同类方法调用时 Spring AOP 代理不生效的问题
+ *
+ * @author alien-cloud
+ * @since 2024-01-01
+ */
+@Service
+@RequiredArgsConstructor
+public class LifeUserLogTransactionService {
+
+    private final LifeUserLogGatewayMapper lifeUserLogMapper;
+
+    /**
+     * 在新事务中插入用户登录日志
+     * 使用 REQUIRES_NEW 确保插入操作立即提交,便于后续查询
+     *
+     * @param lifeUserLog 用户登录日志对象
+     * @return int 插入成功的记录数
+     */
+    @Transactional(propagation = Propagation.REQUIRES_NEW)
+    public int insertLifeUserLog(LifeUserLog lifeUserLog) {
+        return lifeUserLogMapper.insert(lifeUserLog);
+    }
+
+    /**
+     * 在新事务中查询指定时间段内的用户登录日志
+     * 使用 REQUIRES_NEW 确保能读取到已提交的最新数据
+     *
+     * @param startDate 开始时间
+     * @param endDate 结束时间
+     * @param macIp MAC地址/IP
+     * @return List<LifeUserLog> 用户登录日志列表
+     */
+    @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = true)
+    public List<LifeUserLog> getLifeUserLogByDate(String startDate, String endDate, String macIp) {
+        return lifeUserLogMapper.getLifeUserLogByDate(startDate, endDate, macIp);
+    }
+}
+

+ 17 - 8
alien-gateway/src/main/java/shop/alien/gateway/service/LifeUserService.java

@@ -47,6 +47,8 @@ public class LifeUserService extends ServiceImpl<LifeUserGatewayMapper, LifeUser
 
     private final SecondRiskControlRecordMapper secondRiskControlRecordMapper;
 
+    private final LifeUserLogTransactionService lifeUserLogTransactionService;
+
     @Autowired
     private RiskControlProperties riskControlProperties;
 
@@ -135,6 +137,7 @@ public class LifeUserService extends ServiceImpl<LifeUserGatewayMapper, LifeUser
 
     /**
      * 用户登录log存放(加入mac地址)
+     * 记录用户登录信息并进行风控检查
      */
     @Transactional
     public void addLifeUserLogInfo(LifeUser user, String macIp) {
@@ -144,12 +147,13 @@ public class LifeUserService extends ServiceImpl<LifeUserGatewayMapper, LifeUser
             lifeUserLog.setUserName(user.getUserName());
             lifeUserLog.setMacIp(macIp);
             lifeUserLog.setCreatedTime(new Date());
-            int count = lifeUserLogMapper.insert(lifeUserLog);
+            // 通过独立服务类调用,确保事务代理生效,插入操作立即提交
+            int count = lifeUserLogTransactionService.insertLifeUserLog(lifeUserLog);
             if (count > 0) {
                 String startDate = LocalDateTime.now().minusHours(24L).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                 String endDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
-                // 使用一个新的事务来查询,确保能看到刚才插入的数据
-                List<LifeUserLog> lsit = getLifeUserLogByDateInNewTransaction(startDate, endDate, macIp);
+                // 通过独立服务类查询,确保能看到刚才插入并提交的数据
+                List<LifeUserLog> lsit = lifeUserLogTransactionService.getLifeUserLogByDate(startDate, endDate, macIp);
 
                 if (lsit.size() > riskControlProperties.getAccountAbnormal().getRegCount24h() && !isViolation(startDate, endDate, macIp, user.getId())) {
                     String detailInfo = lsit.stream()
@@ -166,11 +170,16 @@ public class LifeUserService extends ServiceImpl<LifeUserGatewayMapper, LifeUser
 
     }
 
-    @Transactional(propagation = Propagation.REQUIRES_NEW)
-    public List<LifeUserLog> getLifeUserLogByDateInNewTransaction(String startDate, String endDate, String macIp) {
-        return lifeUserLogMapper.getLifeUserLogByDate(startDate, endDate, macIp);
-    }
-
+    /**
+     * 判断是否已存在违规记录
+     * 检查指定时间段内是否已经记录过该用户的风控信息
+     *
+     * @param startDate 开始时间
+     * @param endDate 结束时间
+     * @param macIp MAC地址/IP
+     * @param userId 用户ID
+     * @return boolean true-已存在违规记录,false-不存在
+     */
     public boolean isViolation(String startDate, String endDate, String macIp, Integer userId) {
         List<SecondRiskControlRecord> list = secondRiskControlRecordMapper.selectByBusinessId(startDate, endDate, macIp);
         for (SecondRiskControlRecord record : list) {