package com.simuwang.base.config;

import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.simuwang.base.components.MyBatisPlusMetaObjectHandler;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;

import javax.sql.DataSource;
import java.util.List;

@MapperScan(value = {"com.simuwang.base.mapper"}, sqlSessionFactoryRef = "sqlSessionFactory")
@Configuration
public class DataSourceAutoConfig {
    static final String[] MAPPER_LOCATIONS = {"classpath*:mapper/**/*.xml"};

    @Autowired
    @Qualifier(DataSourceConfiguration.DS_DATA_DAQ)
    private DataSource dataSource;

    @Primary
    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        // sql session 配置
        final MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
        sessionFactory.setDataSource(this.dataSource);
        // 扫描追加多个包下的资源文件
        List<Resource> resources = ListUtil.list(false);
        for (String mapperLocation : MAPPER_LOCATIONS) {
            PathMatchingResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
            Resource[] resource = patternResolver.getResources(mapperLocation);
            if (resource.length > 0) {
                resources.addAll(ListUtil.toList(resource));
            }
        }
        sessionFactory.setMapperLocations(resources.toArray(new Resource[]{}));

        // 添加mybatis-plus配置
        MybatisConfiguration configuration = new MybatisConfiguration();
        // 添加分页插件
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        configuration.addInterceptor(interceptor);
        // 设置下划线转驼峰
        configuration.setMapUnderscoreToCamelCase(true);
        sessionFactory.setConfiguration(configuration);

        // 全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setBanner(false);
        globalConfig.setMetaObjectHandler(new MyBatisPlusMetaObjectHandler());
        sessionFactory.setGlobalConfig(globalConfig);
        return sessionFactory.getObject();
    }

    @Primary
    @Bean(name = "transactionManager")
    public DataSourceTransactionManager transactionManager() {
        return new DataSourceTransactionManager(this.dataSource);
    }

    @Primary
    @Bean(name = "transactionTemplate")
    public TransactionTemplate transactionTemplate() {
        return new TransactionTemplate(this.transactionManager());
    }
}

