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 fd5f8dbcf..612b87709 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 @@ -47,4 +47,13 @@ public class DataScope extends HashMap { */ private Boolean isOnly = false; + /** + * 函数名称,默认 SELECT * ; + * + * + */ + private DataScopeFuncEnum Func = DataScopeFuncEnum.ALL; + } diff --git a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeFuncEnum.java b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeFuncEnum.java new file mode 100644 index 000000000..495bb5e79 --- /dev/null +++ b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeFuncEnum.java @@ -0,0 +1,55 @@ +/* + * 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 2020-06-17 + */ +@Getter +@AllArgsConstructor +public enum DataScopeFuncEnum { + + /** + * 查询全部数据 SELECT * FROM (originSql) temp_data_scope WHERE temp_data_scope.dept_id IN + * (1) + */ + ALL("*", "全部"), + + /** + * 查询函数COUNT SELECT COUNT(1) FROM (originSql) temp_data_scope WHERE + * temp_data_scope.dept_id IN (1) + */ + COUNT("COUNT(1)", "自定义"); + + /** + * 类型 + */ + private final String type; + + /** + * 描述 + */ + private final String description; + +} diff --git a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeInterceptor.java b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeInterceptor.java index 7c25ba50e..ec5325629 100644 --- a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeInterceptor.java +++ b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeInterceptor.java @@ -81,13 +81,15 @@ public class DataScopeInterceptor extends AbstractSqlParserHandler implements In } if (deptIds.isEmpty()) { - originalSql = "select * from (" + originalSql + ") temp_data_scope where 1 = 2"; + originalSql = String.format("SELECT %s FROM (%s) temp_data_scope WHERE 1 = 2", + dataScope.getFunc().getType(), originalSql); } else { String join = CollectionUtil.join(deptIds, ","); - originalSql = "select * from (" + originalSql + ") temp_data_scope where temp_data_scope." + scopeName - + " in (" + join + ")"; + originalSql = String.format("SELECT %s FROM (%s) temp_data_scope WHERE temp_data_scope.%s IN (%s)", + dataScope.getFunc().getType(), originalSql, scopeName, join); } + metaObject.setValue("delegate.boundSql.sql", originalSql); return invocation.proceed(); } diff --git a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeMapper.java b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeMapper.java index f7b1d0216..c14f2a42c 100644 --- a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeMapper.java +++ b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeMapper.java @@ -9,8 +9,10 @@ import org.apache.ibatis.annotations.Param; import java.util.List; /** + * 若使用数据权限 Mapper继承此类 + * * @author lengleng - * @date 2020/4/26 + * @date 2020-06-17 */ public interface DataScopeMapper extends BaseMapper { @@ -32,4 +34,12 @@ public interface DataScopeMapper extends BaseMapper { > E selectPageByScope(E page, @Param(Constants.WRAPPER) Wrapper queryWrapper, DataScope scope); + /** + * 根据 Wrapper 条件,查询总记录数 + * @param queryWrapper 实体对象封装操作类(可以为 null) + * @param scope 数据权限范围 + * @return Integer + */ + Integer selectCountByScope(@Param(Constants.WRAPPER) Wrapper queryWrapper, DataScope scope); + } diff --git a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeSqlInjector.java b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeSqlInjector.java index 900b098b2..58a44aeb7 100644 --- a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeSqlInjector.java +++ b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/DataScopeSqlInjector.java @@ -6,8 +6,10 @@ import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector; import java.util.List; /** + * 支持自定义数据权限方法注入 + * * @author lengleng - * @date 2020/4/26 + * @date 2020-06-17 */ public class DataScopeSqlInjector extends DefaultSqlInjector { @@ -16,6 +18,7 @@ public class DataScopeSqlInjector extends DefaultSqlInjector { List methodList = super.getMethodList(mapperClass); methodList.add(new SelectListByScope()); methodList.add(new SelectPageByScope()); + methodList.add(new SelectCountByScope()); return methodList; } diff --git a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/SelectCountByScope.java b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/SelectCountByScope.java new file mode 100644 index 000000000..1fe2dae82 --- /dev/null +++ b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/datascope/SelectCountByScope.java @@ -0,0 +1,28 @@ +package com.pig4cloud.pigx.common.data.datascope; + +import com.baomidou.mybatisplus.core.enums.SqlMethod; +import com.baomidou.mybatisplus.core.injector.AbstractMethod; +import com.baomidou.mybatisplus.core.metadata.TableInfo; +import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.mapping.SqlSource; + +/** + * 扩展支持COUNT查询数量 + * + * @author lengleng + * @date 2020/6/17 + */ +public class SelectCountByScope extends AbstractMethod { + + @Override + public MappedStatement injectMappedStatement(Class mapperClass, Class modelClass, TableInfo tableInfo) { + SqlMethod sqlMethod = SqlMethod.SELECT_LIST; + + String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlSelectColumns(tableInfo, true), + tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo), sqlComment()); + SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass); + + return this.addSelectMappedStatementForOther(mapperClass, "selectCountByScope", sqlSource, Integer.class); + } + +} \ No newline at end of file diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/mapper/SysUserMapper.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/mapper/SysUserMapper.java index bd6cfe6d1..1854965b6 100644 --- a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/mapper/SysUserMapper.java +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/mapper/SysUserMapper.java @@ -19,13 +19,13 @@ package com.pig4cloud.pigx.admin.mapper; -import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.pig4cloud.pigx.admin.api.dto.UserDTO; import com.pig4cloud.pigx.admin.api.entity.SysUser; import com.pig4cloud.pigx.admin.api.vo.UserVO; import com.pig4cloud.pigx.common.data.datascope.DataScope; +import com.pig4cloud.pigx.common.data.datascope.DataScopeMapper; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; @@ -40,7 +40,7 @@ import java.util.List; * @since 2017-10-29 */ @Mapper -public interface SysUserMapper extends BaseMapper { +public interface SysUserMapper extends DataScopeMapper { /** * 通过用户名查询用户信息(含有角色信息) diff --git a/pom.xml b/pom.xml index 08951fd91..3eb363a02 100644 --- a/pom.xml +++ b/pom.xml @@ -33,7 +33,7 @@ - 2.3.0.RELEASE + 2.3.1.RELEASE Hoxton.SR5 2.2.1.RELEASE UTF-8