- 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 等)
262 lines
12 KiB
JavaScript
262 lines
12 KiB
JavaScript
'use strict';
|
||
/**
|
||
* 临时对比测试:quickPutaway(转入库单) vs directPutaway(直入库存)
|
||
* 全链路走真实接口:创建收货单 → 添加明细 → 绑定商品 → 完成收货 → 审核 → 上架。
|
||
* 测试数据不清理,保留以便核对。
|
||
* 用法:node script/tmp-test-putaway-compare.cjs
|
||
*/
|
||
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;
|
||
|
||
// 商品:159061 非批次非效期;159064 效期管理(非批次)
|
||
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 runFlow(tag, putawayMethod) {
|
||
console.log(`\n================ 流程 ${tag}(${putawayMethod}) ================`);
|
||
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 ]
|
||
);
|
||
|
||
// 1. 创建收货单
|
||
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 }))) {
|
||
console.log(' create 响应:', JSON.stringify(createBody).slice(0, 600));
|
||
return { conn, receivingId: 0 };
|
||
}
|
||
console.log(` receiving_id=${receivingId} receiving_number=${receivingNumber}`);
|
||
|
||
// 2. 添加明细(含库位)
|
||
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: `TMPBC${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 }))) {
|
||
console.log(' addDetail 响应:', JSON.stringify(addBody).slice(0, 600));
|
||
return { conn, receivingId };
|
||
}
|
||
detailIds.push(newDetailId);
|
||
}
|
||
|
||
// 3. 绑定系统商品
|
||
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 }))) return { conn, receivingId };
|
||
}
|
||
|
||
// 4. 完成收货
|
||
const completeRes = await call('storage.receiving.completeReceiving', { id: receivingId });
|
||
if (!step('完成收货', completeRes, d => ({ status: d.status || d.receiving_status }))) {
|
||
console.log(' completeReceiving 响应:', JSON.stringify(completeRes.body).slice(0, 500));
|
||
return { conn, receivingId };
|
||
}
|
||
|
||
// 5. 审核
|
||
const reviewRes = await call('storage.receiving.reviewReceiving', { ids: [ receivingId ] });
|
||
if (!step('审核收货单', reviewRes)) {
|
||
console.log(' reviewReceiving 响应:', JSON.stringify(reviewRes.body).slice(0, 500));
|
||
return { conn, receivingId };
|
||
}
|
||
|
||
// 6. 上架
|
||
const putawayRes = await call(putawayMethod, { receiving_id: receivingId });
|
||
if (!step(`上架(${putawayMethod})`, 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, 800));
|
||
return { conn, receivingId };
|
||
}
|
||
|
||
// 7. 对比验证数据
|
||
const entryOrderId = putawayRes.body.data.entry_order_id;
|
||
const entryOrderCode = putawayRes.body.data.entry_order_code;
|
||
|
||
const [afterStock] = await conn.query(
|
||
`SELECT id, quantity, locked_quantity FROM wms_stock_warehouse WHERE id IN (?)`, [ stockIds ]
|
||
);
|
||
console.log(' ── 商品总库存(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(' ── 入库单(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(' ── 入库单明细(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(' ── 绑定(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(' ── 收货单主表(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(' ── 收货明细(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(' ── 库位库存(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(' ── 商品流水(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(' ── 库位流水(wms_storage_transaction,共 ' + storageTxns.length + ' 行)──');
|
||
console.log(' ', JSON.stringify(storageTxns));
|
||
|
||
const [opLogs] = await conn.query(
|
||
`SELECT business_type, operation_code, business_id, operator_name, message
|
||
FROM wms_operation_log WHERE business_id = ? ORDER BY id`, [ String(entryOrderId) ]
|
||
);
|
||
console.log(' ── 操作日志(wms_operation_log,共 ' + opLogs.length + ' 行)──');
|
||
console.log(' ', JSON.stringify(opLogs));
|
||
|
||
await conn.end();
|
||
return { receivingId, entryOrderId, entryOrderCode };
|
||
}
|
||
|
||
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 3`,
|
||
[ 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;
|
||
|
||
// 开关状态:quick 已开(货主 1000000 配置为 1);direct 需开启
|
||
const [beforePolicy] = await conn.query(
|
||
`SELECT policy_config FROM wms_store_warehouse WHERE store_id = ? AND warehouse_id = ?`, [ STORE_ID, WAREHOUSE_ID ]
|
||
);
|
||
console.log('货主 policy_config:', JSON.stringify(beforePolicy[0] && beforePolicy[0].policy_config));
|
||
await conn.end();
|
||
|
||
// 流程 A:转入库单(可用 node script/tmp-test-putaway-compare.cjs A 单独跑)
|
||
if (!process.argv[2] || process.argv[2] === 'A') {
|
||
await runFlow('A-quickPutaway', 'storage.inventory.quickPutaway');
|
||
}
|
||
|
||
// 开启直入开关(测试准备,SQL 直接配置)
|
||
const conn2 = await db();
|
||
await conn2.query(
|
||
`UPDATE wms_store_warehouse SET policy_config = JSON_SET(COALESCE(policy_config,'{}'), '$.allow_receiving_direct_putaway', 1)
|
||
WHERE store_id = ? AND warehouse_id = ?`, [ STORE_ID, WAREHOUSE_ID ]
|
||
);
|
||
await conn2.end();
|
||
console.log('\n已开启 allow_receiving_direct_putaway=1(store=1000000, warehouse=100000)');
|
||
|
||
// 流程 B:直入库存
|
||
if (!process.argv[2] || process.argv[2] === 'B') {
|
||
await runFlow('B-directPutaway', 'storage.inventory.directPutaway');
|
||
}
|
||
|
||
console.log('\n================ 测试结束 ================');
|
||
}
|
||
|
||
main().then(() => process.exit(0)).catch(e => { console.error(e); process.exit(1); });
|