提交 | 用户 | 时间
|
4d3533
|
1 |
package com.iailab.module.data.common.interceptor; |
潘 |
2 |
|
|
3 |
import cn.hutool.core.util.StrUtil; |
|
4 |
import com.baomidou.mybatisplus.core.toolkit.PluginUtils; |
|
5 |
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor; |
|
6 |
import net.sf.jsqlparser.JSQLParserException; |
|
7 |
import net.sf.jsqlparser.expression.Expression; |
|
8 |
import net.sf.jsqlparser.expression.StringValue; |
|
9 |
import net.sf.jsqlparser.expression.operators.conditional.AndExpression; |
|
10 |
import net.sf.jsqlparser.parser.CCJSqlParserUtil; |
|
11 |
import net.sf.jsqlparser.statement.select.PlainSelect; |
|
12 |
import net.sf.jsqlparser.statement.select.Select; |
|
13 |
import org.apache.ibatis.executor.Executor; |
|
14 |
import org.apache.ibatis.mapping.BoundSql; |
|
15 |
import org.apache.ibatis.mapping.MappedStatement; |
|
16 |
import org.apache.ibatis.session.ResultHandler; |
|
17 |
import org.apache.ibatis.session.RowBounds; |
|
18 |
|
|
19 |
import java.util.Map; |
|
20 |
|
|
21 |
/** |
|
22 |
* 数据过滤 |
|
23 |
* |
|
24 |
* @author Mark sunlightcs@gmail.com |
|
25 |
*/ |
|
26 |
public class DataFilterInterceptor implements InnerInterceptor { |
|
27 |
|
|
28 |
@Override |
|
29 |
public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) { |
|
30 |
DataScope scope = getDataScope(parameter); |
|
31 |
// 不进行数据过滤 |
|
32 |
if(scope == null || StrUtil.isBlank(scope.getSqlFilter())){ |
|
33 |
return; |
|
34 |
} |
|
35 |
|
|
36 |
// 拼接新SQL |
|
37 |
String buildSql = getSelect(boundSql.getSql(), scope); |
|
38 |
|
|
39 |
// 重写SQL |
|
40 |
PluginUtils.mpBoundSql(boundSql).sql(buildSql); |
|
41 |
} |
|
42 |
|
|
43 |
private DataScope getDataScope(Object parameter){ |
|
44 |
if (parameter == null){ |
|
45 |
return null; |
|
46 |
} |
|
47 |
|
|
48 |
// 判断参数里是否有DataScope对象 |
|
49 |
if (parameter instanceof Map) { |
|
50 |
Map<?, ?> parameterMap = (Map<?, ?>) parameter; |
|
51 |
for (Map.Entry entry : parameterMap.entrySet()) { |
|
52 |
if (entry.getValue() != null && entry.getValue() instanceof DataScope) { |
|
53 |
return (DataScope) entry.getValue(); |
|
54 |
} |
|
55 |
} |
|
56 |
} else if (parameter instanceof DataScope) { |
|
57 |
return (DataScope) parameter; |
|
58 |
} |
|
59 |
|
|
60 |
return null; |
|
61 |
} |
|
62 |
|
|
63 |
private String getSelect(String buildSql, DataScope scope){ |
|
64 |
try { |
|
65 |
Select select = (Select) CCJSqlParserUtil.parse(buildSql); |
|
66 |
PlainSelect plainSelect = (PlainSelect) select.getSelectBody(); |
|
67 |
|
|
68 |
Expression expression = plainSelect.getWhere(); |
|
69 |
if(expression == null){ |
|
70 |
plainSelect.setWhere(new StringValue(scope.getSqlFilter())); |
|
71 |
}else{ |
|
72 |
AndExpression andExpression = new AndExpression(expression, new StringValue(scope.getSqlFilter())); |
|
73 |
plainSelect.setWhere(andExpression); |
|
74 |
} |
|
75 |
|
|
76 |
return select.toString().replaceAll("'", ""); |
|
77 |
}catch (JSQLParserException e){ |
|
78 |
return buildSql; |
|
79 |
} |
|
80 |
} |
|
81 |
} |