fix(firstleg): avoid phase-three reads in phase-two queries

This commit is contained in:
2026-08-08 12:25:00 +08:00
parent d30e975eed
commit 44bae99999
2 changed files with 55 additions and 7 deletions
@@ -57,20 +57,30 @@ public class UnifiedInboundQueryService {
InboundOrderEntity inbound = inbounds.findByOrderId(tenantId, orderId);
if (inbound == null) return null;
InboundOrderDetail detail = inbounds.detail(tenantId, inbound);
String mainStatus = mainStatus(inbound.getProcessStatus());
boolean includePhaseThree = requiresPhaseThreeSummary(mainStatus);
List<UnifiedInboundDetail.TimelineEvent> timeline = detail.statusHistory().stream()
.map(item -> new UnifiedInboundDetail.TimelineEvent(
"FIRSTLEG", "STATUS", item.fromStatus(), item.toStatus(), item.action(), item.reason(),
item.operatorId(), item.operatorName(), item.occurredAt()))
.toList();
return new UnifiedInboundDetail(summary(inbound, detail,
operations.findPreload(tenantId, inbound.getId()), operations.findPick(tenantId, inbound.getId()),
operations.findEvidence(tenantId, inbound.getId()), deliveryAt(detail.statusHistory()),
operations.findTransport(tenantId, inbound.getId())), timeline);
// Phase-three tables are optional during the phase-two measurement workflow.
com.baiyue.firstleg.split.entity.PreloadPlanEntity preload = null;
com.baiyue.firstleg.loading.entity.InboundLoadingPickTaskEntity pick = null;
List<LoadingEvidenceEntity> evidence = List.of();
LocalDateTime deliveryAt = null;
if (includePhaseThree) {
preload = operations.findPreload(tenantId, inbound.getId());
pick = operations.findPick(tenantId, inbound.getId());
evidence = operations.findEvidence(tenantId, inbound.getId());
deliveryAt = deliveryAt(detail.statusHistory());
}
InboundTransportSnapshotEntity transport = includePhaseThree
? operations.findTransport(tenantId, inbound.getId()) : null;
return new UnifiedInboundDetail(summary(inbound, detail, preload, pick, evidence, deliveryAt, transport), timeline);
}
private List<UnifiedInboundSummary> summaries(List<InboundOrderEntity> records) {
Map<Long, Long> owners = records.stream().collect(Collectors.toMap(
InboundOrderEntity::getId, InboundOrderEntity::getTenantId, (left, right) -> left));
Map<Long, Long> phaseThreeOwners = records.stream()
.filter(inbound -> requiresPhaseThreeSummary(mainStatus(inbound.getProcessStatus())))
.collect(Collectors.toMap(
@@ -79,7 +89,7 @@ public class UnifiedInboundQueryService {
var picks = operations.findPicks(phaseThreeOwners);
var evidences = operations.findEvidence(phaseThreeOwners);
var deliveries = operations.findDeliveryHistory(phaseThreeOwners);
var transports = operations.findTransports(owners);
var transports = operations.findTransports(phaseThreeOwners);
return records.stream().map(inbound -> summary(inbound, null, preloads.get(inbound.getId()),
picks.get(inbound.getId()), evidences.getOrDefault(inbound.getId(), List.of()),
deliveries.get(inbound.getId()) == null ? null : deliveries.get(inbound.getId()).getOccurredAt(),