Sfoglia il codice sorgente

角色修改优化

wangliang 3 anni fa
parent
commit
4ec484b066

+ 40 - 0
teachcloud-common/src/main/java/com/qmth/teachcloud/common/bean/result/UserRoleNameResult.java

@@ -0,0 +1,40 @@
+package com.qmth.teachcloud.common.bean.result;
+
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
+import io.swagger.annotations.ApiModelProperty;
+
+import java.io.Serializable;
+
+/**
+ * @Description: 批量查询用户角色
+ * @Param:
+ * @return:
+ * @Author: wangliang
+ * @Date: 2021/12/21
+ */
+public class UserRoleNameResult implements Serializable {
+
+    @JsonSerialize(using = ToStringSerializer.class)
+    @ApiModelProperty(value = "用户id")
+    private Long userId;
+
+    @ApiModelProperty(value = "用户角色名")
+    private String roleNames;
+
+    public Long getUserId() {
+        return userId;
+    }
+
+    public void setUserId(Long userId) {
+        this.userId = userId;
+    }
+
+    public String getRoleNames() {
+        return roleNames;
+    }
+
+    public void setRoleNames(String roleNames) {
+        this.roleNames = roleNames;
+    }
+}

+ 1 - 0
teachcloud-common/src/main/java/com/qmth/teachcloud/common/contant/SystemConstant.java

