package com.jiayue.ssi.config;

import com.jiayue.ssi.constant.CacheConstants;
import com.jiayue.ssi.entity.SysBlacklist;
import com.jiayue.ssi.interceptor.TokenStatusInterceptor;
import com.jiayue.ssi.service.SysBlacklistService;
import com.jiayue.ssi.service.SysParameterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
* MVC配置
*
* @author xsl
* @since 2023/03/03
*/
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Autowired
    SysParameterService sysParameterService;
    @Autowired
    SysBlacklistService sysBlacklistService;

    @Override
    public void addInterceptors(InterceptorRegistry registry){
        // 刷新token拦截器注册
        registry.addInterceptor(new TokenStatusInterceptor()).addPathPatterns("/**")
                .excludePathPatterns("/sysParameterController/getUseSendMail","/refreshToken","/error","/getMailCode","/getVerifyCode","/login","/index.html","/user/login","/css/**","/images/**","/js/**","/fonts/**");
    }
    /**
     * 初始加载
     */
    @Bean
    public void initLoad() {
        // 加载上报对象缓存
        String useSendMail = sysParameterService.queryByKey("useSendMail", "true");
        CacheConstants.use_send_mail = Boolean.parseBoolean(useSendMail);
    }

    /**
     * 加载黑名单
     */
    @Bean
    public void initBlackList() {
        // 加载上报对象缓存
        List<SysBlacklist> sysBlacklists = sysBlacklistService.list();
        for (SysBlacklist sysBlacklist:sysBlacklists){
            CacheConstants.blacklistMap.put(sysBlacklist.getIp(),sysBlacklist.getId());
        }
    }
}
