package com.njzscloud.common.mvc.config;

import com.njzscloud.common.core.jackson.serializer.BigDecimalModule;
import com.njzscloud.common.core.jackson.serializer.LongModule;
import com.njzscloud.common.core.jackson.serializer.TimeModule;
import com.njzscloud.common.mvc.support.DictHandlerMethodArgumentResolver;
import com.njzscloud.common.mvc.support.GlobalExceptionController;
import com.njzscloud.common.mvc.support.ReusableRequestFilter;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Spring MVC 配置
 */
@Configuration(proxyBeanMethods = false)
public class MvcAutoConfiguration {
    /**
     * Jackson 配置
     */
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return build -> build
                .modules(new TimeModule(), new LongModule(), new BigDecimalModule());
    }

    @Bean
    public DictHandlerMethodArgumentResolver dictHandlerMethodArgumentResolver() {
        return new DictHandlerMethodArgumentResolver();
    }

    @Bean
    public GlobalExceptionController globalExceptionController() {
        return new GlobalExceptionController();
    }

    @Bean
    public FilterRegistrationBean<ReusableRequestFilter> requestBodyCacheFilter() {
        FilterRegistrationBean<ReusableRequestFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new ReusableRequestFilter());
        registration.addUrlPatterns("/*");
        registration.setName("reusableRequestFilter");
        registration.setOrder(0);
        return registration;
    }
}
