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

feat(cockpit): 接入管理信息真实数据

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

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

@@ -0,0 +1,156 @@
+package com.zksy.web.controller.cockpit;
+
+import com.zksy.WaterSupply.WaterSourceInfo.domain.WaterSourceInfo;
+import com.zksy.WaterSupply.WaterSourceInfo.service.IWaterSourceInfoService;
+import com.zksy.base.domain.EquipmentBase;
+import com.zksy.base.domain.EquipmentType;
+import com.zksy.base.domain.ManholeBase;
+import com.zksy.base.domain.PipeNetworkBase;
+import com.zksy.base.service.EquipmentBaseService;
+import com.zksy.base.service.EquipmentTypeService;
+import com.zksy.base.service.ManholeBaseService;
+import com.zksy.base.service.PipeNetworkBaseService;
+import com.zksy.cockpit.infrastructure.BusinessCockpitInfrastructureManagementInformationAdapter;
+import com.zksy.cockpit.infrastructure.CockpitInfrastructureManagementInformationSnapshot;
+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 java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class BusinessCockpitInfrastructureManagementInformationAdapterTest {
+
+    private EquipmentBaseService equipmentBaseService;
+    private EquipmentTypeService equipmentTypeService;
+    private ManholeBaseService manholeBaseService;
+    private PipeNetworkBaseService pipeNetworkBaseService;
+    private IWaterSourceInfoService waterSourceInfoService;
+    private BusinessCockpitInfrastructureManagementInformationAdapter adapter;
+
+    @BeforeEach
+    void setUp() {
+        equipmentBaseService = mock(EquipmentBaseService.class);
+        equipmentTypeService = mock(EquipmentTypeService.class);
+        manholeBaseService = mock(ManholeBaseService.class);
+        pipeNetworkBaseService = mock(PipeNetworkBaseService.class);
+        waterSourceInfoService = mock(IWaterSourceInfoService.class);
+        adapter = new BusinessCockpitInfrastructureManagementInformationAdapter(
+                equipmentBaseService,
+                equipmentTypeService,
+                manholeBaseService,
+                pipeNetworkBaseService,
+                waterSourceInfoService);
+    }
+
+    @Test
+    void mapsOwnershipAndManagementRelationsFromExistingSourceFields() {
+        EquipmentType waterRoot = type(1, "供水", 0);
+        EquipmentType waterLeaf = type(11, "压力计", 1);
+        EquipmentType gasRoot = type(3, "燃气", 0);
+
+        EquipmentBase device = new EquipmentBase();
+        device.setEquipmentCode("DEV-001");
+        device.setEquipmentName("一号压力计");
+        device.setEquipmentTypeId(11);
+        device.setOwnershipUnit("供水公司");
+        device.setUpdateTime(LocalDateTime.parse("2026-09-02T09:58:00"));
+
+        ManholeBase manhole = new ManholeBase();
+        manhole.setManholeCode("MH-001");
+        manhole.setManholeName("一号窨井");
+        manhole.setOwnershipUnit("排水所");
+        manhole.setUpdateTime(LocalDateTime.parse("2026-09-02T09:59:00"));
+
+        PipeNetworkBase pipe = new PipeNetworkBase();
+        pipe.setNetworkCode("PIPE-001");
+        pipe.setNetworkName("排水主线");
+        pipe.setNetworkType("DRAINAGE");
+        pipe.setManagementUnit("排水所");
+        pipe.setUpdateTime(LocalDateTime.parse("2026-09-02T09:57:00"));
+
+        WaterSourceInfo source = new WaterSourceInfo();
+        source.setSourceId("SOURCE-001");
+        source.setSourceName("一号水源地");
+        source.setUnit("水务局");
+        source.setUpdateTime(LocalDateTime.parse("2026-09-02T09:56:00"));
+
+        when(equipmentTypeService.list()).thenReturn(Arrays.asList(waterRoot, waterLeaf, gasRoot));
+        when(equipmentBaseService.list()).thenReturn(Collections.singletonList(device));
+        when(manholeBaseService.list()).thenReturn(Collections.singletonList(manhole));
+        when(pipeNetworkBaseService.list()).thenReturn(Collections.singletonList(pipe));
+        when(waterSourceInfoService.list()).thenReturn(Collections.singletonList(source));
+
+        CockpitInfrastructureManagementInformationSnapshot snapshot = adapter.load();
+
+        assertEquals("2026-09-02T09:59:00+08:00", snapshot.getUpdatedAt());
+        assertEquals(4, snapshot.getRelations().size());
+
+        CockpitInfrastructureManagementInformationSnapshot.SourceRelation deviceRelation =
+                relation(snapshot.getRelations(), "DEV-001");
+        assertEquals("SMART_DEVICE", deviceRelation.getObjectKind());
+        assertEquals("WATER_SUPPLY", deviceRelation.getIndustry());
+        assertEquals("供水公司", deviceRelation.getOrganizationCode());
+        assertEquals("OWNED", deviceRelation.getOwnershipType());
+
+        CockpitInfrastructureManagementInformationSnapshot.SourceRelation pipeRelation =
+                relation(snapshot.getRelations(), "PIPE-001");
+        assertEquals("FACILITY", pipeRelation.getObjectKind());
+        assertEquals("DRAINAGE", pipeRelation.getIndustry());
+        assertEquals("排水所", pipeRelation.getOrganizationCode());
+        assertEquals("MANAGED", pipeRelation.getOwnershipType());
+
+        assertEquals("MANHOLE", relation(snapshot.getRelations(), "MH-001").getIndustry());
+        assertEquals("WATER_SUPPLY", relation(snapshot.getRelations(), "SOURCE-001").getIndustry());
+    }
+
+    @Test
+    void skipsRowsWithoutAuthoritativeIndustryOrOrganization() {
+        EquipmentBase noUnit = new EquipmentBase();
+        noUnit.setEquipmentCode("DEV-NO-UNIT");
+        noUnit.setEquipmentTypeId(1);
+
+        EquipmentBase unknownType = new EquipmentBase();
+        unknownType.setEquipmentCode("DEV-UNKNOWN-TYPE");
+        unknownType.setEquipmentTypeId(99);
+        unknownType.setOwnershipUnit("未知单位");
+
+        PipeNetworkBase noManagement = new PipeNetworkBase();
+        noManagement.setNetworkCode("PIPE-NO-UNIT");
+        noManagement.setNetworkType("2");
+
+        when(equipmentTypeService.list()).thenReturn(Collections.singletonList(type(1, "供水", 0)));
+        when(equipmentBaseService.list()).thenReturn(Arrays.asList(noUnit, unknownType));
+        when(manholeBaseService.list()).thenReturn(Collections.emptyList());
+        when(pipeNetworkBaseService.list()).thenReturn(Collections.singletonList(noManagement));
+        when(waterSourceInfoService.list()).thenReturn(Collections.emptyList());
+
+        CockpitInfrastructureManagementInformationSnapshot snapshot = adapter.load();
+
+        assertTrue(snapshot.getRelations().isEmpty());
+    }
+
+    private static EquipmentType type(int id, String name, int parentId) {
+        EquipmentType type = new EquipmentType();
+        type.setId(id);
+        type.setTypeName(name);
+        type.setParentId(parentId);
+        return type;
+    }
+
+    private static CockpitInfrastructureManagementInformationSnapshot.SourceRelation relation(
+            List<CockpitInfrastructureManagementInformationSnapshot.SourceRelation> relations,
+            String code) {
+        return relations.stream()
+                .filter(item -> code.equals(item.getObjectCode()))
+                .findFirst()
+                .orElseThrow(AssertionError::new);
+    }
+}

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

