package com.muchinfo.mtp3century.configuration;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.muchinfo.mtp3century.interceptor.SignAuthInterceptor;
import com.muchinfo.mtp3interface.mapper.IWskhOpenaccountconfigMapper;
import com.muchinfo.mtp3pojos.entity.WskhOpenaccountconfig;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;


@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Resource
    private SignAuthInterceptor signAuthInterceptor;

    @Resource
    private IWskhOpenaccountconfigMapper iWskhOpenaccountconfigMapper;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        WskhOpenaccountconfig configtype = iWskhOpenaccountconfigMapper.selectOne(new QueryWrapper<WskhOpenaccountconfig>().eq("CONFIGTYPE", 6));

        registry.addResourceHandler("/uploadFile/**")
                .addResourceLocations("file:"+configtype.getConfigvalue()+"/uploadFile/");
    }

    /**
     * //1.加入的顺序就是拦截器执行的顺序，
     * //2.按顺序执行所有拦截器的preHandle
     * //3.所有的preHandle 执行完再执行全部postHandle 最后是postHandle
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(signAuthInterceptor)
                //所有请求都需要进行报文签名sign
                .addPathPatterns("/**")
                //排除部分拦截
                .excludePathPatterns(
                        "/login",
                        "/swagger-ui.html",
                        "/systemmanager/verifyCode",
                        "/common/getCheckSign",
                        "/swagger-resources/**",
                        "/webjars/**",
                        "/uploadFile/**",
                        "/**/api-docs");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowCredentials(false)
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "OPTIONS")
                .maxAge(3600);
    }
}
