mirror of
https://gitee.com/youlaitech/youlai-mall.git
synced 2025-01-03 17:42:20 +08:00
refactor: 部门递归优化
This commit is contained in:
parent
3cf42a7c4d
commit
43f08fdc67
@ -21,8 +21,7 @@ import java.util.List;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author haoxr
|
* @Author haoxr
|
||||||
* @Date 2021-02-25 15:36
|
* @Date 2021/02/25
|
||||||
* @Version 1.0.0
|
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableSwagger2WebMvc
|
@EnableSwagger2WebMvc
|
||||||
|
@ -7,6 +7,9 @@ import org.springframework.security.crypto.password.PasswordEncoder;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 密码编码器
|
* 密码编码器
|
||||||
|
*
|
||||||
|
* @author haoxr
|
||||||
|
* @date 2022/10/21
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
public class PasswordEncoderConfig {
|
public class PasswordEncoderConfig {
|
||||||
|
@ -61,8 +61,6 @@ public class SwaggerConfiguration {
|
|||||||
List<SecurityContext> securityContexts = Lists.newArrayList(securityContext);
|
List<SecurityContext> securityContexts = Lists.newArrayList(securityContext);
|
||||||
return new Docket(DocumentationType.SWAGGER_2)
|
return new Docket(DocumentationType.SWAGGER_2)
|
||||||
.select()
|
.select()
|
||||||
// .apis(RequestHandlerSelectors.basePackage("com.youlai.admin.controller"))
|
|
||||||
// .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
|
|
||||||
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
|
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
|
||||||
.paths(PathSelectors.any())
|
.paths(PathSelectors.any())
|
||||||
.build()
|
.build()
|
||||||
|
@ -3,6 +3,7 @@ package com.youlai.admin.converter;
|
|||||||
import com.youlai.admin.pojo.entity.SysDept;
|
import com.youlai.admin.pojo.entity.SysDept;
|
||||||
import com.youlai.admin.pojo.form.DeptForm;
|
import com.youlai.admin.pojo.form.DeptForm;
|
||||||
import com.youlai.admin.pojo.vo.dept.DeptDetailVO;
|
import com.youlai.admin.pojo.vo.dept.DeptDetailVO;
|
||||||
|
import com.youlai.admin.pojo.vo.dept.DeptVO;
|
||||||
import org.mapstruct.Mapper;
|
import org.mapstruct.Mapper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -14,6 +15,8 @@ import org.mapstruct.Mapper;
|
|||||||
@Mapper(componentModel = "spring")
|
@Mapper(componentModel = "spring")
|
||||||
public interface DeptConverter {
|
public interface DeptConverter {
|
||||||
|
|
||||||
|
DeptVO entity2Vo(SysDept entity);
|
||||||
|
|
||||||
DeptDetailVO entity2DetailVO(SysDept entity);
|
DeptDetailVO entity2DetailVO(SysDept entity);
|
||||||
|
|
||||||
SysDept form2Entity(DeptForm deptForm);
|
SysDept form2Entity(DeptForm deptForm);
|
||||||
|
@ -2,7 +2,6 @@ package com.youlai.admin.service.impl;
|
|||||||
|
|
||||||
import cn.hutool.core.bean.BeanUtil;
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
import cn.hutool.core.collection.CollectionUtil;
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
import cn.hutool.core.lang.Assert;
|
|
||||||
import cn.hutool.core.lang.Validator;
|
import cn.hutool.core.lang.Validator;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
@ -19,7 +18,6 @@ import com.youlai.common.constant.GlobalConstants;
|
|||||||
import com.youlai.common.constant.SystemConstants;
|
import com.youlai.common.constant.SystemConstants;
|
||||||
import com.youlai.common.web.domain.Option;
|
import com.youlai.common.web.domain.Option;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.apache.logging.log4j.util.Strings;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
@ -54,62 +52,56 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
|
|||||||
.eq(Validator.isNotNull(status), SysDept::getStatus, status)
|
.eq(Validator.isNotNull(status), SysDept::getStatus, status)
|
||||||
.orderByAsc(SysDept::getSort)
|
.orderByAsc(SysDept::getSort)
|
||||||
);
|
);
|
||||||
return recurDepartments(deptList);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
List<DeptVO> list = new ArrayList<>();
|
||||||
* 递归生成部门表格层级列表
|
|
||||||
*
|
if (CollectionUtil.isNotEmpty(deptList)) {
|
||||||
* @param deptList 部门列表
|
|
||||||
* @return 部门列表
|
Set<Long> cacheDeptIds = deptList.stream()
|
||||||
*/
|
.map(SysDept::getId)
|
||||||
private static List<DeptVO> recurDepartments(List<SysDept> deptList) {
|
.collect(Collectors.toSet());
|
||||||
List<DeptVO> deptTableList = new ArrayList<>();
|
|
||||||
// 保存所有节点的 id
|
|
||||||
Set<Long> nodeIdSet = deptList.stream()
|
for (SysDept dept : deptList) {
|
||||||
.map(SysDept::getId)
|
Long parentId = dept.getParentId();
|
||||||
.collect(Collectors.toSet());
|
// 不在缓存ID列表的parentId是顶级节点ID,以此作为递归开始
|
||||||
for (SysDept sysDept : deptList) {
|
if (cacheDeptIds.contains(parentId) == false) {
|
||||||
// 不在节点 id 集合中存在的 id 即为顶级节点 id, 递归生成列表
|
list.addAll(recurDepartments(parentId, deptList));
|
||||||
Long parentId = sysDept.getParentId();
|
cacheDeptIds.add(parentId); // 避免重复递归
|
||||||
if (!nodeIdSet.contains(parentId)) {
|
}
|
||||||
deptTableList.addAll(recurTableDepts(parentId, deptList));
|
|
||||||
nodeIdSet.add(parentId);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 如果结果列表为空说明所有的节点都是独立分散的, 直接转换后返回
|
|
||||||
if (deptTableList.isEmpty()) {
|
// 列表为空说明所有的节点都是独立的
|
||||||
return deptList.stream()
|
if (list.isEmpty()) {
|
||||||
.map(item -> {
|
return deptList.stream().map(item -> {
|
||||||
DeptVO deptVO = new DeptVO();
|
DeptVO deptVO = new DeptVO();
|
||||||
BeanUtil.copyProperties(item, deptVO);
|
BeanUtil.copyProperties(item, deptVO);
|
||||||
return deptVO;
|
return deptVO;
|
||||||
})
|
})
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
return deptTableList;
|
|
||||||
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 递归生成部门表格层级列表
|
* 递归生成部门层级列表
|
||||||
*
|
*
|
||||||
* @param parentId
|
* @param parentId
|
||||||
* @param deptList
|
* @param deptList
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static List<DeptVO> recurTableDepts(Long parentId, List<SysDept> deptList) {
|
public List<DeptVO> recurDepartments(Long parentId, List<SysDept> deptList) {
|
||||||
List<DeptVO> deptTableList = new ArrayList<>();
|
List<DeptVO> list = deptList.stream()
|
||||||
Optional.ofNullable(deptList).orElse(new ArrayList<>())
|
|
||||||
.stream()
|
|
||||||
.filter(dept -> dept.getParentId().equals(parentId))
|
.filter(dept -> dept.getParentId().equals(parentId))
|
||||||
.forEach(dept -> {
|
.map(dept -> {
|
||||||
DeptVO deptVO = new DeptVO();
|
DeptVO deptVO = deptConverter.entity2Vo(dept);
|
||||||
BeanUtil.copyProperties(dept, deptVO);
|
List<DeptVO> children = recurDepartments(dept.getId(), deptList);
|
||||||
List<DeptVO> children = recurTableDepts(dept.getId(), deptList);
|
|
||||||
deptVO.setChildren(children);
|
deptVO.setChildren(children);
|
||||||
deptTableList.add(deptVO);
|
return deptVO;
|
||||||
});
|
}).collect(Collectors.toList());
|
||||||
return deptTableList;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -217,7 +209,6 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> impl
|
|||||||
SysDept::getParentId,
|
SysDept::getParentId,
|
||||||
SysDept::getStatus,
|
SysDept::getStatus,
|
||||||
SysDept::getSort
|
SysDept::getSort
|
||||||
|
|
||||||
));
|
));
|
||||||
|
|
||||||
DeptDetailVO detailVO = deptConverter.entity2DetailVO(entity);
|
DeptDetailVO detailVO = deptConverter.entity2DetailVO(entity);
|
||||||
|
Loading…
Reference in New Issue
Block a user