package com.zy.ai.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.zy.ai.entity.LlmCallLog; import com.zy.ai.mapper.LlmCallLogMapper; import com.zy.ai.service.LlmCallLogService; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @Service("llmCallLogService") @Slf4j public class LlmCallLogServiceImpl extends ServiceImpl implements LlmCallLogService { private volatile boolean disabled = false; private volatile long lastRetryTime = 0; private static final long RETRY_INTERVAL_MS = 60_000; // 1 分钟后重试 @Override public void saveIgnoreError(LlmCallLog logItem) { if (logItem == null) { return; } if (disabled) { // 定期重试,防止表后来创建了但 disabled 一直为 true long now = System.currentTimeMillis(); if (now - lastRetryTime < RETRY_INTERVAL_MS) { return; } lastRetryTime = now; log.info("LLM调用日志之前已禁用,尝试重新写入..."); } try { save(logItem); if (disabled) { disabled = false; log.info("LLM调用日志写入成功,已恢复日志记录"); } } catch (Exception e) { String msg = e.getMessage() == null ? "" : e.getMessage(); if (msg.contains("doesn't exist") || msg.contains("不存在")) { disabled = true; lastRetryTime = System.currentTimeMillis(); log.warn("LLM调用日志表不存在,日志记录已暂停,请先执行建表SQL"); return; } log.warn("写入LLM调用日志失败: {}", msg); } } }