From e257556b5ccfc0ce4f2e225eb757aace25a974fe Mon Sep 17 00:00:00 2001
From: zhangnf <zhangnf@chinatelecom.cn>
Date: Thu, 24 Aug 2023 23:30:12 +0800
Subject: [PATCH] =?UTF-8?q?refactor(=E6=9D=83=E9=99=90):=20=E5=A4=9A?=
 =?UTF-8?q?=E4=B8=AA=E2=80=9C=E8=8E=B7=E5=8F=96=E6=8E=88=E6=9D=83=E5=AF=B9?=
 =?UTF-8?q?=E8=B1=A1=E6=8B=A5=E6=9C=89=E8=B5=84=E6=BA=90=E2=80=9DSQL?=
 =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E8=AF=AD=E5=8F=A5=E5=90=88=E5=B9=B6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../auth/service/impl/ExtAuthServiceImpl.java |  55 +++++++--
 .../commons/constants/SysAuthConstants.java   |   6 +
 .../java/io/dataease/ext/ExtAuthMapper.java   |  14 +--
 .../java/io/dataease/ext/ExtAuthMapper.xml    | 115 +-----------------
 4 files changed, 59 insertions(+), 131 deletions(-)

diff --git a/core/backend/src/main/java/io/dataease/auth/service/impl/ExtAuthServiceImpl.java b/core/backend/src/main/java/io/dataease/auth/service/impl/ExtAuthServiceImpl.java
index 2761ea7819..9be75d50aa 100644
--- a/core/backend/src/main/java/io/dataease/auth/service/impl/ExtAuthServiceImpl.java
+++ b/core/backend/src/main/java/io/dataease/auth/service/impl/ExtAuthServiceImpl.java
@@ -2,6 +2,7 @@ package io.dataease.auth.service.impl;
 
 import io.dataease.auth.entity.AuthItem;
 import io.dataease.auth.service.ExtAuthService;
+import io.dataease.commons.constants.SysAuthConstants;
 import io.dataease.plugins.common.base.domain.SysAuth;
 import io.dataease.ext.ExtAuthMapper;
 import io.dataease.commons.constants.AuthConstants;
