Skip to content

Commit d290385

Browse files
committed
feat: 添加部门管理 API
1 parent 348ca8c commit d290385

7 files changed

Lines changed: 351 additions & 0 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.github.cadecode.uniboot.framework.svc.bean.po;
2+
3+
import com.baomidou.mybatisplus.annotation.*;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Builder;
6+
import lombok.Data;
7+
import lombok.NoArgsConstructor;
8+
9+
import java.util.Date;
10+
11+
/**
12+
* 部门 PO
13+
*
14+
* @author Cade Li
15+
* @since 2023/11/24
16+
*/
17+
@Data
18+
@AllArgsConstructor
19+
@NoArgsConstructor
20+
@Builder
21+
@TableName(autoResultMap = true)
22+
public class SysDept {
23+
24+
@TableId(type = IdType.ASSIGN_ID)
25+
private Long id;
26+
27+
/**
28+
* 部门名称
29+
*/
30+
private String deptName;
31+
32+
/**
33+
* 父级 ID
34+
*/
35+
private Long parentId;
36+
37+
/**
38+
* 排序
39+
*/
40+
private Integer orderNum;
41+
42+
/**
43+
* leader 名
44+
*/
45+
private String leader;
46+
47+
/**
48+
* 邮箱
49+
*/
50+
private String mail;
51+
52+
/**
53+
* 电话
54+
*/
55+
private String phone;
56+
57+
@TableField(fill = FieldFill.INSERT)
58+
private Date createTime;
59+
60+
@TableField(fill = FieldFill.UPDATE)
61+
private Date updateTime;
62+
63+
@TableField(fill = FieldFill.INSERT_UPDATE)
64+
private String updateUser;
65+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.github.cadecode.uniboot.framework.svc.bean.vo;
2+
3+
import lombok.Data;
4+
5+
import javax.validation.constraints.NotEmpty;
6+
import javax.validation.constraints.NotNull;
7+
import java.util.Date;
8+
import java.util.List;
9+
10+
/**
11+
* 部门 VO
12+
*
13+
* @author Cade Li
14+
* @since 2023/11/24
15+
*/
16+
public class SysDeptVo {
17+
18+
@Data
19+
public static class SysDeptTreeResVo {
20+
private Long id;
21+
private String deptName;
22+
private Long parentId;
23+
private Integer orderNum;
24+
private String leader;
25+
private String mail;
26+
private String phone;
27+
private Date createTime;
28+
private Date updateTime;
29+
private String updateUser;
30+
private List<SysDeptTreeResVo> children;
31+
}
32+
33+
@Data
34+
public static class SysDeptQueryResVo {
35+
private Long id;
36+
private String deptName;
37+
private Long parentId;
38+
private Integer orderNum;
39+
private String leader;
40+
private String mail;
41+
private String phone;
42+
private Date createTime;
43+
private Date updateTime;
44+
private String updateUser;
45+
}
46+
47+
@Data
48+
public static class SysDeptUpdateReqVo {
49+
@NotNull
50+
private Long id;
51+
private String deptName;
52+
private Integer orderNum;
53+
private String leader;
54+
private String mail;
55+
private String phone;
56+
}
57+
58+
@Data
59+
public static class SysDeptAddReqVo {
60+
private Long parentId;
61+
@NotEmpty
62+
private String deptName;
63+
@NotNull
64+
private Integer orderNum;
65+
private String leader;
66+
private String mail;
67+
private String phone;
68+
}
69+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.github.cadecode.uniboot.framework.svc.controller;
2+
3+
import com.github.cadecode.uniboot.framework.base.annotation.ApiFormat;
4+
import com.github.cadecode.uniboot.framework.svc.bean.po.SysDept;
5+
import com.github.cadecode.uniboot.framework.svc.bean.vo.SysDeptVo.SysDeptAddReqVo;
6+
import com.github.cadecode.uniboot.framework.svc.bean.vo.SysDeptVo.SysDeptQueryResVo;
7+
import com.github.cadecode.uniboot.framework.svc.bean.vo.SysDeptVo.SysDeptTreeResVo;
8+
import com.github.cadecode.uniboot.framework.svc.bean.vo.SysDeptVo.SysDeptUpdateReqVo;
9+
import com.github.cadecode.uniboot.framework.svc.convert.SysDeptConvert;
10+
import com.github.cadecode.uniboot.framework.svc.service.SysDeptService;
11+
import io.swagger.annotations.Api;
12+
import io.swagger.annotations.ApiOperation;
13+
import lombok.RequiredArgsConstructor;
14+
import lombok.extern.slf4j.Slf4j;
15+
import org.springframework.transaction.annotation.Transactional;
16+
import org.springframework.validation.annotation.Validated;
17+
import org.springframework.web.bind.annotation.PostMapping;
18+
import org.springframework.web.bind.annotation.RequestBody;
19+
import org.springframework.web.bind.annotation.RequestMapping;
20+
import org.springframework.web.bind.annotation.RestController;
21+
22+
import javax.validation.Valid;
23+
import javax.validation.constraints.NotEmpty;
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
import java.util.stream.Collectors;
27+
28+
/**
29+
* 部门管理API
30+
*
31+
* @author Cade Li
32+
* @since 2023/11/24
33+
*/
34+
@ApiFormat
35+
@Slf4j
36+
@RequiredArgsConstructor
37+
@Api(tags = "部门管理")
38+
@RequestMapping("system/dept")
39+
@RestController
40+
@Validated
41+
public class SysDeptController {
42+
43+
private final SysDeptService sysDeptService;
44+
45+
@ApiOperation("查询部门列表(树状)")
46+
@PostMapping("list_tree_vo")
47+
public List<SysDeptTreeResVo> listTreeVo() {
48+
return sysDeptService.listTreeVo();
49+
}
50+
51+
@ApiOperation("更新部门")
52+
@PostMapping("update")
53+
public boolean update(@RequestBody @Valid SysDeptUpdateReqVo reqVo) {
54+
SysDept po = SysDeptConvert.INSTANCE.voToPo(reqVo);
55+
return sysDeptService.updateById(po);
56+
}
57+
58+
@ApiOperation("添加部门")
59+
@PostMapping("add")
60+
public boolean add(@RequestBody @Valid SysDeptAddReqVo reqVo) {
61+
SysDept po = SysDeptConvert.INSTANCE.voToPo(reqVo);
62+
return sysDeptService.save(po);
63+
}
64+
65+
@ApiOperation("删除部门(多选)")
66+
@PostMapping("delete")
67+
@Transactional(rollbackFor = Exception.class)
68+
public boolean delete(@RequestBody @NotEmpty List<Long> deptIdList) {
69+
List<Long> assumedParentList = new ArrayList<>(deptIdList);
70+
while (!assumedParentList.isEmpty()) {
71+
List<SysDept> children = sysDeptService.lambdaQuery()
72+
.select(SysDept::getId)
73+
.in(SysDept::getParentId, assumedParentList)
74+
.list();
75+
// 加入删除名单
76+
deptIdList.addAll(children.stream().map(SysDept::getId).collect(Collectors.toList()));
77+
// 再次查询
78+
assumedParentList = children.stream()
79+
.map(SysDept::getId)
80+
.collect(Collectors.toList());
81+
}
82+
return sysDeptService.removeBatchByIds(deptIdList);
83+
}
84+
85+
@ApiOperation("获取部门 byDeptIds")
86+
@PostMapping("list_query_vo_by_dept_ids")
87+
public List<SysDeptQueryResVo> listQueryVoByDeptIds(@RequestBody @NotEmpty List<Long> deptIdList) {
88+
List<SysDept> poList = sysDeptService.listByIds(deptIdList);
89+
return SysDeptConvert.INSTANCE.poToQueryResVo(poList);
90+
}
91+
92+
@ApiOperation("查询部门列表(搜索建议)")
93+
@PostMapping("list_parent_suggest")
94+
public List<SysDeptQueryResVo> listParentSuggest() {
95+
List<SysDept> poList = sysDeptService.lambdaQuery()
96+
.select(SysDept::getId, SysDept::getDeptName, SysDept::getOrderNum)
97+
.list();
98+
return SysDeptConvert.INSTANCE.poToQueryResVo(poList);
99+
}
100+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.github.cadecode.uniboot.framework.svc.convert;
2+
3+
import com.github.cadecode.uniboot.framework.svc.bean.po.SysDept;
4+
import com.github.cadecode.uniboot.framework.svc.bean.vo.SysDeptVo.SysDeptAddReqVo;
5+
import com.github.cadecode.uniboot.framework.svc.bean.vo.SysDeptVo.SysDeptQueryResVo;
6+
import com.github.cadecode.uniboot.framework.svc.bean.vo.SysDeptVo.SysDeptTreeResVo;
7+
import com.github.cadecode.uniboot.framework.svc.bean.vo.SysDeptVo.SysDeptUpdateReqVo;
8+
import org.mapstruct.Mapper;
9+
import org.mapstruct.Mapping;
10+
import org.mapstruct.factory.Mappers;
11+
12+
import java.util.List;
13+
14+
/**
15+
* 部门 bean convert
16+
*
17+
* @author Cade Li
18+
* @since 2023/11/24
19+
*/
20+
@Mapper
21+
public interface SysDeptConvert {
22+
23+
SysDeptConvert INSTANCE = Mappers.getMapper(SysDeptConvert.class);
24+
25+
@Mapping(target = "updateUser", ignore = true)
26+
@Mapping(target = "updateTime", ignore = true)
27+
@Mapping(target = "id", ignore = true)
28+
@Mapping(target = "createTime", ignore = true)
29+
SysDept voToPo(SysDeptAddReqVo reqVo);
30+
31+
@Mapping(target = "parentId", ignore = true)
32+
@Mapping(target = "updateUser", ignore = true)
33+
@Mapping(target = "updateTime", ignore = true)
34+
@Mapping(target = "createTime", ignore = true)
35+
SysDept voToPo(SysDeptUpdateReqVo reqVo);
36+
37+
List<SysDeptQueryResVo> poToQueryResVo(List<SysDept> poList);
38+
39+
List<SysDeptTreeResVo> poToTreeResVo(List<SysDept> poList);
40+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.github.cadecode.uniboot.framework.svc.mapper;
2+
3+
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4+
import com.github.cadecode.uniboot.framework.svc.bean.po.SysDept;
5+
import org.apache.ibatis.annotations.Mapper;
6+
7+
/**
8+
* 部门 DAO
9+
*
10+
* @author Cade Li
11+
* @since 2023/11/24
12+
*/
13+
@Mapper
14+
public interface SysDeptMapper extends BaseMapper<SysDept> {
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.github.cadecode.uniboot.framework.svc.service;
2+
3+
import com.baomidou.mybatisplus.extension.service.IService;
4+
import com.github.cadecode.uniboot.framework.svc.bean.po.SysDept;
5+
import com.github.cadecode.uniboot.framework.svc.bean.vo.SysDeptVo.SysDeptTreeResVo;
6+
7+
import java.util.List;
8+
9+
/**
10+
* 部门服务
11+
*
12+
* @author Cade Li
13+
* @since 2023/11/24
14+
*/
15+
public interface SysDeptService extends IService<SysDept> {
16+
17+
List<SysDeptTreeResVo> listTreeVo();
18+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.github.cadecode.uniboot.framework.svc.serviceimpl;
2+
3+
import cn.hutool.core.util.ObjUtil;
4+
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
5+
import com.github.cadecode.uniboot.framework.svc.bean.po.SysDept;
6+
import com.github.cadecode.uniboot.framework.svc.bean.vo.SysDeptVo.SysDeptTreeResVo;
7+
import com.github.cadecode.uniboot.framework.svc.convert.SysDeptConvert;
8+
import com.github.cadecode.uniboot.framework.svc.mapper.SysDeptMapper;
9+
import com.github.cadecode.uniboot.framework.svc.service.SysDeptService;
10+
import org.springframework.stereotype.Service;
11+
12+
import java.util.Comparator;
13+
import java.util.List;
14+
import java.util.stream.Collectors;
15+
16+
/**
17+
* 部门服务实现
18+
*
19+
* @author Cade Li
20+
* @since 2023/11/24
21+
*/
22+
@Service
23+
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements SysDeptService {
24+
@Override
25+
public List<SysDeptTreeResVo> listTreeVo() {
26+
List<SysDept> poList = list().stream()
27+
.sorted(Comparator.comparing(SysDept::getOrderNum))
28+
.collect(Collectors.toList());
29+
List<SysDeptTreeResVo> treeResVoList = SysDeptConvert.INSTANCE.poToTreeResVo(poList);
30+
return geneTreeVo(treeResVoList, null);
31+
}
32+
33+
private List<SysDeptTreeResVo> geneTreeVo(List<SysDeptTreeResVo> deptList, Long rootId) {
34+
List<SysDeptTreeResVo> parentList = deptList.stream().filter(o -> ObjUtil.equals(rootId, o.getParentId())).collect(Collectors.toList());
35+
parentList.forEach(p -> {
36+
List<SysDeptTreeResVo> children = deptList.stream()
37+
.filter(c -> ObjUtil.equals(c.getParentId(), p.getId()))
38+
.peek(c -> c.setChildren(geneTreeVo(deptList, c.getId())))
39+
.collect(Collectors.toList());
40+
p.setChildren(children);
41+
});
42+
return parentList;
43+
}
44+
}

0 commit comments

Comments
 (0)