diff --git a/order/src/main/java/com/baiyue/order/repository/OrderRepository.java b/order/src/main/java/com/baiyue/order/repository/OrderRepository.java index 0bf9cbe27..dd47c0356 100644 --- a/order/src/main/java/com/baiyue/order/repository/OrderRepository.java +++ b/order/src/main/java/com/baiyue/order/repository/OrderRepository.java @@ -115,7 +115,9 @@ public class OrderRepository { .eq(OutboxEventEntity::getPublishStatus, "PENDING") .and(v -> v.isNull(OutboxEventEntity::getNextRetryAt) .or().le(OutboxEventEntity::getNextRetryAt, LocalDateTime.now())) - .orderByAsc(OutboxEventEntity::getCreatedAt) + // Approval and its first declaration update can share the same DB timestamp. + // Keep the insertion order deterministic so the inbound exists before its update is delivered. + .orderByAsc(OutboxEventEntity::getCreatedAt, OutboxEventEntity::getId) .last("LIMIT " + Math.min(Math.max(limit, 1), 100))); } public void markOutboxPublished(long tenantId, long id, LocalDateTime publishedAt) { diff --git a/order/src/test/java/com/baiyue/order/repository/OrderRepositoryIsolationUpdateTest.java b/order/src/test/java/com/baiyue/order/repository/OrderRepositoryIsolationUpdateTest.java index cd3e273a4..2ad521e39 100644 --- a/order/src/test/java/com/baiyue/order/repository/OrderRepositoryIsolationUpdateTest.java +++ b/order/src/test/java/com/baiyue/order/repository/OrderRepositoryIsolationUpdateTest.java @@ -147,7 +147,7 @@ class OrderRepositoryIsolationUpdateTest { ArgumentCaptor> wrapper = ArgumentCaptor.forClass(Wrapper.class); verify(mapper).selectList(wrapper.capture()); assertThat(wrapper.getValue().getSqlSegment()) - .contains("aggregate_type", "publish_status") + .contains("aggregate_type", "publish_status", "created_at", "id") .doesNotContain("event_type"); assertThat(params(wrapper.getValue()).values()) .contains("FIRST_LEG_ORDER", "PENDING"); diff --git a/scripts/admin-test-env-modules/phase3-loading.mjs b/scripts/admin-test-env-modules/phase3-loading.mjs index 10fd5475b..0c6973665 100644 --- a/scripts/admin-test-env-modules/phase3-loading.mjs +++ b/scripts/admin-test-env-modules/phase3-loading.mjs @@ -48,22 +48,27 @@ async function ensureMatsonSupplier(context, records) { } export function stableLoadedCartonMarks(items) { - const used = new Set() - return [...(Array.isArray(items) ? items : [])] + const groups = new Map() + for (const item of [...(Array.isArray(items) ? items : [])] .sort((left, right) => id(left?.loadingPlanItemId).localeCompare(id(right?.loadingPlanItemId))) - .flatMap((item) => { - const count = Math.max(0, Number(item?.pickedPackageCount) || 0) - const labels = [...new Set((item?.boxCodes || []) - .map((value) => String(value || '').trim()).filter((value) => value && !used.has(value)))] - // Partial loading keeps the lowest carton indexes on the loaded source inbound and - // transfers the highest indexes to the remainder child. Mirror that settlement rule - // instead of sampling labels, otherwise downstream PDA fixtures may reference cartons - // that were deliberately moved out of this container. - .sort((left, right) => left.localeCompare(right, undefined, { numeric: true, sensitivity: 'base' })) - .slice(0, count) - labels.forEach((value) => used.add(value)) - return labels - }) + ) { + const groupKey = id(item?.inboundId) || `item:${id(item?.loadingPlanItemId)}` + const group = groups.get(groupKey) || { count: 0, labels: new Set() } + group.count += Math.max(0, Number(item?.pickedPackageCount) || 0) + for (const value of item?.boxCodes || []) { + const label = String(value || '').trim() + if (label) group.labels.add(label) + } + groups.set(groupKey, group) + } + // Settlement transfers the highest carton indexes to the remainder child for the whole + // inbound, not per cargo line. Aggregate task rows by inbound before selecting the retained + // low indexes so the downstream fixture mirrors the persisted container manifest. + return [...groups.entries()] + .sort(([left], [right]) => left.localeCompare(right)) + .flatMap(([, group]) => [...group.labels] + .sort((left, right) => left.localeCompare(right, undefined, { numeric: true, sensitivity: 'base' })) + .slice(0, group.count)) } async function request(context, method, path, body, headers = {}) { diff --git a/scripts/admin-test-env-modules/phase3-producer-chain-contract.test.mjs b/scripts/admin-test-env-modules/phase3-producer-chain-contract.test.mjs index 5423819d5..f714dac3f 100644 --- a/scripts/admin-test-env-modules/phase3-producer-chain-contract.test.mjs +++ b/scripts/admin-test-env-modules/phase3-producer-chain-contract.test.mjs @@ -436,9 +436,12 @@ test('preload and loading versions use immediate allocation and claim baselines' test('loaded carton fixture mirrors partial settlement and keeps the lowest carton indexes', () => { const marks = stableLoadedCartonMarks([ - { loadingPlanItemId: '20', pickedPackageCount: 3, - boxCodes: ['NYEXX2609020358#8-5', 'NYEXX2609020358#8-2', 'NYEXX2609020358#8-1', 'NYEXX2609020358#8-3'] }, - { loadingPlanItemId: '10', pickedPackageCount: 1, boxCodes: ['NYEXX2609020357#4-4', 'NYEXX2609020357#4-1'] }, + { loadingPlanItemId: '30', inboundId: '200', pickedPackageCount: 1, + boxCodes: ['NYEXX2609020358#8-5'] }, + { loadingPlanItemId: '20', inboundId: '200', pickedPackageCount: 2, + boxCodes: ['NYEXX2609020358#8-2', 'NYEXX2609020358#8-1', 'NYEXX2609020358#8-3'] }, + { loadingPlanItemId: '10', inboundId: '100', pickedPackageCount: 1, + boxCodes: ['NYEXX2609020357#4-4', 'NYEXX2609020357#4-1'] }, ]) assert.deepEqual(marks, [ 'NYEXX2609020357#4-1',