新增一批一次性排查脚本(script/tmp-*.cjs),与仓库既有 script/tmp- 约定保持一致, 覆盖波次池边界与优先级核对、库位/出库报表核对、订单状态与更新范围核验等场景
105 lines
3.9 KiB
JavaScript
105 lines
3.9 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 一次性数据修复:解绑生产残留的拣货车绑定。
|
||
*
|
||
* 背景:修复前打印解绑核实逻辑对「波次含拦截订单」的场景永远不满足,
|
||
* 导致 wms_pick_task_cart_binding.is_active=1 残留(车未解锁)。
|
||
* 核对结论(2026-09-06):
|
||
* - 生产残留 39 条,全部 task.status=2(拣货完成待复核)、review_status=0;
|
||
* - 同车重复活跃绑定 0 条、已复核仍残留 0 条;
|
||
* - 关联订单已全部打印(散单/团购均无未打印订单)。
|
||
*
|
||
* 用法:
|
||
* node script/tmp-unbind-prod-cart.cjs # dry-run:核对范围与快照,不写库
|
||
* node script/tmp-unbind-prod-cart.cjs --apply # 执行解绑 UPDATE,并回读确认
|
||
*/
|
||
const path = require('path');
|
||
const { createRequire } = require('module');
|
||
|
||
(async () => {
|
||
const apply = process.argv.includes('--apply');
|
||
const projectRoot = path.resolve(__dirname, '..');
|
||
const projectRequire = createRequire(path.join(projectRoot, 'package.json'));
|
||
const mysql = projectRequire('mysql2/promise');
|
||
|
||
const configPath = path.join(projectRoot, 'script', 'db-query-prod-config.js');
|
||
const config = require(configPath);
|
||
const client = config && config.mysql && config.mysql.clients
|
||
&& config.mysql.clients.wms;
|
||
if (!client) {
|
||
throw new Error('生产连接配置缺失 (script/db-query-prod-config.js -> mysql.clients.wms)');
|
||
}
|
||
|
||
const connection = await mysql.createConnection({
|
||
host: client.host,
|
||
port: Number(client.port),
|
||
user: client.user,
|
||
password: client.password,
|
||
database: client.database,
|
||
charset: client.charset || 'utf8mb4',
|
||
});
|
||
|
||
try {
|
||
// 目标范围:活跃绑定且任务已结束(待复核/完成/取消)。当前核对全部 status=2。
|
||
const targetSql = `
|
||
SELECT
|
||
b.id, b.task_id, b.cart_id, b.cart_code_snapshot,
|
||
b.warehouse_id, b.bind_user_id,
|
||
DATE_FORMAT(b.bind_time, '%Y-%m-%d %H:%i') AS bind_time,
|
||
t.task_no, t.status AS task_status, t.review_status
|
||
FROM wms_pick_task_cart_binding b
|
||
JOIN wms_wave_pick_task t ON t.id = b.task_id
|
||
WHERE b.is_active = 1
|
||
AND t.status IN (2, 3, 9)
|
||
ORDER BY b.id ASC
|
||
`;
|
||
const [targetRows] = await connection.query(targetSql);
|
||
const targetIds = targetRows.map(row => Number(row.id));
|
||
|
||
console.log(`[unbind-prod-cart] 目标环境: ${client.host}:${client.port}/${client.database}`);
|
||
console.log(`[unbind-prod-cart] 命中活跃绑定: ${targetIds.length} 条`);
|
||
if (targetRows.length > 0) {
|
||
console.table(targetRows.slice(0, 50));
|
||
}
|
||
|
||
if (targetIds.length === 0) {
|
||
console.log('[unbind-prod-cart] 无待解绑记录,结束。');
|
||
return;
|
||
}
|
||
|
||
if (!apply) {
|
||
console.log('[unbind-prod-cart] dry-run 完成,未写库。确认无误后加 --apply 执行。');
|
||
return;
|
||
}
|
||
|
||
// 执行解绑(仅限命中范围,保留 is_active=1 条件防止并发误伤)
|
||
const [updateResult] = await connection.query(`
|
||
UPDATE wms_pick_task_cart_binding
|
||
SET is_active = 0,
|
||
unbind_user_id = 0,
|
||
unbind_time = NOW(),
|
||
update_time = NOW()
|
||
WHERE is_active = 1
|
||
AND id IN (?)
|
||
`, [ targetIds ]);
|
||
console.log(`[unbind-prod-cart] 解绑 UPDATE 完成,受影响行数: ${updateResult.affectedRows}`);
|
||
|
||
// 回读确认
|
||
const [remainingRows] = await connection.query(`
|
||
SELECT COUNT(*) AS cnt
|
||
FROM wms_pick_task_cart_binding b
|
||
JOIN wms_wave_pick_task t ON t.id = b.task_id
|
||
WHERE b.is_active = 1
|
||
AND t.status IN (2, 3, 9)
|
||
`);
|
||
const remainingCount = Number(remainingRows[0] && remainingRows[0].cnt || 0);
|
||
console.log(`[unbind-prod-cart] 回读确认:剩余活跃绑定 ${remainingCount} 条`);
|
||
} finally {
|
||
await connection.end();
|
||
}
|
||
})().catch(error => {
|
||
console.error('[unbind-prod-cart] 执行失败:', error && error.message ? error.message : error);
|
||
process.exitCode = 1;
|
||
});
|