Commit e60f3171b0da0632f1411a377a3fc6ef768ac170
1 parent
eb75ae76
首页查询占比优化保留2位小数
Showing
2 changed files
with
16 additions
and
19 deletions
urbanops-module-garden/src/main/java/com/zteits/urbanops/module/garden/controller/app/homepage/vo/AppWorkOrderSummaryRspVo.java
| @@ -17,7 +17,7 @@ public class AppWorkOrderSummaryRspVo { | @@ -17,7 +17,7 @@ public class AppWorkOrderSummaryRspVo { | ||
| 17 | /*总条数*/ | 17 | /*总条数*/ |
| 18 | private String totalNum; | 18 | private String totalNum; |
| 19 | /*占比*/ | 19 | /*占比*/ |
| 20 | - private List<Integer> percents; | 20 | + private List<Double> percents; |
| 21 | /*单位工单统计*/ | 21 | /*单位工单统计*/ |
| 22 | private List<AppWorkOrderSummaryVo> table; | 22 | private List<AppWorkOrderSummaryVo> table; |
| 23 | 23 |
urbanops-module-garden/src/main/java/com/zteits/urbanops/module/garden/service/homepage/HomepageSummaryServiceImpl.java
| @@ -320,32 +320,29 @@ public class HomepageSummaryServiceImpl implements HomepageSummaryService{ | @@ -320,32 +320,29 @@ public class HomepageSummaryServiceImpl implements HomepageSummaryService{ | ||
| 320 | .mapToInt(AppWorkOrderSummaryVo::getTotal) | 320 | .mapToInt(AppWorkOrderSummaryVo::getTotal) |
| 321 | .sum(); | 321 | .sum(); |
| 322 | 322 | ||
| 323 | - List<Integer> percents; | 323 | + List<Double> percents; |
| 324 | if (totalNum <= 0) { | 324 | if (totalNum <= 0) { |
| 325 | - percents = Collections.singletonList(0); | 325 | + percents = Collections.singletonList(0.00); |
| 326 | } else { | 326 | } else { |
| 327 | - List<Integer> tempPercents = workOrderList.stream() | 327 | + // 2. 计算原始百分比 |
| 328 | + List<Double> tempList = workOrderList.stream() | ||
| 328 | .map(vo -> { | 329 | .map(vo -> { |
| 329 | - double percent = (vo.getTotal() * 100.0) / totalNum; | ||
| 330 | - int round = (int) Math.round(percent); | ||
| 331 | - // 核心:只要 >0 的,至少给 1%,不允许出现 0% | ||
| 332 | - return (round == 0 && vo.getTotal() > 0) ? 1 : round; | 330 | + double val = (vo.getTotal() * 100.0) / totalNum; |
| 331 | + // 保留两位小数 | ||
| 332 | + return Math.round(val * 100.0) / 100.0; | ||
| 333 | }) | 333 | }) |
| 334 | .collect(Collectors.toList()); | 334 | .collect(Collectors.toList()); |
| 335 | 335 | ||
| 336 | - // 校准总和 = 100% | ||
| 337 | - int sum = tempPercents.stream().mapToInt(Integer::intValue).sum(); | ||
| 338 | - int diff = sum - 100; // 多出来的部分,从最大值减掉 | 336 | + // 3. 计算总和,校准到 100.00 |
| 337 | + double sum = tempList.stream().mapToDouble(Double::doubleValue).sum(); | ||
| 338 | + double diff = Math.round((100.00 - sum) * 100.0) / 100.0; | ||
| 339 | 339 | ||
| 340 | - if (diff != 0 && !tempPercents.isEmpty()) { | ||
| 341 | - // 找出最大值的索引,把差值减掉(最合理,不会出现0) | ||
| 342 | - OptionalInt maxIndex = IntStream.range(0, tempPercents.size()) | ||
| 343 | - .reduce((i, j) -> tempPercents.get(i) > tempPercents.get(j) ? i : j); | ||
| 344 | - if (maxIndex.isPresent()) { | ||
| 345 | - tempPercents.set(maxIndex.getAsInt(), tempPercents.get(maxIndex.getAsInt()) - diff); | ||
| 346 | - } | 340 | + if (Math.abs(diff) > 0.001 && !tempList.isEmpty()) { |
| 341 | + // 把差值补到最后一个元素上,保证总和 = 100.00 | ||
| 342 | + int lastIdx = tempList.size() - 1; | ||
| 343 | + tempList.set(lastIdx, Math.round((tempList.get(lastIdx) + diff) * 100.0) / 100.0); | ||
| 347 | } | 344 | } |
| 348 | - percents = tempPercents; | 345 | + percents = tempList; |
| 349 | } | 346 | } |
| 350 | // 6. 封装返回数据 | 347 | // 6. 封装返回数据 |
| 351 | respVo.setLegend(companyNameList); | 348 | respVo.setLegend(companyNameList); |