1.controler层代码
/**
* 获取所有的设备类型树
* @return
*/
@GetMapping("/equipmentType/tree")
@ApiOperation(value ="设备类型树 @author tarzan Liu")
public ResponseDTO<Map<String, Object>> listEquipmentTypeTree(){
return equipmentCompanyTypeService.listEquipmentTypeTree();
}
2.service层代码
查询除所有的数据,然后在java 方法体里,利用父子id组装成树状结构的列表
/**
* 获取设备分类树
* @return
*/
public ResponseDTO<Map<String, Object>> listEquipmentTypeTree() {
Map<String, Object> map = new HashMap<>();
List <EquipmentCompanyTreeDTO>treeList = Lists.newArrayList();
List<EquipmentCompanyTypeBaseDTO> listTree =equipmentCompanyTypeDao.listDataByCompany(DeletedEnum.NO_DELETED.getValue());
List <EquipmentCompanyTreeDTO> treeListType = SmartBeanUtil.copyList(listTree,EquipmentCompanyTreeDTO.class);
for(EquipmentCompanyTreeDTO parent :treeListType){
if(parent.getPid()==null || parent.getPid()==0) {
treeList.add(findChildren(parent, treeListType));
}
}
Long id=equipmentCompanyTypeDao.getDefaultTypeId(listTree.get(0).getId());
map.put("defaultTypeId",id);
map.put("treeList",treeList);
return ResponseDTO.succData(map);
}
/**
* 寻找子节点
*/
private static EquipmentCompanyTreeDTO findChildren(EquipmentCompanyTreeDTO parent, List<EquipmentCompanyTreeDTO> listCategory) {
List<EquipmentCompanyTreeDTO> childList = listCategory.stream().filter(e->parent.getId().equals(e.getPid())).collect(Collectors.toList());
if(CollectionUtils.isNotEmpty(childList)){
for (EquipmentCompanyTreeDTO child : childList){
findChildren(child,listCategory);
}
parent.setChildren(childList);
}
return parent;
}
3.接收对象treeDTO
package com.tulian.colliery.module.equipment.company.domain.dto.company;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.List;
/**
* @Description: 设备信息树形DTO
* @Author: tarzan Liu
* @Date: 2019/11/8 10:35
*/
@Data
public class EquipmentCompanyTreeDTO {
@ApiModelProperty("专业/设备类别id/设备id/空间id/状态值")
private Long id;
@ApiModelProperty("名称")
private String name;
@ApiModelProperty("父id")
private Long pid;
@ApiModelProperty("设备编号")
private String number;
@ApiModelProperty("树节支类型(0:空间/设备类型;1:设备)")
private Integer treeType;
@ApiModelProperty("空间包围盒")
private String spaceBox;
@ApiModelProperty("单体id")
private String modelId;
@ApiModelProperty("构件id")
private String guid;
@ApiModelProperty("子分类")
private List<EquipmentCompanyTreeDTO> children;
}