package com.hdkj.lt.core.bizms.mp;

import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;
import org.apache.commons.lang3.StringUtils;

/**
 * 由于分表，动态表明拦截器，该拦截器只会在一次sql查询中生效，如果多次sql操作那么需要寻求其他解决方案
 */
public class ArgDynamicTableNameInnerInterceptor extends DynamicTableNameInnerInterceptor {

    @Override
    public String changeTable(String sql) {
        // 设置了表类型才执行动态表名处理
        if (super.getTableNameHandler() instanceof ArgTableNameHandler &&
                (StringUtils.isNotBlank(ArgTableNameHandler.getArgument()) ||
                        StringUtils.isNotBlank(ArgTableNameHandler.getArgumentFunc()))) {
            try {
                return super.changeTable(sql);
            } finally {
                ArgTableNameHandler.remove();
            }
        }
        return sql;
    }

}
