Authored by hugufei

修复价格聚合的边界问题

... ... @@ -63,7 +63,7 @@ public class PriceAggregation extends AbstractAggregation {
// 划分价格区间
List<Integer> intervals = PriceRangeUtils.getPriceInterval(allPrices, maxPrice);
// 获取结果
Map<String, Object> priceResult = this.getPriceResult(intervals,maxPrice);
Map<String, Object> priceResult = getPriceResult(intervals,maxPrice);
return priceResult;
}
... ... @@ -79,7 +79,7 @@ public class PriceAggregation extends AbstractAggregation {
return jsonObject;
}
private Map<String, Object> getPriceResult(List<Integer> priceIntervals,float maxPrice) {
private static Map<String, Object> getPriceResult(List<Integer> priceIntervals,float maxPrice) {
Map<String, Object> map = new LinkedHashMap<String, Object>();
if (priceIntervals == null || priceIntervals.isEmpty()) {
return map;
... ... @@ -93,19 +93,21 @@ public class PriceAggregation extends AbstractAggregation {
int toPrice = priceIntervals.get(j + 1);
map.put(String.format(priceKeyPattern, fromPrice, toPrice), "¥" + fromPrice + "-" + toPrice);
}
if(priceIntervals.get(j) + 1 <= maxPrice){
map.put(String.format(priceKeyPattern, priceIntervals.get(j) + 1, 99999), "¥" + priceIntervals.get(j) + "以上");
int lastPrice = priceIntervals.get(j);
int lastPriceNext = lastPrice + 1;
if(lastPriceNext <= maxPrice){
map.put(String.format(priceKeyPattern, lastPriceNext, Math.min(lastPriceNext,99999)), "¥" + lastPrice + "以上");
}
return map;
}
public Map<String, Object> getPrePriceMap() {
Map<String, Object> priceMap = new LinkedHashMap<String, Object>();
priceMap.put("0,300", "¥0-300");
priceMap.put("300,600", "¥300-600");
priceMap.put("600,1000", "¥600-1000");
priceMap.put("1000,2000", "¥1000-2000");
priceMap.put("2000,99999", "¥2000以上");
return priceMap;
public static void main(String[] args) {
float[] allPrices = { 10.5f, 20.5f, 30.5f, 49.5f, 55.5f, 66.6f, 77.7f, 88.7f, 99.9f, 111.1f, 122.2f, 133.3f, 555.5f, 666.6f, 777.7f, 778f, 779, 780 };
float maxPrice = allPrices[allPrices.length - 1];
List<Integer> intervals = PriceRangeUtils.getPriceInterval(allPrices, maxPrice);
System.out.println(intervals);
System.out.println(780);
// 将的到的价格数组组装成前端显示的价格列表
System.out.println(getPriceResult(intervals,maxPrice));
}
}
... ...