WMS_MAIN/script/tmp-verify-wave-pool-priority.cjs
liufumingren c3f1c67592 chore(script): 沉淀波次池/报表/库存/订单本地排查脚本
新增一批一次性排查脚本(script/tmp-*.cjs),与仓库既有 script/tmp- 约定保持一致,
覆盖波次池边界与优先级核对、库位/出库报表核对、订单状态与更新范围核验等场景
2026-09-13 15:31:27 +08:00

171 lines
5.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 订单池分割验证:抓取并验证「今日前订单优先、不掺入今天订单」。
*
* 流程:
* 1. 登录测试账号;
* 2. 取消当前活跃任务(wavePickTaskCancelSelf,若存在);
* 3. waveStrategyFetchPda 按策略 7 抓取(batch_max_orders=20);
* 4. 从库中读取新任务抓取的订单,校验 create_at 全部早于今日 0 点。
*
* @return {Promise<void>}
*/
const mysql = require('mysql2/promise');
const BASE_URL = 'http://127.0.0.1:6060';
const DB_CONFIG = {
host: '127.0.0.1',
port: 3307,
user: 'wms',
password: 'dspxmRwRdRbWtbm7',
database: 'wms',
};
/**
* 发送 /system POST 请求。
*
* @param {string} token 登录令牌。
* @param {string} method 方法名。
* @param {Object} data 请求体。
* @return {Promise<Object>} 响应 JSON。
*/
async function sysCall(token, method, data) {
const qs = `method=${encodeURIComponent(method)}&v=1.0&timestamp=${Math.floor(Date.now() / 1000)}`;
const headers = {
'Content-Type': 'application/json',
platform: 'web',
project: 'wms',
...(token ? { Authorization: `Bearer ${token}` } : {}),
};
const res = await fetch(`${BASE_URL}/system?${qs}`, {
method: 'POST',
headers,
body: JSON.stringify(data),
});
const text = await res.text();
try {
return JSON.parse(text);
} catch (error) {
return { raw: text };
}
}
/**
* 深度优先查找对象中第一个匹配 key 的非空值。
*
* @param {*} target 待查找对象。
* @param {string} key 目标字段名。
* @return {*} 匹配值或 undefined。
*/
function deepFind(target, key) {
if (target === null || target === undefined) {
return undefined;
}
if (Array.isArray(target)) {
for (const item of target) {
const found = deepFind(item, key);
if (found !== undefined) {
return found;
}
}
return undefined;
}
if (typeof target === 'object') {
for (const [ currentKey, value ] of Object.entries(target)) {
if (currentKey === key && value !== null && value !== undefined && value !== '') {
return value;
}
}
for (const value of Object.values(target)) {
const found = deepFind(value, key);
if (found !== undefined) {
return found;
}
}
}
return undefined;
}
/**
* 主流程入口。
*
* @return {Promise<void>}
*/
async function main() {
const login = await sysCall(null, 'user.login', {
username: 'liufumingren',
password: '111111',
});
const token = deepFind(login, 'token');
if (!token) {
throw new Error(`登录失败: ${JSON.stringify(login)}`);
}
console.log('[1] 登录成功');
// 取消活跃任务 260(若存在)
const cancelRes = await sysCall(token, 'order.wave.wavePickTaskCancelSelf', { task_id: 260 });
console.log(`[2] 取消任务260响应: ${JSON.stringify(cancelRes).slice(0, 200)}`);
// 按策略 7 抓取
const fetchRes = await sysCall(token, 'order.wave.waveStrategyFetchPda', { strategy_id: 7 });
console.log(`[3] 抓取响应: ${JSON.stringify(fetchRes).slice(0, 600)}`);
const fetchState = Number(deepFind(fetchRes, 'fetch_state') || 0);
const taskId = Number(deepFind(fetchRes, 'task_id') || 0);
if (fetchState !== 1 || taskId <= 0) {
throw new Error(`抓取未创建新任务: fetch_state=${fetchState}`);
}
// 从库读取该任务订单
const connection = await mysql.createConnection(DB_CONFIG);
try {
const [ taskOrderRows ] = await connection.query(
`SELECT order_id, wave_id, box_no
FROM wms_wave_pick_task_order
WHERE task_id = ?
ORDER BY box_no ASC`,
[ taskId ]
);
const orderIds = taskOrderRows.map(row => Number(row.order_id));
console.log(`[4] 任务 ${taskId} 抓取订单数: ${orderIds.length}`);
console.log(` 订单ID: [${orderIds.join(', ')}]`);
if (orderIds.length === 0) {
throw new Error('任务未关联任何订单');
}
const [ orderRows ] = await connection.query(
`SELECT id, create_at
FROM wms_order_delivery_100000
WHERE id IN (?)
ORDER BY id ASC`,
[ orderIds ]
);
// 今日 0 点(+08:00):2026-09-06 00:00 本地
const todayStart = Date.UTC(2026, 8, 5, 16, 0, 0);
const todayOrderIds = orderRows
.filter(row => new Date(row.create_at).getTime() >= todayStart)
.map(row => Number(row.id));
const beforeTodayCount = orderRows.length - todayOrderIds.length;
console.log('===== 验证结果 =====');
console.log(`任务抓取订单总数: ${orderRows.length}`);
console.log(`其中今日前订单数: ${beforeTodayCount}`);
console.log(`其中今天订单数: ${todayOrderIds.length}`);
if (todayOrderIds.length > 0) {
console.log(`混入的今天订单ID: [${todayOrderIds.join(', ')}]`);
}
const passed = beforeTodayCount === orderRows.length && orderRows.length > 0;
console.log(`结论: ${passed ? '✅ 今日前订单优先出库验证通过,抓取未掺入今天订单' : '❌ 抓取掺入了今天订单,池分割未生效'}`);
} finally {
await connection.end();
}
}
main().catch(error => {
console.error(`验证失败: ${error.message}`);
process.exit(1);
});