Skip to content

Commit 79ff209

Browse files
🚔 获取所有部门的列表和树形列表
1 parent 23b5d46 commit 79ff209

File tree

8 files changed

+230
-9
lines changed

8 files changed

+230
-9
lines changed

docs/db/mysql_spring_boot_plus.sql

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,17 @@ CREATE TABLE `sys_department`
118118
-- ----------------------------
119119
-- Records of sys_department
120120
-- ----------------------------
121-
INSERT INTO sys_department (id, name, parent_id, state, sort, remark, version, create_time, update_time) VALUES (1, '管理部', null, 1, 0, null, 0, '2019-10-25 09:46:49', null);
122-
INSERT INTO sys_department (id, name, parent_id, state, sort, remark, version, create_time, update_time) VALUES (2, '测试部', null, 1, 0, null, 0, '2019-10-25 09:47:06', null);
121+
INSERT INTO sys_department (id, name, parent_id, level, state, sort, remark, version, create_time, update_time) VALUES (1, '管理部', null, 1, 1, 0, null, 0, '2019-10-25 09:46:49', null);
122+
INSERT INTO sys_department (id, name, parent_id, level, state, sort, remark, version, create_time, update_time) VALUES (2, '技术部', null, 1, 1, 0, null, 0, '2019-11-01 20:45:43', null);
123+
INSERT INTO sys_department (id, name, parent_id, level, state, sort, remark, version, create_time, update_time) VALUES (20, '前端开发部', 2, 2, 1, 0, null, 0, '2019-11-01 20:48:38', null);
124+
INSERT INTO sys_department (id, name, parent_id, level, state, sort, remark, version, create_time, update_time) VALUES (21, '后台开发部', 2, 2, 1, 0, null, 0, '2019-11-01 20:48:38', null);
125+
INSERT INTO sys_department (id, name, parent_id, level, state, sort, remark, version, create_time, update_time) VALUES (22, '测试部', 2, 2, 1, 0, null, 0, '2019-11-01 20:48:38', null);
126+
INSERT INTO sys_department (id, name, parent_id, level, state, sort, remark, version, create_time, update_time) VALUES (201, '前端一组', 20, 3, 1, 0, null, 0, '2019-11-01 20:48:38', null);
127+
INSERT INTO sys_department (id, name, parent_id, level, state, sort, remark, version, create_time, update_time) VALUES (202, '前端二组', 20, 3, 1, 0, null, 0, '2019-11-01 20:48:38', null);
128+
INSERT INTO sys_department (id, name, parent_id, level, state, sort, remark, version, create_time, update_time) VALUES (203, '后台一组', 21, 3, 1, 0, null, 0, '2019-11-01 20:48:38', null);
129+
INSERT INTO sys_department (id, name, parent_id, level, state, sort, remark, version, create_time, update_time) VALUES (204, '后台二组', 21, 3, 1, 0, null, 0, '2019-11-01 20:48:38', null);
130+
INSERT INTO sys_department (id, name, parent_id, level, state, sort, remark, version, create_time, update_time) VALUES (205, '测试一组', 22, 3, 1, 0, null, 0, '2019-11-01 20:48:38', null);
131+
123132

124133
-- ----------------------------
125134
-- Table structure for sys_user

src/main/java/io/geekidea/springbootplus/system/controller/SysDepartmentController.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@
2323
import io.geekidea.springbootplus.system.param.SysDepartmentQueryParam;
2424
import io.geekidea.springbootplus.system.service.SysDepartmentService;
2525
import io.geekidea.springbootplus.system.vo.SysDepartmentQueryVo;
26+
import io.geekidea.springbootplus.system.vo.SysDepartmentTreeVo;
2627
import io.swagger.annotations.Api;
2728
import io.swagger.annotations.ApiOperation;
2829
import lombok.extern.slf4j.Slf4j;
2930
import org.apache.shiro.authz.annotation.RequiresPermissions;
30-
import org.apache.shiro.authz.annotation.RequiresRoles;
3131
import org.springframework.beans.factory.annotation.Autowired;
3232
import org.springframework.web.bind.annotation.*;
3333

