package com.platomix.userprofile.config;

import java.util.HashMap;
import java.util.Map;

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

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;

@Configuration
public class MybatisPlusConfig {

	@Value("${dflow.use-debug}")
	private Boolean useDebug;

//	@Bean
//	public DataFlowInterceptor dataFlowInterceptor() {
//		return new DataFlowInterceptor();
//	}

	@Bean
	public MybatisPlusInterceptor paginationInterceptor() {
		MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
		// 动态表名插件
		DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
		Map<String, TableNameHandler> tableNameHandlerMap = new HashMap<>();
		tableNameHandlerMap.put("ads_userprofile_tag", (sql, tableName) -> {
			return useDebug ? "ads_userprofile_tag_dev" : tableName;
		});
		tableNameHandlerMap.put("ads.DIC_USERTAG", (sql, tableName) -> {
			return useDebug ? "ads.DIC_USERTAG_DEV" : tableName;
		});
		tableNameHandlerMap.put("ads.Z_USER_TAG_FLAT_temp", (sql, tableName) -> {
			return useDebug ? "ads.Z_USER_TAG_FLAT_temp_dev" : tableName;
		});
		tableNameHandlerMap.put("ads.Z_USER_TAG_FLAT_clu1", (sql, tableName) -> {
			return useDebug ? "ads.Z_USER_TAG_FLAT_clu1_dev" : tableName;
		});
		tableNameHandlerMap.put("ads.Z_USER_TAG_FLAT_ALL", (sql, tableName) -> {
			return useDebug ? "ads.Z_USER_TAG_FLAT_ALL_dev" : tableName;
		});
		dynamicTableNameInnerInterceptor.setTableNameHandlerMap(tableNameHandlerMap);
		interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
		return interceptor;
	}
}
