refactor(dept):优化部门数据权限规则生成逻辑- 移除无用的GreaterThan导入

- 简化条件判断中的空格格式
- 使用InExpression替代FIND_IN_SET函数提高查询效率-保持代码风格一致性和可读性
This commit is contained in:
2025-10-20 20:50:35 +08:00
parent 136c2fa5aa
commit f3810d3126

View File

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