- stock.check.report:space_id 必传(兼容 spaceId/location_id/locationId),校验仓库归属、分区有效、与 location 编码一致,缺失/非法/跨仓库/无头库位均返回 400;服务端不再按库位编码猜库位 - report:实盘数量上限 2147483647(原超限落库 Out of range 变 500);写库库位以 space_id 为准 - workData / workDataSpaceDetails / workDataDetail:space 视图行与 goods 视图 locations[] 补 space_id、location_id;space 视图记录库位归一(同编码不再出现幽灵行),明细按「库位+商品」索引、记录按「库位+商品」聚合 - 操作日志 STOCK_CHECK_REPORT:message 补库位编码/库位ID,before_changes 落库位与明细库位快照 - location.zone:createZone/updateZone 增加同仓库 zone_code 唯一校验(归档分区不占用编码,沿用规则编码),避免重复货架生成同编码库位 - 新增运维脚本(默认只读,--prod 指定生产):盘点库位一致性体检/订正(只改坏记录,两分支互斥)、作业记录指向已删除库位体检与 CSV 导出;无头库位引用统计改为分批 IN 查询,避免大表相关子查询导致生产连接重置 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
330 lines
15 KiB
JavaScript
330 lines
15 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 盘点库位一致性体检 / 订正脚本(wms_location_space 同编码重复 + 作业记录库位错挂)。
|
||
*
|
||
* 背景:uk_warehouse_space_code 为 (warehouse_id, space_code, deleted_at),MySQL 唯一索引不对
|
||
* NULL 去重,同编码可重复插入;分区删除后库位行未被软删 → 出现「无头库位」(zone 不存在/已删除)
|
||
* 与同编码多条并存。历史 stock.check.report 只按编码 findOne 猜库位,作业记录会挂到这些旧行上,
|
||
* 导致 stock.check.workData space 视图出现 custom_name 为 null 的幽灵行。
|
||
*
|
||
* 用法:
|
||
* node script/stock-check-location-consistency.cjs # 开发库体检(只读)
|
||
* node script/stock-check-location-consistency.cjs --prod # 生产库体检(只读)
|
||
* node script/stock-check-location-consistency.cjs --prod --apply-record # 订正作业记录 location_id
|
||
* node script/stock-check-location-consistency.cjs --prod --sql-out=temp/fix-location.sql # 输出清理 SQL(不执行)
|
||
*
|
||
* 说明:
|
||
* 1. 默认只读;只有显式 --apply-record 才写库,且仅处理「记录库位编码 == 明细库位编码」且
|
||
* 明细库位分区有效的安全场景(跨库位上报需人工确认,只统计不修改)。
|
||
* 2. 无头库位清理不直接执行:--sql-out 生成 SQL 文件(含引用计数过滤),人工审核后再跑。
|
||
*/
|
||
|
||
const path = require('path');
|
||
const fs = require('fs');
|
||
|
||
const PROJECT_ROOT = path.resolve(__dirname, '..');
|
||
|
||
/**
|
||
* 解析命令行参数。
|
||
* @return {Object} 选项。
|
||
*/
|
||
function parseArgs() {
|
||
const args = process.argv.slice(2);
|
||
const options = {
|
||
prod: false,
|
||
applyRecord: false,
|
||
sqlOut: '',
|
||
config: '',
|
||
};
|
||
for (const arg of args) {
|
||
if (arg === '--prod') {
|
||
options.prod = true;
|
||
} else if (arg === '--apply-record') {
|
||
options.applyRecord = true;
|
||
} else if (arg.startsWith('--sql-out=')) {
|
||
options.sqlOut = arg.slice('--sql-out='.length);
|
||
} else if (arg.startsWith('--config=')) {
|
||
options.config = arg.slice('--config='.length);
|
||
}
|
||
}
|
||
return options;
|
||
}
|
||
|
||
/**
|
||
* 读取数据库连接配置。
|
||
* @param {Object} options 命令行选项。
|
||
* @return {Object} mysql2 连接参数。
|
||
*/
|
||
function resolveClient(options) {
|
||
const configPath = options.config
|
||
? path.resolve(PROJECT_ROOT, options.config)
|
||
: path.resolve(PROJECT_ROOT, options.prod ? 'script/db-query-prod-config.js' : 'config/config.local.js');
|
||
// eslint-disable-next-line global-require
|
||
const configFactory = require(configPath);
|
||
const config = typeof configFactory === 'function' ? configFactory({ name: 'wms-script' }) : configFactory;
|
||
const client = config && config.mysql && config.mysql.clients && config.mysql.clients.wms;
|
||
if (!client) {
|
||
throw new Error(`未在 ${configPath} 找到 mysql.clients.wms 配置`);
|
||
}
|
||
return {
|
||
host: client.host,
|
||
port: Number(client.port),
|
||
user: client.user,
|
||
password: client.password,
|
||
database: client.database,
|
||
charset: client.charset || 'utf8mb4',
|
||
connectTimeout: 20000,
|
||
enableKeepAlive: true,
|
||
};
|
||
}
|
||
|
||
/** 引用库位ID的业务表(列名 → 表名),用于判断无头库位是否仍被引用 */
|
||
const SPACE_REFERENCE_COLUMNS = [
|
||
[ 'wms_storage', 'space_id' ],
|
||
[ 'wms_picking', 'space_id' ],
|
||
[ 'wms_receiving_detail', 'space_id' ],
|
||
[ 'wms_storage_transaction', 'space_id' ],
|
||
[ 'wms_inbound_direct_log', 'space_id' ],
|
||
[ 'wms_stock_check_detail', 'location_id' ],
|
||
[ 'wms_stock_check_record', 'location_id' ],
|
||
];
|
||
|
||
/** 分批查询的批大小(避免 IN 列表过长 / 单条大查询超时) */
|
||
const IN_CHUNK_SIZE = 500;
|
||
|
||
/**
|
||
* 数组分块。
|
||
* @param {Array} list 源数组。
|
||
* @param {number} size 每块大小。
|
||
* @return {Array<Array>} 分块结果。
|
||
*/
|
||
function chunk(list, size) {
|
||
const result = [];
|
||
for (let i = 0; i < list.length; i += size) {
|
||
result.push(list.slice(i, i + size));
|
||
}
|
||
return result;
|
||
}
|
||
|
||
/** 有效库位判定 SQL(分区存在且未删除) */
|
||
const VALID_SPACE_SQL = '(SELECT ls.id FROM wms_location_space ls '
|
||
+ 'JOIN wms_location_zone z ON z.id = ls.zone_id AND z.deleted_at IS NULL '
|
||
+ 'WHERE ls.deleted_at IS NULL)';
|
||
|
||
/**
|
||
* 记录当前库位不可用判定(为空 / 行不存在 / 已归档 / 所属分区不存在或已删除)。
|
||
* 订正只允许改动这类「坏记录」,已经是有效库位的记录一律不动。
|
||
*/
|
||
const BROKEN_RECORD_LOCATION_SQL = '(r.location_id IS NULL OR NOT EXISTS ('
|
||
+ 'SELECT 1 FROM wms_location_space cur '
|
||
+ 'JOIN wms_location_zone cz ON cz.id = cur.zone_id AND cz.deleted_at IS NULL '
|
||
+ 'WHERE cur.id = r.location_id AND cur.deleted_at IS NULL))';
|
||
|
||
/** 同编码订正目标表达式(明细固化库位,且分区有效) */
|
||
const SAME_CODE_TARGET_JOIN = 'JOIN wms_stock_check_detail d ON d.id = r.detail_id AND d.check_id = r.check_id '
|
||
+ 'JOIN wms_location_space good ON good.id = d.location_id AND good.deleted_at IS NULL '
|
||
+ 'JOIN wms_location_zone gz ON gz.id = good.zone_id AND gz.deleted_at IS NULL';
|
||
|
||
/** 跨编码订正目标(同仓库同编码、分区有效,取 id 最小);同时 JOIN 明细用于排除「编码本就等于明细编码」的记录 */
|
||
const CROSS_CODE_TARGET_JOIN = 'JOIN wms_stock_check_detail d ON d.id = r.detail_id AND d.check_id = r.check_id '
|
||
+ 'JOIN ('
|
||
+ 'SELECT ls.warehouse_id, ls.space_code, MIN(ls.id) target_id FROM wms_location_space ls '
|
||
+ 'JOIN wms_location_zone z ON z.id = ls.zone_id AND z.deleted_at IS NULL '
|
||
+ 'WHERE ls.deleted_at IS NULL GROUP BY ls.warehouse_id, ls.space_code'
|
||
+ ') t ON t.warehouse_id = r.warehouse_id AND t.space_code = r.location_code';
|
||
|
||
/**
|
||
* 主流程。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
async function main() {
|
||
const options = parseArgs();
|
||
// eslint-disable-next-line global-require
|
||
const mysql = require(path.join(PROJECT_ROOT, 'node_modules/mysql2/promise'));
|
||
const client = resolveClient(options);
|
||
const connection = await mysql.createConnection(client);
|
||
const summary = { target: `${client.host}:${client.port}/${client.database}`, applyRecord: options.applyRecord };
|
||
|
||
try {
|
||
const [ warehouses ] = await connection.query(
|
||
'SELECT warehouse_id, COUNT(*) space_cnt FROM wms_location_space WHERE deleted_at IS NULL GROUP BY warehouse_id'
|
||
);
|
||
summary.warehouseCount = warehouses.length;
|
||
|
||
// 1) 无头库位(分区不存在/已删除)ID 列表与总数
|
||
const [ headlessIdRows ] = await connection.query(
|
||
`SELECT ls.id FROM wms_location_space ls
|
||
WHERE ls.deleted_at IS NULL AND ls.id NOT IN ${VALID_SPACE_SQL}`
|
||
);
|
||
const headlessIds = headlessIdRows.map(row => Number(row.id)).filter(id => id > 0);
|
||
const headlessIdSet = new Set(headlessIds);
|
||
summary.headlessSpaceCount = headlessIds.length;
|
||
|
||
// 2) 同编码重复分组
|
||
const [ dupRows ] = await connection.query(
|
||
`SELECT COUNT(*) group_cnt, COALESCE(SUM(c), 0) rows_in_dup FROM (
|
||
SELECT warehouse_id, space_code, COUNT(*) c FROM wms_location_space
|
||
WHERE deleted_at IS NULL GROUP BY warehouse_id, space_code HAVING COUNT(*) > 1
|
||
) t`
|
||
);
|
||
summary.duplicateCodeGroups = Number(dupRows[0].group_cnt) || 0;
|
||
summary.rowsInDuplicateGroups = Number(dupRows[0].rows_in_dup) || 0;
|
||
|
||
// 3) 作业记录库位错挂统计(记录库位编码 == 明细库位编码,但 location_id 不同)
|
||
const [ sameCodeRows ] = await connection.query(
|
||
`SELECT COUNT(*) cnt,
|
||
SUM(CASE WHEN good.id IS NOT NULL THEN 1 ELSE 0 END) fixable
|
||
FROM wms_stock_check_record r
|
||
JOIN wms_stock_check_detail d ON d.id = r.detail_id AND d.check_id = r.check_id
|
||
LEFT JOIN wms_location_space good ON good.id = d.location_id AND good.deleted_at IS NULL
|
||
WHERE r.location_code = d.location_code
|
||
AND (r.location_id IS NULL OR r.location_id <> d.location_id)`
|
||
);
|
||
summary.sameCodeMismatchRecords = Number(sameCodeRows[0].cnt) || 0;
|
||
summary.sameCodeFixable = Number(sameCodeRows[0].fixable) || 0;
|
||
|
||
// 4) 跨库位上报(记录编码 != 明细编码)——需人工确认,不自动改
|
||
const [ crossCodeRows ] = await connection.query(
|
||
`SELECT COUNT(*) cnt FROM wms_stock_check_record r
|
||
JOIN wms_stock_check_detail d ON d.id = r.detail_id AND d.check_id = r.check_id
|
||
WHERE r.location_code <> d.location_code`
|
||
);
|
||
summary.crossCodeRecords = Number(crossCodeRows[0].cnt) || 0;
|
||
|
||
// 5) 无头库位中「无任何引用」的候选清理数:先取无头库位 id(小集合),再按表分批 IN 查引用,
|
||
// 避免在 wms_storage_transaction / wms_inbound_direct_log 等大表上做相关 NOT EXISTS 导致超时
|
||
const referencedHeadlessIds = new Set();
|
||
for (const [ table, column ] of SPACE_REFERENCE_COLUMNS) {
|
||
for (const ids of chunk(headlessIds, IN_CHUNK_SIZE)) {
|
||
const [ refRows ] = await connection.query(
|
||
`SELECT DISTINCT ${column} AS id FROM ${table} WHERE ${column} IN (?)`,
|
||
[ ids ]
|
||
);
|
||
for (const refRow of refRows) {
|
||
const refId = Number(refRow.id);
|
||
if (headlessIdSet.has(refId)) {
|
||
referencedHeadlessIds.add(refId);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
summary.headlessReferencedCount = referencedHeadlessIds.size;
|
||
summary.headlessOrphanCount = headlessIds.length - referencedHeadlessIds.size;
|
||
|
||
// 6) 需人工处理的重复编码 Top 10(按行数)
|
||
const [ topDup ] = await connection.query(
|
||
`SELECT warehouse_id, space_code, COUNT(*) c FROM wms_location_space
|
||
WHERE deleted_at IS NULL GROUP BY warehouse_id, space_code HAVING COUNT(*) > 1
|
||
ORDER BY c DESC, space_code ASC LIMIT 10`
|
||
);
|
||
summary.topDuplicates = topDup.map(row => ({
|
||
warehouse_id: Number(row.warehouse_id),
|
||
space_code: row.space_code,
|
||
rows: Number(row.c),
|
||
}));
|
||
|
||
// 7) 订正作业记录 location_id(仅安全场景,默认只读):
|
||
// a. 记录编码 == 明细编码 → 改挂明细固化库位(分区有效)
|
||
// b. 记录编码 != 明细编码(跨库位上报)→ 改挂「同仓库同编码、分区有效」的库位行(取 id 最小)
|
||
// 两条都只处理当前 location_id 不可用的坏记录(为空/不存在/已归档/分区已删),已经有效的记录不动
|
||
const [ drySameCode ] = await connection.query(
|
||
`SELECT COUNT(*) cnt FROM wms_stock_check_record r ${SAME_CODE_TARGET_JOIN}
|
||
WHERE r.location_code = d.location_code AND r.location_id <> d.location_id
|
||
AND ${BROKEN_RECORD_LOCATION_SQL}`
|
||
);
|
||
const [ dryCrossCode ] = await connection.query(
|
||
`SELECT COUNT(*) cnt FROM wms_stock_check_record r ${CROSS_CODE_TARGET_JOIN}
|
||
WHERE r.location_code <> d.location_code
|
||
AND r.location_id <> t.target_id AND ${BROKEN_RECORD_LOCATION_SQL}`
|
||
);
|
||
summary.wouldUpdateSameCode = Number(drySameCode[0].cnt) || 0;
|
||
summary.wouldUpdateCrossCode = Number(dryCrossCode[0].cnt) || 0;
|
||
|
||
if (options.applyRecord) {
|
||
const [ sameCodeResult ] = await connection.query(
|
||
`UPDATE wms_stock_check_record r ${SAME_CODE_TARGET_JOIN}
|
||
SET r.location_id = d.location_id
|
||
WHERE r.location_code = d.location_code
|
||
AND r.location_id <> d.location_id
|
||
AND ${BROKEN_RECORD_LOCATION_SQL}`
|
||
);
|
||
const [ crossCodeResult ] = await connection.query(
|
||
`UPDATE wms_stock_check_record r ${CROSS_CODE_TARGET_JOIN}
|
||
SET r.location_id = t.target_id
|
||
WHERE r.location_code <> d.location_code
|
||
AND r.location_id <> t.target_id
|
||
AND ${BROKEN_RECORD_LOCATION_SQL}`
|
||
);
|
||
summary.updatedRecords = (Number(sameCodeResult.affectedRows) || 0)
|
||
+ (Number(crossCodeResult.affectedRows) || 0);
|
||
summary.updatedRecordsSameCode = Number(sameCodeResult.affectedRows) || 0;
|
||
summary.updatedRecordsCrossCode = Number(crossCodeResult.affectedRows) || 0;
|
||
}
|
||
|
||
// 8) 生成无头库位清理 SQL(不执行)
|
||
if (options.sqlOut) {
|
||
const outPath = path.resolve(PROJECT_ROOT, options.sqlOut);
|
||
const sql = [
|
||
'-- 由 script/stock-check-location-consistency.cjs 生成,请人工审核后执行',
|
||
`-- target: ${summary.target}`,
|
||
'-- 1) 盘点作业记录 location_id 订正(只改当前库位不可用的坏记录:为空/不存在/已归档/分区已删)',
|
||
'-- 1.1 记录编码 == 明细编码 → 改挂明细固化库位(要求明细库位分区有效)',
|
||
'UPDATE wms_stock_check_record r',
|
||
' JOIN wms_stock_check_detail d ON d.id = r.detail_id AND d.check_id = r.check_id',
|
||
' JOIN wms_location_space good ON good.id = d.location_id AND good.deleted_at IS NULL',
|
||
' JOIN wms_location_zone gz ON gz.id = good.zone_id AND gz.deleted_at IS NULL',
|
||
' SET r.location_id = d.location_id',
|
||
' WHERE r.location_code = d.location_code',
|
||
' AND r.location_id <> d.location_id',
|
||
` AND ${BROKEN_RECORD_LOCATION_SQL.replace(/\n/g, ' ')};`,
|
||
'',
|
||
'-- 1.2 跨库位上报(记录编码 != 明细编码)→ 改挂同仓库同编码、分区有效的库位行(取 id 最小)',
|
||
'UPDATE wms_stock_check_record r',
|
||
' JOIN wms_stock_check_detail d ON d.id = r.detail_id AND d.check_id = r.check_id',
|
||
' JOIN (',
|
||
' SELECT ls.warehouse_id, ls.space_code, MIN(ls.id) target_id',
|
||
' FROM wms_location_space ls',
|
||
' JOIN wms_location_zone z ON z.id = ls.zone_id AND z.deleted_at IS NULL',
|
||
' WHERE ls.deleted_at IS NULL',
|
||
' GROUP BY ls.warehouse_id, ls.space_code',
|
||
' ) t ON t.warehouse_id = r.warehouse_id AND t.space_code = r.location_code',
|
||
' SET r.location_id = t.target_id',
|
||
' WHERE r.location_code <> d.location_code',
|
||
' AND r.location_id <> t.target_id',
|
||
` AND ${BROKEN_RECORD_LOCATION_SQL.replace(/\n/g, ' ')};`,
|
||
'',
|
||
'-- 2) 无头库位软删除(仅无任何业务引用的孤行;有引用的需人工合并到有效库位)',
|
||
'UPDATE wms_location_space ls',
|
||
' SET ls.deleted_at = NOW()',
|
||
' WHERE ls.deleted_at IS NULL',
|
||
` AND ls.id NOT IN ${VALID_SPACE_SQL}`,
|
||
' AND NOT EXISTS (SELECT 1 FROM wms_storage s WHERE s.space_id = ls.id)',
|
||
' AND NOT EXISTS (SELECT 1 FROM wms_picking p WHERE p.space_id = ls.id)',
|
||
' AND NOT EXISTS (SELECT 1 FROM wms_receiving_detail rd WHERE rd.space_id = ls.id)',
|
||
' AND NOT EXISTS (SELECT 1 FROM wms_storage_transaction st WHERE st.space_id = ls.id)',
|
||
' AND NOT EXISTS (SELECT 1 FROM wms_inbound_direct_log il WHERE il.space_id = ls.id)',
|
||
' AND NOT EXISTS (SELECT 1 FROM wms_stock_check_detail d WHERE d.location_id = ls.id)',
|
||
' AND NOT EXISTS (SELECT 1 FROM wms_stock_check_record r WHERE r.location_id = ls.id);',
|
||
'',
|
||
'-- 3) 清理后复查:仍重复的编码(需人工决定保留哪一行)',
|
||
'SELECT warehouse_id, space_code, COUNT(*) c FROM wms_location_space',
|
||
' WHERE deleted_at IS NULL GROUP BY warehouse_id, space_code HAVING COUNT(*) > 1;',
|
||
'',
|
||
].join('\n');
|
||
fs.mkdirSync(path.dirname(outPath), { recursive: true });
|
||
fs.writeFileSync(outPath, sql, 'utf8');
|
||
summary.sqlOut = outPath;
|
||
}
|
||
|
||
console.log(JSON.stringify(summary, null, 2));
|
||
} finally {
|
||
await connection.end();
|
||
}
|
||
}
|
||
|
||
main().catch(error => {
|
||
console.error('[stock-check-location-consistency] 执行失败:', error.message);
|
||
process.exit(1);
|
||
});
|