package cn.gov.customs.data.config;

/*
 * @Description:
 * @Author: wq
 * @Date: 2025/11/3.
 */

/**第一个数据源对应的配置文件
 **/
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.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="entityManagerPrimary",
        transactionManagerRef="transactionManagerPrimary",
        basePackages= {"cn.gov.customs.data.repository.wxjy"}) //设置Repository所在位置  dao 文件的路径
public class PrimaryConfig {

    @Autowired
    @Qualifier("primaryDataSource")
    private DataSource primaryDataSource;

    @Primary
    @Bean
    PlatformTransactionManager transactionManagerPrimary() {
        return new JpaTransactionManager(entityManagerPrimary().getObject());
    }

    @Primary
    @Bean(name = "entityManagerPrimary")
    public LocalContainerEntityManagerFactoryBean entityManagerPrimary() {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setDatabase(Database.ORACLE);
//        vendorAdapter.setDatabasePlatform("org.hibernate.dialect.DmDialect");
        vendorAdapter.setShowSql(true);

        LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
        factoryBean.setDataSource(primaryDataSource);
        factoryBean.setJpaVendorAdapter(vendorAdapter);
        factoryBean.setPackagesToScan("cn.gov.customs.data.entity.wxjy*"); //jpa对应的实体的位置
        return factoryBean;
    }
    
     

}
