|
@@ -0,0 +1,155 @@
|
|
|
+package com.persagy.dc.common.model;
|
|
|
+
|
|
|
+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.*;
|
|
|
+import lombok.Data;
|
|
|
+import org.apache.poi.ss.formula.functions.T;
|
|
|
+
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 通用查询参数 - 仅用于接收参数
|
|
|
+ * 兼容数据中台查询规则,将数据中台查询条件转换为MyBatisPlus查询条件
|
|
|
+ * <pre>
|
|
|
+ * {
|
|
|
+ * "page": 1,
|
|
|
+ * "size": 10,
|
|
|
+ * "onlyCount": false,
|
|
|
+ * "criteria": {
|
|
|
+ * "name": "zhangsan",
|
|
|
+ * "name": [ "zhangsan", "lisi" ],
|
|
|
+ * "age": {
|
|
|
+ * "$gt": 18,
|
|
|
+ * "$lt": 35
|
|
|
+ * },
|
|
|
+ * "gender": {
|
|
|
+ * "$null": false
|
|
|
+ * }
|
|
|
+ * },
|
|
|
+ * "orders": [
|
|
|
+ * {
|
|
|
+ * "column": "name",
|
|
|
+ * "asc": false
|
|
|
+ * }
|
|
|
+ * ],
|
|
|
+ * "withColumns": [
|
|
|
+ * "name", "age"
|
|
|
+ * ]
|
|
|
+ * }
|
|
|
+ * </pre>
|
|
|
+ * @author yaoll
|
|
|
+ * @date 2020-09-04
|
|
|
+ */
|
|
|
+@Data
|
|
|
+public class QueryCriteria {
|
|
|
+
|
|
|
+ /** 分页页码,从 1 开始 */
|
|
|
+ private Long page;
|
|
|
+ /** 每页行数 */
|
|
|
+ private Long size;
|
|
|
+ /** 是否只统计总数 */
|
|
|
+ private boolean onlyCount = false;
|
|
|
+ /** 是否不统计总数 */
|
|
|
+ private boolean withoutCount = false;
|
|
|
+ /** 查询条件 */
|
|
|
+ private ObjectNode criteria;
|
|
|
+ /** 排序条件 */
|
|
|
+ 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 = Long.MAX_VALUE;
|
|
|
+ }
|
|
|
+ Page pageInfo = new Page();
|
|
|
+ pageInfo.setCurrent(page);
|
|
|
+ pageInfo.setSize(size);
|
|
|
+ pageInfo.setOrders(orders);
|
|
|
+ pageInfo.setSearchCount(!withoutCount);
|
|
|
+ return pageInfo;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 转换为查询条件
|
|
|
+ * @param wrapper
|
|
|
+ */
|
|
|
+ public 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);
|
|
|
+ // 值节点
|
|
|
+ if(fieldValue.isValueNode()) {
|
|
|
+ wrapper.eq(field, JsonNodeUtils.getNodeValue(fieldValue));
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 数组
|
|
|
+ if(fieldValue.isArray()) {
|
|
|
+ wrapper.in(field, JsonNodeUtils.getListValue(fieldValue));
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 对象
|
|
|
+ if(fieldValue.isObject()) {
|
|
|
+ Iterator<String> valueOperators = fieldValue.fieldNames();
|
|
|
+ while(valueOperators.hasNext()) {
|
|
|
+ String valueOperator = valueOperators.next();
|
|
|
+ JsonNode operatorValue = criteria.get(field);
|
|
|
+ addCondition(wrapper, field, valueOperator,operatorValue);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 按条件类型添加查询条件
|
|
|
+ * @param wrapper
|
|
|
+ * @param field
|
|
|
+ * @param operator
|
|
|
+ * @param valueNode
|
|
|
+ */
|
|
|
+ private 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|