package com.vci.starter.web.pagemodel;
|
|
|
import com.alibaba.fastjson.annotation.JSONField;
|
import org.apache.commons.lang3.StringUtils;
|
|
import java.util.*;
|
|
/**
|
* 查询条件 ,经常用于controller的方法中,用于接收前端的请求参数
|
* 互联网产品上不推荐这样的方式,不方便维护
|
* 但是在传统行业因为考虑到查询条件过多,查询的方式复杂多样化,因此建议使用这种方式
|
* 前端需要传递的参数方式应该为‘conditionMap['xxxx']=yyy; xxx是属性的名称,yyy是查询条件的值
|
* 这个对象里已经包含了分页信息
|
*/
|
public class BaseQueryObject implements java.io.Serializable{
|
|
/**
|
* 默认构造函数
|
*/
|
public BaseQueryObject(){
|
|
}
|
|
/**
|
* 构造函数,添加查询条件
|
* @param conditionMap 查询条件
|
*/
|
public BaseQueryObject(Map<String,String> conditionMap){
|
this.setConditionMap(conditionMap);
|
}
|
|
/**
|
* 构造函数,设置分页数
|
* @param limit 每页显示最大数
|
*/
|
public BaseQueryObject(int limit){
|
this.setLimit(limit);
|
}
|
|
/**
|
* 构造函数,添加查询条件和分页数
|
* @param conditionMap 查询条件
|
* @param limit 每页显示最大数
|
*/
|
public BaseQueryObject(Map<String,String> conditionMap,int limit){
|
this.setConditionMap(conditionMap);
|
this.setLimit(limit);
|
}
|
|
/**
|
* 禁止修改这个值
|
*/
|
@JSONField(serialize = false,deserialize = false)
|
private static final long serialVersionUID = 5608634730007623041L;
|
/**
|
* 查询条件的映射关系
|
*/
|
@JSONField()
|
private Map<String,String> conditionMap = new HashMap<String, String>();
|
|
/**
|
* 当前页
|
*/
|
@JSONField()
|
private int page = 1;
|
// 当前页
|
/**
|
* 排序字段
|
*/
|
@JSONField()
|
private String sort;
|
// 排序字段 ---前端只支持使用一个字段来排序,但是后台实际可以使用多个字段来排序,所以可以使用逗号分割
|
/**
|
* 排序类型
|
*/
|
@JSONField()
|
private String order;
|
// asc/desc,如果有多个排序字段时,这里也需要用逗号分割,且与sort的位置对应
|
|
/**
|
* 每页显示页数
|
*/
|
@JSONField()
|
private int limit = 25;//每页显示的条数
|
|
/**
|
* 获取查询条件
|
* @return 查询条件映射,key是字段英文名称,value是查询的值
|
*/
|
public Map<String, String> getConditionMap() {
|
return conditionMap;
|
}
|
|
/**
|
* 根据昵称获取查询条件
|
* @param nick 表格的昵称
|
* @return 带有.的不添加表格昵称
|
*/
|
public Map<String,String> getConditionMapByNick(String nick){
|
if(conditionMap!=null){
|
Map<String,String> conditionMapHasNick = new HashMap<>();
|
conditionMap.forEach( (k,v) -> {
|
if(!k.contains(".")){
|
conditionMapHasNick.put(nick + "." + k,v);
|
}else{
|
conditionMapHasNick.put(k,v);
|
}
|
});
|
return conditionMapHasNick;
|
}
|
return null;
|
}
|
|
/**
|
* 设置查询条件
|
* @param conditionMap 查询条件的映射关系
|
*/
|
public void setConditionMap(Map<String, String> conditionMap) {
|
this.conditionMap = conditionMap;
|
}
|
|
public int getPage() {
|
return page;
|
}
|
|
public void setPage(int page) {
|
this.page = page;
|
}
|
|
public String getSort() {
|
return sort;
|
}
|
|
public void setSort(String sort) {
|
this.sort = sort;
|
}
|
|
public String getOrder() {
|
return order;
|
}
|
|
public void setOrder(String order) {
|
this.order = order;
|
}
|
|
public int getLimit() {
|
return limit;
|
}
|
|
public void setLimit(int limit) {
|
this.limit = limit;
|
}
|
|
/**
|
* 获取查询对象中的分页对象
|
* @return 分页对象
|
*/
|
@JSONField(serialize = false,deserialize = false)
|
public PageHelper getPageHelper(){
|
PageHelper pageHelper = new PageHelper(limit);
|
pageHelper.setPage(getPage());
|
pageHelper.setSort(getSort());
|
pageHelper.setOrder(getOrder());
|
return pageHelper;
|
}
|
|
/**
|
* 增加排序
|
* @param sort 排序字段
|
* @param order 排序方式
|
* @return 当前对象
|
*/
|
public BaseQueryObject addSort(String sort, String order){
|
this.setSort(StringUtils.isBlank(this.getSort())?sort:(this.getSort() + "," + sort) );
|
this.setOrder(StringUtils.isBlank(this.getOrder())?order:(this.getOrder() + "," + order));
|
return this;
|
}
|
|
/**
|
* 增加查询查询条件
|
* @param key 查询条件
|
* @param value 值
|
* @return 当前对象
|
*/
|
public BaseQueryObject addCondition(String key,String value){
|
if(this.getConditionMap() == null){
|
this.conditionMap = new HashMap<>();
|
}
|
this.conditionMap.put(key,value);
|
return this;
|
}
|
|
/**
|
* 设置分页的信息
|
* @param pageHelper 分页对象
|
* @return 当前对象
|
*/
|
public BaseQueryObject page(PageHelper pageHelper){
|
this.setSort(pageHelper.getSort());
|
this.setOrder(pageHelper.getOrder());
|
this.setPage(pageHelper.getPage());
|
this.setLimit(pageHelper.getLimit());
|
return this;
|
}
|
|
/**
|
* 获取feign请求的参数映射,一般在get方法的时候才使用
|
* @return 查询条件,分页和排序等信息,key是参数的对象,value是参数的值
|
*/
|
@JSONField(serialize = false,deserialize = false)
|
public Map<String,String> getFeignRequestMap(){
|
Map<String,String> feignRequestMap = new HashMap<>();
|
//查询条件
|
if(this.conditionMap!=null){
|
this.getConditionMap().forEach( (k,v) -> {
|
feignRequestMap.put( "conditionMap[\"" + k + "\"]",v);
|
});
|
}
|
//分页
|
feignRequestMap.put("page",this.getPage() + "");
|
feignRequestMap.put("limit",this.getLimit() + "");
|
//排序
|
feignRequestMap.put("sort",this.getSort());
|
feignRequestMap.put("order",this.getOrder());
|
return feignRequestMap;
|
}
|
|
@Override
|
public String toString() {
|
return "BaseQueryObject{" +
|
"conditionMap=" + conditionMap +
|
", page=" + page +
|
", sort='" + sort + '\'' +
|
", order='" + order + '\'' +
|
", limit=" + limit +
|
'}';
|
}
|
}
|