/**
* 树类
*
*/
@Data
public class TreeInfoVO extends BaseVO {
/**节点ID*/
private String id;
/**父ID*/
private String parentId;
/**名称*/
private String name;
private List<TreeInfoVO> children;
}
//业务层
package com.jysoft.service;
import com.jysoft.dao.entity.Company;
import com.jysoft.dao.entity.Device;
import com.jysoft.dao.entity.Station;
import com.jysoft.dao.mapper.CompanyMapper;
import com.jysoft.dao.mapper.DeviceMapper;
import com.jysoft.dao.mapper.StationMapper;
import com.jysoft.dao.mapper.VoiceprintMapper;
import com.jysoft.entity.company.vo.CompanyVO;
import com.jysoft.entity.company.vo.TreeInfoVO;
import com.jysoft.resp.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
@Service
@Slf4j
public class CompanyService {
@Autowired
CompanyMapper companyMapper;
@Autowired
StationMapper stationMapper;
@Autowired
DeviceMapper deviceMapper;
@Autowired
VoiceprintMapper voicePrintMapper;
public ResponseResult<?> queryCompanyTreeMainInfo() {
Device device = new Device();
Station station = new Station();
List<Company> companyList = companyMapper.queryAllCompany();
List<Station> stationList = stationMapper.queryAllStations(station);
List<Device> deviceList = deviceMapper.queryDevices(device);
List<TreeInfoVO> treeInfoVOListFromComany = companyBuildEntity(companyList);
List<TreeInfoVO> treeInfoVOListFromStation = stationBuildEntity(stationList);
List<TreeInfoVO> treeInfoVOListFromDevice = deviceBuildEntity(deviceList);
int lengthNumberOne = treeInfoVOListFromComany.size();
for (int i = 0; i < treeInfoVOListFromStation.size(); i++) {
treeInfoVOListFromComany.add(lengthNumberOne + i - 1, treeInfoVOListFromStation.get(i));
}
int lengthNumberTwo = treeInfoVOListFromComany.size();
for (int i = 0; i < treeInfoVOListFromDevice.size(); i++) {
treeInfoVOListFromComany.add(lengthNumberTwo + i - 1, treeInfoVOListFromDevice.get(i));
}
// 把所有的数据都放到map中
Map<String, TreeInfoVO> treeMap = new HashMap<>();
for (int i = 0; i < treeInfoVOListFromComany.size() && !treeInfoVOListFromComany.isEmpty(); i++) {
// 元素的id为键,元素本身为值
treeMap.put(treeInfoVOListFromComany.get(i).getId(), treeInfoVOListFromComany.get(i));
}
// 将所有顶层节点存入result中
List<TreeInfoVO> result = new ArrayList<>();
// 遍历map得到顶层节点或者游离节点
for (TreeInfoVO treeInfoVO : treeInfoVOListFromComany) {
if (!treeMap.containsKey(treeInfoVO.getParentId())) {
result.add(treeInfoVO);
}
}
setParentChildRelative(treeInfoVOListFromComany, treeMap);
return ResponseResult.success(result);
}
private List<TreeInfoVO> companyBuildEntity(List<Company> companyList) {
List<TreeInfoVO> treeInfoVOList = new ArrayList<>();
companyList.forEach(company -> {
TreeInfoVO treeInfoVO = new TreeInfoVO();
treeInfoVO.setId(company.getId());
treeInfoVO.setName(company.getName());
treeInfoVO.setChildren(new ArrayList<>());
treeInfoVO.setParentId(company.getParentId());
treeInfoVOList.add(treeInfoVO);
});
return treeInfoVOList;
}
private List<TreeInfoVO> stationBuildEntity(List<Station> stationList) {
List<TreeInfoVO> treeInfoVOList = new ArrayList<>();
stationList.forEach(station -> {
TreeInfoVO treeInfoVO = new TreeInfoVO();
treeInfoVO.setId(station.getId());
treeInfoVO.setName(station.getName());
treeInfoVO.setChildren(new ArrayList<>());
treeInfoVO.setParentId(station.getCompanyId());
treeInfoVOList.add(treeInfoVO);
});
return treeInfoVOList;
}
private List<TreeInfoVO> deviceBuildEntity(List<Device> deviceList) {
List<TreeInfoVO> treeInfoVOList = new ArrayList<>();
deviceList.forEach(device -> {
TreeInfoVO treeInfoVO = new TreeInfoVO();
treeInfoVO.setId(device.getId());
treeInfoVO.setName(device.getDeviceName());
treeInfoVO.setChildren(new ArrayList<>());
treeInfoVO.setParentId(device.getStationId());
treeInfoVOList.add(treeInfoVO);
});
return treeInfoVOList;
}
public void setParentChildRelative(List<TreeInfoVO> list, Map<String, TreeInfoVO> treeMap) {
for (int i = 0; i < list.size() && !list.isEmpty(); i++) {
TreeInfoVO treeInfoVO = treeMap.get(list.get(i).getParentId());
if (treeInfoVO != null) {
if (treeInfoVO.getChildren() == null) {
treeInfoVO.setChildren(new ArrayList<>());
}
treeInfoVO.getChildren().add(list.get(i));
}
}
}
}
思路:
将所有结果集先转换为TreeInfo类,再将集合合并为一个集合,并转换为Map类;并且循环集合,根据对象的parentId来设置父子关系
前端代码
<el-tree
ref="treeBox"
:data="treeList"
:props="defaultProps"
accordion
:default-expanded-keys="expandedKeys"
node-key="id"
>
</el-tree>
- treeBox ||this.$refs可以直接指向该标签
- treeLisrt || 后端传过来的数据 注意是个集合 不能直接将树的根类传过来
- defaultProps
id: ‘id’, //标识每个结点
label: ‘name’, //结点的文本展示
children: ‘children’ //结点的子节点