package com.slodon.b2b2c.investment.datasource;

import com.slodon.b2b2c.core.constant.StarterConfigConst;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * @author sunshihao
 * @version 1.0
 * @description: TODO
 * @date 2025/8/29 15:14
 */
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(InvestmentProperties.class)
//数据源对象dao所在包-read
@MapperScan(basePackages = "com.slodon.b2b2c.investment.mapper",sqlSessionFactoryRef = "sqlSessionFactoryInvestment")

public class InvestmentConfiguration {

    @Autowired
    private InvestmentProperties investmentProperties;

    /*-------------多数据源配置开始，配合properties文件配置数据源------------------*/

    /**
     * read数据源
     * @return
     */
    @Bean(value = "dataSourceInvestment")
    public DataSource dataSource0() {
        return DataSourceBuilder.create()
                .driverClassName(investmentProperties.getDriverClassName())
                .url(investmentProperties.getUrl())
                .username(investmentProperties.getUsername())
                .password(investmentProperties.getPassword())
                .build();
    }

    @Bean(value = "sqlSessionFactoryInvestment")
    public SqlSessionFactory sqlSessionFactory0(@Qualifier("dataSourceInvestment") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/**/*.xml"));
        return bean.getObject();
    }
}
