2 changed files with 43 additions and 2 deletions
@ -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<WvpDeviceChannelMapper, WvpDeviceChannel> { |
||||
|
||||
public List<WvpDeviceChannel> getDeviceList() { |
||||
List<WvpDeviceChannel> allDevices = list(); |
||||
Map<String, WvpDeviceChannel> deviceMap = allDevices.stream() |
||||
.collect(Collectors.toMap(WvpDeviceChannel::getDeviceId, device -> device)); |
||||
List<WvpDeviceChannel> hierarchy = new ArrayList<>(); |
||||
Queue<WvpDeviceChannel> 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<WvpDeviceChannel> 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; |
||||
} |
||||
} |
||||
Loading…
Reference in new issue