package com.mortals.xhx.base.framework.config;

import com.mortals.framework.springcloud.config.mybatis.AbstractMybatisMasterSlaveDataSourceConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

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


//@Configuration
//@AutoConfigureAfter(DataSourceAutoConfiguration.class)
public class MybatisConfigurationMasterSlave extends AbstractMybatisMasterSlaveDataSourceConfiguration {
    private static Log logger = LogFactory.getLog(MybatisConfigurationMasterSlave.class);

    // 配置类型别名
    @Value("${spring.application.name}")
    private String name;
    // 配置类型别名
    @Value("${mybatis.root-path}")
    private String rootPath;

    // 配置类型别名
    @Value("${mybatis.type-aliases-package}")
    private String typeAliasesPackage;

    // 配置mapper的扫描，找到所有的mapper.xml映射文件
    @Value("${mybatis.mapper-locations}")
    private String mapperLocations;

    // 加载全局的配置文件
    @Value("${mybatis.config-location}")
    private String configLocation;

    // 提供SqlSeesion
    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean(@Qualifier("dataSource") DataSource dataSource) {
        return super.getSqlSessionFactoryBean(dataSource);
    }

    @Override
    public String getTypeAliasesRootPackage() {
        return rootPath;
    }

    @Override
    public String getSqlMappers() {
        return mapperLocations;
    }

    @Override
    public String getMybatisConfig() {
        return configLocation;
    }

    @Override
    public String getTypeAliasesPackage() {
        logger.info("typeAliasesPackage:"+typeAliasesPackage);
        return typeAliasesPackage;
    }

    @Override
    public DataSource getDefaultDataSource() {
        return null;
    }

    @Override
    public List<DataSource> findMasterDataSource() {
        return null;
    }

    @Override
    public List<DataSource> findSlaveDataSource() {
        return null;
    }
}