diff --git a/src/main/java/com/biutag/supervision/pojo/entity/WvpDeviceChannel.java b/src/main/java/com/biutag/supervision/pojo/entity/WvpDeviceChannel.java index 80197b1..14c4d97 100644 --- a/src/main/java/com/biutag/supervision/pojo/entity/WvpDeviceChannel.java +++ b/src/main/java/com/biutag/supervision/pojo/entity/WvpDeviceChannel.java @@ -6,6 +6,9 @@ import com.baomidou.mybatisplus.annotation.TableId; import lombok.Getter; import lombok.Setter; +import java.util.ArrayList; +import java.util.List; + @Setter @Getter public class WvpDeviceChannel { @@ -330,4 +333,7 @@ public class WvpDeviceChannel { @TableField("data_device_id") private Integer dataDeviceId; + // 存储子设备的列表 + @TableField(exist = false) + private List childList = new ArrayList<>(); } \ No newline at end of file diff --git a/src/main/java/com/biutag/supervision/service/WvpDeviceChannelService.java b/src/main/java/com/biutag/supervision/service/WvpDeviceChannelService.java index 86b45c2..58b2d27 100644 --- a/src/main/java/com/biutag/supervision/service/WvpDeviceChannelService.java +++ b/src/main/java/com/biutag/supervision/service/WvpDeviceChannelService.java @@ -1,11 +1,46 @@ package com.biutag.supervision.service; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; -import com.biutag.supervision.pojo.entity.WvpDeviceChannel; import com.biutag.supervision.mapper.WvpDeviceChannelMapper; +import com.biutag.supervision.pojo.entity.WvpDeviceChannel; import org.springframework.stereotype.Service; +import java.util.*; +import java.util.stream.Collectors; + @Service public class WvpDeviceChannelService extends ServiceImpl { -} + public List getDeviceList() { + List allDevices = list(); + Map deviceMap = allDevices.stream() + .collect(Collectors.toMap(WvpDeviceChannel::getDeviceId, device -> device)); + List hierarchy = new ArrayList<>(); + Queue queue = new LinkedList<>(); + for (WvpDeviceChannel device : allDevices) { + if (device.getParentId() == null) { + hierarchy.add(device); + queue.offer(device); + } + } + while (!queue.isEmpty()) { + WvpDeviceChannel parentDevice = queue.poll(); + List children = deviceMap.values().stream() + .filter(device -> parentDevice.getDeviceId().equals(getParentDeviceId(device.getParentId()))) + .collect(Collectors.toList()); + + if (!children.isEmpty()) { + parentDevice.setChildList(children); + queue.addAll(children); + } + } + return hierarchy; + } + + private String getParentDeviceId(String parentId) { + if (parentId != null && parentId.contains("/")) { + return parentId.substring(parentId.lastIndexOf("/") + 1); + } + return parentId; + } +} \ No newline at end of file