|
@@ -19,8 +19,10 @@ import java.time.format.DateTimeFormatter;
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
import java.util.Arrays;
|
|
|
import java.util.Collections;
|
|
import java.util.Collections;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.LinkedHashSet;
|
|
import java.util.LinkedHashSet;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
import java.util.Objects;
|
|
import java.util.Objects;
|
|
|
import java.util.Set;
|
|
import java.util.Set;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
@@ -30,6 +32,10 @@ import java.util.stream.Collectors;
|
|
|
*
|
|
*
|
|
|
* <p>只读、不新增表、不加投影;任何一项数据缺失都按保守口径降级(缺状态=离线),
|
|
* <p>只读、不新增表、不加投影;任何一项数据缺失都按保守口径降级(缺状态=离线),
|
|
|
* 不为了「看起来正常」而伪造在线/无警状态。
|
|
* 不为了「看起来正常」而伪造在线/无警状态。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>批量入口 {@link #prepareBatch(List)} 把状态/预警/工单/报警各**一次查询**读进线程内索引,
|
|
|
|
|
+ * 随后同线程里的 {@link #resolve(EquipmentBase, String)} 直接查内存;没有批量上下文时仍走原单设备
|
|
|
|
|
+ * 查询路径,保证单测与零散调用的行为不变。适配器必须在 finally 里调 {@link #clearBatch()}。
|
|
|
*/
|
|
*/
|
|
|
@Component
|
|
@Component
|
|
|
public class BusinessCockpitDeviceFactResolver {
|
|
public class BusinessCockpitDeviceFactResolver {
|
|
@@ -49,6 +55,9 @@ public class BusinessCockpitDeviceFactResolver {
|
|
|
private final WorkOrderService workOrderService;
|
|
private final WorkOrderService workOrderService;
|
|
|
private final AlarmDataService alarmDataService;
|
|
private final AlarmDataService alarmDataService;
|
|
|
|
|
|
|
|
|
|
+ /** 一次适配器调用内的批量事实;用 ThreadLocal 隔离并发请求。 */
|
|
|
|
|
+ private final ThreadLocal<BatchFacts> batchFacts = new ThreadLocal<>();
|
|
|
|
|
+
|
|
|
public BusinessCockpitDeviceFactResolver(
|
|
public BusinessCockpitDeviceFactResolver(
|
|
|
EquipmentStatusService equipmentStatusService,
|
|
EquipmentStatusService equipmentStatusService,
|
|
|
EarlyWarningService earlyWarningService,
|
|
EarlyWarningService earlyWarningService,
|
|
@@ -60,7 +69,113 @@ public class BusinessCockpitDeviceFactResolver {
|
|
|
this.alarmDataService = alarmDataService;
|
|
this.alarmDataService = alarmDataService;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /** 一次批量取数的内存索引:四类表各查一次,循环里不再回数据库。 */
|
|
|
|
|
+ private static final class BatchFacts {
|
|
|
|
|
+ private final Map<String, EquipmentStatus> statusByDeviceCode = new HashMap<>();
|
|
|
|
|
+ private final Set<String> openWarningDeviceCodes = new LinkedHashSet<>();
|
|
|
|
|
+ private final Map<String, Boolean> openOrderByDeviceId = new HashMap<>();
|
|
|
|
|
+ private final Map<Long, Boolean> openOrderById = new HashMap<>();
|
|
|
|
|
+ private final Map<String, List<AlarmData>> unhandledAlarmsByDeviceCode = new HashMap<>();
|
|
|
|
|
+
|
|
|
|
|
+ private boolean alarmsCovered(String deviceCode) {
|
|
|
|
|
+ List<AlarmData> unhandled = unhandledAlarmsByDeviceCode.get(deviceCode);
|
|
|
|
|
+ if (unhandled == null || unhandled.isEmpty()) return false;
|
|
|
|
|
+ Set<Long> orderIds = unhandled.stream()
|
|
|
|
|
+ .map(AlarmData::getWorkOrderId)
|
|
|
|
|
+ .filter(Objects::nonNull)
|
|
|
|
|
+ .collect(Collectors.toCollection(LinkedHashSet::new));
|
|
|
|
|
+ if (orderIds.size() != unhandled.size()) return false;
|
|
|
|
|
+ return orderIds.stream().allMatch(id -> Boolean.TRUE.equals(openOrderById.get(id)));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 为随后同一线程里的批量解析预载事实。四类数据各自最多一条 SQL;
|
|
|
|
|
+ * 任一类读取失败只让该类索引为空(保守降级),不影响其它类,也不抛给适配器。
|
|
|
|
|
+ */
|
|
|
|
|
+ public void prepareBatch(List<EquipmentBase> devices) {
|
|
|
|
|
+ BatchFacts facts = new BatchFacts();
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, EquipmentStatus> statusById = new HashMap<>();
|
|
|
|
|
+ try {
|
|
|
|
|
+ for (EquipmentStatus status : safeList(equipmentStatusService.list())) {
|
|
|
|
|
+ if (status != null && status.getEquipmentId() != null) {
|
|
|
|
|
+ statusById.putIfAbsent(status.getEquipmentId(), status);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception ignored) {
|
|
|
|
|
+ // 状态不可得:下面按「无状态」降级
|
|
|
|
|
+ }
|
|
|
|
|
+ for (EquipmentBase device : safeList(devices)) {
|
|
|
|
|
+ if (device == null || isBlank(device.getEquipmentCode())) continue;
|
|
|
|
|
+ facts.statusByDeviceCode.put(device.getEquipmentCode().trim(),
|
|
|
|
|
+ statusById.get(device.getEquipmentId()));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ for (EarlyWarning warning : safeList(earlyWarningService.list())) {
|
|
|
|
|
+ if (warning == null || isBlank(warning.getDeviceCode())) continue;
|
|
|
|
|
+ if (warning.getStatus() != null && OPEN_WARNING_STATUS.contains(warning.getStatus().trim().toUpperCase())) {
|
|
|
|
|
+ facts.openWarningDeviceCodes.add(warning.getDeviceCode().trim());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception ignored) {
|
|
|
|
|
+ // 预警不可得:按「无未解决预警」降级
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ for (WorkOrder order : safeList(workOrderService.list())) {
|
|
|
|
|
+ if (order == null || !isOpenOrderStatus(order.getOrderStatus())) continue;
|
|
|
|
|
+ if (order.getOrderId() != null) facts.openOrderById.put(order.getOrderId(), Boolean.TRUE);
|
|
|
|
|
+ if (!isBlank(order.getDeviceId())) facts.openOrderByDeviceId.put(order.getDeviceId(), Boolean.TRUE);
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception ignored) {
|
|
|
|
|
+ // 工单不可得:按「无在途工单」降级
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ for (AlarmData alarm : safeList(alarmDataService.list(new LambdaQueryWrapper<AlarmData>()
|
|
|
|
|
+ .eq(AlarmData::getAlarmStatus, UNHANDLED_ALARM_STATUS)))) {
|
|
|
|
|
+ if (alarm == null || isBlank(alarm.getDeviceCode())) continue;
|
|
|
|
|
+ facts.unhandledAlarmsByDeviceCode
|
|
|
|
|
+ .computeIfAbsent(alarm.getDeviceCode(), key -> new ArrayList<>())
|
|
|
|
|
+ .add(alarm);
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception ignored) {
|
|
|
|
|
+ // 报警不可得:按「无未处理报警」降级
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ batchFacts.set(facts);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 清掉本线程的批量上下文;适配器必须在 finally 调用,防止跨请求复用。 */
|
|
|
|
|
+ public void clearBatch() {
|
|
|
|
|
+ batchFacts.remove();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 单设备状态读取:批量上下文中直接读索引,否则回退原按编码查询。
|
|
|
|
|
+ * 供原本直接调 {@code equipmentStatusService.getByDeviceCode} 的适配器复用。
|
|
|
|
|
+ */
|
|
|
|
|
+ public EquipmentStatus statusFor(String deviceCode) {
|
|
|
|
|
+ BatchFacts facts = batchFacts.get();
|
|
|
|
|
+ if (facts != null && !isBlank(deviceCode) && facts.statusByDeviceCode.containsKey(deviceCode.trim())) {
|
|
|
|
|
+ return facts.statusByDeviceCode.get(deviceCode.trim());
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ return equipmentStatusService.getByDeviceCode(deviceCode);
|
|
|
|
|
+ } catch (Exception ignored) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public CockpitMonitoringPointDeviceSnapshot resolve(EquipmentBase device, String deviceTypeName) {
|
|
public CockpitMonitoringPointDeviceSnapshot resolve(EquipmentBase device, String deviceTypeName) {
|
|
|
|
|
+ BatchFacts facts = batchFacts.get();
|
|
|
|
|
+ return facts == null ? resolveSingle(device, deviceTypeName) : resolveFromBatch(device, deviceTypeName, facts);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 原单设备路径:没有批量上下文时使用(零散调用与既有单测)。 */
|
|
|
|
|
+ private CockpitMonitoringPointDeviceSnapshot resolveSingle(EquipmentBase device, String deviceTypeName) {
|
|
|
String deviceCode = device.getEquipmentCode().trim();
|
|
String deviceCode = device.getEquipmentCode().trim();
|
|
|
EquipmentStatus status = null;
|
|
EquipmentStatus status = null;
|
|
|
try {
|
|
try {
|
|
@@ -68,38 +183,56 @@ public class BusinessCockpitDeviceFactResolver {
|
|
|
} catch (Exception ignored) {
|
|
} catch (Exception ignored) {
|
|
|
// 状态缺失按离线处理,不阻断整张图
|
|
// 状态缺失按离线处理,不阻断整张图
|
|
|
}
|
|
}
|
|
|
|
|
+ return snapshot(device, deviceTypeName, status,
|
|
|
|
|
+ hasOpenWarning(deviceCode),
|
|
|
|
|
+ hasOpenWorkOrder(device.getEquipmentId()),
|
|
|
|
|
+ alarmsCoveredByOpenWorkOrders(deviceCode));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 批量路径:状态/预警/工单/报警都从 prepareBatch 建好的索引里读。 */
|
|
|
|
|
+ private CockpitMonitoringPointDeviceSnapshot resolveFromBatch(
|
|
|
|
|
+ EquipmentBase device, String deviceTypeName, BatchFacts facts) {
|
|
|
|
|
+ String deviceCode = device.getEquipmentCode().trim();
|
|
|
|
|
+ EquipmentStatus status = facts.statusByDeviceCode.containsKey(deviceCode)
|
|
|
|
|
+ ? facts.statusByDeviceCode.get(deviceCode)
|
|
|
|
|
+ : statusFor(deviceCode);
|
|
|
|
|
+ String deviceId = device.getEquipmentId();
|
|
|
|
|
+ boolean openOrder = !isBlank(deviceId) && Boolean.TRUE.equals(facts.openOrderByDeviceId.get(deviceId));
|
|
|
|
|
+ return snapshot(device, deviceTypeName, status,
|
|
|
|
|
+ facts.openWarningDeviceCodes.contains(deviceCode),
|
|
|
|
|
+ openOrder,
|
|
|
|
|
+ facts.alarmsCovered(deviceCode));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private CockpitMonitoringPointDeviceSnapshot snapshot(EquipmentBase device, String deviceTypeName,
|
|
|
|
|
+ EquipmentStatus status, boolean warning, boolean openOrder, boolean alarmsCovered) {
|
|
|
|
|
+ String deviceCode = device.getEquipmentCode().trim();
|
|
|
boolean online = status != null && Integer.valueOf(1).equals(status.getOnlineStatus());
|
|
boolean online = status != null && Integer.valueOf(1).equals(status.getOnlineStatus());
|
|
|
boolean alarm = status != null && Integer.valueOf(1).equals(status.getAlarmStatus());
|
|
boolean alarm = status != null && Integer.valueOf(1).equals(status.getAlarmStatus());
|
|
|
|
|
|
|
|
List<String> reasons = new ArrayList<>();
|
|
List<String> reasons = new ArrayList<>();
|
|
|
- if (alarm) {
|
|
|
|
|
- reasons.add(deviceCode + " 报警");
|
|
|
|
|
- }
|
|
|
|
|
- if (!online && status != null) {
|
|
|
|
|
- reasons.add(deviceCode + " 离线");
|
|
|
|
|
- }
|
|
|
|
|
- if (status == null) {
|
|
|
|
|
- reasons.add(deviceCode + " 无状态记录");
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (alarm) reasons.add(deviceCode + " 报警");
|
|
|
|
|
+ if (!online && status != null) reasons.add(deviceCode + " 离线");
|
|
|
|
|
+ if (status == null) reasons.add(deviceCode + " 无状态记录");
|
|
|
|
|
|
|
|
return new CockpitMonitoringPointDeviceSnapshot(
|
|
return new CockpitMonitoringPointDeviceSnapshot(
|
|
|
deviceCode,
|
|
deviceCode,
|
|
|
device.getEquipmentName(),
|
|
device.getEquipmentName(),
|
|
|
deviceTypeName,
|
|
deviceTypeName,
|
|
|
- device.getLongitude(),
|
|
|
|
|
- device.getLatitude(),
|
|
|
|
|
- CockpitCoordinateValidator.validate(device.getLongitude(), device.getLatitude(), false),
|
|
|
|
|
|
|
+ device.getLongitude(),
|
|
|
|
|
+ device.getLatitude(),
|
|
|
|
|
+ CockpitCoordinateValidator.validate(device.getLongitude(), device.getLatitude(), false),
|
|
|
online,
|
|
online,
|
|
|
alarm,
|
|
alarm,
|
|
|
- hasOpenWarning(deviceCode),
|
|
|
|
|
- hasOpenWorkOrder(device.getEquipmentId()),
|
|
|
|
|
|
|
+ warning,
|
|
|
|
|
+ openOrder,
|
|
|
reasons,
|
|
reasons,
|
|
|
status == null || status.getStatusUpdateTime() == null
|
|
status == null || status.getStatusUpdateTime() == null
|
|
|
? null
|
|
? null
|
|
|
: TIME_FORMATTER.format(status.getStatusUpdateTime()
|
|
: TIME_FORMATTER.format(status.getStatusUpdateTime()
|
|
|
.atZone(BUSINESS_ZONE).toOffsetDateTime()),
|
|
.atZone(BUSINESS_ZONE).toOffsetDateTime()),
|
|
|
status != null,
|
|
status != null,
|
|
|
- alarmsCoveredByOpenWorkOrders(deviceCode));
|
|
|
|
|
|
|
+ alarmsCovered);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -168,23 +301,18 @@ public class BusinessCockpitDeviceFactResolver {
|
|
|
* 与 cockpit 冻结的 1-based 七态**不同值域**;此处只判「是否在途」,不冒充冻结字典。
|
|
* 与 cockpit 冻结的 1-based 七态**不同值域**;此处只判「是否在途」,不冒充冻结字典。
|
|
|
*/
|
|
*/
|
|
|
private boolean hasOpenWorkOrder(String deviceId) {
|
|
private boolean hasOpenWorkOrder(String deviceId) {
|
|
|
- if (deviceId == null || deviceId.trim().isEmpty()) {
|
|
|
|
|
- return false;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (deviceId == null || deviceId.trim().isEmpty()) return false;
|
|
|
try {
|
|
try {
|
|
|
List<WorkOrder> orders = safeList(workOrderService.list(
|
|
List<WorkOrder> orders = safeList(workOrderService.list(
|
|
|
new QueryWrapper<WorkOrder>().eq("device_id", deviceId)));
|
|
new QueryWrapper<WorkOrder>().eq("device_id", deviceId)));
|
|
|
- return orders.stream()
|
|
|
|
|
- .anyMatch(order -> order != null && isOpenOrderStatus(order.getOrderStatus()));
|
|
|
|
|
|
|
+ return orders.stream().anyMatch(order -> order != null && isOpenOrderStatus(order.getOrderStatus()));
|
|
|
} catch (Exception ignored) {
|
|
} catch (Exception ignored) {
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private static boolean isOpenOrderStatus(Integer orderStatus) {
|
|
private static boolean isOpenOrderStatus(Integer orderStatus) {
|
|
|
- if (orderStatus == null) {
|
|
|
|
|
- return false;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (orderStatus == null) return false;
|
|
|
// 0-based:0 待派单、1 待接单、2 已接单、3 处理中、4 待验收、6 延期中 均为在途;5 已结案、7 已取消为终态。
|
|
// 0-based:0 待派单、1 待接单、2 已接单、3 处理中、4 待验收、6 延期中 均为在途;5 已结案、7 已取消为终态。
|
|
|
return (orderStatus >= 0 && orderStatus <= 4) || orderStatus == 6;
|
|
return (orderStatus >= 0 && orderStatus <= 4) || orderStatus == 6;
|
|
|
}
|
|
}
|