Authored by unknown

特殊逻辑拆到实现类中

package com.yoho.search.service.servicenew.scene;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -93,4 +98,51 @@ public class SortSceneService extends AbstractSceneService {
}
}
/**
* 品类列表页针对第一页的商品,将价格高的往前面平移8个
*
* @param paramMap
* @param product_list
* @return
*/
@SuppressWarnings("unused")
private List<Map<String, Object>> moveProductListSort(Map<String, String> paramMap, List<Map<String, Object>> product_list) {
// 判断页面合法性
if (!searchCommonHelper.isSortPageDefault(paramMap)) {
return product_list;
}
String page = paramMap.get("page");
if (StringUtils.isNotBlank(page) && !page.equals("1")) {
return product_list;
}
// 判断总数
int total = product_list.size();
if (total <= 10) {
return product_list;
}
// 计算均价
double totalPrice = 0;
for (Map<String, Object> product : product_list) {
totalPrice += MapUtils.getDoubleValue(product, "sales_price", 0);
}
double averagePrice = totalPrice / total;
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
results.add(product_list.get(0));
product_list.remove(0);
Iterator<Map<String, Object>> iterator = product_list.iterator();
int moveCount = 0;
while (iterator.hasNext() && moveCount < 8) {
Map<String, Object> product = iterator.next();
if (MapUtils.getDoubleValue(product, "sales_price", 0) >= averagePrice) {
results.add(product);
moveCount++;
iterator.remove();
}
}
if (!product_list.isEmpty()) {
results.addAll(product_list);
}
return results;
}
}
... ...
... ... @@ -2,13 +2,11 @@ package com.yoho.search.service.servicenew.scene.common;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -91,7 +89,7 @@ public class SceneProductListService extends BaseService {
dataMap.put("page_size", searchParam.getSize());
dataMap.put("page_total", searchResult.getTotalPage());
List<Map<String, Object>> product_list = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList(), null);
dataMap.put("product_list", this.moveProductListSort(paramMap, product_list));
dataMap.put("product_list", product_list);
// 5)将结果存进缓存
searchCacheService.addJSONObjectToCache(searchCache, indexName, searchParam, dataMap);
... ... @@ -102,52 +100,6 @@ public class SceneProductListService extends BaseService {
}
}
/**
* 品类列表页针对第一页的商品,将价格高的往前面平移8个
*
* @param paramMap
* @param product_list
* @return
*/
private List<Map<String, Object>> moveProductListSort(Map<String, String> paramMap, List<Map<String, Object>> product_list) {
// 判断页面合法性
if (!searchCommonHelper.isSortPageDefault(paramMap)) {
return product_list;
}
String page = paramMap.get("page");
if (StringUtils.isNotBlank(page) && !page.equals("1")) {
return product_list;
}
// 判断总数
int total = product_list.size();
if (total <= 10) {
return product_list;
}
// 计算均价
double totalPrice = 0;
for (Map<String, Object> product : product_list) {
totalPrice += MapUtils.getDoubleValue(product, "sales_price", 0);
}
double averagePrice = totalPrice / total;
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
results.add(product_list.get(0));
product_list.remove(0);
Iterator<Map<String, Object>> iterator = product_list.iterator();
int moveCount = 0;
while (iterator.hasNext() && moveCount < 8) {
Map<String, Object> product = iterator.next();
if (MapUtils.getDoubleValue(product, "sales_price", 0) >= averagePrice) {
results.add(product);
moveCount++;
iterator.remove();
}
}
if (!product_list.isEmpty()) {
results.addAll(product_list);
}
return results;
}
private SearchParam buildProductListSearchParam(Map<String, String> paramMap) throws Exception {
// 1)验证查询条数
int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
... ...