package com.junmp.jyzb.boot;

import com.junmp.jyzb.cache.DeviceRedisCache;
import com.junmp.v2.db.starter.JpDataSourceAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Collection;

/**
 * <pre>
 *
 * 描述：
 * 版本：1.0.0
 * 日期：2022/7/29 15:39
 * 作者：ningzp@junmp.com.cn
 * <br>修改记录
 * <br>修改日期        修改人          修改内容
 *
 * </pre>
 */
@EnableScheduling
@EnableTransactionManagement
@EnableSwagger2
@EnableElasticsearchRepositories(basePackages = "com.junmp.jyzb.Repository")
@SpringBootApplication(scanBasePackages = {"com.junmp"}, exclude = {FlywayAutoConfiguration.class,
        JpDataSourceAutoConfiguration.class})
public class JyzbBootApplication extends SpringBootServletInitializer {

    @Resource
    private DeviceRedisCache redisCache;

    public static void main(String[] args) {
        SpringApplication.run(JyzbBootApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(JyzbBootApplication.class);
    }
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedOrigins("http://127.0.0.1:8080")
                        .allowedMethods("GET", "POST", "PUT", "DELETE")
                        .allowedHeaders("*")
                        .allowCredentials(true);
            }
        };
    }
    @PostConstruct
    public void clearRedisCacheOnStartup() {
        Collection<String> allKeys=redisCache.getAllKeys();
        allKeys.forEach(p->redisCache.remove(p));

    }

}
