fix(transfer): preserve inbound ownership and expose durable receipt recovery

This commit is contained in:
2026-10-05 06:34:23 +08:00
parent 47acfc991b
commit 548a133de6
10 changed files with 306 additions and 16 deletions
@@ -968,7 +968,7 @@ public class RelabelApplicationService implements WalletResultHandler {
LastlegOrder transferOrder = orders.selectById(transfer.getLastlegOrderId());
if (transferOrder != null && customerId.equals(transferOrder.getCustomerId())) {
String transferOrderBefore = transferOrder.getCurrentStatus();
transferOrder.setCurrentStatus(transferStatus);
if (!existingStockTransfer) transferOrder.setCurrentStatus(transferStatus);
transferOrder.setFbaWarehouseCode(transfer.getFbaWarehouseCode());
transferOrder.setRelabelStatus("COMPLETED");
transferOrder.setSubOrderStatus("WAREHOUSE_TRANSFER:" + transferStatus + ";RELABEL:COMPLETED");
@@ -26,6 +26,11 @@ public final class WarehouseTransferResponses {
public record Allocation(Long inventoryPositionId, Long inboundOrderId, String inboundCode,
Long palletId, String palletNo, int packageCount, String status,
Long destinationInventoryPositionId, String skuCode) {}
public record ReceiptTarget(String warehouseId, String warehouseCode, String warehouseName,
Long palletId, String palletNo, Long locationId, String locationCode,
String fbaId, String fbaCode, String fbaName) {}
public record ReceiptCommand(Long sourceInventoryPositionId, String status, String requestHash,
WarehouseTransferRequests.Receipt request, ReceiptTarget target) {}
public record Summary(Long id, String transferNo, String inboundCode, Long lastlegOrderId,
String lastlegOrderNo, String operatingWarehouseId, String fbaWarehouseCode,
String destinationType, String destinationWarehouseCode, String destinationWarehouseName,
@@ -51,6 +56,7 @@ public final class WarehouseTransferResponses {
List<SourceOrder> sourceOrders, List<Item> items, List<Carton> cartons,
List<Pallet> pallets, List<RelabelBinding> relabelBindings,
List<Allocation> allocations, List<MeasurementPhoto> measurementPhotos,
List<ReceiptCommand> receiptCommands,
LocalDateTime customerConfirmedAt,
LocalDateTime actualShipAt, LocalDateTime createdAt) {}
}
@@ -8,6 +8,11 @@ import org.apache.ibatis.annotations.Select;
@Mapper
public interface WarehouseTransferReceiptCommandMapper extends BaseMapper<WarehouseTransferReceiptCommand> {
@Select("SELECT * FROM ll_warehouse_transfer_receipt_command WHERE customer_id=#{customerId} " +
"AND transfer_order_id=#{transferId} ORDER BY id")
java.util.List<WarehouseTransferReceiptCommand> listTransfer(@Param("customerId") String customerId,
@Param("transferId") Long transferId);
@Select("SELECT * FROM ll_warehouse_transfer_receipt_command WHERE customer_id=#{customerId} " +
"AND transfer_order_id=#{transferId} AND source_inventory_position_id=#{sourceId} LIMIT 1 FOR UPDATE")
WarehouseTransferReceiptCommand lockSource(@Param("customerId") String customerId,
@@ -54,6 +54,72 @@ public class WarehouseTransferReceiptCoordinator {
private record ResolvedTarget(Target target, String palletStatus) { }
private record Admission(WarehouseTransferOrder transfer, boolean replay) { }
@org.springframework.transaction.annotation.Transactional(readOnly = true)
public java.util.List<WarehouseTransferResponses.ReceiptCommand> receiptCommands(String customerId, long transferId) {
var transfer = transfers.selectById(transferId);
if (transfer == null || !Objects.equals(customerId, transfer.getCustomerId())) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Warehouse transfer order not found");
}
var rows = commands.listTransfer(customerId, transferId);
require(rows != null, "持久实收命令读取失败,请联系管理员核查");
java.util.Set<Long> sources = new java.util.HashSet<>();
return rows.stream().map(row -> {
require(row != null && Objects.equals(customerId, row.getCustomerId()) && Objects.equals(transferId, row.getTransferOrderId())
&& row.getSourceInventoryPositionId() != null && row.getSourceInventoryPositionId() > 0
&& sources.add(row.getSourceInventoryPositionId())
&& row.getStatus() != null && Set.of("PREPARED", "RESERVED", "COMPLETED").contains(row.getStatus()),
"持久实收命令归属或状态无效,请联系管理员核查");
var request = readSnapshot(row.getRequestJson(), WarehouseTransferRequests.Receipt.class);
var target = readSnapshot(row.getTargetJson(), Target.class);
require(Objects.equals(request.sourceInventoryPositionId(), row.getSourceInventoryPositionId())
&& text(request.idempotencyKey()) != null && text(request.idempotencyKey()).equals(row.getIdempotencyKey())
&& Objects.equals(hash(request), row.getRequestHash()), "持久实收原请求损坏,请联系管理员核查");
long warehouseId = positive(target.warehouseId(), "持久实收目标损坏,请联系管理员核查");
require(target.palletId() != null && target.palletId() > 0 && target.locationId() != null && target.locationId() > 0
&& text(target.warehouseCode()) != null && text(target.warehouseName()) != null
&& text(target.palletNo()) != null && text(target.locationCode()) != null && text(target.fbaCode()) != null
&& text(target.fbaName()) != null && positive(target.fbaId(), "持久实收目标损坏,请联系管理员核查") > 0
&& Objects.equals(row.getTargetWarehouseId(), warehouseId)
&& Objects.equals(row.getTargetPalletId(), target.palletId())
&& Objects.equals(row.getTargetLocationId(), target.locationId())
&& Objects.equals(row.getTargetHash(), hash(new Binding(target.warehouseId(), target.palletId(), target.locationId(), target.fbaCode())))
&& positive(request.destinationOverseasWarehouseId(), "持久实收原请求损坏,请联系管理员核查") == warehouseId
&& Objects.equals(request.destinationPalletId(), target.palletId())
&& Objects.equals(text(request.destinationPalletNo()), target.palletNo())
&& Objects.equals(text(request.destinationWarehouseCode()), target.fbaCode())
&& (text(request.destinationWarehouseId()) == null || positive(request.destinationWarehouseId(),
"持久实收原请求损坏,请联系管理员核查") == positive(target.fbaId(), "持久实收目标损坏,请联系管理员核查"))
&& (request.warehouseLocationId() == null || Objects.equals(request.warehouseLocationId(), target.locationId()))
&& ("PREPARED".equals(row.getStatus()) || row.getResourceLockId() != null && row.getResourceLockId() > 0),
"持久实收目标损坏,请联系管理员核查");
if ("COMPLETED".equals(row.getStatus())) {
var receipt = receipts.findSource(customerId, transferId, row.getSourceInventoryPositionId());
require(receipt != null && Objects.equals(receipt.getCustomerId(), customerId)
&& Objects.equals(receipt.getTransferOrderId(), transferId)
&& Objects.equals(receipt.getRequestHash(), row.getRequestHash())
&& Objects.equals(receipt.getIdempotencyKey(), row.getIdempotencyKey()),
"持久实收完成凭据缺失,请联系管理员核查");
}
return new WarehouseTransferResponses.ReceiptCommand(row.getSourceInventoryPositionId(), row.getStatus(),
row.getRequestHash(), request, new WarehouseTransferResponses.ReceiptTarget(target.warehouseId(),
target.warehouseCode(), target.warehouseName(), target.palletId(), target.palletNo(),
target.locationId(), target.locationCode(), target.fbaId(), target.fbaCode(), target.fbaName()));
}).toList();
}
private <T> T readSnapshot(String payload, Class<T> type) {
try {
com.fasterxml.jackson.databind.JsonNode tree = objectMapper.reader().with(com.fasterxml.jackson.core.JsonParser.Feature.STRICT_DUPLICATE_DETECTION)
.with(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_TRAILING_TOKENS).readTree(payload);
require(tree != null && tree.isObject(), "持久实收快照损坏,请联系管理员核查");
var fields = java.util.Arrays.stream(type.getRecordComponents()).map(java.lang.reflect.RecordComponent::getName)
.collect(java.util.stream.Collectors.toSet());
tree.fieldNames().forEachRemaining(field -> require(fields.contains(field), "持久实收快照含未知字段,请联系管理员核查"));
return objectMapper.treeToValue(tree, type);
} catch (ResponseStatusException failure) { throw failure; }
catch (Exception failure) { throw new ResponseStatusException(HttpStatus.CONFLICT, "持久实收快照损坏,请联系管理员核查"); }
}
public WarehouseTransferResponses.Detail receive(String customerId, String transferNo, String operatorId,
WarehouseTransferRequests.Receipt request, Supplier<WarehouseTransferResponses.Detail> replay,
Function<Context, WarehouseTransferResponses.Detail> stockWork) {
@@ -99,9 +165,11 @@ public class WarehouseTransferReceiptCoordinator {
if (current.replay()) return replay.get();
require("RESERVED".equals(owned.getStatus()) && Objects.equals(owned.getResourceLockId(), lockId),
"Receipt has no active reserved resource");
WarehouseTransferResponses.Detail result = stockWork.apply(new Context(target, lockId, owned.getId()));
owned.setStatus("COMPLETED"); owned.setUpdatedAt(LocalDateTime.now()); commands.updateById(owned);
return result;
stockWork.apply(new Context(target, lockId, owned.getId()));
owned.setStatus("COMPLETED"); owned.setUpdatedAt(LocalDateTime.now());
require(commands.updateById(owned) == 1, "Receipt completion changed concurrently");
// Read inside the same transaction after completion, so the response cannot leak the earlier RESERVED view.
return replay.get();
});
}
@@ -601,6 +601,7 @@ public class WarehouseTransferService {
transfer.getRelabelContactPhone(), transfer.getRelabelDeliveryMethod(),
transfer.getReviewDecision(), transfer.getReviewRemark(), transfer.getReviewedBy(),
transfer.getReviewedAt(), sources, items, cartons, pallets, bindings, allocations, photos,
receiptCoordinator.receiptCommands(customerId, transfer.getId()),
transfer.getCustomerConfirmedAt(), transfer.getOutboundAt(), transfer.getCreatedAt());
}
@@ -1129,8 +1130,7 @@ public class WarehouseTransferService {
transfer.setAllocationIdempotencyKey(key);
transfer.setAllocationRequestHash(requestHash); transfer.setUpdatedAt(now); transfers.updateById(transfer);
LastlegOrder order = requireTailOrder(customerId, transfer);
String before = order.getCurrentStatus(); order.setCurrentStatus(
Boolean.TRUE.equals(transfer.getNeedRelabel()) ? INBOUND_COMPLETED : "WAIT_OUTBOUND");
String before = order.getCurrentStatus(); order.setCurrentStatus(INBOUND_COMPLETED);
if (Boolean.TRUE.equals(transfer.getNeedRelabel())) {
order.setRelabelOrderNo(transfer.getRelabelNo()); order.setRelabelStatus("PENDING_REVIEW");
}
@@ -1203,7 +1203,7 @@ public class WarehouseTransferService {
transfer.setOutboundRequestHash(requestHash); transfer.setOutboundAt(now); transfer.setUpdatedAt(now);
transfers.updateById(transfer);
LastlegOrder order = requireTailOrder(customerId, transfer);
String before = order.getCurrentStatus(); order.setCurrentStatus("IN_TRANSIT");
String before = order.getCurrentStatus();
order.setSubOrderStatus("WAREHOUSE_TRANSFER:OUTBOUNDED"); order.setUpdatedAt(now);
orders.updateById(order);
insertOrderHistory(customerId, order.getId(), before, order.getCurrentStatus(), transferNo,
@@ -1254,12 +1254,16 @@ public class WarehouseTransferService {
if (sourceBalance == null || !customerId.equals(sourceBalance.getCustomerId())) {
throw conflict("Source inventory balance is missing for warehouse transfer receipt");
}
if (sourceBalance.getInboundOrderId() == null || sourceBalance.getInboundOrderId() <= 0
|| !StringUtils.hasText(sourceBalance.getInboundOrderNo()) || !StringUtils.hasText(sourceBalance.getInboundCode())) {
throw conflict("Source inventory inbound identity is missing for warehouse transfer receipt");
}
WarehouseTransferReceiptCoordinator.Target target = context.target();
InventoryCommandResponse result = inventory.stockIn(customerId, operatorId,
new InventoryCommandRequests.StockIn(customerId, sourceBalance.getCustomerName(),
target.warehouseId(), target.warehouseCode(), target.warehouseName(), target.fbaId(),
target.fbaCode(), target.fbaName(),
transfer.getLastlegOrderId(), transfer.getLastlegOrderNo(), transferNo,
sourceBalance.getInboundOrderId(), sourceBalance.getInboundOrderNo(), sourceBalance.getInboundCode(),
Boolean.TRUE.equals(transfer.getNeedRelabel()) ? allocation.getSkuCode() : sourceBalance.getSkuCode(),
sourceBalance.getProductNameZh(), sourceBalance.getProductNameEn(),
target.palletId(), target.palletNo(),
@@ -1281,7 +1285,7 @@ public class WarehouseTransferService {
|| "RECEIVED".equals(value.getStatus()));
transfer.setStatus(complete ? RECEIVED : PARTIALLY_RECEIVED); transfer.setUpdatedAt(now); transfers.updateById(transfer);
LastlegOrder order = requireTailOrder(customerId, transfer);
String before = order.getCurrentStatus(); order.setCurrentStatus(complete ? "COMPLETED" : "IN_TRANSIT");
String before = order.getCurrentStatus();
order.setSubOrderStatus("WAREHOUSE_TRANSFER:" + (complete ? RECEIVED : PARTIALLY_RECEIVED));
order.setUpdatedAt(now); orders.updateById(order);
insertOrderHistory(customerId, order.getId(), before, order.getCurrentStatus(), transferNo,
@@ -1106,7 +1106,7 @@ paths:
idempotencyKey: { type: string, maxLength: 255 }
remark: { type: string, nullable: true, maxLength: 1000 }
responses:
'200': { description: 原转运详情,部分来源PARTIALLY_RECEIVED、全部来源RECEIVED;原键原载荷返回原收货结果 }
'200': { description: 原转运详情,部分来源PARTIALLY_RECEIVED、全部来源RECEIVED;receiptCommands包含真正COMPLETED的本次命令,原键原载荷返回持久结果, content: { application/json: { schema: { $ref: '#/components/schemas/WarehouseTransferDetailApiResponse' } } } }
'409': { description: 原请求冲突、来源状态或目标资源归属不匹配;不得换键绕过 }
'503': { description: 资源服务不可用,保留原收货命令待核对恢复 }
/api/platform/v1/last-leg/warehouse-transfers/{transferNo}/outbound:
@@ -1193,7 +1193,7 @@ paths:
description: 权限 merchant:overseas-warehouse:transfer:list;不泄露其他客户订单。
security: [{ bearerAuth: [] }]
parameters: [{ name: transferNo, in: path, required: true, schema: { type: string } }]
responses: { '200': { description: 当前客户仓储转运单详情 }, '403': { description: 缺少仓储转运查看权限 }, '404': { description: 当前客户下转运单不存在 } }
responses: { '200': { description: 当前客户仓储转运单详情,receiptCommands仅本客户本票;未知状态或损坏快照拒绝返回,不当作新请求, content: { application/json: { schema: { $ref: '#/components/schemas/WarehouseTransferDetailApiResponse' } } } }, '403': { description: 缺少仓储转运查看权限 }, '404': { description: 当前客户下转运单不存在 }, '409': { description: 持久实收命令状态、原请求或目标损坏,须核查原命令 } }
/api/merchant/v1/last-leg/warehouse-transfers/{transferNo}/confirm-measurement:
post:
summary: 客户确认仓储转运实测数据
@@ -1844,6 +1844,62 @@ paths:
responses: { '200': { description: 退货箱唛打印数据 }, '404': { description: 当前仓没有该退货箱 } }
components:
schemas:
WarehouseTransferDetailApiResponse:
type: object
properties:
data: { $ref: '#/components/schemas/WarehouseTransferDetail' }
WarehouseTransferDetail:
type: object
description: 保留原转运详情字段;新增持久实收命令投影供平台与当前客户恢复。成功实收返回本地事务完成后的命令状态,不暴露内部资源锁或reservationKey。
required: [receiptCommands]
properties:
receiptCommands:
type: array
items: { $ref: '#/components/schemas/WarehouseTransferReceiptCommand' }
WarehouseTransferReceiptCommand:
type: object
additionalProperties: false
required: [sourceInventoryPositionId, status, requestHash, request, target]
properties:
sourceInventoryPositionId: { type: string, description: 原来源库存位置标识,仅用于原命令恢复 }
status: { type: string, enum: [PREPARED, RESERVED, COMPLETED] }
requestHash: { type: string, pattern: '^[a-f0-9]{64}$' }
request: { $ref: '#/components/schemas/WarehouseTransferReceiptRequest' }
target: { $ref: '#/components/schemas/WarehouseTransferReceiptTarget' }
WarehouseTransferReceiptTarget:
type: object
additionalProperties: false
required: [warehouseId, warehouseCode, warehouseName, palletId, palletNo, locationId, locationCode, fbaId, fbaCode, fbaName]
properties:
warehouseId: { type: string }
warehouseCode: { type: string }
warehouseName: { type: string }
palletId: { type: string }
palletNo: { type: string }
locationId: { type: string }
locationCode: { type: string }
fbaId: { type: string }
fbaCode: { type: string }
fbaName: { type: string }
WarehouseTransferReceiptRequest:
type: object
additionalProperties: false
required: [sourceInventoryPositionId, destinationOverseasWarehouseId, destinationWarehouseCode, destinationPalletId, destinationPalletNo, idempotencyKey]
properties:
sourceInventoryPositionId: { type: integer, format: int64 }
destinationOverseasWarehouseId: { type: string, maxLength: 128 }
destinationOverseasWarehouseCode: { type: string, nullable: true, maxLength: 128 }
destinationOverseasWarehouseName: { type: string, nullable: true, maxLength: 255 }
destinationWarehouseId: { type: string, nullable: true, maxLength: 128 }
destinationWarehouseCode: { type: string, maxLength: 128 }
destinationWarehouseName: { type: string, nullable: true, maxLength: 255 }
destinationPalletId: { type: integer, format: int64 }
destinationPalletNo: { type: string, maxLength: 128 }
batchNo: { type: string, nullable: true, maxLength: 128 }
warehouseLocationId: { type: integer, format: int64, nullable: true }
warehouseLocationCode: { type: string, nullable: true, maxLength: 128 }
idempotencyKey: { type: string, maxLength: 255 }
remark: { type: string, nullable: true, maxLength: 1000 }
ExceptionOrderDetailResponse:
type: object
properties: