Authored by unknown

fix 品类列表页面

1 package com.yoho.search.service.split.common; 1 package com.yoho.search.service.split.common;
2 2
  3 +import java.util.ArrayList;
  4 +import java.util.Iterator;
3 import java.util.List; 5 import java.util.List;
4 import java.util.Map; 6 import java.util.Map;
5 7
6 import javax.annotation.PostConstruct; 8 import javax.annotation.PostConstruct;
7 9
  10 +import org.apache.commons.collections.MapUtils;
8 import org.apache.commons.lang.StringUtils; 11 import org.apache.commons.lang.StringUtils;
9 import org.springframework.beans.factory.annotation.Autowired; 12 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.stereotype.Service; 13 import org.springframework.stereotype.Service;
@@ -85,10 +88,49 @@ public class SplitProductListService extends BaseService { @@ -85,10 +88,49 @@ public class SplitProductListService extends BaseService {
85 dataMap.put("page_size", searchParam.getSize()); 88 dataMap.put("page_size", searchParam.getSize());
86 dataMap.put("page_total", searchResult.getTotalPage()); 89 dataMap.put("page_total", searchResult.getTotalPage());
87 List<Map<String, Object>> product_list = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList(), null); 90 List<Map<String, Object>> product_list = productIndexBaseService.getProductListWithPricePlan(searchResult.getResultList(), null);
88 - dataMap.put("product_list", product_list);// 处理一下商品的顺序 91 + dataMap.put("product_list", this.moveProductListSort(paramMap, product_list));// 处理一下商品的顺序
89 92
90 // 6)将结果存进缓存 93 // 6)将结果存进缓存
91 searchCacheService.addJSONObjectToCache(productListSearchCache, indexName, searchParam, dataMap); 94 searchCacheService.addJSONObjectToCache(productListSearchCache, indexName, searchParam, dataMap);
92 return new SearchApiResult().setData(dataMap); 95 return new SearchApiResult().setData(dataMap);
93 } 96 }
  97 +
  98 + private List<Map<String, Object>> moveProductListSort(Map<String, String> paramMap, List<Map<String, Object>> product_list) {
  99 + // 判断页面合法性
  100 + if (!searchCommonHelper.isSortPageDefault(paramMap)) {
  101 + return product_list;
  102 + }
  103 + String page = paramMap.get("page");
  104 + if (StringUtils.isNotBlank(page) && !page.equals("1")) {
  105 + return product_list;
  106 + }
  107 + // 判断总数
  108 + int total = product_list.size();
  109 + if (total <= 10) {
  110 + return product_list;
  111 + }
  112 + // 计算均价
  113 + double totalPrice = 0;
  114 + for (Map<String, Object> product : product_list) {
  115 + totalPrice += MapUtils.getDoubleValue(product, "sales_price", 0);
  116 + }
  117 + double averagePrice = totalPrice / total;
  118 + List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
  119 + results.add(product_list.get(0));
  120 + product_list.remove(0);
  121 + Iterator<Map<String, Object>> iterator = product_list.iterator();
  122 + int moveCount = 0;
  123 + while (iterator.hasNext() && moveCount < 8) {
  124 + Map<String, Object> product = iterator.next();
  125 + if (MapUtils.getDoubleValue(product, "sales_price", 0) >= averagePrice) {
  126 + results.add(product);
  127 + moveCount++;
  128 + iterator.remove();
  129 + }
  130 + }
  131 + if (!product_list.isEmpty()) {
  132 + results.addAll(product_list);
  133 + }
  134 + return results;
  135 + }
94 } 136 }