This commit is contained in:
2026-09-29 18:06:18 +08:00
parent 54ad26c607
commit 2c60a29e0f
30 changed files with 384 additions and 139 deletions
@@ -55,23 +55,23 @@ public class LoadingCompletedConsumer {
envelope.requireValid();
if (!EventTypes.FIRST_LEG_LOADING_PLAN_COMPLETED.equals(envelope.eventType())
|| (envelope.eventVersion() != 1 && envelope.eventVersion() != 2 && envelope.eventVersion() != 3))
throw new IllegalArgumentException("不支持的尾程交接事件");
throw new EventProtocolException("不支持的尾程交接事件");
if (envelope.sourceRegion() != RegionCode.CN || envelope.targetRegion() != RegionCode.OVERSEAS)
throw new IllegalArgumentException("尾程交接事件区域无效");
throw new EventProtocolException("尾程交接事件区域无效");
if (!"LOADING_PLAN".equals(envelope.aggregateType()))
throw new IllegalArgumentException("尾程交接事件聚合类型无效");
throw new EventProtocolException("尾程交接事件聚合类型无效");
if (!EventPayloadHash.matches(EventPayloadCanonicalizer.canonicalize(envelope.payload()),
envelope.payloadHash())) throw new IllegalArgumentException("payloadHash 与尾程事件负载不一致");
envelope.payloadHash())) throw new EventProtocolException("payloadHash 与尾程事件负载不一致");
FirstLegLoadingPlanCompletedV1 payload = parsePayload(envelope);
String containerNo = ContainerNumbers.requireCanonical(payload.containerNo());
String sourceContainerBookingId = sourceContainerBookingId(envelope, payload);
if(payload.orders()==null||payload.orders().isEmpty()) throw new IllegalArgumentException("事件没有入仓单");
if(payload.orders()==null||payload.orders().isEmpty()) throw new EventProtocolException("事件没有入仓单");
OwnerSnapshot owner = requireOwnerSnapshot(payload.orders(), envelope.tenantId());
if (envelope.eventVersion() >= 2) validateCartonSkuMappings(payload, envelope.payload());
freezeContainerIdentity(envelope, sourceContainerBookingId, containerNo);
LastlegEventInbox existing=inboxMapper.selectById(envelope.eventId());
if(existing!=null) {
if(!existing.getPayloadHash().equalsIgnoreCase(envelope.payloadHash())) throw new IllegalArgumentException("事件摘要冲突");
if(!existing.getPayloadHash().equalsIgnoreCase(envelope.payloadHash())) throw new EventProtocolException("事件摘要冲突");
if (!"PROCESSED".equals(existing.getStatus())) throw new IllegalStateException("事件尚未完成消费");
return DeliveryAck.of(envelope, DeliveryAck.Outcome.DUPLICATE);
}
@@ -84,7 +84,7 @@ public class LoadingCompletedConsumer {
LastlegEventInbox raced = inboxMapper.selectById(envelope.eventId());
if (raced == null || raced.getPayloadHash() == null
|| !raced.getPayloadHash().equalsIgnoreCase(envelope.payloadHash())) {
throw new IllegalArgumentException("事件摘要冲突", exception);
throw new EventProtocolException("事件摘要冲突", exception);
}
if (!"PROCESSED".equals(raced.getStatus())) throw new IllegalStateException("事件尚未完成消费");
return DeliveryAck.of(envelope, DeliveryAck.Outcome.DUPLICATE);
@@ -111,7 +111,7 @@ public class LoadingCompletedConsumer {
if (envelope.eventVersion() < 3) return;
JsonNode values = envelope.payload().path("loadingEvidence");
if (!values.isArray() || values.size() != 5) {
throw new IllegalArgumentException("V3 尾程交接事件必须包含五类国内装柜照片");
throw new EventProtocolException("V3 尾程交接事件必须包含五类国内装柜照片");
}
Set<String> expected = Set.of("PHOTO_STEP_1", "PHOTO_STEP_2", "PHOTO_STEP_3", "PHOTO_STEP_4", "SEAL");
Set<String> actual = new HashSet<>();
@@ -121,7 +121,7 @@ public class LoadingCompletedConsumer {
String fileName = value.path("fileName").asText("").trim();
if (!expected.contains(type) || !actual.add(type) || fileId <= 0
|| fileName.isBlank() || fileName.length() > 255) {
throw new IllegalArgumentException("V3 尾程交接事件的国内装柜照片引用无效");
throw new EventProtocolException("V3 尾程交接事件的国内装柜照片引用无效");
}
ContainerLoadingEvidence existing = loadingEvidence.find(sourceContainerBookingId, type);
if (existing != null) {
@@ -143,7 +143,7 @@ public class LoadingCompletedConsumer {
|| !Objects.equals(raced.getFileName(), fileName)) throw duplicate;
}
}
if (!actual.equals(expected)) throw new IllegalArgumentException("V3 尾程交接事件的国内装柜照片类型不完整");
if (!actual.equals(expected)) throw new EventProtocolException("V3 尾程交接事件的国内装柜照片类型不完整");
}
private FirstLegLoadingPlanCompletedV1 parsePayload(EventEnvelope envelope) {
@@ -154,7 +154,7 @@ public class LoadingCompletedConsumer {
// identity is validated separately from the original immutable payload.
return compatibleMapper.treeToValue(envelope.payload(), FirstLegLoadingPlanCompletedV1.class);
}
catch(Exception exception) { throw new IllegalArgumentException("尾程事件负载无效",exception); }
catch(Exception exception) { throw new EventProtocolException("尾程事件负载无效",exception); }
}
private int countOrders(EventEnvelope envelope,String containerNo) {
@@ -184,7 +184,7 @@ public class LoadingCompletedConsumer {
? envelope.payload().path("sourceContainerBookingId").asText(null)
: payload.containerBookingId();
if (value == null || value.isBlank() || value.trim().length() > 128) {
throw new IllegalArgumentException("事件缺少稳定来源柜预订 ID sourceContainerBookingId");
throw new EventProtocolException("事件缺少稳定来源柜预订 ID sourceContainerBookingId");
}
return value.trim();
}
@@ -194,12 +194,12 @@ public class LoadingCompletedConsumer {
String customerId = requiredOwnerValue(orders.get(0).customerId(), "customerId");
String customerName = requiredOwnerValue(orders.get(0).customerName(), "customerName");
if (!trustedCustomerId.equals(customerId)) {
throw new IllegalArgumentException("Order customerId does not match the trusted event customer");
throw new EventProtocolException("Order customerId does not match the trusted event customer");
}
for (FirstLegLoadingPlanCompletedV1.Order order : orders) {
if (!customerId.equals(requiredOwnerValue(order.customerId(), "customerId"))
|| !customerName.equals(requiredOwnerValue(order.customerName(), "customerName"))) {
throw new IllegalArgumentException("同一装柜完成事件的货主快照不一致");
throw new EventProtocolException("同一装柜完成事件的货主快照不一致");
}
}
return new OwnerSnapshot(customerId, customerName);
@@ -207,11 +207,11 @@ public class LoadingCompletedConsumer {
private String requiredOwnerValue(String value, String field) {
if (value == null || value.isBlank()) {
throw new IllegalArgumentException("尾程事件缺少不可变货主快照 " + field);
throw new EventProtocolException("尾程事件缺少不可变货主快照 " + field);
}
String normalized = value.trim();
if (normalized.length() > 128) {
throw new IllegalArgumentException("尾程事件货主快照过长 " + field);
throw new EventProtocolException("尾程事件货主快照过长 " + field);
}
return normalized;
}
@@ -231,11 +231,11 @@ public class LoadingCompletedConsumer {
}
int manifestPackages = rawPallets.path(palletIndex).path("manifestPackageCount").asInt(-1);
if (palletCartonMarks.size() != manifestPackages) {
throw new IllegalArgumentException("一件代发托盘物理箱数与清单箱数不一致: " + pallet.palletNo());
throw new EventProtocolException("一件代发托盘物理箱数与清单箱数不一致: " + pallet.palletNo());
}
for (String mark : palletCartonMarks) {
if (!orderCartonMarks.add(mark)) {
throw new IllegalArgumentException("一件代发物理箱不能跨托盘重复: " + mark);
throw new EventProtocolException("一件代发物理箱不能跨托盘重复: " + mark);
}
}
}
@@ -257,7 +257,7 @@ public class LoadingCompletedConsumer {
Set<String> orderCartonMarks) {
List<String> marks = cartonMarks == null ? List.of() : cartonMarks;
if (skuCode == null || skuCode.isBlank()) {
throw new IllegalArgumentException("V2 尾程交接事件缺少 SKU");
throw new EventProtocolException("V2 尾程交接事件缺少 SKU");
}
if (marks.isEmpty()) return;
requireSkuCartonMapping(skuCode, loadedPackages, marks, orderCartonMarks);
@@ -266,15 +266,15 @@ public class LoadingCompletedConsumer {
private void requireSkuCartonMapping(String skuCode, int loadedPackages, List<String> cartonMarks,
Set<String> orderCartonMarks) {
if (skuCode == null || skuCode.isBlank()) {
throw new IllegalArgumentException("V2 尾程交接事件的箱唛映射缺少 SKU");
throw new EventProtocolException("V2 尾程交接事件的箱唛映射缺少 SKU");
}
List<String> marks = cartonMarks == null ? List.of() : cartonMarks;
if (marks.size() != loadedPackages) {
throw new IllegalArgumentException("V2 尾程交接事件的 SKU 箱唛数量必须等于实装箱数: " + skuCode);
throw new EventProtocolException("V2 尾程交接事件的 SKU 箱唛数量必须等于实装箱数: " + skuCode);
}
for (String mark : marks) {
if (mark == null || mark.isBlank() || !orderCartonMarks.add(mark)) {
throw new IllegalArgumentException("V2 尾程交接事件包含空值或重复箱唛: " + skuCode);
throw new EventProtocolException("V2 尾程交接事件包含空值或重复箱唛: " + skuCode);
}
}
}
@@ -298,7 +298,7 @@ public class LoadingCompletedConsumer {
List<DropshipRequests.Pallet> dropshipPallets = "ONE_CLICK_DROPSHIP".equals(orderType)
? pallets(source, rawSource) : List.of();
if ("ONE_CLICK_DROPSHIP".equals(orderType) && dropshipPallets.isEmpty()) {
throw new IllegalArgumentException("One-click dropship event has no pallet snapshot");
throw new EventProtocolException("One-click dropship event has no pallet snapshot");
}
order.setId(IdWorker.getId()); order.setOrderNo(orderNo); order.setCustomerId(customerId); order.setOrderType(orderType);
order.setDestinationType(destinationType);
@@ -346,12 +346,12 @@ public class LoadingCompletedConsumer {
for (int palletIndex = 0; palletIndex < pallets.size(); palletIndex++) {
String palletNo = pallets.get(palletIndex).palletNo();
if (palletNo == null || palletNo.isBlank() || palletNo.length() > 128 || !palletNos.add(palletNo)) {
throw new IllegalArgumentException("散货来源托盘码为空、重复或过长");
throw new EventProtocolException("散货来源托盘码为空、重复或过长");
}
sourcePallets.insert(IdWorker.getId(), customerId, order.getId(), palletNo.trim(), palletIndex + 1, event.eventId());
}
if (!pallets.isEmpty() && order.getPalletCount() > 0 && pallets.size() != order.getPalletCount()) {
throw new IllegalArgumentException("散货来源托盘码数量与托盘数不一致");
throw new EventProtocolException("散货来源托盘码数量与托盘数不一致");
}
}
for(var item:items) {
@@ -379,7 +379,7 @@ public class LoadingCompletedConsumer {
if ("ONE_CLICK_DROPSHIP".equals(orderType)) {
long warehouseId;
try { warehouseId = Long.parseLong(payload.warehouseId()); }
catch (Exception exception) { throw new IllegalArgumentException("One-click dropship warehouseId is invalid", exception); }
catch (Exception exception) { throw new EventProtocolException("One-click dropship warehouseId is invalid", exception); }
dropshipOrders.receive(new DropshipRequests.Receive(customerId, order.getId(), warehouseId,
event.eventId() + ":DROPSHIP:" + source.sourceInboundId(), dropshipPallets));
}
@@ -390,7 +390,7 @@ public class LoadingCompletedConsumer {
try {
return LocalDateTime.parse(value.trim());
} catch (java.time.format.DateTimeParseException exception) {
throw new IllegalArgumentException("事件计划到仓时间 estimatedWarehouseArrivalAt 无效", exception);
throw new EventProtocolException("事件计划到仓时间 estimatedWarehouseArrivalAt 无效", exception);
}
}
@@ -447,7 +447,7 @@ public class LoadingCompletedConsumer {
String detailId = sku.path("sourceInboundDetailId").asText(null);
var item = itemsByDetail.get(detailId);
if (item == null || !Objects.equals(item.skuCode(), sku.path("skuCode").asText(null))) {
throw new IllegalArgumentException("一件代发托盘来源明细缺失或与 SKU 不一致");
throw new EventProtocolException("一件代发托盘来源明细缺失或与 SKU 不一致");
}
List<String> marks = marksByDetail.computeIfAbsent(detailId, ignored -> new java.util.ArrayList<>());
sku.path("cartonMarks").forEach(mark -> marks.add(mark.asText()));
@@ -455,7 +455,7 @@ public class LoadingCompletedConsumer {
}
for (var item : itemsByDetail.values()) {
if (marksByDetail.getOrDefault(item.sourceInboundDetailId(), List.of()).size() != item.packageCount()) {
throw new IllegalArgumentException("一件代发托盘箱唛与来源明细箱数不一致");
throw new EventProtocolException("一件代发托盘箱唛与来源明细箱数不一致");
}
}
return marksByDetail;
@@ -466,7 +466,7 @@ public class LoadingCompletedConsumer {
Map<String, List<FirstLegLoadingPlanCompletedV1.Item>> byDetail = new java.util.LinkedHashMap<>();
for (var item : source == null ? List.<FirstLegLoadingPlanCompletedV1.Item>of() : source) {
if (item.sourceInboundDetailId() == null || item.sourceInboundDetailId().isBlank()) {
throw new IllegalArgumentException("一件代发来源明细标识缺失");
throw new EventProtocolException("一件代发来源明细标识缺失");
}
byDetail.computeIfAbsent(item.sourceInboundDetailId(), ignored -> new ArrayList<>()).add(item);
}
@@ -476,7 +476,7 @@ public class LoadingCompletedConsumer {
if (fragments.stream().anyMatch(item -> !Objects.equals(first.skuCode(), item.skuCode())
|| !Objects.equals(first.destinationWarehouseId(), item.destinationWarehouseId())
|| !Objects.equals(first.destinationWarehouseCode(), item.destinationWarehouseCode()))) {
throw new IllegalArgumentException("同一来源明细的尾程事件分片快照不一致");
throw new EventProtocolException("同一来源明细的尾程事件分片快照不一致");
}
List<String> marks = fragments.stream().flatMap(item -> item.cartonMarks().stream()).toList();
result.add(new FirstLegLoadingPlanCompletedV1.Item(first.sourceInboundDetailId(), first.skuCode(),
@@ -512,7 +512,7 @@ public class LoadingCompletedConsumer {
List<String> marks = sourceMarks == null ? List.of() : sourceMarks;
if (marks.isEmpty()) return;
if (marks.stream().anyMatch(mark -> mark == null || mark.isBlank()) || marks.stream().distinct().count() != marks.size()) {
throw new IllegalArgumentException("装柜事件的箱唛池不可包含空值或重复箱唛");
throw new EventProtocolException("装柜事件的箱唛池不可包含空值或重复箱唛");
}
for (String mark : marks) {
InboundPlanCarton carton = new InboundPlanCarton();
@@ -536,18 +536,18 @@ public class LoadingCompletedConsumer {
boolean overseasWarehouseIdentity = hasText(payload.warehouseId())
&& Objects.equals(payload.warehouseId(), order.destinationWarehouseId());
if (fbaEvidence && overseasWarehouseIdentity) {
throw new IllegalArgumentException("历史尾程事件目的类型证据冲突: " + order.sourceInboundId());
throw new EventProtocolException("历史尾程事件目的类型证据冲突: " + order.sourceInboundId());
}
if (fbaEvidence) return "FBA";
if (overseasWarehouseIdentity) return "OVERSEAS";
throw new IllegalArgumentException("历史尾程事件缺少 destinationType 且无法安全推断: " + order.sourceInboundId());
throw new EventProtocolException("历史尾程事件缺少 destinationType 且无法安全推断: " + order.sourceInboundId());
}
private String normalizeDestinationType(String value) {
if (!hasText(value)) return null;
String normalized = value.trim().toUpperCase(java.util.Locale.ROOT);
if (!"FBA".equals(normalized) && !"OVERSEAS".equals(normalized)) {
throw new IllegalArgumentException("尾程事件 destinationType 必须为 FBA 或 OVERSEAS");
throw new EventProtocolException("尾程事件 destinationType 必须为 FBA 或 OVERSEAS");
}
return normalized;
}
@@ -577,7 +577,7 @@ public class LoadingCompletedConsumer {
List<String> marks = new ArrayList<>(left.cartonMarks()); marks.addAll(right.cartonMarks());
Map<String, Long> quantities = new java.util.LinkedHashMap<>(left.cartonQuantities());
right.cartonQuantities().forEach((mark, quantity) -> {
if (quantities.putIfAbsent(mark, quantity) != null) throw new IllegalArgumentException("Duplicate carton quantity: " + mark);
if (quantities.putIfAbsent(mark, quantity) != null) throw new EventProtocolException("Duplicate carton quantity: " + mark);
});
return new DropshipRequests.SkuPackages(left.skuCode(), left.productName(),
Math.addExact(left.packageCount(), right.packageCount()),
@@ -620,25 +620,25 @@ public class LoadingCompletedConsumer {
private int positiveManifest(JsonNode source, String field) {
int value = source.path(field).asInt(0);
if (value <= 0) throw new IllegalArgumentException("One-click dropship pallet is missing " + field);
if (value <= 0) throw new EventProtocolException("One-click dropship pallet is missing " + field);
return value;
}
private String requiredManifestHash(JsonNode source) {
String value = source.path("manifestHash").asText(null);
if (value == null || !value.matches("(?i)[0-9a-f]{64}")) {
throw new IllegalArgumentException("One-click dropship pallet manifestHash is invalid");
throw new EventProtocolException("One-click dropship pallet manifestHash is invalid");
}
return value.toLowerCase(java.util.Locale.ROOT);
}
private Map<String, Long> cartonQuantities(JsonNode sku) {
JsonNode node = sku.path("cartonQuantities");
if (!node.isObject()) throw new IllegalArgumentException("One-click dropship cartonQuantities is required");
if (!node.isObject()) throw new EventProtocolException("One-click dropship cartonQuantities is required");
Map<String, Long> result = new java.util.LinkedHashMap<>();
node.fields().forEachRemaining(entry -> {
if (!entry.getValue().isIntegralNumber() || !entry.getValue().canConvertToLong() || entry.getValue().longValue() <= 0) {
throw new IllegalArgumentException("Invalid carton quantity: " + entry.getKey());
throw new EventProtocolException("Invalid carton quantity: " + entry.getKey());
}
result.put(entry.getKey(), entry.getValue().longValue());
});