package cn.gov.customs.data.config;

import javax.sql.DataSource;

/*
 * @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.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;

/**
 * transactionManagerRef 事务管理工厂引用名称，对应到@Bean注解对应的方法
 * entityManagerFactoryRef 实体管理工厂引用名称，对应到@Bean注解对应的方法
 */

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

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


    @Bean
    PlatformTransactionManager transactionManagerApp() {
        return new JpaTransactionManager(entityManagerApp().getObject());
    }

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

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