Browse Source

优化动态查询条件

zhou-hao 6 years ago
parent
commit
5f518a9429

+ 58 - 0
hsweb-commons/hsweb-commons-entity/src/main/java/org/hswebframework/web/commons/entity/param/QueryParamEntity.java

@@ -1,9 +1,16 @@
 package org.hswebframework.web.commons.entity.param;
 
+import org.hswebframework.ezorm.core.NestConditional;
 import org.hswebframework.ezorm.core.dsl.Query;
 import org.hswebframework.ezorm.core.param.QueryParam;
+import org.hswebframework.ezorm.core.param.Term;
 import org.hswebframework.web.commons.entity.Entity;
 import org.hswebframework.web.commons.entity.QueryEntity;
+import org.springframework.util.CollectionUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.Consumer;
 
 /**
  * 查询参数实体,使用<a href="https://github.com/hs-web/hsweb-easy-orm">easyorm</a>进行动态查询参数构建<br>
@@ -59,6 +66,57 @@ public class QueryParamEntity extends QueryParam implements QueryEntity {
         return Query.empty(new QueryParamEntity());
     }
 
+    /**
+     * @since 3.0.4
+     */
+    public <T> Query<T, QueryParamEntity> toQuery() {
+        return Query.empty(this);
+    }
+
+    /**
+     * 将已有的条件包装到一个嵌套的条件里,并返回一个Query对象.例如:
+     * <pre>
+     *     entity.toNestQuery().and("userId",userId);
+     * </pre>
+     * <p>
+     * 原有条件: name=? or type=?
+     * <p>
+     * 执行后条件: (name=? or type=?) and userId=?
+     *
+     * @see this#toNestQuery(Consumer)
+     * @since 3.0.4
+     */
+    public <T> Query<T, QueryParamEntity> toNestQuery() {
+        return toNestQuery(null);
+    }
+
+    /**
+     * 将已有的条件包装到一个嵌套的条件里,并返回一个Query对象.例如:
+     * <pre>
+     *     entity.toNestQuery(query->query.and("userId",userId));
+     * </pre>
+     * <p>
+     * 原有条件: name=? or type=?
+     * <p>
+     * 执行后条件: userId=? (name=? or type=?)
+     *
+     * @param before 在包装之前执行,将条件包装到已有条件之前
+     * @since 3.0.4
+     */
+    public <T> Query<T, QueryParamEntity> toNestQuery(Consumer<Query<T, QueryParamEntity>> before) {
+        List<Term> terms = getTerms();
+        setTerms(new ArrayList<>());
+        Query<T, QueryParamEntity> query = toQuery();
+        if (null != before) {
+            before.accept(query);
+        }
+        return query
+                .nest()
+                .each(terms, NestConditional::accept)
+                .end();
+    }
+
+
     @Override
     public String toString() {
         return toHttpQueryParamString();