package com.kexue.skills.config;

import com.kexue.skills.interceptor.LogInterceptor;
import com.kexue.skills.mapper.SysLogMapper;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * 日志配置类
 * 注册日志拦截器，启用异步支持
 * 
 * @author 王志维
 * @since 2026-04-14
 */
@Configuration
@EnableAsync  // 启用异步支持
public class LogConfiguration implements WebMvcConfigurer {
    
    @Resource
    private SysLogMapper sysLogMapper;
    
    /**
     * 注册拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        LogInterceptor logInterceptor = new LogInterceptor();
        logInterceptor.setSysLogMapper(sysLogMapper);
        registry.addInterceptor(logInterceptor)
                .addPathPatterns("/api/**")  // 拦截所有 API 请求
                .excludePathPatterns(
                        "/api/login/validateToken",  // 排除 token 验证接口
                        "/api/captcha/**"             // 排除验证码接口
                );
    }
}
