SearchController.java
8.6 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package com.yoho.search.restapi;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yoho.search.service.SearchProductsService;
import com.yoho.search.service.SearchService;
import com.yoho.search.service.SearchShopService;
import com.yoho.search.service.SearchSortSizeService;
import com.yoho.search.service.SearchSuggestService;
import com.yoho.search.utils.JsonUtil;
import com.yoho.search.vo.SearchApiResult;
import com.yoho.search.vo.SizeSortReqBO;
@Controller
public class SearchController {
private static Logger logger = LoggerFactory.getLogger(SearchController.class);
@Autowired
private SearchService searchService;
@Autowired
private SearchSortSizeService searchSortSizeService;
@Autowired
private SearchShopService searchShopService;
@Autowired
private SearchSuggestService searchSuggestService;
@Autowired
private SearchProductsService searchProductsService;
/**
* 搜索商品列表以及聚合结果的接口
*
* @param request
* @return
*/
@RequestMapping(method = RequestMethod.GET, value = "/search")
@ResponseBody
public Map<String, Object> searchProducts(HttpServletRequest request) {
Map<String, String> paramMap = this.transParamType(request);
try {
return searchProductsService.searchProducts(paramMap, null);
} catch (Exception e) {
return errorReturn("searchProducts", paramMap, e);
}
}
/**
* 获取按折扣聚合的结果
*
* @param request
* @return
*/
@RequestMapping(method = RequestMethod.GET, value = "/discount")
@ResponseBody
public Map<String, Object> discount(HttpServletRequest request) {
Map<String, String> paramMap = this.transParamType(request);
try {
return searchService.searchProductsDiscount(paramMap);
} catch (Exception e) {
return errorReturn("discount", paramMap, e);
}
}
/**
* 获取按上架日期聚合的结果
*
* @param request
* @return
*/
@RequestMapping(method = RequestMethod.GET, value = "/recent")
@ResponseBody
public Map<String, Object> recent(HttpServletRequest request) {
Map<String, String> paramMap = this.transParamType(request);
try {
return searchService.searchProductsRecent(paramMap);
} catch (Exception e) {
return errorReturn("recent", paramMap, e);
}
}
/**
* 获取最新上架的商品列表【已过滤相同的品牌】
*
* @param request
* @return
*/
@RequestMapping(method = RequestMethod.GET, value = "/new-shelve")
@ResponseBody
public Map<String, Object> searchNewestProductWithDiffBrand(HttpServletRequest request) {
Map<String, String> paramMap = this.transParamType(request);
try {
return searchService.searchNewestProductWithDiffBrand(paramMap);
} catch (Exception e) {
return errorReturn("searchNewestProductWithDiffBrand", paramMap, e);
}
}
/**
* 某些品牌最新上架的商品【按order参数对商品排序】
*
* @param request
* @return
*/
@RequestMapping(method = RequestMethod.GET, value = "/new_product")
@ResponseBody
public Map<String, Object> searchNewestProductWithParamBrand(HttpServletRequest request) {
Map<String, String> paramMap = this.transParamType(request);
try {
return searchService.searchNewestProductWithParamBrand(paramMap);
} catch (Exception e) {
return errorReturn("newProduct", paramMap, e);
}
}
/**
* 获取按品牌聚合的结果[品牌列表]
*
* @param request
* @return
*/
@RequestMapping(method = RequestMethod.GET, value = "/brands")
@ResponseBody
public Map<String, Object> brands(HttpServletRequest request) {
Map<String, String> paramMap = this.transParamType(request);
try {
return searchService.brands(paramMap);
} catch (Exception e) {
return errorReturn("brands", paramMap, e);
}
}
/**
* 批量获取brandIds对应的商品数量和商品列表[我收藏的品牌]
*
* @param request
* @return
*/
@RequestMapping(method = RequestMethod.GET, value = "/group_brands")
@ResponseBody
public Map<String, Object> groupBrands(HttpServletRequest request) {
Map<String, String> paramMap = this.transParamType(request);
try {
return searchService.groupBrands(paramMap);
} catch (Exception e) {
return errorReturn("groupBrands", paramMap, e);
}
}
/**
* 对分类进行分组查询
*
* @param request
* @return
*/
@RequestMapping(method = RequestMethod.GET, value = "/sortgroup")
@ResponseBody
public Map<String, Object> sortGroup(HttpServletRequest request) {
Map<String, String> paramMap = this.transParamType(request);
try {
return searchService.sortGroup(paramMap);
} catch (Exception e) {
return errorReturn("sortGroup", paramMap, e);
}
}
/**
* 根据query关键字获取商品总数
*
* @param request
* @return
*/
@RequestMapping(method = RequestMethod.GET, value = "/count")
@ResponseBody
public long searchCount(HttpServletRequest request) {
Map<String, String> paramMap = this.transParamType(request);
try {
return searchService.searchCount(paramMap);
} catch (Exception e) {
logger.error("[※查询]失败:" + e.getMessage(), e);
return 0;
}
}
/**
* 搜索建议接口
*
* @param request
* @return
*/
@RequestMapping(method = RequestMethod.GET, value = "/suggest")
@ResponseBody
public Map<String, Object> suggest(HttpServletRequest request) {
Map<String, String> paramMap = this.transParamType(request);
try {
return searchSuggestService.suggest(paramMap);
} catch (Exception e) {
return errorReturn("suggest", paramMap, e);
}
}
@RequestMapping(method = RequestMethod.GET, value = "/shops")
@ResponseBody
public Map<String, Object> shops(HttpServletRequest request) {
Map<String, String> paramMap = this.transParamType(request);
try {
return searchShopService.searchShops(paramMap);
} catch (Exception e) {
return errorReturn("shops", paramMap, e);
}
}
@RequestMapping(method = RequestMethod.GET, value = "/group_shops")
@ResponseBody
public Map<String, Object> groupShops(HttpServletRequest request) {
Map<String, String> paramMap = this.transParamType(request);
try {
return searchShopService.groupShops(paramMap);
} catch (Exception e) {
return errorReturn("groupShops", paramMap, e);
}
}
/**
* 获取分类,以及分类下的型号列表(with storage_sku)
*
* @param request
* @return
*/
@RequestMapping(method = RequestMethod.GET, value = "/sort_sizes")
@ResponseBody
public SearchApiResult sort_sizes(@ModelAttribute SizeSortReqBO sizeSortReqBO) {
return searchSortSizeService.sortSizes(sizeSortReqBO);
}
/**
* 按分类和型号获取商品(with storage_sku)
*
* @param request
* @return
*/
@RequestMapping(method = RequestMethod.GET, value = "/sort_size_products")
@ResponseBody
public Map<String, Object> sort_size_products(HttpServletRequest request) {
Map<String, String> paramMap = this.transParamType(request);
try {
return searchSortSizeService.sortSizeProducts(paramMap);
} catch (Exception e) {
return errorReturn("sort_size_products", paramMap, e);
}
}
/**
* 将HttpServletRequest中被锁定的ParameterMap转化为普通的HashMap
* */
@SuppressWarnings("unchecked")
private Map<String, String> transParamType(HttpServletRequest request) {
Map<String, Object> paramMap = request.getParameterMap();
Map<String, String> resultMap = new HashMap<String, String>();
Iterator<String> itKeys = paramMap.keySet().iterator();
while (itKeys.hasNext()) {
String key = itKeys.next();
String val = ((String[]) paramMap.get(key))[0];
resultMap.put(key, val);
}
return resultMap;
}
private Map<String, Object> errorReturn(final String funName, final Map<String, ?> paramMap, final Exception e) {
logger.error("[※查询]失败:[func={}][param={}][message={}][stack={}]", funName, JsonUtil.toJson(paramMap), e.getMessage(),e);
Map<String, Object> rtnMap = new HashMap<String, Object>();
rtnMap.put("code", 400);
rtnMap.put("status", "error");
rtnMap.put("errmsg", e.getMessage());
return rtnMap;
}
}