From cc2624d08e422c36a4f93cc15d0ca0f0b7b4951a Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期一, 17 一月 2022 15:46:28 +0800
Subject: [PATCH] update 优化加载字典缓存数据

---
 ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java |   58 ++++++++++++++++++++++++++++------------------------------
 1 files changed, 28 insertions(+), 30 deletions(-)

diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java
index 0a2f8e6..b5b4b88 100644
--- a/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java
+++ b/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysDeptServiceImpl.java
@@ -1,17 +1,16 @@
 package com.ruoyi.system.service.impl;
 
+import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.convert.Convert;
 import cn.hutool.core.lang.tree.Tree;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
-import com.ruoyi.common.annotation.DataScope;
 import com.ruoyi.common.constant.UserConstants;
 import com.ruoyi.common.core.domain.entity.SysDept;
 import com.ruoyi.common.core.domain.entity.SysRole;
 import com.ruoyi.common.core.domain.entity.SysUser;
-import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
 import com.ruoyi.common.exception.ServiceException;
-import com.ruoyi.common.utils.SecurityUtils;
+import com.ruoyi.common.helper.LoginHelper;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.TreeBuildUtils;
 import com.ruoyi.common.utils.spring.SpringUtils;
@@ -19,7 +18,7 @@
 import com.ruoyi.system.mapper.SysRoleMapper;
 import com.ruoyi.system.mapper.SysUserMapper;
 import com.ruoyi.system.service.ISysDeptService;
-import org.springframework.beans.factory.annotation.Autowired;
+import lombok.RequiredArgsConstructor;
 import org.springframework.stereotype.Service;
 
 import java.util.Arrays;
@@ -30,14 +29,13 @@
  *
  * @author Lion Li
  */
+@RequiredArgsConstructor
 @Service
