/**
 * 
 */
package org.springblade.core.config;

import org.springblade.core.mp.processor.YmlDsProcessor;
import org.springblade.core.tool.utils.SpringUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.baomidou.dynamic.datasource.processor.DsHeaderProcessor;
import com.baomidou.dynamic.datasource.processor.DsProcessor;
import com.baomidou.dynamic.datasource.processor.DsSessionProcessor;
import com.baomidou.dynamic.datasource.processor.DsSpelExpressionProcessor;

/**
 * @author wookvn
 *
 */
@Configuration
public class GeometryConfiguration {
	
	@Value("${spring.profiles.active}")
	private String env;
	
	/**
	 * Spring上下文缓存
	 */
	@Bean
	public SpringUtil springUtil() {
		return new SpringUtil();
	}

	/**
	 * @return
	 */
	@Bean
	@Primary
    public DsProcessor ymlDsProcessorConfigurer(){
		DsHeaderProcessor headerProcessor = new DsHeaderProcessor();
        DsSessionProcessor sessionProcessor = new DsSessionProcessor();
        DsSpelExpressionProcessor spelExpressionProcessor = new DsSpelExpressionProcessor();
        
        Resource app = new ClassPathResource("application.yml");
        Resource appEnv = new ClassPathResource("application-"+env+".yml");
        YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
        yamlPropertiesFactoryBean.setResources(app, appEnv);
        YmlDsProcessor ymlDsProcessor = new YmlDsProcessor(yamlPropertiesFactoryBean.getObject());
        
        headerProcessor.setNextProcessor(sessionProcessor);
        sessionProcessor.setNextProcessor(ymlDsProcessor);
        ymlDsProcessor.setNextProcessor(spelExpressionProcessor);
        return headerProcessor;
    }
}