3434
import javax.validation.Valid;
35+
import java.util.List;
3536

3637
/**
3738
* <pre>
@@ -105,5 +106,27 @@ public ApiResult<Paging<SysDepartmentQueryVo>> getSysDepartmentPageList(@Valid @
105106
return ApiResult.ok(paging);
106107
}
107108

109+
/**
110+
* 获取所有部门列表
111+
*/
112+
@PostMapping("/getAllDepartmentList")
113+
// @RequiresPermissions("sys:department:all:list")
114+
@ApiOperation(value = "获取所有部门的树形列表", notes = "获取所有部门的树形列表", response = SysDepartment.class)
115+
public ApiResult<Paging<SysDepartment>> getAllDepartmentList() throws Exception {
116+
List<SysDepartment> list = sysDepartmentService.getAllDepartmentList();
117+
return ApiResult.ok(list);
118+
}
119+
120+
/**
121+
* 获取所有部门的树形列表
122+
*/
123+
@PostMapping("/getAllDepartmentTree")
124+
// @RequiresPermissions("sys:department:all:tree")
125+
@ApiOperation(value = "获取所有部门的树形列表", notes = "获取所有部门的树形列表", response = SysDepartmentTreeVo.class)
126+
public ApiResult<Paging<SysDepartmentTreeVo>> getAllDepartmentTree() throws Exception {
127+
List<SysDepartmentTreeVo> treeVos = sysDepartmentService.getAllDepartmentTree();
128+
return ApiResult.ok(treeVos);
129+
}
130+
108131
}
109132

