Browse Source

增加菜单懒加载、优化部门懒加载

test
smallchill 5 years ago
parent
commit
3778130e6b
  1. 15
      blade-service-api/blade-system-api/src/main/java/org/springblade/system/vo/MenuVO.java
  2. 4
      blade-service/blade-system/src/main/java/org/springblade/system/controller/DeptController.java
  3. 40
      blade-service/blade-system/src/main/java/org/springblade/system/controller/MenuController.java
  4. 39
      blade-service/blade-system/src/main/java/org/springblade/system/mapper/DeptMapper.xml
  5. 10
      blade-service/blade-system/src/main/java/org/springblade/system/mapper/MenuMapper.java
  6. 45
      blade-service/blade-system/src/main/java/org/springblade/system/mapper/MenuMapper.xml
  7. 10
      blade-service/blade-system/src/main/java/org/springblade/system/service/IMenuService.java
  8. 4
      blade-service/blade-system/src/main/java/org/springblade/system/service/impl/DeptServiceImpl.java
  9. 8
      blade-service/blade-system/src/main/java/org/springblade/system/service/impl/MenuServiceImpl.java
  10. 4
      blade-service/blade-system/src/main/java/org/springblade/system/wrapper/MenuWrapper.java

15
blade-service-api/blade-system-api/src/main/java/org/springblade/system/vo/MenuVO.java

