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

feat(cockpit): 接入基础档案真实数据

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

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

@@ -0,0 +1,135 @@
+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.BusinessCockpitInfrastructureBaseArchivesAdapter;
+import com.zksy.cockpit.infrastructure.CockpitInfrastructureBaseArchivesSnapshot;
+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.Clock;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.ZoneOffset;
+import java.util.Arrays;
+import java.util.Collections;
+
+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.when;
+
+class BusinessCockpitInfrastructureBaseArchivesAdapterTest {
+
+    private IWaterPlantInfoService plantService;
+    private IWaterPumpStationService pumpStationService;
+    private PipeNetworkBaseService pipeNetworkBaseService;
+    private EngineeringFacilityService engineeringFacilityService;
+    private BusinessCockpitInfrastructureBaseArchivesAdapter adapter;
+
+    @BeforeEach
+    void setUp() {
+        plantService = mock(IWaterPlantInfoService.class);
+        pumpStationService = mock(IWaterPumpStationService.class);
+        pipeNetworkBaseService = mock(PipeNetworkBaseService.class);
+        engineeringFacilityService = mock(EngineeringFacilityService.class);
+        adapter = new BusinessCockpitInfrastructureBaseArchivesAdapter(
+                plantService, pumpStationService, pipeNetworkBaseService, engineeringFacilityService,
+                Clock.fixed(Instant.parse("2026-09-02T02:00:00Z"), ZoneOffset.UTC));
+    }
+
+    @Test
+    void mapsExistingFacilityTablesToFormalTypesWithoutInventingUnavailableFields() {
+        WaterPlantInfo plant = new WaterPlantInfo();
+        plant.setPlantCode("PLANT-001");
+        plant.setPlantName("第一水厂");
+        plant.setCommissionDate(LocalDate.of(2022, 1, 2));
+
+        WaterPumpStation pump = new WaterPumpStation();
+        pump.setStationCode("PUMP-001");
+        pump.setStationName("城北泵站");
+        pump.setStationType(PumpStationType.WATER_SUPPLY);
+        pump.setCommissionDate(LocalDate.of(2019, 2, 3));
+
+        PipeNetworkBase pipe = new PipeNetworkBase();
+        pipe.setNetworkCode("PIPE-001");
+        pipe.setNetworkName("供水主线");
+        pipe.setNetworkType("2");
+        pipe.setLength(new BigDecimal("2500"));
+        pipe.setBuildDate(java.sql.Date.valueOf(LocalDate.of(2010, 3, 4)));
+
+        EngineeringFacility drainage = new EngineeringFacility();
+        drainage.setId("DRAIN-001");
+        drainage.setName("一号调蓄池");
+        drainage.setArea("CB");
+        drainage.setFacilityType("调蓄设施");
+
+        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(drainage));
+
+        CockpitInfrastructureBaseArchivesSnapshot snapshot = adapter.load();
+
+        assertEquals(4, snapshot.getFacilities().size());
+        assertEquals(8, snapshot.getTypes().size());
+        assertEquals("DRAINAGE_STORAGE", snapshot.getTypes().get(6).getCode());
+        CockpitInfrastructureBaseArchivesSnapshot.SourceFacility plantFacility =
+                snapshot.getFacilities().get(0);
+        assertEquals("PLANT-001", plantFacility.getCode());
+        assertEquals("WATER_PLANT", plantFacility.getTypeCode());
+        assertEquals(LocalDate.of(2022, 1, 2), plantFacility.getCommissionedAt());
+        assertNull(plantFacility.getScale(), "现有字段没有统一规模口径,不应猜测");
+
+        CockpitInfrastructureBaseArchivesSnapshot.SourceFacility pipeFacility =
+                snapshot.getFacilities().stream()
+                        .filter(item -> "PIPE-001".equals(item.getCode()))
+                        .findFirst()
+                        .orElseThrow(AssertionError::new);
+        assertEquals("WATER_PIPE", pipeFacility.getTypeCode());
+        assertEquals(0, new BigDecimal("2.500").compareTo(pipeFacility.getLengthKm()));
+        assertEquals(LocalDate.of(2010, 3, 4), pipeFacility.getBuiltAt());
+
+        CockpitInfrastructureBaseArchivesSnapshot.SourceFacility drainageFacility =
+                snapshot.getFacilities().stream()
+                        .filter(item -> "DRAIN-001".equals(item.getCode()))
+                        .findFirst()
+                        .orElseThrow(AssertionError::new);
+        assertEquals("DRAINAGE_STORAGE", drainageFacility.getTypeCode());
+        assertEquals("CB", drainageFacility.getRegion());
+    }
+
+    @Test
+    void mapsDrainageAndGasPipeKindsAndSkipsUnclassifiableRows() {
+        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());
+
+        CockpitInfrastructureBaseArchivesSnapshot snapshot = adapter.load();
+
+        assertEquals(2, snapshot.getFacilities().size());
+        assertEquals("DRAINAGE_PIPE", snapshot.getFacilities().get(0).getTypeCode());
+        assertEquals("GAS_PIPE", snapshot.getFacilities().get(1).getTypeCode());
+    }
+
+    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;
+    }
+}

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

@@ -0,0 +1,212 @@
+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.Clock;
+import java.time.LocalDate;
+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 第 6 片)。
+ *
+ * <p>组合现有设施表,不新增投影:水厂、共用泵站、统一管网基础表和排水工程设施。
+ * 只有实体明确提供的字段才映射;缺少统一来源的规模、区域或长度保持 null,避免把
+ * 容量、坐标或展示文本误当作基础档案事实。
+ */
+@Component
+@Primary
+public class BusinessCockpitInfrastructureBaseArchivesAdapter
+        implements CockpitInfrastructureBaseArchivesAdapter {
+
+    private static final ZoneId BUSINESS_ZONE = ZoneId.of("Asia/Shanghai");
+    private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
+    private static final List<CockpitInfrastructureBaseArchivesSnapshot.SourceType> TYPES = Arrays.asList(
+            type("WATER_PLANT", "水厂", "WATER_SUPPLY"),
+            type("WATER_PUMP_STATION", "供水泵站", "WATER_SUPPLY"),
+            type("WATER_PIPE", "供水管线", "WATER_SUPPLY"),
+            type("GAS_PIPE", "燃气管线", "GAS"),
+            type("DRAINAGE_PIPE", "排水管线", "DRAINAGE"),
+            type("DRAINAGE_GATE", "排水闸门", "DRAINAGE"),
+            type("DRAINAGE_STORAGE", "排水调蓄设施", "DRAINAGE"),
+            type("DRAINAGE_PUMP_STATION", "排水泵站", "DRAINAGE"));
+
+    private final IWaterPlantInfoService plantService;
+    private final IWaterPumpStationService pumpStationService;
+    private final PipeNetworkBaseService pipeNetworkBaseService;
+    private final EngineeringFacilityService engineeringFacilityService;
+    private final Clock clock;
+
+    public BusinessCockpitInfrastructureBaseArchivesAdapter(
+            IWaterPlantInfoService plantService,
+            IWaterPumpStationService pumpStationService,
+            PipeNetworkBaseService pipeNetworkBaseService,
+            EngineeringFacilityService engineeringFacilityService,
+            Clock clock) {
+        this.plantService = plantService;
+        this.pumpStationService = pumpStationService;
+        this.pipeNetworkBaseService = pipeNetworkBaseService;
+        this.engineeringFacilityService = engineeringFacilityService;
+        this.clock = clock;
+    }
+
+    @Override
+    public CockpitInfrastructureBaseArchivesSnapshot load() {
+        List<CockpitInfrastructureBaseArchivesSnapshot.SourceFacility> facilities = new ArrayList<>();
+        addPlants(facilities);
+        addPumpStations(facilities);
+        addPipes(facilities);
+        addDrainageFacilities(facilities);
+        return new CockpitInfrastructureBaseArchivesSnapshot(
+                TIME_FORMATTER.format(clock.instant().atZone(BUSINESS_ZONE)), TYPES, facilities);
+    }
+
+    private void addPlants(List<CockpitInfrastructureBaseArchivesSnapshot.SourceFacility> facilities) {
+        for (WaterPlantInfo plant : safeList(plantService.list())) {
+            String code = trimToNull(plant.getPlantCode());
+            if (code == null) {
+                continue;
+            }
+            facilities.add(facility(code, "WATER_PLANT", null, null,
+                    plant.getCommissionDate(), null));
+        }
+    }
+
+    private void addPumpStations(List<CockpitInfrastructureBaseArchivesSnapshot.SourceFacility> facilities) {
+        for (WaterPumpStation station : safeList(pumpStationService.list())) {
+            String code = trimToNull(station.getStationCode());
+            if (code == null) {
+                continue;
+            }
+            String stationType = trimToNull(station.getStationType());
+            String typeCode = PumpStationType.DRAINAGE.equalsIgnoreCase(stationType)
+                    ? "DRAINAGE_PUMP_STATION"
+                    : PumpStationType.WATER_SUPPLY.equalsIgnoreCase(stationType)
+                            ? "WATER_PUMP_STATION" : null;
+            if (typeCode == null) {
+                continue;
+            }
+            facilities.add(facility(code, typeCode, null, null,
+                    station.getCommissionDate(), null));
+        }
+    }
+
+    private void addPipes(List<CockpitInfrastructureBaseArchivesSnapshot.SourceFacility> facilities) {
+        for (PipeNetworkBase pipe : safeList(pipeNetworkBaseService.list())) {
+            String code = trimToNull(pipe.getNetworkCode());
+            String typeCode = pipeType(pipe == null ? null : pipe.getNetworkType());
+            if (code == null || typeCode == null) {
+                continue;
+            }
+            BigDecimal lengthKm = pipe.getLength() == null
+                    ? null : pipe.getLength().divide(BigDecimal.valueOf(1000));
+            LocalDate builtAt = pipe.getBuildDate() == null
+                    ? null : new java.sql.Date(pipe.getBuildDate().getTime()).toLocalDate();
+            facilities.add(facility(code, typeCode, null, lengthKm, null, builtAt));
+        }
+    }
+
+    private void addDrainageFacilities(
+            List<CockpitInfrastructureBaseArchivesSnapshot.SourceFacility> facilities) {
+        for (EngineeringFacility source : safeList(engineeringFacilityService.list())) {
+            String code = trimToNull(source.getId());
+            String typeCode = drainageFacilityType(source.getFacilityType());
+            if (code == null || typeCode == null) {
+                continue;
+            }
+            facilities.add(facility(code, typeCode, trimToNull(source.getArea()), null, null,
+                    source.getCreateTime() == null ? null : source.getCreateTime().toLocalDate()));
+        }
+    }
+
+    private static String pipeType(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_PIPE";
+        }
+        if ("GAS".equalsIgnoreCase(normalized)
+                || "3".equals(normalized)
+                || "燃气".equals(normalized)
+                || "燃气管网".equals(normalized)) {
+            return "GAS_PIPE";
+        }
+        if ("DRAINAGE".equalsIgnoreCase(normalized)
+                || "排水".equals(normalized)
+                || "排水管网".equals(normalized)
+                || "SEWER".equalsIgnoreCase(normalized)) {
+            return "DRAINAGE_PIPE";
+        }
+        return null;
+    }
+
+    private static String drainageFacilityType(String facilityType) {
+        String normalized = trimToNull(facilityType);
+        if (normalized == null) {
+            return null;
+        }
+        if (normalized.contains("闸")) {
+            return "DRAINAGE_GATE";
+        }
+        if (normalized.contains("调蓄") || normalized.contains("蓄水")) {
+            return "DRAINAGE_STORAGE";
+        }
+        if (normalized.contains("泵站")) {
+            return "DRAINAGE_PUMP_STATION";
+        }
+        return null;
+    }
+
+    private static CockpitInfrastructureBaseArchivesSnapshot.SourceFacility facility(
+            String code,
+            String typeCode,
+            String region,
+            BigDecimal lengthKm,
+            LocalDate commissionedAt,
+            LocalDate builtAt) {
+        return new CockpitInfrastructureBaseArchivesSnapshot.SourceFacility(
+                code, typeCode, region, lengthKm, null, commissionedAt, builtAt);
+    }
+
+    private static CockpitInfrastructureBaseArchivesSnapshot.SourceType type(
+            String code, String name, String industry) {
+        return new CockpitInfrastructureBaseArchivesSnapshot.SourceType(
+                code, name, industry, "FACILITY");
+    }
+
+    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();
+    }
+}