From d3312db20639814dcb757e852c291e1f1fe3e984 Mon Sep 17 00:00:00 2001 From: tianyongbao Date: Fri, 27 Mar 2026 21:30:23 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A1=98=E5=8F=A3=E6=8E=A7=E5=88=B6?= =?UTF-8?q?=E5=99=A8=E5=88=97=E8=A1=A8=EF=BC=8C=E5=8F=AA=E6=9C=89=E5=BC=80?= =?UTF-8?q?=E5=85=B3=E5=85=B3=E8=81=94=EF=BC=8C=E6=97=A0=E6=BA=B6=E8=A7=A3?= =?UTF-8?q?=E6=B0=A7=E6=8E=A2=E5=A4=B4=E6=97=B6=EF=BC=8C=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=8Cbug=E4=BF=AE=E5=A4=8D=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fishery/controller/PondController.java | 222 +++++++++--------- .../intc/iot/controller/IotController.java | 19 +- 2 files changed, 123 insertions(+), 118 deletions(-) diff --git a/intc-modules/intc-fishery/src/main/java/com/intc/fishery/controller/PondController.java b/intc-modules/intc-fishery/src/main/java/com/intc/fishery/controller/PondController.java index a5e8041..778f2a8 100644 --- a/intc-modules/intc-fishery/src/main/java/com/intc/fishery/controller/PondController.java +++ b/intc-modules/intc-fishery/src/main/java/com/intc/fishery/controller/PondController.java @@ -212,7 +212,7 @@ public class PondController extends BaseController { result.setPondName(pond.getPondName()); result.setKeepNightOpen(pond.getKeepNightOpen()); - // 2. 查询塘口下的所有设备 + // 2. 查询塘口下的所有设备(device.pondId == pondId,用于检测仪和挂了塘口的控制器) List devices = deviceMapper.selectList( new LambdaQueryWrapper() .eq(Device::getPondId, pondId) @@ -220,18 +220,41 @@ public class PondController extends BaseController { .orderByDesc(Device::getCreateTime) ); + // 3. 查询塘口下的所有开关(switch.pondId == pondId),包含开关未挂塘口的控制器也能被找到 + List allSwitches = deviceSwitchMapper.selectList( + new LambdaQueryWrapper() + .eq(DeviceSwitch::getPondId, pondId) + .orderByAsc(DeviceSwitch::getIndex) + ); + + // 4. 从开关中收集所有 deviceId,补全那些本体未挂塘口的控制器 + Set switchDeviceIds = allSwitches.stream() + .map(DeviceSwitch::getDeviceId) + .collect(Collectors.toSet()); + + // 5. 查询开关关联的控制器设备(排除已经在 devices 中的) + Set existDeviceIds = devices.stream().map(Device::getId).collect(Collectors.toSet()); + List extraDeviceIds = switchDeviceIds.stream() + .filter(id -> !existDeviceIds.contains(id)) + .collect(Collectors.toList()); + if (!extraDeviceIds.isEmpty()) { + List extraDevices = deviceMapper.selectBatchIds(extraDeviceIds); + devices = new ArrayList<>(devices); + devices.addAll(extraDevices); + } + if (devices == null || devices.isEmpty()) { result.setListDetector(List.of()); result.setListController(List.of()); return R.ok(result); } - // 3. 收集所有设备ID,用于查询联动控制和故障码 + // 6. 收集所有设备ID List deviceIds = devices.stream() .map(Device::getId) .collect(Collectors.toList()); - // 4. 批量查询所有设备的联动控制 + // 7. 批量查询所有设备的联动控制 List allLinkedCtrls = linkedCtrlMapper.selectList( new LambdaQueryWrapper() .in(LinkedCtrl::getDeviceId, deviceIds) @@ -239,14 +262,7 @@ public class PondController extends BaseController { java.util.Map> linkedCtrlsByDevice = allLinkedCtrls.stream() .collect(Collectors.groupingBy(LinkedCtrl::getDeviceId)); - // 5. 批量查询塘口的所有开关 - List allSwitches = deviceSwitchMapper.selectList( - new LambdaQueryWrapper() - .eq(DeviceSwitch::getPondId, pondId) - .orderByAsc(DeviceSwitch::getIndex) - ); - - // 6. 收集开关ID,查询定时控制 + // 8. 收集开关ID,查询定时控制 List switchIds = allSwitches.stream() .map(DeviceSwitch::getId) .collect(Collectors.toList()); @@ -260,20 +276,21 @@ public class PondController extends BaseController { .collect(Collectors.groupingBy(TimingCtrl::getSwitchId)); } - // 7. 按设备ID分组开关 + // 9. 按设备ID分组开关 java.util.Map> switchesByDevice = allSwitches.stream() .collect(Collectors.groupingBy(DeviceSwitch::getDeviceId)); - // 8. 批量查询故障码 - Set controllerIds = devices.stream() + // 10. 批量查询故障码(合并 devices + 开关关联设备 的所有 deviceId) + Set allControllerIds = devices.stream() .filter(d -> d.getDeviceType() != null && d.getDeviceType() == 2) .map(Device::getId) .collect(Collectors.toSet()); + allControllerIds.addAll(switchDeviceIds); List errorCodes = new ArrayList<>(); - if (!controllerIds.isEmpty()) { + if (!allControllerIds.isEmpty()) { errorCodes = deviceErrorCodeMapper.selectList( new LambdaQueryWrapper() - .in(DeviceErrorCode::getDeviceId, controllerIds) + .in(DeviceErrorCode::getDeviceId, allControllerIds) ); } @@ -316,13 +333,27 @@ public class PondController extends BaseController { } } - // 10. 处理控制器列表 (仅测控一体机 deviceType=2) + // 12. 处理控制器列表(与 C# 一致:以 allSwitches 为主线聚合,确保只挂开关到塘口的控制器也能被找到) + // 构建设备ID -> Device 的映射,便于下面按 deviceId 查询 + java.util.Map deviceById = devices.stream() + .collect(Collectors.toMap(Device::getId, d -> d, (a, b) -> a)); + List controllerList = new ArrayList<>(); - for (Device device : devices) { - if (device.getDeviceType() != null && device.getDeviceType() == 2) { - // 转换为DeviceVo再复制到DeviceWithSwitchVo + // 用 LinkedHashMap 保证控制器顺序,以 deviceId 为 key + java.util.LinkedHashMap controllerMap = new java.util.LinkedHashMap<>(); + + for (DeviceSwitch sw : allSwitches) { + Long deviceId = sw.getDeviceId(); + Device device = deviceById.get(deviceId); + if (device == null) { + continue; // 异常情况跳过 + } + + // 如果该控制器还没有创建,则创建并加入 map + DeviceWithSwitchVo deviceVo = controllerMap.get(deviceId); + if (deviceVo == null) { DeviceVo baseDeviceVo = MapstructUtils.convert(device, DeviceVo.class); - DeviceWithSwitchVo deviceVo = new DeviceWithSwitchVo(); + deviceVo = new DeviceWithSwitchVo(); BeanUtil.copyProperties(baseDeviceVo, deviceVo); // 初始化告警码信息 @@ -339,108 +370,81 @@ public class PondController extends BaseController { // 处理设备级故障码 (switchIndex=0的故障) StringBuilder errorMessage = new StringBuilder(); for (DeviceErrorCode errorCode : errorCodes) { - if (!errorCode.getDeviceId().equals(device.getId())) { - continue; - } - if (errorCode.getSwitchIndex() == null || errorCode.getSwitchIndex() != 0) { - continue; - } - if (errorCode.getErrorCode() == null || errorCode.getErrorCode() > DefineDeviceErrorCode.PowerOff) { - continue; - } + if (!errorCode.getDeviceId().equals(deviceId)) continue; + if (errorCode.getSwitchIndex() == null || errorCode.getSwitchIndex() != 0) continue; + if (errorCode.getErrorCode() == null || errorCode.getErrorCode() > DefineDeviceErrorCode.PowerOff) continue; - // 电压告警需要开关打开 boolean isVoltageError = errorCode.getErrorCode() >= DefineDeviceErrorCode.Three_PhaseA_OverVoltage && errorCode.getErrorCode() <= DefineDeviceErrorCode.Three_PhaseC_UnderVoltage; if (device.getVoltageWarnOpen() != null && device.getVoltageWarnOpen() == 1 && isVoltageError || !isVoltageError) { String msg = DefineDeviceErrorCode.getErrorMessage(errorCode.getErrorCode()); - if (msg != null && !msg.isEmpty()) { - if (errorMessage.length() > 0 && !errorMessage.toString().contains(msg)) { - errorMessage.append(","); - } - if (!errorMessage.toString().contains(msg)) { - errorMessage.append(msg); - } + if (msg != null && !msg.isEmpty() && !errorMessage.toString().contains(msg)) { + if (errorMessage.length() > 0) errorMessage.append(","); + errorMessage.append(msg); } } } deviceVo.setErrorMessage(errorMessage.toString()); - - // 查询该设备的开关列表 - List switches = switchesByDevice.getOrDefault(device.getId(), new ArrayList<>()); - List switchVoList = new ArrayList<>(); - - for (DeviceSwitch sw : switches) { - DeviceSwitchVo switchVo = MapstructUtils.convert(sw, DeviceSwitchVo.class); - - // 判断是否有联动控制 - switchVo.setIsLinkedCtrl(0); - if (sw.getLinkedCtrlId() != null) { - LinkedCtrl linkedCtrl = linkedCtrlMapper.selectById(sw.getLinkedCtrlId()); - if (linkedCtrl != null - && ((linkedCtrl.getOxyLowerOpen() != null && linkedCtrl.getOxyLowerOpen() == 1) - || (linkedCtrl.getOxyUpperOpen() != null && linkedCtrl.getOxyUpperOpen() == 1))) { - switchVo.setIsLinkedCtrl(1); - } - } - - // 判断是否有定时控制 - switchVo.setIsTimingCtrl(false); - List timingCtrls = timingCtrlsBySwitch.get(sw.getId()); - if (timingCtrls != null && !timingCtrls.isEmpty()) { - for (TimingCtrl timingCtrl : timingCtrls) { - if (timingCtrl.getIsOpen() != null && timingCtrl.getIsOpen() == 1) { - switchVo.setIsTimingCtrl(true); - break; - } - } - } - - // 处理开关的故障码 - switchVo.setHasErrorCode(false); - for (DeviceErrorCode errorCode : errorCodes) { - if (!errorCode.getDeviceId().equals(sw.getDeviceId())) { - continue; - } - if (errorCode.getSwitchIndex() == null || !errorCode.getSwitchIndex().equals(sw.getIndex())) { - continue; - } - if (errorCode.getErrorCode() == null || errorCode.getErrorCode() > DefineDeviceErrorCode.PowerOff) { - continue; - } - - // 电流告警范围 - boolean hasElectricError = (errorCode.getErrorCode() >= DefineDeviceErrorCode.Three_Switch1OverElectricA - && errorCode.getErrorCode() <= DefineDeviceErrorCode.Three_Switch4ElectricEmpty) - || (errorCode.getErrorCode() >= DefineDeviceErrorCode.One_Switch1OverElectric - && errorCode.getErrorCode() <= DefineDeviceErrorCode.One_Switch4ElectricEmpty); - - if (sw.getElectricWarnOpen() != null && sw.getElectricWarnOpen() == 1 && hasElectricError) { - switchVo.setHasErrorCode(true); - String msg = DefineDeviceErrorCode.getErrorMessage(errorCode.getErrorCode()); - if (msg != null && !msg.isEmpty()) { - if (errorMessage.length() > 0 && !errorMessage.toString().contains(msg)) { - errorMessage.append(","); - } - if (!errorMessage.toString().contains(msg)) { - errorMessage.append(msg); - } - } - } - } - - switchVoList.add(switchVo); - } - - // 更新设备的错误信息(包含开关的错误) - deviceVo.setErrorMessage(errorMessage.toString()); - deviceVo.setListSwitch(switchVoList); - controllerList.add(deviceVo); + deviceVo.setListSwitch(new ArrayList<>()); + controllerMap.put(deviceId, deviceVo); } + + // 处理开关 Vo + DeviceSwitchVo switchVo = MapstructUtils.convert(sw, DeviceSwitchVo.class); + + // 判断是否有联动控制 + switchVo.setIsLinkedCtrl(0); + if (sw.getLinkedCtrlId() != null) { + LinkedCtrl linkedCtrl = linkedCtrlMapper.selectById(sw.getLinkedCtrlId()); + if (linkedCtrl != null + && ((linkedCtrl.getOxyLowerOpen() != null && linkedCtrl.getOxyLowerOpen() == 1) + || (linkedCtrl.getOxyUpperOpen() != null && linkedCtrl.getOxyUpperOpen() == 1))) { + switchVo.setIsLinkedCtrl(1); + } + } + + // 判断是否有定时控制 + switchVo.setIsTimingCtrl(false); + List timingCtrls = timingCtrlsBySwitch.get(sw.getId()); + if (timingCtrls != null) { + for (TimingCtrl timingCtrl : timingCtrls) { + if (timingCtrl.getIsOpen() != null && timingCtrl.getIsOpen() == 1) { + switchVo.setIsTimingCtrl(true); + break; + } + } + } + + // 处理开关的故障码 + switchVo.setHasErrorCode(false); + StringBuilder devErrMsg = new StringBuilder(deviceVo.getErrorMessage() != null ? deviceVo.getErrorMessage() : ""); + for (DeviceErrorCode errorCode : errorCodes) { + if (!errorCode.getDeviceId().equals(deviceId)) continue; + if (errorCode.getSwitchIndex() == null || !errorCode.getSwitchIndex().equals(sw.getIndex())) continue; + if (errorCode.getErrorCode() == null || errorCode.getErrorCode() > DefineDeviceErrorCode.PowerOff) continue; + + boolean hasElectricError = (errorCode.getErrorCode() >= DefineDeviceErrorCode.Three_Switch1OverElectricA + && errorCode.getErrorCode() <= DefineDeviceErrorCode.Three_Switch4ElectricEmpty) + || (errorCode.getErrorCode() >= DefineDeviceErrorCode.One_Switch1OverElectric + && errorCode.getErrorCode() <= DefineDeviceErrorCode.One_Switch4ElectricEmpty); + + if (sw.getElectricWarnOpen() != null && sw.getElectricWarnOpen() == 1 && hasElectricError) { + switchVo.setHasErrorCode(true); + String msg = DefineDeviceErrorCode.getErrorMessage(errorCode.getErrorCode()); + if (msg != null && !msg.isEmpty() && !devErrMsg.toString().contains(msg)) { + if (devErrMsg.length() > 0) devErrMsg.append(","); + devErrMsg.append(msg); + } + } + } + deviceVo.setErrorMessage(devErrMsg.toString()); + deviceVo.getListSwitch().add(switchVo); } + controllerList.addAll(controllerMap.values()); + result.setListDetector(detectorList); result.setListController(controllerList); diff --git a/intc-modules/intc-iot/src/main/java/com/intc/iot/controller/IotController.java b/intc-modules/intc-iot/src/main/java/com/intc/iot/controller/IotController.java index cf8ad8c..e22c799 100644 --- a/intc-modules/intc-iot/src/main/java/com/intc/iot/controller/IotController.java +++ b/intc-modules/intc-iot/src/main/java/com/intc/iot/controller/IotController.java @@ -15,6 +15,7 @@ import com.intc.fishery.domain.DeviceCorrectRecord; import com.intc.fishery.domain.DeviceBindRecord; import com.intc.fishery.domain.DeviceErrorCode; import com.intc.fishery.domain.AquUser; +import com.intc.fishery.constant.DefineDeviceWarnCode; import com.intc.fishery.domain.bo.*; import com.intc.fishery.domain.vo.DeviceSwitchVo; import com.intc.fishery.mapper.DeviceMapper; @@ -969,12 +970,12 @@ public class IotController extends BaseController { // 获取设备属性 Map deviceProperties = iotDeviceService.queryDeviceProperties(iotId); - // 初始化警告码:默认探头离线且未校准 (0x0081) - int warnCode = 0x0081; + // 初始化警告码:默认探头未校准 (OxyDetectorNoCorrect) + int warnCode = DefineDeviceWarnCode.OxyDetectorNoCorrect; // 如果设备离线,添加设备离线警告 if (status != null && "OFFLINE".equalsIgnoreCase(status)) { - warnCode |= 0x0080; // 设备离线 (0x0080) + warnCode |= DefineDeviceWarnCode.DeviceOffline; } // 计算设备数量,用于生成设备名称 @@ -997,7 +998,7 @@ public class IotController extends BaseController { } else { // 检查是否已过期 if (device.getDeadTime() != null && now.after(device.getDeadTime())) { - warnCode |= 0x0040; // 设备时间到期 (0x0040) + warnCode |= DefineDeviceWarnCode.DeviceTimeDead; } } @@ -1121,7 +1122,7 @@ public class IotController extends BaseController { try { int tcorrect = Integer.parseInt(value.toString()); if (tcorrect == 1) { - warnCode &= ~0x0001; // 清除未校准标记 + warnCode &= ~DefineDeviceWarnCode.OxyDetectorNoCorrect; // 清除未校准标记 } } catch (NumberFormatException e) { log.warn("无法解析校准状态: {}", value); @@ -1239,7 +1240,7 @@ public class IotController extends BaseController { switchIndex = errorCode - 0x200B + 1; // 断电告警 } else if (errorCode == 0x3001) { - warnCode |= 0x0020; // ErrorPowerOff + warnCode |= DefineDeviceWarnCode.ErrorPowerOff; // ErrorPowerOff } } @@ -1479,7 +1480,7 @@ public class IotController extends BaseController { try { int tcorrect = Integer.parseInt(value.toString()); if (tcorrect == 1) { - warnCode &= ~0x0001; // 清除未校准标记 + warnCode &= ~DefineDeviceWarnCode.OxyDetectorNoCorrect; // 清除未校准标记 } } catch (NumberFormatException e) { log.warn("无法解析校准状态: {}", value); @@ -1612,8 +1613,8 @@ public class IotController extends BaseController { // 更新设备警告码:清除未校准标记 int warnCode = device.getWarnCode() != null ? device.getWarnCode() : 0; - if ((warnCode & 0x0001) != 0) { - warnCode &= ~0x0001; // 清除未校准标记 + if ((warnCode & DefineDeviceWarnCode.OxyDetectorNoCorrect) != 0) { + warnCode &= ~DefineDeviceWarnCode.OxyDetectorNoCorrect; // 清除未校准标记 } // 更新设备