ludc
2024-06-26 79dd20bae9e8af17d5d66b67da4ca6ebc56cd9dd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
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<>();
 
    /**
     * 当前页
     */
    @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 conditionMap 查询条件的映射关系
     */
    public void setConditionMap(Map<String, String> conditionMap) {
        this.conditionMap = 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;
    }
 
    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 +
                '}';
    }
 
}