fix: 塘口控制器列表,只有开关关联,无溶解氧探头时,显示问题,bug修复。

This commit is contained in:
tianyongbao
2026-03-27 21:30:23 +08:00
parent 7eb07611a6
commit d3312db206
2 changed files with 123 additions and 118 deletions

View File

@@ -212,7 +212,7 @@ public class PondController extends BaseController {
result.setPondName(pond.getPondName());
result.setKeepNightOpen(pond.getKeepNightOpen());
// 2. 查询塘口下的所有设备
// 2. 查询塘口下的所有设备device.pondId == pondId用于检测仪和挂了塘口的控制器
List<Device> devices = deviceMapper.selectList(
new LambdaQueryWrapper<Device>()
.eq(Device::getPondId, pondId)
@@ -220,18 +220,41 @@ public class PondController extends BaseController {
.orderByDesc(Device::getCreateTime)
);
// 3. 查询塘口下的所有开关switch.pondId == pondId包含开关未挂塘口的控制器也能被找到
List<DeviceSwitch> allSwitches = deviceSwitchMapper.selectList(
new LambdaQueryWrapper<DeviceSwitch>()
.eq(DeviceSwitch::getPondId, pondId)
.orderByAsc(DeviceSwitch::getIndex)
);
// 4. 从开关中收集所有 deviceId补全那些本体未挂塘口的控制器
Set<Long> switchDeviceIds = allSwitches.stream()
.map(DeviceSwitch::getDeviceId)
.collect(Collectors.toSet());
// 5. 查询开关关联的控制器设备(排除已经在 devices 中的)
Set<Long> existDeviceIds = devices.stream().map(Device::getId).collect(Collectors.toSet());
List<Long> extraDeviceIds = switchDeviceIds.stream()
.filter(id -> !existDeviceIds.contains(id))
.collect(Collectors.toList());
if (!extraDeviceIds.isEmpty()) {
List<Device> 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<Long> deviceIds = devices.stream()
.map(Device::getId)
.collect(Collectors.toList());
// 4. 批量查询所有设备的联动控制
// 7. 批量查询所有设备的联动控制
List<LinkedCtrl> allLinkedCtrls = linkedCtrlMapper.selectList(
new LambdaQueryWrapper<LinkedCtrl>()
.in(LinkedCtrl::getDeviceId, deviceIds)
@@ -239,14 +262,7 @@ public class PondController extends BaseController {
java.util.Map<Long, List<LinkedCtrl>> linkedCtrlsByDevice = allLinkedCtrls.stream()
.collect(Collectors.groupingBy(LinkedCtrl::getDeviceId));
// 5. 批量查询塘口的所有开关
List<DeviceSwitch> allSwitches = deviceSwitchMapper.selectList(
new LambdaQueryWrapper<DeviceSwitch>()
.eq(DeviceSwitch::getPondId, pondId)
.orderByAsc(DeviceSwitch::getIndex)
);
// 6. 收集开关ID,查询定时控制
// 8. 收集开关ID查询定时控制
List<Long> 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<Long, List<DeviceSwitch>> switchesByDevice = allSwitches.stream()
.collect(Collectors.groupingBy(DeviceSwitch::getDeviceId));
// 8. 批量查询故障码
Set<Long> controllerIds = devices.stream()
// 10. 批量查询故障码(合并 devices + 开关关联设备 的所有 deviceId
Set<Long> allControllerIds = devices.stream()
.filter(d -> d.getDeviceType() != null && d.getDeviceType() == 2)
.map(Device::getId)
.collect(Collectors.toSet());
allControllerIds.addAll(switchDeviceIds);
List<DeviceErrorCode> errorCodes = new ArrayList<>();
if (!controllerIds.isEmpty()) {
if (!allControllerIds.isEmpty()) {
errorCodes = deviceErrorCodeMapper.selectList(
new LambdaQueryWrapper<DeviceErrorCode>()
.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<Long, Device> deviceById = devices.stream()
.collect(Collectors.toMap(Device::getId, d -> d, (a, b) -> a));
List<DeviceWithSwitchVo> controllerList = new ArrayList<>();
for (Device device : devices) {
if (device.getDeviceType() != null && device.getDeviceType() == 2) {
// 转换为DeviceVo再复制到DeviceWithSwitchVo
// 用 LinkedHashMap 保证控制器顺序,以 deviceId 为 key
java.util.LinkedHashMap<Long, DeviceWithSwitchVo> 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<DeviceSwitch> switches = switchesByDevice.getOrDefault(device.getId(), new ArrayList<>());
List<DeviceSwitchVo> 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<TimingCtrl> 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<TimingCtrl> 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);