@ -57,6 +57,12 @@ public class MenuVO extends Menu implements INode {
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private List<INode> children;
/**
* 是否有子孙节点
*/
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private Boolean hasChildren;
@Override
public List<INode> getChildren() {
if (this.children == null) {
@ -65,6 +71,15 @@ public class MenuVO extends Menu implements INode {
return this.children;
}
@Override
public Boolean getHasChildren() {
if (children.size() > 0) {
return true;
} else {
return this.hasChildren;
}
}
/**
* 上级菜单
*/

4
blade-service/blade-system/src/main/java/org/springblade/system/controller/DeptController.java

@ -94,7 +94,7 @@ public class DeptController extends BladeController {
})
@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) {
public R<List<INode>> lazyList(@ApiIgnore @RequestParam Map<String, Object> dept, Long parentId, BladeUser bladeUser) {
List<DeptVO> list = deptService.lazyList(bladeUser.getTenantId(), parentId, dept);
return R.data(DeptWrapper.build().listNodeLazyVO(list));
}
@ -118,7 +118,7 @@ public class DeptController extends BladeController {
@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) {
public R<List<DeptVO>> lazyTree(String tenantId, Long parentId, BladeUser bladeUser) {
List<DeptVO> tree = deptService.lazyTree(Func.toStrWithEmpty(tenantId, bladeUser.getTenantId()), parentId);
return R.data(tree);
}

40
blade-service/blade-system/src/main/java/org/springblade/system/controller/MenuController.java

@ -88,6 +88,22 @@ public class MenuController extends BladeController {
return R.data(MenuWrapper.build().listNodeVO(list));
}
/**
* 懒加载列表
*/
@GetMapping("/lazy-list")
@ApiImplicitParams({
@ApiImplicitParam(name = "code", value = "菜单编号", paramType = "query", dataType = "string"),
@ApiImplicitParam(name = "name", value = "菜单名称", paramType = "query", dataType = "string")
})
@PreAuth(RoleConstant.HAS_ROLE_ADMINISTRATOR)
@ApiOperationSupport(order = 3)
@ApiOperation(value = "列表", notes = "传入menu")
public R<List<MenuVO>> lazyList(Long parentId, @ApiIgnore @RequestParam Map<String, Object> menu) {
List<MenuVO> list = menuService.lazyList(parentId, menu);
return R.data(MenuWrapper.build().listNodeLazyVO(list));
}
/**
* 列表
*/
@ -97,7 +113,7 @@ public class MenuController extends BladeController {
@ApiImplicitParam(name = "name", value = "菜单名称", paramType = "query", dataType = "string")
})
@PreAuth(RoleConstant.HAS_ROLE_ADMINISTRATOR)
@ApiOperationSupport(order = 2)
@ApiOperationSupport(order = 4)
@ApiOperation(value = "列表", notes = "传入menu")
public R<List<MenuVO>> menuList(@ApiIgnore @RequestParam Map<String, Object> menu) {
List<Menu> list = menuService.list(Condition.getQueryWrapper(menu, Menu.class).lambda().eq(Menu::getAlias, "menu").orderByAsc(Menu::getSort));
@ -110,7 +126,7 @@ public class MenuController extends BladeController {
@PostMapping("/submit")
@CacheEvict(cacheNames = {MENU_CACHE})
@PreAuth(RoleConstant.HAS_ROLE_ADMINISTRATOR)
@ApiOperationSupport(order = 3)
@ApiOperationSupport(order = 5)
@ApiOperation(value = "新增或修改", notes = "传入menu")
public R submit(@Valid @RequestBody Menu menu) {
return R.status(menuService.submit(menu));
@ -123,7 +139,7 @@ public class MenuController extends BladeController {
@PostMapping("/remove")
@CacheEvict(cacheNames = {MENU_CACHE})
@PreAuth(RoleConstant.HAS_ROLE_ADMINISTRATOR)
@ApiOperationSupport(order = 4)
@ApiOperationSupport(order = 6)
@ApiOperation(value = "删除", notes = "传入ids")
public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
return R.status(menuService.removeMenu(ids));
@ -133,7 +149,7 @@ public class MenuController extends BladeController {
* 前端菜单数据
*/
@GetMapping("/routes")
@ApiOperationSupport(order = 5)
@ApiOperationSupport(order = 7)
@ApiOperation(value = "前端菜单数据", notes = "前端菜单数据")
public R<List<MenuVO>> routes(BladeUser user, Long topMenuId) {
List<MenuVO> list = menuService.routes((user == null) ? null : user.getRoleId(), topMenuId);
@ -144,7 +160,7 @@ public class MenuController extends BladeController {
* 前端按钮数据
*/
@GetMapping("/buttons")
@ApiOperationSupport(order = 6)
@ApiOperationSupport(order = 8)
@ApiOperation(value = "前端按钮数据", notes = "前端按钮数据")
public R<List<MenuVO>> buttons(BladeUser user) {
List<MenuVO> list = menuService.buttons(user.getRoleId());
@ -155,7 +171,7 @@ public class MenuController extends BladeController {
* 获取菜单树形结构
*/
@GetMapping("/tree")
@ApiOperationSupport(order = 7)
@ApiOperationSupport(order = 9)
@ApiOperation(value = "树形结构", notes = "树形结构")
public R<List<MenuVO>> tree() {
List<MenuVO> tree = menuService.tree();
@ -166,7 +182,7 @@ public class MenuController extends BladeController {
* 获取权限分配树形结构
*/
@GetMapping("/grant-tree")
@ApiOperationSupport(order = 8)
@ApiOperationSupport(order = 10)
@ApiOperation(value = "权限分配树形结构", notes = "权限分配树形结构")
public R<GrantTreeVO> grantTree(BladeUser user) {
GrantTreeVO vo = new GrantTreeVO();
@ -180,7 +196,7 @@ public class MenuController extends BladeController {
* 获取权限分配树形结构
*/
@GetMapping("/role-tree-keys")
@ApiOperationSupport(order = 9)
@ApiOperationSupport(order = 11)
@ApiOperation(value = "角色所分配的树", notes = "角色所分配的树")
public R<CheckedTreeVO> roleTreeKeys(String roleIds) {
CheckedTreeVO vo = new CheckedTreeVO();
@ -194,7 +210,7 @@ public class MenuController extends BladeController {
* 获取顶部菜单树形结构
*/
@GetMapping("/grant-top-tree")
@ApiOperationSupport(order = 10)
@ApiOperationSupport(order = 12)
@ApiOperation(value = "顶部菜单树形结构", notes = "顶部菜单树形结构")
public R<GrantTreeVO> grantTopTree(BladeUser user) {
GrantTreeVO vo = new GrantTreeVO();
@ -206,7 +222,7 @@ public class MenuController extends BladeController {
* 获取顶部菜单树形结构
*/
@GetMapping("/top-tree-keys")
@ApiOperationSupport(order = 11)
@ApiOperationSupport(order = 13)
@ApiOperation(value = "顶部菜单所分配的树", notes = "顶部菜单所分配的树")
public R<CheckedTreeVO> topTreeKeys(String topMenuIds) {
CheckedTreeVO vo = new CheckedTreeVO();
@ -218,7 +234,7 @@ public class MenuController extends BladeController {
* 顶部菜单数据
*/
@GetMapping("/top-menu")
@ApiOperationSupport(order = 12)
@ApiOperationSupport(order = 14)
@ApiOperation(value = "顶部菜单数据", notes = "顶部菜单数据")
public R<List<TopMenu>> topMenu(BladeUser user) {
if (Func.isEmpty(user)) {
@ -232,7 +248,7 @@ public class MenuController extends BladeController {
* 获取配置的角色权限
*/
@GetMapping("auth-routes")
@ApiOperationSupport(order = 13)
@ApiOperationSupport(order = 15)
@ApiOperation(value = "菜单的角色权限")
public R<List<Kv>> authRoutes(BladeUser user) {
if (Func.isEmpty(user)) {

39
blade-service/blade-system/src/main/java/org/springblade/system/mapper/DeptMapper.xml

@ -15,7 +15,7 @@
<result column="is_deleted" property="isDeleted"/>
</resultMap>
<resultMap id="deptLazyVOResultMap" type="org.springblade.system.vo.DeptVO">
<resultMap id="deptVOResultMap" type="org.springblade.system.vo.DeptVO">
<id column="id" property="id"/>
<result column="parent_id" property="parentId"/>
<result column="dept_name" property="deptName"/>
@ -43,7 +43,7 @@
id, parent_id, dept_name, full_name, sort, remark, is_deleted
</sql>
<select id="lazyList" resultMap="deptLazyVOResultMap">
<select id="lazyList" resultMap="deptVOResultMap">
SELECT
dept.* ,
(
@ -52,24 +52,23 @@
FROM
blade_dept
WHERE
parent_id = dept.id
parent_id = dept.id and is_deleted = 0
) 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>
WHERE dept.is_deleted = 0
<if test="param1!=null and param1!=''">
and dept.tenant_id = #{param1}
</if>
<if test="param2!=null">
and dept.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>
</select>
<select id="tree" resultMap="treeNodeResultMap">
@ -92,14 +91,14 @@
FROM
blade_dept
WHERE
parent_id = dept.id
parent_id = dept.id and is_deleted = 0
) AS "has_children"
FROM
blade_dept dept
WHERE
parent_id = #{param2}
dept.parent_id = #{param2} AND dept.is_deleted = 0
<if test="param1!=null and param1!=''">
and tenant_id = #{param1}
and dept.tenant_id = #{param1}
</if>
</select>

10
blade-service/blade-system/src/main/java/org/springblade/system/mapper/MenuMapper.java

@ -17,12 +17,12 @@
package org.springblade.system.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.system.dto.MenuDTO;
import org.springblade.system.entity.Menu;
import org.springblade.system.vo.MenuVO;
import java.util.List;
import java.util.Map;
/**
@ -33,13 +33,13 @@ import java.util.List;
public interface MenuMapper extends BaseMapper<Menu> {
/**
* 自定义分页
* 懒加载部门列表
*
* @param page
* @param menu
* @param parentId
* @param param
* @return
*/
List<MenuVO> selectMenuPage(IPage page, MenuVO menu);
List<MenuVO> lazyList(Long parentId, Map<String, Object> param);
/**
* 树形结构

45
blade-service/blade-system/src/main/java/org/springblade/system/mapper/MenuMapper.xml

@ -19,6 +19,23 @@
<result column="is_deleted" property="isDeleted"/>
</resultMap>
<resultMap id="menuVOResultMap" type="org.springblade.system.vo.MenuVO">
<id column="id" property="id"/>
<result column="code" property="code"/>
<result column="parent_id" property="parentId"/>
<result column="name" property="name"/>
<result column="alias" property="alias"/>
<result column="path" property="path"/>
<result column="source" property="source"/>
<result column="sort" property="sort"/>
<result column="category" property="category"/>
<result column="action" property="action"/>
<result column="is_open" property="isOpen"/>
<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"/>
@ -27,8 +44,32 @@
<result column="key" property="key"/>
</resultMap>
<select id="selectMenuPage" resultMap="menuResultMap">
select * from blade_menu where is_deleted = 0
<select id="lazyList" resultMap="menuVOResultMap">
SELECT
menu.*,
(
SELECT
CASE WHEN count( 1 ) > 0 THEN 1 ELSE 0 END
FROM
blade_menu
WHERE
parent_id = menu.id and is_deleted = 0
) AS "has_children"
FROM
blade_menu menu
WHERE menu.is_deleted = 0
<if test="param1!=null">
and menu.parent_id = #{param1}
</if>
<if test="param.name!=null and param.name!=''">
and menu.name like concat('%', #{param.name},'%')
</if>
<if test="param.code!=null and param.code!=''">
and menu.code like concat('%', #{param.code},'%')
</if>
<if test="param.alias!=null and param.alias!=''">
and menu.alias like concat('%', #{param.alias},'%')
</if>
</select>
<select id="tree" resultMap="treeNodeResultMap">

10
blade-service/blade-system/src/main/java/org/springblade/system/service/IMenuService.java

@ -16,7 +16,6 @@
*/
package org.springblade.system.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.support.Kv;
@ -24,6 +23,7 @@ import org.springblade.system.entity.Menu;
import org.springblade.system.vo.MenuVO;
import java.util.List;
import java.util.Map;
/**
* 服务类
@ -33,13 +33,13 @@ import java.util.List;
public interface IMenuService extends IService<Menu> {
/**
* 自定义分页
* 懒加载部门列表
*
* @param page
* @param menu
* @param parentId
* @param param
* @return
*/
IPage<MenuVO> selectMenuPage(IPage<MenuVO> page, MenuVO menu);
List<MenuVO> lazyList(Long parentId, Map<String, Object> param);
/**
* 菜单树形结构

4
blade-service/blade-system/src/main/java/org/springblade/system/service/impl/DeptServiceImpl.java

@ -29,7 +29,6 @@ import org.springblade.system.entity.Dept;
import org.springblade.system.mapper.DeptMapper;
import org.springblade.system.service.IDeptService;
import org.springblade.system.vo.DeptVO;
import org.springblade.system.vo.DeptVO;
import org.springframework.stereotype.Service;
import java.util.List;
@ -52,6 +51,9 @@ public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements ID
if (Func.isNotEmpty(paramTenantId) && AuthUtil.isAdministrator()) {
tenantId = paramTenantId;
}
if (Func.isEmpty(param.get("parentId"))) {
parentId = null;
}
return baseMapper.lazyList(tenantId, parentId, param);
}

8
blade-service/blade-system/src/main/java/org/springblade/system/service/impl/MenuServiceImpl.java

@ -16,7 +16,6 @@
*/
package org.springblade.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;
@ -64,8 +63,11 @@ public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IM
private ITopMenuSettingService topMenuSettingService;
@Override
public IPage<MenuVO> selectMenuPage(IPage<MenuVO> page, MenuVO menu) {
return page.setRecords(baseMapper.selectMenuPage(page, menu));
public List<MenuVO> lazyList(Long parentId, Map<String, Object> param) {
if (Func.isEmpty(Func.toStr(param.get("parentId")))) {
parentId = null;
}
return baseMapper.lazyList(parentId, param);
}
@Override

4
blade-service/blade-system/src/main/java/org/springblade/system/wrapper/MenuWrapper.java

@ -64,4 +64,8 @@ public class MenuWrapper extends BaseEntityWrapper<Menu, MenuVO> {
return ForestNodeMerger.merge(collect);
}
public List<MenuVO> listNodeLazyVO(List<MenuVO> list) {
return ForestNodeMerger.merge(list);
}
}

Loading…
Cancel
Save