分期允许多选,按分期维度时同一个分期下的数据就会在两条数据中,总计里面也要统计两次,所以只能最后手动合计。
private void calculateSummary(Object statistics, Object summary) {
try {
Class<?> statisticsClazz = statistics.getClass();
Class<?> summaryClazz = summary.getClass();
Field[] fields = statisticsClazz.getDeclaredFields();
for (Field field : fields) {
if (!field.getType().toString().equals("class java.lang.Integer")) {
continue;
}
Method summarySetMethod = summaryClazz.getMethod("set" + getMethodName(field.getName()));
Method statisticsGetMethod = statisticsClazz.getMethod("get" + getMethodName(field.getName()));
Method summaryGetMethod = summaryClazz.getMethod("get" + getMethodName(field.getName()));
Integer statisticsValue = (Integer) statisticsGetMethod.invoke(statisticsClazz);
Integer summaryValue = (Integer) summaryGetMethod.invoke(summaryClazz);
summarySetMethod.invoke(summaryClazz, statisticsValue + summaryValue);
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 将首字母转换为大写
private static String getMethodName(String fieldName) throws Exception {
byte[] items = fieldName.getBytes();
items[0] = (byte) ((char) items[0] - 'a' + 'A');
return new String(items);
}
该博客主要探讨了在进行数据统计时遇到的问题,特别是在处理分期数据时,由于允许多选,导致相同分期的数据在不同条目中重复计数。为解决这个问题,博主提出了一个手动合计的解决方案,通过遍历和累加每个分期字段的值来确保统计数据的准确性。
710

被折叠的 条评论
为什么被折叠?



