Bladeren bron

feat(cockpit): 接入应急保障真实数据

Kazerin 2 maanden geleden
bovenliggende
commit
7d3190446d

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

@@ -0,0 +1,158 @@
+package com.zksy.web.controller.cockpit;
+
+import com.zksy.base.domain.EquipmentBase;
+import com.zksy.base.domain.EquipmentType;
+import com.zksy.base.domain.HazardHiddenAccount;
+import com.zksy.base.domain.RiskAssessment;
+import com.zksy.base.service.EquipmentBaseService;
+import com.zksy.base.service.EquipmentTypeService;
+import com.zksy.base.service.HazardHiddenAccountService;
+import com.zksy.base.service.RiskAssessmentService;
+import com.zksy.base.alarm.domain.AlarmData;
+import com.zksy.base.alarm.service.AlarmDataService;
+import com.zksy.base.warning.domain.EarlyWarning;
+import com.zksy.base.warning.service.EarlyWarningService;
+import com.zksy.cockpit.infrastructure.BusinessCockpitInfrastructureEmergencySupportAdapter;
+import com.zksy.cockpit.infrastructure.CockpitInfrastructureEmergencySupportSnapshot;
+import com.zksy.ops.domain.WorkOrder;
+import com.zksy.ops.service.WorkOrderService;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+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.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class BusinessCockpitInfrastructureEmergencySupportAdapterTest {
+
+    private EquipmentBaseService equipmentBaseService;
+    private EquipmentTypeService equipmentTypeService;
+    private HazardHiddenAccountService hazardService;
+    private RiskAssessmentService riskService;
+    private AlarmDataService alarmService;
+    private EarlyWarningService warningService;
+    private WorkOrderService workOrderService;
+    private BusinessCockpitInfrastructureEmergencySupportAdapter adapter;
+
+    @BeforeEach
+    void setUp() {
+        equipmentBaseService = mock(EquipmentBaseService.class);
+        equipmentTypeService = mock(EquipmentTypeService.class);
+        hazardService = mock(HazardHiddenAccountService.class);
+        riskService = mock(RiskAssessmentService.class);
+        alarmService = mock(AlarmDataService.class);
+        warningService = mock(EarlyWarningService.class);
+        workOrderService = mock(WorkOrderService.class);
+        adapter = new BusinessCockpitInfrastructureEmergencySupportAdapter(
+                equipmentBaseService,
+                equipmentTypeService,
+                hazardService,
+                riskService,
+                alarmService,
+                warningService,
+                workOrderService);
+    }
+
+    @Test
+    void mapsEmergencyResourcesHazardsAndProtectionTargetsWithEventFacts() {
+        EquipmentType gasRoot = type(3, "燃气", 0);
+        EquipmentBase device = new EquipmentBase();
+        device.setEquipmentId("EQUIP-001");
+        device.setEquipmentCode("GAS-001");
+        device.setEquipmentName("燃气阀门");
+        device.setEquipmentTypeId(3);
+        device.setEquipmentLocation("城北");
+
+        HazardHiddenAccount hazard = new HazardHiddenAccount();
+        hazard.setHazardId("HZ-001");
+        hazard.setHazardNo("HZNO-001");
+        hazard.setHazardDesc("燃气管网泄漏隐患");
+        hazard.setHazardLevel("重大");
+        hazard.setHazardStatus("整改中");
+        hazard.setEquipmentId("EQUIP-001");
+        hazard.setLongitude(null);
+        hazard.setLatitude(null);
+        hazard.setUpdateTime(LocalDateTime.parse("2026-09-02T09:59:50"));
+
+        RiskAssessment risk = new RiskAssessment();
+        risk.setRiskId("RISK-001");
+        risk.setNetworkId("NETWORK-001");
+        risk.setRiskLevel("高");
+        risk.setProtectiveTargets("城北医院");
+        risk.setHazardSources("燃气阀门");
+
+        AlarmData alarm = new AlarmData();
+        alarm.setDeviceCode("GAS-001");
+        alarm.setAlarmStatus(0);
+        EarlyWarning warning = new EarlyWarning();
+        warning.setDeviceCode("GAS-001");
+        warning.setStatus("PENDING");
+        WorkOrder order = new WorkOrder();
+        order.setDeviceId("EQUIP-001");
+        order.setOrderStatus(3);
+
+        when(equipmentTypeService.list()).thenReturn(Collections.singletonList(gasRoot));
+        when(equipmentBaseService.list()).thenReturn(Collections.singletonList(device));
+        when(hazardService.list()).thenReturn(Collections.singletonList(hazard));
+        when(riskService.list()).thenReturn(Collections.singletonList(risk));
+        when(alarmService.list()).thenReturn(Collections.singletonList(alarm));
+        when(warningService.list()).thenReturn(Collections.singletonList(warning));
+        when(workOrderService.list()).thenReturn(Collections.singletonList(order));
+
+        CockpitInfrastructureEmergencySupportSnapshot snapshot = adapter.load();
+
+        assertEquals(3, snapshot.getSections().size());
+        CockpitInfrastructureEmergencySupportSnapshot.SourceSection resource = section(snapshot, "EMERGENCY_RESOURCE");
+        assertEquals(1, resource.getItems().size());
+        assertEquals("GAS", resource.getItems().get(0).getIndustry());
+        assertEquals(1, resource.getItems().get(0).getEventAssociations().getWarningCount());
+        assertEquals(1, resource.getItems().get(0).getEventAssociations().getAlarmCount());
+        assertEquals(1, resource.getItems().get(0).getEventAssociations().getWorkOrderCount());
+
+        CockpitInfrastructureEmergencySupportSnapshot.SourceSection hazardSection = section(snapshot, "HAZARD_SOURCE");
+        assertEquals(1, hazardSection.getItems().size());
+        assertEquals("HZ-001", hazardSection.getItems().get(0).getObjectCode());
+        assertEquals("GAS", hazardSection.getItems().get(0).getIndustry());
+
+        CockpitInfrastructureEmergencySupportSnapshot.SourceSection target = section(snapshot, "PROTECTION_TARGET");
+        assertEquals(1, target.getItems().size());
+        assertEquals("RISK-001", target.getItems().get(0).getObjectCode());
+        assertEquals("GAS", target.getItems().get(0).getIndustry());
+    }
+
+    @Test
+    void returnsEmptySectionsWhenSourcesAreEmpty() {
+        when(equipmentTypeService.list()).thenReturn(Collections.emptyList());
+        when(equipmentBaseService.list()).thenReturn(Collections.emptyList());
+        when(hazardService.list()).thenReturn(Collections.emptyList());
+        when(riskService.list()).thenReturn(Collections.emptyList());
+        when(alarmService.list()).thenReturn(Collections.emptyList());
+        when(warningService.list()).thenReturn(Collections.emptyList());
+        when(workOrderService.list()).thenReturn(Collections.emptyList());
+
+        CockpitInfrastructureEmergencySupportSnapshot snapshot = adapter.load();
+
+        assertTrue(snapshot.getSections().stream().allMatch(item -> item.getItems().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 CockpitInfrastructureEmergencySupportSnapshot.SourceSection section(
+            CockpitInfrastructureEmergencySupportSnapshot snapshot, String category) {
+        return snapshot.getSections().stream()
+                .filter(item -> category.equals(item.getCategory()))
+                .findFirst()
+                .orElseThrow(AssertionError::new);
+    }
+}

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

@@ -0,0 +1,312 @@
+package com.zksy.cockpit.infrastructure;
+
+import com.zksy.base.alarm.domain.AlarmData;
+import com.zksy.base.alarm.service.AlarmDataService;
+import com.zksy.base.domain.EquipmentBase;
+import com.zksy.base.domain.EquipmentType;
+import com.zksy.base.domain.HazardHiddenAccount;
+import com.zksy.base.domain.RiskAssessment;
+import com.zksy.base.service.EquipmentBaseService;
+import com.zksy.base.service.EquipmentTypeService;
+import com.zksy.base.service.HazardHiddenAccountService;
+import com.zksy.base.service.RiskAssessmentService;
+import com.zksy.base.warning.domain.EarlyWarning;
+import com.zksy.base.warning.service.EarlyWarningService;
+import com.zksy.cockpit.foundation.CockpitTypeIndustry;
+import com.zksy.ops.domain.WorkOrder;
+import com.zksy.ops.service.WorkOrderService;
+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.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * 应急保障真实数据适配器。
+ *
+ * <p>应急资源使用设备台账,危险源使用隐患台账,防护目标使用燃气风险评估中的
+ * 防护目标字段。三类现有业务表没有统一的应急对象主表,因此仅在字段能够明确
+ * 关联时输出对象;事件关联按设备编码、设备主键和现行在途/未处理状态计算。</p>
+ */
+@Component
+@Primary
+public class BusinessCockpitInfrastructureEmergencySupportAdapter
+        implements CockpitInfrastructureEmergencySupportAdapter {
+
+    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<String> CATEGORIES = Arrays.asList(
+            "EMERGENCY_RESOURCE", "HAZARD_SOURCE", "PROTECTION_TARGET");
+    private static final Set<String> OPEN_WARNING_STATUS = new HashSet<>(
+            Arrays.asList("PENDING", "PROCESSING", "HANDLING"));
+
+    private final EquipmentBaseService equipmentBaseService;
+    private final EquipmentTypeService equipmentTypeService;
+    private final HazardHiddenAccountService hazardService;
+    private final RiskAssessmentService riskService;
+    private final AlarmDataService alarmService;
+    private final EarlyWarningService warningService;
+    private final WorkOrderService workOrderService;
+
+    public BusinessCockpitInfrastructureEmergencySupportAdapter(
+            EquipmentBaseService equipmentBaseService,
+            EquipmentTypeService equipmentTypeService,
+            HazardHiddenAccountService hazardService,
+            RiskAssessmentService riskService,
+            AlarmDataService alarmService,
+            EarlyWarningService warningService,
+            WorkOrderService workOrderService) {
+        this.equipmentBaseService = equipmentBaseService;
+        this.equipmentTypeService = equipmentTypeService;
+        this.hazardService = hazardService;
+        this.riskService = riskService;
+        this.alarmService = alarmService;
+        this.warningService = warningService;
+        this.workOrderService = workOrderService;
+    }
+
+    @Override
+    public CockpitInfrastructureEmergencySupportSnapshot load() {
+        List<EquipmentBase> equipment = safeList(equipmentBaseService.list());
+        Map<Integer, EquipmentType> types = typeIndex(equipmentTypeService.list());
+        Map<String, EquipmentBase> equipmentById = new HashMap<>();
+        Map<String, EquipmentBase> equipmentByCode = new HashMap<>();
+        for (EquipmentBase item : equipment) {
+            if (item == null) {
+                continue;
+            }
+            if (!isBlank(item.getEquipmentId())) {
+                equipmentById.put(item.getEquipmentId().trim(), item);
+            }
+            if (!isBlank(item.getEquipmentCode())) {
+                equipmentByCode.put(item.getEquipmentCode().trim(), item);
+            }
+        }
+
+        List<AlarmData> alarms = safeList(alarmService.list());
+        List<EarlyWarning> warnings = safeList(warningService.list());
+        List<WorkOrder> workOrders = safeList(workOrderService.list());
+        Map<String, EventCounts> eventCountsByDevice = eventCounts(
+                alarms, warnings, workOrders, equipmentByCode, equipmentById);
+
+        List<CockpitInfrastructureEmergencySupportSnapshot.SourceObject> resources =
+                new ArrayList<>();
+        String latestUpdatedAt = null;
+        for (EquipmentBase item : equipment) {
+            if (item == null || isBlank(item.getEquipmentCode()) || item.getEquipmentTypeId() == null) {
+                continue;
+            }
+            String industry = CockpitTypeIndustry.industryOf(
+                    types.get(item.getEquipmentTypeId()), types);
+            if (industry == null) {
+                continue;
+            }
+            String code = item.getEquipmentCode().trim();
+            EventCounts counts = eventCountsByDevice.getOrDefault(code, EventCounts.EMPTY);
+            resources.add(object(
+                    code,
+                    item.getEquipmentName(),
+                    "EMERGENCY_RESOURCE",
+                    industry,
+                    item.getEquipmentLocation(),
+                    Collections.emptyList(),
+                    counts));
+            latestUpdatedAt = latest(latestUpdatedAt, format(item.getUpdateTime()));
+        }
+
+        List<CockpitInfrastructureEmergencySupportSnapshot.SourceObject> hazards =
+                new ArrayList<>();
+        for (HazardHiddenAccount hazard : safeList(hazardService.list())) {
+            if (hazard == null || isBlank(hazard.getHazardId())) {
+                continue;
+            }
+            EquipmentBase linked = isBlank(hazard.getEquipmentId())
+                    ? null : equipmentById.get(hazard.getEquipmentId().trim());
+            String industry = linked == null || linked.getEquipmentTypeId() == null
+                    ? null
+                    : CockpitTypeIndustry.industryOf(types.get(linked.getEquipmentTypeId()), types);
+            if (industry == null) {
+                continue;
+            }
+            String code = hazard.getHazardId().trim();
+            EventCounts counts = linked == null
+                    ? EventCounts.EMPTY
+                    : eventCountsByDevice.getOrDefault(
+                            trimToNull(linked.getEquipmentCode()), EventCounts.EMPTY);
+            hazards.add(object(
+                    code,
+                    firstNonBlank(hazard.getHazardDesc(), hazard.getHazardNo(), code),
+                    "HAZARD_SOURCE",
+                    industry,
+                    null,
+                    Collections.emptyList(),
+                    counts));
+            latestUpdatedAt = latest(latestUpdatedAt, format(hazard.getUpdateTime()));
+        }
+
+        List<CockpitInfrastructureEmergencySupportSnapshot.SourceObject> targets =
+                new ArrayList<>();
+        for (RiskAssessment risk : safeList(riskService.list())) {
+            if (risk == null || isBlank(risk.getRiskId())
+                    || isBlank(risk.getProtectiveTargets())) {
+                continue;
+            }
+            targets.add(object(
+                    risk.getRiskId().trim(),
+                    risk.getProtectiveTargets(),
+                    "PROTECTION_TARGET",
+                    "GAS",
+                    null,
+                    Collections.emptyList(),
+                    EventCounts.EMPTY));
+            latestUpdatedAt = latest(latestUpdatedAt, format(risk.getUpdateTime()));
+        }
+
+        List<CockpitInfrastructureEmergencySupportSnapshot.SourceSection> sections = Arrays.asList(
+                new CockpitInfrastructureEmergencySupportSnapshot.SourceSection(
+                        "EMERGENCY_RESOURCE", "READY", "READY", resources),
+                new CockpitInfrastructureEmergencySupportSnapshot.SourceSection(
+                        "HAZARD_SOURCE", "READY", "READY", hazards),
+                new CockpitInfrastructureEmergencySupportSnapshot.SourceSection(
+                        "PROTECTION_TARGET", "READY", "READY", targets));
+        return new CockpitInfrastructureEmergencySupportSnapshot(latestUpdatedAt, sections);
+    }
+
+    private static Map<String, EventCounts> eventCounts(
+            List<AlarmData> alarms,
+            List<EarlyWarning> warnings,
+            List<WorkOrder> workOrders,
+            Map<String, EquipmentBase> equipmentByCode,
+            Map<String, EquipmentBase> equipmentById) {
+        Map<String, EventCounts> counts = new LinkedHashMap<>();
+        for (AlarmData alarm : alarms) {
+            if (alarm == null || isBlank(alarm.getDeviceCode()) || isClosedAlarm(alarm)) {
+                continue;
+            }
+            String code = alarm.getDeviceCode().trim();
+            counts.computeIfAbsent(code, ignored -> new EventCounts()).alarmCount++;
+        }
+        for (EarlyWarning warning : warnings) {
+            if (warning == null || isBlank(warning.getDeviceCode())
+                    || !isOpenWarning(warning.getStatus())) {
+                continue;
+            }
+            String code = warning.getDeviceCode().trim();
+            counts.computeIfAbsent(code, ignored -> new EventCounts()).warningCount++;
+        }
+        for (WorkOrder order : workOrders) {
+            if (order == null || isBlank(order.getDeviceId())
+                    || !isOpenWorkOrder(order.getOrderStatus())) {
+                continue;
+            }
+            EquipmentBase device = equipmentById.get(order.getDeviceId().trim());
+            if (device == null || isBlank(device.getEquipmentCode())) {
+                continue;
+            }
+            counts.computeIfAbsent(device.getEquipmentCode().trim(), ignored -> new EventCounts())
+                    .workOrderCount++;
+        }
+        return counts;
+    }
+
+    private static CockpitInfrastructureEmergencySupportSnapshot.SourceObject object(
+            String code,
+            String name,
+            String category,
+            String industry,
+            String area,
+            List<String> pointCodes,
+            EventCounts counts) {
+        return new CockpitInfrastructureEmergencySupportSnapshot.SourceObject(
+                code,
+                firstNonBlank(name, code),
+                category,
+                industry,
+                trimToNull(area),
+                pointCodes,
+                new CockpitInfrastructureEmergencySupportSnapshot.SourceEventAssociation(
+                        counts.warningCount, counts.alarmCount, counts.workOrderCount));
+    }
+
+    private static boolean isClosedAlarm(AlarmData alarm) {
+        return Integer.valueOf(1).equals(alarm.getAlarmStatus())
+                || Integer.valueOf(2).equals(alarm.getAuditStatus());
+    }
+
+    private static boolean isOpenWarning(String status) {
+        return status != null && OPEN_WARNING_STATUS.contains(status.trim().toUpperCase());
+    }
+
+    private static boolean isOpenWorkOrder(Integer status) {
+        return status != null && ((status >= 0 && status <= 4) || status == 6);
+    }
+
+    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 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 String firstNonBlank(String... values) {
+        for (String value : values) {
+            String normalized = trimToNull(value);
+            if (normalized != null) {
+                return normalized;
+            }
+        }
+        return null;
+    }
+
+    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();
+    }
+
+    private static final class EventCounts {
+        private static final EventCounts EMPTY = new EventCounts();
+        private int warningCount;
+        private int alarmCount;
+        private int workOrderCount;
+    }
+}