package cn.com.taiji.mqcrj.config;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.*;

/**
 * @author chen mh
 * @date 2023/7/28 16:08
 */
@Configuration
public class MybatisPlusConfig {
    // 这里是存储需要动态变化的表，防止乱操作
    static List<String> tableList(){
        List<String> tables = new ArrayList<>();
        //伪表名  可以为任意字符串 建议设置复杂度 避免重复    tables.add("C55EA8171877E962E08DFF63AA3678841");
        tables.add("v_sg_");
        return tables;
    }
    //拦截器，获取到表名给替换
    @Bean
    public MybatisPlusInterceptor dynamicTableNameInnerInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
        dynamicTableNameInnerInterceptor.setTableNameHandler((sql, tableName) -> {
            String newTable = null;
            for (String table : tableList()) {
                newTable = RequestDataHelper.getRequestData(table);
                if (table.equals(tableName) && newTable!=null){
                    tableName = newTable;
                    break;
                }
            }
            return tableName;
        });
        interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
        return interceptor;
    }
}


