feat:实现特殊情况的保存

This commit is contained in:
wangyu 2022-01-03 21:18:36 +08:00
parent 617098577e
commit d68c2b2300
2 changed files with 32 additions and 5 deletions

View File

@ -148,7 +148,10 @@ public final class CriteriaBuilder<T extends Domain> {
// 添加自定义criteria // 添加自定义criteria
if (CollectionUtils.isNotEmpty(criterias)) { if (CollectionUtils.isNotEmpty(criterias)) {
for (Supplier<Criteria> builder : criterias) { for (Supplier<Criteria> builder : criterias) {
criteria.add(builder.get()); Criteria built = builder.get();
if (null != built) {
criteria.add(built);
}
} }
} }
return combine(criteria); return combine(criteria);

View File

@ -4,6 +4,7 @@ import com.flyfish.framework.builder.CriteriaBuilder;
import com.flyfish.framework.domain.base.NameLikeQo; import com.flyfish.framework.domain.base.NameLikeQo;
import com.flyfish.framework.domain.po.Department; import com.flyfish.framework.domain.po.Department;
import com.flyfish.framework.enums.UserType; import com.flyfish.framework.enums.UserType;
import lombok.AccessLevel;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.BooleanUtils;
@ -22,6 +23,7 @@ import java.util.Set;
public abstract class AuthorizedQo<T extends AuthorizedDomain> extends NameLikeQo<T> { public abstract class AuthorizedQo<T extends AuthorizedDomain> extends NameLikeQo<T> {
// 是否已发布 // 是否已发布
@Getter(AccessLevel.NONE)
private Boolean published; private Boolean published;
/** /**
@ -40,21 +42,43 @@ public abstract class AuthorizedQo<T extends AuthorizedDomain> extends NameLikeQ
public CriteriaBuilder<T> criteriaBuilder() { public CriteriaBuilder<T> criteriaBuilder() {
// 超级管理员拥有查看所有草稿的权限 // 超级管理员拥有查看所有草稿的权限
if (user.getType() == UserType.SUPER_ADMIN) { if (user.getType() == UserType.SUPER_ADMIN) {
return super.criteriaBuilder().with("published"); return super.criteriaBuilder().with(this::withPublished);
} }
// 查询草稿只查询自己的 // 查询草稿只查询自己的
if (BooleanUtils.isFalse(published)) { if (BooleanUtils.isFalse(published)) {
return super.criteriaBuilder().with("published").with(() -> Criteria.where("creatorId").is(user.getId())); return super.criteriaBuilder()
.with(this::withPublished)
.with(() -> Criteria.where("creatorId").is(user.getId()));
} }
// 普通查询根据权限配置查询 // 普通查询根据权限配置查询
return super.criteriaBuilder() return super.criteriaBuilder()
.with(this::withPublished)
.with(() -> Criteria.where("$or").is( .with(() -> Criteria.where("$or").is(
CriteriaBuilder.createCriteriaList( CriteriaBuilder.createCriteriaList(
Criteria.where("authorizeId").in(authorizeIds()), Criteria.where("authorizeId").in(authorizeIds()),
Criteria.where("creatorId").is(user.getId()) Criteria.where("creatorId").is(user.getId())
.and("authorizeId").in(((AuthorizedUserDetails) user).getVisibleDeparts()) .and("authorizeId").in(((AuthorizedUserDetails) user).getVisibleDeparts())
) )
)) ));
.with("published"); }
/**
* 构造发布状态查询
*
* @return 结果
*/
private Criteria withPublished() {
if (null != published) {
if (BooleanUtils.isTrue(published)) {
return Criteria.where("published").is(true);
}
return Criteria.where("$or").is(
CriteriaBuilder.createCriteriaList(
Criteria.where("published").isNull(),
Criteria.where("published").is(false)
)
);
}
return null;
} }
} }