Просмотр исходного кода

:zap: 部门列表树改为懒加载

smallchill 6 лет назад
Родитель
Сommit
987df0f49a

+ 30 - 3
src/main/java/org/springblade/modules/system/controller/DeptController.java

@@ -32,6 +32,7 @@ import org.springblade.core.tool.node.INode;
 import org.springblade.core.tool.utils.Func;
 import org.springblade.modules.system.entity.Dept;
 import org.springblade.modules.system.service.IDeptService;
+import org.springblade.modules.system.vo.DeptLazyVO;
 import org.springblade.modules.system.vo.DeptVO;
 import org.springblade.modules.system.wrapper.DeptWrapper;
 import org.springframework.cache.annotation.CacheEvict;
@@ -85,22 +86,48 @@ public class DeptController extends BladeController {
 		return R.data(DeptWrapper.build().listNodeVO(list));
 	}
 
+	/**
+	 * 懒加载列表
+	 */
+	@GetMapping("/lazy-list")
+	@ApiImplicitParams({
+		@ApiImplicitParam(name = "deptName", value = "部门名称", paramType = "query", dataType = "string"),
+		@ApiImplicitParam(name = "fullName", value = "部门全称", paramType = "query", dataType = "string")
+	})
+	@ApiOperationSupport(order = 3)
+	@ApiOperation(value = "懒加载列表", notes = "传入dept")
+	public R<List<INode>> lazyList(@ApiIgnore @RequestParam Map<String, Object> dept, @RequestParam(required = false, defaultValue = "0") Long parentId, BladeUser bladeUser) {
+		List<DeptLazyVO> list = deptService.lazyList(bladeUser.getTenantId(), parentId, dept);
+		return R.data(DeptWrapper.build().listNodeLazyVO(list));
+	}
+
 	/**
 	 * 获取部门树形结构
 	 */
 	@GetMapping("/tree")
-	@ApiOperationSupport(order = 3)
+	@ApiOperationSupport(order = 4)
 	@ApiOperation(value = "树形结构", notes = "树形结构")
 	public R<List<DeptVO>> tree(String tenantId, BladeUser bladeUser) {
 		List<DeptVO> tree = deptService.tree(Func.toStrWithEmpty(tenantId, bladeUser.getTenantId()));
 		return R.data(tree);
 	}
 
+	/**
+	 * 懒加载获取部门树形结构
+	 */
+	@GetMapping("/lazy-tree")
+	@ApiOperationSupport(order = 5)
+	@ApiOperation(value = "懒加载树形结构", notes = "树形结构")
+	public R<List<DeptVO>> lazyTree(String tenantId, @RequestParam(required = false, defaultValue = "0") Long parentId, BladeUser bladeUser) {
+		List<DeptVO> tree = deptService.lazyTree(Func.toStrWithEmpty(tenantId, bladeUser.getTenantId()), parentId);
+		return R.data(tree);
+	}
+
 	/**
 	 * 新增或修改
 	 */
 	@PostMapping("/submit")