src/main/java/io/geekidea/springbootplus/system/controller/SysPermissionController.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ public ApiResult<SysPermission> getAllMenuList() throws Exception {
124124
@RequiresPermissions("sys:permission:all:menu:tree")
125125
@ApiOperation(value = "获取所有菜单列表", notes = "获取所有菜单列表", response = SysPermissionTreeVo.class)
126126
public ApiResult<SysPermissionTreeVo> getAllMenuTree() throws Exception {
127-
List<SysPermissionTreeVo> list = sysPermissionService.getAllMenuTree();
128-
return ApiResult.ok(list);
127+
List<SysPermissionTreeVo> treeVos = sysPermissionService.getAllMenuTree();
128+
return ApiResult.ok(treeVos);
129129
}
130130

131131

@@ -147,8 +147,8 @@ public ApiResult<SysPermission> getMenuListByUserId(@PathVariable("userId") Long
147147
@RequiresPermissions("sys:permission:menu:tree")
148148
@ApiOperation(value = "根据用户id获取菜单树形列表", notes = "根据用户id获取菜单树形列表", response = SysPermissionTreeVo.class)
149149
public ApiResult<SysPermissionTreeVo> getMenuTreeByUserId(@PathVariable("userId") Long userId) throws Exception {
150-
List<SysPermissionTreeVo> list = sysPermissionService.getMenuTreeByUserId(userId);
151-
return ApiResult.ok(list);
150+
List<SysPermissionTreeVo> treeVos = sysPermissionService.getMenuTreeByUserId(userId);
151+
return ApiResult.ok(treeVos);
152152
}
153153

154154
/**
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2019-2029 geekidea(https://github.com/geekidea)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.geekidea.springbootplus.system.convert;
18+
19+
import io.geekidea.springbootplus.system.entity.SysDepartment;
20+
import io.geekidea.springbootplus.system.vo.SysDepartmentTreeVo;
21+
import org.mapstruct.Mapper;
22+
import org.mapstruct.factory.Mappers;
23+
24+
import java.util.List;
25+
26+
/**
27+
* 部门对象转换器
28+
*
29+
* @author geekidea
30+
* @date 2019-11-01
31+
**/
32+
@Mapper
33+
public interface SysDepartmentConvert {
34+
35+
SysDepartmentConvert INSTANCE = Mappers.getMapper(SysDepartmentConvert.class);
36+
37+
/**
38+
* SysDepartment转换成SysDepartmentTreeVo对象
39+
*
40+
* @param sysDepartment
41+
* @return
42+
*/
43+
SysDepartmentTreeVo entityToTreeVo(SysDepartment sysDepartment);
44+
45+
/**
46+
* SysDepartment列表转换成SysDepartmentTreeVo列表
47+
*
48+
* @param list
49+
* @return
50+
*/
51+
List<SysDepartmentTreeVo> listToTreeVoList(List<SysDepartment> list);
52+
53+
}

src/main/java/io/geekidea/springbootplus/system/service/SysDepartmentService.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616

1717
package io.geekidea.springbootplus.system.service;
1818

19-
import io.geekidea.springbootplus.system.entity.SysDepartment;
2019
import io.geekidea.springbootplus.common.service.BaseService;
20+
import io.geekidea.springbootplus.common.vo.Paging;
21+
import io.geekidea.springbootplus.system.entity.SysDepartment;
2122
import io.geekidea.springbootplus.system.param.SysDepartmentQueryParam;
2223
import io.geekidea.springbootplus.system.vo.SysDepartmentQueryVo;
23-
import io.geekidea.springbootplus.common.vo.Paging;
24+
import io.geekidea.springbootplus.system.vo.SysDepartmentTreeVo;
2425

2526
import java.io.Serializable;
27+
import java.util.List;
2628

2729
/**
2830
* <pre>
@@ -88,4 +90,16 @@ public interface SysDepartmentService extends BaseService<SysDepartment> {
8890
*/
8991
boolean isEnableSysDepartment(Long id) throws Exception;
9092

93+
/**
94+
* 获取所有可用的部门列表
95+
* @return
96+
*/
97+
List<SysDepartment> getAllDepartmentList();
98+
99+
/**
100+
* 获取所有可用的部门树形列表
101+
* @return
102+
*/
103+
List<SysDepartmentTreeVo> getAllDepartmentTree();
104+
91105
}

src/main/java/io/geekidea/springbootplus/system/service/impl/SysDepartmentServiceImpl.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,22 @@
2323
import io.geekidea.springbootplus.common.service.impl.BaseServiceImpl;
2424
import io.geekidea.springbootplus.common.vo.Paging;
2525
import io.geekidea.springbootplus.enums.StateEnum;
26+
import io.geekidea.springbootplus.system.convert.SysDepartmentConvert;
2627
import io.geekidea.springbootplus.system.entity.SysDepartment;
2728
import io.geekidea.springbootplus.system.mapper.SysDepartmentMapper;
2829
import io.geekidea.springbootplus.system.param.SysDepartmentQueryParam;
2930
import io.geekidea.springbootplus.system.service.SysDepartmentService;
3031
import io.geekidea.springbootplus.system.vo.SysDepartmentQueryVo;
32+
import io.geekidea.springbootplus.system.vo.SysDepartmentTreeVo;
3133
import lombok.extern.slf4j.Slf4j;
34+
import org.apache.commons.collections.CollectionUtils;
3235
import org.springframework.beans.factory.annotation.Autowired;
3336
import org.springframework.stereotype.Service;
3437
import org.springframework.transaction.annotation.Transactional;
3538

3639
import java.io.Serializable;
40+
import java.util.ArrayList;
41+
import java.util.List;
3742

3843

3944
/**
@@ -91,4 +96,47 @@ public boolean isEnableSysDepartment(Long id) throws Exception {
9196
return count > 0;
9297
}
9398

99+
@Override
100+
public List<SysDepartment> getAllDepartmentList() {
101+
SysDepartment sysDepartment = new SysDepartment().setState(StateEnum.ENABLE.ordinal());
102+
// 获取所有已启用的部门列表
103+
return sysDepartmentMapper.selectList(new QueryWrapper(sysDepartment));
104+
}
105+
106+
@Override
107+
public List<SysDepartmentTreeVo> getAllDepartmentTree() {
108+
List<SysDepartment> sysDepartmentList = getAllDepartmentList();
109+
if (CollectionUtils.isEmpty(sysDepartmentList)) {
110+
throw new IllegalArgumentException("SysDepartment列表不能为空");
111+
}
112+
List<SysDepartmentTreeVo> list = SysDepartmentConvert.INSTANCE.listToTreeVoList(sysDepartmentList);
113+
List<SysDepartmentTreeVo> treeVos = new ArrayList<>();
114+
for (SysDepartmentTreeVo treeVo : list) {
115+
if (treeVo.getParentId() == null) {
116+
treeVos.add(findChildren(treeVo, list));
117+
}
118+
}
119+
return treeVos;
120+
}
121+
122+
/**
123+
* 递归获取树形结果列表
124+
*
125+
* @param tree
126+
* @param list
127+
* @return
128+
*/
129+
public SysDepartmentTreeVo findChildren(SysDepartmentTreeVo tree, List<SysDepartmentTreeVo> list) {
130+
for (SysDepartmentTreeVo vo : list) {
131+
if (tree.getId().equals(vo.getParentId())) {
132+
if (tree.getChildren() == null) {
133+
tree.setChildren(new ArrayList<>());
134+
}
135+
tree.getChildren().add(findChildren(vo, list));
136+
}
137+
}
138+
return tree;
139+
}
140+
141+
94142
}

src/main/java/io/geekidea/springbootplus/system/service/impl/SysPermissionServiceImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ public List<SysPermissionTreeVo> getAllMenuTree() throws Exception {
142142

143143
@Override
144144
public List<SysPermissionTreeVo> convertSysPermissionTreeVoList(List<SysPermission> list) {
145+
if (CollectionUtils.isEmpty(list)) {
146+
throw new IllegalArgumentException("SysPermission列表不能为空");
147+
}
145148
// 按level分组获取map
146149
Map<Integer, List<SysPermission>> map = list.stream().collect(Collectors.groupingBy(SysPermission::getLevel));
147150
List<SysPermissionTreeVo> treeVos = new ArrayList<>();
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2019-2029 geekidea(https://github.com/geekidea)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.geekidea.springbootplus.system.vo;
18+
19+
import io.swagger.annotations.ApiModel;
20+
import io.swagger.annotations.ApiModelProperty;
21+
import lombok.Data;
22+
import lombok.experimental.Accessors;
23+
24+
import java.io.Serializable;
25+
import java.util.Date;
26+
import java.util.List;
27+
28+
/**
29+
* <pre>
30+
* 部门TreeVo
31+
* </pre>
32+
*
33+
* @author geekidea
34+
* @since 2019-11-1
35+
*/
36+
@Data
37+
@Accessors(chain = true)
38+
@ApiModel(value = "SysDepartmentTreeVo对象", description = "部门")
39+
public class SysDepartmentTreeVo implements Serializable {
40+
private static final long serialVersionUID = -2250233632748939400L;
41+
42+
@ApiModelProperty(value = "主键")
43+
private Long id;
44+
45+
@ApiModelProperty(value = "部门名称")
46+
private String name;
47+
48+
@ApiModelProperty(value = "父id")
49+
private Long parentId;
50+
51+
@ApiModelProperty(value = "状态,0:禁用,1:启用")
52+
private Integer state;
53+
54+
@ApiModelProperty(value = "排序")
55+
private Integer sort;
56+
57+
@ApiModelProperty(value = "备注")
58+
private String remark;
59+
60+
@ApiModelProperty(value = "版本")
61+
private Integer version;
62+
63+
@ApiModelProperty(value = "创建时间")
64+
private Date createTime;
65+
66+
@ApiModelProperty(value = "修改时间")
67+
private Date updateTime;
68+
69+
private List<SysDepartmentTreeVo> children;
70+
71+
}

0 commit comments

Comments
 (0)