1、计算树形结构的总分数
/**
* 计算树形结构的分数
* @param treePojoDTO 前端传过来的
* @return
*/
public TreePojoDTO getTreeList(TreePojoDTO treePojoDTO) {
//特定分类的总得分
BigDecimal sum = new BigDecimal(StringUtils.isEmpty(treePojoDTO.getScore()) ? "0" : treePojoDTO.getScore());
//一级指标列表
for (TreePojoDTO treePojoDTO1 : treePojoDTO.getChildrenList()) {
calcChildrenIndexScore(treePojoDTO1);
//得分
BigDecimal b1 = new BigDecimal(StringUtils.isEmpty(treePojoDTO1.getScore()) ? "0" : treePojoDTO1.getScore());
//权重
BigDecimal b2 = new BigDecimal(StringUtils.isEmpty(treePojoDTO1.getWeight()) ? "0" : treePojoDTO1.getWeight());
//得分*权重
sum = b1.multiply(b2).add(sum);
}
treePojoDTO.setSpecificValue(String.valueOf(sum));
return treePojoDTO;
}
private TreePojoDTO calcChildrenIndexScore(TreePojoDTO treePojoDTO) {
if (treePojoDTO == null || treePojoDTO.getChildrenList() == null|| treePojoDTO.getChildrenList().size() == 0) {
return treePojoDTO;
}
BigDecimal scoreTotal = new BigDecimal(org.apache.commons.lang3.StringUtils.isEmpty(treePojoDTO.getScore()) ? "0" : treePojoDTO.getScore());
for (TreePojoDTO child : treePojoDTO.getChildrenList()) {
TreePojoDTO indexChild = calcChildrenIndexScore(child);
BigDecimal b1 = new BigDecimal(org.apache.commons.lang3.StringUtils.isEmpty(indexChild.getScore()) ? "0" : indexChild.getScore());
BigDecimal b2 = new BigDecimal(org.apache.commons.lang3.StringUtils.isEmpty(indexChild.getWeight()) ? "0" : indexChild.getWeight());
scoreTotal = b1.multiply(b2).add(scoreTotal);
}
treePojoDTO.setScore(String.valueOf(scoreTotal));
return treePojoDTO;
}
该代码段展示了一个方法,用于计算具有层级关系的数据结构(树形结构)的总分数。它遍历每个节点,根据节点的得分和权重计算加权值,然后累加到总分中。递归函数`calcChildrenIndexScore`处理子节点的计算,确保所有子节点的分数都被考虑在内。

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



