Просмотр исходного кода

feat(cockpit): 接入运行信息真实数据

Kazerin 2 месяцев назад
Родитель
Сommit
140e4d47de

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

@@ -0,0 +1,141 @@
+package com.zksy.web.controller.cockpit;
+
+import com.zksy.WaterSupply.WaterPlantInfo.domain.WaterPlantInfo;
+import com.zksy.WaterSupply.WaterPlantInfo.service.IWaterPlantInfoService;
+import com.zksy.WaterSupply.WaterPumpStation.domain.PumpStationType;
+import com.zksy.WaterSupply.WaterPumpStation.domain.WaterPumpStation;
+import com.zksy.WaterSupply.WaterPumpStation.service.IWaterPumpStationService;
+import com.zksy.base.domain.PipeNetworkBase;
+import com.zksy.base.service.PipeNetworkBaseService;
+import com.zksy.cockpit.infrastructure.BusinessCockpitInfrastructureOperatingInformationAdapter;
+import com.zksy.cockpit.infrastructure.CockpitInfrastructureOperatingInformationSnapshot;
+import com.zksy.drainage.domain.EngineeringFacility;
+import com.zksy.drainage.service.EngineeringFacilityService;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.math.BigDecimal;
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.Collections;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class BusinessCockpitInfrastructureOperatingInformationAdapterTest {
+
+    private IWaterPlantInfoService plantService;
+    private IWaterPumpStationService pumpStationService;
+    private PipeNetworkBaseService pipeNetworkBaseService;
+    private EngineeringFacilityService engineeringFacilityService;
+    private BusinessCockpitInfrastructureOperatingInformationAdapter adapter;
+
+    @BeforeEach
+    void setUp() {
+        plantService = mock(IWaterPlantInfoService.class);
+        pumpStationService = mock(IWaterPumpStationService.class);
+        pipeNetworkBaseService = mock(PipeNetworkBaseService.class);
+        engineeringFacilityService = mock(EngineeringFacilityService.class);
+        adapter = new BusinessCockpitInfrastructureOperatingInformationAdapter(
+                plantService, pumpStationService, pipeNetworkBaseService, engineeringFacilityService);
+    }
+
+    @Test
+    void mapsCapacityAndSupplyFactsWithSourceTimestampAndNoInventedTrend() {
+        WaterPlantInfo plant = new WaterPlantInfo();
+        plant.setPlantCode("PLANT-001");
+        plant.setPlantName("第一水厂");
+        plant.setCurrentCapacity(new BigDecimal("8.50"));
+        plant.setDesignCapacity(new BigDecimal("10.00"));
+        plant.setStatus("0");
+        plant.setUpdateTime(LocalDateTime.parse("2026-09-02T09:59:50"));
+
+        WaterPumpStation pump = new WaterPumpStation();
+        pump.setStationCode("PUMP-001");
+        pump.setStationName("城北泵站");
+        pump.setStationType(PumpStationType.WATER_SUPPLY);
+        pump.setDesignFlow(new BigDecimal("500.00"));
+        pump.setStatus("1");
+        pump.setUpdateTime(LocalDateTime.parse("2026-09-02T09:58:00"));
+
+        PipeNetworkBase pipe = new PipeNetworkBase();
+        pipe.setNetworkCode("PIPE-001");
+        pipe.setNetworkName("供水主线");
+        pipe.setNetworkType("2");
+        pipe.setLength(new BigDecimal("2500"));
+        pipe.setUpdateTime(LocalDateTime.parse("2026-09-02T09:57:00"));
+
+        EngineeringFacility facility = new EngineeringFacility();
+        facility.setId("DRAIN-001");
+        facility.setName("一号调蓄池");
+        facility.setFacilityType("调蓄设施");
+        facility.setPointCount(3);
+        facility.setUpdateTime(LocalDateTime.parse("2026-09-02T09:56:00"));
+
+        when(plantService.list()).thenReturn(Collections.singletonList(plant));
+        when(pumpStationService.list()).thenReturn(Collections.singletonList(pump));
+        when(pipeNetworkBaseService.list()).thenReturn(Collections.singletonList(pipe));
+        when(engineeringFacilityService.list()).thenReturn(Collections.singletonList(facility));
+
+        CockpitInfrastructureOperatingInformationSnapshot snapshot = adapter.load();
+
+        assertEquals(4, snapshot.getMetrics().size());
+        CockpitInfrastructureOperatingInformationSnapshot.SourceMetric plantMetric =
+                snapshot.getMetrics().get(0);
+        assertEquals("water_plant_current_supply", plantMetric.getMetricCode());
+        assertEquals("WATER_SUPPLY", plantMetric.getIndustry());
+        assertEquals(new BigDecimal("8.50"), plantMetric.getValue());
+        assertEquals(new BigDecimal("10.00"), plantMetric.getCapacityValue());
+        assertEquals("万m³/d", plantMetric.getUnit());
+        assertFalse(plantMetric.isAbnormal());
+        assertEquals("2026-09-02T09:59:50+08:00", plantMetric.getSampledAt());
+        assertTrue(plantMetric.getTrend().isEmpty(), "没有历史趋势来源时必须返回空趋势");
+
+        CockpitInfrastructureOperatingInformationSnapshot.SourceMetric pipeMetric =
+                snapshot.getMetrics().stream()
+                        .filter(item -> "water_supply_pipe_length".equals(item.getMetricCode()))
+                        .findFirst()
+                        .orElseThrow(AssertionError::new);
+        assertEquals(new BigDecimal("2500"), pipeMetric.getValue());
+        assertEquals("m", pipeMetric.getUnit());
+        assertNull(pipeMetric.getCapacityValue());
+
+        CockpitInfrastructureOperatingInformationSnapshot.SourceMetric drainageMetric =
+                snapshot.getMetrics().stream()
+                        .filter(item -> "drainage_storage_point_count".equals(item.getMetricCode()))
+                        .findFirst()
+                        .orElseThrow(AssertionError::new);
+        assertEquals("DRAINAGE", drainageMetric.getIndustry());
+        assertEquals(new BigDecimal("3"), drainageMetric.getValue());
+    }
+
+    @Test
+    void mapsDrainageAndGasPipesAndSkipsUnclassifiableRows() {
+        PipeNetworkBase drainage = pipe("DRAIN-PIPE", "排水管线", "drainage", "1000");
+        PipeNetworkBase gas = pipe("GAS-PIPE", "燃气管线", "GAS", "500");
+        PipeNetworkBase unknown = pipe("UNKNOWN-PIPE", "未知管线", "99", "500");
+        when(plantService.list()).thenReturn(Collections.emptyList());
+        when(pumpStationService.list()).thenReturn(Collections.emptyList());
+        when(pipeNetworkBaseService.list()).thenReturn(Arrays.asList(drainage, gas, unknown));
+        when(engineeringFacilityService.list()).thenReturn(Collections.emptyList());
+
+        CockpitInfrastructureOperatingInformationSnapshot snapshot = adapter.load();
+
+        assertEquals(2, snapshot.getMetrics().size());
+        assertEquals("DRAINAGE", snapshot.getMetrics().get(0).getIndustry());
+        assertEquals("GAS", snapshot.getMetrics().get(1).getIndustry());
+    }
+
+    private static PipeNetworkBase pipe(String code, String name, String type, String length) {
+        PipeNetworkBase pipe = new PipeNetworkBase();
+        pipe.setNetworkCode(code);
+        pipe.setNetworkName(name);
+        pipe.setNetworkType(type);
+        pipe.setLength(new BigDecimal(length));
+        return pipe;
+    }
+}

