All Downloads are FREE. Search and download functionalities are using the official Maven repository.

im.shs.tick.mybatis.handler.TenantHandler Maven / Gradle / Ivy

The newest version!
package im.shs.tick.mybatis.handler;

import im.shs.tick.tenant.TenantProperties;
import im.shs.tick.tenant.TenantContextHolder;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.NullValue;
import net.sf.jsqlparser.expression.StringValue;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 租户维护处理器
 */
@Slf4j
public class TenantHandler implements com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler {
	@Autowired
	private TenantProperties properties;

	/**
	 * 获取租户值
	 *
	 * @return 租户值
	 */
	@Override
	public Expression getTenantId(boolean where) {
		String tenantNo = TenantContextHolder.getTenantNo();
		log.debug("当前租户为 >> {}", tenantNo);

		if (tenantNo == null) {
			return new NullValue();
		}
		return new StringValue(tenantNo);
	}

	/**
	 * 获取租户字段名
	 *
	 * @return 租户字段名
	 */
	@Override
	public String getTenantIdColumn() {
		return properties.getColumn();
	}

	/**
	 * 根据表名判断是否进行过滤
	 *
	 * @param tableName 表名
	 * @return 是否进行过滤
	 */
	@Override
	public boolean doTableFilter(String tableName) {
		String tenantNo = TenantContextHolder.getTenantNo();
		// 租户中ID 为空,查询全部,不进行过滤
		if (tenantNo == null) {
			return Boolean.TRUE;
		}

		return !properties.getTables().contains(tableName);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy