'use strict'; /** * 批次维度对比测试(真实接口): * 1. A 两步:批次 B-A-1 入库 10 件 → storage 建行 * 2. B 一步:批次 B-B-1 入库 10 件 → storage 建行(不同批次不互相影响) * 3. A 两步:批次 B-A-1 再入库 5 件 → 同批次累加到 15 * 4. B 一步:批次 B-B-1 再入库 5 件 → 同批次累加到 15 * 商品 159061 / 库位 5918。测试数据不清理。 */ const mysql = require('mysql2/promise'); const jwt = require('jsonwebtoken'); const BASE = 'http://127.0.0.1:6060'; const JWT_SECRET = 'E5#ex.g@,GfL_8?'; const ACCOUNT_ID = 1; const STORE_ID = 1000000; const STOCK_ID = 159061; const SPACE_ID = 5918; const token = jwt.sign({ id: ACCOUNT_ID }, JWT_SECRET, { expiresIn: 14400 }); async function db() { return mysql.createConnection({ host: '127.0.0.1', port: 3307, user: 'wms', password: 'dspxmRwRdRbWtbm7', database: 'wms' }); } async function call(method, data) { const res = await fetch(`${BASE}/system?method=${method}&v=1×tamp=${Date.now()}`, { method: 'POST', headers: { 'Content-Type': 'application/json', platform: 'web', project: 'all', authorization: `Bearer ${token}`, }, body: JSON.stringify(data || {}), }); const text = await res.text(); let parsed; try { parsed = JSON.parse(text); } catch { parsed = text; } return { http: res.status, body: parsed }; } function step(name, res) { const code = res.body && res.body.code; const ok = res.http === 200 && res.body && res.body.flag === 'success' && (code === 200 || code === 1); console.log(` [${ok ? 'OK' : 'FAIL'}] ${name} http=${res.http} code=${code} msg=${res.body && res.body.message}`); if (!ok) console.log(' 响应:', JSON.stringify(res.body).slice(0, 600)); return ok; } async function runFlow(tag, mode, batchNo, qty) { console.log(`\n===== ${tag}:批次=${batchNo} 数量=${qty} =====`); const conn = await db(); const [before] = await conn.query( `SELECT quantity FROM wms_stock_warehouse WHERE id = ?`, [ STOCK_ID ] ); // 收货单全链路 const createRes = await call('storage.receiving.create', { store_id: STORE_ID, delivery_method: 'other', loading_method: 'with_pallet', remark: `[批次对比-${tag}-${batchNo}]`, status: 'receiving', }); const b = createRes.body || {}; const receivingId = (b.data && b.data.id) || b.id; if (!step('创建收货单', createRes)) { await conn.end(); return null; } const addRes = await call('storage.receiving.addDetail', { receiving_id: receivingId, item: { product_name: '批次对比测试商品', barcode: `B${Date.now()}`, actual_quantity: qty, batch_number: batchNo, space_id: SPACE_ID }, }); const addBody = addRes.body || {}; const addData = addBody.data || addBody; const detailId = addData.selectId || (addData.details && addData.details[0] && addData.details[0].id); if (!step('添加明细', addRes)) { await conn.end(); return null; } if (!step('绑定商品', await call('storage.receiving.bindDetailProduct', { detail_id: detailId, system_product_id: STOCK_ID }))) { await conn.end(); return null; } if (!step('完成收货', await call('storage.receiving.completeReceiving', { id: receivingId }))) { await conn.end(); return null; } if (!step('审核', await call('storage.receiving.reviewReceiving', { ids: [ receivingId ] }))) { await conn.end(); return null; } let entryOrderId; if (mode === 'A') { const pRes = await call('storage.inventory.quickPutaway', { receiving_id: receivingId }); if (!step('quickPutaway', pRes)) { await conn.end(); return null; } entryOrderId = pRes.body.data.entry_order_id; if (!step('addItem(绑定收货明细)', await call('storage.entry.addItem', { entry_order_id: entryOrderId, items: [{ stock_id: STOCK_ID, quantity: qty, space_id: SPACE_ID, receiving_detail_id: detailId, allocated_quantity: qty, inbound_type: 'pda' }], }))) { await conn.end(); return null; } if (!step('commit', await call('storage.entry.commit', { entry_order_id: entryOrderId }))) { await conn.end(); return null; } } else { const dRes = await call('storage.inventory.directPutaway', { receiving_id: receivingId }); if (!step('directPutaway', dRes)) { await conn.end(); return null; } entryOrderId = dRes.body.data.entry_order_id; } const [after] = await conn.query( `SELECT quantity FROM wms_stock_warehouse WHERE id = ?`, [ STOCK_ID ] ); console.log(` ── wms_stock_warehouse ${STOCK_ID}: ${before[0].quantity} → ${after[0].quantity}(+${qty})`); const [storages] = await conn.query( `SELECT id, space_id, stock_id, batch_no, inventory_type, quantity, available_quantity, inbound_order_no, entry_order_id FROM wms_storage WHERE stock_id = ? AND space_id = ? ORDER BY batch_no`, [ STOCK_ID, SPACE_ID ] ); console.log(' ── wms_storage(商品 159061 / 库位 5918 全部行)──'); console.log(' ', JSON.stringify(storages)); await conn.end(); return { receivingId, entryOrderId }; } async function main() { await runFlow('① A-两步', 'A', 'B-A-1', 10); await runFlow('② B-一步', 'B', 'B-B-1', 10); await runFlow('③ A-两步(同批次累加)', 'A', 'B-A-1', 5); await runFlow('④ B-一步(同批次累加)', 'B', 'B-B-1', 5); const conn = await db(); const [storages] = await conn.query( `SELECT id, space_id, stock_id, batch_no, inventory_type, quantity, locked_quantity, available_quantity, inbound_order_no, receiving_id, entry_order_id FROM wms_storage WHERE stock_id = 159061 AND space_id = 5918 ORDER BY batch_no` ); console.log('\n========== 最终 wms_storage 对比(商品 159061 / 库位 5918) =========='); console.log(JSON.stringify(storages, null, 2)); const [txns] = await conn.query( `SELECT order_no, transaction_type, stock_id, storage_id, space_id, quantity, before_quantity, after_quantity, batch_no, order_type FROM wms_storage_transaction WHERE stock_id = 159061 AND space_id = 5918 AND batch_no IN ('B-A-1','B-B-1') ORDER BY id` ); console.log('\n========== 库位流水(批次 B-A-1 / B-B-1) =========='); console.log(JSON.stringify(txns, null, 2)); await conn.end(); console.log('\n================ 批次对比测试结束 ================'); } main().then(() => process.exit(0)).catch(e => { console.error(e); process.exit(1); });