feat:优化逻辑,实现预期目标

This commit is contained in:
wangyu 2022-03-08 17:06:23 +08:00
parent 975526b811
commit 2b995f5d56
4 changed files with 62 additions and 12 deletions

View File

@ -6,6 +6,8 @@ import com.flyfish.framework.domain.AdminUserDetails;
import com.flyfish.framework.domain.DepartmentQo;
import com.flyfish.framework.domain.po.Department;
import com.flyfish.framework.enums.UserType;
import com.flyfish.framework.service.DepartmentService;
import com.flyfish.framework.utils.DepartUtils;
import com.flyfish.framework.utils.UserUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
@ -17,6 +19,7 @@ import reactor.core.publisher.Mono;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
* 部分或者校区controller
@ -39,8 +42,16 @@ public class DepartmentController extends ReactiveTreeController<Department, Dep
if (details.getType() == UserType.SUPER_ADMIN) {
return reactiveService.getAll();
}
if (CollectionUtils.isNotEmpty(details.getVisibleDeparts())) {
return reactiveService.getByIds(new ArrayList<>(details.getVisibleDeparts()));
if (CollectionUtils.isNotEmpty(details.getDepartments())) {
DepartmentService departmentService = getService();
Set<String> departs = DepartUtils.mergeDeparts(details.getDepartments());
return departmentService.getSubCodes(departs)
.flatMapMany(codes -> {
List<String> merged = new ArrayList<>();
merged.addAll(codes);
merged.addAll(departs);
return departmentService.getByIds(merged);
});
}
return Flux.empty();
})

View File

@ -155,7 +155,10 @@ public class AdminUserDetails implements UserDetails, IUser, AuthorizedUserDetai
@Override
public String getAuthority() {
if (StringUtils.isBlank(authority) && null != departments) {
authority = departments.stream().findFirst().map(Domain::getId).orElse(null);
authority = departments.stream()
.min(Comparator.comparing(Department::getDepth))
.map(Domain::getId)
.orElse(null);
}
return authority;
}

View File

@ -2,14 +2,16 @@ package com.flyfish.framework.service;
import com.flyfish.framework.domain.AdminUserDetails;
import com.flyfish.framework.domain.DepartmentQo;
import com.flyfish.framework.domain.authorized.AuthorizedUserDetails;
import com.flyfish.framework.domain.base.Qo;
import com.flyfish.framework.domain.po.Department;
import com.flyfish.framework.domain.tree.TreeDomain;
import com.flyfish.framework.enums.UserType;
import com.flyfish.framework.service.impl.BaseReactiveServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
@ -17,7 +19,7 @@ import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import javax.annotation.Resource;
import java.util.List;
import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;
@ -27,6 +29,7 @@ import java.util.stream.Collectors;
* @author wangyu
*/
@Service
@Slf4j
public class DepartmentService extends BaseReactiveServiceImpl<Department> {
@Resource
@ -51,10 +54,36 @@ public class DepartmentService extends BaseReactiveServiceImpl<Department> {
*/
@Override
public Flux<Department> getList(Qo<Department> query) {
return convertQo(query).flatMapMany(super::getList);
}
/**
* 在一批编码中找到深度最小的值
*
* @param codes 编码们
* @return 结果
*/
private Mono<Integer> getMinDepthInIds(Collection<String> codes) {
return reactiveMongoOperations.aggregate(
Aggregation.newAggregation(Aggregation.group().min("depth").as("depth")),
Department.class, Department.class)
.single()
.map(TreeDomain::getDepth);
}
/**
* 转换qo经过可见部门转换更加私密
*
* @param query 查询
* @return 结果
*/
private Mono<Qo<Department>> convertQo(Qo<Department> query) {
if (query instanceof DepartmentQo) {
DepartmentQo qo = (DepartmentQo) query;
// 如果是非管理员
if (qo.getUser().getType() != UserType.SUPER_ADMIN) {
// 占位包装查询实体
Mono<Qo<Department>> mono = Mono.just(qo);
// 使用列表内的部门作为条件
AdminUserDetails userDetails = (AdminUserDetails) query.getUser();
// 查询根节点下的节点
@ -63,18 +92,21 @@ public class DepartmentService extends BaseReactiveServiceImpl<Department> {
qo.setIds(userDetails.getVisibleDeparts());
// 不递归指定深度保证单层
if (BooleanUtils.isNotTrue(qo.getRecursive())) {
// 指定深度
int maxDepth = userDetails.getDepartments().stream().max((a, b) -> a.getDepth() - b.getDepth())
.map(Department::getDepth)
.orElse(0);
qo.setDepth(maxDepth);
// 指定深度从可见的列表中获取
mono = getMinDepthInIds(userDetails.getVisibleDeparts())
.map(depth -> {
qo.setDepth(depth);
return qo;
});
}
}
// 父id均为空返回所有可见部门即可
if (null == qo.getParentId() && CollectionUtils.isEmpty(qo.getParentIds())) {
qo.setIds(userDetails.getVisibleDeparts());
}
return mono;
}
}
return super.getList(query);
return Mono.just(query);
}
}

View File

@ -9,6 +9,7 @@ import com.flyfish.framework.utils.CopyUtils;
import com.flyfish.framework.utils.DepartUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;
@ -52,8 +53,11 @@ public class UserDetailsConverter {
Set<Role.Authority> authorities = user.getRoles().stream()
.flatMap(role -> null == role.getAuthorities() ? Stream.empty() : role.getAuthorities().stream())
.collect(Collectors.toSet());
// 默认使用用户当前选择的权限进行查询
String authority = user.getAuthority();
// 根据权限组装查询条件
Set<String> departs = DepartUtils.mergeDeparts(user.getDepartments());
Set<String> departs = StringUtils.isNotBlank(authority) ? Collections.singleton(authority) :
DepartUtils.mergeDeparts(user.getDepartments());
// 查询所有子部门id
return departmentService.getSubCodes(departs)
.map(codes -> {