This commit is contained in:
2025-11-25 14:27:10 +08:00
parent d3931a9ddd
commit 7d8aeedcf2
23 changed files with 414 additions and 166 deletions

View File

@ -8,6 +8,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yj.earth.business.domain.Device;
import com.yj.earth.business.service.DeviceService;
import com.yj.earth.common.util.ApiResponse;
import com.yj.earth.common.util.NetUtils;
import com.yj.earth.vo.AddDeviceDto;
import com.yj.earth.dto.device.ImportDeviceDto;
import com.yj.earth.dto.device.UpdateDeviceDto;
@ -27,6 +28,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
@Tag(name = "设备信息管理")
@ -73,14 +75,57 @@ public class DeviceController {
}
@GetMapping("/list")
@Operation(summary = "查询设备信息")
public ApiResponse listDevice(@Parameter(description = "分页数量") Integer pageNum, @Parameter(description = "分页大小") Integer pageSize,@Parameter(description = "设备名称") String cameraName) {
@Operation(summary = "查询设备信息(含在线状态)")
public ApiResponse listDevice(
@Parameter(description = "分页数量") Integer pageNum,
@Parameter(description = "分页大小") Integer pageSize,
@Parameter(description = "设备名称") String cameraName
) {
LambdaQueryWrapper<Device> queryWrapper = new LambdaQueryWrapper<>();
if (StringUtils.isNotBlank(cameraName)) {
queryWrapper.like(Device::getCameraName, cameraName);
}
// 执行标准的分页查询
Page<Device> devicePage = deviceService.page(new Page<>(pageNum, pageSize), queryWrapper);
return ApiResponse.success(devicePage);
List<Device> deviceList = devicePage.getRecords();
if (CollectionUtils.isEmpty(deviceList)) {
// 如果没有数据,直接返回空分页
return ApiResponse.success(devicePage);
}
try {
// 提取所有设备的 IP 地址、用于批量检测
List<String> ipAddresses = deviceList.stream()
.map(Device::getIp)
.filter(Objects::nonNull)
.collect(Collectors.toList());
// 异步并发检测所有 IP 的在线状态
Map<String, Boolean> reachabilityMap = NetUtils.checkReachabilityAsync(ipAddresses);
// 更新设备状态:遍历设备列表,根据 IP 设置 status 字段
deviceList.forEach(device -> {
String ip = device.getIp();
if (ip != null) {
Boolean isOnline = reachabilityMap.get(ip);
// 根据 Ping 结果设置 status: 1 为在线0 为离线
device.setStatus(isOnline != null && isOnline ? 1 : 0);
} else {
// 如果 IP 地址为空,直接设置为离线
device.setStatus(0);
}
});
// 直接返回更新后的分页对象
return ApiResponse.success(devicePage);
} catch (ExecutionException | InterruptedException e) {
// 恢复线程中断状态
Thread.currentThread().interrupt();
return ApiResponse.success(devicePage);
}
}
@GetMapping("/delete")
@ -270,7 +315,7 @@ public class DeviceController {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("设备信息导入模板", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
response.setHeader("Content-disposition", "attachment;filename*=" + fileName + ".xlsx");
// 写入模板表头(通过空数据列表触发表头生成)
EasyExcel.write(response.getOutputStream(), ImportDeviceDto.class)
.sheet("设备信息")