package com.tofly.common.oauth.config;

import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.parsers.DynamicTableNameParser;
import com.baomidou.mybatisplus.extension.parsers.ITableNameHandler;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.tofly.common.mapper.ToflyCodeCommonMapper;
import com.tofly.common.oauth.handler.MybatisPlusHandler;
import com.tofly.common.oauth.util.ToflyDataTypeConstant;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Collections;
import java.util.HashMap;
import java.util.Properties;

/**
 * @author Administrator
 * @date 2019/9/4  15:58
 */
@Configuration
public class MybatisPlusConfigurer {


    @Autowired
    private ToflyDataTypeConstant toflyDataTypeConstant;


    /**
     * 分页插件
     *
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        paginationInterceptor.setLimit(1000);

        //增加mysql的关键字处理
        String datatype=toflyDataTypeConstant.getDataType();
        switch (datatype){
            case "mysql":
            {
                DynamicTableNameParser dynamicTableNameParser = new DynamicTableNameParser();
                dynamicTableNameParser.setTableNameHandlerMap(new HashMap<String, ITableNameHandler>(2) {{
                    put("TF_SMPN_CODE_X", new ITableNameHandler() {
                        @Override
                        public String dynamicTableName(MetaObject metaObject, String sql, String tableName) {
                            return null;
                        }
                        @Override
                        public String process(MetaObject metaObject, String sql, String tableName) {
//                            String dynamicTableName = this.dynamicTableName(metaObject, sql, tableName);
//                            return null != dynamicTableName && !dynamicTableName.equalsIgnoreCase(tableName) ? sql.replaceAll(tableName, dynamicTableName) : sql;
                            sql=sql.replaceAll(String.format("(?<=[,\\s])%s(?=[,\\s])", "key"),"`key`");
                            sql=sql.replaceAll(String.format("(?<=[,\\s])%s(?=[,\\s])", "value"),"`value`");
                            return sql;
                        }
                    });
                }});
                paginationInterceptor.setSqlParserList(Collections.singletonList(dynamicTableNameParser));
            }
            break;
        }
        return paginationInterceptor;
    }



    /**
     * 自动填充功能
     * @return
     */
    @Bean
    public GlobalConfig globalConfig() {
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setMetaObjectHandler(new MybatisPlusHandler());

        return globalConfig;
    }


    @Bean
    public ConfigurationCustomizer mybatisConfigurationCustomizer(){

        return new ConfigurationCustomizer() {
            @Override
            public void customize(MybatisConfiguration configuration) {
                configuration.setObjectWrapperFactory(new MapWrapperFactory());
            }
        };
    }

    /**
     * 数据库兼容,在mybatis里设置相应的databaseId即可
     */
    @Bean
    public DatabaseIdProvider databaseIdProvider(){
        DatabaseIdProvider databaseIdProvider=new VendorDatabaseIdProvider();
        Properties properties=new Properties();
        properties.setProperty("Oracle","oracle");
        properties.setProperty("MySQL","mysql");
        databaseIdProvider.setProperties(properties);
        return databaseIdProvider;
    }

//    @Bean
//    public ToflySqlCondition toflySqlCondition(){
//        ToflySqlCondition toflySqlCondition=new ToflySqlCondition();
//        String type=druidDataSource.getDbType();
//        if(StringUtil.isNotEmpty(type)){
//            type=type.toLowerCase();
//        }
//        switch (type){
//            case "oracle":
//                toflySqlCondition.like = "%s LIKE '%%'||#{%s}||'%%'";
//                break;
//            case "mysql":
//                toflySqlCondition.like = "%s LIKE CONCAT('%%',#{%s},'%%')";
//                break;
//            default:
//                toflySqlCondition.like = "%s LIKE CONCAT('%%',#{%s},'%%')";
//        }
//        return toflySqlCondition;
//    }

    /* *//**
     * oracle 配置自增
     * @return
     *//*
    @Bean
    public OracleKeyGenerator oracleKeyGenerator(){
        return new OracleKeyGenerator();
    }*/
}
