package com.yifu.cloud.plus.v1.yifu.insurances.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;

import javax.sql.DataSource;

/**
 * @author zhaji
 * @description TODO
 * @date 2022-08-25 16:57:06
 */
@Configuration
@MapperScan(basePackages = "com.yifu.cloud.plus.v1.yifu.insurances.mapper.insurances", sqlSessionTemplateRef = "insurancesSqlSessionTemplate", sqlSessionFactoryRef = "insurancesSqlSessionFactory")
public class InsurancesDataSourceConfig {

	@Bean(name = "insurancesDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.insurances")
	@Primary
    public DataSource insurancesDataSource() {
		         return DataSourceBuilder.create().build();
	}
	@Bean
     public SqlSessionFactory insurancesSqlSessionFactory(@Qualifier("insurancesDataSource") DataSource dataSource) throws Exception {
		MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		//添加XML目录
		ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		try {
			bean.setMapperLocations(resolver.getResources("classpath:mapper/insurances/*.xml"));
			//分页插件配置,不配置分页插件无效,否则会出现total为0的情况
			bean.setPlugins(insurancesPlusInterceptor());
			return bean.getObject();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
/*		MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		MybatisConfiguration configuration = new MybatisConfiguration();
		configuration.setMapUnderscoreToCamelCase(true);
		bean.setConfiguration(configuration);
		bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/insurances/*.xml"));
		return bean.getObject();*/
	}


	@Bean
     public DataSourceTransactionManager insurancesTransactionManager(@Qualifier("insurancesDataSource") DataSource dataSource) {
		         return new DataSourceTransactionManager(dataSource);
	}
	@Bean
     public SqlSessionTemplate insurancesSqlSessionTemplate(@Qualifier("insurancesSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
		        return new SqlSessionTemplate(sqlSessionFactory);
	}
	@Bean
	public TransactionTemplate insurancesTransactionTemplate(@Qualifier("insurancesTransactionManager") DataSourceTransactionManager dataSourceTransactionManager) {
	         return new TransactionTemplate(dataSourceTransactionManager);
   }

	@Bean
	public MybatisPlusInterceptor insurancesPlusInterceptor() {
		MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
		interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
		return interceptor;
	}
}
