|
|
@@ -6,12 +6,15 @@ import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
|
|
|
import com.jzg.commons.constants.SystemConstants;
|
|
|
import com.jzg.commons.core.base.BaseController;
|
|
|
import com.jzg.commons.entity.properties.DataScopeProperties;
|
|
|
+import lombok.SneakyThrows;
|
|
|
import net.sf.jsqlparser.JSQLParserException;
|
|
|
import net.sf.jsqlparser.expression.Expression;
|
|
|
import net.sf.jsqlparser.expression.ExpressionVisitor;
|
|
|
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
|
|
|
import net.sf.jsqlparser.parser.SimpleNode;
|
|
|
import net.sf.jsqlparser.schema.Table;
|
|
|
+import net.sf.jsqlparser.statement.Statement;
|
|
|
+import net.sf.jsqlparser.util.TablesNamesFinder;
|
|
|
import org.apache.ibatis.executor.Executor;
|
|
|
import org.apache.ibatis.executor.statement.StatementHandler;
|
|
|
import org.apache.ibatis.mapping.MappedStatement;
|
|
|
@@ -62,9 +65,10 @@ import java.util.regex.Pattern;
|
|
|
this.dataScopeProperties = dataScopeProperties;
|
|
|
}
|
|
|
|
|
|
- private boolean shouldIgnoreTable(String sql) {
|
|
|
+ private boolean shouldIgnoreTable(String sql) throws JSQLParserException {
|
|
|
for (String table : dataScopeProperties.getTables()) {
|
|
|
- if (sql.toLowerCase().contains(table.toLowerCase())) {
|
|
|
+ String regex = "(?i)\\b" + table + "\\b"; // (?i) 忽略大小写,\\b 表示单词边界
|
|
|
+ if (sql.matches(".*" + regex + ".*")) {
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
@@ -72,15 +76,24 @@ import java.util.regex.Pattern;
|
|
|
}
|
|
|
|
|
|
private String getTableName(Table table){
|
|
|
+// return String.valueOf(table.getAlias() == null ? table.getName() : table.getAlias().getName());
|
|
|
+// return String.valueOf(table.getAlias() == null ? table.getName() : table.getAlias().getName());
|
|
|
+ return String.valueOf(table.getNameParts().get(0));
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getAliasName(Table table){
|
|
|
return String.valueOf(table.getAlias() == null ? table.getName() : table.getAlias().getName());
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
@Override
|
|
|
public Expression getSqlSegment(Table table, Expression where, String mappedStatementId) {
|
|
|
if(!shouldIgnoreTable(table.getName()))
|
|
|
{
|
|
|
return null;
|
|
|
}
|
|
|
+
|
|
|
String currentUserDeptId = baseController.getUserDeptId();
|
|
|
String userDataScope = baseController.getUserDataScope();
|
|
|
String userSystemCode = baseController.getUserSystemCode();
|
|
|
@@ -92,8 +105,9 @@ import java.util.regex.Pattern;
|
|
|
}
|
|
|
StringBuilder additionalSql = new StringBuilder();
|
|
|
String tableName = getTableName(table);
|
|
|
- if(!shouldIgnoreTable(table.getName()) && !dataScope.isEmpty()) {
|
|
|
+ if(shouldIgnoreTable(table.getName()) && !dataScope.isEmpty()) {
|
|
|
String fieldName = table.getName().equals("sys_dept") ? "id" : "dept_id";
|
|
|
+ tableName = getAliasName(table);
|
|
|
List<String> deptIds = new ArrayList<>();
|
|
|
if (dataScope.getString("dataMark").equals("2")) {
|
|
|
additionalSql.append(tableName).append(".").append(fieldName).append(" in (");
|