package com.liangjian.dataplatform.member.config;

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.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;
import java.util.Properties;

/**
 * @Author: 黄子奇
 * @Date: 2019/11/26
 *
 * 正式数据库， 用来只读的
 */
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="entityManagerFactoryFourthly",
        transactionManagerRef="transactionManagerFourthly",
        basePackages= { "com.liangjian.dataplatform.member.dao.fourthly" })
public class DBFourthlyConfig {

    @Autowired
    private HibernateProperties hibernateProperties;

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

    @Bean(name = "entityManagerFourthly")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryFourthly(builder).getObject().createEntityManager();
    }

    @Bean(name = "entityManagerFactoryFourthly")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryFourthly (EntityManagerFactoryBuilder builder) {
        //这里直接不走yml配置，直接在这里配置上去
        JpaProperties jpaProperties = new JpaProperties();
        //配置表映射的命名规则，不做任何改变，原来的是遇到大写就变小写和下划线
        hibernateProperties.getNaming().setPhysicalStrategy("org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl");
        Map<String, Object> properties = hibernateProperties.determineHibernateProperties(
                jpaProperties.getProperties(), new HibernateSettings());

        LocalContainerEntityManagerFactoryBean factoryBean = builder
                .dataSource(fourthlyDataSource)
                .properties(properties)
                .packages("com.liangjian.dataplatform.member.entity.po.fourthly")
                .persistenceUnit("fourthlyPersistenceUnit")
                .build();
        factoryBean.setJpaProperties(jpaProperties());
        factoryBean.setJpaVendorAdapter(vendorAdaptor());
        return factoryBean;
    }

    private Properties jpaProperties() {
        Properties properties = new Properties();
        properties.put("database-platform","org.hibernate.dialect.SQLServer2012Dialect");
        properties.put("show-sql","true");
        properties.put("open-in-view","true");
//        properties.put("hibernate.hbm2ddl.auto", "validate");
        return properties;
    }

    private HibernateJpaVendorAdapter vendorAdaptor() {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setDatabasePlatform("org.hibernate.dialect.SQLServer2012Dialect");
        vendorAdapter.setShowSql(true);
        return vendorAdapter;
    }

    @Bean(name = "transactionManagerFourthly")
    PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryFourthly(builder).getObject());
    }
}
