使grants中的每个条件之间是“或”的关系
正确写法:
public List<Long> getAllEmployeeIds(List<GrantPermission> grants) {
List<PropertyFilterBuilder> builderList = new ArrayList<>();
for(GrantPermission g : grants){
switch (g.getSecurityType()){
case employee:
builderList.add(new PropertyFilterBuilder().equal("id",Long.parseLong(g.getValue())));
case department:
Department department = departmentDao.findById(Long.parseLong(g.getValue())).orElse(null);
if(department == null)
continue;
builderList.add(new PropertyFilterBuilder().startsWith("employeePositions.department.path",department.getPath()));
case employeeGroup:
builderList.add(new PropertyFilterBuilder().equal("groups.id",g.getValue()));
case role:
builderList.add(new PropertyFilterBuilder().equal("user.roles.id",g.getValue()));
default:
log.error("未找到权限对应的类型");
}
}
List<PropertyFilter> build = PropertyFilter.builder().or(builderList.stream().map(e -> e.build()).flatMap(Collection::stream).collect(Collectors.toList()))
.and(new PropertyFilterBuilder().in("organizationEmployees.status.code", "fullparty","probationary","work")).build();
return employeeService.findEmployeeInUse(build);
}
运行结果:
错误写法:
public List<Long> getAllEmployeeIds(List<GrantPermission> grants) {
PropertyFilterBuilder builder = new PropertyFilterBuilder();
for(GrantPermission g : grants){
switch (g.getSecurityType()){
case employee:
builder.or(new PropertyFilterBuilder().equal("id",Long.parseLong(g.getValue())));
case department:
Department department = departmentDao.findById(Long.parseLong(g.getValue())).orElse(null);
if(department == null)
continue;
builder.or(new PropertyFilterBuilder().startsWith("employeePositions.department.path",department.getPath()));
case employeeGroup:
builder.or(new PropertyFilterBuilder().equal("groups.id",g.getValue()));
case role:
builder.or(new PropertyFilterBuilder().equal("user.roles.id",g.getValue()));
default:
log.error("未找到权限对应的类型");
}
}
PropertyFilterBuilder builder2 = new PropertyFilterBuilder();
builder2.in("organizationEmployees.status.code", "fullparty","probationary","work");
return employeeService.findEmployeeInUse(builder2.and(builder).build());
}
运行结果: