- 取数统一为 fetchPickingBinSkuRows:一行 = 一个「仓位 × 商品编码」,列表与导出共用 商品日销 = 商品窗口整档销量 ÷ 天数(不做仓位分摊);同一商品占几个仓位都是同一等级 另输出分摊销量 = 商品销量 ÷ 占用同类型仓位数,供按行求和 - 列表页同步拆到商品粒度:新增商品编码列,去掉 SKU数列,搜索支持商品编码 - 汇总卡片主数值改为去重商品数,另显示明细行数;销量按去重商品口径统计 - 导出新增「分摊销量」「商品日销」列,表头口径标注更明确 - 性能:仓位类型选满时不拼 IN 过滤(避免参数嗅探走坏计划),全类型查询由超时降到约 30 秒 验证(基准日 2026-10-08、30 天、拣选位): - 明细 11871 行 / 去重商品 6821 个 / 库存合计 499141(与旧口径一致) - 跨多仓位的 2028 个商品,销量与等级不一致 0 个 - 导出行求和:分摊销量合计 96847 = 去重商品销量合计 96847(差异 -0.0017,浮点舍入) - 单测 9/9 通过;语法检查 182 文件通过;前端 eslint 无报错、vite build 成功
150 lines
5.7 KiB
JavaScript
150 lines
5.7 KiB
JavaScript
'use strict';
|
||
|
||
const test = require('node:test');
|
||
const assert = require('node:assert/strict');
|
||
|
||
const {
|
||
buildGradeCase,
|
||
buildInClause,
|
||
fetchPickingBinSkuRows,
|
||
} = require('../src/services/pickingBinSalesAnalysis');
|
||
const {
|
||
PICKING_BIN_GRADE_RULES,
|
||
PICKING_BIN_DEFAULT_DAYS,
|
||
PICKING_BIN_WINDOW_OPTIONS,
|
||
PICKING_BIN_DEFAULT_TYPES,
|
||
BIN_TYPES,
|
||
} = require('../src/config/pickingBinSalesAnalysis');
|
||
|
||
test('buildInClause 按位展开参数', () => {
|
||
const { clause, params } = buildInClause([101, 102], 'w');
|
||
assert.equal(clause, '@w0,@w1');
|
||
assert.deepEqual(params, { w0: 101, w1: 102 });
|
||
});
|
||
|
||
test('buildGradeCase 覆盖全部正向等级,并以销售=0 兜底', () => {
|
||
const expr = buildGradeCase();
|
||
PICKING_BIN_GRADE_RULES.filter((rule) => rule.lower > 0).forEach((rule) => {
|
||
assert.ok(expr.includes(`THEN '${rule.key}'`), `${rule.key} 未出现在分档表达式中`);
|
||
});
|
||
assert.ok(expr.includes("THEN 'slow2'"));
|
||
assert.ok(expr.includes("ELSE 'zero'"));
|
||
});
|
||
|
||
test('等级按商品销量判定(默认表达式取 s.salesQty,不含仓位汇总列)', () => {
|
||
const expr = buildGradeCase();
|
||
assert.ok(expr.includes('ISNULL(s.salesQty, 0) / @days'));
|
||
assert.ok(!expr.includes('x.salesQty'));
|
||
});
|
||
|
||
test('默认窗口为 30 天,默认只看拣选位', () => {
|
||
assert.equal(PICKING_BIN_DEFAULT_DAYS, 30);
|
||
assert.ok(PICKING_BIN_WINDOW_OPTIONS.includes(30));
|
||
assert.deepEqual(PICKING_BIN_DEFAULT_TYPES, ['拣选']);
|
||
assert.deepEqual(BIN_TYPES, ['拣选', '存货']);
|
||
});
|
||
|
||
test('fetchPickingBinSkuRows 首参不是数据库实例时抛错', async () => {
|
||
await assert.rejects(
|
||
() => fetchPickingBinSkuRows({}, [101], { shipFrom: 'a', shipTo: 'b', days: 30 }),
|
||
TypeError
|
||
);
|
||
});
|
||
|
||
test('fetchPickingBinSkuRows 缺失必要窗口参数时返回空数组,不打库', async () => {
|
||
let called = 0;
|
||
const db = { query: async () => { called += 1; return [{ skuId: 'S1' }]; } };
|
||
assert.deepEqual(await fetchPickingBinSkuRows(db, [], { shipFrom: 'a', shipTo: 'b', days: 30 }), []);
|
||
assert.deepEqual(await fetchPickingBinSkuRows(db, [101], { shipFrom: null, shipTo: 'b', days: 30 }), []);
|
||
assert.deepEqual(await fetchPickingBinSkuRows(db, [101], { shipFrom: 'a', shipTo: 'b', days: 0 }), []);
|
||
assert.equal(called, 0);
|
||
});
|
||
|
||
test('fetchPickingBinSkuRows 按仓位×商品输出,等级与日销来自商品整档销量', async () => {
|
||
let captured = null;
|
||
const db = {
|
||
query: async (sqlText, params) => {
|
||
captured = { sqlText, params };
|
||
return [
|
||
{
|
||
wmsCoId: 101, binName: 'XA01-01-01', binType: '拣选', skuId: 'SKU1',
|
||
stockQty: 12, skuCount: 2, salesQty: 600, allocatedSalesQty: 300,
|
||
dailySales: 20, gradeKey: 'hot',
|
||
},
|
||
{
|
||
wmsCoId: 101, binName: 'XA01-01-01', binType: '拣选', skuId: 'SKU2',
|
||
stockQty: 8, skuCount: 2, salesQty: 45, allocatedSalesQty: 22.5,
|
||
dailySales: 1.5, gradeKey: 'slow1',
|
||
},
|
||
];
|
||
},
|
||
};
|
||
|
||
const rows = await fetchPickingBinSkuRows(db, [101, 102], {
|
||
shipFrom: '2026-09-08 01:00:00',
|
||
shipTo: '2026-10-08 01:00:00',
|
||
days: 30,
|
||
binTypes: ['拣选'],
|
||
});
|
||
|
||
assert.equal(rows.length, 2);
|
||
assert.deepEqual(rows.map((r) => r.skuId), ['SKU1', 'SKU2']);
|
||
// 同一仓位两个商品拆两行,库存是各商品在该仓位的实际库存
|
||
assert.deepEqual(rows.map((r) => r.stockQty), [12, 8]);
|
||
// 等级按商品自身日销,同一仓位的多行可以不同
|
||
assert.deepEqual(rows.map((r) => r.gradeKey), ['hot', 'slow1']);
|
||
// 分摊销量 = 商品销量 ÷ 占用的同类型仓位数(用于按行求和)
|
||
assert.deepEqual(rows.map((r) => r.allocatedSalesQty), [300, 22.5]);
|
||
assert.deepEqual(rows[0], {
|
||
wmsCoId: '101',
|
||
binName: 'XA01-01-01',
|
||
binType: '拣选',
|
||
skuId: 'SKU1',
|
||
stockQty: 12,
|
||
skuCount: 2,
|
||
salesQty: 600,
|
||
allocatedSalesQty: 300,
|
||
dailySales: 20,
|
||
gradeKey: 'hot',
|
||
});
|
||
|
||
// 参数必须带上两个仓库、发货窗口与天数,以及仓位类型过滤
|
||
assert.equal(captured.params.w0, 101);
|
||
assert.equal(captured.params.w1, 102);
|
||
assert.equal(captured.params.v0, 101);
|
||
assert.equal(captured.params.shipFrom, '2026-09-08 01:00:00');
|
||
assert.equal(captured.params.shipTo, '2026-10-08 01:00:00');
|
||
assert.equal(captured.params.days, 30);
|
||
assert.equal(captured.params.t0, '拣选');
|
||
|
||
// 商品销量按占用仓位数分摊(供求和),但等级依据的是未分摊的商品销量
|
||
assert.ok(captured.sqlText.includes('/ NULLIF(sp.binCount, 0)'));
|
||
assert.ok(captured.sqlText.includes('ISNULL(s.salesQty, 0) / @days AS DECIMAL(18, 4)'));
|
||
// 仓位类型:bin_group 为空视为拣选位;只统计有库存的行
|
||
assert.ok(captured.sqlText.includes("THEN N'拣选'"));
|
||
assert.ok(captured.sqlText.includes('ISNULL(p.qty, 0) <> 0'));
|
||
assert.ok(captured.sqlText.includes('HAVING ABS(SUM(CAST(b.binSkuQty AS DECIMAL(18, 2)))) > 0.0001'));
|
||
});
|
||
|
||
test('fetchPickingBinSkuRows 不传类型时不过滤 binType', async () => {
|
||
let captured = null;
|
||
const db = {
|
||
query: async (sqlText, params) => { captured = { sqlText, params }; return []; },
|
||
};
|
||
await fetchPickingBinSkuRows(db, [101], {
|
||
shipFrom: 'a', shipTo: 'b', days: 15, binTypes: [],
|
||
});
|
||
assert.ok(!captured.sqlText.includes('b.binType IN'));
|
||
assert.equal(captured.params.t0, undefined);
|
||
});
|
||
|
||
test('gradeKey 缺失时兜底为 zero', async () => {
|
||
const db = { query: async () => [{ wmsCoId: 101, binName: 'X', binType: '拣选', skuId: 'S1', stockQty: 5 }] };
|
||
const rows = await fetchPickingBinSkuRows(db, [101], { shipFrom: 'a', shipTo: 'b', days: 15 });
|
||
assert.equal(rows[0].gradeKey, 'zero');
|
||
assert.equal(rows[0].salesQty, 0);
|
||
assert.equal(rows[0].allocatedSalesQty, 0);
|
||
assert.equal(rows[0].dailySales, 0);
|
||
assert.equal(rows[0].skuId, 'S1');
|
||
});
|