|
|
@@ -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();
|
|
|
+ }
|
|
|
+}
|