소스 검색

日志记录ID格式修改

2643616413 3 주 전
부모
커밋
9ea828eb78

+ 1 - 1
server/yusp-tagging-core/src/main/java/cn/com/yusys/yusp/aop/ApiLogAspect.java

@@ -30,7 +30,7 @@ public class ApiLogAspect {
         long startTime = System.currentTimeMillis();
 
         AitagApiLog apiLog = new AitagApiLog();
-        apiLog.setId(apiLogService.generateLogId());
+        apiLog.setId(apiLogService.generateUuid());
         apiLog.setCreateDate(new Date());
 
         // 设置用户信息 - 通过 SessionCommonUtil 获取

+ 6 - 6
server/yusp-tagging-core/src/main/java/cn/com/yusys/yusp/service/AitagApiLogService.java

@@ -8,16 +8,16 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 public interface AitagApiLogService {
 
     /**
-     * 记录API调用日志
+     * 记录 API 调用日志(使用 UUID 作为 ID)
      * @param apiLog 日志实体
      */
     void recordApiLog(AitagApiLog apiLog);
 
     /**
-     * 生成日志ID
-     * @return 日志ID
+     * 生成 UUID 日志 ID
+     * @return UUID 格式的日志 ID
      */
-    String generateLogId();
+    String generateUuid();
 
     /**
      * 分页查询日志列表
@@ -35,8 +35,8 @@ public interface AitagApiLogService {
     Page<AitagApiLogListVo> refreshApiLogs(Integer page, Integer pageSize);
 
     /**
-     * 根据ID查询日志详情
-     * @param id 日志ID
+     * 根据 ID 查询日志详情
+     * @param id 日志 ID
      * @return 日志详情
      */
     AitagApiLog getApiLogDetail(String id);

+ 12 - 85
server/yusp-tagging-core/src/main/java/cn/com/yusys/yusp/service/impl/AitagApiLogServiceImpl.java

@@ -10,15 +10,11 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
-import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.List;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.locks.ReentrantLock;
 import java.util.stream.Collectors;
 
 @Service
@@ -29,57 +25,26 @@ public class AitagApiLogServiceImpl implements AitagApiLogService {
     @Autowired
     private AitagApiLogMapper apiLogMapper;
 
-    // 日期格式化器
-    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
-
-    // 缓存当天的最大序列号 key:日期 value:最大序列号
-    private static final Map<String, Long> DATE_SEQUENCE_CACHE = new ConcurrentHashMap<>();
-
-    // 当前缓存的日期
-    private volatile String cachedDate = "";
-
-    private static final ReentrantLock sequenceLock = new ReentrantLock();
-
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public void recordApiLog(AitagApiLog apiLog) {
         try {
+            // 不再需要手动设置 ID,由数据库自增生成
+            // 如果是新插入的记录,ID 应该为 null
+            apiLog.setId(null);
+
             apiLogMapper.insertApiLog(apiLog);
-            logger.info("API 调用日志记录成功:{} - {}", apiLog.getId(), apiLog.getOperationType());
+            logger.info("API 调用日志记录成功,自增 ID: {}", apiLog.getId());
         } catch (Exception e) {
             logger.error("记录 API 调用日志失败:{}", e.getMessage(), e);
+            throw e;
         }
     }
 
     @Override
-    public String generateLogId() {
-        sequenceLock.lock();
-        try {
-            String currentDate = DATE_FORMAT.format(new Date());
-
-            if (!currentDate.equals(cachedDate)) {
-                refreshCache(currentDate);
-            }
-
-            Long currentSequence = DATE_SEQUENCE_CACHE.get(currentDate);
-
-            // 如果缓存中没有序列号,初始化为 0
-            if (currentSequence == null) {
-                currentSequence = 0L;
-            }
-
-            currentSequence++;
-
-            // 序列号达到最大值后重置为 1
-            if (currentSequence > 999999) {
-                currentSequence = 1L;
-            }
-
-            DATE_SEQUENCE_CACHE.put(currentDate, currentSequence);
-
-            return String.format("API%s%06d", currentDate, currentSequence);
-        } finally {
-            sequenceLock.unlock();
-        }
+    public String generateUuid() {
+        // 生成不带横线的 UUID,格式:API + 32 位 UUID
+        return "API" + java.util.UUID.randomUUID().toString().replace("-", "");
     }
 
     @Override
@@ -99,7 +64,7 @@ public class AitagApiLogServiceImpl implements AitagApiLogService {
                 pageSize
         );
 
-        // 转换为VO
+        // 转换为 VO
         List<AitagApiLogListVo> voList = logs.stream().map(log -> {
             AitagApiLogListVo vo = new AitagApiLogListVo();
             BeanUtils.copyProperties(log, vo);
@@ -123,42 +88,4 @@ public class AitagApiLogServiceImpl implements AitagApiLogService {
     public AitagApiLog getApiLogDetail(String id) {
         return apiLogMapper.selectApiLogById(id);
     }
-
-    /**
-     * 刷新缓存 - 每天0点执行
-     */
-    @Scheduled(cron = "0 0 0 * * ?")
-    public void dailyCacheRefresh() {
-        String newDate = DATE_FORMAT.format(new Date());
-        refreshCache(newDate);
-        logger.info("日志ID缓存已刷新,新日期: {}", newDate);
-    }
-
-    /**
-     * 刷新缓存
-     */
-    private synchronized void refreshCache(String newDate) {
-        cachedDate = newDate;
-        DATE_SEQUENCE_CACHE.clear();
-        loadFromDatabase(newDate);
-    }
-
-    /**
-     * 从数据库加载序列号
-     */
-    private void loadFromDatabase(String dateStr) {
-        String maxId = apiLogMapper.getMaxIdByDate(dateStr);
-        long sequence = 0;
-
-        if (maxId != null && maxId.length() >= 17) {
-            try {
-                String sequenceStr = maxId.substring(11, 17);
-                sequence = Long.parseLong(sequenceStr);
-            } catch (Exception e) {
-                logger.warn("解析数据库最大ID序列号失败: {}", e.getMessage());
-            }
-        }
-
-        DATE_SEQUENCE_CACHE.put(dateStr, sequence);
-    }
 }

+ 0 - 8
server/yusp-tagging-core/src/main/resources/mapper/AitagApiLogMapper.xml

@@ -25,14 +25,6 @@
                  )
     </insert>
 
-    <!-- 根据日期获取最大的日志ID -->
-    <select id="getMaxIdByDate" resultType="string">
-        SELECT id
-        FROM aitag_api_log
-        WHERE id LIKE CONCAT('API', #{dateStr}, '%')
-        ORDER BY id DESC
-            LIMIT 1
-    </select>
 
     <!-- 统计符合条件的日志数量 -->
     <select id="countApiLogs" resultType="long">