package com.steerinfo;

import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ToStringSerializer;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
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.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.scheduling.annotation.EnableScheduling;
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.WebMvcConfigurer;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.servlet.MultipartConfigElement;
import java.util.ArrayList;
import java.util.List;

@EnableTransactionManagement                      // mybatis中service实现类中加入事务注解
@RestController                                   // 在启动类中注入HttpMessageConverters
@MapperScan(basePackages = {"com.steerinfo.**","com.jisco.**"})
@ComponentScan(basePackages = {"com.steerinfo.**","com.jisco.**"})
@ServletComponentScan(basePackages = {"com.steerinfo.filter","com.jisco.filter"})
@EnableCaching
@SpringBootApplication
@EnableSwagger2
@EnableFeignClients("com.steerinfo.feign")
@EnableCircuitBreaker
@EnableHystrix
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
//@EnableScheduling   // 2.开启定时任务
public class SysManageApplication implements WebMvcConfigurer {

    public static void main(String[] args) {
        try {
            long startTime = System.currentTimeMillis();

            SpringApplication app = new SpringApplication(SysManageApplication.class);
//		app.addListeners(new ContextRefreshedListener());
            app.run(args);

            long endTime = System.currentTimeMillis();
            float excTime = (float) (endTime - startTime) / 1000;
            System.out.println("====================================>执行时间：" + excTime + "s");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @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 值
        SerializeConfig serializeConfig = SerializeConfig.getGlobalInstance();
        serializeConfig.put(Float.TYPE, ToStringSerializer.instance);
        serializeConfig.put(Float.class, ToStringSerializer.instance);
        fastJsonConfig.setSerializeConfig(serializeConfig);

        //3.杨秀东添加添加autotype白名单；并启动autotype功能
//		ParserConfig.getGlobalInstance().addAccept("com.steerinfo.baseinfo.combaseinfo."); //白名单
//		ParserConfig.getGlobalInstance().addDeny("com.steerinfo.baseinfo.meterbasematterinfomatch."); //黑名单
//      ParserConfig.getGlobalInstance().setAutoTypeSupport(true);


        // 4.将配置添加到转换器对象中
        fastConverter.setFastJsonConfig(fastJsonConfig);
        // 5. 解决中文乱码问题
        List<MediaType> mediaTypes = new ArrayList<MediaType>();
        mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        mediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
        fastConverter.setSupportedMediaTypes(mediaTypes);
        // 6.将转换器对象转化为HttpMessageConverter对象
        HttpMessageConverter<?> converter = fastConverter;
        converters.add(fastConverter);
    }

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //单个文件最大
        factory.setMaxFileSize("20MB");
        //设置总上传数据总大小
        factory.setMaxRequestSize("50MB");
        return factory.createMultipartConfig();
    }

}