@@ -0,0 +1,203 @@
+package com.zksy.cockpit.infrastructure;
+
+import com.zksy.WaterSupply.WaterSourceInfo.domain.WaterSourceInfo;
+import com.zksy.WaterSupply.WaterSourceInfo.service.IWaterSourceInfoService;
+import com.zksy.base.domain.EquipmentBase;
+import com.zksy.base.domain.EquipmentType;
+import com.zksy.base.domain.ManholeBase;
+import com.zksy.base.domain.PipeNetworkBase;
+import com.zksy.base.service.EquipmentBaseService;
+import com.zksy.base.service.EquipmentTypeService;
+import com.zksy.base.service.ManholeBaseService;
+import com.zksy.base.service.PipeNetworkBaseService;
+import com.zksy.cockpit.foundation.CockpitTypeIndustry;
+import org.springframework.context.annotation.Primary;
+import org.springframework.stereotype.Component;
+
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.stream.Collectors;
+
+/**
+ * 管理信息真实数据适配器。
+ *
+ * <p>当前基础表没有独立组织主数据表,因此将现有的权属/管养/所属单位原值作为
+ * 组织编码和名称。没有单位、设备类型无法归属正式行业的记录不猜测归属并跳过。
+ */
+@Component
+@Primary
+public class BusinessCockpitInfrastructureManagementInformationAdapter
+        implements CockpitInfrastructureManagementInformationAdapter {
+
+    private static final ZoneId BUSINESS_ZONE = ZoneId.of("Asia/Shanghai");
+    private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
+
+    private final EquipmentBaseService equipmentBaseService;
+    private final EquipmentTypeService equipmentTypeService;
+    private final ManholeBaseService manholeBaseService;
+    private final PipeNetworkBaseService pipeNetworkBaseService;
+    private final IWaterSourceInfoService waterSourceInfoService;
+
+    public BusinessCockpitInfrastructureManagementInformationAdapter(
+            EquipmentBaseService equipmentBaseService,
+            EquipmentTypeService equipmentTypeService,
+            ManholeBaseService manholeBaseService,
+            PipeNetworkBaseService pipeNetworkBaseService,
+            IWaterSourceInfoService waterSourceInfoService) {
+        this.equipmentBaseService = equipmentBaseService;
+        this.equipmentTypeService = equipmentTypeService;
+        this.manholeBaseService = manholeBaseService;
+        this.pipeNetworkBaseService = pipeNetworkBaseService;
+        this.waterSourceInfoService = waterSourceInfoService;
+    }
+
+    @Override
+    public CockpitInfrastructureManagementInformationSnapshot load() {
+        List<CockpitInfrastructureManagementInformationSnapshot.SourceRelation> relations =
+                new ArrayList<>();
+        String[] latestUpdatedAt = new String[1];
+        Map<Integer, EquipmentType> types = typeIndex(equipmentTypeService.list());
+
+        for (EquipmentBase device : safeList(equipmentBaseService.list())) {
+            if (device == null) {
+                continue;
+            }
+            String objectCode = trimToNull(device.getEquipmentCode());
+            String organization = trimToNull(device.getOwnershipUnit());
+            String industry = device.getEquipmentTypeId() == null
+                    ? null
+                    : CockpitTypeIndustry.industryOf(types.get(device.getEquipmentTypeId()), types);
+            if (objectCode == null || organization == null || industry == null) {
+                continue;
+            }
+            relations.add(relation(objectCode, "SMART_DEVICE", industry,
+                    organization, organization, "OWNED"));
+            latestUpdatedAt[0] = latest(latestUpdatedAt[0], format(device.getUpdateTime()));
+        }
+
+        for (ManholeBase manhole : safeList(manholeBaseService.list())) {
+            if (manhole == null) {
+                continue;
+            }
+            String objectCode = trimToNull(manhole.getManholeCode());
+            String organization = trimToNull(manhole.getOwnershipUnit());
+            if (objectCode == null || organization == null) {
+                continue;
+            }
+            relations.add(relation(objectCode, "FACILITY", "MANHOLE",
+                    organization, organization, "OWNED"));
+            latestUpdatedAt[0] = latest(latestUpdatedAt[0], format(manhole.getUpdateTime()));
+        }
+
+        for (PipeNetworkBase pipe : safeList(pipeNetworkBaseService.list())) {
+            if (pipe == null) {
+                continue;
+            }
+            String objectCode = trimToNull(pipe.getNetworkCode());
+            String organization = trimToNull(pipe.getManagementUnit());
+            String industry = pipeIndustry(pipe.getNetworkType());
+            if (objectCode == null || organization == null || industry == null) {
+                continue;
+            }
+            relations.add(relation(objectCode, "FACILITY", industry,
+                    organization, organization, "MANAGED"));
+            latestUpdatedAt[0] = latest(latestUpdatedAt[0], format(pipe.getUpdateTime()));
+        }
+
+        for (WaterSourceInfo source : safeList(waterSourceInfoService.list())) {
+            if (source == null) {
+                continue;
+            }
+            String objectCode = trimToNull(source.getSourceId());
+            String organization = trimToNull(source.getUnit());
+            if (objectCode == null || organization == null) {
+                continue;
+            }
+            relations.add(relation(objectCode, "FACILITY", "WATER_SUPPLY",
+                    organization, organization, "OWNED"));
+            latestUpdatedAt[0] = latest(latestUpdatedAt[0], format(source.getUpdateTime()));
+        }
+
+        return new CockpitInfrastructureManagementInformationSnapshot(latestUpdatedAt[0], relations);
+    }
+
+    private static CockpitInfrastructureManagementInformationSnapshot.SourceRelation relation(
+            String objectCode,
+            String objectKind,
+            String industry,
+            String organizationCode,
+            String organizationName,
+            String ownershipType) {
+        return new CockpitInfrastructureManagementInformationSnapshot.SourceRelation(
+                objectCode, objectKind, industry, organizationCode, organizationName, ownershipType);
+    }
+
+    private static Map<Integer, EquipmentType> typeIndex(List<EquipmentType> values) {
+        Map<Integer, EquipmentType> result = new HashMap<>();
+        for (EquipmentType value : safeList(values)) {
+            if (value != null && value.getId() != null) {
+                result.put(value.getId(), value);
+            }
+        }
+        return result;
+    }
+
+    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 ("3".equals(normalized)
+                || "GAS".equalsIgnoreCase(normalized)
+                || "燃气".equals(normalized)
+                || "燃气管网".equals(normalized)) {
+            return "GAS";
+        }
+        if ("DRAINAGE".equalsIgnoreCase(normalized)
+                || "SEWER".equalsIgnoreCase(normalized)
+                || "排水".equals(normalized)
+                || "排水管网".equals(normalized)) {
+            return "DRAINAGE";
+        }
+        return null;
+    }
+
+    private static String format(LocalDateTime value) {
+        return value == null
+                ? null
+                : TIME_FORMATTER.format(value.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 value == null || value.trim().isEmpty() ? null : value.trim();
+    }
+}