@@ -66,59 +67,95 @@ public class ExtAuthServiceImpl implements ExtAuthService {
     @Cacheable(value = AuthConstants.USER_LINK_NAME, key = "'user' + #userId")
     @Override
     public List<AuthItem> dataSourceIdByUser(Long userId) {
-        return extAuthMapper.dataSourceIdByUser(userId.toString());
+        return extAuthMapper.queryAuthItems(
+                SysAuthConstants.AUTH_TARGET_TYPE_USER,
+                userId.toString(),
+                SysAuthConstants.AUTH_SOURCE_TYPE_DATASOURCE
+        );
     }
 
     @Cacheable(value = AuthConstants.USER_DATASET_NAME, key = "'user' + #userId")
     @Override
     public List<AuthItem> dataSetIdByUser(Long userId) {
-        return extAuthMapper.dataSetIdByUser(userId.toString());
+        return extAuthMapper.queryAuthItems(
+                SysAuthConstants.AUTH_TARGET_TYPE_USER,
+                userId.toString(),
+                SysAuthConstants.AUTH_SOURCE_TYPE_DATASET
+        );
     }
 
     @Cacheable(value = AuthConstants.USER_PANEL_NAME, key = "'user' + #userId")
     @Override
     public List<AuthItem> panelIdByUser(Long userId) {
-        return extAuthMapper.panelIdByUser(userId.toString());
+        return extAuthMapper.queryAuthItems(
+                SysAuthConstants.AUTH_TARGET_TYPE_USER,
+                userId.toString(),
+                SysAuthConstants.AUTH_SOURCE_TYPE_PANEL
+        );
     }
 
 
     @Cacheable(value = AuthConstants.ROLE_LINK_NAME, key = "'role' + #roleId")
     @Override
     public List<AuthItem> dataSourceIdByRole(Long roleId) {
-        return extAuthMapper.dataSourceIdByRole(roleId.toString());
+        return extAuthMapper.queryAuthItems(
+                SysAuthConstants.AUTH_TARGET_TYPE_ROLE,
+                roleId.toString(),
+                SysAuthConstants.AUTH_SOURCE_TYPE_DATASOURCE
+        );
     }
 
     @Cacheable(value = AuthConstants.ROLE_DATASET_NAME, key = "'role' + #roleId")
     @Override
     public List<AuthItem> dataSetIdByRole(Long roleId) {
-        return extAuthMapper.dataSetIdByRole(roleId.toString());
+        return extAuthMapper.queryAuthItems(
+                SysAuthConstants.AUTH_TARGET_TYPE_ROLE,
+                roleId.toString(),
+                SysAuthConstants.AUTH_SOURCE_TYPE_DATASET
+        );
     }
 
     @Cacheable(value = AuthConstants.ROLE_PANEL_NAME, key = "'role' + #roleId")
     @Override
     public List<AuthItem> panelIdByRole(Long roleId) {
-        return extAuthMapper.panelIdByRole(roleId.toString());
+        return extAuthMapper.queryAuthItems(
+                SysAuthConstants.AUTH_TARGET_TYPE_ROLE,
+                roleId.toString(),
+                SysAuthConstants.AUTH_SOURCE_TYPE_PANEL
+        );
     }
 
     @Cacheable(value = AuthConstants.DEPT_LINK_NAME, key = "'dept' + #deptId")
     @Override
     public List<AuthItem> dataSourceIdByDept(Long deptId) {
         if (ObjectUtils.isEmpty(deptId)) return emptyResult;
-        return extAuthMapper.dataSourceIdByDept(deptId.toString());
+        return extAuthMapper.queryAuthItems(
+                SysAuthConstants.AUTH_TARGET_TYPE_DEPT,
+                deptId.toString(),
+                SysAuthConstants.AUTH_SOURCE_TYPE_DATASOURCE
+        );
     }
 
     @Cacheable(value = AuthConstants.DEPT_DATASET_NAME, key = "'dept' + #deptId")
     @Override
     public List<AuthItem> dataSetIdByDept(Long deptId) {
         if (ObjectUtils.isEmpty(deptId)) return emptyResult;
-        return extAuthMapper.dataSetIdByDept(deptId.toString());
+        return extAuthMapper.queryAuthItems(
+                SysAuthConstants.AUTH_TARGET_TYPE_DEPT,
+                deptId.toString(),
+                SysAuthConstants.AUTH_SOURCE_TYPE_DATASET
+        );
     }
 
     @Cacheable(value = AuthConstants.DEPT_PANEL_NAME, key = "'dept' + #deptId")
     @Override
     public List<AuthItem> panelIdByDept(Long deptId) {
         if (ObjectUtils.isEmpty(deptId)) return emptyResult;
-        return extAuthMapper.panelIdByDept(deptId.toString());
+        return extAuthMapper.queryAuthItems(
+                SysAuthConstants.AUTH_TARGET_TYPE_DEPT,
+                deptId.toString(),
+                SysAuthConstants.AUTH_SOURCE_TYPE_PANEL
+        );
     }
 
     @Caching(evict = {
diff --git a/core/backend/src/main/java/io/dataease/commons/constants/SysAuthConstants.java b/core/backend/src/main/java/io/dataease/commons/constants/SysAuthConstants.java
index a541e0f488..051f273df1 100644
--- a/core/backend/src/main/java/io/dataease/commons/constants/SysAuthConstants.java
+++ b/core/backend/src/main/java/io/dataease/commons/constants/SysAuthConstants.java
@@ -7,6 +7,12 @@ package io.dataease.commons.constants;
  */
 public class SysAuthConstants {
 
+    public final static String AUTH_TARGET_TYPE_USER = "user";
+
+    public final static String AUTH_TARGET_TYPE_ROLE = "role";
+
+    public final static String AUTH_TARGET_TYPE_DEPT = "dept";
+
     public final static String AUTH_SOURCE_TYPE_PANEL = "panel";
 
     public final static String AUTH_SOURCE_TYPE_DATASET = "dataset";
diff --git a/core/backend/src/main/java/io/dataease/ext/ExtAuthMapper.java b/core/backend/src/main/java/io/dataease/ext/ExtAuthMapper.java
index fe4fc5ca88..0e35cccd6e 100644
--- a/core/backend/src/main/java/io/dataease/ext/ExtAuthMapper.java
+++ b/core/backend/src/main/java/io/dataease/ext/ExtAuthMapper.java
@@ -16,17 +16,9 @@ public interface ExtAuthMapper {
 
     List<SysAuth> queryByResource(@Param("resourceId") String resourceId);
 
-    List<AuthItem> dataSourceIdByUser(String userId);
-    List<AuthItem> dataSetIdByUser(String userId);
-    List<AuthItem> panelIdByUser(String userId);
-
-    List<AuthItem> dataSourceIdByRole(String roleId);
-    List<AuthItem> dataSetIdByRole(String roleId);
-    List<AuthItem> panelIdByRole(String roleId);
-
-    List<AuthItem> dataSourceIdByDept(String deptId);
-    List<AuthItem> dataSetIdByDept(String deptId);
-    List<AuthItem> panelIdByDept(String deptId);
+    List<AuthItem> queryAuthItems(@Param("authTargetType") String authTargetType,
+                                  @Param("authTarget") String authTarget,
+                                  @Param("sourceType") String sourceType);
 
     String parentResource(@Param("resourceId") String resourceId, @Param("type") String type);
 }
diff --git a/core/backend/src/main/java/io/dataease/ext/ExtAuthMapper.xml b/core/backend/src/main/java/io/dataease/ext/ExtAuthMapper.xml
index fded977e66..1f81c468ff 100644
--- a/core/backend/src/main/java/io/dataease/ext/ExtAuthMapper.xml
+++ b/core/backend/src/main/java/io/dataease/ext/ExtAuthMapper.xml
@@ -33,121 +33,16 @@
         where a.auth_source = #{resourceId} and  b.privilege_value = 1
     </select>
 
-    <select id="dataSourceIdByUser" resultMap="AuthItemMap">
+    <select id="queryAuthItems" resultMap="AuthItemMap">
         SELECT
             auth_source, MAX(d.privilege_type) as level
         FROM
             sys_auth a
         LEFT JOIN sys_auth_detail d on d.auth_id = a.id
         WHERE
-            auth_source_type = 'link'
-            AND auth_target_type = 'user'
-            AND auth_target = #{userId}
-            AND d.privilege_value = 1
-        GROUP BY a.id
-    </select>
-    <select id="dataSetIdByUser" resultMap="AuthItemMap" >
-        SELECT
-            auth_source, MAX(d.privilege_type) as level
-        FROM
-            sys_auth a
-        LEFT JOIN sys_auth_detail d on d.auth_id = a.id
-        WHERE
-            auth_source_type = 'dataset'
-            AND auth_target_type = 'user'
-            AND auth_target = #{userId}
-            AND d.privilege_value = 1
-        GROUP BY a.id
-    </select>
-    <select id="panelIdByUser" resultMap="AuthItemMap">
-        SELECT
-            auth_source, MAX(d.privilege_type) as level
-        FROM
-            sys_auth a
-        LEFT JOIN sys_auth_detail d on d.auth_id = a.id
-
-        WHERE
-            auth_source_type = 'panel'
-            AND auth_target_type = 'user'
-            AND auth_target = #{userId}
-            AND d.privilege_value = 1
-        GROUP BY a.id
-    </select>
-    <select id="dataSourceIdByRole" resultMap="AuthItemMap" >
-        SELECT
-            auth_source, MAX(d.privilege_type) as level
-        FROM
-            sys_auth a
-        LEFT JOIN sys_auth_detail d on d.auth_id = a.id
-        WHERE
-            auth_source_type = 'link'
-            AND auth_target_type = 'role'
-            AND auth_target = #{roleId}
-            AND d.privilege_value = 1
-        GROUP BY a.id
-    </select>
-    <select id="dataSetIdByRole" resultMap="AuthItemMap" >
-        SELECT
-            auth_source, MAX(d.privilege_type) as level
-        FROM
-            sys_auth a
-        LEFT JOIN sys_auth_detail d on d.auth_id = a.id
-        WHERE
-            auth_source_type = 'dataset'
-            AND auth_target_type = 'role'
-            AND auth_target = #{roleId}
-            AND d.privilege_value = 1
-        GROUP BY a.id
-    </select>
-    <select id="panelIdByRole" resultMap="AuthItemMap" >
-        SELECT
-            auth_source, MAX(d.privilege_type) as level
-        FROM
-            sys_auth a
-        LEFT JOIN sys_auth_detail d on d.auth_id = a.id
-        WHERE
-            auth_source_type = 'panel'
-            AND auth_target_type = 'role'
-            AND auth_target = #{roleId}
-            AND d.privilege_value = 1
-        GROUP BY a.id
-    </select>
-    <select id="dataSourceIdByDept" resultMap="AuthItemMap" >
-        SELECT
-            auth_source, MAX(d.privilege_type) as level
-        FROM
-            sys_auth a
-        LEFT JOIN sys_auth_detail d on d.auth_id = a.id
-        WHERE
-            auth_source_type = 'link'
-            AND auth_target_type = 'dept'
-            AND auth_target = #{deptId}
-            AND d.privilege_value = 1
-        GROUP BY a.id
-    </select>
-    <select id="dataSetIdByDept" resultMap="AuthItemMap" >
-        SELECT
-            auth_source, MAX(d.privilege_type) as level
-        FROM
-            sys_auth a
-        LEFT JOIN sys_auth_detail d on d.auth_id = a.id
-        WHERE
-            auth_source_type = 'dataset'
-            AND auth_target_type = 'dept'
-            AND auth_target = #{deptId}
-            AND d.privilege_value = 1
-        GROUP BY a.id
-    </select>
-    <select id="panelIdByDept" resultMap="AuthItemMap" >
-        SELECT
-            auth_source, MAX(d.privilege_type) as level
-        FROM
-            sys_auth a
-        LEFT JOIN sys_auth_detail d on d.auth_id = a.id
-        WHERE
-            auth_source_type = 'panel'
-            AND auth_target_type = 'dept'
-            AND auth_target = #{deptId}
+            auth_source_type = #{sourceType}
+            AND auth_target_type = #{authTargetType}
+            AND auth_target = #{authTarget}
             AND d.privilege_value = 1
         GROUP BY a.id
     </select>
@@ -156,6 +51,4 @@
         select GET_V_AUTH_MODEL_WITH_PARENT(#{resourceId}, #{type})
     </select>
 
-
-
 </mapper>