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