|
|
@@ -0,0 +1,159 @@
|
|
|
+package cn.com.yusys.yusp.service.impl;
|
|
|
+
|
|
|
+import cn.com.yusys.yusp.domain.dto.AitagApiLogQueryDTO;
|
|
|
+import cn.com.yusys.yusp.domain.entity.AitagApiLog;
|
|
|
+import cn.com.yusys.yusp.domain.vo.AitagApiLogListVO;
|
|
|
+import cn.com.yusys.yusp.mapper.AitagApiLogMapper;
|
|
|
+import cn.com.yusys.yusp.service.AitagApiLogService;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+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 java.text.SimpleDateFormat;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class AitagApiLogServiceImpl implements AitagApiLogService {
|
|
|
+
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(AitagApiLogServiceImpl.class);
|
|
|
+
|
|
|
+ @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 = "";
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void recordApiLog(AitagApiLog apiLog) {
|
|
|
+ try {
|
|
|
+ apiLogMapper.insertApiLog(apiLog);
|
|
|
+ logger.info("API调用日志记录成功: {} - {}", apiLog.getId(), apiLog.getOperationType());
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("记录API调用日志失败: {}", e.getMessage(), e);
|
|
|
+ // 不抛出异常,避免影响主业务流程
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String generateLogId() {
|
|
|
+ String currentDate = DATE_FORMAT.format(new Date());
|
|
|
+
|
|
|
+ // 检查是否需要刷新缓存(跨天了)
|
|
|
+ if (!currentDate.equals(cachedDate)) {
|
|
|
+ refreshCache(currentDate);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 从缓存获取并递增序列号
|
|
|
+ Long currentSequence = DATE_SEQUENCE_CACHE.get(currentDate);
|
|
|
+ if (currentSequence == null) {
|
|
|
+ // 缓存中没有数据,从数据库查询
|
|
|
+ loadFromDatabase(currentDate);
|
|
|
+ currentSequence = DATE_SEQUENCE_CACHE.get(currentDate);
|
|
|
+ }
|
|
|
+
|
|
|
+ currentSequence++;
|
|
|
+ DATE_SEQUENCE_CACHE.put(currentDate, currentSequence);
|
|
|
+
|
|
|
+ // 重置序列号(超过6位数限制)
|
|
|
+ if (currentSequence > 999999) {
|
|
|
+ currentSequence = 1L;
|
|
|
+ DATE_SEQUENCE_CACHE.put(currentDate, currentSequence);
|
|
|
+ }
|
|
|
+
|
|
|
+ return String.format("API%s%06d", currentDate, currentSequence);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<AitagApiLogListVO> listApiLogs(AitagApiLogQueryDTO queryDTO) {
|
|
|
+ int page = queryDTO.getPage() != null ? queryDTO.getPage() : 1;
|
|
|
+ int pageSize = queryDTO.getPageSize() != null ? queryDTO.getPageSize() : 10;
|
|
|
+ int offset = (page - 1) * pageSize;
|
|
|
+
|
|
|
+ // 查询总记录数
|
|
|
+ long total = apiLogMapper.countApiLogs(queryDTO.getStartTime(), queryDTO.getEndTime());
|
|
|
+
|
|
|
+ // 查询列表数据
|
|
|
+ List<AitagApiLog> logs = apiLogMapper.selectApiLogs(
|
|
|
+ queryDTO.getStartTime(),
|
|
|
+ queryDTO.getEndTime(),
|
|
|
+ offset,
|
|
|
+ pageSize
|
|
|
+ );
|
|
|
+
|
|
|
+ // 转换为VO
|
|
|
+ List<AitagApiLogListVO> voList = logs.stream().map(log -> {
|
|
|
+ AitagApiLogListVO vo = new AitagApiLogListVO();
|
|
|
+ BeanUtils.copyProperties(log, vo);
|
|
|
+ return vo;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+
|
|
|
+ Page<AitagApiLogListVO> pageResult = new Page<>(page, pageSize, total);
|
|
|
+ pageResult.setRecords(voList);
|
|
|
+ return pageResult;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<AitagApiLogListVO> refreshApiLogs(Integer page, Integer pageSize) {
|
|
|
+ AitagApiLogQueryDTO queryDTO = new AitagApiLogQueryDTO();
|
|
|
+ queryDTO.setPage(page != null ? page : 1);
|
|
|
+ queryDTO.setPageSize(pageSize != null ? pageSize : 10);
|
|
|
+ return listApiLogs(queryDTO);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+}
|