|
|
package com.yoho.search.service.base;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import org.apache.commons.collections.MapUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import java.util.*;
|
|
|
|
|
|
@Service
|
|
|
public class ProductListSortService {
|
|
|
|
|
|
private String getSortKey(Map<String, Object> product) {
|
|
|
int brandId = MapUtils.getInteger(product, "brand_id", 0);
|
|
|
int small_sort_id = MapUtils.getInteger(product, "small_sort_id", 0);
|
|
|
return brandId + "_" + small_sort_id;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 第一页的商品按品类+品牌维度分组,每组取第一个放前面
|
|
|
*
|
|
|
* @param product_list
|
|
|
* @param paramMap
|
|
|
* @return
|
|
|
*/
|
|
|
public List<Map<String, Object>> sortProductList(List<Map<String, Object>> product_list, Map<String, String> paramMap) {
|
|
|
if (product_list == null || product_list.isEmpty()) {
|
|
|
return product_list;
|
|
|
}
|
|
|
if (StringUtils.isNotBlank(paramMap.get("order"))) {
|
|
|
return product_list;
|
|
|
}
|
|
|
int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
|
|
|
if (page != 1) {
|
|
|
return product_list;
|
|
|
}
|
|
|
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
|
|
|
Map<String, Integer> keyCount = new HashMap<String, Integer>();
|
|
|
Iterator<Map<String, Object>> iterator = product_list.iterator();
|
|
|
while (iterator.hasNext()) {
|
|
|
Map<String, Object> product = iterator.next();
|
|
|
String key = this.getSortKey(product);
|
|
|
int count = keyCount.getOrDefault(key, 1);
|
|
|
if (count <= 2) {
|
|
|
results.add(product);
|
|
|
iterator.remove();
|
|
|
public List<Map<String, Object>> sortProductList(List<Map<String, Object>> product_list) {
|
|
|
ProductListSortKey<Map<String, Object>> productSortKey = new ProductListSortKey<Map<String, Object>>() {
|
|
|
@Override
|
|
|
public String getSortKey(Map<String, Object> product) {
|
|
|
int brandId = MapUtils.getInteger(product, "brand_id", 0);
|
|
|
int small_sort_id = MapUtils.getInteger(product, "small_sort_id", 0);
|
|
|
return brandId + "_" + small_sort_id;
|
|
|
}
|
|
|
keyCount.put(key, count + 1);
|
|
|
}
|
|
|
if (!product_list.isEmpty()) {
|
|
|
results.addAll(product_list);
|
|
|
}
|
|
|
return results;
|
|
|
@Override
|
|
|
public int getMaxCount() {
|
|
|
return 2;
|
|
|
}
|
|
|
};
|
|
|
return this.sortProductList(product_list,productSortKey);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 品牌打散
|
|
|
*
|
|
|
* @param product_list
|
|
|
* @param paramMap
|
|
|
* @return
|
|
|
*/
|
|
|
public List<Map<String, Object>> sortProductMapList(List<Map<String, Object>> product_list, ProductListSortKey productSortKey) {
|
|
|
|
|
|
public <T> List<T> sortProductList(List<T> product_list, ProductListSortKey<T> productSortKey) {
|
|
|
if (product_list == null || product_list.isEmpty()) {
|
|
|
return product_list;
|
|
|
}
|
|
|
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
|
|
|
List<T> results = new ArrayList<T>();
|
|
|
while (!product_list.isEmpty()) {
|
|
|
results.addAll(getTempProductMapList(product_list, productSortKey));
|
|
|
}
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
private List<Map<String, Object>> getTempProductMapList(List<Map<String, Object>> product_list, ProductListSortKey productSortKey) {
|
|
|
List<Map<String, Object>> tempResults = new ArrayList<Map<String, Object>>();
|
|
|
private <T> List<T> getTempProductMapList(List<T> product_list, ProductListSortKey<T> productSortKey) {
|
|
|
List<T> tempResults = new ArrayList<T>();
|
|
|
Map<String, Integer> tempKeyCount = new HashMap<String, Integer>();
|
|
|
Iterator<Map<String, Object>> iterator = product_list.iterator();
|
|
|
Iterator<T> iterator = product_list.iterator();
|
|
|
while (iterator.hasNext()) {
|
|
|
Map<String, Object> product = iterator.next();
|
|
|
T product = iterator.next();
|
|
|
String sortKey = productSortKey.getSortKey(product);
|
|
|
int count = tempKeyCount.getOrDefault(sortKey, 1);
|
|
|
if (count <= productSortKey.getMaxCount()) {
|
...
|
...
|
@@ -92,40 +52,5 @@ public class ProductListSortService { |
|
|
}
|
|
|
return tempResults;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 品牌打散
|
|
|
*
|
|
|
* @param product_list
|
|
|
* @param paramMap
|
|
|
* @return
|
|
|
*/
|
|
|
public List<JSONObject> sortProductJSONObjectList(List<JSONObject> product_list, ProductListSortKey productListSortKey) {
|
|
|
if (product_list == null || product_list.isEmpty()) {
|
|
|
return product_list;
|
|
|
}
|
|
|
List<JSONObject> results = new ArrayList<JSONObject>();
|
|
|
while (!product_list.isEmpty()) {
|
|
|
results.addAll(getTempProductJSONObjectList(product_list, productListSortKey));
|
|
|
}
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
private List<JSONObject> getTempProductJSONObjectList(List<JSONObject> product_list, ProductListSortKey productListSortKey) {
|
|
|
List<JSONObject> tempResults = new ArrayList<JSONObject>();
|
|
|
Map<String, Integer> tempKeyCount = new HashMap<String, Integer>();
|
|
|
Iterator<JSONObject> iterator = product_list.iterator();
|
|
|
while (iterator.hasNext()) {
|
|
|
JSONObject product = iterator.next();
|
|
|
String sortKey = productListSortKey.getSortKey(product);
|
|
|
int count = tempKeyCount.getOrDefault(sortKey, 1);
|
|
|
if (count <= productListSortKey.getMaxCount()) {
|
|
|
tempResults.add(product);
|
|
|
iterator.remove();
|
|
|
}
|
|
|
tempKeyCount.put(sortKey, count + 1);
|
|
|
}
|
|
|
return tempResults;
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|