feat(web): 增加REST统一查询参数
This commit is contained in:
@@ -36,7 +36,7 @@ public class AuthenticationController extends SimpleControllerSupport<Authentica
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Authentication fromSaveItem(SaveItem item) {
|
protected Authentication fromSaveItem(SaveItem item) throws Exception {
|
||||||
Authentication authentication = new Authentication();
|
Authentication authentication = new Authentication();
|
||||||
authentication.setId(item.getId());
|
authentication.setId(item.getId());
|
||||||
authentication.setDescription(item.getDescription());
|
authentication.setDescription(item.getDescription());
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.eshore.gringotts.web.domain.base.controller;
|
package com.eshore.gringotts.web.domain.base.controller;
|
||||||
|
|
||||||
import com.eshore.gringotts.web.configuration.amis.AmisResponse;
|
import com.eshore.gringotts.web.configuration.amis.AmisResponse;
|
||||||
|
import com.eshore.gringotts.web.domain.base.controller.query.DetailQuery;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author lanyuanxiaoyao
|
* @author lanyuanxiaoyao
|
||||||
@@ -8,4 +9,5 @@ import com.eshore.gringotts.web.configuration.amis.AmisResponse;
|
|||||||
*/
|
*/
|
||||||
public interface DetailController<DETAIL_ITEM> {
|
public interface DetailController<DETAIL_ITEM> {
|
||||||
AmisResponse<DETAIL_ITEM> detail(Long id) throws Exception;
|
AmisResponse<DETAIL_ITEM> detail(Long id) throws Exception;
|
||||||
|
AmisResponse<DETAIL_ITEM> detail(DetailQuery query) throws Exception;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.eshore.gringotts.web.domain.base.controller;
|
package com.eshore.gringotts.web.domain.base.controller;
|
||||||
|
|
||||||
import com.eshore.gringotts.web.configuration.amis.AmisResponse;
|
import com.eshore.gringotts.web.configuration.amis.AmisResponse;
|
||||||
|
import com.eshore.gringotts.web.domain.base.controller.query.ListQuery;
|
||||||
import org.eclipse.collections.api.list.ImmutableList;
|
import org.eclipse.collections.api.list.ImmutableList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -9,4 +10,5 @@ import org.eclipse.collections.api.list.ImmutableList;
|
|||||||
*/
|
*/
|
||||||
public interface ListController<LIST_ITEM> {
|
public interface ListController<LIST_ITEM> {
|
||||||
AmisResponse<ImmutableList<LIST_ITEM>> list() throws Exception;
|
AmisResponse<ImmutableList<LIST_ITEM>> list() throws Exception;
|
||||||
|
AmisResponse<ImmutableList<LIST_ITEM>> list(ListQuery query) throws Exception;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package com.eshore.gringotts.web.domain.base.controller;
|
package com.eshore.gringotts.web.domain.base.controller;
|
||||||
|
|
||||||
import com.eshore.gringotts.web.configuration.amis.AmisResponse;
|
import com.eshore.gringotts.web.configuration.amis.AmisResponse;
|
||||||
|
import com.eshore.gringotts.web.domain.base.controller.query.DetailQuery;
|
||||||
|
import com.eshore.gringotts.web.domain.base.controller.query.ListQuery;
|
||||||
import com.eshore.gringotts.web.domain.base.entity.SimpleEntity;
|
import com.eshore.gringotts.web.domain.base.entity.SimpleEntity;
|
||||||
import com.eshore.gringotts.web.domain.base.service.SimpleServiceSupport;
|
import com.eshore.gringotts.web.domain.base.service.SimpleServiceSupport;
|
||||||
import org.eclipse.collections.api.list.ImmutableList;
|
import org.eclipse.collections.api.list.ImmutableList;
|
||||||
@@ -16,7 +18,8 @@ import org.springframework.web.bind.annotation.RequestBody;
|
|||||||
public abstract class SimpleControllerSupport<ENTITY extends SimpleEntity, SAVE_ITEM, LIST_ITEM, DETAIL_ITEM> implements SimpleController<SAVE_ITEM, LIST_ITEM, DETAIL_ITEM> {
|
public abstract class SimpleControllerSupport<ENTITY extends SimpleEntity, SAVE_ITEM, LIST_ITEM, DETAIL_ITEM> implements SimpleController<SAVE_ITEM, LIST_ITEM, DETAIL_ITEM> {
|
||||||
protected static final String SAVE = "/save";
|
protected static final String SAVE = "/save";
|
||||||
protected static final String LIST = "/list";
|
protected static final String LIST = "/list";
|
||||||
protected static final String DETAIL = "/detail/{id}";
|
protected static final String DETAIL_ID = "/detail/{id}";
|
||||||
|
protected static final String DETAIL = "/detail";
|
||||||
protected static final String REMOVE = "/remove/{id}";
|
protected static final String REMOVE = "/remove/{id}";
|
||||||
|
|
||||||
private final SimpleServiceSupport<ENTITY> service;
|
private final SimpleServiceSupport<ENTITY> service;
|
||||||
@@ -33,7 +36,7 @@ public abstract class SimpleControllerSupport<ENTITY extends SimpleEntity, SAVE_
|
|||||||
|
|
||||||
@GetMapping(LIST)
|
@GetMapping(LIST)
|
||||||
@Override
|
@Override
|
||||||
public AmisResponse<ImmutableList<LIST_ITEM>> list() {
|
public AmisResponse<ImmutableList<LIST_ITEM>> list() throws Exception {
|
||||||
return AmisResponse.responseSuccess(service.list().collect(entity -> {
|
return AmisResponse.responseSuccess(service.list().collect(entity -> {
|
||||||
try {
|
try {
|
||||||
return toListItem(entity);
|
return toListItem(entity);
|
||||||
@@ -43,12 +46,24 @@ public abstract class SimpleControllerSupport<ENTITY extends SimpleEntity, SAVE_
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping(DETAIL)
|
@PostMapping(LIST)
|
||||||
|
@Override
|
||||||
|
public AmisResponse<ImmutableList<LIST_ITEM>> list(@RequestBody ListQuery query) throws Exception {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping(DETAIL_ID)
|
||||||
@Override
|
@Override
|
||||||
public AmisResponse<DETAIL_ITEM> detail(@PathVariable Long id) throws Exception {
|
public AmisResponse<DETAIL_ITEM> detail(@PathVariable Long id) throws Exception {
|
||||||
return AmisResponse.responseSuccess(toDetailItem(service.detail(id)));
|
return AmisResponse.responseSuccess(toDetailItem(service.detail(id)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping(DETAIL)
|
||||||
|
@Override
|
||||||
|
public AmisResponse<DETAIL_ITEM> detail(@RequestBody DetailQuery query) throws Exception {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping(REMOVE)
|
@GetMapping(REMOVE)
|
||||||
@Override
|
@Override
|
||||||
public AmisResponse<Object> remove(@PathVariable Long id) {
|
public AmisResponse<Object> remove(@PathVariable Long id) {
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
package com.eshore.gringotts.web.domain.base.controller.query;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import org.eclipse.collections.api.list.ImmutableList;
|
||||||
|
import org.eclipse.collections.api.map.ImmutableMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 统一查询
|
||||||
|
*
|
||||||
|
* @author lanyuanxiaoyao
|
||||||
|
* @date 2024-12-03
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class DetailQuery {
|
||||||
|
private Queryable query;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class Queryable {
|
||||||
|
private ImmutableList<String> nullEqual;
|
||||||
|
private ImmutableList<String> notNullEqual;
|
||||||
|
private ImmutableList<String> empty;
|
||||||
|
private ImmutableList<String> notEmpty;
|
||||||
|
private ImmutableMap<String, ?> equal;
|
||||||
|
private ImmutableMap<String, ?> notEqual;
|
||||||
|
private ImmutableMap<String, String> like;
|
||||||
|
private ImmutableMap<String, String> notLike;
|
||||||
|
private ImmutableMap<String, ?> great;
|
||||||
|
private ImmutableMap<String, ?> less;
|
||||||
|
private ImmutableMap<String, ?> greatEqual;
|
||||||
|
private ImmutableMap<String, ?> lessEqual;
|
||||||
|
private ImmutableMap<String, ImmutableList<?>> in;
|
||||||
|
private ImmutableMap<String, ImmutableList<?>> notIn;
|
||||||
|
private ImmutableMap<String, Between> between;
|
||||||
|
private ImmutableMap<String, Between> notBetween;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class Between {
|
||||||
|
private String start;
|
||||||
|
private String end;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
package com.eshore.gringotts.web.domain.base.controller.query;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.ToString;
|
||||||
|
import org.eclipse.collections.api.list.ImmutableList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lanyuanxiaoyao
|
||||||
|
* @date 2024-12-03
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@ToString(callSuper = true)
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
public class ListQuery extends DetailQuery {
|
||||||
|
private ImmutableList<Sortable> sort;
|
||||||
|
private Pageable page;
|
||||||
|
|
||||||
|
public ListQuery(Queryable query) {
|
||||||
|
super(query);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ListQuery(Queryable query, ImmutableList<Sortable> sort, Pageable page) {
|
||||||
|
super(query);
|
||||||
|
this.sort = sort;
|
||||||
|
this.page = page;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class Sortable {
|
||||||
|
private String column;
|
||||||
|
private Direction direction;
|
||||||
|
|
||||||
|
public enum Direction {
|
||||||
|
ASC,
|
||||||
|
DESC,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public static class Pageable {
|
||||||
|
private Integer page = 1;
|
||||||
|
private Integer size = 10;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -30,12 +30,7 @@ public abstract class LogicDeleteService<ENTITY extends LogicDeleteEntity> exten
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ImmutableList<Predicate> extraListPredicates(Root<ENTITY> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
|
protected ImmutableList<Predicate> listPredicates(Root<ENTITY> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
|
||||||
return Lists.immutable.of(builder.equal(root.get("deleted"), false));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected ImmutableList<Predicate> extraDetailPredicates(Root<ENTITY> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
|
|
||||||
return Lists.immutable.of(builder.equal(root.get("deleted"), false));
|
return Lists.immutable.of(builder.equal(root.get("deleted"), false));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package com.eshore.gringotts.web.domain.base.service;
|
package com.eshore.gringotts.web.domain.base.service;
|
||||||
|
|
||||||
|
import com.eshore.gringotts.web.domain.base.controller.query.DetailQuery;
|
||||||
|
import com.eshore.gringotts.web.domain.base.controller.query.ListQuery;
|
||||||
import com.eshore.gringotts.web.domain.base.entity.SimpleEntity;
|
import com.eshore.gringotts.web.domain.base.entity.SimpleEntity;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import org.eclipse.collections.api.list.ImmutableList;
|
import org.eclipse.collections.api.list.ImmutableList;
|
||||||
@@ -16,13 +18,23 @@ public interface SimpleService<ENTITY extends SimpleEntity> {
|
|||||||
|
|
||||||
ImmutableList<ENTITY> list(ImmutableSet<Long> ids) throws Exception;
|
ImmutableList<ENTITY> list(ImmutableSet<Long> ids) throws Exception;
|
||||||
|
|
||||||
|
ImmutableList<ENTITY> list(ListQuery query) throws Exception;
|
||||||
|
|
||||||
Optional<ENTITY> detailOptional(Long id) throws Exception;
|
Optional<ENTITY> detailOptional(Long id) throws Exception;
|
||||||
|
|
||||||
|
Optional<ENTITY> detailOptional(DetailQuery query) throws Exception;
|
||||||
|
|
||||||
ENTITY detail(Long id) throws Exception;
|
ENTITY detail(Long id) throws Exception;
|
||||||
|
|
||||||
|
ENTITY detail(DetailQuery query) throws Exception;
|
||||||
|
|
||||||
ENTITY detailOrThrow(Long id) throws Exception;
|
ENTITY detailOrThrow(Long id) throws Exception;
|
||||||
|
|
||||||
|
ENTITY detailOrThrow(DetailQuery query) throws Exception;
|
||||||
|
|
||||||
ENTITY detailOrNull(Long id) throws Exception;
|
ENTITY detailOrNull(Long id) throws Exception;
|
||||||
|
|
||||||
|
ENTITY detailOrNull(DetailQuery query) throws Exception;
|
||||||
|
|
||||||
void remove(Long id) throws Exception;
|
void remove(Long id) throws Exception;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import cn.hutool.core.bean.BeanUtil;
|
|||||||
import cn.hutool.core.bean.copier.CopyOptions;
|
import cn.hutool.core.bean.copier.CopyOptions;
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import com.eshore.gringotts.web.domain.base.controller.query.DetailQuery;
|
||||||
|
import com.eshore.gringotts.web.domain.base.controller.query.ListQuery;
|
||||||
import com.eshore.gringotts.web.domain.base.entity.SimpleEntity;
|
import com.eshore.gringotts.web.domain.base.entity.SimpleEntity;
|
||||||
import com.eshore.gringotts.web.domain.base.repository.SimpleRepository;
|
import com.eshore.gringotts.web.domain.base.repository.SimpleRepository;
|
||||||
import com.eshore.gringotts.web.domain.user.entity.User;
|
import com.eshore.gringotts.web.domain.user.entity.User;
|
||||||
@@ -59,43 +61,99 @@ public abstract class SimpleServiceSupport<ENTITY extends SimpleEntity> implemen
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ImmutableList<ENTITY> list() {
|
public ImmutableList<ENTITY> list() throws Exception {
|
||||||
|
return Lists.immutable.ofAll(repository.findAll((root, query, builder) -> builder.and(listPredicates(root, query, builder).toArray(new Predicate[]{}))));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ImmutableList<ENTITY> list(ImmutableSet<Long> ids) throws Exception {
|
||||||
return Lists.immutable.ofAll(repository.findAll(
|
return Lists.immutable.ofAll(repository.findAll(
|
||||||
(root, query, builder) -> {
|
(root, query, builder) -> {
|
||||||
|
MutableList<Predicate> predicates = Lists.mutable.ofAll(listPredicates(root, query, builder));
|
||||||
|
predicates.add(builder.in(root.get("id")).value(ids));
|
||||||
|
return builder.and(predicates.toArray(new Predicate[predicates.size()]));
|
||||||
|
}
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
private ImmutableList<Predicate> queryPredicates(DetailQuery.Queryable queryable, Root<ENTITY> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
|
||||||
|
MutableList<Predicate> predicates = Lists.mutable.empty();
|
||||||
|
if (ObjectUtil.isEmpty(queryable)) {
|
||||||
|
return predicates.toImmutable();
|
||||||
|
}
|
||||||
|
if (ObjectUtil.isNotEmpty(queryable.getNullEqual())) {
|
||||||
|
queryable.getNullEqual().forEach(column -> predicates.add(builder.isNull(root.get(column))));
|
||||||
|
}
|
||||||
|
if (ObjectUtil.isNotEmpty(queryable.getNotNullEqual())) {
|
||||||
|
queryable.getNotNullEqual().forEach(column -> predicates.add(builder.isNull(root.get(column))));
|
||||||
|
}
|
||||||
|
if (ObjectUtil.isNotEmpty(queryable.getEmpty())) {
|
||||||
|
queryable.getEmpty().forEach(column -> predicates.add(builder.isEmpty(root.get(column))));
|
||||||
|
}
|
||||||
|
if (ObjectUtil.isNotEmpty(queryable.getNotEmpty())) {
|
||||||
|
queryable.getNotEmpty().forEach(column -> predicates.add(builder.isNotEmpty(root.get(column))));
|
||||||
|
}
|
||||||
|
if (ObjectUtil.isNotEmpty(queryable.getEqual())) {
|
||||||
|
queryable.getEqual().forEachKeyValue((column, value) -> predicates.add(builder.equal(root.get(column), value)));
|
||||||
|
}
|
||||||
|
if (ObjectUtil.isNotEmpty(queryable.getNotEqual())) {
|
||||||
|
queryable.getEqual().forEachKeyValue((column, value) -> predicates.add(builder.notEqual(root.get(column), value)));
|
||||||
|
}
|
||||||
|
if (ObjectUtil.isNotEmpty(queryable.getLike())) {
|
||||||
|
queryable.getLike().forEachKeyValue((column, value) -> predicates.add(builder.like(root.get(column), value)));
|
||||||
|
}
|
||||||
|
if (ObjectUtil.isNotEmpty(queryable.getNotLike())) {
|
||||||
|
queryable.getNotLike().forEachKeyValue((column, value) -> predicates.add(builder.notLike(root.get(column), value)));
|
||||||
|
}
|
||||||
|
if (ObjectUtil.isNotEmpty(queryable.getGreat())) {
|
||||||
|
queryable.getGreat().forEachKeyValue((column, value) -> predicates.add(builder.greaterThan(root.get(column), (Comparable<Object>) value)));
|
||||||
|
}
|
||||||
|
if (ObjectUtil.isNotEmpty(queryable.getLess())) {
|
||||||
|
queryable.getLess().forEachKeyValue((column, value) -> predicates.add(builder.lessThan(root.get(column), (Comparable<Object>) value)));
|
||||||
|
}
|
||||||
|
if (ObjectUtil.isNotEmpty(queryable.getGreatEqual())) {
|
||||||
|
queryable.getGreatEqual().forEachKeyValue((column, value) -> predicates.add(builder.greaterThanOrEqualTo(root.get(column), (Comparable<Object>) value)));
|
||||||
|
}
|
||||||
|
if (ObjectUtil.isNotEmpty(queryable.getLessEqual())) {
|
||||||
|
queryable.getLessEqual().forEachKeyValue((column, value) -> predicates.add(builder.lessThanOrEqualTo(root.get(column), (Comparable<Object>) value)));
|
||||||
|
}
|
||||||
|
if (ObjectUtil.isNotEmpty(queryable.getIn())) {
|
||||||
|
queryable.getIn().forEachKeyValue((column, value) -> predicates.add(builder.in(root.get(column)).value(value)));
|
||||||
|
}
|
||||||
|
if (ObjectUtil.isNotEmpty(queryable.getNotIn())) {
|
||||||
|
queryable.getNotIn().forEachKeyValue((column, value) -> predicates.add(builder.in(root.get(column)).value(value).not()));
|
||||||
|
}
|
||||||
|
if (ObjectUtil.isNotEmpty(queryable.getBetween())) {
|
||||||
|
queryable.getBetween().forEachKeyValue((column, value) -> predicates.add(builder.between(root.get(column), value.getStart(), value.getEnd())));
|
||||||
|
}
|
||||||
|
if (ObjectUtil.isNotEmpty(queryable.getNotBetween())) {
|
||||||
|
queryable.getNotBetween().forEachKeyValue((column, value) -> predicates.add(builder.between(root.get(column), value.getStart(), value.getEnd())));
|
||||||
|
}
|
||||||
|
return predicates.toImmutable();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected ImmutableList<Predicate> listPredicates(Root<ENTITY> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
|
||||||
MutableList<Predicate> predicates = Lists.mutable.empty();
|
MutableList<Predicate> predicates = Lists.mutable.empty();
|
||||||
User user = userService.currentLoginUser();
|
User user = userService.currentLoginUser();
|
||||||
if (User.isNotAdministrator(user)) {
|
if (User.isNotAdministrator(user)) {
|
||||||
predicates.add(builder.equal(root.get("createdUser"), user));
|
predicates.add(builder.equal(root.get("createdUser"), user));
|
||||||
}
|
}
|
||||||
predicates.addAllIterable(extraListPredicates(root, query, builder));
|
return predicates.toImmutable();
|
||||||
return builder.and(predicates.toArray(new Predicate[predicates.size()]));
|
|
||||||
},
|
|
||||||
Sort.by("createdTime").descending()
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ImmutableList<ENTITY> list(ImmutableSet<Long> ids) {
|
public ImmutableList<ENTITY> list(ListQuery listQuery) throws Exception {
|
||||||
return Lists.immutable.ofAll(repository.findAll(
|
return Lists.immutable.ofAll(repository.findAll(
|
||||||
(root, query, builder) -> {
|
(root, query, builder) -> {
|
||||||
MutableList<Predicate> predicates = Lists.mutable.of(
|
MutableList<Predicate> predicates = Lists.mutable.ofAll(listPredicates(root, query, builder));
|
||||||
builder.in(root.get("id")).value(ids.select(ObjectUtil::isNotNull))
|
predicates.addAllIterable(queryPredicates(listQuery.getQuery(), root, query, builder));
|
||||||
);
|
|
||||||
User user = userService.currentLoginUser();
|
|
||||||
if (User.isNotAdministrator(user)) {
|
|
||||||
predicates.add(builder.equal(root.get("createdUser"), user));
|
|
||||||
}
|
|
||||||
predicates.addAllIterable(extraListPredicates(root, query, builder));
|
|
||||||
return builder.and(predicates.toArray(new Predicate[predicates.size()]));
|
return builder.and(predicates.toArray(new Predicate[predicates.size()]));
|
||||||
},
|
},
|
||||||
Sort.by("createdTime").descending()
|
Sort.by("createdTime").descending()
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ImmutableList<Predicate> extraListPredicates(Root<ENTITY> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
|
|
||||||
return Lists.immutable.empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<ENTITY> detailOptional(Long id) {
|
public Optional<ENTITY> detailOptional(Long id) {
|
||||||
if (ObjectUtil.isNull(id)) {
|
if (ObjectUtil.isNull(id)) {
|
||||||
@@ -103,21 +161,22 @@ public abstract class SimpleServiceSupport<ENTITY extends SimpleEntity> implemen
|
|||||||
}
|
}
|
||||||
return repository.findOne(
|
return repository.findOne(
|
||||||
(root, query, builder) -> {
|
(root, query, builder) -> {
|
||||||
MutableList<Predicate> predicates = Lists.mutable.of(
|
MutableList<Predicate> predicates = Lists.mutable.ofAll(listPredicates(root, query, builder));
|
||||||
builder.equal(root.get("id"), id)
|
predicates.add(builder.equal(root.get("id"), id));
|
||||||
);
|
|
||||||
User user = userService.currentLoginUser();
|
|
||||||
if (User.isNotAdministrator(user)) {
|
|
||||||
predicates.add(builder.equal(root.get("createdUser"), user));
|
|
||||||
}
|
|
||||||
predicates.addAllIterable(extraDetailPredicates(root, query, builder));
|
|
||||||
return builder.and(predicates.toArray(new Predicate[predicates.size()]));
|
return builder.and(predicates.toArray(new Predicate[predicates.size()]));
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ImmutableList<Predicate> extraDetailPredicates(Root<ENTITY> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
|
@Override
|
||||||
return Lists.immutable.empty();
|
public Optional<ENTITY> detailOptional(DetailQuery detailQuery) throws Exception {
|
||||||
|
return repository.findOne(
|
||||||
|
(root, query, builder) -> {
|
||||||
|
MutableList<Predicate> predicates = Lists.mutable.ofAll(listPredicates(root, query, builder));
|
||||||
|
predicates.addAllIterable(queryPredicates(detailQuery.getQuery(), root, query, builder));
|
||||||
|
return builder.and(predicates.toArray(new Predicate[predicates.size()]));
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -125,16 +184,31 @@ public abstract class SimpleServiceSupport<ENTITY extends SimpleEntity> implemen
|
|||||||
return detailOrNull(id);
|
return detailOrNull(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ENTITY detail(DetailQuery query) throws Exception {
|
||||||
|
return detailOrNull(query);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ENTITY detailOrThrow(Long id) {
|
public ENTITY detailOrThrow(Long id) {
|
||||||
return detailOptional(id).orElseThrow(() -> new IdNotFoundException(id));
|
return detailOptional(id).orElseThrow(() -> new IdNotFoundException(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ENTITY detailOrThrow(DetailQuery query) throws Exception {
|
||||||
|
return detailOptional(query).orElseThrow(IdNotFoundException::new);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ENTITY detailOrNull(Long id) {
|
public ENTITY detailOrNull(Long id) {
|
||||||
return detailOptional(id).orElse(null);
|
return detailOptional(id).orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ENTITY detailOrNull(DetailQuery query) throws Exception {
|
||||||
|
return detailOptional(query).orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
@Transactional(rollbackOn = Throwable.class)
|
@Transactional(rollbackOn = Throwable.class)
|
||||||
@Override
|
@Override
|
||||||
public void remove(Long id) {
|
public void remove(Long id) {
|
||||||
@@ -144,6 +218,10 @@ public abstract class SimpleServiceSupport<ENTITY extends SimpleEntity> implemen
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static final class IdNotFoundException extends RuntimeException {
|
public static final class IdNotFoundException extends RuntimeException {
|
||||||
|
public IdNotFoundException() {
|
||||||
|
super("资源不存在");
|
||||||
|
}
|
||||||
|
|
||||||
public IdNotFoundException(Long id) {
|
public IdNotFoundException(Long id) {
|
||||||
super(StrUtil.format("ID为{}的资源不存在", id));
|
super(StrUtil.format("ID为{}的资源不存在", id));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.eshore.gringotts.web.domain.check.controller;
|
|||||||
|
|
||||||
import com.eshore.gringotts.web.configuration.amis.AmisResponse;
|
import com.eshore.gringotts.web.configuration.amis.AmisResponse;
|
||||||
import com.eshore.gringotts.web.domain.base.controller.ListController;
|
import com.eshore.gringotts.web.domain.base.controller.ListController;
|
||||||
|
import com.eshore.gringotts.web.domain.base.controller.query.ListQuery;
|
||||||
import com.eshore.gringotts.web.domain.check.entity.CheckOrder;
|
import com.eshore.gringotts.web.domain.check.entity.CheckOrder;
|
||||||
import com.eshore.gringotts.web.domain.check.service.CheckOrderService;
|
import com.eshore.gringotts.web.domain.check.service.CheckOrderService;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
@@ -43,6 +44,11 @@ public class CheckOrderController implements ListController<CheckOrderController
|
|||||||
return AmisResponse.responseSuccess(checkOrderService.list().collect(this::toListItem));
|
return AmisResponse.responseSuccess(checkOrderService.list().collect(this::toListItem));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AmisResponse<ImmutableList<ListItem>> list(ListQuery query) throws Exception {
|
||||||
|
return AmisResponse.responseSuccess(checkOrderService.list().collect(this::toListItem));
|
||||||
|
}
|
||||||
|
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
private ListItem toListItem(CheckOrder order) {
|
private ListItem toListItem(CheckOrder order) {
|
||||||
ListItem item = new ListItem();
|
ListItem item = new ListItem();
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ public class ConfirmationController extends SimpleControllerSupport<Confirmation
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Confirmation fromSaveItem(SaveItem item) {
|
protected Confirmation fromSaveItem(SaveItem item) throws Exception {
|
||||||
Confirmation confirmation = new Confirmation();
|
Confirmation confirmation = new Confirmation();
|
||||||
confirmation.setId(item.getId());
|
confirmation.setId(item.getId());
|
||||||
confirmation.setDescription(item.getDescription());
|
confirmation.setDescription(item.getDescription());
|
||||||
|
|||||||
@@ -0,0 +1,69 @@
|
|||||||
|
package com.eshore.gringotts.web;
|
||||||
|
|
||||||
|
import com.eshore.gringotts.web.domain.base.controller.query.DetailQuery;
|
||||||
|
import com.eshore.gringotts.web.domain.base.controller.query.ListQuery;
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.datatype.eclipsecollections.EclipseCollectionsModule;
|
||||||
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lanyuanxiaoyao
|
||||||
|
* @date 2024-12-03
|
||||||
|
*/
|
||||||
|
public class TestQueryParse {
|
||||||
|
public static void main(String[] args) throws JsonProcessingException {
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
mapper.registerModule(new EclipseCollectionsModule());
|
||||||
|
mapper.registerModule(new JavaTimeModule());
|
||||||
|
// language=JSON
|
||||||
|
System.out.println(mapper.readValue("{}", DetailQuery.class));
|
||||||
|
// language=JSON
|
||||||
|
System.out.println(mapper.readValue("{\n" +
|
||||||
|
" \"query\": {\n" +
|
||||||
|
" \"equal\": {\n" +
|
||||||
|
" \"name\": \"lanyuanxiaoyao\"\n" +
|
||||||
|
" }\n" +
|
||||||
|
" }\n" +
|
||||||
|
"}", DetailQuery.class));
|
||||||
|
// language=JSON
|
||||||
|
System.out.println(mapper.readValue("{\n" +
|
||||||
|
" \"query\": {\n" +
|
||||||
|
" \"equal\": {\n" +
|
||||||
|
" \"name\": \"lanyuanxiaoyao\"\n" +
|
||||||
|
" },\n" +
|
||||||
|
" \"notEqual\": {\n" +
|
||||||
|
" \"username\": \"lanyuanxiaoyao@qq.com\",\n" +
|
||||||
|
" \"password\": \"mingland87\"\n" +
|
||||||
|
" },\n" +
|
||||||
|
" \"great\": {\n" +
|
||||||
|
" \"age\": 12,\n" +
|
||||||
|
" \"createTime\": \"2020-10-12 00:00:00\"\n" +
|
||||||
|
" },\n" +
|
||||||
|
" \"in\": {\n" +
|
||||||
|
" \"age\": [\n" +
|
||||||
|
" 15,\n" +
|
||||||
|
" 18,\n" +
|
||||||
|
" 20\n" +
|
||||||
|
" ]\n" +
|
||||||
|
" },\n" +
|
||||||
|
" \"between\": {\n" +
|
||||||
|
" \"createTime\": {\n" +
|
||||||
|
" \"start\": \"2024-12-03 00:00:00\",\n" +
|
||||||
|
" \"end\": \"2024-12-03 00:00:00\"\n" +
|
||||||
|
" }\n" +
|
||||||
|
" }\n" +
|
||||||
|
" },\n" +
|
||||||
|
" \"sort\": [\n" +
|
||||||
|
" {\n" +
|
||||||
|
" \"column\": \"createTime\",\n" +
|
||||||
|
" \"direction\": \"ASC\"\n" +
|
||||||
|
" }\n" +
|
||||||
|
" ],\n" +
|
||||||
|
" \"page\": {\n" +
|
||||||
|
" \"size\": 10,\n" +
|
||||||
|
" \"page\": 1\n" +
|
||||||
|
" }\n" +
|
||||||
|
"}", ListQuery.class));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user