plus.extvos.restlet.QuerySet Maven / Gradle / Ivy
The newest version!
package plus.extvos.restlet;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import plus.extvos.common.exception.ResultException;
import plus.extvos.restlet.service.QueryBuilder;
import plus.extvos.restlet.utils.FieldConvertor;
import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author Mingcai SHEN
*/
public class QuerySet implements Serializable {
private static final String OP_NOT = "not";
private static final String OP_NOTNULL = "notnull";
private static final String OP_ISNULL = "isnull";
private static final String OP_CONTAINS = "contains";
private static final String OP_START_WITH = "startWith";
private static final String OP_END_WITH = "endWith";
private static final String OP_GT = "gt";
private static final String OP_GTE = "gte";
private static final String OP_LT = "lt";
private static final String OP_LTE = "lte";
private static final String OP_RANGE = "range";
private static final String OP_BETWEEN = "between";
private static final String OP_IN = "in";
private static final String OP_OR = "or";
private static final String OP_AND = "and";
private static final Logger log = LoggerFactory.getLogger(QuerySet.class);
private long page;
private long pageSize;
private Map queries;
private Set orderBy;
private Set includeCols;
public Set getIncludeCols() {
return includeCols;
}
public void setIncludeCols(Set includeCols) {
this.includeCols = includeCols;
}
public void updateIncludeCols(Set incs) {
if (null == incs || incs.isEmpty()) {
return;
}
if (null != includeCols) {
includeCols.addAll(incs);
} else {
includeCols = incs;
}
}
public Set getExcludeCols() {
return excludeCols;
}
public void setExcludeCols(Set excludeCols) {
this.excludeCols = excludeCols;
}
public void updateExcludeCols(Set excs) {
if (null == excs || excs.isEmpty()) {
return;
}
if (excludeCols != null) {
excludeCols.addAll(excs);
} else {
excludeCols = excs;
}
}
private Set excludeCols;
private TableInfo tableInfo;
private Map columnMap;
private Map> columnTypeMap;
public Set columns() {
if (includeCols != null && includeCols.size() > 0) {
includeCols.forEach((String k) -> {
if (!columnMap.containsKey(k)) {
includeCols.remove(k);
}
});
if (includeCols.size() > 0) {
return includeCols;
}
}
if (excludeCols != null && excludeCols.size() > 0) {
Set ss = new HashSet<>();
ss.add(tableInfo.getKeyColumn());
tableInfo.getFieldList().forEach((TableFieldInfo f) -> {
if (!(excludeCols.contains(f.getProperty()) || excludeCols.contains(f.getColumn()))) {
ss.add(f.getColumn());
}
});
return ss;
}
Set ss = new HashSet<>();
ss.add(tableInfo.getKeyColumn());
tableInfo.getFieldList().forEach((TableFieldInfo f) -> {
ss.add(f.getColumn());
});
return ss;
}
public Set getOrderBy() {
return orderBy;
}
public void setOrderBy(Set orderBy) {
this.orderBy = orderBy.stream().map(s -> {
String ss = s;
String ps = "";
if (s.startsWith("-")) {
ps = "-";
ss = s.substring(1);
}
if (columnMap.containsKey(ss)) {
ss = columnMap.get(ss);
}
return ps + ss;
}).collect(Collectors.toSet());
}
public QuerySet(TableInfo tableInfo) {
setTableInfo(tableInfo);
}
public QuerySet(TableInfo tableInfo, long page, long pageSize) {
setTableInfo(tableInfo);
this.page = page;
this.pageSize = pageSize;
}
public QuerySet(TableInfo tableInfo, Map map) {
setTableInfo(tableInfo);
this.putAll(map);
}
public QuerySet(TableInfo tableInfo, long page, long pageSize, Map map) {
log.debug("QuerySet({},{},{})", page, pageSize, map);
setTableInfo(tableInfo);
this.page = page;
this.pageSize = pageSize;
this.putAll(map);
}
public void setTableInfo(TableInfo tableInfo) {
this.tableInfo = tableInfo;
if (this.tableInfo != null) {
columnMap = new LinkedHashMap<>();
columnTypeMap = new LinkedHashMap<>();
columnMap.put(tableInfo.getKeyColumn(), tableInfo.getKeyColumn());
columnMap.put(tableInfo.getKeyProperty(), tableInfo.getKeyColumn());
columnTypeMap.put(tableInfo.getKeyColumn(), tableInfo.getKeyType());
tableInfo.getFieldList().forEach((TableFieldInfo f) -> {
columnMap.put(f.getProperty(), f.getColumn());
columnMap.put(f.getColumn(), f.getColumn());
columnTypeMap.put(f.getColumn(), f.getPropertyType());
});
}
}
public long getPage() {
log.debug("getOffset {}", page);
return page;
}
public void setPage(long page) {
this.page = page;
}
public long getPageSize() {
log.debug("getLimit {}", pageSize);
return pageSize;
}
public void setPageSize(long pageSize) {
this.pageSize = pageSize;
}
public Map getQueries() {
return queries;
}
public void setQueries(Map queries) {
this.queries = queries;
}
public boolean hasKey(String k) {
return this.queries != null && this.queries.containsKey(k);
}
public int size() {
if (this.queries == null) {
return 0;
} else {
return this.queries.size();
}
}
public boolean isEmpty() {
return this.queries == null || this.queries.isEmpty();
}
public boolean containsKey(Object key) {
return this.queries != null && this.queries.containsKey(key);
}
public boolean containsValue(Object value) {
return this.queries != null && this.queries.containsValue(value);
}
public Object get(Object key) {
if (this.queries == null) {
return null;
}
return this.queries.get(key);
}
public Object put(String key, Object value) {
if (this.queries == null) {
this.queries = new HashMap<>();
}
return this.queries.put(key, value);
}
public Object remove(Object key) {
if (this.queries == null) {
return null;
} else {
return this.queries.remove(key);
}
}
public void putAll(Map extends String, ?> m) {
if (this.queries == null) {
this.queries = new HashMap<>();
}
this.queries.putAll(m);
}
public void clear() {
if (this.queries != null) {
this.queries.clear();
}
}
public Set keys() {
if (this.queries == null) {
return new HashSet<>();
} else {
return this.queries.keySet();
}
}
public Collection