Browse Source

部门加载慢问题

zhonghui 3 years ago
parent
commit
5b4da09298

+ 31 - 1
boman-api/boman-domain/src/main/java/com.boman.domain/SysDept.java

@@ -50,7 +50,13 @@ public class SysDept extends BaseEntity
 
     /** 父部门名称 */
     private String parentName;
-    
+
+    private boolean listByParentId;
+
+    private boolean hasChildren = Boolean.TRUE;
+
+    private Integer num;
+
     /** 子部门 */
     private List<SysDept> children = new ArrayList<SysDept>();
 
@@ -178,6 +184,30 @@ public class SysDept extends BaseEntity
         this.children = children;
     }
 
+    public boolean isListByParentId() {
+        return listByParentId;
+    }
+
+    public void setListByParentId(boolean listByParentId) {
+        this.listByParentId = listByParentId;
+    }
+
+    public boolean isHasChildren() {
+        return hasChildren;
+    }
+
+    public void setHasChildren(boolean hasChildren) {
+        this.hasChildren = hasChildren;
+    }
+
+    public Integer getNum() {
+        return num;
+    }
+
+    public void setNum(Integer num) {
+        this.num = num;
+    }
+
     @Override
     public String toString() {
         return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)

+ 2 - 3
boman-modules/boman-system/src/main/java/com/boman/system/controller/SysDeptController.java

@@ -39,10 +39,9 @@ public class SysDeptController extends BaseController
     /**
      * 获取部门列表
      */
-    @PreAuthorize(hasPermi = "system:dept:list")
+//    @PreAuthorize(hasPermi = "system:dept:list")
     @GetMapping("/list")
-    public AjaxResult list(SysDept dept)
-    {
+    public AjaxResult list(SysDept dept) {
         List<SysDept> depts = deptService.selectDeptList(dept);
         return AjaxResult.success(depts);
     }

+ 3 - 0
boman-modules/boman-system/src/main/java/com/boman/system/mapper/SysDeptMapper.java

@@ -30,6 +30,9 @@ public interface SysDeptMapper
      */
     public List<Integer> selectDeptListById(@Param("id") Long id, @Param("deptCheckStrictly") boolean deptCheckStrictly);
 
+
+    public List<SysDept> validateHasChirld(@Param("depts") List<SysDept> depts);
+
     /**
      * 根据部门ID查询信息
      * 

+ 34 - 6
boman-modules/boman-system/src/main/java/com/boman/system/service/impl/SysDeptServiceImpl.java

@@ -1,8 +1,6 @@
 package com.boman.system.service.impl;
 
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
+import java.util.*;
 import java.util.stream.Collectors;
 
 import com.boman.common.core.utils.obj.ObjectUtils;
@@ -42,9 +40,39 @@ public class SysDeptServiceImpl implements ISysDeptService
      */
     @Override
     @DataScope(deptAlias = "d")
-    public List<SysDept> selectDeptList(SysDept dept)
-    {
-        return deptMapper.selectDeptList(dept);
+    public List<SysDept> selectDeptList(SysDept dept) {
+        Long id = dept.getId();
+        if(id == null) {
+            if(StringUtils.isNotEmpty(dept.getDeptName()) || StringUtils.isNotEmpty(dept.getStatus())) {
+                dept.setListByParentId(Boolean.TRUE);
+                return deptMapper.selectDeptList(dept);
+            }
+            dept.setListByParentId(Boolean.FALSE);
+        }else{
+            dept.setListByParentId(Boolean.TRUE);
+            dept.setParentId(id);
+        }
+        List<SysDept> depts = deptMapper.selectDeptList(dept);
+        // 这里的逻辑是:根据id获取到这个下面的所有子部门,每个部门判断是否有下一级别
+        if(depts != null && depts.size() > 0) {
+            List<SysDept> hasChirlds = deptMapper.validateHasChirld(depts);
+            if(hasChirlds != null && hasChirlds.size() > 0) {
+                Map<Long, SysDept> hasChirldMap = new HashMap<>();
+                for (SysDept hasChirld : hasChirlds) {
+                    hasChirldMap.put(hasChirld.getId(), hasChirld);
+                }
+                for (SysDept sysDept : depts) {
+                    if(hasChirldMap.get(sysDept.getId()) == null) {
+                        sysDept.setHasChildren(Boolean.FALSE);
+                    }
+                }
+            }else {
+                for (SysDept sysDept : depts) {
+                    sysDept.setHasChildren(Boolean.FALSE);
+                }
+            }
+        }
+        return depts;
     }
 
     /**

+ 12 - 0
boman-modules/boman-system/src/main/resources/mapper/system/SysDeptMapper.xml

@@ -16,6 +16,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 		<result property="status"     column="status"      />
 		<result property="delFlag"    column="del_flag"    />
 		<result property="parentName" column="parent_name" />
+		<result property="num" column="num" />
 		<result property="createBy"   column="create_by"   />
 		<result property="createTime" column="create_time" />
 		<result property="updateBy"   column="update_by"   />
@@ -30,6 +31,9 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 	<select id="selectDeptList" parameterType="com.boman.domain.SysDept" resultMap="SysDeptResult">
         <include refid="selectDeptVo"/>
         where d.del_flag = '0'
+		<if test="listByParentId != null and listByParentId == false">
+			AND (parent_id is null or parent_id = 0)
+		</if>
         <if test="parentId != null and parentId != 0">
 			AND parent_id = #{parentId}
 		</if>
@@ -43,6 +47,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 		${params.dataScope}
 		order by d.parent_id, d.order_num
     </select>
+
+	<select id="validateHasChirld" parameterType="java.util.List" resultMap="SysDeptResult">
+		SELECT a.id as id, count(1) as num FROM sys_dept a LEFT JOIN sys_dept b on a.id = b.parent_id WHERE b.parent_id in
+		<foreach collection="depts" item="dept" index="index" separator="," open="(" close=")">
+			#{dept.id}
+		</foreach>
+		GROUP BY a.id
+	</select>
     
     <select id="selectDeptListById" resultType="java.lang.Integer">
 		select d.id