记录两个赋值名次的方法,顺序保持不变

 有这样一个需要,在一个list<Bean>中,给Bean中的多个字段进行排名,例如数量、金额、同比、占比等添加上名次。写了以下两个工具类,将List,Bean.class和需要排名的字段传入即可

 1     /**
 2      * @return void
 3      * @Author 龙谷情
 4      * @Description 进行数值数据表格的名次赋值
 5      * @Date 10:36 2023/3/17
 6      * @Param [list, field]
 7      **/
 8     public static <T> void getDigitRanking(List<T> list, Class<T> clazz, String... fields) {
 9         Iterator<String> fieldIt = Arrays.stream(fields).iterator();
10         while (fieldIt.hasNext()) {
11             String field = fieldIt.next();
12             list.forEach(item1 -> {
13                 List<Double> tmpList = new ArrayList<>(16);
14                 JSONObject itemJson1 = JSONObject.parseObject(JSONObject.toJSONString(item1));
15                 if (itemJson1.getIntValue("orgId") != 417) {
16                     AtomicInteger ranking = new AtomicInteger();
17                     list.forEach(item2 -> {
18                         JSONObject itemJson2 = (JSONObject) JSONObject.toJSON(item2);
19                         if (itemJson2.getIntValue("orgId") != 417) {
20 
21                             if (!tmpList.contains(itemJson2.getDoubleValue(field))) {
22                                 tmpList.add(itemJson2.getDoubleValue(field));
23                                 if (itemJson1.getDoubleValue(field) == itemJson2.getDoubleValue(field)) {
24                                     ranking.getAndDecrement();
25                                 } else if (itemJson1.getDoubleValue(field) < itemJson2.getDoubleValue(field)) {
26                                     ranking.getAndIncrement();
27                                 }
28                             }
29                         }
30                     });
31                     itemJson1.put(field + "Ranking", ranking.intValue() + 2);
32                     T rankBean = JSONObject.parseObject(itemJson1.toJSONString(), clazz);
33                     try {
34                         MyBeanUtils.merge(item1, rankBean);
35                     } catch (Exception e) {
36                         logger.info(e.getMessage());
37                     }
38                 }
39             });
40         }
41     }
42 
43     /**
44      * @Author 龙谷情
45      * @Description 进行百分号数据表格的名次赋值
46      * @Date 18:17 2023/3/22
47      * @Param [list, clazz, fields]
48      * @return void
49      **/
50     public static <T> void getPercentRanking(List<T> list, Class<T> clazz, String... fields) {
51         Iterator<String> fieldIt = Arrays.stream(fields).iterator();
52         while (fieldIt.hasNext()) {
53             String field = fieldIt.next();
54             list.forEach(item1 -> {
55                 List<String> tmpList = new ArrayList<>(16);
56                 JSONObject itemJson1 = JSONObject.parseObject(JSONObject.toJSONString(item1));
57                 if (itemJson1.getIntValue("orgId") != 417) {
58                     AtomicInteger ranking = new AtomicInteger();
59                     list.forEach(item2 -> {
60                         JSONObject itemJson2 = (JSONObject) JSONObject.toJSON(item2);
61                         if (itemJson1.getIntValue("orgId") != 417) {
62                             Double value1 = 0.00;
63                             Double value2 = 0.00;
64                             if (!itemJson1.getString(field).equals("--")) {
65                                 value1 = Double.valueOf(itemJson1.getString(field).substring(0, itemJson1.getString(field).length() - 1));
66                             }
67                             if (!itemJson2.getString(field).equals("--")) {
68                                 value2 = Double.valueOf(itemJson2.getString(field).substring(0, itemJson2.getString(field).length() - 1));
69                             }
70                             if (!tmpList.contains(itemJson2.getString(field))) {
71                                 tmpList.add(itemJson2.getString(field));
72                                 if (value1.equals(value2)) {
73                                     ranking.getAndDecrement();
74                                 } else if (value1 < value2) {
75                                     ranking.getAndIncrement();
76                                 }
77                             }
78                         }
79                     });
80                     itemJson1.put(field + "Ranking", ranking.intValue()+2);
81                     T rankBean = JSONObject.parseObject(itemJson1.toJSONString(), clazz);
82                     try {
83                         MyBeanUtils.merge(item1, rankBean);
84                     } catch (Exception e) {
85                         logger.info(e.getMessage());
86                     }
87                 }
88             });
89         }
90     }

 使用:

List<CompensationFormEntity> compensationFormEntityList
EvaluationUtils.getDigitRanking(compensationFormEntityList, CompensationFormEntity.class, "sumCount", "sumMoney", "sumCountYear", "sumMoneyYear");
List<CompensationMaintenance> compensationMaintenanceList
EvaluationUtils.getPercentRanking(compensationMaintenanceList, CompensationMaintenance.class, "sumMaintenanceCostRate", "sumMaintenanceCostYearRate");

 

posted @ 2023-03-24 19:08  龙谷情Sinoam  阅读(29)  评论(0编辑  收藏  举报
Smiley face