Authored by unknown

新版本发现好货接口

@@ -3,6 +3,8 @@ package com.yoho.search.service.scene; @@ -3,6 +3,8 @@ package com.yoho.search.service.scene;
3 import java.util.ArrayList; 3 import java.util.ArrayList;
4 import java.util.Arrays; 4 import java.util.Arrays;
5 import java.util.Date; 5 import java.util.Date;
  6 +import java.util.HashMap;
  7 +import java.util.Iterator;
6 import java.util.List; 8 import java.util.List;
7 import java.util.Map; 9 import java.util.Map;
8 10
@@ -119,7 +121,7 @@ public class NewGoodProductSceneService { @@ -119,7 +121,7 @@ public class NewGoodProductSceneService {
119 final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX; 121 final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
120 JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(goodProductSearchCache, indexName, searchParam); 122 JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(goodProductSearchCache, indexName, searchParam);
121 if (cacheObject != null) { 123 if (cacheObject != null) {
122 - return new SearchApiResult().setData(cacheObject); 124 + //return new SearchApiResult().setData(cacheObject);
123 } 125 }
124 // 6、查询ES 126 // 6、查询ES
125 SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam); 127 SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
@@ -134,7 +136,7 @@ public class NewGoodProductSceneService { @@ -134,7 +136,7 @@ public class NewGoodProductSceneService {
134 dataMap.put("page_total", searchResult.getTotalPage()); 136 dataMap.put("page_total", searchResult.getTotalPage());
135 137
136 List<Map<String, Object>> product_list = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList()); 138 List<Map<String, Object>> product_list = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList());
137 - dataMap.put("product_list", this.sortGoodProductList(product_list, paramMap, recProductSkns));// 处理一下商品的顺序; 139 + dataMap.put("product_list", this.sortGoodProductList(product_list, recProductSkns));// 处理一下商品的顺序;
138 140
139 // 8)将结果存进缓存 141 // 8)将结果存进缓存
140 searchCacheService.addJSONObjectToCache(goodProductSearchCache, indexName, searchParam, dataMap); 142 searchCacheService.addJSONObjectToCache(goodProductSearchCache, indexName, searchParam, dataMap);
@@ -247,6 +249,12 @@ public class NewGoodProductSceneService { @@ -247,6 +249,12 @@ public class NewGoodProductSceneService {
247 return result; 249 return result;
248 } 250 }
249 251
  252 + private String getSortKey(Map<String, Object> product) {
  253 + int brandId = MapUtils.getInteger(product, "brand_id", 0);
  254 + int small_sort_id = MapUtils.getInteger(product, "small_sort_id", 0);
  255 + return brandId + "_" + small_sort_id;
  256 + }
  257 +
250 /** 258 /**
251 * 二次排序 259 * 二次排序
252 * 260 *
@@ -254,12 +262,66 @@ public class NewGoodProductSceneService { @@ -254,12 +262,66 @@ public class NewGoodProductSceneService {
254 * @param recProductSkns 262 * @param recProductSkns
255 * @return 263 * @return
256 */ 264 */
257 - private List<Map<String, Object>> sortGoodProductList(List<Map<String, Object>> product_list, Map<String, String> paramMap, List<String> recProductSkns) {  
258 - List<Map<String, Object>> productList = productListSortService.sortProductList(product_list, paramMap);  
259 - List<Map<String, Object>>  
260 -  
261 -  
262 - return productList; 265 + private List<Map<String, Object>> sortGoodProductList(List<Map<String, Object>> product_list, List<String> recProductSkns) {
  266 + if (product_list == null || product_list.isEmpty()) {
  267 + return product_list;
  268 + }
  269 + List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
  270 + Map<String, Integer> keyCount = new HashMap<String, Integer>();
  271 + Iterator<Map<String, Object>> iterator = product_list.iterator();
  272 + // 1、非推荐skn的品牌品类打散
  273 + while (iterator.hasNext()) {
  274 + Map<String, Object> product = iterator.next();
  275 + if (recProductSkns.contains(MapUtils.getString(product, "product_skn"))) {
  276 + continue;
  277 + }
  278 + String key = this.getSortKey(product);
  279 + int count = keyCount.getOrDefault(key, 1);
  280 + if (count <= 1) {
  281 + results.add(product);
  282 + iterator.remove();
  283 + }
  284 + keyCount.put(key, count + 1);
  285 + }
  286 + // 2、处理剩余的非推荐skn
  287 + iterator = product_list.iterator();
  288 + while (iterator.hasNext()) {
  289 + Map<String, Object> product = iterator.next();
  290 + if (recProductSkns.contains(MapUtils.getString(product, "product_skn"))) {
  291 + continue;
  292 + }
  293 + results.add(product);
  294 + iterator.remove();
  295 + }
  296 + // 3、将推荐的skn按顺序插到指定位置
  297 + Map<String, Map<String, Object>> productInfo = new HashMap<String, Map<String, Object>>();
  298 + for (Map<String, Object> product : product_list) {
  299 + productInfo.put(MapUtils.getString(product, "product_skn"), product);
  300 + }
  301 + int orginSize = results.size();
  302 + for (int i = 0; i < recProductSkns.size(); i++) {
  303 + Map<String, Object> product = productInfo.get(recProductSkns.get(i));
  304 + if(product==null){
  305 + continue;
  306 + }
  307 + int index = getIndex(orginSize,results);
  308 + results.add(index,product);
  309 + }
  310 + return results;
263 } 311 }
264 - 312 +
  313 + private int getIndex(int orginSize,List<Map<String, Object>> results){
  314 + int currenSize = results.size();
  315 + if(currenSize==orginSize){
  316 + return 0;
  317 + }
  318 + if(currenSize-orginSize==1){
  319 + return currenSize>4?4:currenSize;
  320 + }
  321 + if(currenSize-orginSize==2){
  322 + return currenSize>7?7:currenSize;
  323 + }
  324 + return currenSize;
  325 + }
  326 +
265 } 327 }