@@ -68,6 +68,7 @@ public class SystemConstant {
     public static final int PAGE_SIZE_MIN = 10;
     public static final int PAGE_SIZE_MAX = 500;
     public static final int PAGE_NUMBER_MIN = 1;
+    public static final int IN_SIZE_MAX = 1000;
     public static final int ALL_CARD = -1;
     public static final String MANUAL = "manual";
     public static final String DELIMITER = ":";

+ 9 - 0
teachcloud-common/src/main/java/com/qmth/teachcloud/common/mapper/SysUserMapper.java

@@ -7,6 +7,7 @@ import com.qmth.teachcloud.common.bean.dto.BlurryUserDto;
 import com.qmth.teachcloud.common.bean.dto.UserDto;
 import com.qmth.teachcloud.common.bean.dto.VerifyCodeCheckDto;
 import com.qmth.teachcloud.common.bean.params.ApproveUserResult;
+import com.qmth.teachcloud.common.bean.result.UserRoleNameResult;
 import com.qmth.teachcloud.common.entity.SysUser;
 import org.apache.ibatis.annotations.Param;
 
@@ -74,4 +75,12 @@ public interface SysUserMapper extends BaseMapper<SysUser> {
      * @return
      */
     List<ApproveUserResult> findByOrgIds(@Param("orgIdSet") Set<Long> orgIdSet);
+
+    /**
+     * 查找用户角色名
+     *
+     * @param userIds
+     * @return
+     */
+    List<UserRoleNameResult> selectRoleNames(@Param("userIds") List<Long> userIds);
 }

+ 9 - 0
teachcloud-common/src/main/java/com/qmth/teachcloud/common/service/SysUserService.java

@@ -10,6 +10,7 @@ import com.qmth.teachcloud.common.bean.params.UserPushParam;
 import com.qmth.teachcloud.common.bean.params.UserSaveParams;
 import com.qmth.teachcloud.common.bean.result.LoginResult;
 import com.qmth.teachcloud.common.bean.result.SyncCountResult;
+import com.qmth.teachcloud.common.bean.result.UserRoleNameResult;
 import com.qmth.teachcloud.common.entity.SysUser;
 import com.qmth.teachcloud.common.enums.FlowApproveNameEnum;
 import com.qmth.teachcloud.common.enums.userPush.SpecialPrivilegeEnum;
@@ -243,4 +244,12 @@ public interface SysUserService extends IService<SysUser> {
     SpecialPrivilegeEnum findUserSpecialPrivilegeByUserId(Long userId);
 
     SyncCountResult needPushAgainOperate(SysUser requestUser) throws IllegalAccessException;
+
+    /**
+     * 查找用户角色名
+     *
+     * @param userIds
+     * @return
+     */
+    List<UserRoleNameResult> selectRoleNames(List<Long> userIds);
 }

+ 11 - 2
teachcloud-common/src/main/java/com/qmth/teachcloud/common/service/TeachcloudCommonService.java

@@ -119,6 +119,14 @@ public interface TeachcloudCommonService {
      */
     public void removeUserInfo(Long userId, boolean all) throws NoSuchAlgorithmException;
 
+    /**
+     * 批量删除用户信息
+     *
+     * @param userIds
+     * @param all
+     */
+    public void removeUserInfoBatch(List<Long> userIds, boolean all);
+
     /**
      * 修改角色状态
      *
@@ -243,9 +251,10 @@ public interface TeachcloudCommonService {
      */
     /**
      * 根据编码类型获取系统编码
-     * @param type 编码类型
+     *
+     * @param type        编码类型
      * @param requestUser 请求的用户
      * @return 系统编码
      */
-    public String getSysIncrCode(SystemCodeEnum type,SysUser requestUser);
+    public String getSysIncrCode(SystemCodeEnum type, SysUser requestUser);
 }

+ 6 - 4
teachcloud-common/src/main/java/com/qmth/teachcloud/common/service/impl/SysRoleServiceImpl.java

@@ -18,6 +18,8 @@ import com.qmth.teachcloud.common.mapper.SysRoleMapper;
 import com.qmth.teachcloud.common.service.*;
 import com.qmth.teachcloud.common.util.ResultUtil;
 import com.qmth.teachcloud.common.util.ServletUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.dao.DuplicateKeyException;
 import org.springframework.stereotype.Service;
@@ -38,6 +40,7 @@ import java.util.stream.Collectors;
  */
 @Service
 public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
+    private final static Logger log = LoggerFactory.getLogger(SysRoleServiceImpl.class);
 
     @Autowired
     private SysUserRoleService sysUserRoleService;
@@ -132,6 +135,7 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
     @Transactional
     @Override
     public boolean saveRoleNew(SysRole role) {
+        long start = System.currentTimeMillis();
         try {
             SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
             Long schoolId = Objects.nonNull(ServletUtil.getRequestHeaderSchoolIdByNotVaild()) ? Long.valueOf(ServletUtil.getRequestHeaderSchoolIdByNotVaild().toString()) : null;
@@ -164,7 +168,7 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
                 commonCacheService.updateRoleCache(role.getId());
                 if (count != sysRolePrivilegeList.size() || count != finalRelatedSet.size()) {
                     // 删除权限前先更新涉及特殊权限用户 -> 需要重新同步
-                    List<SysRolePrivilege> dbRolePriList = sysRolePrivilegeService.listByRoleId(role.getId());
+                    List<SysRolePrivilege> dbRolePriList = commonCacheService.rolePrivilegeCache(role.getId());
                     this.updateUserPushStats(dbRolePriList.stream().map(SysRolePrivilege::getPrivilegeId).distinct().collect(Collectors.toList()),
                             Arrays.asList(role.getPrivilegeIds()),
                             role.getId(),
@@ -174,9 +178,7 @@ public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> impl
                     commonCacheService.updateRolePrivilegeCache(role.getId());
                     //绑定该角色的用户都需要清除鉴权缓存
                     List<SysUserRole> sysUserRoleList = sysUserRoleService.listByRoleId(role.getId());
-                    for (SysUserRole s : sysUserRoleList) {
-                        commonService.removeUserInfo(s.getUserId(), true);
-                    }
+                    commonService.removeUserInfoBatch(sysUserRoleList.stream().map(s -> s.getUserId()).collect(Collectors.toList()), true);
                 }
             } else {
                 role.insertInfo(sysUser.getId());

+ 12 - 0
teachcloud-common/src/main/java/com/qmth/teachcloud/common/service/impl/SysUserServiceImpl.java

@@ -22,6 +22,7 @@ import com.qmth.teachcloud.common.bean.params.UserSaveParams;
 import com.qmth.teachcloud.common.bean.result.LoginResult;
 import com.qmth.teachcloud.common.bean.result.SyncCountResult;
 import com.qmth.teachcloud.common.bean.result.UserBatchDisposeResult;
+import com.qmth.teachcloud.common.bean.result.UserRoleNameResult;
 import com.qmth.teachcloud.common.config.DictionaryConfig;
 import com.qmth.teachcloud.common.contant.SpringContextHolder;
 import com.qmth.teachcloud.common.contant.SystemConstant;
@@ -1189,6 +1190,17 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
         return syncCountResult;
     }
 
+    /**
+     * 查找用户角色名
+     *
+     * @param userIds
+     * @return
+     */
+    @Override
+    public List<UserRoleNameResult> selectRoleNames(List<Long> userIds) {
+        return sysUserMapper.selectRoleNames(userIds);
+    }
+
     /**
      * 批量处理用户信息帮助类
      *

+ 57 - 1
teachcloud-common/src/main/java/com/qmth/teachcloud/common/service/impl/TeachcloudCommonServiceImpl.java

@@ -420,6 +420,62 @@ public class TeachcloudCommonServiceImpl implements TeachcloudCommonService {
         commonCacheService.removeUserRolePrivilegeCache(userId);
     }
 
+    /**
+     * 批量删除用户信息
+     *
+     * @param userIds
+     * @param all
+     */
+    @Override
+    public void removeUserInfoBatch(List<Long> userIds, boolean all) {
+        new Thread(() -> {
+            if (all) {
+                Set<String> sessionIdSet = new HashSet<>(Platform.values().length * userIds.size());
+                int min = 0;
+                int max = SystemConstant.IN_SIZE_MAX, size = userIds.size();
+                if (max >= size) {
+                    max = size;
+                }
+                while (max <= size) {
+                    List<UserRoleNameResult> userRoleNameResultList = sysUserService.selectRoleNames(userIds.subList(min, max));
+                    for (UserRoleNameResult userRoleNameResult : userRoleNameResultList) {
+                        for (Platform p : Platform.values()) {
+                            String sessionId = null;
+                            try {
+                                sessionId = SessionUtil.digest(userRoleNameResult.getUserId(), Math.abs(Arrays.asList(userRoleNameResult.getRoleNames().split(",")).toString().hashCode()), p.name());
+                            } catch (NoSuchAlgorithmException e) {
+                                e.printStackTrace();
+                            }
+                            sessionIdSet.add(sessionId);
+                        }
+                    }
+                    if (max == size) {
+                        break;
+                    }
+                    min = max;
+                    max += SystemConstant.IN_SIZE_MAX;
+                    if (max >= size) {
+                        max = size;
+                    }
+                }
+                tbSessionService.removeByIds(sessionIdSet);
+                for (String s : sessionIdSet) {
+                    redisUtil.deleteUserSession(s);
+                }
+            } else {
+                TBSession tbSession = (TBSession) ServletUtil.getRequestSession();
+                tbSessionService.removeById(tbSession.getId());
+                redisUtil.deleteUserSession(tbSession.getId());
+            }
+            for (Long l : userIds) {
+                commonCacheService.removeUserCache(l);
+                commonCacheService.removeUserAuthCache(l);
+                commonCacheService.removeUserMenuCache(l);
+                commonCacheService.removeUserRolePrivilegeCache(l);
+            }
+        }).start();
+    }
+
     /**
      * 修改角色状态
      *
@@ -770,7 +826,7 @@ public class TeachcloudCommonServiceImpl implements TeachcloudCommonService {
         try {
             String zipPath = SystemConstant.TEMP_FILES_DIR + File.separator + schoolId;
             File zipPathFile = new File(zipPath);
-            if(!zipPathFile.exists()){
+            if (!zipPathFile.exists()) {
                 zipPathFile.mkdirs();
             }
             zipFile = FileUtil.file(zipPath, time + ".zip");

+ 15 - 0
teachcloud-common/src/main/resources/mapper/SysUserMapper.xml

@@ -196,6 +196,21 @@
         </where>
     </select>
 
+    <select id="selectRoleNames" resultType="com.qmth.teachcloud.common.bean.result.UserRoleNameResult">
+        select group_concat(sr.name) as roleNames
+             ,sur.user_id as userId from sys_role sr
+        join sys_user_role sur on sur.role_id = sr.id
+        <where>
+            <if test="userIds != null">
+                AND sur.user_id IN
+                <foreach collection="userIds" item="item" index="index" open="(" separator="," close=")">
+                    #{item}
+                </foreach>
+            </if>
+        </where>
+        group by sur.user_id
+    </select>
+
     <select id="findVerifyCodeByUser" resultType="com.qmth.teachcloud.common.bean.dto.VerifyCodeCheckDto">
         SELECT
             id,