package com.dxhy.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;

/**
 * mybatis-plus配置
 *
 * @author jiaohongyang
 */
@Configuration
public class MybatisPlusConfig {
    @Value("${dataBase.type:mysql}")
    private String dataBaseType;

    /**
     * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
     * MYSQL("mysql", "MySql数据库"), MARIADB("mariadb", "MariaDB数据库"), ORACLE("oracle",
     * "Oracle11g及以下数据库(高版本推荐使用ORACLE_NEW)"), ORACLE_12C("oracle12c", "Oracle12c+数据库"), DB2("db2", "DB2数据库"), H2("h2",
     * "H2数据库"), HSQL("hsql", "HSQL数据库"), SQLITE("sqlite", "SQLite数据库"), POSTGRE_SQL("postgresql", "Postgre数据库"),
     * SQL_SERVER2005("sqlserver2005", "SQLServer2005数据库"), SQL_SERVER("sqlserver", "SQLServer数据库"), DM("dm", "达梦数据库"),
     * XU_GU("xugu", "虚谷数据库"), KINGBASE_ES("kingbasees", "人大金仓数据库"), PHOENIX("phoenix", "Phoenix HBase数据库"),
     * GAUSS("zenith", "Gauss 数据库"), CLICK_HOUSE("clickhouse", "clickhouse 数据库"), GBASE("gbase", "南大通用数据库"),
     * OSCAR("oscar", "神通数据库"), SYBASE("sybase", "Sybase ASE 数据库"), OCEAN_BASE("oceanbase", "OceanBase 数据库"),
     * FIREBIRD("Firebird", "Firebird 数据库"), OTHER("other", "其他数据库");
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        if (dataBaseType.equals(DbType.MYSQL.getDb())) {
            interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        } else if (dataBaseType.equals(DbType.ORACLE.getDb())) {
            interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.ORACLE));
        } else if (dataBaseType.equals(DbType.ORACLE_12C.getDb())) {
            interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.ORACLE_12C));
        } else if (dataBaseType.equals(DbType.OCEAN_BASE.getDb())) {
            interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.OCEAN_BASE));
        } else if (dataBaseType.equals(DbType.SQL_SERVER.getDb())) {
            interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.SQL_SERVER));
        } else if (dataBaseType.equals(DbType.SQL_SERVER2005.getDb())) {
            interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.SQL_SERVER2005));
        } else if (dataBaseType.equals(DbType.DB2.getDb())) {
            interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.DB2));
        } else {
            interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        }

        return interceptor;
    }

    @Bean
    public ISqlInjector sqlInjector() {
        return new DefaultSqlInjector();
    }
}
