码迷,mamicode.com
首页 > 其他好文 > 详细

最简单的组织树

时间:2021-01-02 11:43:00      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:jdk   lis   style   ring   ever   cto   ide   factory   部门   

同步部门例子代码:
/**保存部门层级名称**/
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);
}
}
}
}
}

最简单的组织树

标签:jdk   lis   style   ring   ever   cto   ide   factory   部门   

原文地址:https://www.cnblogs.com/zxy-come-on/p/14209183.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!