2026-08-12 20:36:11 +08:00
|
|
|
const MODULE = 'manual-state-loading-pda'
|
|
|
|
|
const OPTIONS = { scenario: 'manual-loading-pda-state-matrix', deploymentBlocker: true }
|
|
|
|
|
const CONTAINERS = '/api/platform/v1/containers'
|
|
|
|
|
const SMART = '/api/platform/v1/first-leg/smart-loading'
|
|
|
|
|
const LOADING = '/api/platform/v1/first-leg'
|
|
|
|
|
const PDA = `${LOADING}/pda-loading`
|
|
|
|
|
const CONTAINER_STATUSES = ['WAIT_LOADING', 'EDITING', 'LOADING', 'COMPLETED']
|
|
|
|
|
const PRELOAD_STATUSES = ['EDITING', 'LOADING']
|
|
|
|
|
const PDA_STATUSES = ['UNASSIGNED', 'IN_PROGRESS', 'AWAITING_REVIEW', 'COMPLETED']
|
|
|
|
|
|
2026-09-03 18:13:00 +08:00
|
|
|
import { resolveOceanBookingAgent } from './ocean-booking-fixture.mjs'
|
2026-09-10 21:18:52 +08:00
|
|
|
import { channelGroup } from './smart-loading-channel-group.mjs'
|
2026-09-03 18:13:00 +08:00
|
|
|
|
2026-08-12 20:36:11 +08:00
|
|
|
function id(value) { return value == null ? '' : String(value) }
|
|
|
|
|
function records(value) { return Array.isArray(value?.records) ? value.records : [] }
|
|
|
|
|
function suffix(context, label) {
|
|
|
|
|
return `${String(context.marker).replace(/[^A-Za-z0-9]/g, '').slice(-10)}${label}`.slice(-20)
|
|
|
|
|
}
|
|
|
|
|
function operationId(context, label) {
|
|
|
|
|
return `${context.marker}-manual-loading-${label}-${crypto.randomUUID()}`.slice(0, 128)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function post(context, path, body, label) {
|
|
|
|
|
return context.request('POST', path, body, context.token,
|
|
|
|
|
{ 'Idempotency-Key': operationId(context, label) })
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-24 13:53:10 +08:00
|
|
|
async function ensureCompletionEvidence(context, chain, label) {
|
2026-09-02 13:59:05 +08:00
|
|
|
const taskId = id(chain.task?.id)
|
|
|
|
|
const warehouseId = id(chain.source?.warehouseId)
|
2026-08-24 13:53:10 +08:00
|
|
|
const png = Uint8Array.from([137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,0,1,0,0,0,1,8,6,0,0,0,31,21,196,137,0,0,0,13,73,68,65,84,8,215,99,248,207,192,240,31,0,5,0,1,255,137,153,61,29,0,0,0,0,73,69,78,68,174,66,96,130])
|
|
|
|
|
for (const evidenceType of ['PHOTO_STEP_1', 'PHOTO_STEP_2', 'PHOTO_STEP_3', 'PHOTO_STEP_4', 'SEAL']) {
|
2026-09-02 13:59:05 +08:00
|
|
|
const query = new URLSearchParams({ warehouseId, expectedVersion: '0', evidenceType,
|
|
|
|
|
remark: context.marker })
|
|
|
|
|
if (evidenceType === 'SEAL') query.set('sealNo', `SEAL-${suffix(context, label)}`.slice(0, 64))
|
|
|
|
|
const uploadPath = `${PDA}/tasks/${taskId}/evidence?${query}`
|
2026-08-24 13:53:10 +08:00
|
|
|
const form = new FormData()
|
|
|
|
|
form.append('file', new Blob([png], { type: 'image/png' }), `${label}-${evidenceType}.png`)
|
|
|
|
|
const rawUpload = await fetch(`${context.options.gateway}${uploadPath}`, {
|
2026-09-02 13:59:05 +08:00
|
|
|
method: 'POST', headers: { Accept: 'application/json', Authorization: `Bearer ${context.token}`,
|
|
|
|
|
'Idempotency-Key': operationId(context, `${label}-evidence-${evidenceType}`) },
|
2026-08-24 13:53:10 +08:00
|
|
|
body: form, signal: AbortSignal.timeout(60_000),
|
|
|
|
|
})
|
|
|
|
|
const uploadBody = await rawUpload.json()
|
|
|
|
|
const upload = { method: 'POST', path: uploadPath, http: rawUpload.status, body: uploadBody,
|
|
|
|
|
requestId: uploadBody?.requestId || rawUpload.headers.get('x-request-id'),
|
|
|
|
|
traceId: uploadBody?.traceId || rawUpload.headers.get('x-trace-id') }
|
2026-09-02 13:59:05 +08:00
|
|
|
const evidence = context.apiSuccess(MODULE, `${label} 上传并绑定 ${evidenceType} 装柜证据`, upload, '', OPTIONS)
|
|
|
|
|
const active = evidence?.evidences?.find?.((item) => item.evidenceType === evidenceType
|
|
|
|
|
&& item.status !== 'VOID')
|
|
|
|
|
if (!active?.evidenceId || id(evidence?.taskId) !== taskId) {
|
|
|
|
|
throw new Error(`${label} ${evidenceType} 未返回任务证据ID`)
|
|
|
|
|
}
|
2026-08-24 13:53:10 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-12 20:36:11 +08:00
|
|
|
async function prepareInbound(context, label) {
|
|
|
|
|
const originalMarker = context.marker
|
|
|
|
|
const fixtureMarker = `${originalMarker}-MAN-${label}`
|
|
|
|
|
const createdKeys = ['orders', 'ocrInbound', 'testInbound', 'inboundMeasurement', 'inboundWarehouseResources']
|
|
|
|
|
const contextKeys = ['inboundWeightOcr', 'ocrInbound', 'testInbound', 'createdInbound']
|
|
|
|
|
const createdSnapshot = new Map(createdKeys.map((key) => [key,
|
|
|
|
|
{ present: Object.hasOwn(context.createdResources, key), value: context.createdResources[key] }]))
|
|
|
|
|
const contextSnapshot = new Map(contextKeys.map((key) => [key,
|
|
|
|
|
{ present: Object.hasOwn(context.context, key), value: context.context[key] }]))
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
context.marker = fixtureMarker
|
|
|
|
|
const orders = await import(new URL('./orders.mjs', import.meta.url))
|
|
|
|
|
await orders.run(context)
|
|
|
|
|
const measurement = await import(new URL('./inbound-measurement.mjs', import.meta.url))
|
|
|
|
|
await measurement.run(context)
|
|
|
|
|
const measured = context.createdResources.inboundMeasurement
|
|
|
|
|
const order = context.createdResources.orders
|
2026-08-13 11:57:33 +08:00
|
|
|
if (!measured?.inboundId || !measured?.tenantId) throw new Error(`${label} 未生成独立 WAIT_SPLIT 入库单`)
|
2026-08-12 20:36:11 +08:00
|
|
|
return { marker: fixtureMarker, tenantId: id(measured.tenantId), inboundId: id(measured.inboundId),
|
|
|
|
|
orderId: id(order?.orderId) }
|
|
|
|
|
} finally {
|
|
|
|
|
context.marker = originalMarker
|
|
|
|
|
for (const [key, snapshot] of createdSnapshot) {
|
|
|
|
|
if (snapshot.present) context.createdResources[key] = snapshot.value
|
|
|
|
|
else delete context.createdResources[key]
|
|
|
|
|
}
|
|
|
|
|
for (const [key, snapshot] of contextSnapshot) {
|
|
|
|
|
if (snapshot.present) context.context[key] = snapshot.value
|
|
|
|
|
else delete context.context[key]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function readInbound(context, fixture) {
|
|
|
|
|
const response = await context.request('GET',
|
|
|
|
|
`/api/platform/inbounds/${fixture.inboundId}?tenantId=${encodeURIComponent(fixture.tenantId)}`)
|
|
|
|
|
const value = context.apiSuccess(MODULE, '读取人工装柜独立入库单', response, '', OPTIONS)
|
|
|
|
|
const warehouseId = id(value?.summary?.warehouseId)
|
|
|
|
|
context.assert(MODULE, '人工装柜来源入库单可独立排柜', response,
|
2026-08-13 14:55:50 +08:00
|
|
|
value?.summary?.processStatus === 'WAIT_SPLIT'
|
2026-08-13 11:57:33 +08:00
|
|
|
&& value?.summary?.measureReviewStatus === 'APPROVED'
|
2026-08-12 20:36:11 +08:00
|
|
|
&& Boolean(warehouseId),
|
|
|
|
|
`inboundId=${fixture.inboundId}, status=${value?.summary?.processStatus || 'missing'}`, OPTIONS)
|
|
|
|
|
return { ...fixture, detail: value, warehouseId }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function createConfirmedContainer(context, source, label) {
|
|
|
|
|
const value = suffix(context, label)
|
2026-08-31 12:13:20 +08:00
|
|
|
const containerNo = iso6346ContainerNumber(`${context.marker}-manual-${label}`)
|
2026-08-12 20:36:11 +08:00
|
|
|
const summary = source.detail.summary
|
2026-09-02 13:59:05 +08:00
|
|
|
const { ensureMatsonCarrierPartner } = await import('./carrier-partner-fixture.mjs')
|
|
|
|
|
const carrier = await ensureMatsonCarrierPartner(context, MODULE, OPTIONS)
|
|
|
|
|
if (!carrier?.id) throw new Error(`${label} 未取得 ACTIVE 船公司`)
|
2026-09-03 18:13:00 +08:00
|
|
|
const agent = await resolveOceanBookingAgent(context, MODULE, carrier.carrierCode, OPTIONS)
|
|
|
|
|
if (!agent) throw new Error(`${label} 未取得当前船公司关联的 ACTIVE 订舱代理`)
|
2026-08-12 20:36:11 +08:00
|
|
|
const draft = {
|
|
|
|
|
customerId: source.tenantId, customerName: summary.customerName || 'IT 测试客户',
|
2026-09-23 17:47:50 +08:00
|
|
|
// Ocean booking 当前契约接收稳定的渠道分组码;入库摘要仍保留真实渠道码用于候选匹配。
|
|
|
|
|
channelCode: channelGroup(summary.channelCode), warehouseId: source.warehouseId,
|
2026-08-17 13:24:48 +08:00
|
|
|
billOfLadingNo: `BL-${value}`.slice(0, 128),
|
2026-09-02 13:59:05 +08:00
|
|
|
shippingCompanyId: id(carrier.id), shippingCompanyName: carrier.name, carrierCode: carrier.carrierCode,
|
2026-08-24 13:48:16 +08:00
|
|
|
bookingAgentId: id(agent.id), bookingAgentName: agent.name, departurePortCode: 'CNSZX',
|
2026-08-15 09:35:52 +08:00
|
|
|
vesselName: `人工船-${value}`.slice(0, 128), voyageNo: `V-${value}`.slice(0, 64),
|
2026-08-12 20:36:11 +08:00
|
|
|
departurePortName: '深圳', destinationPortCode: 'USLAX', destinationPortName: '洛杉矶',
|
2026-09-15 10:37:34 +08:00
|
|
|
placeOfDelivery: 'LAX',
|
2026-08-15 09:35:52 +08:00
|
|
|
estimatedSailingAt: '2026-09-01T08:00:00', estimatedArrivalAt: '2026-09-20T08:00:00',
|
|
|
|
|
remark: `${context.marker}-${label}`,
|
2026-08-12 20:36:11 +08:00
|
|
|
parties: [{ partyType: 'SHIPPER', partyName: `发货方-${value}` },
|
|
|
|
|
{ partyType: 'CONSIGNEE', partyName: `收货方-${value}` },
|
|
|
|
|
{ partyType: 'NOTIFY_PARTY', partyName: `通知方-${value}` }],
|
2026-09-15 10:37:34 +08:00
|
|
|
containerRequirements: [{ containerType: '40HQ', containerKind: 'FCL', quantity: 1, capacityCbm: 68,
|
|
|
|
|
maxWeightKg: 19500 }],
|
2026-08-12 20:36:11 +08:00
|
|
|
}
|
2026-08-14 21:00:20 +08:00
|
|
|
const simulationResponse = await context.request('POST', CONTAINERS, {
|
2026-09-15 10:37:34 +08:00
|
|
|
channelGroupCode: channelGroup(summary.channelCode), warehouseId: source.warehouseId, containerType: '40HQ',
|
|
|
|
|
capacityCbm: 68, maxWeightKg: 19500,
|
2026-08-14 21:00:20 +08:00
|
|
|
})
|
|
|
|
|
const simulation = context.apiSuccess(MODULE, `${label} 新增内部模拟柜`, simulationResponse, '', OPTIONS)
|
|
|
|
|
const containerId = id(simulation?.id)
|
|
|
|
|
if (!containerId) throw new Error(`${label} 未生成内部模拟柜`)
|
2026-08-12 20:36:11 +08:00
|
|
|
const createdResponse = await post(context, '/api/platform/v1/ocean-bookings',
|
|
|
|
|
{ expectedVersion: 0, data: draft }, `${label}-booking-create`)
|
|
|
|
|
const created = context.apiSuccess(MODULE, `${label} 创建订舱`, createdResponse, '', OPTIONS)
|
|
|
|
|
const bookingId = id(created?.oceanBooking?.id)
|
2026-09-02 13:59:05 +08:00
|
|
|
if (!bookingId) throw new Error(`${label} 创建订舱未返回订舱ID`)
|
|
|
|
|
const documentForm = new FormData()
|
|
|
|
|
documentForm.append('expectedVersion', String(Number(created?.oceanBooking?.version)))
|
|
|
|
|
documentForm.append('documentType', 'BOOKING_CONFIRMATION')
|
|
|
|
|
documentForm.append('file', new Blob([`${context.marker}-${label}-BOOKING_CONFIRMATION`],
|
|
|
|
|
{ type: 'text/plain' }), `booking-confirmation-${value}.txt`)
|
|
|
|
|
const documentResponse = await context.requestMultipart('POST',
|
|
|
|
|
`/api/platform/v1/ocean-bookings/${bookingId}/documents/upload`, documentForm, context.token,
|
|
|
|
|
{ 'Idempotency-Key': operationId(context, `${label}-booking-document`) })
|
|
|
|
|
const document = context.apiSuccess(MODULE, `${label} 上传订舱确认文件`, documentResponse, '', OPTIONS)
|
|
|
|
|
const releaseVersion = Number(document?.bookingVersion)
|
|
|
|
|
if (!Number.isInteger(releaseVersion)) throw new Error(`${label} 订舱确认文件未返回有效版本`)
|
2026-08-18 16:39:39 +08:00
|
|
|
const releasedResponse = await post(context, `/api/platform/v1/ocean-bookings/${bookingId}/release`, {
|
2026-09-02 13:59:05 +08:00
|
|
|
expectedVersion: releaseVersion, reason: context.marker,
|
2026-08-18 16:39:39 +08:00
|
|
|
}, `${label}-booking-release`)
|
|
|
|
|
const confirmed = context.apiSuccess(MODULE, `${label} 放舱订舱`, releasedResponse, '', OPTIONS)
|
|
|
|
|
context.assert(MODULE, `${label} 订舱为 RELEASED 且不预生成真实柜`, releasedResponse,
|
|
|
|
|
confirmed?.oceanBooking?.status === 'RELEASED' && (!confirmed?.containers || confirmed.containers.length === 0),
|
2026-08-15 09:35:52 +08:00
|
|
|
`status=${confirmed?.oceanBooking?.status || 'missing'}`, OPTIONS)
|
2026-09-15 10:37:34 +08:00
|
|
|
const requirement = confirmed?.containerRequirements?.find?.((item) => item.containerType === '40HQ')
|
2026-08-18 16:39:39 +08:00
|
|
|
if (!requirement?.id) throw new Error(`${label} 放舱订舱未返回柜型需求`)
|
2026-08-14 21:00:20 +08:00
|
|
|
const linkedResponse = await context.request('POST', `${CONTAINERS}/${containerId}/ocean-booking-link`, {
|
2026-08-18 16:39:39 +08:00
|
|
|
oceanBookingId: bookingId, requirementId: id(requirement.id), containerNo,
|
|
|
|
|
expectedVersion: Number(simulation.version),
|
2026-08-14 21:00:20 +08:00
|
|
|
})
|
|
|
|
|
const linked = context.apiSuccess(MODULE, `${label} 关联模拟柜与真实订舱`, linkedResponse, '', OPTIONS)
|
|
|
|
|
if (id(linked?.id) !== containerId || linked?.cabinetNo !== simulation.cabinetNo) {
|
|
|
|
|
throw new Error(`${label} 关联真实订舱后内部柜次身份漂移`)
|
|
|
|
|
}
|
2026-08-18 16:39:39 +08:00
|
|
|
context.apiSuccess(MODULE, `${label} 放舱后关联真实柜号`, linkedResponse, '', OPTIONS)
|
2026-09-02 13:59:05 +08:00
|
|
|
return { containerId, containerNo }
|
2026-08-12 20:36:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function createPlanTask(context, label) {
|
|
|
|
|
const source = await readInbound(context, await prepareInbound(context, label))
|
2026-09-02 13:59:05 +08:00
|
|
|
const container = await createConfirmedContainer(context, source, label)
|
|
|
|
|
const { containerId, containerNo } = container
|
2026-08-12 20:36:11 +08:00
|
|
|
const candidateResponse = await context.request('GET',
|
|
|
|
|
`${SMART}/candidates?bookingId=${containerId}&warehouseId=${source.warehouseId}`)
|
|
|
|
|
const candidates = context.apiSuccess(MODULE, `${label} 查询同渠道候选货物`, candidateResponse, '', OPTIONS)
|
|
|
|
|
const candidate = Array.isArray(candidates)
|
|
|
|
|
? candidates.find((item) => id(item.inboundId) === source.inboundId && Number(item.availablePackageCount) > 0)
|
|
|
|
|
: null
|
|
|
|
|
if (!candidate?.inboundDetailId) throw new Error(`${label} 独立入库单未进入同渠道排柜候选`)
|
|
|
|
|
|
2026-09-02 13:03:03 +08:00
|
|
|
const planResponse = await context.request('GET', `${SMART}/plans/by-booking/${containerId}`)
|
|
|
|
|
let plan = context.apiSuccess(MODULE, `${label} 读取模拟柜自动创建的智能排柜计划`, planResponse, '', OPTIONS)
|
2026-08-12 20:36:11 +08:00
|
|
|
const assignResponse = await context.request('POST', `${SMART}/plans/${plan.id}/assignments`, {
|
|
|
|
|
inboundId: source.inboundId, inboundDetailId: id(candidate.inboundDetailId), packageCount: 1,
|
|
|
|
|
expectedVersion: Number(plan.version), operationId: operationId(context, `${label}-assign`),
|
|
|
|
|
})
|
|
|
|
|
plan = context.apiSuccess(MODULE, `${label} 分配独立候选货物`, assignResponse, '', OPTIONS)
|
2026-09-02 13:03:03 +08:00
|
|
|
const pushResponse = await context.request('GET', `${SMART}/plans/${plan.id}`)
|
|
|
|
|
plan = context.apiSuccess(MODULE, `${label} 分配后自动建立预装关联`, pushResponse, '', OPTIONS)
|
2026-08-15 17:40:01 +08:00
|
|
|
const planId = id(plan?.id)
|
|
|
|
|
const unconfirmedTaskResponse = await context.request('GET',
|
|
|
|
|
`${PDA}/tasks?loadingPlanId=${planId}&warehouseId=${source.warehouseId}`)
|
|
|
|
|
context.addStep(MODULE, `${label} 计划确认前不生成 PDA 任务`, unconfirmedTaskResponse,
|
|
|
|
|
unconfirmedTaskResponse.http === 200 && String(unconfirmedTaskResponse?.body?.code) === 'A0404',
|
|
|
|
|
`HTTP=${unconfirmedTaskResponse.http}, code=${unconfirmedTaskResponse?.body?.code ?? 'missing'}, requestId=${unconfirmedTaskResponse.requestId || 'missing'}, traceId=${unconfirmedTaskResponse.traceId || 'missing'}`, OPTIONS)
|
|
|
|
|
|
|
|
|
|
const confirmableResponse = await context.request('GET',
|
|
|
|
|
`${LOADING}/loading-plans/${planId}?warehouseId=${source.warehouseId}`)
|
|
|
|
|
const confirmable = context.apiSuccess(MODULE, `${label} 读取计划级确认即时版本`, confirmableResponse, '', OPTIONS)
|
|
|
|
|
const confirmableVersion = Number(confirmable?.summary?.version)
|
|
|
|
|
context.assert(MODULE, `${label} 整计划允许确认且 PDA 尚未就绪`, confirmableResponse,
|
|
|
|
|
Number.isInteger(confirmableVersion) && confirmable?.summary?.status === 'EDITING'
|
|
|
|
|
&& confirmable?.summary?.readiness?.canConfirm === true
|
|
|
|
|
&& confirmable?.summary?.readiness?.readyForPda === false
|
|
|
|
|
&& confirmable?.summary?.readiness?.pdaTaskId == null,
|
|
|
|
|
`version=${confirmable?.summary?.version ?? 'missing'}, canConfirm=${confirmable?.summary?.readiness?.canConfirm}, pdaTaskId=${id(confirmable?.summary?.readiness?.pdaTaskId) || 'none'}`, OPTIONS)
|
|
|
|
|
if (!Number.isInteger(confirmableVersion) || confirmable?.summary?.readiness?.canConfirm !== true) {
|
|
|
|
|
throw new Error(`${label} 计划尚未满足整计划确认门禁`)
|
|
|
|
|
}
|
2026-09-23 18:26:04 +08:00
|
|
|
const pdaOperatorsResponse = await context.request('GET',
|
|
|
|
|
`/api/platform/v1/iam/users/domestic-pda-operators?warehouseId=${encodeURIComponent(source.warehouseId)}`)
|
|
|
|
|
const pdaOperators = context.apiSuccess(MODULE, `${label} 查询国内头程 PDA 作业员候选`, pdaOperatorsResponse, '', OPTIONS)
|
|
|
|
|
const pdaOperator = (Array.isArray(pdaOperators) ? pdaOperators : pdaOperators?.records || [])[0]
|
|
|
|
|
context.assert(MODULE, `${label} 存在可用 PDA 作业员`, pdaOperatorsResponse,
|
|
|
|
|
Boolean(pdaOperator?.userId), `operatorId=${id(pdaOperator?.userId) || 'missing'}`, OPTIONS)
|
|
|
|
|
if (!pdaOperator?.userId) throw new Error(`${label} 缺少可用 PDA 作业员`)
|
2026-08-15 17:40:01 +08:00
|
|
|
|
|
|
|
|
const confirmPath = `${LOADING}/loading-plans/${planId}/confirm`
|
2026-09-23 18:32:23 +08:00
|
|
|
const confirmBody = Object.freeze({ expectedVersion: confirmableVersion, assigneeId: id(pdaOperator.userId) })
|
2026-08-15 17:40:01 +08:00
|
|
|
const confirmHeaders = Object.freeze({
|
|
|
|
|
'Idempotency-Key': operationId(context, `${label}-plan-confirm`),
|
|
|
|
|
})
|
|
|
|
|
const confirmResponse = await context.request('POST', confirmPath, confirmBody, context.token, confirmHeaders)
|
|
|
|
|
const confirmed = context.apiSuccess(MODULE, `${label} 计划级确认并生成唯一 PDA 任务`, confirmResponse, '', OPTIONS)
|
|
|
|
|
const confirmedTaskId = id(confirmed?.readiness?.pdaTaskId)
|
|
|
|
|
context.assert(MODULE, `${label} 计划确认返回 CONFIRMED 与 readyForPda`, confirmResponse,
|
|
|
|
|
id(confirmed?.planId) === planId && id(confirmed?.containerBookingId) === containerId
|
|
|
|
|
&& confirmed?.confirmationStatus === 'CONFIRMED'
|
|
|
|
|
&& confirmed?.readiness?.confirmationStatus === 'CONFIRMED'
|
|
|
|
|
&& confirmed?.readiness?.readyForPda === true && Boolean(confirmedTaskId)
|
|
|
|
|
&& Number(confirmed?.version) > confirmableVersion,
|
|
|
|
|
`confirmationStatus=${confirmed?.confirmationStatus || 'missing'}, readyForPda=${confirmed?.readiness?.readyForPda}, pdaTaskId=${confirmedTaskId || 'missing'}`, OPTIONS)
|
|
|
|
|
if (!confirmedTaskId || confirmed?.confirmationStatus !== 'CONFIRMED') {
|
|
|
|
|
throw new Error(`${label} 计划确认未生成唯一 PDA 任务`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const confirmReplay = await context.request('POST', confirmPath, confirmBody, context.token, confirmHeaders)
|
|
|
|
|
context.assert(MODULE, `${label} 计划确认同键同参精确重放`, confirmReplay,
|
|
|
|
|
confirmReplay.http === 200 && String(confirmReplay?.body?.code) === '200'
|
|
|
|
|
&& id(confirmReplay?.body?.data?.readiness?.pdaTaskId) === confirmedTaskId
|
|
|
|
|
&& Number(confirmReplay?.body?.data?.version) === Number(confirmed.version),
|
|
|
|
|
`HTTP=${confirmReplay.http}, code=${confirmReplay?.body?.code ?? 'missing'}, requestId=${confirmReplay.requestId || 'missing'}, traceId=${confirmReplay.traceId || 'missing'}`, OPTIONS)
|
|
|
|
|
|
2026-08-12 20:36:11 +08:00
|
|
|
const taskResponse = await context.request('GET',
|
2026-08-15 17:40:01 +08:00
|
|
|
`${PDA}/tasks?loadingPlanId=${planId}&warehouseId=${source.warehouseId}`)
|
2026-08-12 20:36:11 +08:00
|
|
|
const task = context.apiSuccess(MODULE, `${label} 读取 PDA 任务`, taskResponse, '', OPTIONS)
|
2026-08-15 17:40:01 +08:00
|
|
|
if (id(task?.id) !== confirmedTaskId || !task?.items?.[0]?.id) throw new Error(`${label} PDA 任务未生成或身份漂移`)
|
2026-08-12 20:36:11 +08:00
|
|
|
return { source, containerId, containerNo, plan, task }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function claim(context, chain, label) {
|
|
|
|
|
const response = await context.request('POST', `${PDA}/tasks/${chain.task.id}/claim`, {
|
|
|
|
|
expectedAssignmentVersion: Number(chain.task?.assignment?.version || 0),
|
|
|
|
|
operationId: operationId(context, `${label}-claim`),
|
|
|
|
|
})
|
|
|
|
|
chain.task = context.apiSuccess(MODULE, `${label} 领取 PDA 任务`, response, '', OPTIONS)
|
|
|
|
|
return chain
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function pickAll(context, chain, label) {
|
|
|
|
|
const snapshotResponse = await context.request('GET',
|
|
|
|
|
`${PDA}/tasks/${chain.task.id}/inventory-snapshot?warehouseId=${chain.source.warehouseId}`)
|
|
|
|
|
const snapshot = context.apiSuccess(MODULE, `${label} 获取 PDA 库存快照`, snapshotResponse, '', OPTIONS)
|
|
|
|
|
const response = await post(context, `${PDA}/tasks/${chain.task.id}/batch`, {
|
|
|
|
|
warehouseId: chain.source.warehouseId, loadingPlanId: id(chain.task.loadingPlanId),
|
2026-08-13 11:57:33 +08:00
|
|
|
containerBookingId: chain.containerId,
|
2026-08-12 20:36:11 +08:00
|
|
|
expectedVersion: Number(chain.task.version), inventorySnapshotToken: snapshot.snapshotToken,
|
|
|
|
|
items: chain.task.items.map((item) => ({ itemId: id(item.id), action: 'PICK',
|
|
|
|
|
pickedPackageCount: Number(item.plannedPackageCount), finalPickedPackageCount: null,
|
2026-09-04 12:18:36 +08:00
|
|
|
version: Number(item.version), reason: null })),
|
2026-08-12 20:36:11 +08:00
|
|
|
}, `${label}-batch`)
|
|
|
|
|
chain.task = context.apiSuccess(MODULE, `${label} 完成 PDA 拣货`, response, '', OPTIONS)
|
|
|
|
|
return chain
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function complete(context, chain, label) {
|
2026-08-24 13:53:10 +08:00
|
|
|
await ensureCompletionEvidence(context, chain, label)
|
2026-08-12 20:36:11 +08:00
|
|
|
const response = await context.request('POST', `${PDA}/tasks/${chain.task.id}/complete`, {
|
|
|
|
|
expectedTaskVersion: Number(chain.task.version), operationId: operationId(context, `${label}-complete`),
|
|
|
|
|
remark: `${context.marker} 人工完成状态样本`,
|
|
|
|
|
})
|
|
|
|
|
chain.task = context.apiSuccess(MODULE, `${label} 人工确认装柜完成`, response, '', OPTIONS)
|
2026-09-02 13:59:05 +08:00
|
|
|
for (let attempt = 0; attempt < 90 && chain.task?.taskStatus !== 'COMPLETED'
|
|
|
|
|
&& chain.task?.taskStatus !== 'COMPLETION_FAILED'; attempt += 1) {
|
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 1_000))
|
|
|
|
|
const detailResponse = await context.request('GET',
|
|
|
|
|
`${PDA}/tasks/${chain.task.id}?warehouseId=${encodeURIComponent(chain.source.warehouseId)}`)
|
|
|
|
|
if (detailResponse.http === 200 && String(detailResponse?.body?.code) === '200') {
|
|
|
|
|
chain.task = detailResponse.body?.data
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-08-12 20:36:11 +08:00
|
|
|
return chain
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function verifyContainer(context, status, fixture) {
|
2026-09-09 12:19:16 +08:00
|
|
|
const response = await context.request('GET', `${CONTAINERS}/domestic?scope=ACTIVE&status=${status}&page=1&size=100`)
|
2026-08-12 20:36:11 +08:00
|
|
|
const page = context.apiSuccess(MODULE, `${status} 货柜可按列表状态筛选`, response, '', OPTIONS)
|
|
|
|
|
context.assert(MODULE, `${status} 货柜样本保留`, response,
|
2026-09-09 12:19:16 +08:00
|
|
|
records(page).some((item) => id(item.container?.id) === fixture.id && item.container?.planStatus === status),
|
2026-08-12 20:36:11 +08:00
|
|
|
`id=${fixture.id}`, OPTIONS)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function verifyPlan(context, status, fixture) {
|
|
|
|
|
const response = await context.request('GET',
|
|
|
|
|
`${LOADING}/loading-plans?status=${status}&warehouseId=${fixture.warehouseId}&page=1&size=100`)
|
|
|
|
|
const page = context.apiSuccess(MODULE, `${status} 预装清单可筛选`, response, '', OPTIONS)
|
|
|
|
|
context.assert(MODULE, `${status} 预装清单样本保留`, response,
|
|
|
|
|
records(page).some((item) => id(item.planId) === fixture.id && item.status === status),
|
|
|
|
|
`planId=${fixture.id}`, OPTIONS)
|
|
|
|
|
const detailResponse = await context.request('GET',
|
|
|
|
|
`${LOADING}/loading-plans/${fixture.id}?warehouseId=${fixture.warehouseId}`)
|
|
|
|
|
const detail = context.apiSuccess(MODULE, `${status} 预装清单详情可打开`, detailResponse, '', OPTIONS)
|
|
|
|
|
context.assert(MODULE, `${status} 预装清单详情身份正确`, detailResponse,
|
|
|
|
|
id(detail?.summary?.planId) === fixture.id && detail?.summary?.status === status,
|
|
|
|
|
`planId=${fixture.id}, actual=${detail?.summary?.status || 'missing'}`, OPTIONS)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function verifyTask(context, status, fixture) {
|
|
|
|
|
const response = await context.request('GET',
|
|
|
|
|
`${PDA}/tasks/${fixture.id}?warehouseId=${fixture.warehouseId}`)
|
|
|
|
|
const task = context.apiSuccess(MODULE, `${status} PDA 任务可打开`, response, '', OPTIONS)
|
|
|
|
|
context.assert(MODULE, `${status} PDA 任务样本保留`, response,
|
|
|
|
|
id(task?.id) === fixture.id && task?.workflowStatus === status,
|
|
|
|
|
`taskId=${fixture.id}, actual=${task?.workflowStatus || 'missing'}`, OPTIONS)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function fixture(value, status, detailPath, extra = {}) {
|
|
|
|
|
return { id: id(value), status, detailPath, ...extra }
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-09 20:36:53 +08:00
|
|
|
async function registerClearanceAction(context, matrix) {
|
|
|
|
|
const documentsFixture = context.createdResources?.phase3ContainerDocuments
|
|
|
|
|
const containerId = id(documentsFixture?.containerBookingId)
|
|
|
|
|
const expectedDocumentId = id(documentsFixture?.documentIds?.DESTINATION_CLEARANCE)
|
|
|
|
|
context.assert(MODULE, '复用本轮离仓后已完成的目的港清关动作', null,
|
|
|
|
|
documentsFixture?.marker === context.marker && Boolean(containerId && expectedDocumentId),
|
|
|
|
|
`containerBookingId=${containerId || 'missing'}, documentId=${expectedDocumentId || 'missing'}`, OPTIONS)
|
|
|
|
|
if (!containerId || !expectedDocumentId || documentsFixture?.marker !== context.marker) return
|
2026-08-13 09:49:55 +08:00
|
|
|
|
2026-09-09 20:36:53 +08:00
|
|
|
const response = await context.request('GET', `${CONTAINERS}/${containerId}/clearance`)
|
|
|
|
|
const completed = context.apiSuccess(MODULE, '回读目的港清关动作验收事实', response, '', OPTIONS)
|
|
|
|
|
context.assert(MODULE, '目的港清关动作保持完成和版本事实', response,
|
|
|
|
|
id(completed?.id) === expectedDocumentId && completed?.documentType === 'DESTINATION_CLEARANCE'
|
|
|
|
|
&& Boolean(completed?.completedAt) && Number.isInteger(Number(completed?.version)),
|
|
|
|
|
`documentId=${id(completed?.id) || 'missing'}, completedAt=${completed?.completedAt || 'missing'}, version=${completed?.version ?? 'missing'}`, OPTIONS)
|
|
|
|
|
|
|
|
|
|
matrix.pages.containerLoading.actions = { clearance: fixture(containerId, 'COMPLETED',
|
|
|
|
|
`/container-loading/${containerId}/smart-loading`, {
|
|
|
|
|
documentId: id(completed?.id), documentVersion: Number(completed?.version),
|
|
|
|
|
completedAt: completed?.completedAt || null,
|
2026-08-13 09:49:55 +08:00
|
|
|
}) }
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-12 20:36:11 +08:00
|
|
|
export async function run(context, matrix = context.createdResources.manualStateMatrix) {
|
2026-08-14 23:14:11 +08:00
|
|
|
if (!matrix?.pages) {
|
|
|
|
|
context.addStep(MODULE, '人工装柜状态模块由人工矩阵统一编排', null, false,
|
|
|
|
|
'missing=manualStateMatrix.pages;请通过 manual-state-matrix 入口执行', OPTIONS)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-08-13 09:49:55 +08:00
|
|
|
matrix.pages.containerLoading = { requiredStatuses: CONTAINER_STATUSES,
|
|
|
|
|
requiredActions: ['clearance'], fixtures: {}, actions: {} }
|
2026-08-12 20:36:11 +08:00
|
|
|
matrix.pages.preloadPlans = { requiredStatuses: PRELOAD_STATUSES, fixtures: {} }
|
2026-08-13 09:49:55 +08:00
|
|
|
matrix.pages.loadingLists = { requiredStatuses: ['COMPLETED'],
|
2026-09-02 13:59:05 +08:00
|
|
|
requiredActions: [], fixtures: {}, actions: {} }
|
2026-08-12 20:36:11 +08:00
|
|
|
matrix.pages.pdaTasks = { requiredStatuses: PDA_STATUSES, fixtures: {} }
|
|
|
|
|
|
2026-08-13 11:57:33 +08:00
|
|
|
const waitingSource = await readInbound(context, await prepareInbound(context, 'WAIT'))
|
2026-09-02 13:59:05 +08:00
|
|
|
const waitingContainer = await createConfirmedContainer(context, waitingSource, 'WAIT')
|
|
|
|
|
matrix.pages.containerLoading.fixtures.WAIT_LOADING = fixture(waitingContainer.containerId, 'WAIT_LOADING',
|
|
|
|
|
`/container-loading?containerId=${waitingContainer.containerId}`)
|
2026-08-12 20:36:11 +08:00
|
|
|
|
|
|
|
|
const editing = await createPlanTask(context, 'EDIT')
|
|
|
|
|
const inProgress = await claim(context, await createPlanTask(context, 'PROGRESS'), 'PROGRESS')
|
|
|
|
|
const awaiting = await pickAll(context,
|
|
|
|
|
await claim(context, await createPlanTask(context, 'REVIEW'), 'REVIEW'), 'REVIEW')
|
|
|
|
|
const completed = await complete(context, await pickAll(context,
|
|
|
|
|
await claim(context, await createPlanTask(context, 'DONE'), 'DONE'), 'DONE'), 'DONE')
|
|
|
|
|
|
2026-08-24 14:08:17 +08:00
|
|
|
const loadingOrderId = id(inProgress.source?.detail?.summary?.orderId)
|
|
|
|
|
const loadingTenantId = id(inProgress.source?.tenantId)
|
|
|
|
|
const loadingOrderResponse = await context.request('GET',
|
|
|
|
|
`/api/platform/v1/orders/${encodeURIComponent(loadingOrderId)}?tenantId=${encodeURIComponent(loadingTenantId)}`)
|
|
|
|
|
const loadingOrder = context.apiSuccess(MODULE, '读取人工 PROGRESS 对应 LOADING 统一订单', loadingOrderResponse, '', OPTIONS)
|
|
|
|
|
context.assert(MODULE, '人工 PROGRESS 稳定提供统一订单 LOADING 样本', loadingOrderResponse,
|
|
|
|
|
id(loadingOrder?.summary?.orderId) === loadingOrderId && loadingOrder?.summary?.mainStatus === 'LOADING',
|
|
|
|
|
`orderId=${loadingOrderId || 'missing'}, status=${loadingOrder?.summary?.mainStatus || 'missing'}`, OPTIONS)
|
|
|
|
|
if (loadingOrderId && loadingOrder?.summary?.mainStatus === 'LOADING') {
|
|
|
|
|
matrix.pages.unifiedOrders.fixtures.LOADING = {
|
|
|
|
|
id: loadingOrderId, status: 'LOADING', inboundId: inProgress.source.inboundId,
|
|
|
|
|
detailPath: `/orders/${loadingOrderId}`,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-12 20:36:11 +08:00
|
|
|
const chains = { EDITING: editing, LOADING: inProgress, COMPLETED: completed }
|
|
|
|
|
for (const [status, chain] of Object.entries(chains)) {
|
|
|
|
|
matrix.pages.containerLoading.fixtures[status] = fixture(chain.containerId, status,
|
|
|
|
|
`/container-loading/${chain.containerId}/smart-loading`, { planId: id(chain.plan.id) })
|
|
|
|
|
}
|
|
|
|
|
for (const [status, chain] of Object.entries({ EDITING: editing, LOADING: inProgress })) {
|
|
|
|
|
matrix.pages.preloadPlans.fixtures[status] = fixture(chain.plan.id, status,
|
|
|
|
|
`/loading-plans/${chain.plan.id}?warehouseId=${chain.source.warehouseId}`,
|
|
|
|
|
{ warehouseId: chain.source.warehouseId })
|
|
|
|
|
}
|
|
|
|
|
matrix.pages.loadingLists.fixtures.COMPLETED = fixture(completed.plan.id, 'COMPLETED',
|
|
|
|
|
`/loading-lists?planId=${completed.plan.id}&warehouseId=${completed.source.warehouseId}`,
|
|
|
|
|
{ warehouseId: completed.source.warehouseId })
|
|
|
|
|
|
|
|
|
|
const taskChains = { UNASSIGNED: editing, IN_PROGRESS: inProgress, AWAITING_REVIEW: awaiting, COMPLETED: completed }
|
|
|
|
|
for (const [status, chain] of Object.entries(taskChains)) {
|
|
|
|
|
matrix.pages.pdaTasks.fixtures[status] = fixture(chain.task.id, status,
|
|
|
|
|
`/pages/picking/picking?taskId=${chain.task.id}&warehouseId=${chain.source.warehouseId}`,
|
|
|
|
|
{ warehouseId: chain.source.warehouseId, loadingPlanId: id(chain.plan.id) })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const [status, value] of Object.entries(matrix.pages.containerLoading.fixtures))
|
|
|
|
|
await verifyContainer(context, status, value)
|
|
|
|
|
for (const [status, value] of Object.entries(matrix.pages.preloadPlans.fixtures))
|
|
|
|
|
await verifyPlan(context, status, value)
|
|
|
|
|
for (const [status, value] of Object.entries(matrix.pages.pdaTasks.fixtures))
|
|
|
|
|
await verifyTask(context, status, value)
|
|
|
|
|
|
|
|
|
|
const listFixture = matrix.pages.loadingLists.fixtures.COMPLETED
|
|
|
|
|
const listResponse = await context.request('GET',
|
|
|
|
|
`${LOADING}/loading-lists?warehouseId=${listFixture.warehouseId}&page=1&size=100`)
|
|
|
|
|
const list = context.apiSuccess(MODULE, 'COMPLETED 装柜清单可从列表打开', listResponse, '', OPTIONS)
|
|
|
|
|
context.assert(MODULE, 'COMPLETED 装柜清单样本保留', listResponse,
|
|
|
|
|
records(list).some((item) => id(item.planId) === listFixture.id && item.status === 'COMPLETED'),
|
|
|
|
|
`planId=${listFixture.id}`, OPTIONS)
|
|
|
|
|
const listDetailResponse = await context.request('GET',
|
|
|
|
|
`${LOADING}/loading-lists/${listFixture.id}?warehouseId=${listFixture.warehouseId}`)
|
|
|
|
|
const listDetail = context.apiSuccess(MODULE, 'COMPLETED 装柜清单详情可打开', listDetailResponse, '', OPTIONS)
|
|
|
|
|
context.assert(MODULE, 'COMPLETED 装柜清单详情身份正确', listDetailResponse,
|
|
|
|
|
id(listDetail?.summary?.planId) === listFixture.id && listDetail?.summary?.status === 'COMPLETED',
|
|
|
|
|
`planId=${listFixture.id}, actual=${listDetail?.summary?.status || 'missing'}`, OPTIONS)
|
2026-08-13 09:49:55 +08:00
|
|
|
|
2026-09-09 20:36:53 +08:00
|
|
|
await registerClearanceAction(context, matrix)
|
2026-08-12 20:36:11 +08:00
|
|
|
}
|
2026-08-31 12:13:20 +08:00
|
|
|
import { iso6346ContainerNumber } from './iso6346-container-number.mjs'
|