Przeglądaj źródła

1.利润分析详情添加排序

wuhp 1 rok temu
rodzic
commit
0ce1912263

+ 2 - 0
src/main/java/com/ydtech/modules/admin/model/report/profitAnalysis/ProfitAnalysisQueryDto.java

@@ -54,4 +54,6 @@ public class ProfitAnalysisQueryDto {
     @ApiModelProperty("类目ids")
     private List<String> ids;
 
+    @ApiModelProperty("排序")
+    private String orderBy;
 }

+ 36 - 1
src/main/java/com/ydtech/modules/admin/service/impl/ProfitAnalysisServiceImpl.java

@@ -203,7 +203,42 @@ public class ProfitAnalysisServiceImpl implements ProfitAnalysisService {
 
             resultList.addAll(allAccountExpenses);
         }
-        return getTree(resultList);
+
+        List<ProfitAnalysisDto> tree = getTree(resultList);
+        String orderBy = profitAnalysisQueryDto.getOrderBy();
+        return sortListByField(tree, orderBy);
+    }
+
+    /**
+     *
+     * @param tree
+     * @param orderBy
+     * @return  排序
+     */
+    private List<ProfitAnalysisDto> sortListByField(List<ProfitAnalysisDto> tree, String orderBy) {
+        if (tree == null || tree.isEmpty()) {
+            return tree;
+        }
+        if(StringUtils.isEmpty(orderBy)){
+            return tree;
+        }
+
+        // 初始化比较器
+        Comparator<ProfitAnalysisDto> comparator = (dto1, dto2) -> {
+            BigDecimal applyAmount = dto1.getApplyAmount();// 假设字段名为applyAmount,根据实际情况调整
+            BigDecimal applyAmount1 = dto2.getApplyAmount();
+            return applyAmount.compareTo(applyAmount1);
+        };
+
+        // 根据排序方向调整比较器
+        if ("desc".equalsIgnoreCase(orderBy)) {
+            comparator = comparator.reversed();
+        }
+
+        // 对列表进行排序
+        List<ProfitAnalysisDto> sortedList = new ArrayList<>(tree);
+        Collections.sort(sortedList, comparator);
+        return sortedList;
     }
 
     private BigDecimal aggregateChildAmounts(String expensesId, Map<String, ProfitAnalysisDto> map) {