Authored by wangnan9279

productList by skn

@@ -4,8 +4,9 @@ import com.alibaba.fastjson.JSONObject; @@ -4,8 +4,9 @@ import com.alibaba.fastjson.JSONObject;
4 import com.yoho.search.aop.downgrade.PersionalRateLimit; 4 import com.yoho.search.aop.downgrade.PersionalRateLimit;
5 import com.yoho.search.common.utils.HttpServletRequestUtils; 5 import com.yoho.search.common.utils.HttpServletRequestUtils;
6 import com.yoho.search.models.SearchApiResult; 6 import com.yoho.search.models.SearchApiResult;
7 -import com.yoho.search.service.scene.pages.entrance.ProductListSwitchService; 7 +import com.yoho.search.service.scene.others.ProductListBySknService;
8 import com.yoho.search.service.scene.others.guang.ProductListWithPhraseService; 8 import com.yoho.search.service.scene.others.guang.ProductListWithPhraseService;
  9 +import com.yoho.search.service.scene.pages.entrance.ProductListSwitchService;
9 import org.springframework.beans.factory.annotation.Autowired; 10 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.stereotype.Controller; 11 import org.springframework.stereotype.Controller;
11 import org.springframework.web.bind.annotation.RequestBody; 12 import org.springframework.web.bind.annotation.RequestBody;
@@ -24,6 +25,8 @@ public class ProductListController { @@ -24,6 +25,8 @@ public class ProductListController {
24 private ProductListWithPhraseService productListWithPhraseService; 25 private ProductListWithPhraseService productListWithPhraseService;
25 @Autowired 26 @Autowired
26 private ProductListSwitchService productListSwitchService; 27 private ProductListSwitchService productListSwitchService;
  28 + @Autowired
  29 + private ProductListBySknService productListBySknService;
27 30
28 /** 31 /**
29 * 获取商品列表-支持个性化降级 32 * 获取商品列表-支持个性化降级
@@ -64,6 +67,18 @@ public class ProductListController { @@ -64,6 +67,18 @@ public class ProductListController {
64 return productListWithPhraseService.productListWithPhrase(paramMap); 67 return productListWithPhraseService.productListWithPhrase(paramMap);
65 } 68 }
66 69
  70 + /**
  71 + * 根据传入的skn获取商品列表,并按传入skn顺序返回-不支持个性化
  72 + */
  73 + @RequestMapping(method = RequestMethod.GET, value = "/productindex/productListBySkn")
  74 + @ResponseBody
  75 + public SearchApiResult productListBySkn(HttpServletRequest request) {
  76 + Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
  77 + paramMap.remove("uid");
  78 + return productListBySknService.productListBySkn(paramMap);
  79 + }
  80 +
  81 +
67 private Map<String, String> getParamMap(JSONObject param) { 82 private Map<String, String> getParamMap(JSONObject param) {
68 Map<String, String> paramMap = new HashMap<String, String>(); 83 Map<String, String> paramMap = new HashMap<String, String>();
69 for (Map.Entry<String, Object> entry : param.entrySet()) { 84 for (Map.Entry<String, Object> entry : param.entrySet()) {
@@ -52,6 +52,20 @@ public class ProductListHelper { @@ -52,6 +52,20 @@ public class ProductListHelper {
52 return this.innerBuildProductListSearchParam(paramMap, false, true); 52 return this.innerBuildProductListSearchParam(paramMap, false, true);
53 } 53 }
54 54
  55 + public SearchParam buildProductListBySknSearchParam(Map<String, String> paramMap,int size) throws Exception {
  56 + // 1)构建基本查询参数
  57 + SearchParam searchParam = searchParamHelper.buildWithPersional(paramMap, false);
  58 + searchParam.setAggregationBuilders(null);
  59 + searchParam.setSize(size);
  60 + searchParam.setOffset(0);
  61 + // 2)设置排序字段
  62 + searchParam.setSortBuilders(searchSortHelper.buildSortList(paramMap));
  63 + // 3)设置返回的结果
  64 + List<String> includeFields = productIndexBaseService.getProductIndexIncludeFields();
  65 + searchParam.setIncludeFields(includeFields);
  66 + return searchParam;
  67 + }
  68 +
55 private SearchParam innerBuildProductListSearchParam(Map<String, String> paramMap, boolean needPersional, boolean containPhrase) throws Exception { 69 private SearchParam innerBuildProductListSearchParam(Map<String, String> paramMap, boolean needPersional, boolean containPhrase) throws Exception {
56 // 1)验证查询条数 70 // 1)验证查询条数
57 int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum")); 71 int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
  1 +package com.yoho.search.service.scene.others;
  2 +
  3 +import com.alibaba.fastjson.JSONObject;
  4 +import com.yoho.search.aop.cache.SearchCacheAble;
  5 +import com.yoho.search.base.utils.ISearchConstants;
  6 +import com.yoho.search.common.SearchCommonService;
  7 +import com.yoho.search.common.SearchRequestParams;
  8 +import com.yoho.search.common.utils.SearchApiResultUtils;
  9 +import com.yoho.search.core.es.model.SearchParam;
  10 +import com.yoho.search.core.es.model.SearchResult;
  11 +import com.yoho.search.models.SearchApiResult;
  12 +import com.yoho.search.service.helper.ProductListHelper;
  13 +import com.yoho.search.service.recall.beans.strategy.NotRecallTypeEnum;
  14 +import org.apache.commons.lang.StringUtils;
  15 +import org.slf4j.Logger;
  16 +import org.slf4j.LoggerFactory;
  17 +import org.springframework.beans.factory.annotation.Autowired;
  18 +import org.springframework.stereotype.Service;
  19 +
  20 +import java.util.ArrayList;
  21 +import java.util.Arrays;
  22 +import java.util.List;
  23 +import java.util.Map;
  24 +import java.util.stream.Collectors;
  25 +
  26 +/**
  27 + * @author wangnan
  28 + * @version 2018/10/15
  29 + */
  30 +@Service
  31 +public class ProductListBySknService {
  32 +
  33 + private static final Logger logger = LoggerFactory.getLogger(ProductListBySknService.class);
  34 +
  35 + @Autowired
  36 + private SearchCommonService searchCommonService;
  37 + @Autowired
  38 + private ProductListHelper productListHelper;
  39 +
  40 + @SearchCacheAble(cacheInMinute = 10, cacheName = "PRODUCT_LIST_BY_SKN")
  41 + public SearchApiResult productListBySkn(Map<String, String> paramMap) {
  42 + try {
  43 + // 0、必传参数
  44 + if (!this.checkParam(paramMap)) {
  45 + return new SearchApiResult().setCode(400).setMessage("请传skn参数");
  46 + }
  47 +
  48 + // 1)构造搜索参数
  49 + List<String> sknList = Arrays.asList(paramMap.get(SearchRequestParams.PARAM_SEARCH_PRODUCT_SKN).split(","));
  50 + sknList = sknList.size() > 100 ? sknList.subList(0, 100) : sknList;
  51 + SearchParam searchParam = productListHelper.buildProductListBySknSearchParam(paramMap, sknList.size());
  52 +
  53 + // 2)查询ES
  54 + SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
  55 + if (searchResult == null) {
  56 + return new SearchApiResult().setCode(500).setMessage("execption");
  57 + }
  58 +
  59 + // 3)获取返回结果
  60 + JSONObject productListResult = productListHelper.buildProductListResult(searchResult, searchParam.getSize(), false, NotRecallTypeEnum.NOT_PERSIONAL);
  61 +
  62 + // 4)排序
  63 + List<Map<String, Object>> productReturnInfoList = (List<Map<String, Object>>) productListResult.get("product_list");
  64 + Map<String, Map<String, Object>> productReturnInfoMap = productReturnInfoList.stream().collect(Collectors.toMap(p -> p.get("product_skn").toString(), p -> p));
  65 + List<Map<String, Object>> productReturnInfoListSorted = new ArrayList<>(productReturnInfoList.size());
  66 + for (String skn : sknList) {
  67 + Map<String, Object> sknInfo = productReturnInfoMap.get(skn);
  68 + if (sknInfo != null) {
  69 + productReturnInfoListSorted.add(sknInfo);
  70 + }
  71 + }
  72 +
  73 + //5)构造返回结果
  74 + productListResult.put("product_list", productReturnInfoListSorted);
  75 + productListResult.put("total", productReturnInfoListSorted.size());
  76 + productListResult.remove("page");
  77 + productListResult.remove("page_total");
  78 + productListResult.remove("page_size");
  79 + return new SearchApiResult().setData(productListResult);
  80 + } catch (Exception e) {
  81 + return SearchApiResultUtils.errorSearchApiResult(logger, paramMap, e);
  82 + }
  83 + }
  84 +
  85 + private boolean checkParam(Map<String, String> paramMap) {
  86 + if (StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_PRODUCT_SKN))) {
  87 + return true;
  88 + }
  89 + return false;
  90 + }
  91 +}