优化代码

This commit is contained in:
阿沐 2023-10-06 15:55:36 +08:00
parent 181a489908
commit 9a3775624c

View File

@ -30,7 +30,7 @@ public class DataScopeInnerInterceptor implements InnerInterceptor {
public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) { public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
DataScope scope = getDataScope(parameter); DataScope scope = getDataScope(parameter);
// 不进行数据过滤 // 不进行数据过滤
if(scope == null || StrUtil.isBlank(scope.getSqlFilter())){ if (scope == null || StrUtil.isBlank(scope.getSqlFilter())) {
return; return;
} }
@ -41,15 +41,14 @@ public class DataScopeInnerInterceptor implements InnerInterceptor {
PluginUtils.mpBoundSql(boundSql).sql(buildSql); PluginUtils.mpBoundSql(boundSql).sql(buildSql);
} }
private DataScope getDataScope(Object parameter){ private DataScope getDataScope(Object parameter) {
if (parameter == null){ if (parameter == null) {
return null; return null;
} }
// 判断参数里是否有DataScope对象 // 判断参数里是否有DataScope对象
if (parameter instanceof Map) { if (parameter instanceof Map<?, ?> parameterMap) {
Map<?, ?> parameterMap = (Map<?, ?>) parameter; for (Map.Entry<?, ?> entry : parameterMap.entrySet()) {
for (Map.Entry entry : parameterMap.entrySet()) {
if (entry.getValue() != null && entry.getValue() instanceof DataScope) { if (entry.getValue() != null && entry.getValue() instanceof DataScope) {
return (DataScope) entry.getValue(); return (DataScope) entry.getValue();
} }
@ -61,21 +60,21 @@ public class DataScopeInnerInterceptor implements InnerInterceptor {
return null; return null;
} }
private String getSelect(String buildSql, DataScope scope){ private String getSelect(String buildSql, DataScope scope) {
try { try {
Select select = (Select) CCJSqlParserUtil.parse(buildSql); Select select = (Select) CCJSqlParserUtil.parse(buildSql);
PlainSelect plainSelect = (PlainSelect) select.getSelectBody(); PlainSelect plainSelect = (PlainSelect) select.getSelectBody();
Expression expression = plainSelect.getWhere(); Expression expression = plainSelect.getWhere();
if(expression == null){ if (expression == null) {
plainSelect.setWhere(new StringValue(scope.getSqlFilter())); plainSelect.setWhere(new StringValue(scope.getSqlFilter()));
}else{ } else {
AndExpression andExpression = new AndExpression(expression, new StringValue(scope.getSqlFilter())); AndExpression andExpression = new AndExpression(expression, new StringValue(scope.getSqlFilter()));
plainSelect.setWhere(andExpression); plainSelect.setWhere(andExpression);
} }
return select.toString().replaceAll("'", ""); return select.toString().replaceAll("'", "");
}catch (JSQLParserException e){ } catch (JSQLParserException e) {
return buildSql; return buildSql;
} }
} }