|
@@ -1,16 +1,9 @@
|
|
|
package com.persagy.dc.basic.model;
|
|
|
|
|
|
-import cn.hutool.core.util.StrUtil;
|
|
|
-import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.OrderItem;
|
|
|
-import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
-import com.fasterxml.jackson.databind.JsonNode;
|
|
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
-import com.persagy.dc.basic.utils.JsonNodeUtils;
|
|
|
-import com.persagy.dc.common.model.entity.BaseEntity;
|
|
|
import lombok.Data;
|
|
|
|
|
|
-import java.util.Iterator;
|
|
|
import java.util.List;
|
|
|
import java.util.Set;
|
|
|
|
|
@@ -64,95 +57,4 @@ public class QueryCriteria {
|
|
|
private List<OrderItem> orders;
|
|
|
/** 额外返回的扩展字段清单 */
|
|
|
private Set<String> withColumns;
|
|
|
-
|
|
|
- /**
|
|
|
- * 转换为Page分页信息
|
|
|
- * @return
|
|
|
- */
|
|
|
- public Page toPage() {
|
|
|
- // 当前页默认为1
|
|
|
- if(page == null || page < 1) {
|
|
|
- page = 1L;
|
|
|
- }
|
|
|
- // 没有每页行数表示不分页
|
|
|
- if(size == null || size < 1) {
|
|
|
- size = -1L;
|
|
|
- }
|
|
|
- Page pageInfo = new Page();
|
|
|
- pageInfo.setCurrent(page);
|
|
|
- pageInfo.setSize(size);
|
|
|
- pageInfo.setOrders(orders);
|
|
|
- pageInfo.setSearchCount(!withoutCount);
|
|
|
- return pageInfo;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 转换为查询条件
|
|
|
- * @param wrapper
|
|
|
- */
|
|
|
- public <T extends BaseEntity> void toWrapper(QueryWrapper<T> wrapper) {
|
|
|
- if(criteria == null || criteria.isEmpty(null)) {
|
|
|
- return;
|
|
|
- }
|
|
|
- Iterator<String> fields = criteria.fieldNames();
|
|
|
- // 按字段循环
|
|
|
- while(fields.hasNext()) {
|
|
|
- String field = fields.next();
|
|
|
- JsonNode fieldValue = criteria.get(field);
|
|
|
- String dbField = StrUtil.toUnderlineCase(field);
|
|
|
- // 值节点
|
|
|
- if(fieldValue.isValueNode()) {
|
|
|
- wrapper.eq(dbField, JsonNodeUtils.getNodeValue(fieldValue));
|
|
|
- continue;
|
|
|
- }
|
|
|
- // 数组
|
|
|
- if(fieldValue.isArray()) {
|
|
|
- wrapper.in(dbField, JsonNodeUtils.getListValue(fieldValue));
|
|
|
- continue;
|
|
|
- }
|
|
|
- // 对象
|
|
|
- if(fieldValue.isObject()) {
|
|
|
- Iterator<String> valueOperators = fieldValue.fieldNames();
|
|
|
- while(valueOperators.hasNext()) {
|
|
|
- String valueOperator = valueOperators.next();
|
|
|
- JsonNode operatorValue = fieldValue.get(valueOperator);
|
|
|
- addCondition(wrapper, dbField, valueOperator,operatorValue);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 按条件类型添加查询条件
|
|
|
- * @param wrapper
|
|
|
- * @param field
|
|
|
- * @param operator
|
|
|
- * @param valueNode
|
|
|
- */
|
|
|
- private <T extends BaseEntity> void addCondition(QueryWrapper<T> wrapper, String field, String operator, JsonNode valueNode) {
|
|
|
- if("$ne".equalsIgnoreCase(operator)) {
|
|
|
- wrapper.ne(field, JsonNodeUtils.getNodeValue(valueNode));
|
|
|
- } else if("$gt".equalsIgnoreCase(operator)) {
|
|
|
- wrapper.gt(field, JsonNodeUtils.getNodeValue(valueNode));
|
|
|
- } else if("$gte".equalsIgnoreCase(operator)) {
|
|
|
- wrapper.ge(field, JsonNodeUtils.getNodeValue(valueNode));
|
|
|
- } else if("$lt".equalsIgnoreCase(operator)) {
|
|
|
- wrapper.lt(field, JsonNodeUtils.getNodeValue(valueNode));
|
|
|
- } else if("$lte".equalsIgnoreCase(operator)) {
|
|
|
- wrapper.le(field, JsonNodeUtils.getNodeValue(valueNode));
|
|
|
- } else if("$in".equalsIgnoreCase(operator)) {
|
|
|
- wrapper.in(field, JsonNodeUtils.getListValue(valueNode));
|
|
|
- } else if("$like".equalsIgnoreCase(operator)) {
|
|
|
- wrapper.like(field, JsonNodeUtils.getNodeValue(valueNode));
|
|
|
- } else if("$notLike".equalsIgnoreCase(operator)) {
|
|
|
- wrapper.notLike(field, JsonNodeUtils.getNodeValue(valueNode));
|
|
|
- } else if("$null".equalsIgnoreCase(operator)) {
|
|
|
- boolean nullFlag = valueNode.booleanValue();
|
|
|
- if(nullFlag) {
|
|
|
- wrapper.isNull(field);
|
|
|
- } else {
|
|
|
- wrapper.isNotNull(field);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
}
|