|
|
@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
|
|
|
import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils; |
|
|
|
import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils; |
|
|
|
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler; |
|
|
|
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler; |
|
|
|
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor; |
|
|
|
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor; |
|
|
|
|
|
|
|
import com.cloud.kicc.common.data.override.TenantLikeExpression; |
|
|
|
import lombok.NoArgsConstructor; |
|
|
|
import lombok.NoArgsConstructor; |
|
|
|
import net.sf.jsqlparser.expression.Expression; |
|
|
|
import net.sf.jsqlparser.expression.Expression; |
|
|
|
import net.sf.jsqlparser.expression.Parenthesis; |
|
|
|
import net.sf.jsqlparser.expression.Parenthesis; |
|
|
@ -13,8 +14,8 @@ import net.sf.jsqlparser.expression.StringValue; |
|
|
|
import net.sf.jsqlparser.expression.operators.conditional.AndExpression; |
|
|
|
import net.sf.jsqlparser.expression.operators.conditional.AndExpression; |
|
|
|
import net.sf.jsqlparser.expression.operators.conditional.OrExpression; |
|
|
|
import net.sf.jsqlparser.expression.operators.conditional.OrExpression; |
|
|
|
import net.sf.jsqlparser.expression.operators.relational.ExpressionList; |
|
|
|
import net.sf.jsqlparser.expression.operators.relational.ExpressionList; |
|
|
|
import net.sf.jsqlparser.expression.operators.relational.InExpression; |
|
|
|
|
|
|
|
import net.sf.jsqlparser.expression.operators.relational.ItemsList; |
|
|
|
import net.sf.jsqlparser.expression.operators.relational.ItemsList; |
|
|
|
|
|
|
|
import net.sf.jsqlparser.expression.operators.relational.LikeExpression; |
|
|
|
import net.sf.jsqlparser.expression.operators.relational.MultiExpressionList; |
|
|
|
import net.sf.jsqlparser.expression.operators.relational.MultiExpressionList; |
|
|
|
import net.sf.jsqlparser.schema.Column; |
|
|
|
import net.sf.jsqlparser.schema.Column; |
|
|
|
import net.sf.jsqlparser.schema.Table; |
|
|
|
import net.sf.jsqlparser.schema.Table; |
|
|
@ -37,7 +38,7 @@ import java.util.stream.Collectors; |
|
|
|
/** |
|
|
|
/** |
|
|
|
*<p> |
|
|
|
*<p> |
|
|
|
* 租户线路内部拦截器 |
|
|
|
* 租户线路内部拦截器 |
|
|
|
* 重构租户线路内部拦截器,支持查询多个多租户ID,默认只支持一个多租户id查询 |
|
|
|
* 重构租户线路内部拦截器,支持查询多个多租户ID,还支持查询多个租户的共享数据,默认只支持一个多租户id查询 |
|
|
|
* 支持多租户ID不存在时,查询所有租户ID数据 |
|
|
|
* 支持多租户ID不存在时,查询所有租户ID数据 |
|
|
|
*</p> |
|
|
|
*</p> |
|
|
|
* |
|
|
|
* |
|
|
@ -76,12 +77,21 @@ public class KiccTenantLineInnerInterceptor extends TenantLineInnerInterceptor { |
|
|
|
String tenantIdColumn = this.tenantLineHandler.getTenantIdColumn(); |
|
|
|
String tenantIdColumn = this.tenantLineHandler.getTenantIdColumn(); |
|
|
|
if (!this.tenantLineHandler.ignoreInsert(columns, tenantIdColumn)) { |
|
|
|
if (!this.tenantLineHandler.ignoreInsert(columns, tenantIdColumn)) { |
|
|
|
columns.add(new Column(tenantIdColumn)); |
|
|
|
columns.add(new Column(tenantIdColumn)); |
|
|
|
|
|
|
|
|
|
|
|
List<Expression> duplicateUpdateColumns = insert.getDuplicateUpdateExpressionList(); |
|
|
|
List<Expression> duplicateUpdateColumns = insert.getDuplicateUpdateExpressionList(); |
|
|
|
if (CollectionUtils.isNotEmpty(duplicateUpdateColumns)) { |
|
|
|
if (CollectionUtils.isNotEmpty(duplicateUpdateColumns)) { |
|
|
|
// 替换InExpression支持查询多个租户ID的条件
|
|
|
|
// 替换likeExpression支持查询多个租户ID的条件,包括查询数据对应多个多租户ID的数据
|
|
|
|
InExpression inExpression = new InExpression(); |
|
|
|
List<String> tenantIds = StrUtil.split(this.tenantLineHandler.getTenantId().toString(), ","); |
|
|
|
inExpression.setLeftExpression(new StringValue(tenantIdColumn)); |
|
|
|
StringBuilder statementBuilder = new StringBuilder(); |
|
|
|
inExpression.setRightItemsList(new ExpressionList(this.tenantLineHandler.getTenantId())); |
|
|
|
tenantIds.forEach(tenantId -> { |
|
|
|
|
|
|
|
LikeExpression likeExpression = new LikeExpression(); |
|
|
|
|
|
|
|
likeExpression.setLeftExpression(new Column(tenantIdColumn)); |
|
|
|
|
|
|
|
likeExpression.setRightExpression(new StringValue("%" + tenantId + "%")); |
|
|
|
|
|
|
|
statementBuilder.append(likeExpression + " OR "); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
statementBuilder.delete(statementBuilder.length()-4, statementBuilder.length()); |
|
|
|
|
|
|
|
TenantLikeExpression tenantLikeExpression = new TenantLikeExpression(statementBuilder.toString()); |
|
|
|
|
|
|
|
duplicateUpdateColumns.add(tenantLikeExpression); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Select select = insert.getSelect(); |
|
|
|
Select select = insert.getSelect(); |
|
|
@ -111,27 +121,34 @@ public class KiccTenantLineInnerInterceptor extends TenantLineInnerInterceptor { |
|
|
|
protected void processUpdate(Update update, int index, String sql, Object obj) { |
|
|
|
protected void processUpdate(Update update, int index, String sql, Object obj) { |
|
|
|
Table table = update.getTable(); |
|
|
|
Table table = update.getTable(); |
|
|
|
if (!this.tenantLineHandler.ignoreTable(table.getName())) { |
|
|
|
if (!this.tenantLineHandler.ignoreTable(table.getName())) { |
|
|
|
update.setWhere(this.andInExpression(table, update.getWhere())); |
|
|
|
update.setWhere(this.andLikeExpression(table, update.getWhere())); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
protected void processDelete(Delete delete, int index, String sql, Object obj) { |
|
|
|
protected void processDelete(Delete delete, int index, String sql, Object obj) { |
|
|
|
if (!this.tenantLineHandler.ignoreTable(delete.getTable().getName())) { |
|
|
|
if (!this.tenantLineHandler.ignoreTable(delete.getTable().getName())) { |
|
|
|
delete.setWhere(this.andInExpression(delete.getTable(), delete.getWhere())); |
|
|
|
delete.setWhere(this.andLikeExpression(delete.getTable(), delete.getWhere())); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** 重写andExpression表达式,支持in查询多个参数 */ |
|
|
|
/** 重写andExpression表达式,支持like查询多个参数 */ |
|
|
|
protected Expression andInExpression(Table table, Expression where) { |
|
|
|
protected Expression andLikeExpression(Table table, Expression where) { |
|
|
|
// 替换InExpression支持查询多个租户ID的条件
|
|
|
|
// 替换likeExpression支持查询多个租户ID的条件,包括查询数据对应多个多租户ID的数据
|
|
|
|
InExpression inExpression = new InExpression(); |
|
|
|
List<String> tenantIds = StrUtil.split(this.tenantLineHandler.getTenantId().toString(), ","); |
|
|
|
inExpression.setLeftExpression(this.getAliasColumn(table)); |
|
|
|
StringBuilder statementBuilder = new StringBuilder(); |
|
|
|
inExpression.setRightItemsList(new ExpressionList(this.tenantLineHandler.getTenantId())); |
|
|
|
tenantIds.forEach(tenantId -> { |
|
|
|
|
|
|
|
LikeExpression likeExpression = new LikeExpression(); |
|
|
|
|
|
|
|
likeExpression.setLeftExpression(this.getAliasColumn(table)); |
|
|
|
|
|
|
|
likeExpression.setRightExpression(new StringValue("%" + tenantId + "%")); |
|
|
|
|
|
|
|
statementBuilder.append(likeExpression + " OR "); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
statementBuilder.delete(statementBuilder.length()-4, statementBuilder.length()); |
|
|
|
|
|
|
|
TenantLikeExpression tenantLikeExpression = new TenantLikeExpression(statementBuilder.toString()); |
|
|
|
if (null != where) { |
|
|
|
if (null != where) { |
|
|
|
return where instanceof OrExpression ? new AndExpression(inExpression, new Parenthesis(where)) : new AndExpression(inExpression, where); |
|
|
|
return where instanceof OrExpression ? new AndExpression(tenantLikeExpression, new Parenthesis(where)) : new AndExpression(tenantLikeExpression, where); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return inExpression; |
|
|
|
return tenantLikeExpression; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -140,18 +157,29 @@ public class KiccTenantLineInnerInterceptor extends TenantLineInnerInterceptor { |
|
|
|
if (CollectionUtils.isEmpty(tables)) { |
|
|
|
if (CollectionUtils.isEmpty(tables)) { |
|
|
|
return currentExpression; |
|
|
|
return currentExpression; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
Expression tenantId = this.tenantLineHandler.getTenantId(); |
|
|
|
// 替换likeExpression支持查询多个租户ID的条件,包括查询数据对应多个多租户ID的数据
|
|
|
|
// 替换InExpression支持查询多个租户ID的条件
|
|
|
|
List<TenantLikeExpression> strExpressions = tables.stream() |
|
|
|
List<InExpression> inExpressions = tables.stream() |
|
|
|
.filter(x -> !this.tenantLineHandler.ignoreTable(x.getName())) |
|
|
|
.filter(x -> !this.tenantLineHandler.ignoreTable(x.getName())) |
|
|
|
.map(item -> { |
|
|
|
.map(item -> new InExpression(this.getAliasColumn(item), new ExpressionList(tenantId))).collect(Collectors.toList()); |
|
|
|
List<String> tenantIds = StrUtil.split(this.tenantLineHandler.getTenantId().toString(), ","); |
|
|
|
if (CollectionUtils.isEmpty(inExpressions)) { |
|
|
|
StringBuilder statementBuilder = new StringBuilder(); |
|
|
|
|
|
|
|
tenantIds.forEach(tenantId -> { |
|
|
|
|
|
|
|
LikeExpression likeExpression = new LikeExpression(); |
|
|
|
|
|
|
|
likeExpression.setLeftExpression(this.getAliasColumn(item)); |
|
|
|
|
|
|
|
likeExpression.setRightExpression(new StringValue("%" + tenantId + "%")); |
|
|
|
|
|
|
|
statementBuilder.append(likeExpression + " OR "); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
statementBuilder.delete(statementBuilder.length()-4, statementBuilder.length()); |
|
|
|
|
|
|
|
TenantLikeExpression tenantLikeExpression = new TenantLikeExpression(statementBuilder.toString()); |
|
|
|
|
|
|
|
return tenantLikeExpression; |
|
|
|
|
|
|
|
}).collect(Collectors.toList()); |
|
|
|
|
|
|
|
if (CollectionUtils.isEmpty(strExpressions)) { |
|
|
|
return currentExpression; |
|
|
|
return currentExpression; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
Expression injectExpression = inExpressions.get(0); |
|
|
|
Expression injectExpression = strExpressions.get(0); |
|
|
|
if (inExpressions.size() > 1) { |
|
|
|
if (strExpressions.size() > 1) { |
|
|
|
for(int i = 1; i < inExpressions.size(); ++i) { |
|
|
|
for(int i = 1; i < strExpressions.size(); ++i) { |
|
|
|
injectExpression = new AndExpression(injectExpression, inExpressions.get(i)); |
|
|
|
injectExpression = new AndExpression(injectExpression, strExpressions.get(i)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|