Procházet zdrojové kódy

feat(cockpit): 点位详情支持跨端点同快照与坐标校验契约 (#69)

Kazerin před 2 měsíci
rodič
revize
3dd4b32f08

+ 9 - 3
docs/cockpit/README.md

@@ -1,6 +1,6 @@
 # 城市生命线驾驶舱后端契约:移交与联调手册
 
-本手册面向 UE / 前端对接团队,记录 `/api/cockpit/v1/` 的目标契约、字典、分页、错误与实时增量口径,并给出联调检查表。**当前代码与数据库已于 2026-09-24 发生结构变化,旧冻结字典不能直接当成当前运行时验收结果;第 8 节的“当前重建基线”优先于历史描述。**对应的契约测试仍为 `CockpitContractIntegrationTest` 与 `CockpitContractDictionaryTest`,但目前工作树编译未通过,不能宣称这些测试已在新模型下通过。
+本手册面向 UE / 前端对接团队,记录 `/api/cockpit/v1/` 的目标契约、字典、分页、错误与实时增量口径,并给出联调检查表。**当前代码与数据库已于 2026-09-24 发生结构变化,旧冻结字典不能直接当成当前运行时验收结果;第 8 节的“当前重建基线”优先于历史描述。**对应的契约测试仍为 `CockpitContractIntegrationTest` 与 `CockpitContractDictionaryTest`(#69 起新增 `CockpitMonitoringPointDetailSnapshotContractTest` 覆盖跨端点同快照契约);第 3 行早期“工作树编译未通过”仅描述 2026-09-24 前的状态,当前可编译状态以第 8 节为准。
 
 ## 1. 契约总则
 
@@ -50,7 +50,7 @@
 | 用途 | 端点 |
 | --- | --- |
 | 地图分页快照(**地图取数路径**) | `GET /api/cockpit/v1/map-snapshot?page&pageSize&snapshotAt&industry&region&deviceType` |
-| 监测点详情 | `GET /api/cockpit/v1/monitoring-points/{monitoringPointCode}` |
+| 监测点详情 | `GET /api/cockpit/v1/monitoring-points/{monitoringPointCode}?snapshotAt` |
 | 设备档案(含最新图片) | `GET /api/cockpit/v1/equipments/{deviceCode}/archive` |
 | 类型指标 | `GET /api/cockpit/v1/equipments/{deviceCode}/metrics/current`、`.../metrics/history` |
 | 事件 | `GET /api/cockpit/v1/equipments/{deviceCode}/events/current`、`.../events/history` |
@@ -97,7 +97,13 @@
 | `coordinateValidation.renderable` | 只有可信校核来源存在且坐标有效时为 `true`;前端仅在此为 `true` 时绘制设备点位。 |
 | `coordinateValidation.crs` | 当前统一为 `WGS84`。 |
 
-当前 `equipment_base` 没有可信坐标校核字段,因此真实适配器透传设备坐标但按 `UNVERIFIED_COORDINATE` 降级,`renderable=false`;设备仍保留在详情清单中。点位详情与 `map-snapshot` 都从同一设备关联关系和设备编码集合构造,均不因离线状态或坐标不可渲染而静默删除设备。
+当前 `equipment_base` 没有可信坐标校核字段,因此真实适配器透传设备坐标但按 `UNVERIFIED_COORDINATE` 降级,`renderable=false`;设备仍保留在详情清单中。
+
+**跨端点同快照(#69)**:
+
+- 监测点详情接受可选查询参数 `snapshotAt`(ISO-8601 带偏移,语义与 `map-snapshot` 一致)。提供 `snapshotAt` 时,详情从**同一份地图快照**(同一 `CockpitMapSnapshotAdapter`)按点位编码取设备集合,保证同一 `snapshotAt` 下详情 `devices[]` 与 `map-snapshot` 对应点位的 `deviceCodes` 集合一致(含离线设备、不重复、不静默丢失);快照时间戳与请求不一致时返回 `SNAPSHOT_EXPIRED`。
+- 未提供 `snapshotAt` 时走详情适配器路径(尽力模式):两个请求之间点位-设备关系变化时,详情与地图集合可能不一致,前端按决策票 05「跨端点尽力展示并提示可能不一致」处理。
+- **当前正式交付边界**:`equipment_base` 暂无可信坐标校核字段,现网关联设备均按 `UNVERIFIED_COORDINATE` 降级、`renderable=false`,前端准入规则下**只保留设备清单、不绘制设备位置**。可信坐标校核来源(如基于设备安装定位/人工复核的 `coordinateVerified` 字段)需另行排期,由后端在数据源就绪后接入并回填。
 
 ## 5. WebSocket 实时增量
 

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

@@ -5,6 +5,7 @@ import com.zksy.cockpit.foundation.CockpitInvalidCodeException;
 import com.zksy.cockpit.foundation.CockpitInvalidRequestException;
 import com.zksy.cockpit.foundation.CockpitMonitoringPointNotFoundException;
 import com.zksy.cockpit.foundation.CockpitReadException;
+import com.zksy.cockpit.foundation.CockpitSnapshotExpiredException;
 import com.zksy.cockpit.point.CockpitMonitoringPointDetailResponse;
 import com.zksy.cockpit.point.CockpitMonitoringPointDetailService;
 import com.zksy.common.core.domain.AjaxResult;
@@ -17,6 +18,7 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestHeader;
+import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.util.UriUtils;
 
@@ -38,16 +40,19 @@ public class CockpitMonitoringPointDetailController {
     @PreAuthorize("isAuthenticated()")
     public ResponseEntity<AjaxResult> monitoringPointDetail(
             @PathVariable("monitoringPointCode") String monitoringPointCode,
+            @RequestParam(value = "snapshotAt", required = false) String snapshotAt,
             @RequestHeader(value = REQUEST_ID_HEADER, required = false) String requestId) {
         CockpitMonitoringPointDetailResponse response = detailService.load(
-                resolveRequestId(requestId), decodePathSegment(monitoringPointCode));
+                resolveRequestId(requestId), decodePathSegment(monitoringPointCode), snapshotAt);
         return ResponseEntity.ok(AjaxResult.success(response));
     }
 
     @ExceptionHandler(CockpitInvalidRequestException.class)
-    public ResponseEntity<AjaxResult> handleInvalidRequest(HttpServletRequest request) {
+    public ResponseEntity<AjaxResult> handleInvalidRequest(
+            HttpServletRequest request,
+            CockpitInvalidRequestException exception) {
         return error(HttpStatus.BAD_REQUEST, detailService.invalidRequest(
-                resolveRequestId(request.getHeader(REQUEST_ID_HEADER))));
+                resolveRequestId(request.getHeader(REQUEST_ID_HEADER)), exception.getMessage()));
     }
 
     @ExceptionHandler(CockpitInvalidCodeException.class)
@@ -68,6 +73,12 @@ public class CockpitMonitoringPointDetailController {
                 resolveRequestId(request.getHeader(REQUEST_ID_HEADER))));
     }
 
+    @ExceptionHandler(CockpitSnapshotExpiredException.class)
+    public ResponseEntity<AjaxResult> handleSnapshotExpired(HttpServletRequest request) {
+        return error(HttpStatus.BAD_REQUEST, detailService.snapshotExpired(
+                resolveRequestId(request.getHeader(REQUEST_ID_HEADER))));
+    }
+
     private ResponseEntity<AjaxResult> error(HttpStatus status, CockpitErrorPayload payload) {
         return ResponseEntity.status(status)
                 .body(new AjaxResult(status.value(), payload.getMessage(), payload));

+ 60 - 1
pipe-network-service/zksy-admin/src/test/java/com/zksy/web/controller/cockpit/CockpitMonitoringPointDetailControllerTest.java

@@ -1,6 +1,8 @@
 package com.zksy.web.controller.cockpit;
 
 import com.zksy.cockpit.foundation.CockpitReadException;
+import com.zksy.cockpit.map.CockpitMapSnapshot;
+import com.zksy.cockpit.map.CockpitMapSnapshotAdapter;
 import com.zksy.cockpit.point.CockpitMonitoringPointDetail;
 import com.zksy.cockpit.point.CockpitMonitoringPointDetailAdapter;
 import com.zksy.cockpit.point.CockpitMonitoringPointDetailService;
@@ -33,13 +35,15 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
 class CockpitMonitoringPointDetailControllerTest {
 
     private CockpitMonitoringPointDetailAdapter adapter;
+    private CockpitMapSnapshotAdapter mapAdapter;
     private MockMvc mockMvc;
 
     @BeforeEach
     void setUp() {
         adapter = mock(CockpitMonitoringPointDetailAdapter.class);
+        mapAdapter = mock(CockpitMapSnapshotAdapter.class);
         CockpitMonitoringPointDetailService service = new CockpitMonitoringPointDetailService(
-                adapter,
+                adapter, mapAdapter,
                 Clock.fixed(Instant.parse("2026-09-02T02:00:00Z"), ZoneOffset.UTC));
         CockpitMonitoringPointDetailController controller = new CockpitMonitoringPointDetailController(service);
         mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
@@ -190,6 +194,61 @@ class CockpitMonitoringPointDetailControllerTest {
         verifyNoInteractions(adapter);
     }
 
+    @Test
+    void detailWithSnapshotAtReadsFromMapSnapshotSource() throws Exception {
+        CockpitMapSnapshot snapshot = new CockpitMapSnapshot(
+                "2026-09-02T10:00:00+08:00", Collections.emptyList(),
+                Collections.singletonList(new CockpitMapSnapshot.SourceMonitoringPoint(
+                        "MP-SNAP", "供水干管监测点", null, "11",
+                        new BigDecimal("110.141"), new BigDecimal("28.452"), false,
+                        Collections.singletonList(
+                                device("EQ-001", true, false, false, true, Collections.emptyList())))),
+                Collections.emptyList());
+        when(mapAdapter.load("2026-09-02T10:00:00+08:00")).thenReturn(snapshot);
+
+        mockMvc.perform(get("/api/cockpit/v1/monitoring-points/MP-SNAP")
+                        .param("snapshotAt", "2026-09-02T10:00:00+08:00"))
+                .andExpect(status().isOk())
+                .andExpect(jsonPath("$.data.monitoringPointCode").value("MP-SNAP"))
+                .andExpect(jsonPath("$.data.devices[0].deviceCode").value("EQ-001"));
+
+        verifyNoInteractions(adapter);
+    }
+
+    @Test
+    void detailWithMismatchedSnapshotAtReturnsSnapshotExpired() throws Exception {
+        CockpitMapSnapshot snapshot = new CockpitMapSnapshot(
+                "2026-09-02T11:00:00+08:00", Collections.emptyList(),
+                Collections.singletonList(new CockpitMapSnapshot.SourceMonitoringPoint(
+                        "MP-SNAP", "供水干管监测点", null, "11",
+                        new BigDecimal("110.141"), new BigDecimal("28.452"), false,
+                        Collections.emptyList())),
+                Collections.emptyList());
+        when(mapAdapter.load("2026-09-02T10:00:00+08:00")).thenReturn(snapshot);
+
+        mockMvc.perform(get("/api/cockpit/v1/monitoring-points/MP-SNAP")
+                        .param("snapshotAt", "2026-09-02T10:00:00+08:00"))
+                .andExpect(status().isBadRequest())
+                .andExpect(jsonPath("$.code").value(400))
+                .andExpect(jsonPath("$.data.category").value("SNAPSHOT_EXPIRED"))
+                .andExpect(jsonPath("$.data.message").value("监测点详情快照已过期,请刷新后重试"));
+
+        verifyNoInteractions(adapter);
+    }
+
+        @Test
+    void detailWithMalformedSnapshotAtReturnsInvalidRequest() throws Exception {
+        mockMvc.perform(get("/api/cockpit/v1/monitoring-points/MP-SNAP")
+                        .param("snapshotAt", "not-a-timestamp"))
+                .andExpect(status().isBadRequest())
+                .andExpect(jsonPath("$.code").value(400))
+                .andExpect(jsonPath("$.data.category").value("INVALID_REQUEST"))
+                .andExpect(jsonPath("$.data.message").value("监测点快照参数不合法"));
+
+        verifyNoInteractions(adapter);
+        verifyNoInteractions(mapAdapter);
+    }
+
     @Test
     void controllerOnlyExposesAuthenticatedReadOnlyOperations() {
         assertTrue(Arrays.stream(CockpitMonitoringPointDetailController.class.getDeclaredMethods())

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

@@ -0,0 +1,288 @@
+package com.zksy.web.controller.cockpit;
+
+import com.zksy.cockpit.foundation.CockpitInvalidRequestException;
+import com.zksy.cockpit.foundation.CockpitSnapshotExpiredException;
+import com.zksy.cockpit.map.CockpitMapSnapshot;
+import com.zksy.cockpit.map.CockpitMapSnapshotAdapter;
+import com.zksy.cockpit.map.CockpitMapSnapshotService;
+import com.zksy.cockpit.map.CockpitMonitoringPointSummary;
+import com.zksy.cockpit.point.CockpitMonitoringPointDetailAdapter;
+import com.zksy.cockpit.point.CockpitMonitoringPointDetailResponse;
+import com.zksy.cockpit.point.CockpitMonitoringPointDetailService;
+import com.zksy.cockpit.point.CockpitMonitoringPointDeviceCard;
+import com.zksy.cockpit.point.CockpitMonitoringPointDeviceSnapshot;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.math.BigDecimal;
+import java.time.Clock;
+import java.time.Instant;
+import java.time.ZoneOffset;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verifyNoInteractions;
+import static org.mockito.Mockito.when;
+
+/**
+ * 跨端点同快照契约:同一 snapshotAt 下,监测点详情 devices[] 与 map-snapshot
+ * 对应点位的 deviceCodes 集合必须一致(含离线设备、不重复、不静默丢失),
+ * 且详情设备坐标/校验对象与快照同源。
+ */
+class CockpitMonitoringPointDetailSnapshotContractTest {
+
+    private static final String SNAPSHOT_AT = "2026-09-02T10:00:00+08:00";
+    private static final Clock CLOCK =
+            Clock.fixed(Instant.parse("2026-09-02T02:00:00Z"), ZoneOffset.UTC);
+
+    private CockpitMonitoringPointDetailAdapter detailAdapter;
+    private CockpitMapSnapshotAdapter mapAdapter;
+    private CockpitMonitoringPointDetailService detailService;
+    private CockpitMapSnapshotService mapService;
+
+    @BeforeEach
+    void setUp() {
+        detailAdapter = mock(CockpitMonitoringPointDetailAdapter.class);
+        mapAdapter = mock(CockpitMapSnapshotAdapter.class);
+        detailService = new CockpitMonitoringPointDetailService(detailAdapter, mapAdapter, CLOCK);
+        mapService = new CockpitMapSnapshotService(mapAdapter, CLOCK);
+    }
+
+    @Test
+    void detailDevicesMatchMapSnapshotDeviceCodesAtSameSnapshotIncludingOffline() {
+        CockpitMapSnapshot snapshot = snapshotWith(SNAPSHOT_AT,
+                new CockpitMapSnapshot.SourceMonitoringPoint(
+                        "MP-001", "供水干管监测点", null, "11",
+                        new BigDecimal("110.141"), new BigDecimal("28.452"), false,
+                        Arrays.asList(
+                                device("EQ-003", "110.142", "28.453", false, true, false, false, true),
+                                device("EQ-001", "110.143", "28.454", false, true, true, false, true),
+                                device("EQ-005", "110.144", "28.455", false, false, false, true, true),
+                                device("EQ-002", "110.145", "28.456", false, true, false, false, false),
+                                device("EQ-004", null, null, false, false, false, false, false))));
+        when(mapAdapter.load(SNAPSHOT_AT)).thenReturn(snapshot);
+
+        CockpitMonitoringPointDetailResponse response =
+                detailService.load("req", "MP-001", SNAPSHOT_AT);
+        assertNotNull(response);
+
+        List<String> detailDeviceCodes = response.getDevices().stream()
+                .map(CockpitMonitoringPointDeviceCard::getDeviceCode)
+                .sorted(Comparator.nullsLast(String::compareTo))
+                .collect(Collectors.toList());
+
+        List<CockpitMonitoringPointSummary> points = mapService.load(
+                "req", 1, 500, SNAPSHOT_AT, null, null, null).getMonitoringPoints();
+        assertEquals(1, points.size());
+        List<String> mapDeviceCodes = points.get(0).getDeviceCodes().stream()
+                .sorted(Comparator.nullsLast(String::compareTo))
+                .collect(Collectors.toList());
+
+        assertEquals(5, detailDeviceCodes.size(), "详情必须包含全部关联设备");
+        assertEquals(mapDeviceCodes, detailDeviceCodes,
+                "同一 snapshotAt 下详情设备集合必须与 map-snapshot deviceCodes 一致(含离线、不重复)");
+        assertEquals(detailDeviceCodes.stream().distinct().count(), detailDeviceCodes.size(),
+                "详情设备集合不得重复");
+        assertTrue(mapDeviceCodes.contains("EQ-002"), "离线设备必须在集合中");
+        assertTrue(detailDeviceCodes.contains("EQ-004"), "缺坐标设备必须在集合中");
+    }
+
+    @Test
+    void detailCoordinatesAndValidationArePreservedFromSnapshotSource() {
+        CockpitMapSnapshot snapshot = snapshotWith(SNAPSHOT_AT,
+                new CockpitMapSnapshot.SourceMonitoringPoint(
+                        "MP-001", "供水干管监测点", null, "11",
+                        new BigDecimal("110.141"), new BigDecimal("28.452"), false,
+                        Arrays.asList(
+                                device("EQ-VALID", "110.142", "28.453", false, true, true, false, false),
+                                device("EQ-MISSING", null, null, false, false, false, false, false),
+                                device("EQ-INVALID", "200.000", "28.453", true, true, false, false, false),
+                                device("EQ-UNVERIFIED", "110.146", "28.457", false, true, false, false, false))));
+        when(mapAdapter.load(SNAPSHOT_AT)).thenReturn(snapshot);
+
+        CockpitMonitoringPointDetailResponse response =
+                detailService.load("req", "MP-001", SNAPSHOT_AT);
+
+        List<CockpitMonitoringPointDeviceCard> devices = response.getDevices();
+        assertEquals(4, devices.size());
+
+        CockpitMonitoringPointDeviceCard valid = byCode(devices, "EQ-VALID");
+        assertEquals(new BigDecimal("110.142"), valid.getLongitude());
+        assertEquals(new BigDecimal("28.453"), valid.getLatitude());
+        assertEquals("UNVERIFIED", valid.getCoordinateValidation().getState());
+        assertFalse(valid.getCoordinateValidation().isRenderable());
+
+        CockpitMonitoringPointDeviceCard missing = byCode(devices, "EQ-MISSING");
+        assertNull(missing.getLongitude());
+        assertNull(missing.getLatitude());
+        assertFalse(missing.getCoordinateValidation().isRenderable());
+        assertEquals("UNVERIFIED_COORDINATE", missing.getCoordinateValidation().getReason());
+
+        CockpitMonitoringPointDeviceCard invalid = byCode(devices, "EQ-INVALID");
+        assertEquals("INVALID", invalid.getCoordinateValidation().getState());
+        assertEquals("LONGITUDE_OUT_OF_RANGE", invalid.getCoordinateValidation().getReason());
+        assertFalse(invalid.getCoordinateValidation().isRenderable());
+
+        CockpitMonitoringPointDeviceCard unverified = byCode(devices, "EQ-UNVERIFIED");
+        assertEquals("UNVERIFIED", unverified.getCoordinateValidation().getState());
+        assertEquals("UNVERIFIED_COORDINATE", unverified.getCoordinateValidation().getReason());
+        assertFalse(unverified.getCoordinateValidation().isRenderable());
+    }
+
+    @Test
+    void snapshotTimestampMismatchFailsLikeMapSnapshot() {
+        CockpitMapSnapshot snapshot = snapshotWith("2026-09-02T11:00:00+08:00",
+                new CockpitMapSnapshot.SourceMonitoringPoint(
+                        "MP-001", "供水干管监测点", null, "11",
+                        new BigDecimal("110.141"), new BigDecimal("28.452"), false,
+                        Collections.emptyList()));
+        when(mapAdapter.load(SNAPSHOT_AT)).thenReturn(snapshot);
+
+        assertThrows(CockpitSnapshotExpiredException.class,
+                () -> detailService.load("req", "MP-001", SNAPSHOT_AT));
+    }
+
+    @Test
+    void snapshotMissingPointReturnsNullLikeAdapterPath() {
+        CockpitMapSnapshot snapshot = snapshotWith(SNAPSHOT_AT,
+                new CockpitMapSnapshot.SourceMonitoringPoint(
+                        "MP-OTHER", "另一个点位", null, "11",
+                        new BigDecimal("110.1"), new BigDecimal("28.4"), false,
+                        Collections.emptyList()));
+        when(mapAdapter.load(SNAPSHOT_AT)).thenReturn(snapshot);
+
+        assertThrows(com.zksy.cockpit.foundation.CockpitMonitoringPointNotFoundException.class,
+                () -> detailService.load("req", "MP-404", SNAPSHOT_AT));
+        verifyNoInteractions(detailAdapter);
+    }
+
+    @Test
+    void withoutSnapshotAtFallsBackToDetailAdapterPath() {
+        when(detailAdapter.load("MP-001"))
+                .thenReturn(new com.zksy.cockpit.point.CockpitMonitoringPointDetail(
+                        "MP-001", "供水干管监测点",
+                        new BigDecimal("110.141"), new BigDecimal("28.452"), false,
+                        Collections.singletonList(device("EQ-001", "110.142", "28.453",
+                                false, true, false, false, false))));
+
+        CockpitMonitoringPointDetailResponse response =
+                detailService.load("req", "MP-001", null);
+        assertNotNull(response);
+        assertEquals(1, response.getDevices().size());
+        assertEquals("EQ-001", response.getDevices().get(0).getDeviceCode());
+    }
+
+    @Test
+    void twoParameterLoadDelegatesToNullSnapshotAt() {
+        when(detailAdapter.load("MP-001"))
+                .thenReturn(new com.zksy.cockpit.point.CockpitMonitoringPointDetail(
+                        "MP-001", "供水干管监测点",
+                        new BigDecimal("110.141"), new BigDecimal("28.452"), false,
+                        Collections.singletonList(device("EQ-001", "110.142", "28.453",
+                                false, true, false, false, false))));
+
+        CockpitMonitoringPointDetailResponse response =
+                detailService.load("req", "MP-001");
+        assertNotNull(response);
+        assertEquals(1, response.getDevices().size());
+    }
+
+    @Test
+    void malformedSnapshotAtFailsLikeMapSnapshotBeforeReading() {
+        assertThrows(CockpitInvalidRequestException.class,
+                () -> detailService.load("req", "MP-001", "not-a-timestamp"));
+        verifyNoInteractions(mapAdapter);
+        verifyNoInteractions(detailAdapter);
+    }
+
+    @Test
+    void duplicateAndBlankDeviceCodesAreNormalizedLikeMapSnapshot() {
+        CockpitMapSnapshot snapshot = snapshotWith(SNAPSHOT_AT,
+                new CockpitMapSnapshot.SourceMonitoringPoint(
+                        "MP-001", "供水干管监测点", null, "11",
+                        new BigDecimal("110.141"), new BigDecimal("28.452"), false,
+                        Arrays.asList(
+                                device("EQ-001", "110.142", "28.453", false, true, false, false, false),
+                                device(" EQ-001 ", "110.143", "28.454", false, true, false, false, false),
+                                device(null, "110.144", "28.455", false, false, false, false, false),
+                                device("", "110.145", "28.456", false, false, false, false, false),
+                                device("EQ-002", null, null, false, false, false, false, false))));
+        when(mapAdapter.load(SNAPSHOT_AT)).thenReturn(snapshot);
+
+        CockpitMonitoringPointDetailResponse response =
+                detailService.load("req", "MP-001", SNAPSHOT_AT);
+
+        List<String> detailDeviceCodes = response.getDevices().stream()
+                .map(CockpitMonitoringPointDeviceCard::getDeviceCode)
+                .sorted(Comparator.nullsLast(String::compareTo))
+                .collect(Collectors.toList());
+
+        List<CockpitMonitoringPointSummary> points = mapService.load(
+                "req", 1, 500, SNAPSHOT_AT, null, null, null).getMonitoringPoints();
+        List<String> mapDeviceCodes = points.get(0).getDeviceCodes().stream()
+                .sorted(Comparator.nullsLast(String::compareTo))
+                .collect(Collectors.toList());
+
+        assertEquals(Arrays.asList("EQ-001", "EQ-002"), detailDeviceCodes,
+                "详情必须按 map 口径去重 trim 滤空码:EQ-001 只出现一次,空码/空白码剔除");
+        assertEquals(mapDeviceCodes, detailDeviceCodes,
+                "重复/空码数据下详情与 map-snapshot 集合仍必须一致");
+    }
+
+    @Test
+    void verifiedValidCoordinatesRender() {
+        CockpitMapSnapshot snapshot = snapshotWith(SNAPSHOT_AT,
+                new CockpitMapSnapshot.SourceMonitoringPoint(
+                        "MP-001", "供水干管监测点", null, "11",
+                        new BigDecimal("110.141"), new BigDecimal("28.452"), false,
+                        Collections.singletonList(
+                                device("EQ-VERIFIED", "110.142", "28.453",
+                                        true, true, false, false, false))));
+        when(mapAdapter.load(SNAPSHOT_AT)).thenReturn(snapshot);
+
+        CockpitMonitoringPointDetailResponse response =
+                detailService.load("req", "MP-001", SNAPSHOT_AT);
+
+        CockpitMonitoringPointDeviceCard verified = byCode(response.getDevices(), "EQ-VERIFIED");
+        assertEquals(new BigDecimal("110.142"), verified.getLongitude());
+        assertEquals(new BigDecimal("28.453"), verified.getLatitude());
+        assertEquals("VALID", verified.getCoordinateValidation().getState());
+        assertTrue(verified.getCoordinateValidation().isRenderable());
+    }
+
+    private static CockpitMapSnapshot snapshotWith(String snapshotAt,
+            CockpitMapSnapshot.SourceMonitoringPoint point) {
+        return new CockpitMapSnapshot(snapshotAt, Collections.emptyList(),
+                Collections.singletonList(point), Collections.emptyList());
+    }
+
+    private static CockpitMonitoringPointDeviceCard byCode(
+            List<CockpitMonitoringPointDeviceCard> devices, String code) {
+        return devices.stream()
+                .filter(device -> code.equals(device.getDeviceCode()))
+                .findFirst()
+                .orElseThrow(() -> new AssertionError("missing device " + code));
+    }
+
+    private static CockpitMonitoringPointDeviceSnapshot device(String code, String longitude, String latitude,
+            boolean coordinateVerified, boolean online, boolean alarm, boolean warning,
+            boolean workOrderInProgress) {
+        return new CockpitMonitoringPointDeviceSnapshot(
+                code, code + "设备", "压力计",
+                longitude == null ? null : new BigDecimal(longitude),
+                latitude == null ? null : new BigDecimal(latitude),
+                coordinateVerified,
+                online, alarm, warning, workOrderInProgress,
+                Collections.emptyList(), null);
+    }
+}

+ 129 - 5
pipe-network-service/zksy-system/src/main/java/com/zksy/cockpit/point/CockpitMonitoringPointDetailService.java

@@ -6,38 +6,76 @@ import com.zksy.cockpit.foundation.CockpitErrorCategory;
 import com.zksy.cockpit.foundation.CockpitErrorPayload;
 import com.zksy.cockpit.foundation.CockpitInvalidRequestException;
 import com.zksy.cockpit.foundation.CockpitMonitoringPointNotFoundException;
+import com.zksy.cockpit.foundation.CockpitSnapshotExpiredException;
+import com.zksy.cockpit.map.CockpitMapSnapshot;
+import com.zksy.cockpit.map.CockpitMapSnapshotAdapter;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import java.time.Clock;
+import java.time.OffsetDateTime;
 import java.time.ZoneId;
 import java.time.format.DateTimeFormatter;
+import java.time.format.DateTimeParseException;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.LinkedHashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.Objects;
 import java.util.stream.Collectors;
 
 @Service
 public class CockpitMonitoringPointDetailService {
     private final CockpitMonitoringPointDetailAdapter adapter;
+    private final CockpitMapSnapshotAdapter mapSnapshotAdapter;
     private final Clock clock;
 
     public CockpitMonitoringPointDetailService(CockpitMonitoringPointDetailAdapter adapter, Clock clock) {
+        this(adapter, null, clock);
+    }
+
+    @Autowired
+    public CockpitMonitoringPointDetailService(CockpitMonitoringPointDetailAdapter adapter,
+            CockpitMapSnapshotAdapter mapSnapshotAdapter, Clock clock) {
         this.adapter = adapter;
+        this.mapSnapshotAdapter = mapSnapshotAdapter;
         this.clock = clock;
     }
 
     public CockpitMonitoringPointDetailResponse load(String requestId, String monitoringPointCode) {
+        return load(requestId, monitoringPointCode, null);
+    }
+
+    /**
+     * 加载监测点详情。
+     *
+     * <p>传入 {@code snapshotAt}(且装配了 {@link CockpitMapSnapshotAdapter})时走跨端点同快照路径:
+     * 从与 {@code map-snapshot} 同一适配器的同一份快照中按点位编码取设备集合,保证同一
+     * {@code snapshotAt} 下详情 {@code devices[]} 与地图对应点位 {@code deviceCodes} 同源一致
+     * (含离线设备,不重复、不静默丢失)。未传 {@code snapshotAt} 时回退到原详情适配器路径。
+     */
+    public CockpitMonitoringPointDetailResponse load(String requestId, String monitoringPointCode,
+            String snapshotAt) {
         if (isBlank(monitoringPointCode)) {
-            throw new CockpitInvalidRequestException("monitoring point code is required");
+            throw new CockpitInvalidRequestException("监测点编码不能为空");
+        }
+        String normalizedCode = monitoringPointCode.trim();
+        if (!isBlank(snapshotAt)) {
+            validateSnapshotAt(snapshotAt);
+        }
+        CockpitMonitoringPointDetail detail;
+        if (!isBlank(snapshotAt) && mapSnapshotAdapter != null) {
+            detail = loadFromSnapshot(normalizedCode, snapshotAt.trim());
+        } else {
+            detail = adapter.load(normalizedCode);
         }
-        CockpitMonitoringPointDetail detail = adapter.load(monitoringPointCode.trim());
         if (detail == null) {
             throw new CockpitMonitoringPointNotFoundException("monitoring point not found");
         }
 
-        List<CockpitMonitoringPointDeviceSnapshot> devices = detail.getDevices() == null
-                ? Collections.emptyList()
-                : detail.getDevices();
+        List<CockpitMonitoringPointDeviceSnapshot> devices = normalizeDevices(detail.getDevices());
         CockpitMonitoringPointStatus pointStatus =
                 CockpitMonitoringPointStatusResolver.resolve(devices);
         CockpitCoordinateValidation coordinateValidation = CockpitCoordinateValidator.validate(
@@ -57,6 +95,37 @@ public class CockpitMonitoringPointDetailService {
                 statusDictionary());
     }
 
+    private CockpitMonitoringPointDetail loadFromSnapshot(String monitoringPointCode, String snapshotAt) {
+        CockpitMapSnapshot snapshot = mapSnapshotAdapter.load(snapshotAt);
+        if (snapshot == null) {
+            snapshot = CockpitMapSnapshot.empty(snapshotAt);
+        }
+        if (snapshot.getSnapshotAt() == null) {
+            throw new CockpitSnapshotExpiredException("snapshot timestamp missing");
+        }
+        if (!snapshotAt.equals(snapshot.getSnapshotAt())) {
+            throw new CockpitSnapshotExpiredException("snapshot timestamp changed");
+        }
+        List<CockpitMapSnapshot.SourceMonitoringPoint> points = snapshot.getMonitoringPoints() == null
+                ? Collections.emptyList()
+                : snapshot.getMonitoringPoints();
+        for (CockpitMapSnapshot.SourceMonitoringPoint point : points) {
+            if (point == null) {
+                continue;
+            }
+            if (monitoringPointCode.equals(trimToNull(point.getPointCode()))) {
+                return new CockpitMonitoringPointDetail(
+                        point.getPointCode(),
+                        point.getPointName(),
+                        point.getLongitude(),
+                        point.getLatitude(),
+                        point.isCoordinateVerified(),
+                        point.getDevices() == null ? Collections.emptyList() : point.getDevices());
+            }
+        }
+        return null;
+    }
+
     public CockpitErrorPayload invalidRequest(String requestId) {
         return new CockpitErrorPayload(
                 CockpitErrorCategory.INVALID_REQUEST,
@@ -65,6 +134,14 @@ public class CockpitMonitoringPointDetailService {
                 businessTime());
     }
 
+    public CockpitErrorPayload invalidRequest(String requestId, String message) {
+        return new CockpitErrorPayload(
+                CockpitErrorCategory.INVALID_REQUEST,
+                isBlank(message) ? "监测点编码不能为空" : message,
+                requestId,
+                businessTime());
+    }
+
     public CockpitErrorPayload invalidCode(String requestId) {
         return new CockpitErrorPayload(
                 CockpitErrorCategory.INVALID_REQUEST,
@@ -89,16 +166,63 @@ public class CockpitMonitoringPointDetailService {
                 businessTime());
     }
 
+    public CockpitErrorPayload snapshotExpired(String requestId) {
+        return new CockpitErrorPayload(
+                CockpitErrorCategory.SNAPSHOT_EXPIRED,
+                "监测点详情快照已过期,请刷新后重试",
+                requestId,
+                businessTime());
+    }
+
     private static List<CockpitMonitoringPointStatusEntry> statusDictionary() {
         return Arrays.stream(CockpitMonitoringPointStatus.values())
                 .map(CockpitMonitoringPointStatusEntry::new)
                 .collect(Collectors.toList());
     }
 
+    /**
+     * 与 map-snapshot 的 deviceCodes 构造保持同一规范化口径:滤 null/空码、trim、
+     * 按首次出现去重。保证同一 snapshotAt 下详情 devices[].deviceCode 与地图
+     * monitoringPoints[].deviceCodes 集合一致(含离线设备、不重复、不静默丢失)。
+     */
+    private static List<CockpitMonitoringPointDeviceSnapshot> normalizeDevices(
+            List<CockpitMonitoringPointDeviceSnapshot> devices) {
+        if (devices == null) {
+            return Collections.emptyList();
+        }
+        Map<String, CockpitMonitoringPointDeviceSnapshot> byCode = new LinkedHashMap<>();
+        for (CockpitMonitoringPointDeviceSnapshot device : devices) {
+            if (device == null || isBlank(device.getDeviceCode())) {
+                continue;
+            }
+            byCode.putIfAbsent(device.getDeviceCode().trim(), device);
+        }
+        return new ArrayList<>(byCode.values());
+    }
+
+    /**
+     * 与 map-snapshot 对齐:snapshotAt 必须是可解析的带偏移 ISO-8601 时间戳,
+     * 否则按无效请求(INVALID_REQUEST 400)拒绝,避免畸形时间戳落入 404/200。
+     */
+    private static void validateSnapshotAt(String snapshotAt) {
+        if (snapshotAt == null) {
+            return;
+        }
+        try {
+            OffsetDateTime.parse(snapshotAt);
+        } catch (DateTimeParseException exception) {
+            throw new CockpitInvalidRequestException("监测点快照参数不合法");
+        }
+    }
+
     private static boolean isBlank(String value) {
         return value == null || value.trim().isEmpty();
     }
 
+    private static String trimToNull(String value) {
+        return isBlank(value) ? null : value.trim();
+    }
+
     private String businessTime() {
         return DateTimeFormatter.ISO_OFFSET_DATE_TIME
                 .format(clock.instant().atZone(ZoneId.of("Asia/Shanghai")));