Przeglądaj źródła

feat(cockpit): 报警事件暴露业务单号 alarmNo

alarm_data.alarm_no 是报警业务单号,但驾驶舱 CockpitEventAlarm 只暴露 alarmId(主键哈希),
界面只能把内部主键当标识显示。本次补出单号供界面使用。

- CockpitEventAlarm 增加 alarmNo(紧跟 alarmId),既有字段名与顺序不变,保留旧构造(alarmNo 为 null)。
- 适配器取同一条已加载 alarm_data 行的 alarm_no 并 trimToNull,零新增查询。
- CockpitEventReadService 重建对象时透传,避免这条路径丢字段。
- 缺失或空白一律 null,不拿主键顶替、不伪造。

验证:zksy-system 305 通过(事件包定向 20 例全绿)、zksy-admin cockpit 包 749 通过。
Kazerin 3 tygodni temu
rodzic
commit
729c764602

+ 6 - 2
pipe-network-service/zksy-admin/src/test/java/com/zksy/web/controller/cockpit/CockpitEventReadControllerTest.java

@@ -74,6 +74,7 @@ class CockpitEventReadControllerTest {
                 .andExpect(jsonPath("$.data.warnings[1].levelColor").value("YELLOW"))
                 .andExpect(jsonPath("$.data.alarms", org.hamcrest.Matchers.hasSize(1)))
                 .andExpect(jsonPath("$.data.alarms[0].alarmId").value("ALM-20260902-001"))
+                .andExpect(jsonPath("$.data.alarms[0].alarmNo").value("BJ202609291243545660"))
                 .andExpect(jsonPath("$.data.alarms[0].level").value(3))
                 .andExpect(jsonPath("$.data.alarms[0].levelColor").value("BLUE"))
                 .andExpect(jsonPath("$.data.alarms[0].levelName").value("三级报警"))
@@ -120,6 +121,7 @@ class CockpitEventReadControllerTest {
                 .andExpect(jsonPath("$.data.page").value(1))
                 .andExpect(jsonPath("$.data.pageSize").value(100))
                 .andExpect(jsonPath("$.data.total").value(3))
+                .andExpect(jsonPath("$.data.alarms[0].alarmNo").value(org.hamcrest.Matchers.nullValue()))
                 .andExpect(jsonPath("$.data.warnings", org.hamcrest.Matchers.hasSize(2)))
                 .andExpect(jsonPath("$.data.alarms", org.hamcrest.Matchers.hasSize(1)))
                 .andExpect(jsonPath("$.data.alarmWarningRelations", org.hamcrest.Matchers.hasSize(1)));
@@ -205,7 +207,8 @@ class CockpitEventReadControllerTest {
                                 "PENDING", "2026-09-02T09:58:30+08:00",
                                 new BigDecimal("8.1"), "", DEVICE_CODE, "污水环境监测设备", "太常片区")),
                 Collections.singletonList(new CockpitEventAlarm(
-                        "ALM-20260902-001", "WS_ENV_HIGH", "污水COD报警", "WS_ENV", 3, "BLUE", "三级报警",
+                        "ALM-20260902-001", "BJ202609291243545660", "WS_ENV_HIGH", "污水COD报警", "WS_ENV",
+                        3, "BLUE", "三级报警",
                         "化学需氧量(COD)报警超过上限", "UNHANDLED", "2026-09-02T10:00:00+08:00",
                         new BigDecimal("42.0"), new BigDecimal("40.0"), new BigDecimal("50.0"), "mg/L",
                         DEVICE_CODE, "污水环境监测设备", "太常片区")),
@@ -235,7 +238,8 @@ class CockpitEventReadControllerTest {
                 "污水环境监测设备",
                 Arrays.asList(warning("WRN-20260901-001", 2), warning("WRN-20260901-002", 4)),
                 Collections.singletonList(new CockpitEventAlarm(
-                        "ALM-20260901-001", "WS_ENV_HIGH", "污水COD报警", "WS_ENV", 1, "RED", "一级报警",
+                        "ALM-20260901-001", null, "WS_ENV_HIGH", "污水COD报警", "WS_ENV",
+                        1, "RED", "一级报警",
                         null, "HANDLED", "2026-09-01T15:30:00+08:00",
                         new BigDecimal("45.0"), new BigDecimal("40.0"), new BigDecimal("50.0"), "mg/L",
                         DEVICE_CODE, "污水环境监测设备", "太常片区")),

+ 2 - 0
pipe-network-service/zksy-system/src/main/java/com/zksy/cockpit/event/BusinessCockpitEventAdapter.java

@@ -271,6 +271,8 @@ public class BusinessCockpitEventAdapter {
         EquipmentWarningType metadata = warningType(alarm, facts);
         return new CockpitEventAlarm(
                 trimToNull(alarm.getId()),
+                // 报警业务单号来自同一条已加载的 alarm_data 行,原样透传,缺失为 null。
+                trimToNull(alarm.getAlarmNo()),
                 trimToNull(alarm.getWarningCode()),
                 firstNonBlank(warningTypeName(metadata), alarm.getWarningCode()),
                 null,

+ 30 - 0
pipe-network-service/zksy-system/src/main/java/com/zksy/cockpit/event/CockpitEventAlarm.java

@@ -4,6 +4,8 @@ import java.math.BigDecimal;
 
 public class CockpitEventAlarm {
     private final String alarmId;
+    /** 报警业务单号({@code alarm_data.alarm_no}),供界面显示可读单号;缺失为 null。 */
+    private final String alarmNo;
     private final String warningCode;
     private final String warningName;
     private final String warningType;
@@ -23,6 +25,7 @@ public class CockpitEventAlarm {
 
     public CockpitEventAlarm(
             String alarmId,
+            String alarmNo,
             String warningCode,
             String warningName,
             String warningType,
@@ -40,6 +43,7 @@ public class CockpitEventAlarm {
             String deviceName,
             String location) {
         this.alarmId = alarmId;
+        this.alarmNo = alarmNo;
         this.warningCode = warningCode;
         this.warningName = warningName;
         this.warningType = warningType;
@@ -58,7 +62,33 @@ public class CockpitEventAlarm {
         this.location = location;
     }
 
+    /** 兼容旧构造(不提供报警单号):alarmNo 如实为 null,不伪造。 */
+    public CockpitEventAlarm(
+            String alarmId,
+            String warningCode,
+            String warningName,
+            String warningType,
+            Integer level,
+            String levelColor,
+            String levelName,
+            String content,
+            String status,
+            String eventTime,
+            BigDecimal value,
+            BigDecimal thresholdMinValue,
+            BigDecimal thresholdMaxValue,
+            String unit,
+            String deviceCode,
+            String deviceName,
+            String location) {
+        this(alarmId, null, warningCode, warningName, warningType, level, levelColor, levelName,
+                content, status, eventTime, value, thresholdMinValue, thresholdMaxValue, unit,
+                deviceCode, deviceName, location);
+    }
+
     public String getAlarmId() { return alarmId; }
+    /** 报警业务单号(alarm_data.alarm_no),原样透传;缺失 null。 */
+    public String getAlarmNo() { return alarmNo; }
     public String getWarningCode() { return warningCode; }
     public String getWarningName() { return warningName; }
     public String getWarningType() { return warningType; }

+ 2 - 1
pipe-network-service/zksy-system/src/main/java/com/zksy/cockpit/event/CockpitEventReadService.java

@@ -134,7 +134,8 @@ public class CockpitEventReadService {
                 continue;
             }
             result.add(new CockpitEventAlarm(
-                    item.getAlarmId(), item.getWarningCode(), item.getWarningName(), item.getWarningType(),
+                    item.getAlarmId(), item.getAlarmNo(), item.getWarningCode(), item.getWarningName(),
+                    item.getWarningType(),
                     item.getLevel(), CockpitEventLevelColor.resolve(item.getLevel()), item.getLevelName(),
                     item.getContent(), item.getStatus(), item.getEventTime(), item.getValue(),
                     item.getThresholdMinValue(), item.getThresholdMaxValue(), item.getUnit(),

+ 181 - 0
pipe-network-service/zksy-system/src/test/java/com/zksy/cockpit/event/CockpitEventAlarmNoTest.java

@@ -0,0 +1,181 @@
+package com.zksy.cockpit.event;
+
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.zksy.base.alarm.domain.AlarmData;
+import com.zksy.base.alarm.service.AlarmDataService;
+import com.zksy.base.domain.EquipmentBase;
+import com.zksy.base.domain.EquipmentType;
+import com.zksy.base.service.EquipmentBaseService;
+import com.zksy.base.service.EquipmentTypeService;
+import com.zksy.base.service.MonitorPointService;
+import com.zksy.base.warning.mapper.AlarmWarningRelMapper;
+import com.zksy.base.warning.mapper.EquipmentWarningTypeMapper;
+import com.zksy.base.warning.mapper.WarningTypeMapper;
+import com.zksy.base.warning.service.EarlyWarningService;
+import com.zksy.ops.service.WorkOrderService;
+import com.zksy.system.mapper.SysDictDataMapper;
+import org.junit.jupiter.api.Test;
+
+import java.time.Clock;
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.time.ZoneOffset;
+import java.util.Collections;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * 报警业务单号(alarmNo):取自 {@code alarm_data.alarm_no},原样透传;
+ * 主键 {@code alarmId} 仍在,二者并存,缺失时如实 null。
+ */
+class CockpitEventAlarmNoTest {
+    private static final String DEVICE_CODE = "TC-PS-WS-202609020012";
+    private static final String ALARM_ID = "b4dc6387944a1585a9b43dfca5cf680f";
+    private static final String ALARM_NO = "BJ202609291243545660";
+
+    private final EquipmentBaseService equipmentBaseService = mock(EquipmentBaseService.class);
+    private final EquipmentTypeService equipmentTypeService = mock(EquipmentTypeService.class);
+    private final MonitorPointService monitorPointService = mock(MonitorPointService.class);
+    private final EarlyWarningService earlyWarningService = mock(EarlyWarningService.class);
+    private final AlarmDataService alarmDataService = mock(AlarmDataService.class);
+    private final EquipmentWarningTypeMapper warningTypeMapper = mock(EquipmentWarningTypeMapper.class);
+    private final WarningTypeMapper refWarningTypeMapper = mock(WarningTypeMapper.class);
+    private final AlarmWarningRelMapper alarmWarningRelMapper = mock(AlarmWarningRelMapper.class);
+    private final WorkOrderService workOrderService = mock(WorkOrderService.class);
+    private final SysDictDataMapper sysDictDataMapper = mock(SysDictDataMapper.class);
+
+    private final BusinessCockpitEventAdapter adapter = new BusinessCockpitEventAdapter(
+            equipmentBaseService, equipmentTypeService, monitorPointService, earlyWarningService,
+            alarmDataService, warningTypeMapper, refWarningTypeMapper, alarmWarningRelMapper,
+            workOrderService, sysDictDataMapper,
+            Clock.fixed(Instant.parse("2026-09-02T02:00:00Z"), ZoneOffset.UTC));
+
+    @Test
+    void adapterExposesBusinessAlarmNoAlongsidePrimaryKey() {
+        stubBaseFacts();
+        AlarmData alarm = alarm(ALARM_ID, ALARM_NO);
+        when(alarmDataService.list()).thenReturn(Collections.singletonList(alarm));
+        when(earlyWarningService.list()).thenReturn(Collections.emptyList());
+        when(sysDictDataMapper.selectDictDataByType(any())).thenReturn(Collections.emptyList());
+
+        CockpitEventAlarm item = adapter.loadCurrent(DEVICE_CODE).getAlarms().get(0);
+
+        // 业务单号与主键并存:界面显示可读单号,主键继续供回联使用
+        assertEquals(ALARM_NO, item.getAlarmNo());
+        assertEquals(ALARM_ID, item.getAlarmId());
+        // 既有字段不受影响
+        assertEquals("WS_ENV_HIGH", item.getWarningCode());
+        assertEquals(2, item.getLevel());
+    }
+
+    @Test
+    void missingAlarmNoStaysNullAndDoesNotFallBackToPrimaryKey() {
+        stubBaseFacts();
+        when(alarmDataService.list()).thenReturn(Collections.singletonList(alarm(ALARM_ID, null)));
+        when(earlyWarningService.list()).thenReturn(Collections.emptyList());
+        when(sysDictDataMapper.selectDictDataByType(any())).thenReturn(Collections.emptyList());
+
+        CockpitEventAlarm item = adapter.loadCurrent(DEVICE_CODE).getAlarms().get(0);
+
+        assertNull(item.getAlarmNo(), "缺单号就该是 null,不得把主键冒充单号");
+        assertEquals(ALARM_ID, item.getAlarmId());
+    }
+
+    @Test
+    void blankAlarmNoIsNormalizedToNull() {
+        stubBaseFacts();
+        when(alarmDataService.list()).thenReturn(Collections.singletonList(alarm(ALARM_ID, "   ")));
+        when(earlyWarningService.list()).thenReturn(Collections.emptyList());
+        when(sysDictDataMapper.selectDictDataByType(any())).thenReturn(Collections.emptyList());
+
+        assertNull(adapter.loadCurrent(DEVICE_CODE).getAlarms().get(0).getAlarmNo());
+    }
+
+    @Test
+    void rebuildInReadServiceKeepsAlarmNoInsteadOfDroppingIt() throws Exception {
+        CockpitEventAlarm source = new CockpitEventAlarm(
+                ALARM_ID, ALARM_NO, "WS_ENV_HIGH", "污水COD报警", "WS_ENV", 2, "YELLOW", "二级报警",
+                "化学需氧量(COD)报警超过上限", "UNHANDLED", "2026-09-02T10:00:00+08:00",
+                null, null, null, "mg/L", DEVICE_CODE, "污水环境监测设备", "太常片区");
+
+        // decorateAlarms 是 ReadService 内部重建路径(返回新的 DTO 实例),必须照抄 alarmNo。
+        java.lang.reflect.Method decorate = CockpitEventReadService.class
+                .getDeclaredMethod("decorateAlarms", java.util.List.class);
+        decorate.setAccessible(true);
+        java.util.List<CockpitEventAlarm> decorated = (java.util.List<CockpitEventAlarm>) decorate.invoke(
+                new CockpitEventReadService(
+                        mock(CockpitEventReadAdapter.class),
+                        Clock.fixed(Instant.parse("2026-09-02T02:00:00Z"), ZoneOffset.UTC)),
+                Collections.singletonList(source));
+
+        assertNotNull(decorated);
+        assertEquals(1, decorated.size());
+        assertNotNull(decorated.get(0));
+        assertEquals(ALARM_NO, decorated.get(0).getAlarmNo(), "重建对象不得丢单号");
+        assertEquals(ALARM_ID, decorated.get(0).getAlarmId());
+        assertEquals("二级报警", decorated.get(0).getLevelName());
+        assertEquals("化学需氧量(COD)报警超过上限", decorated.get(0).getContent());
+    }
+
+    @Test
+    void alarmNoSerializesAsPlainStringAndNullWhenMissing() throws Exception {
+        ObjectMapper mapper = new ObjectMapper();
+        CockpitEventAlarm withNo = new CockpitEventAlarm(
+                ALARM_ID, ALARM_NO, "WS_ENV_HIGH", "污水COD报警", "WS_ENV", 2, "YELLOW", "二级报警",
+                null, "UNHANDLED", "2026-09-02T10:00:00+08:00", null, null, null, null,
+                DEVICE_CODE, "污水环境监测设备", "太常片区");
+        CockpitEventAlarm withoutNo = new CockpitEventAlarm(
+                ALARM_ID, null, "WS_ENV_HIGH", "污水COD报警", "WS_ENV", 2, "YELLOW", "二级报警",
+                null, "UNHANDLED", "2026-09-02T10:00:00+08:00", null, null, null, null,
+                DEVICE_CODE, "污水环境监测设备", "太常片区");
+
+        JsonNode json = mapper.readTree(mapper.writeValueAsString(withNo));
+        JsonNode missing = mapper.readTree(mapper.writeValueAsString(withoutNo));
+
+        assertEquals(ALARM_NO, json.get("alarmNo").asText());
+        assertEquals(ALARM_ID, json.get("alarmId").asText());
+        // 缺失时键仍存在、值为 null(前端可据此回落),不是把键删掉
+        org.junit.jupiter.api.Assertions.assertTrue(missing.has("alarmNo"));
+        assertNull(missing.get("alarmNo").textValue());
+    }
+
+    private void stubBaseFacts() {
+        EquipmentType type = new EquipmentType();
+        type.setId(7);
+        type.setTypeName("污水环境监测设备");
+        when(equipmentTypeService.list()).thenReturn(Collections.singletonList(type));
+
+        EquipmentBase device = new EquipmentBase();
+        device.setEquipmentId("EQ-1");
+        device.setEquipmentCode(DEVICE_CODE);
+        device.setEquipmentName("污水环境监测设备");
+        device.setEquipmentTypeId(7);
+        when(equipmentBaseService.list()).thenReturn(Collections.singletonList(device));
+        when(equipmentBaseService.getEquipmentByPoint(any())).thenReturn(Collections.emptyList());
+
+        when(monitorPointService.list()).thenReturn(Collections.emptyList());
+        when(warningTypeMapper.selectList(any(Wrapper.class))).thenReturn(Collections.emptyList());
+        when(alarmWarningRelMapper.selectList(any(Wrapper.class))).thenReturn(Collections.emptyList());
+        when(workOrderService.list()).thenReturn(Collections.emptyList());
+    }
+
+    private static AlarmData alarm(String id, String alarmNo) {
+        AlarmData alarm = new AlarmData();
+        alarm.setId(id);
+        alarm.setAlarmNo(alarmNo);
+        alarm.setWarningCode("WS_ENV_HIGH");
+        alarm.setAlarmLevel("2");
+        alarm.setAlarmStatus(0);
+        alarm.setCreateTime(LocalDateTime.of(2026, 9, 2, 10, 0, 0));
+        alarm.setDeviceCode(DEVICE_CODE);
+        alarm.setDeviceName("污水环境监测设备");
+        return alarm;
+    }
+}

+ 1 - 1
pipe-network-service/zksy-system/src/test/java/com/zksy/cockpit/event/CockpitEventContentTest.java

@@ -116,7 +116,7 @@ class CockpitEventContentTest {
                 WARNING_CONTENT, "PROCESSING", "2026-09-02T09:59:30+08:00",
                 null, null, DEVICE_CODE, "污水环境监测设备", "太常片区");
         CockpitEventAlarm alarm = new CockpitEventAlarm(
-                "ALM-1", "WS_ENV_HIGH", "污水COD报警", "WS_ENV", 2, "YELLOW", "二级报警",
+                "ALM-1", null, "WS_ENV_HIGH", "污水COD报警", "WS_ENV", 2, "YELLOW", "二级报警",
                 null, "UNHANDLED", "2026-09-02T10:00:00+08:00", null, null, null, null,
                 DEVICE_CODE, "污水环境监测设备", "太常片区");
 

+ 2 - 1
pipe-network-service/zksy-system/src/test/java/com/zksy/cockpit/event/CockpitEventLevelNameSerializationTest.java

@@ -21,7 +21,7 @@ class CockpitEventLevelNameSerializationTest {
                 "2026-09-02T09:59:30+08:00", new BigDecimal("18.2"), "mg/L",
                 "TC-PS-WS-202609020012", "污水环境监测设备", "太常片区");
         CockpitEventAlarm alarm = new CockpitEventAlarm(
-                "ALM-1", "WS_ENV_HIGH", "污水COD报警", "WS_ENV", 4, "GREEN", "四级报警",
+                "ALM-1", "BJ202609291243545660", "WS_ENV_HIGH", "污水COD报警", "WS_ENV", 4, "GREEN", "四级报警",
                 "化学需氧量(COD)报警超过上限", "UNHANDLED", "2026-09-02T10:00:00+08:00", new BigDecimal("42.0"),
                 new BigDecimal("40.0"), new BigDecimal("50.0"), "mg/L",
                 "TC-PS-WS-202609020012", "污水环境监测设备", "太常片区");
@@ -32,6 +32,7 @@ class CockpitEventLevelNameSerializationTest {
         assertEquals("RED", warningJson.get("levelColor").asText());
 
         JsonNode alarmJson = mapper.readTree(mapper.writeValueAsString(alarm));
+        assertEquals("BJ202609291243545660", alarmJson.get("alarmNo").asText());
         assertEquals("四级报警", alarmJson.get("levelName").asText());
         assertEquals(4, alarmJson.get("level").asInt());
         assertEquals("GREEN", alarmJson.get("levelColor").asText());