package com.chinaweal.aiccs.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.chinaweal.aiccs.config.handler.LocalUserMetaObjectHandler;
import com.chinaweal.publibv2.db.spring.DBComponent;
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * aicorg 系统的数据源
 */

@Configuration
@MapperScan(basePackages = {"com.chinaweal.aiccs.org.**.mapper", "com.chinaweal.aiccs.door.**.mapper"}, sqlSessionTemplateRef = "aicorgSqlSessionTemplate")
public class AicorgDataSource {

    @Bean
    public DBComponent dbComponent(@Qualifier("aicorgDS") DataSource dataSource) {
        DBComponent dbComponet = new DBComponent();
        Map<String, DataSource> dataSourceMap = new HashMap<>();
        dataSourceMap.put("aicOrgDataSource", dataSource);
        dbComponet.setDataSourceMap(dataSourceMap);
        return dbComponet;
    }

    @Bean(name = "aicorgDS", initMethod = "init", destroyMethod = "close")
    @ConfigurationProperties(prefix = "spring.datasource.aicorg")
    public DruidDataSource dataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "aicorgSqlSessionFactory")
    public MybatisSqlSessionFactoryBean sqlSessionFactory(@Qualifier("aicorgDS") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setMetaObjectHandler(new LocalUserMetaObjectHandler());
        bean.setGlobalConfig(globalConfig);
        bean.setConfigLocation(new ClassPathResource("mybatis/mybatis-config.xml"));
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mybatis/mapper/aicorg/**/*.xml"));
        return bean;
    }

    @Bean(name = "aicorgTransactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("aicorgDS") DataSource dataSource) {
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(dataSource);
        dataSourceTransactionManager.setEnforceReadOnly(true);
        return dataSourceTransactionManager;
    }

    @Bean(name = "aicorgSqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("aicorgSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}
