/** * 临时验证脚本:对比 stock.report.search 改造后的 SQL(聚合极值)与旧 SQL(全量明细+JS归并)在开发库的结果等价性。 * 用法:node script/tmp-verify-report-edge-sql.cjs * 验证完成后可删除。 */ 'use strict'; const path = require('path'); const { createRequire } = require('module'); const projectRoot = path.resolve(__dirname, '..'); const projectRequire = createRequire(path.join(projectRoot, 'package.json')); const mysql = projectRequire('mysql2/promise'); function buildOldEdgeMap(rows, keyFn) { const map = new Map(); for (const row of rows) { const key = keyFn(row); if (!map.has(key)) { map.set(key, { first_before: row.before_quantity, last_after: row.after_quantity, }); } else { map.get(key).last_after = row.after_quantity; } } return map; } function buildOldPreStartMap(rows, keyFn) { const latest = new Map(); for (const row of rows) { const key = keyFn(row); if (!key) continue; const ct = new Date(row.created_at).getTime(); const id = Number(row.id); const cur = latest.get(key); if (!cur || ct > cur.ct || (ct === cur.ct && id > cur.id)) { latest.set(key, { after_quantity: row.after_quantity, ct, id }); } } const map = new Map(); for (const [ key, v ] of latest.entries()) { map.set(key, v.after_quantity); } return map; } function diffMaps(label, oldMap, newMap) { const keys = new Set([ ...oldMap.keys(), ...newMap.keys() ]); const diffs = []; for (const key of keys) { const a = oldMap.get(key); const b = newMap.get(key); const isScalarOld = a === null || typeof a !== 'object'; const isScalarNew = b === null || typeof b !== 'object'; if (isScalarOld || isScalarNew) { const av = Number(a === undefined || a === null ? 0 : a); const bv = Number(b === undefined || b === null ? 0 : b); if (av !== bv) { diffs.push(`${key}: old=${JSON.stringify(a)} new=${JSON.stringify(b)}`); } continue; } const aFirst = a.first_before === undefined ? null : a.first_before; const aLast = a.last_after === undefined ? null : a.last_after; const bFirst = b.first_before === undefined ? null : b.first_before; const bLast = b.last_after === undefined ? null : b.last_after; if (Number(aFirst) !== Number(bFirst) || Number(aLast) !== Number(bLast)) { diffs.push(`${key}: old=(${aFirst},${aLast}) new=(${bFirst},${bLast})`); } } console.log(`[${label}] old keys=${oldMap.size}, new keys=${newMap.size}, diffs=${diffs.length}`); diffs.slice(0, 10).forEach(d => console.log(' DIFF ' + d)); return diffs.length === 0; } (async () => { const configFactory = require(path.join(projectRoot, 'config', 'config.local.js')); const config = typeof configFactory === 'function' ? configFactory({ name: 'wms-debug' }) : configFactory; const client = config.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: 'utf8mb4', }); try { const startTime = '2026-08-01 00:00:00'; const endTime = '2026-08-31 23:59:59'; // by_space=false 分支用更宽区间(覆盖区间内有流水的场景) const wideStart = '2026-05-01 00:00:00'; const wideEnd = '2026-09-03 00:00:00'; // ============ by_space=false(wms_stock_transaction) ============ const warehouseId = 1000028; const oldRangeParams = [ warehouseId, wideStart, wideEnd ]; const oldIntervalRows = (await connection.query( `SELECT t.id, t.stock_id, t.store_id, t.before_quantity, t.after_quantity, t.created_at FROM wms_stock_transaction t WHERE t.warehouse_id = ? AND t.created_at >= ? AND t.created_at <= ? ORDER BY t.stock_id ASC, t.store_id ASC, t.created_at ASC, t.id ASC`, oldRangeParams ))[0]; const newIntervalRows = (await connection.query( `SELECT g.stock_id, g.space_id, g.store_id, ft.before_quantity, lt.after_quantity, ft.created_at FROM ( SELECT t.stock_id, 0 AS space_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.warehouse_id = ? AND t.created_at >= ? AND t.created_at <= ? 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.warehouse_id = ? AND t2.created_at >= ? AND t2.created_at <= ? 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.warehouse_id = ? AND t3.created_at >= ? AND t3.created_at <= ? 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 ASC, g.space_id ASC, g.store_id ASC, g._min_ct ASC, g._max_ct ASC`, [ warehouseId, wideStart, wideEnd, warehouseId, wideStart, wideEnd, warehouseId, wideStart, wideEnd ] ))[0]; const oldEdge = buildOldEdgeMap(oldIntervalRows, r => `${r.stock_id}_${r.store_id}`); const newEdge = new Map(newIntervalRows.map(r => [ `${r.stock_id}_${r.store_id}`, { first_before: r.before_quantity, last_after: r.after_quantity }, ])); const edgeOk = diffMaps('edge by_space=false', oldEdge, newEdge); // 旧 preStart(全量历史 + JS 归并) const oldPreRows = (await connection.query( `SELECT t.id, t.stock_id, t.store_id, t.after_quantity, t.created_at FROM wms_stock_transaction t WHERE t.warehouse_id = ? AND t.created_at < ? ORDER BY t.stock_id ASC, t.store_id ASC, t.created_at DESC, t.id DESC`, [ warehouseId, startTime ] ))[0]; const newPreRows = (await connection.query( `SELECT g.stock_id, g.space_id, g.store_id, lt.after_quantity, lt.created_at FROM ( SELECT t.stock_id, 0 AS space_id, t.store_id, MAX(t.created_at) AS _max_ct FROM wms_stock_transaction t WHERE t.warehouse_id = ? AND t.created_at < ? GROUP BY t.stock_id, t.store_id ) g LEFT JOIN wms_stock_transaction lt ON lt.id = ( SELECT MAX(t3.id) FROM wms_stock_transaction t3 WHERE t3.warehouse_id = ? AND t3.created_at < ? 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 ASC, g.space_id ASC, g.store_id ASC, g._max_ct DESC`, [ warehouseId, startTime, warehouseId, startTime ] ))[0]; const oldPre = buildOldPreStartMap(oldPreRows, r => `${r.stock_id}_${r.store_id}`); const newPre = new Map(newPreRows.map(r => [ `${r.stock_id}_${r.store_id}`, r.after_quantity ])); const preOk = diffMaps('preStart by_space=false', oldPre, newPre); // ============ by_space=true(wms_storage_transaction) ============ const whRows = (await connection.query( 'SELECT warehouse_id, COUNT(*) AS cnt FROM wms_storage_transaction GROUP BY warehouse_id ORDER BY cnt DESC LIMIT 1' ))[0]; const storageWh = Number(whRows[0] && whRows[0].warehouse_id) || 0; console.log(`storage_transaction max warehouse_id=${storageWh}`); const oldSpaceRows = (await connection.query( `SELECT t.id, t.stock_id, COALESCE(t.space_id, 0) AS space_id, t.store_id, t.before_quantity, t.after_quantity, t.created_at FROM wms_storage_transaction t WHERE t.warehouse_id = ? AND t.created_at >= ? AND t.created_at <= ? ORDER BY t.stock_id ASC, COALESCE(t.space_id, 0) ASC, t.store_id ASC, t.created_at ASC, t.id ASC`, [ storageWh, startTime, endTime ] ))[0]; const newSpaceRows = (await connection.query( `SELECT g.stock_id, g.space_id, g.store_id, ft.before_quantity, lt.after_quantity, ft.created_at FROM ( SELECT t.stock_id, COALESCE(t.space_id, 0) AS space_id, t.store_id, MIN(t.created_at) AS _min_ct, MAX(t.created_at) AS _max_ct FROM wms_storage_transaction t WHERE t.warehouse_id = ? AND t.created_at >= ? AND t.created_at <= ? GROUP BY t.stock_id, COALESCE(t.space_id, 0), t.store_id ) g LEFT JOIN wms_storage_transaction ft ON ft.id = ( SELECT MIN(t2.id) FROM wms_storage_transaction t2 WHERE t2.warehouse_id = ? AND t2.created_at >= ? AND t2.created_at <= ? AND t2.stock_id = g.stock_id AND COALESCE(t2.space_id, 0) = g.space_id AND t2.store_id = g.store_id AND t2.created_at = g._min_ct ) LEFT JOIN wms_storage_transaction lt ON lt.id = ( SELECT MAX(t3.id) FROM wms_storage_transaction t3 WHERE t3.warehouse_id = ? AND t3.created_at >= ? AND t3.created_at <= ? AND t3.stock_id = g.stock_id AND COALESCE(t3.space_id, 0) = g.space_id AND t3.store_id = g.store_id AND t3.created_at = g._max_ct ) ORDER BY g.stock_id ASC, g.space_id ASC, g.store_id ASC, g._min_ct ASC, g._max_ct ASC`, [ storageWh, startTime, endTime, storageWh, startTime, endTime, storageWh, startTime, endTime ] ))[0]; const oldSpaceEdge = buildOldEdgeMap(oldSpaceRows, r => `${r.stock_id}_${r.space_id}_${r.store_id}`); const newSpaceEdge = new Map(newSpaceRows.map(r => [ `${r.stock_id}_${r.space_id}_${r.store_id}`, { first_before: r.before_quantity, last_after: r.after_quantity }, ])); const spaceEdgeOk = diffMaps('edge by_space=true', oldSpaceEdge, newSpaceEdge); const oldSpacePreRows = (await connection.query( `SELECT t.id, t.stock_id, COALESCE(t.space_id, 0) AS space_id, t.store_id, t.after_quantity, t.created_at FROM wms_storage_transaction t WHERE t.warehouse_id = ? AND t.created_at < ? ORDER BY t.stock_id ASC, COALESCE(t.space_id, 0) ASC, t.store_id ASC, t.created_at DESC, t.id DESC`, [ storageWh, startTime ] ))[0]; const newSpacePreRows = (await connection.query( `SELECT g.stock_id, g.space_id, g.store_id, lt.after_quantity, lt.created_at FROM ( SELECT t.stock_id, COALESCE(t.space_id, 0) AS space_id, t.store_id, MAX(t.created_at) AS _max_ct FROM wms_storage_transaction t WHERE t.warehouse_id = ? AND t.created_at < ? GROUP BY t.stock_id, COALESCE(t.space_id, 0), t.store_id ) g LEFT JOIN wms_storage_transaction lt ON lt.id = ( SELECT MAX(t3.id) FROM wms_storage_transaction t3 WHERE t3.warehouse_id = ? AND t3.created_at < ? AND t3.stock_id = g.stock_id AND COALESCE(t3.space_id, 0) = g.space_id AND t3.store_id = g.store_id AND t3.created_at = g._max_ct ) ORDER BY g.stock_id ASC, g.space_id ASC, g.store_id ASC, g._max_ct DESC`, [ storageWh, startTime, storageWh, startTime ] ))[0]; const oldSpacePre = buildOldPreStartMap(oldSpacePreRows, r => `${r.stock_id}_${r.space_id}_${r.store_id}`); const newSpacePre = new Map(newSpacePreRows.map(r => [ `${r.stock_id}_${r.space_id}_${r.store_id}`, r.after_quantity ])); const spacePreOk = diffMaps('preStart by_space=true', oldSpacePre, newSpacePre); // ============ EXPLAIN 新 intervalEdge SQL ============ const explainRows = (await connection.query( `EXPLAIN SELECT g.stock_id, g.space_id, g.store_id, ft.before_quantity, lt.after_quantity, ft.created_at FROM ( SELECT t.stock_id, 0 AS space_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.warehouse_id = ? AND t.created_at >= ? AND t.created_at <= ? 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.warehouse_id = ? AND t2.created_at >= ? AND t2.created_at <= ? 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.warehouse_id = ? AND t3.created_at >= ? AND t3.created_at <= ? AND t3.stock_id = g.stock_id AND t3.store_id = g.store_id AND t3.created_at = g._max_ct )`, [ warehouseId, startTime, endTime, warehouseId, startTime, endTime, warehouseId, startTime, endTime ] ))[0]; console.log('--- EXPLAIN new intervalEdge ---'); explainRows.forEach(r => console.log(` ${r.id} | ${r.select_type} | ${r.table} | type=${r.type} | possible_keys=${r.possible_keys} | key=${r.key} | rows=${r.rows}`)); const allOk = edgeOk && preOk && spaceEdgeOk && spacePreOk; console.log(allOk ? '\n=== ALL PASS ===' : '\n=== FAIL ==='); process.exit(allOk ? 0 : 1); } finally { await connection.end(); } })().catch(err => { console.error(err); process.exit(1); });