Authored by wangnan9279

productList by skn

... ... @@ -4,8 +4,9 @@ import com.alibaba.fastjson.JSONObject;
import com.yoho.search.aop.downgrade.PersionalRateLimit;
import com.yoho.search.common.utils.HttpServletRequestUtils;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.scene.pages.entrance.ProductListSwitchService;
import com.yoho.search.service.scene.others.ProductListBySknService;
import com.yoho.search.service.scene.others.guang.ProductListWithPhraseService;
import com.yoho.search.service.scene.pages.entrance.ProductListSwitchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
... ... @@ -24,6 +25,8 @@ public class ProductListController {
private ProductListWithPhraseService productListWithPhraseService;
@Autowired
private ProductListSwitchService productListSwitchService;
@Autowired
private ProductListBySknService productListBySknService;
/**
* 获取商品列表-支持个性化降级
... ... @@ -64,6 +67,18 @@ public class ProductListController {
return productListWithPhraseService.productListWithPhrase(paramMap);
}
/**
* 根据传入的skn获取商品列表,并按传入skn顺序返回-不支持个性化
*/
@RequestMapping(method = RequestMethod.GET, value = "/productindex/productListBySkn")
@ResponseBody
public SearchApiResult productListBySkn(HttpServletRequest request) {
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
paramMap.remove("uid");
return productListBySknService.productListBySkn(paramMap);
}
private Map<String, String> getParamMap(JSONObject param) {
Map<String, String> paramMap = new HashMap<String, String>();
for (Map.Entry<String, Object> entry : param.entrySet()) {
... ...
... ... @@ -52,6 +52,20 @@ public class ProductListHelper {
return this.innerBuildProductListSearchParam(paramMap, false, true);
}
public SearchParam buildProductListBySknSearchParam(Map<String, String> paramMap,int size) throws Exception {
// 1)构建基本查询参数
SearchParam searchParam = searchParamHelper.buildWithPersional(paramMap, false);
searchParam.setAggregationBuilders(null);
searchParam.setSize(size);
searchParam.setOffset(0);
// 2)设置排序字段
searchParam.setSortBuilders(searchSortHelper.buildSortList(paramMap));
// 3)设置返回的结果
List<String> includeFields = productIndexBaseService.getProductIndexIncludeFields();
searchParam.setIncludeFields(includeFields);
return searchParam;
}
private SearchParam innerBuildProductListSearchParam(Map<String, String> paramMap, boolean needPersional, boolean containPhrase) throws Exception {
// 1)验证查询条数
int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
... ...
package com.yoho.search.service.scene.others;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.aop.cache.SearchCacheAble;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.common.SearchCommonService;
import com.yoho.search.common.SearchRequestParams;
import com.yoho.search.common.utils.SearchApiResultUtils;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.helper.ProductListHelper;
import com.yoho.search.service.recall.beans.strategy.NotRecallTypeEnum;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author wangnan
* @version 2018/10/15
*/
@Service
public class ProductListBySknService {
private static final Logger logger = LoggerFactory.getLogger(ProductListBySknService.class);
@Autowired
private SearchCommonService searchCommonService;
@Autowired
private ProductListHelper productListHelper;
@SearchCacheAble(cacheInMinute = 10, cacheName = "PRODUCT_LIST_BY_SKN")
public SearchApiResult productListBySkn(Map<String, String> paramMap) {
try {
// 0、必传参数
if (!this.checkParam(paramMap)) {
return new SearchApiResult().setCode(400).setMessage("请传skn参数");
}
// 1)构造搜索参数
List<String> sknList = Arrays.asList(paramMap.get(SearchRequestParams.PARAM_SEARCH_PRODUCT_SKN).split(","));
sknList = sknList.size() > 100 ? sknList.subList(0, 100) : sknList;
SearchParam searchParam = productListHelper.buildProductListBySknSearchParam(paramMap, sknList.size());
// 2)查询ES
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
if (searchResult == null) {
return new SearchApiResult().setCode(500).setMessage("execption");
}
// 3)获取返回结果
JSONObject productListResult = productListHelper.buildProductListResult(searchResult, searchParam.getSize(), false, NotRecallTypeEnum.NOT_PERSIONAL);
// 4)排序
List<Map<String, Object>> productReturnInfoList = (List<Map<String, Object>>) productListResult.get("product_list");
Map<String, Map<String, Object>> productReturnInfoMap = productReturnInfoList.stream().collect(Collectors.toMap(p -> p.get("product_skn").toString(), p -> p));
List<Map<String, Object>> productReturnInfoListSorted = new ArrayList<>(productReturnInfoList.size());
for (String skn : sknList) {
Map<String, Object> sknInfo = productReturnInfoMap.get(skn);
if (sknInfo != null) {
productReturnInfoListSorted.add(sknInfo);
}
}
//5)构造返回结果
productListResult.put("product_list", productReturnInfoListSorted);
productListResult.put("total", productReturnInfoListSorted.size());
productListResult.remove("page");
productListResult.remove("page_total");
productListResult.remove("page_size");
return new SearchApiResult().setData(productListResult);
} catch (Exception e) {
return SearchApiResultUtils.errorSearchApiResult(logger, paramMap, e);
}
}
private boolean checkParam(Map<String, String> paramMap) {
if (StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_PRODUCT_SKN))) {
return true;
}
return false;
}
}
... ...