Authored by wangnan9279

图库

1 package com.yoho.search.restapi.others; 1 package com.yoho.search.restapi.others;
2 2
  3 +import com.yoho.search.common.cache.aop.SearchCacheAble;
  4 +import com.yoho.search.common.utils.HttpServletRequestUtils;
  5 +import com.yoho.search.models.SearchApiResult;
  6 +import com.yoho.search.service.service.IImageRepertoryService;
  7 +import org.springframework.beans.factory.annotation.Autowired;
3 import org.springframework.stereotype.Controller; 8 import org.springframework.stereotype.Controller;
  9 +import org.springframework.web.bind.annotation.RequestMapping;
  10 +import org.springframework.web.bind.annotation.RequestMethod;
  11 +import org.springframework.web.bind.annotation.ResponseBody;
  12 +
  13 +import javax.servlet.http.HttpServletRequest;
  14 +import java.util.Map;
4 15
5 /** 16 /**
6 * @Author: wangnan 17 * @Author: wangnan
@@ -8,4 +19,18 @@ import org.springframework.stereotype.Controller; @@ -8,4 +19,18 @@ import org.springframework.stereotype.Controller;
8 */ 19 */
9 @Controller 20 @Controller
10 public class ImageRepertoryController { 21 public class ImageRepertoryController {
  22 +
  23 + @Autowired
  24 + private IImageRepertoryService imageRepertoryService;
  25 +
  26 + /**
  27 + * 搜索图库列表
  28 + */
  29 + @SearchCacheAble(cacheInMinute = 1, cacheName = "IMAGEREPERTORY")
  30 + @RequestMapping(method = RequestMethod.GET, value = "/imageRepertory")
  31 + @ResponseBody
  32 + public SearchApiResult searchImageRepertory(HttpServletRequest request) {
  33 + Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
  34 + return imageRepertoryService.searchImageRepertoryList(paramMap);
  35 + }
11 } 36 }
@@ -120,6 +120,18 @@ public class SearchRequestParams { @@ -120,6 +120,18 @@ public class SearchRequestParams {
120 public static final String SHOPS_PARAM_SHOPSTYPE = "shopsType"; 120 public static final String SHOPS_PARAM_SHOPSTYPE = "shopsType";
121 public static final String SHOPS_PARAM_STATUS = "status"; 121 public static final String SHOPS_PARAM_STATUS = "status";
122 122
  123 + //ImageRepertory index param
  124 + public static final String IMAGEREPERTORY_PARAM_ID = "id";
  125 + public static final String IMAGEREPERTORY_PARAM_ITEMID = "itemId";
  126 + public static final String IMAGEREPERTORY_PARAM_CHANNEL = "channel";
  127 + public static final String IMAGEREPERTORY_PARAM_SEASON = "season";
  128 + public static final String IMAGEREPERTORY_PARAM_IMAGESIZE = "imageSize";
  129 + public static final String IMAGEREPERTORY_PARAM_IMAGENAME = "imageName";
  130 + public static final String IMAGEREPERTORY_PARAM_IMAGETYPE = "imageType";
  131 + public static final String IMAGEREPERTORY_PARAM_ITEMTYPE = "itemType";
  132 + public static final String IMAGEREPERTORY_PARAM_PORT = "port";
  133 +
  134 +
123 public static final String SHOPS_PARAM_CUSTOMIZE_TAG = "customize_tag"; 135 public static final String SHOPS_PARAM_CUSTOMIZE_TAG = "customize_tag";
124 136
125 //Promotion index field 137 //Promotion index field
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