use crate::audit::AuditLog; use crate::entity::audit_log; use sea_orm::{ActiveModelTrait, Set}; use tracing; /// 持久化审计日志到 audit_logs 表。 /// /// 使用 fire-and-forget 模式:失败仅记录 warning 日志,不影响业务操作。 pub async fn record(log: AuditLog, db: &sea_orm::DatabaseConnection) { let model = audit_log::ActiveModel { id: Set(log.id), tenant_id: Set(log.tenant_id), user_id: Set(log.user_id), action: Set(log.action), resource_type: Set(log.resource_type), resource_id: Set(log.resource_id), old_value: Set(log.old_value), new_value: Set(log.new_value), ip_address: Set(log.ip_address), user_agent: Set(log.user_agent), created_at: Set(log.created_at), }; if let Err(e) = model.insert(db).await { tracing::warn!(error = %e, "审计日志写入失败"); } }