-	@ApiOperationSupport(order = 4)
+	@ApiOperationSupport(order = 6)
 	@ApiOperation(value = "新增或修改", notes = "传入dept")
 	@CacheEvict(cacheNames = {SYS_CACHE}, allEntries = true)
 	public R submit(@Valid @RequestBody Dept dept) {
@@ -111,7 +138,7 @@ public class DeptController extends BladeController {
 	 * 删除
 	 */
 	@PostMapping("/remove")
-	@ApiOperationSupport(order = 5)
+	@ApiOperationSupport(order = 7)
 	@ApiOperation(value = "删除", notes = "传入ids")
 	@CacheEvict(cacheNames = {SYS_CACHE}, allEntries = true)
 	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {

+ 16 - 5
src/main/java/org/springblade/modules/system/mapper/DeptMapper.java

@@ -17,11 +17,12 @@
 package org.springblade.modules.system.mapper;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
-import com.baomidou.mybatisplus.core.metadata.IPage;
 import org.springblade.modules.system.entity.Dept;
+import org.springblade.modules.system.vo.DeptLazyVO;
 import org.springblade.modules.system.vo.DeptVO;
 
 import java.util.List;
+import java.util.Map;
 
 /**
  * Mapper 接口
@@ -31,13 +32,14 @@ import java.util.List;
 public interface DeptMapper extends BaseMapper<Dept> {
 
 	/**
-	 * 自定义分页
+	 * 懒加载部门列表
 	 *
-	 * @param page
-	 * @param dept
+	 * @param tenantId
+	 * @param parentId
+	 * @param param
 	 * @return
 	 */
-	List<DeptVO> selectDeptPage(IPage page, DeptVO dept);
+	List<DeptLazyVO> lazyList(String tenantId, Long parentId, Map<String, Object> param);
 
 	/**
 	 * 获取树形节点
@@ -47,6 +49,15 @@ public interface DeptMapper extends BaseMapper<Dept> {
 	 */
 	List<DeptVO> tree(String tenantId);
 
+	/**
+	 * 懒加载获取树形节点
+	 *
+	 * @param tenantId
+	 * @param parentId
+	 * @return
+	 */
+	List<DeptVO> lazyTree(String tenantId, Long parentId);
+
 	/**
 	 * 获取部门名
 	 *

+ 65 - 2
src/main/java/org/springblade/modules/system/mapper/DeptMapper.xml

@@ -15,12 +15,26 @@
         <result column="is_deleted" property="isDeleted"/>
     </resultMap>
 
+    <resultMap id="deptLazyVOResultMap" type="org.springblade.modules.system.vo.DeptLazyVO">
+        <id column="id" property="id"/>
+        <result column="parent_id" property="parentId"/>
+        <result column="dept_name" property="deptName"/>
+        <result column="full_name" property="fullName"/>
+        <result column="ancestors" property="ancestors"/>
+        <result column="dept_category" property="deptCategory"/>
+        <result column="sort" property="sort"/>
+        <result column="remark" property="remark"/>
+        <result column="is_deleted" property="isDeleted"/>
+        <result column="has_children" property="hasChildren"/>
+    </resultMap>
+
     <resultMap id="treeNodeResultMap" type="org.springblade.core.tool.node.TreeNode">
         <id column="id" property="id"/>
         <result column="parent_id" property="parentId"/>
         <result column="title" property="title"/>
         <result column="value" property="value"/>
         <result column="key" property="key"/>
+        <result column="has_children" property="hasChildren"/>
     </resultMap>
 
     <!-- 通用查询结果列 -->
@@ -29,8 +43,33 @@
         id, parent_id, dept_name, full_name, sort, remark, is_deleted
     </sql>
 
-    <select id="selectDeptPage" resultMap="deptResultMap">
-        select * from blade_dept where is_deleted = 0
+    <select id="lazyList" resultMap="deptLazyVOResultMap">
+        SELECT
+            dept.* ,
+            (
+                SELECT
+                    CASE WHEN count(1) > 0 THEN 1 ELSE 0 END
+                FROM
+                    blade_dept
+                WHERE
+                    parent_id = dept.id
+            ) AS "has_children"
+        FROM
+            blade_dept dept
+        <where>
+            <if test="param1!=null and param1!=''">
+                and tenant_id = #{param1}
+            </if>
+            <if test="param2!=null and param2!=''">
+                and parent_id = #{param2}
+            </if>
+            <if test="param.deptName!=null and param.deptName!=''">
+                and dept.dept_name like concat('%', #{param.deptName},'%')
+            </if>
+            <if test="param.fullName!=null and param.fullName!=''">
+                and dept.full_name like concat('%', #{param.fullName},'%')
+            </if>
+        </where>
     </select>
 
     <select id="tree" resultMap="treeNodeResultMap">
@@ -40,6 +79,30 @@
         </if>
     </select>
 
+    <select id="lazyTree" resultMap="treeNodeResultMap" >
+        SELECT
+            dept.id,
+            dept.parent_id,
+            dept.dept_name AS title,
+            dept.id AS "value",
+            dept.id AS "key",
+            (
+                SELECT
+                    count( 1 ) > 0
+                FROM
+                    blade_dept
+                WHERE
+                    parent_id = dept.id
+            ) AS "has_children"
+        FROM
+            blade_dept dept
+        WHERE
+            parent_id = #{param2}
+        <if test="param1!=null and param1!=''">
+            and tenant_id = #{param1}
+        </if>
+    </select>
+
     <select id="getDeptNames" resultType="java.lang.String">
         SELECT
         dept_name

+ 16 - 5
src/main/java/org/springblade/modules/system/service/IDeptService.java

@@ -16,12 +16,13 @@
  */
 package org.springblade.modules.system.service;
 
-import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.service.IService;
 import org.springblade.modules.system.entity.Dept;
+import org.springblade.modules.system.vo.DeptLazyVO;
 import org.springblade.modules.system.vo.DeptVO;
 
 import java.util.List;
+import java.util.Map;
 
 /**
  * 服务类
@@ -31,13 +32,14 @@ import java.util.List;
 public interface IDeptService extends IService<Dept> {
 
 	/**
-	 * 自定义分页
+	 * 懒加载部门列表
 	 *
-	 * @param page
-	 * @param dept
+	 * @param tenantId
+	 * @param parentId
+	 * @param param
 	 * @return
 	 */
-	IPage<DeptVO> selectDeptPage(IPage<DeptVO> page, DeptVO dept);
+	List<DeptLazyVO> lazyList(String tenantId, Long parentId, Map<String, Object> param);
 
 	/**
 	 * 树形结构
@@ -47,6 +49,15 @@ public interface IDeptService extends IService<Dept> {
 	 */
 	List<DeptVO> tree(String tenantId);
 
+	/**
+	 * 懒加载树形结构
+	 *
+	 * @param tenantId
+	 * @param parentId
+	 * @return
+	 */
+	List<DeptVO> lazyTree(String tenantId, Long parentId);
+
 	/**
 	 * 获取部门名
 	 *

+ 19 - 3
src/main/java/org/springblade/modules/system/service/impl/DeptServiceImpl.java

@@ -16,7 +16,6 @@
  */
 package org.springblade.modules.system.service.impl;
 
-import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.exceptions.ApiException;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -29,10 +28,12 @@ import org.springblade.core.tool.utils.StringPool;
 import org.springblade.modules.system.entity.Dept;
 import org.springblade.modules.system.mapper.DeptMapper;
 import org.springblade.modules.system.service.IDeptService;
+import org.springblade.modules.system.vo.DeptLazyVO;
 import org.springblade.modules.system.vo.DeptVO;
 import org.springframework.stereotype.Service;
 
 import java.util.List;
+import java.util.Map;
 
 /**
  * 服务实现类
@@ -43,8 +44,15 @@ import java.util.List;
 public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements IDeptService {
 
 	@Override
-	public IPage<DeptVO> selectDeptPage(IPage<DeptVO> page, DeptVO dept) {
-		return page.setRecords(baseMapper.selectDeptPage(page, dept));
+	public List<DeptLazyVO> lazyList(String tenantId, Long parentId, Map<String, Object> param) {
+		if (AuthUtil.isAdministrator()) {
+			tenantId = StringPool.EMPTY;
+		}
+		String paramTenantId = Func.toStr(param.get("tenantId"));
+		if (Func.isNotEmpty(paramTenantId) && AuthUtil.isAdministrator()) {
+			tenantId = paramTenantId;
+		}
+		return baseMapper.lazyList(tenantId, parentId, param);
 	}
 
 	@Override
@@ -55,6 +63,14 @@ public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements ID
 		return ForestNodeMerger.merge(baseMapper.tree(tenantId));
 	}
 
+	@Override
+	public List<DeptVO> lazyTree(String tenantId, Long parentId) {
+		if (AuthUtil.isAdministrator()) {
+			tenantId = StringPool.EMPTY;
+		}
+		return ForestNodeMerger.merge(baseMapper.lazyTree(tenantId, parentId));
+	}
+
 	@Override
 	public List<String> getDeptNames(String deptIds) {
 		return baseMapper.getDeptNames(Func.toLongArray(deptIds));

+ 38 - 0
src/main/java/org/springblade/modules/system/vo/DeptLazyVO.java

@@ -0,0 +1,38 @@
+/*
+ *      Copyright (c) 2018-2028, Chill Zhuang 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 dreamlu.net 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: Chill 庄骞 (smallchill@163.com)
+ */
+package org.springblade.modules.system.vo;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import io.swagger.annotations.ApiModel;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
+/**
+ * 视图实体类
+ *
+ * @author Chill
+ */
+@Data
+@EqualsAndHashCode(callSuper = true)
+@ApiModel(value = "DeptLazyVO对象", description = "DeptLazyVO对象")
+public class DeptLazyVO extends DeptVO {
+	private static final long serialVersionUID = 1L;
+
+	@JsonInclude(JsonInclude.Include.NON_EMPTY)
+	private Boolean hasChildren;
+
+}

+ 9 - 1
src/main/java/org/springblade/modules/system/wrapper/DeptWrapper.java

@@ -25,6 +25,7 @@ import org.springblade.core.tool.node.INode;
 import org.springblade.core.tool.utils.BeanUtil;
 import org.springblade.core.tool.utils.Func;
 import org.springblade.modules.system.entity.Dept;
+import org.springblade.modules.system.vo.DeptLazyVO;
 import org.springblade.modules.system.vo.DeptVO;
 
 import java.util.List;
@@ -56,7 +57,6 @@ public class DeptWrapper extends BaseEntityWrapper<Dept, DeptVO> {
 		return deptVO;
 	}
 
-
 	public List<INode> listNodeVO(List<Dept> list) {
 		List<INode> collect = list.stream().map(dept -> {
 			DeptVO deptVO = BeanUtil.copy(dept, DeptVO.class);
@@ -67,4 +67,12 @@ public class DeptWrapper extends BaseEntityWrapper<Dept, DeptVO> {
 		return ForestNodeMerger.merge(collect);
 	}
 
+	public List<INode> listNodeLazyVO(List<DeptLazyVO> list) {
+		List<INode> collect = list.stream().peek(dept -> {
+			String category = DictCache.getValue("org_category", dept.getDeptCategory());
+			Objects.requireNonNull(dept).setDeptCategoryName(category);
+		}).collect(Collectors.toList());
+		return ForestNodeMerger.merge(collect);
+	}
+
 }