最简单的组织树

同步部门例子代码:
/**保存部门层级名称**/
public void saveDeptPathName(List<Department> departmentLists){
if(!StringUtils.isEmpty(departmentLists)&&departmentLists.size()>0){
for (Department depts:departmentLists) {
ArrayList<String> pathIds =new ArrayList<>();
ArrayList<String> pathNames =new ArrayList<>();
getDeptNames(departmentLists,depts.getDingtalkId(),pathIds,pathNames);
Collections.reverse(pathNames);
String pathName = org.apache.commons.lang3.StringUtils.join(pathNames, "-");
Collections.reverse(pathIds);
String pathids = org.apache.commons.lang3.StringUtils.join(pathIds, ",");
jpaQueryFactory.update(DEPARTMENT)
.set(DEPARTMENT.pathName,pathName)
.set(DEPARTMENT.path,pathids)
.where(DEPARTMENT.id.eq(depts.getId()))
.execute();
}
}
}
/**获取部门名称**/
public void getDeptNames(List<Department> departmentLists,Long dingtalkId,ArrayList<String> pathIds,ArrayList<String> pathNames){
Department department = departmentLists.stream().filter(x -> x.getDingtalkId().longValue() == dingtalkId.longValue())
.collect(Collectors.toList()).get(0);
if(!StringUtils.isEmpty(department)){
pathIds.add(department.getId() + "");
pathNames.add(department.getName());
if(!StringUtils.isEmpty(department.getDingtalkParentid())){
getDeptNames(departmentLists, department.getDingtalkParentid(),pathIds,pathNames);
}
}
}

/**保存部门父级id**/
public void saveDeptPid(List<Department> departmentLists){
if(!StringUtils.isEmpty(departmentLists)&&departmentLists.size()>0){
for (Department dep:departmentLists) {
long pid = 0;
if (!StringUtils.isEmpty(dep.getDingtalkParentid())) {
Department department = departmentLists.stream()
.filter(x -> x.getDingtalkId().longValue() == dep.getDingtalkParentid().longValue())
.collect(Collectors.toList()).get(0);
if (!StringUtils.isEmpty(department)) {
pid = department.getId();
}
}
jpaQueryFactory.update(DEPARTMENT)
.set(DEPARTMENT.parentId,pid)
.where(DEPARTMENT.id.eq(dep.getId()))
.execute();
}
}

}


部门树代码:
/***
* 来源分类树列表
* @return
*/
@Override
public List<SourceTypeTreeDto> list() {
List<SourceTypeTreeDto> all = this.all();
List<SourceTypeTreeDto> sourceTypeTreeDtoList = new ArrayList<>();
this.digui(0l, all, sourceTypeTreeDtoList);
return sourceTypeTreeDtoList;
}

private List<SourceTypeTreeDto> all() {
return this.jpaQueryFactory.selectFrom(Q_SOURCE_TYPE).
where(
Q_SOURCE_TYPE.corpId.eq(EmployeeContext.get().getCorpId()),
Q_SOURCE_TYPE.state.eq(SourceTypeState.NORMAL)
).fetch().
stream().map(res -> BeanPlusUtils.copyPropertiesPlus(res, new SourceTypeTreeDto())).collect(Collectors.toList());
}

private void digui(Long parentId, List<SourceTypeTreeDto> all, List<SourceTypeTreeDto> sourceTypeTreeDtoList) {
/**jdk1.8**/
List<SourceTypeTreeDto> child = all.stream().filter(x -> x.getParentId().longValue() == parentId.longValue()).collect(Collectors.toList());
if (child.size() > 0) {
if (parentId == 0l) {
for (SourceTypeTreeDto dto : child) {
dto.setChildren(new ArrayList<>());
sourceTypeTreeDtoList.add(dto);
this.digui(dto.getId(), all, sourceTypeTreeDtoList);
}
} else {
SourceTypeTreeDto parent = all.stream().filter(x -> x.getId().longValue() == parentId.longValue()).collect(Collectors.toList()).get(0);
parent.setChildren(child);
for (SourceTypeTreeDto c : child) {
c.setChildren(new ArrayList<>());
this.digui(c.getId(), all, sourceTypeTreeDtoList);
}
}
}
}
}
上一篇:Leetcode No.184 部门工资最高的员工


下一篇:为什么数据库能查出两条id相同的数据