package com.scm.contract.config;


import java.util.Properties;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import com.scm.contract.properties.MybatisWzhtProperties;
import com.set.direwolf.core.mybatis.DirewolfMapperScan;
import com.set.direwolf.core.mybatis.PageInterceptor;
import com.set.direwolf.core.mybatis.UpdateInterceptor;
import com.zaxxer.hikari.HikariDataSource;

@Configuration
@EnableConfigurationProperties(MybatisWzhtProperties.class)
@DirewolfMapperScan(basePackages = "${mybatis.config2.type-aliases-package}",sqlSessionTemplateRef = "sqlSessionTemplate2")
public class MybatisPlusWzhtConfigurer {
	@Autowired
	private MybatisWzhtProperties properties;


	@Value("${mybatis.config2.configuration.ds-name}")
	private String dsName;

	@Value("${mybatis.config2.configuration.ds-type}")
	private String dsType;
	/**
	 * 创建工厂
	 *@param dataSource
	 *@throws Exception
	 *@return SqlSessionFactory
	 */
	@Bean(name = "sqlSessionFactory2")
	public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource2") HikariDataSource dataSource) throws Exception {
		SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
		factory.setDataSource(dataSource);
		factory.setVfs(SpringBootVFS.class);

		applyConfiguration(factory);
		if (this.properties.getConfigurationProperties() != null) {
			factory.setConfigurationProperties(this.properties.getConfigurationProperties());
		}
		if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
			factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
		}
		if (this.properties.getTypeAliasesSuperType() != null) {
			factory.setTypeAliasesSuperType(this.properties.getTypeAliasesSuperType());
		}
		if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
			factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
		}
		Interceptor[] plugins = new Interceptor[] { pageInterceptor(), updateInterceptor() };
		if (!ObjectUtils.isEmpty(plugins)) {
			factory.setPlugins(plugins);
		}
		if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
			factory.setMapperLocations(this.properties.resolveMapperLocations());
		}

		return factory.getObject();
	}

	/**
	 * 创建事务
	 *@param dataSource
	 *@return DataSourceTransactionManager
	 */
	@Bean(name = "transactionManager2")
	public DataSourceTransactionManager transactionManager(@Qualifier("dataSource2") HikariDataSource dataSource) {
		return new DataSourceTransactionManager(dataSource);
	}

	/**
	 * 创建模板
	 *@param sqlSessionFactory
	 *@return SqlSessionTemplate
	 */
	@Bean(name = "sqlSessionTemplate2")
	public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory2") SqlSessionFactory sqlSessionFactory) {
		return new SqlSessionTemplate(sqlSessionFactory);
	}

	public PageInterceptor pageInterceptor() {
		Properties props = new Properties();
		props.setProperty("dsName", dsName);
		props.setProperty("dsType", dsType);
		PageInterceptor interceptor = new PageInterceptor();
		interceptor.setProperties(props);
		return interceptor;
	}

	public UpdateInterceptor updateInterceptor() {
		Properties props = new Properties();
		props.setProperty("dsName", dsName);
		props.setProperty("dsType", dsType);
		UpdateInterceptor interceptor = new UpdateInterceptor();
		interceptor.setProperties(props);
		return interceptor;
	}

	private void applyConfiguration(SqlSessionFactoryBean factory) {
		org.apache.ibatis.session.Configuration configuration = this.properties.getConfiguration();
		if (configuration == null && !StringUtils.hasText(this.properties.getConfigLocation())) {
			configuration = new org.apache.ibatis.session.Configuration();
		}
		factory.setConfiguration(configuration);
	}

}
