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.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.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @Author: 黄子奇
 * @Date: 2019/11/11
 *
 * 主数据源(mysql) 配置
 * 脱离yml的jpa配置
 */
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "entityManagerFactoryPrimary",//配置连接工厂 entityManagerFactory
        transactionManagerRef = "transactionManagerPrimary", //配置 事物管理器  transactionManager
        basePackages = {"com.liangjian.dataplatform.member.dao.primary"}//设置dao（repo）所在位置
)
//@MapperScan("com.liangjian.dataplatform.member.dao.primary")
public class DBPrimaryConfig {

//    @Autowired
//    private JpaProperties jpaProperties;
    @Autowired
    private HibernateProperties hibernateProperties;

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

    @Bean(name = "entityManagerFactoryPrimary")
    @Primary
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
        //这里直接不走yml配置，直接在这里配置上去
        JpaProperties jpaProperties = new JpaProperties();
        Map<String,String> jpaMapProperties = new HashMap<>();
        jpaMapProperties.put("database-platform","com.liangjian.dataplatform.member.util.MySQL5DialectUTF8");
        jpaProperties.setProperties(jpaMapProperties);

        Map<String, Object> properties = hibernateProperties.determineHibernateProperties(
                jpaProperties.getProperties(), new HibernateSettings());

        LocalContainerEntityManagerFactoryBean factoryBean = builder
                //设置数据源
                .dataSource(primaryDataSource)
                //设置数据源属性
                .properties(properties)
                //设置实体类所在位置.扫描所有带有 @Entity 注解的类
                .packages("com.liangjian.dataplatform.member.entity.po.primary")
                // Spring会将EntityManagerFactory注入到Repository之中.有了 EntityManagerFactory之后,
                // Repository就能用它来创建 EntityManager 了,然后Entity就可以针对数据库执行操作
                .persistenceUnit("primaryPersistenceUnit")
                .build();
        factoryBean.setJpaProperties(jpaProperties());
        factoryBean.setJpaVendorAdapter(vendorAdaptor());
        return factoryBean;
    }

    private Properties jpaProperties() {
        Properties properties = new Properties();
        properties.put("database-platform","com.liangjian.dataplatform.member.util.MySQL5DialectUTF8");
        properties.put("show-sql","true");
        properties.put("open-in-view","true");
        properties.put("hibernate.hbm2ddl.auto", "update");
        return properties;
    }

    private HibernateJpaVendorAdapter vendorAdaptor() {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setDatabasePlatform("com.liangjian.dataplatform.member.util.MySQL5DialectUTF8");
        vendorAdapter.setShowSql(true);
        return vendorAdapter;
    }

     /**
     * 事物管理器
     *
     * @param builder
     * @return
     */
    @Bean(name = "transactionManagerPrimary")
    @Primary
    PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
    }

    //mybtais的配置，现在尝试和jpa混合使用
//    @Bean
//    @Primary
//    @Qualifier("primarySqlSession")
//    public MybatisSqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) throws Exception {
//        MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean();
//        mybatisPlus.setDataSource(dataSource);
//
//        GlobalConfig globalConfig = new GlobalConfig();
//        mybatisPlus.setGlobalConfig(globalConfig);
//
//        MybatisConfiguration mc = new MybatisConfiguration();
//        mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
//        mc.setLogImpl(StdOutImpl.class);
//        mybatisPlus.setConfiguration(mc);
//
//        return mybatisPlus;
//    }
}
