feat(datapermission):优化部门数据权限规则生成逻辑

- 引入 GreaterThan 表达式支持更灵活的条件构建
- 使用 FIND_IN_SET 函数替换原有的 IN 表达式实现
- 支持多部门 ID 的逐个条件拼接,提升查询准确性
-保留原有括号包装与 OR 连接逻辑确保语义一致
- 统一代码缩进风格增强可读性
This commit is contained in:
2025-10-20 16:00:44 +08:00
parent 8840eccf41
commit 8558ff726e

View File

@@ -19,6 +19,7 @@ import net.sf.jsqlparser.expression.*;
import net.sf.jsqlparser.expression.operators.conditional.OrExpression; import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
import net.sf.jsqlparser.expression.operators.relational.EqualsTo; import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList; import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.expression.operators.relational.GreaterThan;
import net.sf.jsqlparser.expression.operators.relational.InExpression; import net.sf.jsqlparser.expression.operators.relational.InExpression;
import javax.annotation.Resource; import javax.annotation.Resource;
@@ -140,17 +141,17 @@ public class DeptDataPermissionRule implements DataPermissionRule {
return customDeptExpression; return customDeptExpression;
} }
if(Objects.nonNull(deptExpression) && Objects.nonNull(userExpression) && Objects.nonNull(customDeptExpression)){ if (Objects.nonNull(deptExpression) && Objects.nonNull(userExpression) && Objects.nonNull(customDeptExpression)) {
// 目前,如果有指定部门 + 可查看自己,采用 OR 条件。即WHERE (dept_id IN ? OR user_id = ?) // 目前,如果有指定部门 + 可查看自己,采用 OR 条件。即WHERE (dept_id IN ? OR user_id = ?)
return new Parenthesis(new OrExpression(new OrExpression(deptExpression, userExpression), customDeptExpression)); return new Parenthesis(new OrExpression(new OrExpression(deptExpression, userExpression), customDeptExpression));
} }
if(Objects.nonNull(deptExpression) && Objects.nonNull(userExpression)){ if (Objects.nonNull(deptExpression) && Objects.nonNull(userExpression)) {
// 目前,如果有指定部门 + 可查看自己,采用 OR 条件。即WHERE (dept_id IN ? OR user_id = ?) // 目前,如果有指定部门 + 可查看自己,采用 OR 条件。即WHERE (dept_id IN ? OR user_id = ?)
return new Parenthesis(new OrExpression(deptExpression, userExpression)); return new Parenthesis(new OrExpression(deptExpression, userExpression));
} }
if(Objects.nonNull(deptExpression)){ if (Objects.nonNull(deptExpression)) {
// 目前,如果有指定部门 + 可查看自己,采用 OR 条件。即WHERE (dept_id IN ? OR user_id = ?) // 目前,如果有指定部门 + 可查看自己,采用 OR 条件。即WHERE (dept_id IN ? OR user_id = ?)
return new Parenthesis(new OrExpression(deptExpression, customDeptExpression)); return new Parenthesis(new OrExpression(deptExpression, customDeptExpression));
} }
@@ -195,12 +196,16 @@ public class DeptDataPermissionRule implements DataPermissionRule {
Expression returnExpression = null; Expression returnExpression = null;
String[] columnNames = columnName.split(","); String[] columnNames = columnName.split(",");
for (String column : columnNames) { for (String column : columnNames) {
Expression deptExpression = new InExpression(MyBatisUtils.buildColumn(tableName, tableAlias, column), for (Long deptId : deptIds) {
new ExpressionList(CollectionUtils.convertList(deptIds, LongValue::new))); Expression deptExpression = new GreaterThan()
if (Objects.isNull(returnExpression)) { .withLeftExpression(new Function().withName("FIND_IN_SET")
returnExpression = deptExpression; .withParameters(new ExpressionList(new StringValue(deptId.toString()), MyBatisUtils.buildColumn(tableName, tableAlias, column))))
} else { .withRightExpression(new LongValue(0));
returnExpression = new Parenthesis(new OrExpression(returnExpression, deptExpression)); if (Objects.isNull(returnExpression)) {
returnExpression = deptExpression;
} else {
returnExpression = new Parenthesis(new OrExpression(returnExpression, deptExpression));
}
} }
} }
return returnExpression; return returnExpression;