+ 269 - 0
pipe-network-service/zksy-system/src/main/java/com/zksy/cockpit/infrastructure/BusinessCockpitInfrastructureOperatingInformationAdapter.java

@@ -0,0 +1,269 @@
+package com.zksy.cockpit.infrastructure;
+
+import com.zksy.WaterSupply.WaterPlantInfo.domain.WaterPlantInfo;
+import com.zksy.WaterSupply.WaterPlantInfo.service.IWaterPlantInfoService;
+import com.zksy.WaterSupply.WaterPumpStation.domain.PumpStationType;
+import com.zksy.WaterSupply.WaterPumpStation.domain.WaterPumpStation;
+import com.zksy.WaterSupply.WaterPumpStation.service.IWaterPumpStationService;
+import com.zksy.base.domain.PipeNetworkBase;
+import com.zksy.base.service.PipeNetworkBaseService;
+import com.zksy.drainage.domain.EngineeringFacility;
+import com.zksy.drainage.service.EngineeringFacilityService;
+import org.springframework.context.annotation.Primary;
+import org.springframework.stereotype.Component;
+
+import java.math.BigDecimal;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Collectors;
+
+/**
+ * 基础设施运行信息真实数据适配器(BT-5 第 7 片)。
+ *
+ * <p>指标只由现有业务实体中明确存在的当前值生成。现有模型没有统一历史采样表,
+ * 因此不会合成趋势点;更新时间取每条源记录的更新时间,缺失时保持空值交由契约层
+ * 使用服务端时间补齐。
+ */
+@Component
+@Primary
+public class BusinessCockpitInfrastructureOperatingInformationAdapter
+        implements CockpitInfrastructureOperatingInformationAdapter {
+
+    private static final ZoneId BUSINESS_ZONE = ZoneId.of("Asia/Shanghai");
+    private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
+
+    private final IWaterPlantInfoService plantService;
+    private final IWaterPumpStationService pumpStationService;
+    private final PipeNetworkBaseService pipeNetworkBaseService;
+    private final EngineeringFacilityService engineeringFacilityService;
+
+    public BusinessCockpitInfrastructureOperatingInformationAdapter(
+            IWaterPlantInfoService plantService,
+            IWaterPumpStationService pumpStationService,
+            PipeNetworkBaseService pipeNetworkBaseService,
+            EngineeringFacilityService engineeringFacilityService) {
+        this.plantService = plantService;
+        this.pumpStationService = pumpStationService;
+        this.pipeNetworkBaseService = pipeNetworkBaseService;
+        this.engineeringFacilityService = engineeringFacilityService;
+    }
+
+    @Override
+    public CockpitInfrastructureOperatingInformationSnapshot load() {
+        List<CockpitInfrastructureOperatingInformationSnapshot.SourceMetric> metrics = new ArrayList<>();
+        String latestUpdatedAt = null;
+        for (WaterPlantInfo plant : safeList(plantService.list())) {
+            if (plant == null || isBlank(plant.getPlantCode()) || plant.getCurrentCapacity() == null) {
+                continue;
+            }
+            String sampledAt = format(plant.getUpdateTime());
+            latestUpdatedAt = latest(latestUpdatedAt, sampledAt);
+            metrics.add(metric(
+                    "water_plant_current_supply",
+                    isBlank(plant.getPlantName()) ? "水厂实际供水能力" : plant.getPlantName() + "实际供水能力",
+                    "SUPPLY",
+                    "万m³/d",
+                    "WATER_SUPPLY",
+                    plant.getCurrentCapacity(),
+                    plant.getDesignCapacity(),
+                    sampledAt,
+                    abnormalStatus(plant.getStatus()),
+                    plant.getPlantCode()));
+        }
+
+        for (WaterPumpStation station : safeList(pumpStationService.list())) {
+            if (station == null || isBlank(station.getStationCode()) || station.getDesignFlow() == null) {
+                continue;
+            }
+            String industry = pumpIndustry(station.getStationType());
+            if (industry == null) {
+                continue;
+            }
+            String sampledAt = format(station.getUpdateTime());
+            latestUpdatedAt = latest(latestUpdatedAt, sampledAt);
+            metrics.add(metric(
+                    metricCode(industry, "pump_station_design_flow"),
+                    isBlank(station.getStationName()) ? "泵站设计流量" : station.getStationName() + "设计流量",
+                    "FLOW",
+                    "m³/h",
+                    industry,
+                    station.getDesignFlow(),
+                    null,
+                    sampledAt,
+                    abnormalStatus(station.getStatus()),
+                    station.getStationCode()));
+        }
+
+        for (PipeNetworkBase pipe : safeList(pipeNetworkBaseService.list())) {
+            if (pipe == null || isBlank(pipe.getNetworkCode()) || pipe.getLength() == null) {
+                continue;
+            }
+            String industry = pipeIndustry(pipe.getNetworkType());
+            if (industry == null) {
+                continue;
+            }
+            String sampledAt = format(pipe.getUpdateTime());
+            latestUpdatedAt = latest(latestUpdatedAt, sampledAt);
+            metrics.add(metric(
+                    metricCode(industry, "pipe_length"),
+                    isBlank(pipe.getNetworkName()) ? "管网长度" : pipe.getNetworkName() + "长度",
+                    "CAPACITY",
+                    "m",
+                    industry,
+                    pipe.getLength(),
+                    null,
+                    sampledAt,
+                    abnormalStatus(pipe.getStatus()),
+                    pipe.getNetworkCode()));
+        }
+
+        for (EngineeringFacility facility : safeList(engineeringFacilityService.list())) {
+            if (facility == null || isBlank(facility.getId()) || facility.getPointCount() == null) {
+                continue;
+            }
+            String typeCode = drainageFacilityType(facility.getFacilityType());
+            if (typeCode == null) {
+                continue;
+            }
+            String sampledAt = format(facility.getUpdateTime());
+            latestUpdatedAt = latest(latestUpdatedAt, sampledAt);
+            metrics.add(metric(
+                    "drainage_" + typeCode.toLowerCase() + "_point_count",
+                    isBlank(facility.getName()) ? "排水设施点数" : facility.getName() + "点数",
+                    "CAPACITY",
+                    "个",
+                    "DRAINAGE",
+                    BigDecimal.valueOf(facility.getPointCount()),
+                    null,
+                    sampledAt,
+                    false,
+                    facility.getId()));
+        }
+
+        return new CockpitInfrastructureOperatingInformationSnapshot(latestUpdatedAt, metrics);
+    }
+
+    private static CockpitInfrastructureOperatingInformationSnapshot.SourceMetric metric(
+            String code,
+            String name,
+            String category,
+            String unit,
+            String industry,
+            BigDecimal value,
+            BigDecimal capacityValue,
+            String sampledAt,
+            boolean abnormal,
+            String pointCode) {
+        return new CockpitInfrastructureOperatingInformationSnapshot.SourceMetric(
+                code,
+                name,
+                category,
+                unit,
+                industry,
+                value,
+                capacityValue,
+                sampledAt,
+                abnormal,
+                pointCode == null ? Collections.emptyList() : Collections.singletonList(pointCode.trim()),
+                Collections.emptyList());
+    }
+
+    private static String pumpIndustry(String stationType) {
+        if (PumpStationType.WATER_SUPPLY.equalsIgnoreCase(trimToNull(stationType))) {
+            return "WATER_SUPPLY";
+        }
+        if (PumpStationType.DRAINAGE.equalsIgnoreCase(trimToNull(stationType))) {
+            return "DRAINAGE";
+        }
+        return null;
+    }
+
+    private static String pipeIndustry(String networkType) {
+        String normalized = trimToNull(networkType);
+        if (normalized == null) {
+            return null;
+        }
+        if ("2".equals(normalized)
+                || "WATER_SUPPLY".equalsIgnoreCase(normalized)
+                || "供水".equals(normalized)
+                || "供水管网".equals(normalized)) {
+            return "WATER_SUPPLY";
+        }
+        if ("GAS".equalsIgnoreCase(normalized)
+                || "3".equals(normalized)
+                || "燃气".equals(normalized)
+                || "燃气管网".equals(normalized)) {
+            return "GAS";
+        }
+        if ("DRAINAGE".equalsIgnoreCase(normalized)
+                || "排水".equals(normalized)
+                || "排水管网".equals(normalized)
+                || "SEWER".equalsIgnoreCase(normalized)) {
+            return "DRAINAGE";
+        }
+        return null;
+    }
+
+    private static String drainageFacilityType(String facilityType) {
+        String normalized = trimToNull(facilityType);
+        if (normalized == null) {
+            return null;
+        }
+        if (normalized.contains("闸")) {
+            return "gate";
+        }
+        if (normalized.contains("调蓄") || normalized.contains("蓄水")) {
+            return "storage";
+        }
+        if (normalized.contains("泵站")) {
+            return "pump_station";
+        }
+        return null;
+    }
+
+    private static String metricCode(String industry, String suffix) {
+        return industry.toLowerCase() + "_" + suffix;
+    }
+
+    private static boolean abnormalStatus(String status) {
+        String normalized = trimToNull(status);
+        return normalized != null && !"0".equals(normalized)
+                && !"NORMAL".equalsIgnoreCase(normalized)
+                && !"RUNNING".equalsIgnoreCase(normalized)
+                && !"ACTIVE".equalsIgnoreCase(normalized);
+    }
+
+    private static String format(LocalDateTime time) {
+        return time == null ? null : TIME_FORMATTER.format(time.atZone(BUSINESS_ZONE).toOffsetDateTime());
+    }
+
+    private static String latest(String left, String right) {
+        if (left == null) {
+            return right;
+        }
+        if (right == null) {
+            return left;
+        }
+        return left.compareTo(right) >= 0 ? left : right;
+    }
+
+    private static <T> List<T> safeList(List<T> value) {
+        return value == null
+                ? Collections.emptyList()
+                : value.stream().filter(Objects::nonNull).collect(Collectors.toList());
+    }
+
+    private static String trimToNull(String value) {
+        return isBlank(value) ? null : value.trim();
+    }
+
+    private static boolean isBlank(String value) {
+        return value == null || value.trim().isEmpty();
+    }
+}