提交 8f66e28d 编写于 作者: zhouweidong's avatar zhouweidong

补充自定义条件查询

上级 b9dc3d7a
......@@ -8,6 +8,10 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
import com.mongodb.QueryBuilder;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
@Slf4j
@Data
......@@ -17,4 +21,163 @@ public class QueryBuildContext extends SearchContextBase implements ISearchConte
@JSONField(serialize = false)
private QueryBuilder selectCond=new QueryBuilder();
public QueryBuilder getSearchCond(){
return this.selectCond;
}
/**
* 填充自定义查询条件
* @return
*/
public QueryBuilder getSelectCond() {
if(!ObjectUtils.isEmpty(filter)){
QueryBuilder queryBuilder=parseQueryFilter(filter);
if(!ObjectUtils.isEmpty(queryBuilder)){
selectCond.and(queryBuilder.get());
}
}
return selectCond;
}
/**
* 解析自定义查询条件
* @param queryFilter
* @return
*/
private QueryBuilder parseQueryFilter(QueryFilter queryFilter){
if( ObjectUtils.isEmpty(queryFilter.get$and()) && ObjectUtils.isEmpty(queryFilter.get$or()) && ObjectUtils.isEmpty(queryFilter.any()))
return null;
QueryBuilder rsBuilder=QueryBuilder.start();
QueryBuilder fieldBuilder=parseFieldMap(queryFilter.any());
QueryBuilder orBuilder=parseOrQueryFilter(queryFilter.get$or());
QueryBuilder andBuilder=parseAndQueryFilter(queryFilter.get$and());
if(!ObjectUtils.isEmpty(fieldBuilder)){
rsBuilder.and(fieldBuilder.get());
}
if(!ObjectUtils.isEmpty(orBuilder)){
rsBuilder.and(orBuilder.get());
}
if(!ObjectUtils.isEmpty(andBuilder)){
rsBuilder.and(andBuilder.get());
}
return rsBuilder;
}
/**
* 解析自定义条件[or]
* @param queryFilters
* @return
*/
private QueryBuilder parseOrQueryFilter(List<QueryFilter> queryFilters) {
QueryBuilder orBuilder =new QueryBuilder();
if(queryFilters==null || queryFilters.size()==0){
return null;
}
for(QueryFilter queryFilter: queryFilters){
QueryBuilder queryBuilder=parseQueryFilter(queryFilter);
if(!ObjectUtils.isEmpty(queryBuilder)){
orBuilder.or(queryBuilder.get());
}
}
return orBuilder;
}
/**
* 解析自定义条件[and]
* @param queryFilters
* @return
*/
private QueryBuilder parseAndQueryFilter(List<QueryFilter> queryFilters) {
QueryBuilder orBuilder =new QueryBuilder();
if(queryFilters==null || queryFilters.size()==0){
return null;
}
for(QueryFilter queryFilter: queryFilters){
QueryBuilder queryBuilder=parseQueryFilter(queryFilter);
if(!ObjectUtils.isEmpty(queryBuilder)){
orBuilder.and(queryBuilder.get());
}
}
return orBuilder;
}
/**
* 解析自定义条件[字段条件]
* @param fieldMap
* @return
*/
private QueryBuilder parseFieldMap(Map<String , QueryFilter.SegmentCond> fieldMap) {
if(fieldMap.size()==0)
return null;
QueryBuilder fieldBuilders=QueryBuilder.start();
for(Map.Entry<String, QueryFilter.SegmentCond> entry: fieldMap.entrySet()){
getSegmentCondSql(entry.getKey(),entry.getValue(),fieldBuilders);
}
return fieldBuilders;
}
/**
* 解析自定义条件[字段条件]
* @param fieldName
* @param segmentCond
* @param fieldBuilder
*/
private void getSegmentCondSql(String fieldName, QueryFilter.SegmentCond segmentCond,QueryBuilder fieldBuilder) {
Map<String , Object> segmentCondMap = segmentCond.any();
for(Map.Entry<String , Object> entry: segmentCondMap.entrySet()){
Object value=entry.getValue();
switch (entry.getKey()){
case "$eq":
fieldBuilder.and(QueryBuilder.start().and(fieldName).is(value).get());
break;
case "$ne":
fieldBuilder.and(QueryBuilder.start().not().and(fieldName).is(value).get());
break;
case "$gt":
fieldBuilder.and(QueryBuilder.start().and(fieldName).greaterThan(value).get());
break;
case "$gte":
fieldBuilder.and(QueryBuilder.start().and(fieldName).greaterThanEquals(value).get());
break;
case "$lt":
fieldBuilder.and(QueryBuilder.start().and(fieldName).lessThan(value).get());
break;
case "$lte":
fieldBuilder.and(QueryBuilder.start().and(fieldName).lessThanEquals(value).get());
break;
case "$null":
fieldBuilder.and(QueryBuilder.start().and(fieldName).exists(null).get());
break;
case "$notNull":
fieldBuilder.and(QueryBuilder.start().and(fieldName).exists(true).and(fieldName).notEquals("").and(fieldName).notEquals(null).get());
break;
case "$in":
fieldBuilder.and(QueryBuilder.start().and(fieldName).in(value).get());
break;
case "$notIn":
fieldBuilder.and(QueryBuilder.start().and(fieldName).not().in(value).get());
break;
case "$like":
Pattern likePattern = Pattern.compile("^.*" + value + ".*$", Pattern.CASE_INSENSITIVE);
fieldBuilder.and(QueryBuilder.start().and(fieldName).regex(likePattern).get());
break;
case "$startsWith":
Pattern startsWithPattern = Pattern.compile(value + ".*$", Pattern.CASE_INSENSITIVE);
fieldBuilder.and(QueryBuilder.start().and(fieldName).regex(startsWithPattern).get());
break;
case "$endsWith":
Pattern endsWithPattern = Pattern.compile("^.*" + value + "$", Pattern.CASE_INSENSITIVE);
fieldBuilder.and(QueryBuilder.start().and(fieldName).regex(endsWithPattern).get());
break;
case "$exists":
break;
case "$notExists":
break;
}
}
}
}
<#ibiztemplate>
TARGET=PSSYSTEM
</#ibiztemplate>
package ${pub.getPKGCodeName()}.util.filter;
import com.fasterxml.jackson.annotation.*;
import org.springframework.data.annotation.Transient;
import java.util.*;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class QueryFilter {
public static QueryFilter createQuery()
{
QueryFilter queryFilter=new QueryFilter();
return queryFilter;
}
public QueryFilter eq(String column,Object value) {
return op(column,SegmentCond.eq(value));
}
public QueryFilter ne(String column,Object value) {
return op(column,SegmentCond.ne(value));
}
public QueryFilter gt(String column,Object value) {
return op(column,SegmentCond.gt(value));
}
public QueryFilter ge(String column,Object value) {
return op(column,SegmentCond.ge(value));
}
public QueryFilter lt(String column,Object value) {
return op(column,SegmentCond.lt(value));
}
public QueryFilter le(String column,Object value) {
return op(column,SegmentCond.le(value));
}
public QueryFilter isnull(String column) {
return op(column,SegmentCond.isnull());
}
public QueryFilter isnotnull(String column) {
return op(column,SegmentCond.isnotnull());
}
public QueryFilter in(String column,Collection value) {
return op(column,SegmentCond.in(value));
}
public QueryFilter notin(String column,Collection value) {
return op(column,SegmentCond.notin(value));
}
public QueryFilter like(String column,String value) {
return op(column,SegmentCond.like(value));
}
public QueryFilter startsWith(String column,String value) {
return op(column,SegmentCond.startsWith(value));
}
public QueryFilter endsWith(String column,String value) {
return op(column,SegmentCond.endsWith(value));
}
public QueryFilter between(String column,Object from,Object to) {
return op(column,SegmentCond.between(from,to));
}
@JsonIgnore
@Transient
private Map<String,SegmentCond> map;
private Map<String,SegmentCond> getMap()
{
if(map==null)
map=new LinkedHashMap<>();
return map;
}
@JsonAnyGetter
public Map<String , SegmentCond> any() {
return getMap();
}
@JsonAnySetter
public void set(String column, SegmentCond value) {
getMap().put(column,value);
}
@JsonProperty(index = 999)
private List<QueryFilter> $or;
public List<QueryFilter> get$or() {
return $or;
}
public void set$or(List<QueryFilter> $or) {
this.$or = $or;
}
public QueryFilter or(QueryFilter... ors)
{
if ($or == null) {
$or = new ArrayList();
}
Collections.addAll($or, ors);
return this;
}
@JsonProperty(index = 999)
private List<QueryFilter> $and;
public List<QueryFilter> get$and() {
return $and;
}
public void set$and(List<QueryFilter> $and) {
this.$and = $and;
}
public QueryFilter and(QueryFilter... ands)
{
if ($and == null) {
$and = new ArrayList();
}
Collections.addAll($and, ands);
return this;
}
@Override
public String toString() {
return "QueryFilter{" +
"map=" + map +
", $or=" + $or +
'}';
}
private QueryFilter op(String column, SegmentCond segmentCond) {
if(this.getMap().containsKey(column))
((SegmentCond)this.getMap().get(column)).getMap().putAll(segmentCond.getMap());
else
this.getMap().put(column,segmentCond);
return this;
}
public static class SegmentCond {
@JsonIgnore
@Transient
private Map<String,Object> map;
@Override
public String toString() {
return "SegmentCond{" +
"map=" + map +
'}';
}
private Map<String,Object> getMap()
{
if(map==null)
map=new LinkedHashMap<>();
return map;
}
@JsonAnyGetter
public Map<String , Object> any() {
return getMap();
}
@JsonAnySetter
public void set(String column, Object value) {
getMap().put(column,value);
}
public static SegmentCond eq(Object value) {
return op(Segment.EQ,value);
}
public static SegmentCond ne(Object value) {
return op(Segment.NE,value);
}
public static SegmentCond gt(Object value) {
return op(Segment.GT,value);
}
public static SegmentCond ge(Object value) {
return op(Segment.GE,value);
}
public static SegmentCond lt(Object value) {
return op(Segment.LT,value);
}
public static SegmentCond le(Object value) {
return op(Segment.LE,value);
}
public static SegmentCond isnull() {
return op(Segment.IS_NULL,true);
}
public static SegmentCond isnotnull() {
return op(Segment.IS_NOT_NULL,true);
}
public static SegmentCond in(Collection value) {
return op(Segment.IN,value);
}
public static SegmentCond notin(Collection value) {
return op(Segment.NOTIN,value);
}
public static SegmentCond like(String value) {
return op(Segment.LIKE,value);
}
public static SegmentCond startsWith(String value) {
return op(Segment.LEFTLIKE,value);
}
public static SegmentCond endsWith(String value) {
return op(Segment.RIGHTLIKE,value);
}
public static SegmentCond between(Object from,Object to) {
return op(Segment.GE,from).op(Segment.LT,to);
}
private static SegmentCond op(Segment segment,Object value) {
SegmentCond segmentCond=new SegmentCond();
segmentCond.getMap().put(segment.keyword,value);
return segmentCond;
}
}
public enum Segment {
AND("$and"),
OR("$or"),
EQ("$eq"),
NE("$ne"),
GT("$gt"),
GE("$gte"),
LT("$lt"),
LE("$lte"),
IS_NULL("$null"),
IS_NOT_NULL("$notNull"),
IN("$in"),
NOTIN("$notIn"),
LIKE("$like"),
LEFTLIKE("$startsWith"),
RIGHTLIKE("$endsWith"),
EXISTS("$exists"),
NOTEXISTS("$notExists");
private final String keyword;
Segment(final String keyword) {
this.keyword = keyword;
}
}
/*
SQL: (field1>1 and field2='3' and (field3 like "a" or (field4 is not null and field5 in ['11','12']) )
JAVA: QueryFilter filter=QueryFilter.createQuery()
.gt("field1",1)
.eq("field2",'3')
.or(QueryFilter.createQuery().like("field3","a")
,QueryFilter.createQuery().isnotnull("field4").in("field5",Arrays.asList("11","12")));
JSON: {
"$or":[
{
"field3":{"$like":"a"}
},
{
"field4":{ "$notNull":true},
"field5":{"$in":["11","12"]}
}],
"field1":{"$gt":1},
"field2":{"$eq":"3" }
}
*/
}
......@@ -13,11 +13,9 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Sort;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.*;
import java.util.function.Consumer;
@Slf4j
@Data
......@@ -76,4 +74,150 @@ public class QueryWrapperContext<T> extends SearchContextBase implements ISearch
return page;
}
public QueryWrapper<T> getSearchCond(){
return this.selectCond;
}
/**
* 填充自定义查询条件
* @return
*/
public QueryWrapper<T> getSelectCond() {
if(!ObjectUtils.isEmpty(filter)){
Consumer queryWrapper = parseQueryFilter(filter);
if(!ObjectUtils.isEmpty(queryWrapper)){
selectCond.and(queryWrapper);
}
}
return selectCond;
}
/**
* 解析自定义查询条件
* @param queryFilter
* @return
*/
private Consumer<QueryWrapper<T>> parseQueryFilter(QueryFilter queryFilter){
if(queryFilter.any().size()==0 && queryFilter.get$or()==null && queryFilter.get$and()==null)
return null;
Consumer<QueryWrapper<T>> consumer = queryWrapper -> {
Consumer fieldConsumer=parseFieldMap(queryFilter.any());
Consumer orConsumer=parseOrQueryFilter(queryFilter.get$or());
Consumer andConsumer=parseAndQueryFilter(queryFilter.get$and());
if(!ObjectUtils.isEmpty(fieldConsumer)){
queryWrapper.and(fieldConsumer);
}
if(!ObjectUtils.isEmpty(orConsumer)){
queryWrapper.and(orConsumer);
}
if(!ObjectUtils.isEmpty(andConsumer)){
queryWrapper.and(andConsumer);
}
};
return consumer;
}
/**
* 解析自定义条件[or]
* @param queryFilters
* @return
*/
private Consumer<QueryWrapper<T>> parseOrQueryFilter(List<QueryFilter> queryFilters) {
if(queryFilters==null || queryFilters.size()==0)
return null;
Consumer<QueryWrapper<T>> consumer = queryWrapper -> {
for(QueryFilter queryFilter: queryFilters){
Consumer tempQueryWrapper=parseQueryFilter(queryFilter);
queryWrapper.or(tempQueryWrapper);
}
};
return consumer;
}
/**
* 解析自定义条件[and]
* @param queryFilters
* @return
*/
private Consumer<QueryWrapper<T>> parseAndQueryFilter(List<QueryFilter> queryFilters) {
if(queryFilters==null || queryFilters.size()==0)
return null;
Consumer<QueryWrapper<T>> consumer = queryWrapper -> {
for(QueryFilter queryFilter: queryFilters){
Consumer tempQueryWrapper=parseQueryFilter(queryFilter);
queryWrapper.and(tempQueryWrapper);
}
};
return consumer;
}
/**
* 解析自定义条件[字段条件]
* @param fieldMap
* @return
*/
private Consumer<QueryWrapper<T>> parseFieldMap(Map<String , QueryFilter.SegmentCond> fieldMap) {
if(fieldMap.size()==0)
return null;
Consumer<QueryWrapper<T>> consumer = queryWrapper -> {
for(Map.Entry<String, QueryFilter.SegmentCond> field: fieldMap.entrySet()){
String fieldName=field.getKey();
QueryFilter.SegmentCond segmentCond=field.getValue();
Map<String , Object> segmentCondMap = segmentCond.any();
for(Map.Entry<String , Object> fieldCond: segmentCondMap.entrySet()){
Object value=fieldCond.getValue();
switch (fieldCond.getKey()){
case "$eq":
queryWrapper.eq(fieldName,value);
break;
case "$ne":
queryWrapper.ne(fieldName,value);
break;
case "$gt":
queryWrapper.gt(fieldName,value);
break;
case "$gte":
queryWrapper.ge(fieldName,value);
break;
case "$lt":
queryWrapper.lt(fieldName,value);
break;
case "$lte":
queryWrapper.le(fieldName,value);
break;
case "$null":
queryWrapper.isNull(fieldName);
break;
case "$notNull":
queryWrapper.isNotNull(fieldName);
break;
case "$in":
queryWrapper.in(fieldName,(Collection)value);
break;
case "$notIn":
queryWrapper.notIn(fieldName,(Collection)value);
break;
case "$like":
queryWrapper.like(fieldName,value);
break;
case "$startsWith":
queryWrapper.likeRight(fieldName,value);
break;
case "$endsWith":
queryWrapper.likeLeft(fieldName,value);
break;
case "$exists":
break;
case "$notExists":
break;
}
}
}
};
return consumer;
}
}
......@@ -15,9 +15,7 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;
import org.springframework.util.ObjectUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
@Slf4j
@Data
......@@ -38,7 +36,11 @@ public class SearchContextBase implements ISearchContext{
*/
@JsonProperty("query")
public String query;
/**
* 条件表达式
*/
@JsonProperty("filter")
public QueryFilter filter;
/**
* 数据查询
*/
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册