1、如下图所示红色框框里面的数字如何统计出来
条件:
已知条件为最下一个层级的总数
答:
1、获取所有分类信息列表
2、根据分类信息列表获取最下一级商品总数
3、反向累加子级数据到父级
int counts = 0;
List<TreeVO> treeVos = new ArrayList<>();
TreeVO treeVo = new TreeVO(0, "全部", "#", "fa fa-folder-o", "#", null, false, true, true, counts, d);
treeVos.add(treeVo);
for (ProductTypePo p : productTypes) {
TreeVO tree = null;
Map<String, Object> attr = new HashMap<>();
attr.put("level", p.getClassLvl());
attr.put("edit", true);
int count = 0;
if (request.isCountProduct()) {
// 组装模糊匹配key
count = null == countMap.get(String.valueOf(p.getClassId())) ? 0 : countMap.get(String
.valueOf(p.getClassId()));
counts += count;
attr.put("count", count);
}
if (p.getClassLvl().intValue() == 4) {
attr.put("child", false);
tree = new TreeVO(p.getClassId(), p.getClassNm(), p.getfClassId(), "fa fa-folder-o",
p.getClassLvl(), p.getClassType(), false, false, false, count, attr);
} else {
attr.put("child", true);
tree = new TreeVO(p.getClassId(), p.getClassNm(), p.getfClassId(), "fa fa-folder-o",
p.getClassLvl(), p.getClassType(), false, false, false, count, attr);
}
treeVos.add(tree);
}
d.put("count", counts);
Integer levl = 4;//最高层级
boolean flag = true;
while(flag){
levl-=1;
List<TreeVO> treeVoLst = new ArrayList<>();
for (TreeVO tree : treeVos) {
if(tree.getData().get("level").equals(levl)){
treeVoLst.add(tree);
}
}
totTreeCount(treeVos,treeVoLst);
if(levl==0)
flag = false;
}
private boolean totTreeCount(List<TreeVO> treeVos, List<TreeVO> treeVoLst) {
for (TreeVO treeVO1 : treeVoLst) {
int count = 0;
for (TreeVO treeVO2 : treeVos) {
if (treeVO1.getId().equals(treeVO2.getParent())) {
count += treeVO2.getCount();
}
}
treeVO1.setCount(count);
treeVO1.getData().put("count", count);
}
return true;
}