- tmp-test-putaway-full-compare.cjs:全链路真实接口对比(A 两步 quickPutaway→addItem→commit / B 一步 directPutaway),支持 A|B 参数单独跑 - tmp-test-putaway-batch-compare.cjs:批次维度对比(不同批次建行 / 同批次累加) - tmp-verify-all-tables.cjs:15 张表逐表核对输出 - tmp-check-*:条码、请求日志、通知失败表、权限、库存行/流水等定点核对 - tmp-clear-user-cache.cjs:清 Redis 用户缓存(权限变更后刷新) - 测试数据保留不清理(收货单 1521-1535、入库单 381-392 等)
282 lines
13 KiB
JavaScript
282 lines
13 KiB
JavaScript
'use strict';
|
||
/**
|
||
* 临时完整对比测试:
|
||
* 流程 A(两步):storage.inventory.quickPutaway(转入库单)
|
||
* → storage.entry.addItem(导入入库,绑定收货明细)
|
||
* → storage.entry.commit(提交确认写库存)
|
||
* 流程 B(一步):storage.inventory.directPutaway(直入库存)
|
||
* 收货单链路全部走真实接口。测试数据不清理。
|
||
* 用法:node script/tmp-test-putaway-full-compare.cjs [A|B]
|
||
*/
|
||
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 WAREHOUSE_ID = 100000;
|
||
const STORE_ID = 1000000;
|
||
|
||
const PRODUCTS = [
|
||
{ stock_id: 159061, item_code: '20260703', qty: 30, batch_number: 'TQB1', space_id: null },
|
||
{ stock_id: 159064, item_code: '544354', qty: 7, batch_number: 'TQB2', production_date: '2026-01-01', expiry_date: '2027-01-01', space_id: null },
|
||
];
|
||
|
||
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, extract) {
|
||
const bodyCode = res.body && res.body.code;
|
||
const ok = res.http === 200 && res.body && res.body.flag === 'success' && (bodyCode === 200 || bodyCode === 1);
|
||
let extra = '';
|
||
if (extract && res.body && res.body.data) {
|
||
try { extra = JSON.stringify(extract(res.body.data)); } catch (e) { /* ignore */ }
|
||
}
|
||
console.log(` [${ok ? 'OK' : 'FAIL'}] ${name} http=${res.http} code=${bodyCode} msg=${res.body && res.body.message} ${extra}`);
|
||
return ok;
|
||
}
|
||
|
||
async function verifyFlow(conn, tag, receivingId, entryOrderId, entryOrderCode, beforeStock) {
|
||
const stockIds = PRODUCTS.map(p => p.stock_id);
|
||
const [afterStock] = await conn.query(
|
||
`SELECT id, quantity, locked_quantity FROM wms_stock_warehouse WHERE id IN (?)`, [ stockIds ]
|
||
);
|
||
console.log(` ── [${tag}] 商品总库存(wms_stock_warehouse)──`);
|
||
for (const b of beforeStock) {
|
||
const a = afterStock.find(x => String(x.id) === String(b.id)) || { quantity: '?', locked_quantity: '?' };
|
||
console.log(` stock_id=${b.id} before=${b.quantity} after=${a.quantity} locked=${a.locked_quantity}`);
|
||
}
|
||
|
||
const [entryOrder] = await conn.query(
|
||
`SELECT id, entry_order_code, order_type, order_status, sync_status, total_order_lines FROM wms_entry_order WHERE id = ?`, [ entryOrderId ]
|
||
);
|
||
console.log(` ── [${tag}] 入库单(wms_entry_order)──`);
|
||
console.log(' ', JSON.stringify(entryOrder[0] || null));
|
||
|
||
const [entryItems] = await conn.query(
|
||
`SELECT order_line_no, item_code, stock_id, count, received_quantity, inventory_type FROM wms_entry_items WHERE order_id = ? ORDER BY order_line_no`, [ entryOrderId ]
|
||
);
|
||
console.log(` ── [${tag}] 入库单明细(wms_entry_items,共 ${entryItems.length} 行)──`);
|
||
console.log(' ', JSON.stringify(entryItems));
|
||
|
||
const [orderReceiving] = await conn.query(
|
||
`SELECT entry_order_id, receiving_id FROM wms_entry_order_receiving WHERE entry_order_id = ?`, [ entryOrderId ]
|
||
);
|
||
console.log(` ── [${tag}] 绑定(wms_entry_order_receiving)──`);
|
||
console.log(' ', JSON.stringify(orderReceiving));
|
||
|
||
const [receivingRow] = await conn.query(
|
||
`SELECT id, status, is_review, storage_status, detail_status FROM wms_receiving WHERE id = ?`, [ receivingId ]
|
||
);
|
||
console.log(` ── [${tag}] 收货单主表(wms_receiving)──`);
|
||
console.log(' ', JSON.stringify(receivingRow[0] || null));
|
||
|
||
const [receivingDetails] = await conn.query(
|
||
`SELECT id, system_product_id, actual_quantity, stored_quantity, storage_status, space_id, batch_number FROM wms_receiving_detail WHERE receiving_id = ? ORDER BY id`, [ receivingId ]
|
||
);
|
||
console.log(` ── [${tag}] 收货明细(wms_receiving_detail)──`);
|
||
console.log(' ', JSON.stringify(receivingDetails));
|
||
|
||
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, production_date, expiry_date
|
||
FROM wms_storage
|
||
WHERE entry_order_id = ? OR (inbound_order_no = ? AND receiving_id = ?)
|
||
ORDER BY id`, [ entryOrderId, entryOrderCode, receivingId ]
|
||
);
|
||
console.log(` ── [${tag}] 库位库存(wms_storage,共 ${storages.length} 行)──`);
|
||
console.log(' ', JSON.stringify(storages));
|
||
|
||
const [stockTxns] = await conn.query(
|
||
`SELECT transaction_type, stock_id, quantity, before_quantity, after_quantity, batch_no, order_type, order_no, receiving_id, entry_order_id
|
||
FROM wms_stock_transaction WHERE order_no = ? OR entry_order_id = ? ORDER BY id`, [ entryOrderCode, entryOrderId ]
|
||
);
|
||
console.log(` ── [${tag}] 商品流水(wms_stock_transaction,共 ${stockTxns.length} 行)──`);
|
||
console.log(' ', JSON.stringify(stockTxns));
|
||
|
||
const [storageTxns] = await conn.query(
|
||
`SELECT transaction_type, stock_id, storage_id, space_id, quantity, before_quantity, after_quantity, batch_no, order_type, order_no
|
||
FROM wms_storage_transaction WHERE order_no = ? OR order_id = ? ORDER BY id`, [ entryOrderCode, entryOrderId ]
|
||
);
|
||
console.log(` ── [${tag}] 库位流水(wms_storage_transaction,共 ${storageTxns.length} 行)──`);
|
||
console.log(' ', JSON.stringify(storageTxns));
|
||
|
||
const [directLogs] = await conn.query(
|
||
`SELECT id, stock_id, quantity, entry_item_id, receiving_detail_id, space_id, status, inbound_type
|
||
FROM wms_inbound_direct_log WHERE entry_order_id = ? ORDER BY id`, [ entryOrderId ]
|
||
);
|
||
console.log(` ── [${tag}] 导入入库记录(wms_inbound_direct_log,共 ${directLogs.length} 行)──`);
|
||
console.log(' ', JSON.stringify(directLogs));
|
||
|
||
const [opLogs] = await conn.query(
|
||
`SELECT business_type, operation_code, operator_name, message
|
||
FROM wms_operation_log WHERE business_id = ? AND operation_code IN
|
||
('ENTRY_ORDER_QUICK_PUTAWAY','ENTRY_ORDER_DIRECT_PUTAWAY','STORAGE_DIRECT_PUTAWAY','STOCK_DIRECT_PUTAWAY','DIRECT_INBOUND_ADD','DIRECT_INBOUND_COMMIT')
|
||
ORDER BY id`, [ String(entryOrderId) ]
|
||
);
|
||
console.log(` ── [${tag}] 本流程操作日志(wms_operation_log,共 ${opLogs.length} 行)──`);
|
||
console.log(' ', JSON.stringify(opLogs));
|
||
}
|
||
|
||
async function runFlow(tag, mode) {
|
||
console.log(`\n================ 流程 ${tag}(${mode}) ================`);
|
||
const conn = await db();
|
||
|
||
const stockIds = PRODUCTS.map(p => p.stock_id);
|
||
const [beforeStock] = await conn.query(
|
||
`SELECT id, quantity, locked_quantity FROM wms_stock_warehouse WHERE id IN (?)`, [ stockIds ]
|
||
);
|
||
|
||
// 收货单全链路
|
||
const createRes = await call('storage.receiving.create', {
|
||
store_id: STORE_ID,
|
||
delivery_method: 'other',
|
||
loading_method: 'with_pallet',
|
||
remark: `[完整对比测试-${tag}]`,
|
||
status: 'receiving',
|
||
});
|
||
const createBody = createRes.body || {};
|
||
const receivingId = createBody.data && createBody.data.id ? createBody.data.id : createBody.id;
|
||
const receivingNumber = (createBody.data && createBody.data.receiving_number) || createBody.receiving_number;
|
||
if (!step('创建收货单', createRes, () => ({ id: receivingId, receiving_number: receivingNumber }))) {
|
||
await conn.end(); return;
|
||
}
|
||
console.log(` receiving_id=${receivingId} receiving_number=${receivingNumber}`);
|
||
|
||
const detailIds = [];
|
||
for (let i = 0; i < PRODUCTS.length; i++) {
|
||
const p = PRODUCTS[i];
|
||
const addRes = await call('storage.receiving.addDetail', {
|
||
receiving_id: receivingId,
|
||
item: {
|
||
product_name: `完整对比测试商品${i + 1}`,
|
||
barcode: `FULLBC${Date.now()}${i}`,
|
||
actual_quantity: p.qty,
|
||
batch_number: p.batch_number,
|
||
production_date: p.production_date || null,
|
||
expiry_date: p.expiry_date || null,
|
||
space_id: p.space_id,
|
||
},
|
||
});
|
||
const addBody = addRes.body || {};
|
||
const addData = addBody.data || addBody;
|
||
const newDetailId = addData.selectId
|
||
|| (addData.details && Array.isArray(addData.details) && addData.details[0] && addData.details[0].id);
|
||
if (!step(`添加明细#${i + 1}`, addRes, () => ({ detail_id: newDetailId }))) { await conn.end(); return; }
|
||
detailIds.push(newDetailId);
|
||
}
|
||
|
||
for (let i = 0; i < PRODUCTS.length; i++) {
|
||
const bindRes = await call('storage.receiving.bindDetailProduct', {
|
||
detail_id: detailIds[i],
|
||
system_product_id: PRODUCTS[i].stock_id,
|
||
});
|
||
if (!step(`绑定商品#${i + 1}`, bindRes, d => ({ detail_id: d.detail_id, system_product_id: d.system_product_id }))) { await conn.end(); return; }
|
||
}
|
||
|
||
const completeRes = await call('storage.receiving.completeReceiving', { id: receivingId });
|
||
if (!step('完成收货', completeRes)) {
|
||
console.log(' completeReceiving 响应:', JSON.stringify(completeRes.body).slice(0, 500));
|
||
await conn.end(); return;
|
||
}
|
||
|
||
const reviewRes = await call('storage.receiving.reviewReceiving', { ids: [ receivingId ] });
|
||
if (!step('审核收货单', reviewRes)) {
|
||
console.log(' reviewReceiving 响应:', JSON.stringify(reviewRes.body).slice(0, 500));
|
||
await conn.end(); return;
|
||
}
|
||
|
||
let entryOrderId;
|
||
let entryOrderCode;
|
||
|
||
if (mode === 'A') {
|
||
// 两步流程第一步:转入库单
|
||
const putawayRes = await call('storage.inventory.quickPutaway', { receiving_id: receivingId });
|
||
if (!step('① quickPutaway(转入库单)', putawayRes, d => ({ entry_order_id: d.entry_order_id, entry_order_code: d.entry_order_code, order_status: d.order_status }))) {
|
||
console.log(' 响应:', JSON.stringify(putawayRes.body).slice(0, 600));
|
||
await conn.end(); return;
|
||
}
|
||
entryOrderId = putawayRes.body.data.entry_order_id;
|
||
entryOrderCode = putawayRes.body.data.entry_order_code;
|
||
|
||
// 两步流程第二步:导入入库(绑定收货明细 + 收货明细库位)
|
||
const addItemRes = await call('storage.entry.addItem', {
|
||
entry_order_id: entryOrderId,
|
||
items: [
|
||
{ stock_id: PRODUCTS[0].stock_id, quantity: PRODUCTS[0].qty, space_id: PRODUCTS[0].space_id, receiving_detail_id: detailIds[0], allocated_quantity: PRODUCTS[0].qty, inbound_type: 'pda' },
|
||
{ stock_id: PRODUCTS[1].stock_id, quantity: PRODUCTS[1].qty, space_id: PRODUCTS[1].space_id, receiving_detail_id: detailIds[1], allocated_quantity: PRODUCTS[1].qty, inbound_type: 'pda' },
|
||
],
|
||
});
|
||
if (!step('② storage.entry.addItem(导入入库,绑定收货明细)', addItemRes, d => ({ count: d.count, total_quantity: d.total_quantity }))) {
|
||
console.log(' 响应:', JSON.stringify(addItemRes.body).slice(0, 800));
|
||
await conn.end(); return;
|
||
}
|
||
|
||
// 两步流程第三步:提交确认写库存
|
||
const commitRes = await call('storage.entry.commit', { entry_order_id: entryOrderId });
|
||
if (!step('③ storage.entry.commit(提交确认入库)', commitRes, d => ({ success: d.success, message: d.message }))) {
|
||
console.log(' 响应:', JSON.stringify(commitRes.body).slice(0, 800));
|
||
await conn.end(); return;
|
||
}
|
||
} else {
|
||
// 一步直入
|
||
const directRes = await call('storage.inventory.directPutaway', { receiving_id: receivingId });
|
||
if (!step('① directPutaway(一步直入)', directRes, d => ({ entry_order_id: d.entry_order_id, entry_order_code: d.entry_order_code, order_status: d.order_status }))) {
|
||
console.log(' 响应:', JSON.stringify(directRes.body).slice(0, 600));
|
||
await conn.end(); return;
|
||
}
|
||
entryOrderId = directRes.body.data.entry_order_id;
|
||
entryOrderCode = directRes.body.data.entry_order_code;
|
||
}
|
||
|
||
await verifyFlow(conn, tag, receivingId, entryOrderId, entryOrderCode, beforeStock);
|
||
await conn.end();
|
||
console.log(`================ 流程 ${tag} 完成:receiving=${receivingId} entry_order=${entryOrderId} ================`);
|
||
}
|
||
|
||
async function main() {
|
||
const conn = await db();
|
||
const [spaces] = await conn.query(
|
||
`SELECT id, space_code FROM wms_location_space WHERE warehouse_id = ? AND is_enabled = 1 AND deleted_at IS NULL ORDER BY id LIMIT 2`,
|
||
[ WAREHOUSE_ID ]
|
||
);
|
||
console.log('使用库位:', JSON.stringify(spaces));
|
||
PRODUCTS[0].space_id = spaces[0].id;
|
||
PRODUCTS[1].space_id = spaces[1] ? spaces[1].id : spaces[0].id;
|
||
await conn.end();
|
||
|
||
const only = (process.argv[2] || '').toUpperCase();
|
||
if (!only || only === 'A') {
|
||
await runFlow('A-两步', 'A');
|
||
}
|
||
if (!only || only === 'B') {
|
||
await runFlow('B-一步', 'B');
|
||
}
|
||
|
||
console.log('\n================ 测试结束 ================');
|
||
}
|
||
|
||
main().then(() => process.exit(0)).catch(e => { console.error(e); process.exit(1); });
|