Authored by hugufei

修复价格聚合的边界问题

@@ -63,7 +63,7 @@ public class PriceAggregation extends AbstractAggregation { @@ -63,7 +63,7 @@ public class PriceAggregation extends AbstractAggregation {
63 // 划分价格区间 63 // 划分价格区间
64 List<Integer> intervals = PriceRangeUtils.getPriceInterval(allPrices, maxPrice); 64 List<Integer> intervals = PriceRangeUtils.getPriceInterval(allPrices, maxPrice);
65 // 获取结果 65 // 获取结果
66 - Map<String, Object> priceResult = this.getPriceResult(intervals,maxPrice); 66 + Map<String, Object> priceResult = getPriceResult(intervals,maxPrice);
67 return priceResult; 67 return priceResult;
68 } 68 }
69 69
@@ -79,7 +79,7 @@ public class PriceAggregation extends AbstractAggregation { @@ -79,7 +79,7 @@ public class PriceAggregation extends AbstractAggregation {
79 return jsonObject; 79 return jsonObject;
80 } 80 }
81 81
82 - private Map<String, Object> getPriceResult(List<Integer> priceIntervals,float maxPrice) { 82 + private static Map<String, Object> getPriceResult(List<Integer> priceIntervals,float maxPrice) {
83 Map<String, Object> map = new LinkedHashMap<String, Object>(); 83 Map<String, Object> map = new LinkedHashMap<String, Object>();
84 if (priceIntervals == null || priceIntervals.isEmpty()) { 84 if (priceIntervals == null || priceIntervals.isEmpty()) {
85 return map; 85 return map;
@@ -93,19 +93,21 @@ public class PriceAggregation extends AbstractAggregation { @@ -93,19 +93,21 @@ public class PriceAggregation extends AbstractAggregation {
93 int toPrice = priceIntervals.get(j + 1); 93 int toPrice = priceIntervals.get(j + 1);
94 map.put(String.format(priceKeyPattern, fromPrice, toPrice), "¥" + fromPrice + "-" + toPrice); 94 map.put(String.format(priceKeyPattern, fromPrice, toPrice), "¥" + fromPrice + "-" + toPrice);
95 } 95 }
96 - if(priceIntervals.get(j) + 1 <= maxPrice){  
97 - map.put(String.format(priceKeyPattern, priceIntervals.get(j) + 1, 99999), "¥" + priceIntervals.get(j) + "以上"); 96 + int lastPrice = priceIntervals.get(j);
  97 + int lastPriceNext = lastPrice + 1;
  98 + if(lastPriceNext <= maxPrice){
  99 + map.put(String.format(priceKeyPattern, lastPriceNext, Math.min(lastPriceNext,99999)), "¥" + lastPrice + "以上");
98 } 100 }
99 return map; 101 return map;
100 } 102 }
101 103
102 - public Map<String, Object> getPrePriceMap() {  
103 - Map<String, Object> priceMap = new LinkedHashMap<String, Object>();  
104 - priceMap.put("0,300", "¥0-300");  
105 - priceMap.put("300,600", "¥300-600");  
106 - priceMap.put("600,1000", "¥600-1000");  
107 - priceMap.put("1000,2000", "¥1000-2000");  
108 - priceMap.put("2000,99999", "¥2000以上");  
109 - return priceMap; 104 + public static void main(String[] args) {
  105 + 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 };
  106 + float maxPrice = allPrices[allPrices.length - 1];
  107 + List<Integer> intervals = PriceRangeUtils.getPriceInterval(allPrices, maxPrice);
  108 + System.out.println(intervals);
  109 + System.out.println(780);
  110 + // 将的到的价格数组组装成前端显示的价格列表
  111 + System.out.println(getPriceResult(intervals,maxPrice));
110 } 112 }
111 } 113 }