Authored by unknown

特殊逻辑拆到实现类中

1 package com.yoho.search.service.servicenew.scene; 1 package com.yoho.search.service.servicenew.scene;
2 2
  3 +import java.util.ArrayList;
  4 +import java.util.Iterator;
  5 +import java.util.List;
3 import java.util.Map; 6 import java.util.Map;
4 import java.util.concurrent.CompletableFuture; 7 import java.util.concurrent.CompletableFuture;
5 import java.util.concurrent.ExecutorService; 8 import java.util.concurrent.ExecutorService;
6 import java.util.concurrent.Executors; 9 import java.util.concurrent.Executors;
7 10
  11 +import org.apache.commons.collections.MapUtils;
  12 +import org.apache.commons.lang.StringUtils;
8 import org.slf4j.Logger; 13 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory; 14 import org.slf4j.LoggerFactory;
10 import org.springframework.beans.factory.annotation.Autowired; 15 import org.springframework.beans.factory.annotation.Autowired;
@@ -93,4 +98,51 @@ public class SortSceneService extends AbstractSceneService { @@ -93,4 +98,51 @@ public class SortSceneService extends AbstractSceneService {
93 } 98 }
94 } 99 }
95 100
  101 + /**
  102 + * 品类列表页针对第一页的商品,将价格高的往前面平移8个
  103 + *
  104 + * @param paramMap
  105 + * @param product_list
  106 + * @return
  107 + */
  108 + @SuppressWarnings("unused")
  109 + private List<Map<String, Object>> moveProductListSort(Map<String, String> paramMap, List<Map<String, Object>> product_list) {
  110 + // 判断页面合法性
  111 + if (!searchCommonHelper.isSortPageDefault(paramMap)) {
  112 + return product_list;
  113 + }
  114 + String page = paramMap.get("page");
  115 + if (StringUtils.isNotBlank(page) && !page.equals("1")) {
  116 + return product_list;
  117 + }
  118 + // 判断总数
  119 + int total = product_list.size();
  120 + if (total <= 10) {
  121 + return product_list;
  122 + }
  123 + // 计算均价
  124 + double totalPrice = 0;
  125 + for (Map<String, Object> product : product_list) {
  126 + totalPrice += MapUtils.getDoubleValue(product, "sales_price", 0);
  127 + }
  128 + double averagePrice = totalPrice / total;
  129 + List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
  130 + results.add(product_list.get(0));
  131 + product_list.remove(0);
  132 + Iterator<Map<String, Object>> iterator = product_list.iterator();
  133 + int moveCount = 0;
  134 + while (iterator.hasNext() && moveCount < 8) {
  135 + Map<String, Object> product = iterator.next();
  136 + if (MapUtils.getDoubleValue(product, "sales_price", 0) >= averagePrice) {
  137 + results.add(product);
  138 + moveCount++;
  139 + iterator.remove();
  140 + }
  141 + }
  142 + if (!product_list.isEmpty()) {
  143 + results.addAll(product_list);
  144 + }
  145 + return results;
  146 + }
  147 +
96 } 148 }
@@ -2,13 +2,11 @@ package com.yoho.search.service.servicenew.scene.common; @@ -2,13 +2,11 @@ package com.yoho.search.service.servicenew.scene.common;
2 2
3 import java.util.ArrayList; 3 import java.util.ArrayList;
4 import java.util.Arrays; 4 import java.util.Arrays;
5 -import java.util.Iterator;  
6 import java.util.List; 5 import java.util.List;
7 import java.util.Map; 6 import java.util.Map;
8 7
9 import javax.annotation.PostConstruct; 8 import javax.annotation.PostConstruct;
10 9
11 -import org.apache.commons.collections.MapUtils;  
12 import org.apache.commons.lang.StringUtils; 10 import org.apache.commons.lang.StringUtils;
13 import org.slf4j.Logger; 11 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory; 12 import org.slf4j.LoggerFactory;
@@ -91,7 +89,7 @@ public class SceneProductListService extends BaseService { @@ -91,7 +89,7 @@ public class SceneProductListService extends BaseService {
91 dataMap.put("page_size", searchParam.getSize()); 89 dataMap.put("page_size", searchParam.getSize());
92 dataMap.put("page_total", searchResult.getTotalPage()); 90 dataMap.put("page_total", searchResult.getTotalPage());
93 List<Map<String, Object>> product_list = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList(), null); 91 List<Map<String, Object>> product_list = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList(), null);
94 - dataMap.put("product_list", this.moveProductListSort(paramMap, product_list)); 92 + dataMap.put("product_list", product_list);
95 93
96 // 5)将结果存进缓存 94 // 5)将结果存进缓存
97 searchCacheService.addJSONObjectToCache(searchCache, indexName, searchParam, dataMap); 95 searchCacheService.addJSONObjectToCache(searchCache, indexName, searchParam, dataMap);
@@ -102,52 +100,6 @@ public class SceneProductListService extends BaseService { @@ -102,52 +100,6 @@ public class SceneProductListService extends BaseService {
102 } 100 }
103 } 101 }
104 102
105 - /**  
106 - * 品类列表页针对第一页的商品,将价格高的往前面平移8个  
107 - *  
108 - * @param paramMap  
109 - * @param product_list  
110 - * @return  
111 - */  
112 - private List<Map<String, Object>> moveProductListSort(Map<String, String> paramMap, List<Map<String, Object>> product_list) {  
113 - // 判断页面合法性  
114 - if (!searchCommonHelper.isSortPageDefault(paramMap)) {  
115 - return product_list;  
116 - }  
117 - String page = paramMap.get("page");  
118 - if (StringUtils.isNotBlank(page) && !page.equals("1")) {  
119 - return product_list;  
120 - }  
121 - // 判断总数  
122 - int total = product_list.size();  
123 - if (total <= 10) {  
124 - return product_list;  
125 - }  
126 - // 计算均价  
127 - double totalPrice = 0;  
128 - for (Map<String, Object> product : product_list) {  
129 - totalPrice += MapUtils.getDoubleValue(product, "sales_price", 0);  
130 - }  
131 - double averagePrice = totalPrice / total;  
132 - List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();  
133 - results.add(product_list.get(0));  
134 - product_list.remove(0);  
135 - Iterator<Map<String, Object>> iterator = product_list.iterator();  
136 - int moveCount = 0;  
137 - while (iterator.hasNext() && moveCount < 8) {  
138 - Map<String, Object> product = iterator.next();  
139 - if (MapUtils.getDoubleValue(product, "sales_price", 0) >= averagePrice) {  
140 - results.add(product);  
141 - moveCount++;  
142 - iterator.remove();  
143 - }  
144 - }  
145 - if (!product_list.isEmpty()) {  
146 - results.addAll(product_list);  
147 - }  
148 - return results;  
149 - }  
150 -  
151 private SearchParam buildProductListSearchParam(Map<String, String> paramMap) throws Exception { 103 private SearchParam buildProductListSearchParam(Map<String, String> paramMap) throws Exception {
152 // 1)验证查询条数 104 // 1)验证查询条数
153 int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum")); 105 int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));