fix: converge inbound pallets and retries

This commit is contained in:
2026-09-11 00:06:57 +08:00
parent ed12769858
commit 2bd12f8a5f
11 changed files with 65 additions and 15 deletions
@@ -130,6 +130,13 @@ public class WarehouseResourceRepository {
.orderByAsc(WarehousePalletEntity::getId));
}
public WarehousePalletEntity findPublicPalletByCode(long warehouseId, String palletCode) {
return pallets.selectOne(new LambdaQueryWrapper<WarehousePalletEntity>()
.eq(WarehousePalletEntity::getCustomerId, 0L)
.eq(WarehousePalletEntity::getWarehouseId, warehouseId)
.eq(WarehousePalletEntity::getPalletCode, palletCode));
}
public WarehousePalletEntity findPublicPallet(long palletId) {
return pallets.selectOne(new LambdaQueryWrapper<WarehousePalletEntity>()
.eq(WarehousePalletEntity::getCustomerId, 0L).eq(WarehousePalletEntity::getId, palletId));
@@ -88,12 +88,16 @@ public class WarehousePalletBatchService {
SharedReservation shared = isDropshipPallet(requested.palletCode())
? sharedDropship.get(requested.palletCode()) : null;
if (shared == null) {
Long palletId = resources.createPallet(WarehouseResourceService.PUBLIC_RESOURCE_SCOPE,
normalized.warehouseId(), new PalletRequests.SaveRequest(requested.palletCode(), "WOOD",
AUTO_PALLET_CAPACITY_CBM, AUTO_PALLET_UNIT_PRICE_USD, "IDLE", null, AUTO_PALLET_REMARK,
requested.fbaWarehouseCode()));
WarehousePalletEntity createdPallet = resources.pallet(WarehouseResourceService.PUBLIC_RESOURCE_SCOPE,
normalized.warehouseId(), palletId);
WarehousePalletEntity createdPallet = resources.reusablePublicPallet(
normalized.warehouseId(), requested.palletCode());
if (createdPallet == null) {
Long palletId = resources.createPallet(WarehouseResourceService.PUBLIC_RESOURCE_SCOPE,
normalized.warehouseId(), new PalletRequests.SaveRequest(requested.palletCode(), "WOOD",
AUTO_PALLET_CAPACITY_CBM, AUTO_PALLET_UNIT_PRICE_USD, "IDLE", null, AUTO_PALLET_REMARK,
requested.fbaWarehouseCode()));
createdPallet = resources.pallet(WarehouseResourceService.PUBLIC_RESOURCE_SCOPE,
normalized.warehouseId(), palletId);
}
WarehouseResourceLockResponse lock = locks.reserve(new WarehouseResourceLockRequests.ReserveRequest(
requested.customerId(), normalized.warehouseId(), createdPallet.getId(), createdPallet.getLocationId(),
BUSINESS_TYPE, requested.businessKey(), reservationKey(batch, requested.sequenceNo())));
@@ -191,6 +191,12 @@ public class WarehouseResourceService {
return palletReference(pallet);
}
public WarehousePalletEntity reusablePublicPallet(long warehouseId, String palletCode) {
if (!StringUtils.hasText(palletCode)) return null;
requireWarehouseReference(warehouseId);
return repository.findPublicPalletByCode(warehouseId, palletCode.trim());
}
public WarehousePalletResolveResponse resolvePublicPallet(long palletId) {
WarehousePalletEntity pallet = repository.findPublicPallet(palletId);
if (pallet == null) throw new BizException(ResultCode.NOT_FOUND, "托盘不存在");
@@ -132,6 +132,25 @@ class WarehousePalletBatchServiceTest {
assertThat(response.items()).extracting(value -> value.palletCode()).containsExactly("PAL-301");
}
@Test
void compensatedSourcePalletCodeReusesTheExistingIdlePhysicalPallet() {
when(batches.insertIgnore(any())).thenReturn(1);
WarehousePalletEntity existing = pallet(301L, 401L, "DP-260910-0001");
existing.setStatus("IDLE");
when(resources.reusablePublicPallet(WAREHOUSE, "DP-260910-0001")).thenReturn(existing);
when(locks.reserve(any())).thenReturn(lockResponse(CUSTOMER_A, 501L));
var request = new WarehousePalletBatchRequests.EnsureReserved(WAREHOUSE, "retry-container", List.of(
new WarehousePalletBatchRequests.Item(CUSTOMER_A, "container:dropship-a:1", "PLAN-A", 1,
"STANDARD", null, "DP-260910-0001")));
var response = service.ensureReserved(request);
assertThat(response.items()).extracting(value -> value.palletId()).containsExactly(301L);
assertThat(response.items()).extracting(value -> value.palletCode()).containsExactly("DP-260910-0001");
verify(resources, never()).createPallet(anyLong(), anyLong(), any());
verify(locks).reserve(any());
}
@Test
void concurrentSameRequestReplaysWinningBatchWithoutCreatingResources() {
AtomicReference<WarehousePalletBatchEntity> inserted = new AtomicReference<>();