package com.inkasso.config;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
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.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * @author 张超
 * @date 2021/4/13
 */
@Configuration
@MapperScan(basePackages = {"com.inkasso.**.*.dao"}, sqlSessionTemplateRef = "sqlTemplate1")
public class MybatisPlusOneConfig {

    @Resource(name = "masterDataSource")
    private DataSource dataSource;

    @Autowired
    private MetaObjectHandler metaObjectHandler;

    @Autowired
    private MybatisPlusInterceptor mybatisPlusInterceptor;

    @Bean(name = "sqlFactory1")
    public SqlSessionFactory sqlSessionFactory()
            throws Exception {
        MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        factoryBean.setMapperLocations(resolver.getResources("classpath*:mapping/**/*.xml"));
        MybatisConfiguration config = new MybatisConfiguration();
        config.setMapUnderscoreToCamelCase(true);
        config.setMultipleResultSetsEnabled(true);
        config.setMapUnderscoreToCamelCase(true);
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setBanner(false);

        globalConfig.setMetaObjectHandler(metaObjectHandler);
        GlobalConfig.DbConfig dbConfig = new GlobalConfig.DbConfig();
        dbConfig.setIdType(IdType.ASSIGN_ID).setTableUnderline(true).setLogicDeleteField("delete_flag").setLogicDeleteValue("1").setLogicNotDeleteValue("0");
        globalConfig.setDbConfig(dbConfig);


        GlobalConfigUtils.setGlobalConfig(config,globalConfig);
        factoryBean.setConfiguration(config);

        factoryBean.setPlugins(mybatisPlusInterceptor);

        return factoryBean.getObject();
    }

    @Transactional
    @Bean(name = "sqlTemplate1")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlFactory1") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean
    public PlatformTransactionManager platformTransactionManager(){
         return new DataSourceTransactionManager(dataSource);
    }



}
