SysOrgServiceImpl.java
2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.youjia.system.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.spring.service.impl.ServiceImpl;
import com.youjia.common.constant.CommonConstants;
import com.youjia.system.entity.SysOrg;
import com.youjia.system.mapper.SysOrgMapper;
import com.youjia.system.service.SysOrgService;
import com.youjia.system.vo.TreeNodeVO;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* 组织服务实现。
*/
@Service
public class SysOrgServiceImpl extends ServiceImpl<SysOrgMapper, SysOrg> implements SysOrgService {
@Override
public List<TreeNodeVO> listOrgTree() {
List<SysOrg> orgs = this.list(new LambdaQueryWrapper<SysOrg>()
.eq(SysOrg::getStatus, CommonConstants.STATUS_ENABLED)
.orderByAsc(SysOrg::getSort)
.orderByAsc(SysOrg::getOrgId));
Map<Long, List<SysOrg>> childrenMap = orgs.stream()
.collect(Collectors.groupingBy(o -> o.getParentId() == null ? 0L : o.getParentId()));
return buildChildren(0L, childrenMap);
}
@Override
public List<Long> listSelfAndChildOrgIds(Long orgId) {
if (orgId == null) {
return new ArrayList<>();
}
SysOrg org = getById(orgId);
if (org == null) {
return new ArrayList<>();
}
List<Long> result = new ArrayList<>();
result.add(orgId);
if (StringUtils.hasText(org.getPath())) {
this.list(new LambdaQueryWrapper<SysOrg>().likeRight(SysOrg::getPath, org.getPath()))
.forEach(child -> result.add(child.getOrgId()));
}
return result.stream().distinct().collect(Collectors.toList());
}
private List<TreeNodeVO> buildChildren(Long parentId, Map<Long, List<SysOrg>> childrenMap) {
List<SysOrg> children = childrenMap.get(parentId);
if (children == null || children.isEmpty()) {
return new ArrayList<>();
}
return children.stream()
.sorted(Comparator.comparing(SysOrg::getSort, Comparator.nullsLast(Integer::compareTo)))
.map(org -> {
TreeNodeVO node = new TreeNodeVO();
node.setId(org.getOrgId());
node.setParentId(org.getParentId());
node.setLabel(org.getOrgName());
node.setSort(org.getSort());
node.setStatus(org.getStatus());
node.setChildren(buildChildren(org.getOrgId(), childrenMap));
return node;
})
.collect(Collectors.toList());
}
}