package com.steerinfo;

import java.util.ArrayList;
import java.util.List;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.scheduling.annotation.EnableScheduling;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableTransactionManagement                      // mybatis中service实现类中加入事务注解
@RestController                                   // 在启动类中注入HttpMessageConverters
@MapperScan(basePackages={"com.steerinfo.**"})
@ComponentScan(basePackages={"com.steerinfo.**"})
@EnableCaching
@SpringBootApplication
@EnableSwagger2
@EnableFeignClients
@EnableCircuitBreaker
@EnableHystrix
public class SysManageApplication implements WebMvcConfigurer{
	
	public static void main(String[] args) {
		SpringApplication app = new SpringApplication(SysManageApplication.class);
		// app.addListeners(new ContextRefreshedListener());
		app.run(args);
	}
	
	@Bean
	public RequestContextListener requestListener() {
		return new RequestContextListener();
	}
	
	@Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		// 1.创建一个converter对象
		FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
		// 2.创建配置对象
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		// 3.添加配置
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");                       // 自定义时间格式
        fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue); // 正常转换 null 值

		// 4.将配置添加到转换器对象中
		fastConverter.setFastJsonConfig(fastJsonConfig);
		// 5. 解决中文乱码问题
		List<MediaType> mediaTypes = new ArrayList<MediaType>();
		mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
		fastConverter.setSupportedMediaTypes(mediaTypes);
		// 6.将转换器对象转化为HttpMessageConverter对象
		HttpMessageConverter<?> converter = fastConverter;
        // 7.增加一行注解
		converters.add(fastConverter);
	}
	
	@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/static/**").addResourceLocations("file:/static/");
	}
	
}
