1
|
package com.yoho.search.service.service.impl;
|
1
|
package com.yoho.search.service.service.impl;
|
2
|
|
2
|
|
|
|
3
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
4
|
+import com.yoho.error.event.SearchEvent;
|
|
|
5
|
+import com.yoho.search.base.utils.EventReportEnum;
|
|
|
6
|
+import com.yoho.search.base.utils.ISearchConstants;
|
|
|
7
|
+import com.yoho.search.common.utils.SearchApiResultUtils;
|
|
|
8
|
+import com.yoho.search.core.es.model.SearchParam;
|
|
|
9
|
+import com.yoho.search.core.es.model.SearchResult;
|
|
|
10
|
+import com.yoho.search.core.es.utils.IgnoreSomeException;
|
3
|
import com.yoho.search.models.SearchApiResult;
|
11
|
import com.yoho.search.models.SearchApiResult;
|
|
|
12
|
+import com.yoho.search.service.base.SearchCommonService;
|
|
|
13
|
+import com.yoho.search.service.base.SearchRequestParams;
|
4
|
import com.yoho.search.service.service.IImageRepertoryService;
|
14
|
import com.yoho.search.service.service.IImageRepertoryService;
|
|
|
15
|
+import org.apache.commons.lang.StringUtils;
|
|
|
16
|
+import org.elasticsearch.index.query.BoolQueryBuilder;
|
|
|
17
|
+import org.elasticsearch.index.query.QueryBuilders;
|
|
|
18
|
+import org.slf4j.Logger;
|
|
|
19
|
+import org.slf4j.LoggerFactory;
|
|
|
20
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
21
|
+import org.springframework.context.ApplicationEventPublisher;
|
|
|
22
|
+import org.springframework.context.ApplicationEventPublisherAware;
|
|
|
23
|
+import org.springframework.stereotype.Service;
|
5
|
|
24
|
|
|
|
25
|
+import java.util.ArrayList;
|
|
|
26
|
+import java.util.LinkedHashMap;
|
|
|
27
|
+import java.util.List;
|
6
|
import java.util.Map;
|
28
|
import java.util.Map;
|
7
|
|
29
|
|
8
|
/**
|
30
|
/**
|
|
@@ -10,11 +32,107 @@ import java.util.Map; |
|
@@ -10,11 +32,107 @@ import java.util.Map; |
10
|
* @Author: wangnan
|
32
|
* @Author: wangnan
|
11
|
* @Date: Created in 2018/4/16
|
33
|
* @Date: Created in 2018/4/16
|
12
|
*/
|
34
|
*/
|
13
|
-public class ImageRepertoryServiceImpl implements IImageRepertoryService{
|
35
|
+@Service
|
|
|
36
|
+public class ImageRepertoryServiceImpl extends BaseService implements IImageRepertoryService, ApplicationEventPublisherAware {
|
|
|
37
|
+
|
|
|
38
|
+ private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
|
|
39
|
+
|
|
|
40
|
+ private ApplicationEventPublisher publisher;
|
|
|
41
|
+
|
|
|
42
|
+ @Autowired
|
|
|
43
|
+ private SearchCommonService searchCommonService;
|
|
|
44
|
+
|
|
|
45
|
+ @Override
|
|
|
46
|
+ public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
|
|
|
47
|
+ this.publisher = applicationEventPublisher;
|
|
|
48
|
+ }
|
14
|
|
49
|
|
15
|
@Override
|
50
|
@Override
|
16
|
public SearchApiResult searchImageRepertoryList(Map<String, String> paramMap) {
|
51
|
public SearchApiResult searchImageRepertoryList(Map<String, String> paramMap) {
|
17
|
- return null;
|
52
|
+ try {
|
|
|
53
|
+ logger.info("[class=ImageRepertoryServiceImpl][func=searchImageRepertoryList][param={}][begin={}]", paramMap.toString(), System.currentTimeMillis());
|
|
|
54
|
+
|
|
|
55
|
+ // 1.构造查询参数
|
|
|
56
|
+ SearchParam searchParam = new SearchParam();
|
|
|
57
|
+ int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
|
|
|
58
|
+ int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
|
|
|
59
|
+ if (page < 1 || pageSize < 0) {
|
|
|
60
|
+ return new SearchApiResult().setCode(400).setMessage("分页参数错误");
|
|
|
61
|
+ }
|
|
|
62
|
+ if (pageSize > 100) {
|
|
|
63
|
+ pageSize = 100;
|
|
|
64
|
+ }
|
|
|
65
|
+ searchParam.setOffset((page - 1) * pageSize);
|
|
|
66
|
+ searchParam.setSize(pageSize);
|
|
|
67
|
+
|
|
|
68
|
+ //2.配置Filter
|
|
|
69
|
+ BoolQueryBuilder boolFilter = constructHelperFilterBuilder(paramMap);
|
|
|
70
|
+ searchParam.setFiter(boolFilter);
|
|
|
71
|
+
|
|
|
72
|
+ final String indexName = ISearchConstants.INDEX_NAME_IMAGE_REPERTORY;
|
|
|
73
|
+
|
|
|
74
|
+ //3.查询ES
|
|
|
75
|
+ SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
|
|
|
76
|
+ SearchApiResult searchApiResult = new SearchApiResult();
|
|
|
77
|
+ if (searchResult == null) {
|
|
|
78
|
+ return searchApiResult.setCode(500);
|
|
|
79
|
+ }
|
|
|
80
|
+
|
|
|
81
|
+ // 8.构造返回结果并加入缓存
|
|
|
82
|
+ JSONObject dataMap = new JSONObject();
|
|
|
83
|
+ dataMap.put("total", searchResult.getTotal());
|
|
|
84
|
+ dataMap.put("page", searchResult.getPage());
|
|
|
85
|
+ dataMap.put("page_size", searchParam.getSize());
|
|
|
86
|
+ dataMap.put("page_total", searchResult.getTotalPage());
|
|
|
87
|
+ dataMap.put("imageRepertory_list", getImageRepertoryMap(searchResult.getResultList()));
|
|
|
88
|
+ return new SearchApiResult().setData(dataMap);
|
|
|
89
|
+ } catch (Exception e) {
|
|
|
90
|
+ publisher.publishEvent(new SearchEvent(EventReportEnum.SEARCHCONTROLLER_IMAGEREPERTORY.getEventName(), EventReportEnum.SEARCHCONTROLLER_IMAGEREPERTORY.getFunctionName(),
|
|
|
91
|
+ EventReportEnum.SEARCHCONTROLLER_IMAGEREPERTORY.getMoudleName(), "exception", IgnoreSomeException.filterSomeException(e), null));
|
|
|
92
|
+ return SearchApiResultUtils.errorSearchApiResult("imageRepertory", paramMap, e);
|
|
|
93
|
+ }
|
|
|
94
|
+ }
|
|
|
95
|
+
|
|
|
96
|
+ private BoolQueryBuilder constructHelperFilterBuilder(Map<String, String> paramMap) throws Exception {
|
|
|
97
|
+ BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
|
|
|
98
|
+ boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_ID);
|
|
|
99
|
+ boolFilter = boolQueryTermsQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_ITEMID);
|
|
|
100
|
+ boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_CHANNEL);
|
|
|
101
|
+ boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_SEASON);
|
|
|
102
|
+ boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_IMAGESIZE);
|
|
|
103
|
+ boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_IMAGENAME);
|
|
|
104
|
+ boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_IMAGETYPE);
|
|
|
105
|
+ boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_ITEMTYPE);
|
|
|
106
|
+ boolFilter = boolQueryTermQuery(paramMap, boolFilter, SearchRequestParams.IMAGEREPERTORY_PARAM_PORT);
|
|
|
107
|
+ boolFilter = generalHandler(paramMap, boolFilter);
|
|
|
108
|
+ return boolFilter;
|
|
|
109
|
+ }
|
|
|
110
|
+
|
|
|
111
|
+ private List<Map<String, Object>> getImageRepertoryMap(List<Map<String, Object>> resultList) {
|
|
|
112
|
+ List<Map<String, Object>> returnList = new ArrayList<Map<String, Object>>();
|
|
|
113
|
+ for (Map<String, Object> map : resultList) {
|
|
|
114
|
+ Map<String, Object> productMap = getImageRepertoryMap(map);
|
|
|
115
|
+ returnList.add(productMap);
|
|
|
116
|
+ }
|
|
|
117
|
+ return returnList;
|
|
|
118
|
+ }
|
|
|
119
|
+
|
|
|
120
|
+ private Map<String, Object> getImageRepertoryMap(Map<String, Object> map) {
|
|
|
121
|
+ Map<String, Object> productMap = new LinkedHashMap<>();
|
|
|
122
|
+ productMap.put("id", map.get("id") == null ? "" : map.get("id"));
|
|
|
123
|
+ productMap.put("itemId", map.get("itemId") == null ? "" : map.get("itemId"));
|
|
|
124
|
+ productMap.put("channel", map.get("channel") == null ? "" : map.get("channel"));
|
|
|
125
|
+ productMap.put("season", map.get("season") == null ? "" : map.get("season"));
|
|
|
126
|
+ productMap.put("imageUrl", map.get("imageUrl") == null ? "" : map.get("imageUrl"));
|
|
|
127
|
+ productMap.put("imageSize", map.get("imageSize") == null ? "" : map.get("imageSize"));
|
|
|
128
|
+ productMap.put("imageName", map.get("imageName") == null ? "" : map.get("imageName"));
|
|
|
129
|
+ productMap.put("imageType", map.get("imageType") == null ? "" : map.get("imageType"));
|
|
|
130
|
+ productMap.put("itemType", map.get("itemType") == null ? "" : map.get("itemType"));
|
|
|
131
|
+ productMap.put("remarks", map.get("remarks") == null ? "" : map.get("remarks"));
|
|
|
132
|
+ productMap.put("port", map.get("port") == null ? "" : map.get("port"));
|
|
|
133
|
+ productMap.put("menu", map.get("menu") == null ? "" : map.get("menu"));
|
|
|
134
|
+ productMap.put("menuName", map.get("menuName") == null ? "" : map.get("menuName"));
|
|
|
135
|
+ return productMap;
|
18
|
}
|
136
|
}
|
19
|
}
|
137
|
}
|
20
|
|
138
|
|