package me.zhengjie.config.jpa;

import java.util.Map;

import javax.persistence.EntityManager;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
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.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactoryPrimary", transactionManagerRef = "transactionManagerPrimary", basePackages = {
                "me.zhengjie.repository",
                "me.zhengjie.modules.company.repository",
                "me.zhengjie.modules.flow.repository",
                // "me.zhengjie.modules.goods.repository",
                "me.zhengjie.modules.mnt.repository",
                "me.zhengjie.modules.quartz.repository",
                "me.zhengjie.modules.security.repository",
                "me.zhengjie.modules.store.repository",
                "me.zhengjie.modules.system.repository",
                "me.zhengjie.modules.supplier.repository"
})
public class JPAPrimaryConfig {

        @Autowired
        private JpaProperties jpaProperties;

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

        @Autowired
        private HibernateProperties hibernateProperties;

        @Bean(name = "entityManagerFactoryPrimary")
        @Primary
        public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
                System.out.println(hibernateProperties);
                Map<String, Object> properties = hibernateProperties.determineHibernateProperties(
                                jpaProperties.getProperties(),
                                new HibernateSettings());
                return builder.dataSource(primaryDataSource).properties(properties)
                                .packages(
                                                "me.zhengjie.domain",
                                                "me.zhengjie.modules.company.domain",
                                                "me.zhengjie.modules.flow.domain",
                                                // "me.zhengjie.modules.goods.domain",
                                                "me.zhengjie.modules.mnt.domain",
                                                "me.zhengjie.modules.quartz.domain",
                                                "me.zhengjie.modules.security.domain",
                                                "me.zhengjie.modules.store.domain",
                                                "me.zhengjie.modules.system.domain",
                                                "me.zhengjie.modules.supplier.domain")
                                .persistenceUnit("primaryPersistenceUnit").build();
        }

        @Primary
        @Bean(name = "entityManagerPrimary")
        public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
                return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
        }

        @Primary
        @Bean(name = "transactionManagerPrimary")
        public PlatformTransactionManager transactionManagerPrimary(
                        EntityManagerFactoryBuilder builder) {
                return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
        }
}
