修改查看设备相关代码 #87

Merged
hnu202326010106 merged 3 commits from wanglei_branch into develop 4 weeks ago

@ -100,24 +100,20 @@ public class DeviceStatusController {
}
@GetMapping("/by-status")
@Operation(summary = "按状态查询设备", description = "根据状态查询设备列表")
@Operation(summary = "按状态查询设备", description = "根据状态和设备类型查询设备列表")
public ResponseEntity<ResultVO<List<Device>>> getDevicesByStatus(
@RequestParam String status,
@RequestParam(required = false) String areaId,
@RequestParam(required = false) String deviceType) {
// 添加默认值处理
if (deviceType == null || deviceType.isEmpty()) {
deviceType = "water_maker"; // 默认值
}
@RequestParam String status,
@RequestParam(required = false) String areaId,
@RequestParam(required = false) String deviceType) { // 保留设备类型参数,去除默认值
try {
List<Device> devices = deviceStatusService.getDevicesByStatus(status, areaId, deviceType);
return ResponseEntity.ok(ResultVO.success(devices));
} catch (Exception e) {
return ResponseEntity.ok(ResultVO.error(500, "查询设备失败: " + e.getMessage()));
try {
// 调用服务层方法时传递所有参数包括可能为null的deviceType
List<Device> devices = deviceStatusService.getDevicesByStatus(status, areaId, deviceType);
return ResponseEntity.ok(ResultVO.success(devices));
} catch (Exception e) {
return ResponseEntity.ok(ResultVO.error(500, "查询设备失败: " + e.getMessage()));
}
}
}
@GetMapping("/status-count")

@ -41,4 +41,7 @@ public interface DeviceRepository extends JpaRepository<Device, String> {
// 根据制水机ID查询关联的供水机
List<Device> findByParentMakerIdAndDeviceType(String parentMakerId, Device.DeviceType deviceType);
// 按状态和区域查询(无设备类型筛选)
List<Device> findByStatusAndAreaId(Device.DeviceStatus status, String areaId);
}

@ -76,8 +76,24 @@ public class DeviceStatusServiceImpl implements DeviceStatusService {
@Override
public List<Device> getDevicesByStatus(String status, String areaId, String deviceType) {
Device.DeviceStatus targetStatus = Device.DeviceStatus.valueOf(status);
Device.DeviceType targetType = Device.DeviceType.valueOf(deviceType);
return deviceRepository.findByStatusAndAreaIdAndDeviceType(targetStatus, areaId, targetType);
// 处理设备类型参数允许为null
Device.DeviceType targetType = null;
if (deviceType != null && !deviceType.isEmpty()) {
targetType = Device.DeviceType.valueOf(deviceType);
}
// 根据设备类型是否为null执行不同查询
if (targetType != null) {
return deviceRepository.findByStatusAndAreaIdAndDeviceType(targetStatus, areaId, targetType);
} else {
// 仅按状态和区域查询如果有区域ID
if (areaId != null && !areaId.isEmpty()) {
return deviceRepository.findByStatusAndAreaId(targetStatus, areaId);
} else {
return deviceRepository.findByStatus(targetStatus);
}
}
}
@Override

Loading…
Cancel
Save