update 优化代码

1. 删除内部if对部门不为null的多余判断,可以提前判断
2. 使用内部工具类StreamUtils
This commit is contained in:
cxh 2024-11-04 17:42:37 +08:00
parent 80e6943d2e
commit baef55ae1b

View File

@ -15,6 +15,7 @@ import org.dromara.common.core.service.DeptService;
import org.dromara.common.core.utils.MapstructUtils; import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.SpringUtils; import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.StringUtils; import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.StreamUtils;
import org.dromara.common.core.utils.TreeBuildUtils; import org.dromara.common.core.utils.TreeBuildUtils;
import org.dromara.common.mybatis.helper.DataBaseHelper; import org.dromara.common.mybatis.helper.DataBaseHelper;
import org.dromara.common.redis.utils.CacheUtils; import org.dromara.common.redis.utils.CacheUtils;
@ -106,8 +107,8 @@ public class SysDeptServiceImpl implements ISysDeptService, DeptService {
List<Tree<Long>> treeList = CollUtil.newArrayList(); List<Tree<Long>> treeList = CollUtil.newArrayList();
for (SysDeptVo d : depts) { for (SysDeptVo d : depts) {
Long parentId = d.getParentId(); Long parentId = d.getParentId();
SysDeptVo sysDeptVo = depts.stream().filter(it -> it.getDeptId().longValue() == parentId).findFirst().orElse(null); SysDeptVo sysDeptVo = StreamUtils.findFirst(depts, it -> it.getDeptId().longValue() == parentId);
if (sysDeptVo == null) { if (ObjectUtil.isNull(sysDeptVo)) {
List<Tree<Long>> trees = TreeBuildUtils.build(depts, parentId, (dept, tree) -> List<Tree<Long>> trees = TreeBuildUtils.build(depts, parentId, (dept, tree) ->
tree.setId(dept.getDeptId()) tree.setId(dept.getDeptId())
.setParentId(dept.getParentId()) .setParentId(dept.getParentId())
@ -276,11 +277,14 @@ public class SysDeptServiceImpl implements ISysDeptService, DeptService {
public int updateDept(SysDeptBo bo) { public int updateDept(SysDeptBo bo) {
SysDept dept = MapstructUtils.convert(bo, SysDept.class); SysDept dept = MapstructUtils.convert(bo, SysDept.class);
SysDept oldDept = baseMapper.selectById(dept.getDeptId()); SysDept oldDept = baseMapper.selectById(dept.getDeptId());
if (ObjectUtil.isNull(oldDept)) {
throw new ServiceException("部门不存在,无法修改");
}
if (!oldDept.getParentId().equals(dept.getParentId())) { if (!oldDept.getParentId().equals(dept.getParentId())) {
// 如果是新父部门 则校验是否具有新父部门权限 避免越权 // 如果是新父部门 则校验是否具有新父部门权限 避免越权
this.checkDeptDataScope(dept.getParentId()); this.checkDeptDataScope(dept.getParentId());
SysDept newParentDept = baseMapper.selectById(dept.getParentId()); SysDept newParentDept = baseMapper.selectById(dept.getParentId());
if (ObjectUtil.isNotNull(newParentDept) && ObjectUtil.isNotNull(oldDept)) { if (ObjectUtil.isNotNull(newParentDept)) {
String newAncestors = newParentDept.getAncestors() + StringUtils.SEPARATOR + newParentDept.getDeptId(); String newAncestors = newParentDept.getAncestors() + StringUtils.SEPARATOR + newParentDept.getDeptId();
String oldAncestors = oldDept.getAncestors(); String oldAncestors = oldDept.getAncestors();
dept.setAncestors(newAncestors); dept.setAncestors(newAncestors);