SearchController.java
7.43 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
package com.yoho.search.restapi;
import com.yoho.search.service.SearchService;
import com.yoho.search.utils.JsonUtil;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* 搜索web端
*/
@Controller
@RequestMapping("/")
public class SearchController {
private static Logger logger = LoggerFactory.getLogger(SearchController.class);
@Autowired
SearchService searchService;
@SuppressWarnings("unchecked")
@RequestMapping(method = RequestMethod.GET, value = "/search")
@ResponseBody
public Map searchNew(HttpServletRequest request) {
Map<String, Object> paramMap = request.getParameterMap();
try {
Map<String, Object> jsonMap = searchService.searchNew(transParamType(paramMap));
return jsonMap;
} catch (Exception e) {
return errorReturn("searchNew", paramMap, e.getMessage());
}
}
@SuppressWarnings("unchecked")
@RequestMapping(method = RequestMethod.GET, value = "/discount")
@ResponseBody
public Map discount(HttpServletRequest request) {
Map<String, Object> paramMap = request.getParameterMap();
try {
Map<String, Object> jsonMap = searchService.discount(transParamType(paramMap));
return jsonMap;
} catch (Exception e) {
return errorReturn("discount", paramMap, e.getMessage());
}
}
@SuppressWarnings("unchecked")
@RequestMapping(method = RequestMethod.GET, value = "/recent")
@ResponseBody
public Map recent(HttpServletRequest request) {
Map<String, Object> paramMap = request.getParameterMap();
try {
Map<String, Object> jsonMap = searchService.recentShelveDay(transParamType(paramMap));
return jsonMap;
} catch (Exception e) {
return errorReturn("recent", paramMap, e.getMessage());
}
}
@SuppressWarnings("unchecked")
@RequestMapping(method = RequestMethod.GET, value = "/count")
@ResponseBody
public long searchCount(HttpServletRequest request) {
Map<String, Object> paramMap = request.getParameterMap();
try {
long count = searchService.searchCount(transParamType(paramMap));
return count;
} catch (Exception e) {
logger.error("[※查询]失败:"+ e.getMessage());
Map<String, Object> rtnMap = new HashMap<String, Object>();
rtnMap.put("code", 400);
rtnMap.put("status", "error");
rtnMap.put("errmsg", e.getMessage());
return 0;
}
}
@SuppressWarnings("unchecked")
@RequestMapping(method = RequestMethod.GET, value = "/suggest")
@ResponseBody
public Map suggest(HttpServletRequest request) {
Map<String, Object> paramMap = request.getParameterMap();
try {
Map<String, Object> jsonMap = searchService.suggest(transParamType(paramMap));
return jsonMap;
} catch (Exception e) {
return errorReturn("suggest", paramMap, e.getMessage());
}
}
@SuppressWarnings("unchecked")
@RequestMapping(method = RequestMethod.GET, value = "/sortgroup")
@ResponseBody
public Map sortGroup(HttpServletRequest request) {
Map<String, Object> paramMap = request.getParameterMap();
try {
Map<String, Object> jsonMap = searchService.sortGroup(transParamType(paramMap));
return jsonMap;
} catch (Exception e) {
return errorReturn("sortGroup", paramMap, e.getMessage());
}
}
@SuppressWarnings("unchecked")
@RequestMapping(method = RequestMethod.GET, value = "/brands")
@ResponseBody
public Map brands(HttpServletRequest request) {
Map<String, Object> paramMap = request.getParameterMap();
try {
Map<String, Object> jsonMap = searchService.brands(transParamType(paramMap));
return jsonMap;
} catch (Exception e) {
return errorReturn("brands", paramMap, e.getMessage());
}
}
@SuppressWarnings("unchecked")
@RequestMapping(method = RequestMethod.GET, value = "/group_brands")
@ResponseBody
public Map groupBrands(HttpServletRequest request) {
Map<String, Object> paramMap = request.getParameterMap();
try {
Map<String, Object> jsonMap = searchService.groupBrands(transParamType(paramMap));
return jsonMap;
} catch (Exception e) {
return errorReturn("groupBrands", paramMap, e.getMessage());
}
}
@SuppressWarnings("unchecked")
@RequestMapping(method = RequestMethod.GET, value = "/new-shelve")
@ResponseBody
public Map newShelveProduct(HttpServletRequest request) {
Map<String, Object> paramMap = request.getParameterMap();
try {
Map<String, Object> jsonMap = searchService.newShelveProduct(transParamType(paramMap));
return jsonMap;
} catch (Exception e) {
return errorReturn("newShelveProduct", paramMap, e.getMessage());
}
}
@SuppressWarnings("unchecked")
@RequestMapping(method = RequestMethod.GET, value = "/new_product")
@ResponseBody
public Map newProduct(HttpServletRequest request) {
Map<String, Object> paramMap = request.getParameterMap();
try {
Map<String, Object> jsonMap = searchService.newProduct(transParamType(paramMap));
return jsonMap;
} catch (Exception e) {
return errorReturn("newProduct", paramMap, e.getMessage());
}
}
@SuppressWarnings("unchecked")
@RequestMapping(method = RequestMethod.GET, value = "/shops")
@ResponseBody
public Map shops(HttpServletRequest request) {
Map<String, Object> paramMap = request.getParameterMap();
try {
Map<String, Object> jsonMap = searchService.shops(transParamType(paramMap));
return jsonMap;
} catch (Exception e) {
return errorReturn("shops", paramMap, e.getMessage());
}
}
private Map errorReturn(final String funName, final Map<String, Object> paramMap, final String errorMessage) {
logger.error("[※查询]失败:[func={}][param={}][message={}]", funName, JsonUtil.toJson(paramMap), errorMessage);
Map<String, Object> rtnMap = new HashMap<String, Object>();
rtnMap.put("code", 400);
rtnMap.put("status", "error");
rtnMap.put("errmsg", errorMessage);
return rtnMap; //new ModelAndView(ISearchConstans.JSON_VIEW_NAME, rtnMap);
}
/**
* 将HttpServletRequest中被锁定的ParameterMap转化为普通的HashMap
* */
public static Map<String, String> transParamType(Map<String, Object> paramMap){
Map<String, String> rtnMap = new HashMap<String, String>();
Iterator<String> itKeys = paramMap.keySet().iterator();
while(itKeys.hasNext()){
String key = itKeys.next();
String val = ((String[])paramMap.get(key))[0];
rtnMap.put(key, val);
}
return rtnMap;
}
}