package cn.gov.customs.data.config;

import cn.gov.customs.data.repository.rh2k.Rh2kRepository;
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.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;

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

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="entityManagerRh2k",
        transactionManagerRef="transactionManagerRh2k",
        basePackages= {"cn.gov.customs.data.repository.rh2k"},
        basePackageClasses = {Rh2kRepository.class}) //设置Repository所在位置  dao 文件的路径
public class Rh2kConfig {

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


    @Bean
    PlatformTransactionManager transactionManagerRh2k() {
        return new JpaTransactionManager(entityManagerRh2k().getObject());
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerRh2k() {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setDatabase(Database.MYSQL);
        vendorAdapter.setShowSql(true);

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