fix(lastleg): restore returned inventory from shipment source
This commit is contained in:
+231
-1
@@ -3,6 +3,8 @@ package com.baiyue.lastleg.inventory.command.service;
|
||||
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
||||
import com.baiyue.lastleg.inventory.command.dto.InventoryCommandRequests;
|
||||
import com.baiyue.lastleg.inventory.command.dto.InventoryCommandResponse;
|
||||
import com.baiyue.lastleg.inventory.command.dto.ReturnedInventoryRestoreRequest;
|
||||
import com.baiyue.lastleg.inventory.command.dto.ReturnedInventoryRestoreResponse;
|
||||
import com.baiyue.lastleg.inventory.command.mapper.InventoryCommandMapper;
|
||||
import com.baiyue.lastleg.inventory.entity.InventoryBalance;
|
||||
import com.baiyue.lastleg.inventory.entity.InventoryLedger;
|
||||
@@ -27,7 +29,9 @@ import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HexFormat;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
@Service
|
||||
@@ -245,6 +249,175 @@ public class InventoryCommandService {
|
||||
});
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public ReturnedInventoryRestoreResponse restoreReturnedInventory(String customerId, String operatorId,
|
||||
ReturnedInventoryRestoreRequest request) {
|
||||
if (request == null) throw badRequest("Returned inventory restore request is required");
|
||||
String exceptionNo = required(request.exceptionNo(), "Exception number is required");
|
||||
String outboundNo = required(request.outboundNo(), "Outbound number is required");
|
||||
String warehouseId = required(request.overseasWarehouseId(), "Overseas warehouse ID is required");
|
||||
long warehouseResourceId;
|
||||
try {
|
||||
warehouseResourceId = Long.parseLong(warehouseId);
|
||||
} catch (NumberFormatException exception) {
|
||||
throw badRequest("Overseas warehouse ID must be numeric");
|
||||
}
|
||||
if (request.inboundOrderId() == null || request.inboundOrderId() <= 0) {
|
||||
throw badRequest("Inbound order ID is required");
|
||||
}
|
||||
if (request.sources() == null || request.sources().isEmpty()) {
|
||||
throw badRequest("Returned inventory sources are required");
|
||||
}
|
||||
if (request.receipts() == null || request.receipts().isEmpty()) {
|
||||
throw badRequest("Returned inventory receipts are required");
|
||||
}
|
||||
if (request.evidenceFileIds() == null || request.evidenceFileIds().isEmpty()
|
||||
|| request.evidenceFileIds().stream().anyMatch(value -> trim(value) == null)) {
|
||||
throw badRequest("Return evidence file IDs are required");
|
||||
}
|
||||
|
||||
Map<Long, ReturnedInventoryRestoreRequest.SourceItem> sources = new LinkedHashMap<>();
|
||||
for (ReturnedInventoryRestoreRequest.SourceItem source : request.sources()) {
|
||||
if (source == null || source.outboundPalletItemId() == null || source.outboundPalletItemId() <= 0
|
||||
|| source.inventoryPositionId() == null || source.inventoryPositionId() <= 0
|
||||
|| source.maxReturnPackages() <= 0) {
|
||||
throw badRequest("A valid returned inventory source is required");
|
||||
}
|
||||
String sourceSku = required(source.skuCode(), "Returned inventory source SKU is required");
|
||||
ReturnedInventoryRestoreRequest.SourceItem normalized = new ReturnedInventoryRestoreRequest.SourceItem(
|
||||
source.outboundPalletItemId(), source.inventoryPositionId(), sourceSku, source.maxReturnPackages());
|
||||
if (sources.putIfAbsent(source.outboundPalletItemId(), normalized) != null) {
|
||||
throw badRequest("Returned inventory source must be unique");
|
||||
}
|
||||
}
|
||||
Map<Long, Long> requestedBySource = new LinkedHashMap<>();
|
||||
Map<String, Boolean> receiptTargets = new LinkedHashMap<>();
|
||||
for (ReturnedInventoryRestoreRequest.Receipt receipt : request.receipts()) {
|
||||
ReturnedInventoryRestoreRequest.SourceItem source = receipt == null ? null
|
||||
: sources.get(receipt.outboundPalletItemId());
|
||||
if (source == null || !Objects.equals(source.inventoryPositionId(), receipt.inventoryPositionId())) {
|
||||
throw badRequest("Return receipt must match an immutable outbound inventory source");
|
||||
}
|
||||
if (receipt.receivedPackages() <= 0 || receipt.targetPalletId() == null || receipt.targetPalletId() <= 0
|
||||
|| receipt.targetLocationId() == null || receipt.targetLocationId() <= 0) {
|
||||
throw badRequest("A valid returned inventory receipt is required");
|
||||
}
|
||||
required(receipt.targetPalletNo(), "Target pallet number is required");
|
||||
required(receipt.targetLocationCode(), "Target location code is required");
|
||||
String receiptTarget = receipt.outboundPalletItemId() + ":" + receipt.targetPalletId();
|
||||
if (receiptTargets.putIfAbsent(receiptTarget, Boolean.TRUE) != null) {
|
||||
throw badRequest("Return receipt source and target pallet must be unique");
|
||||
}
|
||||
requestedBySource.merge(source.outboundPalletItemId(), receipt.receivedPackages(), Long::sum);
|
||||
}
|
||||
for (ReturnedInventoryRestoreRequest.SourceItem source : sources.values()) {
|
||||
long requested = requestedBySource.getOrDefault(source.outboundPalletItemId(), 0L);
|
||||
if (requested <= 0 || requested > source.maxReturnPackages()) {
|
||||
throw conflict("Returned packages exceed the approved source limit");
|
||||
}
|
||||
}
|
||||
|
||||
String normalizedKey = required(request.idempotencyKey(), "Idempotency key is required");
|
||||
String requestHash = hash(request);
|
||||
InventoryOperation existing = operations.findIdempotency(customerId, normalizedKey);
|
||||
if (existing != null) return replayReturned(existing, requestHash);
|
||||
|
||||
InventoryOperation operation = new InventoryOperation();
|
||||
operation.setId(IdWorker.getId());
|
||||
operation.setCustomerId(customerId);
|
||||
operation.setIdempotencyKey(normalizedKey);
|
||||
operation.setOperationType("RETURN_STOCK_IN");
|
||||
operation.setRequestHash(requestHash);
|
||||
operation.setStatus("PROCESSING");
|
||||
operation.setCreatedAt(LocalDateTime.now());
|
||||
operation.setUpdatedAt(operation.getCreatedAt());
|
||||
try {
|
||||
operations.insert(operation);
|
||||
} catch (DuplicateKeyException exception) {
|
||||
InventoryOperation concurrent = operations.findIdempotency(customerId, normalizedKey);
|
||||
if (concurrent != null) return replayReturned(concurrent, requestHash);
|
||||
throw exception;
|
||||
}
|
||||
|
||||
Map<Long, Locked> lockedSources = new LinkedHashMap<>();
|
||||
Map<Long, Long> balanceBefore = new LinkedHashMap<>();
|
||||
for (ReturnedInventoryRestoreRequest.SourceItem source : sources.values()) {
|
||||
Locked locked = locked(customerId, source.inventoryPositionId());
|
||||
InventoryBalance balance = locked.balance();
|
||||
if (!Objects.equals(balance.getInboundOrderId(), request.inboundOrderId())) {
|
||||
throw conflict("Returned inventory source inbound order does not match");
|
||||
}
|
||||
if (!warehouseId.equals(trim(balance.getOverseasWarehouseId()))) {
|
||||
throw conflict("Returned inventory source warehouse does not match");
|
||||
}
|
||||
long shipped = commands.shippedPackages(customerId, source.inventoryPositionId(), outboundNo,
|
||||
source.skuCode());
|
||||
long restored = commands.restoredPackages(customerId, returnSourceIdentity(source), source.skuCode());
|
||||
long requested = requestedBySource.get(source.outboundPalletItemId());
|
||||
if (shipped <= 0) throw conflict("Immutable outbound shipment source was not found");
|
||||
if (restored + requested > shipped) {
|
||||
throw conflict("Returned packages exceed the remaining shipped packages");
|
||||
}
|
||||
lockedSources.put(source.outboundPalletItemId(), locked);
|
||||
balanceBefore.putIfAbsent(balance.getId(), value(balance.getAvailablePackages()));
|
||||
}
|
||||
|
||||
long restoredPackages = 0;
|
||||
List<Long> ledgerIds = new java.util.ArrayList<>();
|
||||
for (ReturnedInventoryRestoreRequest.Receipt receipt : request.receipts()) {
|
||||
ReturnedInventoryRestoreRequest.SourceItem source = sources.get(receipt.outboundPalletItemId());
|
||||
Locked sourceLocked = lockedSources.get(receipt.outboundPalletItemId());
|
||||
InventoryBalance balance = sourceLocked.balance();
|
||||
WarehouseResourceGateway.WarehousePalletReference pallet = warehouseGateway.resolvePallet(
|
||||
receipt.targetPalletId());
|
||||
if (pallet == null || pallet.palletId() != receipt.targetPalletId()
|
||||
|| pallet.warehouseId() != warehouseResourceId
|
||||
|| pallet.locationId() != receipt.targetLocationId()
|
||||
|| !required(receipt.targetPalletNo(), "Target pallet number is required").equals(pallet.palletNo())) {
|
||||
throw conflict("Target pallet does not match the returned inventory warehouse and location");
|
||||
}
|
||||
WarehouseResourceGateway.WarehouseLocationReference location = warehouseGateway.resolveLocation(
|
||||
warehouseResourceId, receipt.targetLocationId());
|
||||
if (location == null || location.locationId() != receipt.targetLocationId()
|
||||
|| !required(receipt.targetLocationCode(), "Target location code is required")
|
||||
.equals(location.locationCode())) {
|
||||
throw conflict("Target location does not match the returned inventory warehouse");
|
||||
}
|
||||
|
||||
InventoryPosition target = returnedPosition(customerId, balance, exceptionNo, receipt, location);
|
||||
long before = value(target.getAvailablePackages());
|
||||
target.setAvailablePackages(before + receipt.receivedPackages());
|
||||
target.setStatus("OCCUPIED");
|
||||
target.setVersion(value(target.getVersion()) + 1);
|
||||
target.setUpdatedAt(LocalDateTime.now());
|
||||
positions.updateById(target);
|
||||
|
||||
balance.setAvailablePackages(value(balance.getAvailablePackages()) + receipt.receivedPackages());
|
||||
touch(balance);
|
||||
balances.updateById(balance);
|
||||
InventoryLedger ledger = ledger(customerId, operatorId, operation.getId(), balance, target,
|
||||
source.skuCode(), "RETURN_STOCK_IN", "SHIPPED", "AVAILABLE", before,
|
||||
receipt.receivedPackages(), before + receipt.receivedPackages(), exceptionNo,
|
||||
balance.getInboundOrderNo(), outboundNo, returnSourceIdentity(source),
|
||||
normalizedKey + ":" + receipt.outboundPalletItemId() + ":" + target.getId(),
|
||||
"Exception return received with evidence " + request.evidenceFileIds().size());
|
||||
ledgerIds.add(ledger.getId());
|
||||
restoredPackages += receipt.receivedPackages();
|
||||
}
|
||||
long beforePackages = balanceBefore.values().stream().mapToLong(Long::longValue).sum();
|
||||
long afterPackages = balanceBefore.keySet().stream()
|
||||
.mapToLong(balanceId -> value(lockedSources.values().stream()
|
||||
.map(Locked::balance).filter(balance -> balance.getId().equals(balanceId)).findFirst().orElseThrow()
|
||||
.getAvailablePackages())).sum();
|
||||
ReturnedInventoryRestoreResponse response = new ReturnedInventoryRestoreResponse(operation.getId(),
|
||||
List.copyOf(ledgerIds), beforePackages, restoredPackages, afterPackages, false, LocalDateTime.now());
|
||||
operation.setStatus("COMPLETED");
|
||||
operation.setResultJson(write(response));
|
||||
operation.setUpdatedAt(response.completedAt());
|
||||
operations.updateById(operation);
|
||||
return response;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public InventoryCommandResponse adjust(String customerId, String operatorId,
|
||||
InventoryCommandRequests.Adjustment request) {
|
||||
@@ -483,7 +656,63 @@ public class InventoryCommandService {
|
||||
}
|
||||
}
|
||||
|
||||
private void ledger(String customerId, String operatorId, Long operationId, InventoryBalance balance,
|
||||
private InventoryPosition returnedPosition(String customerId, InventoryBalance balance, String exceptionNo,
|
||||
ReturnedInventoryRestoreRequest.Receipt receipt,
|
||||
WarehouseResourceGateway.WarehouseLocationReference location) {
|
||||
String batchNo = "RETURN:" + exceptionNo;
|
||||
InventoryPosition position = positions.lockPosition(customerId, balance.getId(), receipt.targetPalletId(), batchNo);
|
||||
if (position != null) {
|
||||
if (!Objects.equals(position.getWarehouseLocationId(), location.locationId())
|
||||
|| !Objects.equals(position.getWarehouseLocationCode(), location.locationCode())
|
||||
|| !Objects.equals(position.getPalletNo(), receipt.targetPalletNo())) {
|
||||
throw conflict("Returned inventory target position snapshot does not match");
|
||||
}
|
||||
return position;
|
||||
}
|
||||
position = new InventoryPosition();
|
||||
position.setId(IdWorker.getId());
|
||||
position.setCustomerId(customerId);
|
||||
position.setBalanceId(balance.getId());
|
||||
position.setOverseasWarehouseId(balance.getOverseasWarehouseId());
|
||||
position.setDestinationWarehouseCode(balance.getDestinationWarehouseCode());
|
||||
position.setPalletId(receipt.targetPalletId());
|
||||
position.setPalletNo(receipt.targetPalletNo().trim());
|
||||
position.setBatchNo(batchNo);
|
||||
position.setWarehouseLocationId(location.locationId());
|
||||
position.setWarehouseLocationCode(location.locationCode());
|
||||
position.setAvailablePackages(0L);
|
||||
position.setHoldPackages(0L);
|
||||
position.setAllocatedPackages(0L);
|
||||
position.setPickedPackages(0L);
|
||||
position.setReceivingPackages(0L);
|
||||
position.setStatus("OCCUPIED");
|
||||
position.setVersion(0L);
|
||||
position.setCreatedAt(LocalDateTime.now());
|
||||
position.setUpdatedAt(position.getCreatedAt());
|
||||
positions.insert(position);
|
||||
return position;
|
||||
}
|
||||
|
||||
private ReturnedInventoryRestoreResponse replayReturned(InventoryOperation operation, String requestHash) {
|
||||
if (!"RETURN_STOCK_IN".equals(operation.getOperationType())
|
||||
|| !requestHash.equals(operation.getRequestHash())) {
|
||||
throw conflict("Idempotency key was already used with a different inventory request");
|
||||
}
|
||||
if (!"COMPLETED".equals(operation.getStatus()) || trim(operation.getResultJson()) == null) {
|
||||
throw conflict("Inventory operation is still processing");
|
||||
}
|
||||
try {
|
||||
return objectMapper.readValue(operation.getResultJson(), ReturnedInventoryRestoreResponse.class).asReplay();
|
||||
} catch (JsonProcessingException exception) {
|
||||
throw new IllegalStateException("Cannot read returned inventory idempotency result", exception);
|
||||
}
|
||||
}
|
||||
|
||||
private static String returnSourceIdentity(ReturnedInventoryRestoreRequest.SourceItem source) {
|
||||
return "RETURN_SOURCE:" + source.outboundPalletItemId() + ":" + source.inventoryPositionId();
|
||||
}
|
||||
|
||||
private InventoryLedger ledger(String customerId, String operatorId, Long operationId, InventoryBalance balance,
|
||||
InventoryPosition position, String skuCode, String businessType, String from, String to,
|
||||
long beforePackages, long deltaPackages, long afterPackages,
|
||||
String businessOrderNo, String inboundOrderNo, String outboundOrderNo,
|
||||
@@ -524,6 +753,7 @@ public class InventoryCommandService {
|
||||
ledger.setOccurredAt(LocalDateTime.now());
|
||||
ledger.setCreatedAt(ledger.getOccurredAt());
|
||||
ledgers.insert(ledger);
|
||||
return ledger;
|
||||
}
|
||||
|
||||
private static void mergeSkuSnapshot(InventoryBalance balance, String skuCode) {
|
||||
|
||||
Reference in New Issue
Block a user