Kaynağa Gözat

feat(cockpit): 字典端点透传风险与隐患六类字典

票 04(.scratch/cockpit-emergency-detail)。应急保障详情与预警详情的风险等级、
风险颜色、隐患类型/等级/来源/状态原来只拿得到码值,前端被迫内置第二份词表。

- /dict/{dictType} 白名单由 pump_station_type 扩到七类(ALLOWED_DICT_TYPES),新增
  risk_level、risk_color、hazard_type、hazard_level、hazard_source、hazard_status
- 仍走 ISysDictTypeService.selectDictDataByType,读 sys_dict_data(status='0'、
  order by dict_sort),前后端都不落词表;契约外类型照旧返回空列表且不读库
- 测试:六类逐类透传改参数化(失败时直接报出是哪类字典),补「白名单字典无已发布行
  返回空」覆盖 rows==null 分支与「契约外字典不读库」断言;CockpitDictControllerTest 10/10

zksy-admin 783 项全绿;zksy-system 4 项失败为既有红点
(CollectionRecordAssessmentServiceTest 2、LegacyPipeTableRetirementTest 1、
PipeNetworkStatusDictionaryContractTest 1),与本次改动无关。
Kazerin 3 gün önce
ebeveyn
işleme
cb51148afd

+ 3 - 3
pipe-network-service/zksy-admin/src/main/java/com/zksy/web/controller/cockpit/CockpitDictController.java

