From a28ca3f36d0ace05966a8c0fac1e4b5fe921f882 Mon Sep 17 00:00:00 2001 From: houzhongjian <houzhongyi@126.com> Date: 星期三, 30 十月 2024 16:25:12 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/master' --- iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/common/interceptor/DataFilterInterceptor.java | 170 +++++++++++++++++++++++++++----------------------------- 1 files changed, 81 insertions(+), 89 deletions(-) diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/common/interceptor/DataFilterInterceptor.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/common/interceptor/DataFilterInterceptor.java index 4369aea..743af73 100644 --- a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/common/interceptor/DataFilterInterceptor.java +++ b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/common/interceptor/DataFilterInterceptor.java @@ -1,89 +1,81 @@ -///** -// * Copyright (c) 2018 人人开源 All rights reserved. -// * -// * https://www.renren.io -// * -// * 版权所有,侵权必究! -// */ -// -//package com.iailab.module.common.interceptor; -// -//import cn.hutool.core.util.StrUtil; -//import com.baomidou.mybatisplus.core.toolkit.PluginUtils; -//import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor; -//import net.sf.jsqlparser.JSQLParserException; -//import net.sf.jsqlparser.expression.Expression; -//import net.sf.jsqlparser.expression.StringValue; -//import net.sf.jsqlparser.expression.operators.conditional.AndExpression; -//import net.sf.jsqlparser.parser.CCJSqlParserUtil; -//import net.sf.jsqlparser.statement.select.PlainSelect; -//import net.sf.jsqlparser.statement.select.Select; -//import org.apache.ibatis.executor.Executor; -//import org.apache.ibatis.mapping.BoundSql; -//import org.apache.ibatis.mapping.MappedStatement; -//import org.apache.ibatis.session.ResultHandler; -//import org.apache.ibatis.session.RowBounds; -// -//import java.util.Map; -// -///** -// * 数据过滤 -// * -// * @author Mark sunlightcs@gmail.com -// */ -//public class DataFilterInterceptor implements InnerInterceptor { -// -// @Override -// public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) { -// DataScope scope = getDataScope(parameter); -// // 不进行数据过滤 -// if(scope == null || StrUtil.isBlank(scope.getSqlFilter())){ -// return; -// } -// -// // 拼接新SQL -// String buildSql = getSelect(boundSql.getSql(), scope); -// -// // 重写SQL -// PluginUtils.mpBoundSql(boundSql).sql(buildSql); -// } -// -// private DataScope getDataScope(Object parameter){ -// if (parameter == null){ -// return null; -// } -// -// // 判断参数里是否有DataScope对象 -// if (parameter instanceof Map) { -// Map<?, ?> parameterMap = (Map<?, ?>) parameter; -// for (Map.Entry entry : parameterMap.entrySet()) { -// if (entry.getValue() != null && entry.getValue() instanceof DataScope) { -// return (DataScope) entry.getValue(); -// } -// } -// } else if (parameter instanceof DataScope) { -// return (DataScope) parameter; -// } -// -// return null; -// } -// -// private String getSelect(String buildSql, DataScope scope){ -// try { -// Select select = (Select) CCJSqlParserUtil.parse(buildSql); -// PlainSelect plainSelect = (PlainSelect) select.getSelectBody(); -// -// Expression expression = plainSelect.getWhere(); -// if(expression == null){ -// plainSelect.setWhere(new StringValue(scope.getSqlFilter())); -// }else{ -// AndExpression andExpression = new AndExpression(expression, new StringValue(scope.getSqlFilter())); -// plainSelect.setWhere(andExpression); -// } -// -// return select.toString().replaceAll("'", ""); -// }catch (JSQLParserException e){ -// return buildSql; -// } -// } -//} \ No newline at end of file +package com.iailab.module.data.common.interceptor; + +import cn.hutool.core.util.StrUtil; +import com.baomidou.mybatisplus.core.toolkit.PluginUtils; +import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor; +import net.sf.jsqlparser.JSQLParserException; +import net.sf.jsqlparser.expression.Expression; +import net.sf.jsqlparser.expression.StringValue; +import net.sf.jsqlparser.expression.operators.conditional.AndExpression; +import net.sf.jsqlparser.parser.CCJSqlParserUtil; +import net.sf.jsqlparser.statement.select.PlainSelect; +import net.sf.jsqlparser.statement.select.Select; +import org.apache.ibatis.executor.Executor; +import org.apache.ibatis.mapping.BoundSql; +import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.session.ResultHandler; +import org.apache.ibatis.session.RowBounds; + +import java.util.Map; + +/** + * 数据过滤 + * + * @author Mark sunlightcs@gmail.com + */ +public class DataFilterInterceptor implements InnerInterceptor { + + @Override + public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) { + DataScope scope = getDataScope(parameter); + // 不进行数据过滤 + if(scope == null || StrUtil.isBlank(scope.getSqlFilter())){ + return; + } + + // 拼接新SQL + String buildSql = getSelect(boundSql.getSql(), scope); + + // 重写SQL + PluginUtils.mpBoundSql(boundSql).sql(buildSql); + } + + private DataScope getDataScope(Object parameter){ + if (parameter == null){ + return null; + } + + // 判断参数里是否有DataScope对象 + if (parameter instanceof Map) { + Map<?, ?> parameterMap = (Map<?, ?>) parameter; + for (Map.Entry entry : parameterMap.entrySet()) { + if (entry.getValue() != null && entry.getValue() instanceof DataScope) { + return (DataScope) entry.getValue(); + } + } + } else if (parameter instanceof DataScope) { + return (DataScope) parameter; + } + + return null; + } + + private String getSelect(String buildSql, DataScope scope){ + try { + Select select = (Select) CCJSqlParserUtil.parse(buildSql); + PlainSelect plainSelect = (PlainSelect) select.getSelectBody(); + + Expression expression = plainSelect.getWhere(); + if(expression == null){ + plainSelect.setWhere(new StringValue(scope.getSqlFilter())); + }else{ + AndExpression andExpression = new AndExpression(expression, new StringValue(scope.getSqlFilter())); + plainSelect.setWhere(andExpression); + } + + return select.toString().replaceAll("'", ""); + }catch (JSQLParserException e){ + return buildSql; + } + } +} \ No newline at end of file -- Gitblit v1.9.3