Authored by hugufei

添加searchLikeForSaleOut接口

@@ -51,6 +51,13 @@ public class SearchLikeSecneController { @@ -51,6 +51,13 @@ public class SearchLikeSecneController {
51 return searchLikeService.searchLike(paramMap); 51 return searchLikeService.searchLike(paramMap);
52 } 52 }
53 53
  54 + @RequestMapping(method = RequestMethod.GET, value = "/searchLikeForSaleOut")
  55 + @ResponseBody
  56 + public SearchApiResult searchLikeForSaleOut(HttpServletRequest request) {
  57 + Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
  58 + return searchLikeService.searchLikeForSaleOut(paramMap);
  59 + }
  60 +
54 @RequestMapping(method = RequestMethod.GET, value = "/searchLikeInShop") 61 @RequestMapping(method = RequestMethod.GET, value = "/searchLikeInShop")
55 @ResponseBody 62 @ResponseBody
56 public SearchApiResult searchLikeInShop(HttpServletRequest request) { 63 public SearchApiResult searchLikeInShop(HttpServletRequest request) {
@@ -192,4 +192,79 @@ public class SearchLikeSceneService { @@ -192,4 +192,79 @@ public class SearchLikeSceneService {
192 } 192 }
193 } 193 }
194 194
  195 + /**
  196 + * @售罄商品全站找相似功能
  197 + */
  198 + @SearchCacheAble(cacheInMinute = 600, cacheName = "SEARCH_LIKE_FOR_SALE_OUT", includeParams = { "product_skn", "viewNum" })
  199 + public SearchApiResult searchLikeForSaleOut(Map<String, String> paramMap) {
  200 + try {
  201 + // 1、获取参数
  202 + String productSkn = paramMap.get(SearchRequestParams.PARAM_SEARCH_PRODUCT_SKN);
  203 + if (StringUtils.isBlank(productSkn)) {
  204 + return new SearchApiResult().setCode(400).setMessage("请输入SKN");
  205 + }
  206 + // 2、检测分页参数【默认30条,最多60条】
  207 + int pageSize = searchLikeHelper.getPageSize(paramMap);
  208 +
  209 + // 3、获取当前查询的SKN的基本信息
  210 + JSONObject productInfoInEs = searchLikeHelper.getProductInfoInEs(productSkn);
  211 + if (productInfoInEs == null) {
  212 + return new SearchApiResult().setCode(400).setMessage("SKN不存在");
  213 + }
  214 +
  215 + // 4、设置第一步SearchParam
  216 + List<SearchParam> searchParams = new ArrayList<SearchParam>();
  217 + searchParams.add(this.buildSearchLikeForSaleOutParam(productInfoInEs,pageSize));
  218 +
  219 + // 5、获取搜索结果[并截取条数]
  220 + List<Map<String, Object>> tempProductList = searchLikeHelper.queryProductList(searchParams);
  221 + if (tempProductList.size() > pageSize) {
  222 + tempProductList = CollectionUtils.safeSubList(tempProductList,0, pageSize);
  223 + }
  224 +
  225 + // 6、构造真实返回结果
  226 + List<Map<String, Object>> productListResults = new ArrayList<Map<String, Object>>();
  227 + if (!tempProductList.isEmpty()) {
  228 + productListResults = productIndexBaseService.getProductListWithPricePlan(tempProductList);
  229 + }
  230 + JSONObject jsonObject = new JSONObject();
  231 + jsonObject.put("page", 1);
  232 + jsonObject.put("page_total", 1);
  233 + jsonObject.put("page_size", pageSize);
  234 + jsonObject.put("total", productListResults.size());
  235 + jsonObject.put("product_info", searchLikeHelper.genProductInfoResult(productInfoInEs));
  236 + jsonObject.put("product_list", productListResults);
  237 + return new SearchApiResult().setData(jsonObject);
  238 + } catch (Exception e) {
  239 + logger.error(e.getMessage(), e);
  240 + return new SearchApiResult().setData(null).setMessage("searchLike Exception").setCode(500);
  241 + }
  242 + }
  243 +
  244 + private SearchParam buildSearchLikeForSaleOutParam(JSONObject productInfoInEs, int limitCount) {
  245 + SearchParam searchParam = new SearchParam();
  246 +
  247 + // 1、构建Query
  248 + String queryString = searchLikeHelper.genYohoQueryStringWithBrandName(productInfoInEs);
  249 + QueryBuilder queryBuilder = searchLikeHelper.genSearchLikeQueryBuilder(queryString, "20%", null);
  250 + searchParam.setQuery(queryBuilder);
  251 +
  252 + // 2、设置过滤条件
  253 + BoolQueryBuilder boolFilter = searchLikeHelper.genDefaultSearchLikeFilter(Arrays.asList(productInfoInEs.getString("productSkn")), false);
  254 + boolFilter.must(QueryBuilders.termsQuery(ProductIndexEsField.storeShowStatus, Arrays.asList("1","2")));
  255 + searchParam.setFiter(boolFilter);
  256 +
  257 + // 3、设置排序规则[按打分排序]
  258 + List<SortBuilder<?>> sortBuilders = new ArrayList<SortBuilder<?>>();
  259 + sortBuilders.add(SortBuilders.scoreSort().order(SortOrder.DESC));
  260 + searchParam.setSortBuilders(sortBuilders);
  261 + // 4、设置分页参数
  262 + searchParam.setOffset(0);
  263 + searchParam.setSize(limitCount);
  264 + // 5设置返回的参数【节省带宽】
  265 + List<String> includeFields = productIndexBaseService.getProductIndexIncludeFields();
  266 + searchParam.setIncludeFields(includeFields);
  267 + return searchParam;
  268 + }
  269 +
195 } 270 }