|
|
package com.yoho.search.service.split.common;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
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.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
...
|
...
|
@@ -85,10 +88,49 @@ public class SplitProductListService 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", product_list);// 处理一下商品的顺序
|
|
|
dataMap.put("product_list", this.moveProductListSort(paramMap, product_list));// 处理一下商品的顺序
|
|
|
|
|
|
// 6)将结果存进缓存
|
|
|
searchCacheService.addJSONObjectToCache(productListSearchCache, indexName, searchParam, dataMap);
|
|
|
return new SearchApiResult().setData(dataMap);
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
}
|
|
|
} |
...
|
...
|
|