@@ -23,9 +23,9 @@ import java.util.stream.Collectors;
 @RequestMapping("/api/cockpit/v1")
 public class CockpitDictController extends BaseCockpitController {
     /** 只透传驾驶舱契约需要的字典,避免把后台任意字典暴露到公开端点。 */
-    private static final Set<String> ALLOWED = Set.of(
+    private static final Set<String> ALLOWED_DICT_TYPES = Set.of(
             "pump_station_type",
-            // 风险评估与隐患台账:预警详情与应急保障详情的唯一词表来源(ADR 0010),前端不内置第二份。
+            // 风险评估与隐患台账:预警详情与应急保障详情的词表只认这里下发的,前端不内置第二份。
             "risk_level", "risk_color",
             "hazard_type", "hazard_level", "hazard_source", "hazard_status");
 
@@ -37,7 +37,7 @@ public class CockpitDictController extends BaseCockpitController {
 
     @GetMapping("/dict/{dictType}")
     public ResponseEntity<AjaxResult> dictType(@PathVariable String dictType) {
-        if (!ALLOWED.contains(dictType)) {
+        if (!ALLOWED_DICT_TYPES.contains(dictType)) {
             return ResponseEntity.ok(AjaxResult.success(Collections.emptyList()));
         }
         List<SysDictData> rows = dictTypeService.selectDictDataByType(dictType);

+ 27 - 19
pipe-network-service/zksy-admin/src/test/java/com/zksy/web/controller/cockpit/CockpitDictControllerTest.java

@@ -4,6 +4,8 @@ import com.zksy.common.core.domain.entity.SysDictData;
 import com.zksy.system.service.ISysDictTypeService;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
 import org.springframework.test.web.servlet.MockMvc;
 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
 
@@ -41,24 +43,31 @@ class CockpitDictControllerTest {
         verify(dictTypeService).selectDictDataByType("pump_station_type");
     }
 
-    @Test void exposesRiskAndHazardDictionariesUsedByDetailPanels() throws Exception {
-        String[][] samples = {
-                {"risk_level", "0", "重大风险"},
-                {"risk_color", "0", "红"},
-                {"hazard_type", "0", "腐蚀"},
-                {"hazard_level", "1", "一级隐患"},
-                {"hazard_source", "0", "巡检"},
-                {"hazard_status", "0", "整改中"},
-        };
-        for (String[] sample : samples) {
-            stubPublished(sample[0], sample[1], sample[2]);
-            mvc.perform(get("/api/cockpit/v1/dict/" + sample[0]))
-                    .andExpect(status().isOk())
-                    .andExpect(jsonPath("$.code").value(200))
-                    .andExpect(jsonPath("$.data.length()").value(1))
-                    .andExpect(jsonPath("$.data[0].dictValue").value(sample[1]))
-                    .andExpect(jsonPath("$.data[0].dictLabel").value(sample[2]));
-        }
+    @ParameterizedTest(name = "{0} 原样透传 {1}={2}")
+    @CsvSource({
+            "risk_level, 0, 重大风险",
+            "risk_color, 0, 红",
+            "hazard_type, 0, 腐蚀",
+            "hazard_level, 1, 一级隐患",
+            "hazard_source, 0, 巡检",
+            "hazard_status, 0, 整改中",
+    })
+    void exposesRiskAndHazardDictionariesUsedByDetailPanels(String dictType, String dictValue, String dictLabel) throws Exception {
+        stubPublished(dictType, dictValue, dictLabel);
+        mvc.perform(get("/api/cockpit/v1/dict/" + dictType))
+                .andExpect(status().isOk())
+                .andExpect(jsonPath("$.code").value(200))
+                .andExpect(jsonPath("$.data.length()").value(1))
+                .andExpect(jsonPath("$.data[0].dictValue").value(dictValue))
+                .andExpect(jsonPath("$.data[0].dictLabel").value(dictLabel));
+    }
+
+    @Test void whitelistedDictionaryWithoutPublishedRowsIsEmpty() throws Exception {
+        when(dictTypeService.selectDictDataByType("hazard_type")).thenReturn(null);
+        mvc.perform(get("/api/cockpit/v1/dict/hazard_type"))
+                .andExpect(status().isOk())
+                .andExpect(jsonPath("$.data.length()").value(0));
+        verify(dictTypeService).selectDictDataByType("hazard_type");
     }
 
     @Test void dictionaryOutsideCockpitContractIsNotRead() throws Exception {
@@ -69,7 +78,6 @@ class CockpitDictControllerTest {
     }
 
     @Test void missingDictionaryIsEmptyNotError() throws Exception {
-        when(dictTypeService.selectDictDataByType("unknown_type")).thenReturn(null);
         mvc.perform(get("/api/cockpit/v1/dict/unknown_type"))
                 .andExpect(status().isOk())
                 .andExpect(jsonPath("$.code").value(200))

+ 114 - 0
pipe-network-service/zksy-admin/src/test/java/com/zksy/web/controller/cockpit/CockpitEventDetailServiceAlarmTest.java

@@ -0,0 +1,114 @@
+package com.zksy.web.controller.cockpit;
+
+import com.zksy.base.alarm.domain.AlarmLog;
+import com.zksy.base.alarm.dto.AlarmVO;
+import com.zksy.base.alarm.mapper.AlarmLogMapper;
+import com.zksy.base.alarm.mapper.AlarmOperationsMapper;
+import com.zksy.base.warning.mapper.AlarmWarningRelMapper;
+import com.zksy.base.warning.mapper.EarlyWarningMapper;
+import com.zksy.base.warning.mapper.WarningAttachmentMapper;
+import com.zksy.base.warning.mapper.WarningDisposalMapper;
+import com.zksy.base.warning.mapper.WarningSupervisionMapper;
+import com.zksy.cockpit.event.CockpitEventDetailService;
+import com.zksy.ops.dto.WorkOrderDetailVO;
+import com.zksy.ops.domain.vo.WorkOrderVO;
+import com.zksy.ops.service.WorkOrderService;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.time.LocalDateTime;
+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.assertTrue;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class CockpitEventDetailServiceAlarmTest {
+
+    @Mock private EarlyWarningMapper warnings;
+    @Mock private AlarmOperationsMapper alarms;
+    @Mock private WarningDisposalMapper disposals;
+    @Mock private WarningAttachmentMapper attachments;
+    @Mock private WarningSupervisionMapper supervisions;
+    @Mock private AlarmWarningRelMapper linkedAlarms;
+    @Mock private AlarmLogMapper alarmLogs;
+    @Mock private WorkOrderService workOrders;
+    @InjectMocks private CockpitEventDetailService service;
+
+    @Test
+    void alarmDetailCarriesHistoryAndAttachmentList() {
+        AlarmVO detail = new AlarmVO();
+        detail.setId("A1");
+        detail.setAlarmNo("BJ1");
+        detail.setEquipmentImage("/upload/device.png");
+        detail.setWorkOrderId(9L);
+        when(alarms.selectCockpitDetailByNo("BJ1")).thenReturn(detail);
+
+        AlarmLog log = new AlarmLog();
+        log.setOperType(1);
+        log.setOperDesc("确认报警");
+        log.setOperUserName("值班员");
+        log.setOperTime(LocalDateTime.of(2026, 12, 1, 9, 30));
+        log.setAttachUrl("/upload/alarm-a.png,/upload/alarm-b.png");
+        when(alarmLogs.selectHistory("A1")).thenReturn(Collections.singletonList(log));
+
+        WorkOrderVO order = new WorkOrderVO();
+        order.setMaintenanceBeforeImg("/upload/before.png");
+        order.setMaintenanceAfterImg("/upload/after.png");
+        WorkOrderDetailVO orderDetail = new WorkOrderDetailVO();
+        orderDetail.setOrder(order);
+        when(workOrders.getDetail(9L)).thenReturn(orderDetail);
+
+        AlarmVO result = service.alarmDetail("BJ1");
+
+        assertEquals(1, result.getDisposalHistory().size());
+        Map<String, Object> record = result.getDisposalHistory().get(0);
+        assertEquals("确认报警", record.get("operationDesc"));
+        assertEquals("值班员", record.get("operatorName"));
+        assertEquals("2026-12-01 09:30:00", record.get("operatedAt"));
+
+        List<String> urls = result.getAttachmentList().stream()
+                .map(item -> String.valueOf(item.get("url")))
+                .toList();
+        assertTrue(urls.contains("/upload/device.png"));
+        assertTrue(urls.contains("/upload/alarm-a.png"));
+        assertTrue(urls.contains("/upload/before.png"));
+        assertTrue(urls.contains("/upload/after.png"));
+        assertEquals(urls.size(), result.getAttachmentList().size(), "附件按 URL 去重");
+    }
+
+    @Test
+    void alarmDetailReturnsEmptyArraysWhenNoHistoryAndNoAttachments() {
+        AlarmVO detail = new AlarmVO();
+        detail.setId("A2");
+        detail.setAlarmNo("BJ2");
+        when(alarms.selectCockpitDetailByNo("BJ2")).thenReturn(detail);
+        when(alarmLogs.selectHistory("A2")).thenReturn(Collections.emptyList());
+
+        AlarmVO result = service.alarmDetail("BJ2");
+
+        assertEquals(Collections.emptyList(), result.getDisposalHistory());
+        assertEquals(Collections.emptyList(), result.getAttachmentList());
+    }
+
+    @Test
+    void alarmDetailKeepsBasicDetailWhenHistoryReadFails() {
+        AlarmVO detail = new AlarmVO();
+        detail.setId("A3");
+        detail.setAlarmNo("BJ3");
+        when(alarms.selectCockpitDetailByNo("BJ3")).thenReturn(detail);
+        when(alarmLogs.selectHistory("A3")).thenThrow(new RuntimeException("log table unavailable"));
+
+        AlarmVO result = service.alarmDetail("BJ3");
+
+        assertEquals("BJ3", result.getAlarmNo());
+        assertEquals(Collections.emptyList(), result.getDisposalHistory());
+        assertEquals(Collections.emptyList(), result.getAttachmentList());
+    }
+}

+ 7 - 0
pipe-network-service/zksy-system/src/main/java/com/zksy/base/alarm/dto/AlarmVO.java

@@ -5,6 +5,8 @@ import com.zksy.ops.dto.WorkOrderDetailVO;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 import java.time.LocalDateTime;
+import java.util.List;
+import java.util.Map;
 
 @Data
 @EqualsAndHashCode(callSuper = true)
@@ -31,4 +33,9 @@ public class AlarmVO extends AlarmReviewVO {
     private LocalDateTime latestOperationTime;
     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     private LocalDateTime planFinishTime;
+
+    /** 驾驶舱只读投影:报警处置/审计轨迹;无记录时为空数组。 */
+    private List<Map<String, Object>> disposalHistory;
+    /** 驾驶舱只读投影:设备、报警操作与关联工单维修附件;无附件时为空数组。 */
+    private List<Map<String, Object>> attachmentList;
 }

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

@@ -1,6 +1,9 @@
 package com.zksy.cockpit.event;
 
 import com.zksy.base.alarm.dto.AlarmVO;
+import com.zksy.base.alarm.domain.AlarmLog;
+import com.zksy.base.alarm.enums.AlarmLogOperTypeEnum;
+import com.zksy.base.alarm.mapper.AlarmLogMapper;
 import com.zksy.base.alarm.mapper.AlarmOperationsMapper;
 import com.zksy.base.warning.domain.WarningSupervision;
 import com.zksy.base.warning.mapper.AlarmWarningRelMapper;
@@ -8,14 +11,23 @@ import com.zksy.base.warning.mapper.EarlyWarningMapper;
 import com.zksy.base.warning.mapper.WarningAttachmentMapper;
 import com.zksy.base.warning.mapper.WarningDisposalMapper;
 import com.zksy.base.warning.mapper.WarningSupervisionMapper;
+import com.zksy.ops.dto.WorkOrderDetailVO;
+import com.zksy.ops.service.WorkOrderService;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.zksy.base.warning.domain.WarningAttachment;
 import lombok.RequiredArgsConstructor;
 import org.springframework.stereotype.Service;
 
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 /**
  * 驾驶舱只读事件详情(维护者 2026-10-29)。
@@ -39,6 +51,8 @@ public class CockpitEventDetailService {
     private final WarningAttachmentMapper attachments;
     private final WarningSupervisionMapper supervisions;
     private final AlarmWarningRelMapper linkedAlarms;
+    private final AlarmLogMapper alarmLogs;
+    private final WorkOrderService workOrders;
 
     /**
      * 预警详情:基本信息 + 处置记录 + 附件 + 督办 + 关联报警(各段独立成败)。
@@ -86,7 +100,97 @@ public class CockpitEventDetailService {
         }
         String key = eventId.trim();
         AlarmVO detail = alarms.selectCockpitDetailByNo(key);
-        return detail != null ? detail : alarms.selectCockpitDetail(key);
+        if (detail == null) detail = alarms.selectCockpitDetail(key);
+        if (detail == null) return null;
+
+        // 与 WARNING 详情同一套只读投影:处置轨迹与附件都是顶层数组,无数据返回空数组。
+        // 记录/附件读失败只降级这两段,不把基础报警详情打崩。
+        List<AlarmLog> history = alarmHistory(detail.getId());
+        detail.setDisposalHistory(alarmDisposalHistory(history));
+        detail.setAttachmentList(alarmAttachments(detail, history));
+        return detail;
+    }
+
+    /** 报警操作流水:免鉴权读取底层 alarm_log,不经过需要登录态的 admin 端点。 */
+    private List<AlarmLog> alarmHistory(String alarmId) {
+        if (alarmId == null || alarmId.trim().isEmpty()) return Collections.emptyList();
+        try {
+            List<AlarmLog> history = alarmLogs.selectHistory(alarmId.trim());
+            return history == null ? Collections.emptyList() : history;
+        } catch (RuntimeException error) {
+            return Collections.emptyList();
+        }
+    }
+
+    private List<Map<String, Object>> alarmDisposalHistory(List<AlarmLog> history) {
+        if (history == null || history.isEmpty()) return Collections.emptyList();
+        List<Map<String, Object>> result = new ArrayList<>();
+        for (AlarmLog log : history) {
+            if (log == null) continue;
+            AlarmLogOperTypeEnum type = AlarmLogOperTypeEnum.fromCode(log.getOperType());
+            Map<String, Object> item = new LinkedHashMap<>();
+            item.put("operationName", type == null ? "历史操作" : type.getLabel());
+            item.put("operatedAt", formatAlarmTime(log.getOperTime()));
+            item.put("operatorName", log.getOperUserName() == null ? "" : log.getOperUserName());
+            item.put("operationDesc", log.getOperDesc() == null ? "" : log.getOperDesc());
+            item.put("operType", log.getOperType());
+            item.put("attachments", splitUrls(log.getAttachUrl()));
+            result.add(item);
+        }
+        return result;
+    }
+
+    private List<Map<String, Object>> alarmAttachments(AlarmVO detail, List<AlarmLog> history) {
+        Map<String, Map<String, Object>> byUrl = new LinkedHashMap<>();
+        addAttachment(byUrl, detail.getEquipmentImage(), "设备图片");
+        if (history != null) {
+            for (AlarmLog log : history) {
+                if (log != null) addAttachment(byUrl, log.getAttachUrl(), "报警附件");
+            }
+        }
+        WorkOrderDetailVO order = detail.getWorkOrderDetail();
+        if (order == null && detail.getWorkOrderId() != null) {
+            try {
+                order = workOrders.getDetail(detail.getWorkOrderId());
+            } catch (RuntimeException ignored) {
+                // 工单不作为报警详情的前置条件;附件段失败只返回已有附件。
+            }
+        }
+        if (order != null && order.getOrder() != null) {
+            addAttachment(byUrl, order.getOrder().getMaintenanceBeforeImg(), "维修前");
+            addAttachment(byUrl, order.getOrder().getMaintenanceAfterImg(), "维修后");
+        }
+        return new ArrayList<>(byUrl.values());
+    }
+
+    private static void addAttachment(Map<String, Map<String, Object>> byUrl, String raw, String type) {
+        for (String url : splitUrls(raw)) {
+            if (byUrl.containsKey(url)) continue;
+            Map<String, Object> item = new LinkedHashMap<>();
+            item.put("name", fileName(url));
+            item.put("url", url);
+            item.put("type", type);
+            byUrl.put(url, item);
+        }
+    }
+
+    private static List<String> splitUrls(String raw) {
+        if (raw == null || raw.trim().isEmpty()) return Collections.emptyList();
+        Set<String> urls = new LinkedHashSet<>();
+        for (String part : raw.split(",")) {
+            String url = part == null ? "" : part.trim();
+            if (!url.isEmpty()) urls.add(url);
+        }
+        return new ArrayList<>(urls);
+    }
+
+    private static String fileName(String url) {
+        int slash = url.lastIndexOf('/');
+        return slash >= 0 ? url.substring(slash + 1) : url;
+    }
+
+    private static String formatAlarmTime(LocalDateTime time) {
+        return time == null ? "" : time.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
     }
 
     /** 下划线列名 → 驼峰键(SQL Map 与 Java Bean 输出口径对齐)。 */