-public class SysDeptServiceImpl extends ServicePlusImpl<SysDeptMapper, SysDept, SysDept> implements ISysDeptService {
+public class SysDeptServiceImpl implements ISysDeptService {
 
-    @Autowired
-    private SysRoleMapper roleMapper;
-
-    @Autowired
-    private SysUserMapper userMapper;
+    private final SysDeptMapper baseMapper;
+    private final SysRoleMapper roleMapper;
+    private final SysUserMapper userMapper;
 
     /**
      * 鏌ヨ閮ㄩ棬绠$悊鏁版嵁
@@ -46,7 +44,6 @@
      * @return 閮ㄩ棬淇℃伅闆嗗悎
      */
     @Override
-    @DataScope(deptAlias = "d")
     public List<SysDept> selectDeptList(SysDept dept) {
         return baseMapper.selectDeptList(dept);
     }
@@ -59,7 +56,11 @@
      */
     @Override
     public List<Tree<Long>> buildDeptTreeSelect(List<SysDept> depts) {
-        return TreeBuildUtils.build(depts, (dept, tree) ->
+        if (CollUtil.isEmpty(depts)) {
+            return CollUtil.newArrayList();
+        }
+        Long parentId = depts.get(0).getParentId();
+        return TreeBuildUtils.build(depts, parentId, (dept, tree) ->
             tree.setId(dept.getDeptId())
                 .setParentId(dept.getParentId())
                 .setName(dept.getDeptName())
@@ -73,7 +74,7 @@
      * @return 閫変腑閮ㄩ棬鍒楄〃
      */
     @Override
-    public List<Integer> selectDeptListByRoleId(Long roleId) {
+    public List<Long> selectDeptListByRoleId(Long roleId) {
         SysRole role = roleMapper.selectById(roleId);
         return baseMapper.selectDeptListByRoleId(roleId, role.isDeptCheckStrictly());
     }
@@ -86,7 +87,7 @@
      */
     @Override
     public SysDept selectDeptById(Long deptId) {
-        return getById(deptId);
+        return baseMapper.selectById(deptId);
     }
 
     /**
@@ -97,7 +98,7 @@
      */
     @Override
     public long selectNormalChildrenDeptById(Long deptId) {
-        return count(new LambdaQueryWrapper<SysDept>()
+        return baseMapper.selectCount(new LambdaQueryWrapper<SysDept>()
             .eq(SysDept::getStatus, 0)
             .apply("find_in_set({0}, ancestors)", deptId));
     }
@@ -110,10 +111,8 @@
      */
     @Override
     public boolean hasChildByDeptId(Long deptId) {
-        long result = count(new LambdaQueryWrapper<SysDept>()
-            .eq(SysDept::getParentId, deptId)
-            .last("limit 1"));
-        return result > 0;
+        return baseMapper.exists(new LambdaQueryWrapper<SysDept>()
+            .eq(SysDept::getParentId, deptId));
     }
 
     /**
@@ -124,9 +123,8 @@
      */
     @Override
     public boolean checkDeptExistUser(Long deptId) {
-        long result = userMapper.selectCount(new LambdaQueryWrapper<SysUser>()
+        return userMapper.exists(new LambdaQueryWrapper<SysUser>()
             .eq(SysUser::getDeptId, deptId));
-        return result > 0;
     }
 
     /**
@@ -138,11 +136,11 @@
     @Override
     public String checkDeptNameUnique(SysDept dept) {
         Long deptId = StringUtils.isNull(dept.getDeptId()) ? -1L : dept.getDeptId();
-        SysDept info = getOne(new LambdaQueryWrapper<SysDept>()
+        boolean count = baseMapper.exists(new LambdaQueryWrapper<SysDept>()
             .eq(SysDept::getDeptName, dept.getDeptName())
             .eq(SysDept::getParentId, dept.getParentId())
-            .last("limit 1"));
-        if (StringUtils.isNotNull(info) && info.getDeptId().longValue() != deptId.longValue()) {
+            .ne(SysDept::getDeptId, deptId));
+        if (count) {
             return UserConstants.NOT_UNIQUE;
         }
         return UserConstants.UNIQUE;
@@ -155,7 +153,7 @@
      */
     @Override
     public void checkDeptDataScope(Long deptId) {
-        if (!SysUser.isAdmin(SecurityUtils.getUserId())) {
+        if (!SysUser.isAdmin(LoginHelper.getUserId())) {
             SysDept dept = new SysDept();
             dept.setDeptId(deptId);
             List<SysDept> depts = SpringUtils.getAopProxy(this).selectDeptList(dept);
@@ -173,7 +171,7 @@
      */
     @Override
     public int insertDept(SysDept dept) {
-        SysDept info = getById(dept.getParentId());
+        SysDept info = baseMapper.selectById(dept.getParentId());
         // 濡傛灉鐖惰妭鐐逛笉涓烘甯哥姸鎬�,鍒欎笉鍏佽鏂板瀛愯妭鐐�
         if (!UserConstants.DEPT_NORMAL.equals(info.getStatus())) {
             throw new ServiceException("閮ㄩ棬鍋滅敤锛屼笉鍏佽鏂板");
@@ -190,8 +188,8 @@
      */
     @Override
     public int updateDept(SysDept dept) {
-        SysDept newParentDept = getById(dept.getParentId());
-        SysDept oldDept = getById(dept.getDeptId());
+        SysDept newParentDept = baseMapper.selectById(dept.getParentId());
+        SysDept oldDept = baseMapper.selectById(dept.getDeptId());
         if (StringUtils.isNotNull(newParentDept) && StringUtils.isNotNull(oldDept)) {
             String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId();
             String oldAncestors = oldDept.getAncestors();
@@ -215,7 +213,7 @@
     private void updateParentDeptStatusNormal(SysDept dept) {
         String ancestors = dept.getAncestors();
         Long[] deptIds = Convert.toLongArray(ancestors);
-        update(null, new LambdaUpdateWrapper<SysDept>()
+        baseMapper.update(null, new LambdaUpdateWrapper<SysDept>()
             .set(SysDept::getStatus, "0")
             .in(SysDept::getDeptId, Arrays.asList(deptIds)));
     }
@@ -228,7 +226,7 @@
      * @param oldAncestors 鏃х殑鐖禝D闆嗗悎
      */
     public void updateDeptChildren(Long deptId, String newAncestors, String oldAncestors) {
-        List<SysDept> children = list(new LambdaQueryWrapper<SysDept>()
+        List<SysDept> children = baseMapper.selectList(new LambdaQueryWrapper<SysDept>()
             .apply("find_in_set({0},ancestors)", deptId));
         for (SysDept child : children) {
             child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));

--
Gitblit v1.9.3