新增一批一次性排查脚本(script/tmp-*.cjs),与仓库既有 script/tmp- 约定保持一致, 覆盖波次池边界与优先级核对、库位/出库报表核对、订单状态与更新范围核验等场景
227 lines
9.2 KiB
JavaScript
227 lines
9.2 KiB
JavaScript
/**
|
||
* 临时只读核对脚本(生产库,仅 SELECT):
|
||
* 核对 stock.report.searchStore 报表期初/期末快照 SQL 的关联缺失问题。
|
||
*
|
||
* 用法(生产):
|
||
* $env:NODE_ENV='production'; node script/tmp-verify-stockreport-edge-prod.cjs
|
||
*/
|
||
|
||
'use strict';
|
||
|
||
const path = require('path');
|
||
|
||
const projectRoot = path.resolve(__dirname, '..');
|
||
const { createRequire } = require('module');
|
||
const projectRequire = createRequire(path.join(projectRoot, 'package.json'));
|
||
const mysql = projectRequire('mysql2/promise');
|
||
|
||
function loadProdDbConfig() {
|
||
const defaultConfigPath = path.join(projectRoot, 'config', 'config.default.js');
|
||
const defaultConfigFactory = require(defaultConfigPath);
|
||
const defaultConfig = typeof defaultConfigFactory === 'function'
|
||
? defaultConfigFactory({ name: 'tmp-verify-stockreport-edge', baseDir: projectRoot })
|
||
: defaultConfigFactory;
|
||
const sequelizeConfig = defaultConfig && defaultConfig.sequelize;
|
||
if (sequelizeConfig && sequelizeConfig.host) {
|
||
return {
|
||
host: sequelizeConfig.host,
|
||
port: Number(sequelizeConfig.port || 3306),
|
||
user: sequelizeConfig.username,
|
||
password: sequelizeConfig.password,
|
||
database: sequelizeConfig.database || 'wms',
|
||
};
|
||
}
|
||
throw new Error('无法解析生产数据库配置');
|
||
}
|
||
|
||
(async () => {
|
||
const dbConfig = loadProdDbConfig();
|
||
console.log('目标:', dbConfig.host + ':' + dbConfig.port + '/' + dbConfig.database, '(生产 config.default.js)');
|
||
|
||
const connection = await mysql.createConnection(dbConfig);
|
||
|
||
try {
|
||
const storeId = 1000166;
|
||
const warehouseId = 1000028;
|
||
const stockIds = [ 132556, 132603, 132604, 132610, 132611, 133746, 133747, 133752 ];
|
||
|
||
console.log('\n=== 1) 目标 SKU 的仓库绑定与当前库存 ===');
|
||
const [ swRows ] = await connection.query(
|
||
'SELECT id, store_id, warehouse_id, quantity, item_code, item_name FROM wms_stock_warehouse WHERE id IN (?) ORDER BY id',
|
||
[ stockIds ]
|
||
);
|
||
for (const row of swRows) {
|
||
console.log(JSON.stringify(row));
|
||
}
|
||
|
||
console.log('\n=== 2) 全部流水明细(wms_stock_transaction,按 SKU 分组) ===');
|
||
const [ flowRows ] = await connection.query(
|
||
`SELECT id, stock_id, transaction_type, quantity, before_quantity, after_quantity,
|
||
DATE_FORMAT(created_at, '%Y-%m-%d %H:%i:%s') AS created_at
|
||
FROM wms_stock_transaction
|
||
WHERE stock_id IN (?) AND store_id = ? AND warehouse_id = ?
|
||
ORDER BY stock_id, created_at, id
|
||
LIMIT 300`,
|
||
[ stockIds, storeId, warehouseId ]
|
||
);
|
||
let lastStockId = null;
|
||
for (const row of flowRows) {
|
||
if (row.stock_id !== lastStockId) {
|
||
console.log(`\n-- stock_id ${row.stock_id} --`);
|
||
lastStockId = row.stock_id;
|
||
}
|
||
console.log(
|
||
` id=${row.id} type=${row.transaction_type} qty=${row.quantity} before=${row.before_quantity} after=${row.after_quantity} at=${row.created_at}`
|
||
);
|
||
}
|
||
|
||
console.log('\n=== 3) 区间汇总(rangeRows 口径:INBOUND/OUTBOUND 求和) ===');
|
||
const [ rangeRows ] = await connection.query(
|
||
`SELECT t.stock_id,
|
||
SUM(CASE WHEN t.transaction_type = 'INBOUND' THEN t.quantity ELSE 0 END) AS inbound_total,
|
||
SUM(CASE WHEN t.transaction_type = 'OUTBOUND' THEN t.quantity ELSE 0 END) AS outbound_total
|
||
FROM wms_stock_transaction t
|
||
WHERE t.store_id = ? AND t.warehouse_id = ? AND t.stock_id IN (?)
|
||
GROUP BY t.stock_id, t.store_id
|
||
ORDER BY t.stock_id`,
|
||
[ storeId, warehouseId, stockIds ]
|
||
);
|
||
for (const row of rangeRows) {
|
||
console.log(JSON.stringify(row));
|
||
}
|
||
|
||
console.log('\n=== 4) 现有快照 SQL(缺 stock_id 关联,BUG 版本) vs 修正版 ===');
|
||
const [ buggy ] = await connection.query(
|
||
`SELECT g.stock_id,
|
||
ft.before_quantity AS buggy_begin,
|
||
lt.after_quantity AS buggy_end
|
||
FROM (
|
||
SELECT t.stock_id, t.store_id, MIN(t.created_at) AS _min_ct, MAX(t.created_at) AS _max_ct
|
||
FROM wms_stock_transaction t
|
||
WHERE t.store_id = ? AND t.warehouse_id = ? AND t.stock_id IN (?)
|
||
GROUP BY t.stock_id, t.store_id
|
||
) g
|
||
LEFT JOIN wms_stock_transaction ft
|
||
ON ft.id = (
|
||
SELECT MIN(t2.id) FROM wms_stock_transaction t2
|
||
WHERE t2.store_id = ? AND t2.warehouse_id = ? AND t2.stock_id IN (?)
|
||
AND t2.created_at = g._min_ct
|
||
)
|
||
LEFT JOIN wms_stock_transaction lt
|
||
ON lt.id = (
|
||
SELECT MAX(t3.id) FROM wms_stock_transaction t3
|
||
WHERE t3.store_id = ? AND t3.warehouse_id = ? AND t3.stock_id IN (?)
|
||
AND t3.created_at = g._max_ct
|
||
)
|
||
ORDER BY g.stock_id`,
|
||
[ storeId, warehouseId, stockIds, storeId, warehouseId, stockIds, storeId, warehouseId, stockIds ]
|
||
);
|
||
|
||
const [ fixed ] = await connection.query(
|
||
`SELECT g.stock_id,
|
||
ft.before_quantity AS fixed_begin,
|
||
lt.after_quantity AS fixed_end
|
||
FROM (
|
||
SELECT t.stock_id, t.store_id, MIN(t.created_at) AS _min_ct, MAX(t.created_at) AS _max_ct
|
||
FROM wms_stock_transaction t
|
||
WHERE t.store_id = ? AND t.warehouse_id = ? AND t.stock_id IN (?)
|
||
GROUP BY t.stock_id, t.store_id
|
||
) g
|
||
LEFT JOIN wms_stock_transaction ft
|
||
ON ft.id = (
|
||
SELECT MIN(t2.id) FROM wms_stock_transaction t2
|
||
WHERE t2.store_id = ? AND t2.warehouse_id = ? AND t2.stock_id IN (?)
|
||
AND t2.stock_id = g.stock_id AND t2.store_id = g.store_id
|
||
AND t2.created_at = g._min_ct
|
||
)
|
||
LEFT JOIN wms_stock_transaction lt
|
||
ON lt.id = (
|
||
SELECT MAX(t3.id) FROM wms_stock_transaction t3
|
||
WHERE t3.store_id = ? AND t3.warehouse_id = ? AND t3.stock_id IN (?)
|
||
AND t3.stock_id = g.stock_id AND t3.store_id = g.store_id
|
||
AND t3.created_at = g._max_ct
|
||
)
|
||
ORDER BY g.stock_id`,
|
||
[ storeId, warehouseId, stockIds, storeId, warehouseId, stockIds, storeId, warehouseId, stockIds ]
|
||
);
|
||
|
||
const buggyMap = new Map(buggy.map(r => [ r.stock_id, r ]));
|
||
console.log('stock_id | buggy_begin | buggy_end | fixed_begin | fixed_end');
|
||
for (const row of fixed) {
|
||
const b = buggyMap.get(row.stock_id) || {};
|
||
console.log(
|
||
`${row.stock_id} | ${b.buggy_begin} | ${b.buggy_end} | ${row.fixed_begin} | ${row.fixed_end}`
|
||
);
|
||
}
|
||
|
||
console.log('\n=== 5) 不带仓库过滤(货主全仓库口径,用户报表场景) ===');
|
||
const [ buggy2 ] = await connection.query(
|
||
`SELECT g.stock_id,
|
||
ft.before_quantity AS buggy_begin,
|
||
lt.after_quantity AS buggy_end
|
||
FROM (
|
||
SELECT t.stock_id, t.store_id, MIN(t.created_at) AS _min_ct, MAX(t.created_at) AS _max_ct
|
||
FROM wms_stock_transaction t
|
||
WHERE t.store_id = ? AND t.stock_id IN (?)
|
||
GROUP BY t.stock_id, t.store_id
|
||
) g
|
||
LEFT JOIN wms_stock_transaction ft
|
||
ON ft.id = (
|
||
SELECT MIN(t2.id) FROM wms_stock_transaction t2
|
||
WHERE t2.store_id = ? AND t2.stock_id IN (?)
|
||
AND t2.created_at = g._min_ct
|
||
)
|
||
LEFT JOIN wms_stock_transaction lt
|
||
ON lt.id = (
|
||
SELECT MAX(t3.id) FROM wms_stock_transaction t3
|
||
WHERE t3.store_id = ? AND t3.stock_id IN (?)
|
||
AND t3.created_at = g._max_ct
|
||
)
|
||
ORDER BY g.stock_id`,
|
||
[ storeId, stockIds, storeId, stockIds, storeId, stockIds ]
|
||
);
|
||
|
||
const [ fixed2 ] = await connection.query(
|
||
`SELECT g.stock_id,
|
||
ft.before_quantity AS fixed_begin,
|
||
lt.after_quantity AS fixed_end
|
||
FROM (
|
||
SELECT t.stock_id, t.store_id, MIN(t.created_at) AS _min_ct, MAX(t.created_at) AS _max_ct
|
||
FROM wms_stock_transaction t
|
||
WHERE t.store_id = ? AND t.stock_id IN (?)
|
||
GROUP BY t.stock_id, t.store_id
|
||
) g
|
||
LEFT JOIN wms_stock_transaction ft
|
||
ON ft.id = (
|
||
SELECT MIN(t2.id) FROM wms_stock_transaction t2
|
||
WHERE t2.store_id = ? AND t2.stock_id IN (?)
|
||
AND t2.stock_id = g.stock_id AND t2.store_id = g.store_id
|
||
AND t2.created_at = g._min_ct
|
||
)
|
||
LEFT JOIN wms_stock_transaction lt
|
||
ON lt.id = (
|
||
SELECT MAX(t3.id) FROM wms_stock_transaction t3
|
||
WHERE t3.store_id = ? AND t3.stock_id IN (?)
|
||
AND t3.stock_id = g.stock_id AND t3.store_id = g.store_id
|
||
AND t3.created_at = g._max_ct
|
||
)
|
||
ORDER BY g.stock_id`,
|
||
[ storeId, stockIds, storeId, stockIds, storeId, stockIds ]
|
||
);
|
||
|
||
const buggy2Map = new Map(buggy2.map(r => [ r.stock_id, r ]));
|
||
console.log('stock_id | buggy_begin | buggy_end | fixed_begin | fixed_end');
|
||
for (const row of fixed2) {
|
||
const b = buggy2Map.get(row.stock_id) || {};
|
||
console.log(
|
||
`${row.stock_id} | ${b.buggy_begin} | ${b.buggy_end} | ${row.fixed_begin} | ${row.fixed_end}`
|
||
);
|
||
}
|
||
} finally {
|
||
await connection.end();
|
||
}
|
||
})().catch(err => {
|
||
console.error(err && err.stack ? err.stack : String(err));
|
||
process.exit(1);
|
||
});
|