|
|
@@ -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);
|
|
|
- }
|
|
|
}
|