diff --git a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScope.java b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScope.java index f609b6aeb..34b088553 100644 --- a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScope.java +++ b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScope.java @@ -71,6 +71,16 @@ public class DataScope extends HashMap { */ private DataScopeFuncEnum func = DataScopeFuncEnum.ALL; + /** + * 逻辑组合模式,默认OR(向后兼容) + * 当同时有本人权限(username)和部门权限(deptList)时的逻辑组合方式 + * + */ + private DataScopeLogicEnum logicMode = DataScopeLogicEnum.OR; + /** * of 获取实例 */ @@ -93,4 +103,14 @@ public class DataScope extends HashMap { return this; } + /** + * 设置逻辑组合模式 + * @param logicMode ��辑模式(AND/OR) + * @return DataScope + */ + public DataScope logic(DataScopeLogicEnum logicMode) { + this.logicMode = logicMode; + return this; + } + } diff --git a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeInnerInterceptor.java b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeInnerInterceptor.java index 90e736b1b..74f2e8d3e 100644 --- a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeInnerInterceptor.java +++ b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeInnerInterceptor.java @@ -63,8 +63,13 @@ public class DataScopeInnerInterceptor implements DataScopeInterceptor { // 2.如果为本人权限 + 部门权限控制 else if (StrUtil.isNotBlank(dataScope.getUsername()) && CollUtil.isNotEmpty(deptIds)) { String join = CollectionUtil.join(deptIds, ","); - originalSql = String.format("SELECT %s FROM (%s) temp_data_scope WHERE temp_data_scope.%s = '%s' OR temp_data_scope.%s IN (%s)", - dataScope.getFunc().getType(), originalSql, dataScope.getScopeUserName(), dataScope.getUsername(), dataScope.getScopeDeptName(), join); + // 使用配置的组合模式(AND/OR),默认OR保持向后兼容 + String logicKeyword = dataScope.getLogicMode() != null ? dataScope.getLogicMode().getKeyword() + : DataScopeLogicEnum.OR.getKeyword(); + originalSql = String.format( + "SELECT %s FROM (%s) temp_data_scope WHERE temp_data_scope.%s = '%s' %s temp_data_scope.%s IN (%s)", + dataScope.getFunc().getType(), originalSql, dataScope.getScopeUserName(), dataScope.getUsername(), + logicKeyword, dataScope.getScopeDeptName(), join); } // 3. 如果为本人 else if (StrUtil.isNotBlank(dataScope.getUsername())) { diff --git a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeLogicEnum.java b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeLogicEnum.java new file mode 100644 index 000000000..5f17fa794 --- /dev/null +++ b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeLogicEnum.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.common.data.datascope; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * 数据权限逻辑组合类型 + * + * @author lengleng + * @date 2025-12-10 + */ +@Getter +@AllArgsConstructor +public enum DataScopeLogicEnum { + + /** + * OR逻辑 - 满足任一条件即可(默认) 示例: WHERE create_by='user' OR dept_id IN (1,2,3) + */ + OR("OR", "或"), + + /** + * AND逻辑 - 需同时满足所有条件 示例: WHERE create_by='user' AND dept_id IN (1,2,3) + */ + AND("AND", "且"); + + /** + * SQL关键字 + */ + private final String keyword; + + /** + * 描述 + */ + private final String description; + +}