public List<ItemVO> searchItemCategory(OperatorInfo operatorInfo){
//顶级目录
List<ItemEntity>itemEntities=itemDBRepository.findAllByInstitutionNumberAndDelFalse(operatorInfo.getInstitutionNumber());
List<ItemVO> itemVOS;
itemVOS = this.parentAndChildren(itemEntities);
return itemVOS;
}
public List<ItemVO> parentAndChildren(List<ItemEntity> itemEntities){
//最顶层根节点
List<ItemVO> rootList = new ArrayList<>();
//非最顶层根节点
List<ItemVO> bodyList = new ArrayList<>();
for (ItemEntity itemEntity : itemEntities) {
if (StringUtils.isEmpty(itemEntity.getSuperiorMark())){
rootList.add(itemMapper.transformToVO(itemEntity));
}else{
bodyList.add(itemMapper.transformToVO(itemEntity));
}
}
return getTree(rootList,bodyList);
}
public List<ItemVO> getTree(List<ItemVO>rootList, List<ItemVO>bodyList){
if(bodyList != null){
//声明一个map,用来过滤已操作过的数据
Map<String,String> map = new HashMap<>(bodyList.size());
rootList.forEach(parent->getChild(parent,bodyList,map));
return rootList;
}else{
return rootList;
}
}
private void getChild(ItemDTO parent, List<ItemVO>bodyList, Map<String,String> map){
List<ItemDTO>childList = new ArrayList<>();
bodyList.stream().filter(c->!map.containsKey(c.getMark()))
.filter(c->c.getSuperiorMark().equals(parent.getMark()))
.filter(c->c.getItemType() == 1)
.forEach(c->{
map.put(c.getMark(),c.getSuperiorMark());
getChild(c,bodyList,map);
childList.add(c);
});
parent.setItems(childList);
}