Procházet zdrojové kódy

feat(cockpit): 设备详情预警报警补充字典等级名称

设备详情的事件列表原先只下发 level 数字,前端只能显示「4 级」。
维护者要求按字典口径显示(一级预警…),本次在事件快照中补 levelName。

- 新增 CockpitEventLevelName:预警取 alert_level、报警取 alarm_level 的 dict_label,
  与概览模块同一套「一/二/三/四级预警|报警」兜底措辞;等级缺失或越界回 null,不伪造;
  字典查询异常降级为空表,不让辅助展示拖垮整份事件数据。
- CockpitEventWarning / CockpitEventAlarm 增加 levelName,既有字段与行为不变。
- BusinessCockpitEventAdapter 注入 SysDictDataMapper,每族按请求批量查一次字典(族内无事件不查),
  不逐条回查。
- CockpitEventReadService 重建对象时透传 levelName(原先会被丢弃)。
- 新增 3 个测试类(12 例)并更新 CockpitEventReadControllerTest 断言。

验证:zksy-system -am test 291 通过;zksy-admin cockpit 包 742 通过。
Kazerin před 3 týdny
rodič
revize
ee33740f56

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

@@ -64,6 +64,7 @@ class CockpitEventReadControllerTest {
                 .andExpect(jsonPath("$.data.warnings[0].warningNo").value("WRN-20260902-001"))
                 .andExpect(jsonPath("$.data.warnings[0].level").value(1))
                 .andExpect(jsonPath("$.data.warnings[0].levelColor").value("RED"))
+                .andExpect(jsonPath("$.data.warnings[0].levelName").value("一级预警"))
                 .andExpect(jsonPath("$.data.warnings[0].status").value("PROCESSING"))
                 .andExpect(jsonPath("$.data.warnings[0].deviceCode").value(DEVICE_CODE))
                 .andExpect(jsonPath("$.data.warnings[1].level").value(2))
@@ -72,6 +73,7 @@ class CockpitEventReadControllerTest {
                 .andExpect(jsonPath("$.data.alarms[0].alarmId").value("ALM-20260902-001"))
                 .andExpect(jsonPath("$.data.alarms[0].level").value(3))
                 .andExpect(jsonPath("$.data.alarms[0].levelColor").value("BLUE"))
+                .andExpect(jsonPath("$.data.alarms[0].levelName").value("三级报警"))
                 .andExpect(jsonPath("$.data.alarms[0].status").value("UNHANDLED"))
                 .andExpect(jsonPath("$.data.alarms[0].deviceCode").value(DEVICE_CODE))
                 .andExpect(jsonPath("$.data.alarmWarningRelations", org.hamcrest.Matchers.hasSize(1)))
@@ -189,15 +191,15 @@ class CockpitEventReadControllerTest {
                 "污水环境监测设备",
                 Arrays.asList(
                         new CockpitEventWarning(
-                                "WRN-20260902-001", "污水COD预警", "WS_ENV", 1, "RED",
+                                "WRN-20260902-001", "污水COD预警", "WS_ENV", 1, "RED", "一级预警",
                                 "PROCESSING", "2026-09-02T09:59:30+08:00",
                                 new BigDecimal("18.2"), "mg/L", DEVICE_CODE, "污水环境监测设备", "太常片区"),
                         new CockpitEventWarning(
-                                "WRN-20260902-002", "污水pH预警", "WS_ENV", 2, "YELLOW",
+                                "WRN-20260902-002", "污水pH预警", "WS_ENV", 2, "YELLOW", "二级预警",
                                 "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", "WS_ENV_HIGH", "污水COD报警", "WS_ENV", 3, "BLUE", "三级报警",
                         "UNHANDLED", "2026-09-02T10:00:00+08:00",
                         new BigDecimal("42.0"), new BigDecimal("40.0"), new BigDecimal("50.0"), "mg/L",
                         DEVICE_CODE, "污水环境监测设备", "太常片区")),
@@ -227,7 +229,7 @@ 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", "WS_ENV_HIGH", "污水COD报警", "WS_ENV", 1, "RED", "一级报警",
                         "HANDLED", "2026-09-01T15:30:00+08:00",
                         new BigDecimal("45.0"), new BigDecimal("40.0"), new BigDecimal("50.0"), "mg/L",
                         DEVICE_CODE, "污水环境监测设备", "太常片区")),
@@ -245,7 +247,8 @@ class CockpitEventReadControllerTest {
 
     private CockpitEventWarning warning(String warningNo, Integer level) {
         return new CockpitEventWarning(
-                warningNo, "等级映射预警", "WS_ENV", level, CockpitEventLevelColor.resolve(level), "PENDING",
+                warningNo, "等级映射预警", "WS_ENV", level, CockpitEventLevelColor.resolve(level),
+                level == null ? null : level + "级预警", "PENDING",
                 "2026-09-02T09:59:30+08:00", null, "", DEVICE_CODE, "污水环境监测设备", "太常片区");
     }
 }

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

@@ -21,6 +21,7 @@ import com.zksy.cockpit.foundation.CockpitDistrict;
 import com.zksy.cockpit.foundation.CockpitTypeIndustry;
 import com.zksy.ops.domain.WorkOrder;
 import com.zksy.ops.service.WorkOrderService;
+import com.zksy.system.mapper.SysDictDataMapper;
 import org.springframework.context.annotation.Primary;
 import org.springframework.stereotype.Component;
 
@@ -71,6 +72,7 @@ public class BusinessCockpitEventAdapter {
     private final WarningTypeMapper refWarningTypeMapper;
     private final AlarmWarningRelMapper alarmWarningRelMapper;
     private final WorkOrderService workOrderService;
+    private final SysDictDataMapper sysDictDataMapper;
     private final Clock clock;
     private Map<Integer, WarningType> warningTypeById = Collections.emptyMap();
 
@@ -84,6 +86,7 @@ public class BusinessCockpitEventAdapter {
             WarningTypeMapper refWarningTypeMapper,
             AlarmWarningRelMapper alarmWarningRelMapper,
             WorkOrderService workOrderService,
+            SysDictDataMapper sysDictDataMapper,
             Clock clock) {
         this.equipmentBaseService = equipmentBaseService;
         this.equipmentTypeService = equipmentTypeService;
@@ -94,6 +97,7 @@ public class BusinessCockpitEventAdapter {
         this.refWarningTypeMapper = refWarningTypeMapper;
         this.alarmWarningRelMapper = alarmWarningRelMapper;
         this.workOrderService = workOrderService;
+        this.sysDictDataMapper = sysDictDataMapper;
         this.clock = clock;
     }
 
@@ -216,11 +220,17 @@ public class BusinessCockpitEventAdapter {
             List<WarningFact> warnings,
             List<AlarmFact> alarms,
             Integer total) {
+        // 等级名称按族批量取一次字典(每族每请求一次 selectDictDataByType),不逐事件回查;
+        // 该族没有事件时不查库。
+        Map<Integer, String> alertLevelLabels = warnings.isEmpty() ? Collections.emptyMap()
+                : CockpitEventLevelName.labels(sysDictDataMapper, CockpitEventLevelName.WARNING_LEVEL_DICT);
+        Map<Integer, String> alarmLevelLabels = alarms.isEmpty() ? Collections.emptyMap()
+                : CockpitEventLevelName.labels(sysDictDataMapper, CockpitEventLevelName.ALARM_LEVEL_DICT);
         List<CockpitEventWarning> warningItems = warnings.stream()
-                .map(item -> toEventWarning(item, facts))
+                .map(item -> toEventWarning(item, facts, alertLevelLabels))
                 .collect(Collectors.toList());
         List<CockpitEventAlarm> alarmItems = alarms.stream()
-                .map(item -> toEventAlarm(item, facts))
+                .map(item -> toEventAlarm(item, facts, alarmLevelLabels))
                 .collect(Collectors.toList());
         return new CockpitEventSnapshot(
                 device.getEquipmentCode().trim(),
@@ -232,7 +242,8 @@ public class BusinessCockpitEventAdapter {
                 total);
     }
 
-    private CockpitEventWarning toEventWarning(WarningFact item, Facts facts) {
+    private CockpitEventWarning toEventWarning(
+            WarningFact item, Facts facts, Map<Integer, String> alertLevelLabels) {
         EarlyWarning warning = item.warning;
         PointFact point = facts.pointsByDevice.get(normalize(warning.getDeviceCode()));
         return new CockpitEventWarning(
@@ -241,6 +252,7 @@ public class BusinessCockpitEventAdapter {
                 trimToNull(warning.getWarningType()),
                 level(warning.getWarningLevel()),
                 CockpitEventLevelColor.resolve(warning.getWarningLevel()),
+                CockpitEventLevelName.warningOf(level(warning.getWarningLevel()), alertLevelLabels),
                 trimToNull(warning.getStatus()),
                 offsetTime(warning.getPublishTime()),
                 null,
@@ -250,7 +262,8 @@ public class BusinessCockpitEventAdapter {
                 point == null ? null : point.location);
     }
 
-    private CockpitEventAlarm toEventAlarm(AlarmFact item, Facts facts) {
+    private CockpitEventAlarm toEventAlarm(
+            AlarmFact item, Facts facts, Map<Integer, String> alarmLevelLabels) {
         AlarmData alarm = item.alarm;
         PointFact point = facts.pointsByDevice.get(normalize(alarm.getDeviceCode()));
         EquipmentWarningType metadata = warningType(alarm, facts);
@@ -261,6 +274,7 @@ public class BusinessCockpitEventAdapter {
                 null,
                 level(alarm.getAlarmLevel()),
                 CockpitEventLevelColor.resolve(alarm.getAlarmLevel()),
+                CockpitEventLevelName.alarmOf(level(alarm.getAlarmLevel()), alarmLevelLabels),
                 alarmStatus(alarm.getAlarmStatus()),
                 offsetTime(alarm.getCreateTime()),
                 alarm.getActualValue(),

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

@@ -9,6 +9,7 @@ public class CockpitEventAlarm {
     private final String warningType;
     private final Integer level;
     private final String levelColor;
+    private final String levelName;
     private final String status;
     private final String eventTime;
     private final BigDecimal value;
@@ -26,6 +27,7 @@ public class CockpitEventAlarm {
             String warningType,
             Integer level,
             String levelColor,
+            String levelName,
             String status,
             String eventTime,
             BigDecimal value,
@@ -41,6 +43,7 @@ public class CockpitEventAlarm {
         this.warningType = warningType;
         this.level = level;
         this.levelColor = levelColor;
+        this.levelName = levelName;
         this.status = status;
         this.eventTime = eventTime;
         this.value = value;
@@ -58,6 +61,8 @@ public class CockpitEventAlarm {
     public String getWarningType() { return warningType; }
     public Integer getLevel() { return level; }
     public String getLevelColor() { return levelColor; }
+    /** 等级名称:字典 alarm_level 的 dict_label;缺项回退「N级报警」,等级缺失为 null。 */
+    public String getLevelName() { return levelName; }
     public String getStatus() { return status; }
     public String getEventTime() { return eventTime; }
     public BigDecimal getValue() { return value; }

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

@@ -0,0 +1,106 @@
+package com.zksy.cockpit.event;
+
+import com.zksy.common.core.domain.entity.SysDictData;
+import com.zksy.system.mapper.SysDictDataMapper;
+
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 设备详情预警/报警事件的等级展示名(票:等级显示字典口径)。
+ *
+ * <p>取值与概览模块同一口径:预警取字典 {@code alert_level}、报警取字典 {@code alarm_level}
+ * 的 {@code dict_label};字典缺项按既有「N级预警 / N级报警」兜底(前缀「一/二/三/四」),
+ * 不另造措辞。等级缺失或越界(非 1–4)如实回 {@code null},不伪造。</p>
+ *
+ * <p>字典按请求批量读取一次(每族一次 {@code selectDictDataByType}),不逐事件回查。</p>
+ */
+public final class CockpitEventLevelName {
+    public static final String WARNING_LEVEL_DICT = "alert_level";
+    public static final String ALARM_LEVEL_DICT = "alarm_level";
+    public static final String WARNING_SUFFIX = "预警";
+    public static final String ALARM_SUFFIX = "报警";
+
+    /** 与概览模块 {@code LEVEL_PREFIXES} 同一套中文序号,避免两处措辞分叉。 */
+    private static final String[] LEVEL_PREFIXES = {"一", "二", "三", "四"};
+
+    private CockpitEventLevelName() {
+    }
+
+    /**
+     * 一次取全一族的等级字典({@code dict_value → dict_label})。
+     *
+     * <p>字典只影响展示名:查询异常时退回空表,让事件列表按既有「N级」口径继续下发,
+     * 不因为一个辅助表象把整份事件数据判死。</p>
+     */
+    public static Map<Integer, String> labels(SysDictDataMapper dictMapper, String dictType) {
+        if (dictMapper == null || dictType == null) {
+            return Collections.emptyMap();
+        }
+        try {
+            return labels(dictMapper.selectDictDataByType(dictType));
+        } catch (RuntimeException ignored) {
+            return Collections.emptyMap();
+        }
+    }
+
+    /** 字典行 → 等级标签;仅取 {@code dict_value} 为 1–4 且标签非空的行,重复值保留首条。 */
+    public static Map<Integer, String> labels(List<SysDictData> source) {
+        Map<Integer, String> result = new LinkedHashMap<>();
+        if (source == null) {
+            return result;
+        }
+        for (SysDictData data : source) {
+            if (data == null) {
+                continue;
+            }
+            String label = trimToNull(data.getDictLabel());
+            Integer level = levelOf(data.getDictValue());
+            if (label != null && level != null) {
+                result.putIfAbsent(level, label);
+            }
+        }
+        return result;
+    }
+
+    /** 等级展示名:字典有标签用标签,缺项回退「N级<suffix>」;等级为空/越界回 {@code null}。 */
+    public static String of(Integer level, Map<Integer, String> dictLabels, String suffix) {
+        if (level == null || level < 1 || level > 4) {
+            return null;
+        }
+        String label = dictLabels == null ? null : trimToNull(dictLabels.get(level));
+        if (label != null) {
+            return label;
+        }
+        return LEVEL_PREFIXES[level - 1] + "级" + suffix;
+    }
+
+    /** 预警族:{@code alert_level} + 「N级预警」兜底。 */
+    public static String warningOf(Integer level, Map<Integer, String> alertLevelLabels) {
+        return of(level, alertLevelLabels, WARNING_SUFFIX);
+    }
+
+    /** 报警族:{@code alarm_level} + 「N级报警」兜底。 */
+    public static String alarmOf(Integer level, Map<Integer, String> alarmLevelLabels) {
+        return of(level, alarmLevelLabels, ALARM_SUFFIX);
+    }
+
+    private static Integer levelOf(String value) {
+        String normalized = trimToNull(value);
+        if (normalized == null) {
+            return null;
+        }
+        try {
+            int resolved = Integer.parseInt(normalized);
+            return resolved >= 1 && resolved <= 4 ? resolved : null;
+        } catch (NumberFormatException ignored) {
+            return null;
+        }
+    }
+
+    private static String trimToNull(String value) {
+        return value == null || value.trim().isEmpty() ? null : value.trim();
+    }
+}

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

@@ -120,8 +120,9 @@ public class CockpitEventReadService {
             }
             result.add(new CockpitEventWarning(
                     item.getWarningNo(), item.getWarningName(), item.getWarningType(), item.getLevel(),
-                    CockpitEventLevelColor.resolve(item.getLevel()), item.getStatus(), item.getEventTime(),
-                    item.getValue(), item.getUnit(), item.getDeviceCode(), item.getDeviceName(), item.getLocation()));
+                    CockpitEventLevelColor.resolve(item.getLevel()), item.getLevelName(), item.getStatus(),
+                    item.getEventTime(), item.getValue(), item.getUnit(), item.getDeviceCode(),
+                    item.getDeviceName(), item.getLocation()));
         }
         return result;
     }
@@ -134,9 +135,10 @@ public class CockpitEventReadService {
             }
             result.add(new CockpitEventAlarm(
                     item.getAlarmId(), item.getWarningCode(), item.getWarningName(), item.getWarningType(),
-                    item.getLevel(), CockpitEventLevelColor.resolve(item.getLevel()), item.getStatus(),
-                    item.getEventTime(), item.getValue(), item.getThresholdMinValue(), item.getThresholdMaxValue(),
-                    item.getUnit(), item.getDeviceCode(), item.getDeviceName(), item.getLocation()));
+                    item.getLevel(), CockpitEventLevelColor.resolve(item.getLevel()), item.getLevelName(),
+                    item.getStatus(), item.getEventTime(), item.getValue(), item.getThresholdMinValue(),
+                    item.getThresholdMaxValue(), item.getUnit(), item.getDeviceCode(), item.getDeviceName(),
+                    item.getLocation()));
         }
         return result;
     }

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

@@ -8,6 +8,7 @@ public class CockpitEventWarning {
     private final String warningType;
     private final Integer level;
     private final String levelColor;
+    private final String levelName;
     private final String status;
     private final String eventTime;
     private final BigDecimal value;
@@ -22,6 +23,7 @@ public class CockpitEventWarning {
             String warningType,
             Integer level,
             String levelColor,
+            String levelName,
             String status,
             String eventTime,
             BigDecimal value,
@@ -34,6 +36,7 @@ public class CockpitEventWarning {
         this.warningType = warningType;
         this.level = level;
         this.levelColor = levelColor;
+        this.levelName = levelName;
         this.status = status;
         this.eventTime = eventTime;
         this.value = value;
@@ -48,6 +51,8 @@ public class CockpitEventWarning {
     public String getWarningType() { return warningType; }
     public Integer getLevel() { return level; }
     public String getLevelColor() { return levelColor; }
+    /** 等级名称:字典 alert_level 的 dict_label;缺项回退「N级预警」,等级缺失为 null。 */
+    public String getLevelName() { return levelName; }
     public String getStatus() { return status; }
     public String getEventTime() { return eventTime; }
     public BigDecimal getValue() { return value; }

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

@@ -0,0 +1,196 @@
+package com.zksy.cockpit.event;
+
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
+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.domain.EarlyWarning;
+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.common.core.domain.entity.SysDictData;
+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.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+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.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * 设备详情事件适配器:等级名称按族取字典(预警 alert_level、报警 alarm_level),
+ * 缺项回退既有「N级预警 / N级报警」,且每族每请求只查一次字典。
+ */
+class BusinessCockpitEventAdapterLevelNameTest {
+    private static final String DEVICE_CODE = "TC-PS-WS-202609020012";
+
+    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 warningUsesAlertLevelDictAndAlarmUsesAlarmLevelDictInOneBatch() {
+        stubBaseFacts();
+        when(earlyWarningService.list()).thenReturn(Arrays.asList(
+                warning("WRN-1", "1"), warning("WRN-2", "2")));
+        // 报警等级写在 alarm_level 字典里;与预警字典刻意给不同的标签,证明按族取值。
+        when(alarmDataService.list()).thenReturn(Collections.singletonList(alarm("ALM-1", "4")));
+        when(sysDictDataMapper.selectDictDataByType(CockpitEventLevelName.WARNING_LEVEL_DICT))
+                .thenReturn(Collections.singletonList(dict("alert_level", "1", "一级预警")));
+        when(sysDictDataMapper.selectDictDataByType(CockpitEventLevelName.ALARM_LEVEL_DICT))
+                .thenReturn(Collections.singletonList(dict("alarm_level", "4", "四级报警")));
+
+        CockpitEventSnapshot snapshot = adapter.loadCurrent(DEVICE_CODE);
+
+        assertNotNull(snapshot);
+        // 字典命中:取 dict_label。
+        assertEquals("一级预警", snapshot.getWarnings().get(0).getLevelName());
+        // 字典缺项:回退既有「N级预警」口径,不猜、不硬编码前端。
+        assertEquals("二级预警", snapshot.getWarnings().get(1).getLevelName());
+        assertEquals("四级报警", snapshot.getAlarms().get(0).getLevelName());
+        // 既有字段与行为不变。
+        assertEquals(1, snapshot.getWarnings().get(0).getLevel());
+        assertEquals("RED", snapshot.getWarnings().get(0).getLevelColor());
+        assertEquals(4, snapshot.getAlarms().get(0).getLevel());
+        assertEquals("GREEN", snapshot.getAlarms().get(0).getLevelColor());
+        // 批量:每族一次字典查询,不随事件条数增长。
+        verify(sysDictDataMapper, times(1)).selectDictDataByType(CockpitEventLevelName.WARNING_LEVEL_DICT);
+        verify(sysDictDataMapper, times(1)).selectDictDataByType(CockpitEventLevelName.ALARM_LEVEL_DICT);
+    }
+
+    @Test
+    void dictionaryUnavailableFallsBackAndMissingLevelStaysNull() {
+        stubBaseFacts();
+        when(earlyWarningService.list()).thenReturn(Arrays.asList(
+                warning("WRN-1", "3"), warning("WRN-2", null)));
+        when(alarmDataService.list()).thenReturn(Collections.singletonList(alarm("ALM-1", "2")));
+        when(sysDictDataMapper.selectDictDataByType(any())).thenReturn(Collections.emptyList());
+
+        CockpitEventSnapshot snapshot = adapter.loadCurrent(DEVICE_CODE);
+
+        assertNotNull(snapshot);
+        assertEquals("三级预警", snapshot.getWarnings().get(0).getLevelName());
+        assertNull(snapshot.getWarnings().get(1).getLevel());
+        assertNull(snapshot.getWarnings().get(1).getLevelName());
+        assertEquals("二级报警", snapshot.getAlarms().get(0).getLevelName());
+    }
+
+    @Test
+    void dictionaryFailureDegradesToFallbackInsteadOfFailingTheEvents() {
+        stubBaseFacts();
+        when(earlyWarningService.list()).thenReturn(Collections.singletonList(warning("WRN-1", "2")));
+        when(alarmDataService.list()).thenReturn(Collections.singletonList(alarm("ALM-1", "1")));
+        when(sysDictDataMapper.selectDictDataByType(CockpitEventLevelName.WARNING_LEVEL_DICT))
+                .thenThrow(new IllegalStateException("dict table unavailable"));
+        when(sysDictDataMapper.selectDictDataByType(CockpitEventLevelName.ALARM_LEVEL_DICT))
+                .thenThrow(new IllegalStateException("dict table unavailable"));
+
+        CockpitEventSnapshot snapshot = adapter.loadCurrent(DEVICE_CODE);
+
+        assertNotNull(snapshot);
+        assertEquals("二级预警", snapshot.getWarnings().get(0).getLevelName());
+        assertEquals("一级报警", snapshot.getAlarms().get(0).getLevelName());
+    }
+
+    @Test
+    void familyWithoutEventsDoesNotTouchItsDictionary() {
+        stubBaseFacts();
+        when(earlyWarningService.list()).thenReturn(Collections.singletonList(warning("WRN-1", "1")));
+        when(alarmDataService.list()).thenReturn(Collections.emptyList());
+        when(sysDictDataMapper.selectDictDataByType(CockpitEventLevelName.WARNING_LEVEL_DICT))
+                .thenReturn(Collections.singletonList(dict("alert_level", "1", "一级预警")));
+
+        CockpitEventSnapshot snapshot = adapter.loadCurrent(DEVICE_CODE);
+
+        assertNotNull(snapshot);
+        assertEquals("一级预警", snapshot.getWarnings().get(0).getLevelName());
+        verify(sysDictDataMapper, times(1)).selectDictDataByType(CockpitEventLevelName.WARNING_LEVEL_DICT);
+        verify(sysDictDataMapper, times(0)).selectDictDataByType(CockpitEventLevelName.ALARM_LEVEL_DICT);
+    }
+
+    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 EarlyWarning warning(String warningNo, String level) {
+        EarlyWarning warning = new EarlyWarning();
+        warning.setWarningId("WID-" + warningNo);
+        warning.setWarningNo(warningNo);
+        warning.setWarningType("WS_ENV");
+        warning.setWarningLevel(level);
+        warning.setStatus("PROCESSING");
+        warning.setPublishTime(LocalDateTime.of(2026, 9, 2, 9, 59, 30));
+        warning.setDeviceCode(DEVICE_CODE);
+        warning.setDeviceName("污水环境监测设备");
+        return warning;
+    }
+
+    private static AlarmData alarm(String id, String level) {
+        AlarmData alarm = new AlarmData();
+        alarm.setId(id);
+        alarm.setAlarmNo(id);
+        alarm.setWarningCode("WS_ENV_HIGH");
+        alarm.setAlarmLevel(level);
+        alarm.setAlarmStatus(0);
+        alarm.setCreateTime(LocalDateTime.of(2026, 9, 2, 10, 0, 0));
+        alarm.setDeviceCode(DEVICE_CODE);
+        alarm.setDeviceName("污水环境监测设备");
+        return alarm;
+    }
+
+    private static SysDictData dict(String dictType, String value, String label) {
+        SysDictData data = new SysDictData();
+        data.setDictType(dictType);
+        data.setDictValue(value);
+        data.setDictLabel(label);
+        data.setStatus("0");
+        return data;
+    }
+}

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

@@ -0,0 +1,66 @@
+package com.zksy.cockpit.event;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.junit.jupiter.api.Test;
+
+import java.math.BigDecimal;
+import java.util.Collections;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/** 事件 DTO 的对外 JSON 形状:新增 levelName 与既有 level/levelColor 并存,缺名时为 null。 */
+class CockpitEventLevelNameSerializationTest {
+    private final ObjectMapper mapper = new ObjectMapper();
+
+    @Test
+    void warningAndAlarmSerializeLevelNameAlongsideLegacyLevelFields() throws Exception {
+        CockpitEventWarning warning = new CockpitEventWarning(
+                "WRN-1", "污水COD预警", "WS_ENV", 1, "RED", "一级预警", "PROCESSING",
+                "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", "四级报警",
+                "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", "污水环境监测设备", "太常片区");
+
+        JsonNode warningJson = mapper.readTree(mapper.writeValueAsString(warning));
+        assertEquals("一级预警", warningJson.get("levelName").asText());
+        assertEquals(1, warningJson.get("level").asInt());
+        assertEquals("RED", warningJson.get("levelColor").asText());
+
+        JsonNode alarmJson = mapper.readTree(mapper.writeValueAsString(alarm));
+        assertEquals("四级报警", alarmJson.get("levelName").asText());
+        assertEquals(4, alarmJson.get("level").asInt());
+        assertEquals("GREEN", alarmJson.get("levelColor").asText());
+    }
+
+    @Test
+    void missingLevelKeepsLegacyUnknownColorAndNullLevelName() throws Exception {
+        CockpitEventWarning warning = new CockpitEventWarning(
+                "WRN-2", "无等级预警", "WS_ENV", null, "UNKNOWN", null, "PENDING",
+                null, null, null, null, null, null);
+
+        JsonNode json = mapper.readTree(mapper.writeValueAsString(warning));
+
+        assertTrue(json.has("levelName"));
+        assertTrue(json.get("levelName").isNull());
+        assertTrue(json.get("level").isNull());
+        assertEquals("UNKNOWN", json.get("levelColor").asText());
+    }
+
+    @Test
+    void emptyBatchStillSerializesEmptyFamilies() throws Exception {
+        CockpitEventSnapshot snapshot = new CockpitEventSnapshot(
+                "TC-PS-WS-202609020012", "ws_sewage_environment", "污水环境监测设备",
+                Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), 0);
+
+        JsonNode json = mapper.readTree(mapper.writeValueAsString(snapshot));
+
+        assertEquals(0, json.get("warnings").size());
+        assertEquals(0, json.get("alarms").size());
+        assertEquals(0, json.get("total").asInt());
+    }
+}

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

@@ -0,0 +1,114 @@
+package com.zksy.cockpit.event;
+
+import com.zksy.common.core.domain.entity.SysDictData;
+import com.zksy.system.mapper.SysDictDataMapper;
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
+/** 事件等级展示名:字典命中、缺项兜底、缺失等级不伪造,以及每族只查一次字典。 */
+class CockpitEventLevelNameTest {
+
+    @Test
+    void dictionaryLabelWinsOverFallbackForBothFamilies() {
+        Map<Integer, String> alert = CockpitEventLevelName.labels(Arrays.asList(
+                dict("alert_level", "1", "一级预警"),
+                dict("alert_level", "2", "二级预警"),
+                dict("alert_level", "3", "三级预警"),
+                dict("alert_level", "4", "四级预警")));
+        Map<Integer, String> alarm = CockpitEventLevelName.labels(Arrays.asList(
+                dict("alarm_level", "1", "一级报警"),
+                dict("alarm_level", "4", "四级报警")));
+
+        assertEquals("一级预警", CockpitEventLevelName.warningOf(1, alert));
+        assertEquals("四级预警", CockpitEventLevelName.warningOf(4, alert));
+        assertEquals("一级报警", CockpitEventLevelName.alarmOf(1, alarm));
+        assertEquals("四级报警", CockpitEventLevelName.alarmOf(4, alarm));
+    }
+
+    @Test
+    void missingDictionaryEntryFallsBackToTheExistingChineseLevelWording() {
+        Map<Integer, String> onlyLevelOne = CockpitEventLevelName.labels(
+                Collections.singletonList(dict("alert_level", "1", "一级预警")));
+
+        assertEquals("一级预警", CockpitEventLevelName.warningOf(1, onlyLevelOne));
+        assertEquals("二级预警", CockpitEventLevelName.warningOf(2, onlyLevelOne));
+        assertEquals("三级预警", CockpitEventLevelName.warningOf(3, onlyLevelOne));
+        assertEquals("四级预警", CockpitEventLevelName.warningOf(4, onlyLevelOne));
+
+        // 整族字典缺失(含查询异常回空表)同样按既有口径兜底,不猜、不抛。
+        assertEquals("三级报警", CockpitEventLevelName.alarmOf(3, Collections.emptyMap()));
+        assertEquals("二级报警", CockpitEventLevelName.alarmOf(2, null));
+    }
+
+    @Test
+    void absentOrOutOfRangeLevelHasNoName() {
+        Map<Integer, String> alert = CockpitEventLevelName.labels(
+                Collections.singletonList(dict("alert_level", "1", "一级预警")));
+
+        assertNull(CockpitEventLevelName.warningOf(null, alert));
+        assertNull(CockpitEventLevelName.warningOf(0, alert));
+        assertNull(CockpitEventLevelName.warningOf(5, alert));
+        assertNull(CockpitEventLevelName.alarmOf(null, null));
+    }
+
+    @Test
+    void blankLabelAndInvalidDictValueAreIgnoredInsteadOfInventingAName() {
+        Map<Integer, String> alert = CockpitEventLevelName.labels(Arrays.asList(
+                dict("alert_level", "1", "  "),
+                dict("alert_level", "x", "非数字"),
+                dict("alert_level", "9", "越界标签"),
+                null,
+                dict("alert_level", "2", "二级预警")));
+
+        assertEquals(Collections.singletonMap(2, "二级预警"), alert);
+        // 标签缺失仍按兜底口径,而不是把空串当作名字下发。
+        assertEquals("一级预警", CockpitEventLevelName.warningOf(1, alert));
+    }
+
+    @Test
+    void eachFamilyIsQueriedOncePerBatchNotPerEvent() {
+        SysDictDataMapper dictMapper = mock(SysDictDataMapper.class);
+        List<SysDictData> alertRows = Arrays.asList(
+                dict("alert_level", "1", "一级预警"), dict("alert_level", "4", "四级预警"));
+        when(dictMapper.selectDictDataByType(CockpitEventLevelName.WARNING_LEVEL_DICT)).thenReturn(alertRows);
+        when(dictMapper.selectDictDataByType(CockpitEventLevelName.ALARM_LEVEL_DICT))
+                .thenReturn(Collections.emptyList());
+
+        Map<Integer, String> alert = CockpitEventLevelName.labels(
+                dictMapper, CockpitEventLevelName.WARNING_LEVEL_DICT);
+        Map<Integer, String> alarm = CockpitEventLevelName.labels(
+                dictMapper, CockpitEventLevelName.ALARM_LEVEL_DICT);
+
+        // 反复取标签只读本地映射,不再回查字典。
+        for (int level = 1; level <= 4; level++) {
+            CockpitEventLevelName.warningOf(level, alert);
+            CockpitEventLevelName.alarmOf(level, alarm);
+        }
+        verify(dictMapper).selectDictDataByType(CockpitEventLevelName.WARNING_LEVEL_DICT);
+        verify(dictMapper).selectDictDataByType(CockpitEventLevelName.ALARM_LEVEL_DICT);
+        verifyNoMoreInteractions(dictMapper);
+        assertEquals("一级预警", CockpitEventLevelName.warningOf(1, alert));
+        assertEquals("四级预警", CockpitEventLevelName.warningOf(4, alert));
+        assertEquals("一级报警", CockpitEventLevelName.alarmOf(1, alarm));
+    }
+
+    private static SysDictData dict(String dictType, String value, String label) {
+        SysDictData data = new SysDictData();
+        data.setDictType(dictType);
+        data.setDictValue(value);
+        data.setDictLabel(label);
+        data.setStatus("0");
+        return data;
+    }
+}