1
This commit is contained in:
parent
81785081a2
commit
61c530c0cc
@ -4,6 +4,30 @@ module.exports = app => {
|
||||
const { BIGINT, STRING, DATE, TINYINT, JSON: JSON_TYPE } = app.Sequelize;
|
||||
const DEFAULT_MAX_NOTICE_WEBHOOK_COUNT = 5;
|
||||
const DEFAULT_MAX_WAVE_QUANTITY = 500;
|
||||
const INVENTORY_WARNING_RULE_COLOR_MAP = Object.freeze({
|
||||
1: '#ff4d4f',
|
||||
2: '#ff7a45',
|
||||
3: '#faad14',
|
||||
4: '#fadb14',
|
||||
5: '#95de64',
|
||||
6: '#52c41a',
|
||||
});
|
||||
const DEFAULT_INVENTORY_WARNING_RULES = Object.freeze([
|
||||
Object.freeze({ name: '01级', key: INVENTORY_WARNING_RULE_COLOR_MAP[1], expire_days: 0, tip_text: '立即报废' }),
|
||||
Object.freeze({ name: '02级', key: INVENTORY_WARNING_RULE_COLOR_MAP[2], expire_days: 30, tip_text: '优先处理' }),
|
||||
Object.freeze({ name: '03级', key: INVENTORY_WARNING_RULE_COLOR_MAP[3], expire_days: 90, tip_text: '尽快处理' }),
|
||||
Object.freeze({ name: '04级', key: INVENTORY_WARNING_RULE_COLOR_MAP[4], expire_days: 180, tip_text: '建议处理' }),
|
||||
Object.freeze({ name: '05级', key: INVENTORY_WARNING_RULE_COLOR_MAP[5], expire_days: 365, tip_text: '库存关注' }),
|
||||
Object.freeze({ name: '06级', key: INVENTORY_WARNING_RULE_COLOR_MAP[6], expire_days: 999999, tip_text: '常规监控' }),
|
||||
]);
|
||||
|
||||
/**
|
||||
* 获取库存预警默认规则(深拷贝)。
|
||||
*
|
||||
* @return {Array<Object>} 默认库存预警规则。
|
||||
*/
|
||||
const getDefaultInventoryWarningRules = () => DEFAULT_INVENTORY_WARNING_RULES.map(rule => ({ ...rule }));
|
||||
|
||||
const DEFAULT_SUBJECT_CONFIG = Object.freeze({
|
||||
max_notice_webhook_count: DEFAULT_MAX_NOTICE_WEBHOOK_COUNT,
|
||||
max_wave_quantity: DEFAULT_MAX_WAVE_QUANTITY,
|
||||
@ -13,6 +37,7 @@ module.exports = app => {
|
||||
default_platform_code: 'TB',
|
||||
group_wave_prefix: 'B',
|
||||
delete_order_wave_prefix: 'A',
|
||||
inventory_warning_rules: DEFAULT_INVENTORY_WARNING_RULES,
|
||||
});
|
||||
|
||||
const normalizeSubjectConfigText = (value, defaultValue) => {
|
||||
@ -40,6 +65,57 @@ module.exports = app => {
|
||||
throw new Error(`${fieldName} 必须是布尔值`);
|
||||
};
|
||||
|
||||
/**
|
||||
* 规范化库存预警规则配置。
|
||||
*
|
||||
* @param {Array|null|undefined|string} rawRules 原始库存预警规则。
|
||||
* @return {Array<Object>} 规范化后的库存预警规则数组。
|
||||
*/
|
||||
const normalizeInventoryWarningRules = rawRules => {
|
||||
if (rawRules === undefined || rawRules === null || rawRules === '') {
|
||||
return getDefaultInventoryWarningRules();
|
||||
}
|
||||
|
||||
if (!Array.isArray(rawRules)) {
|
||||
throw new Error('subject_config.inventory_warning_rules 必须是数组');
|
||||
}
|
||||
|
||||
return rawRules.map((rule, index) => {
|
||||
const basePath = `subject_config.inventory_warning_rules[${index}]`;
|
||||
if (!rule || typeof rule !== 'object' || Array.isArray(rule)) {
|
||||
throw new Error(`${basePath} 必须是对象`);
|
||||
}
|
||||
|
||||
const name = String(rule.name === undefined || rule.name === null ? '' : rule.name).trim();
|
||||
if (!name) {
|
||||
throw new Error(`${basePath}.name 不能为空`);
|
||||
}
|
||||
|
||||
const key = String(rule.key === undefined || rule.key === null ? '' : rule.key).trim();
|
||||
if (!key) {
|
||||
throw new Error(`${basePath}.key 不能为空`);
|
||||
}
|
||||
|
||||
const rawExpireDays = rule.expire_days;
|
||||
if (rawExpireDays === undefined || rawExpireDays === null || rawExpireDays === '') {
|
||||
throw new Error(`${basePath}.expire_days 必须是非负整数`);
|
||||
}
|
||||
const expireDays = Number(rawExpireDays);
|
||||
if (!Number.isInteger(expireDays) || expireDays < 0) {
|
||||
throw new Error(`${basePath}.expire_days 必须是非负整数`);
|
||||
}
|
||||
|
||||
const tipText = rule.tip_text === undefined || rule.tip_text === null ? '' : String(rule.tip_text);
|
||||
|
||||
return {
|
||||
name,
|
||||
key,
|
||||
expire_days: expireDays,
|
||||
tip_text: tipText,
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* 规范化主体配置,确保默认值与字段约束一致。
|
||||
*
|
||||
@ -48,7 +124,10 @@ module.exports = app => {
|
||||
*/
|
||||
const normalizeSubjectConfig = rawValue => {
|
||||
if (rawValue === undefined || rawValue === null || rawValue === '') {
|
||||
return { ...DEFAULT_SUBJECT_CONFIG };
|
||||
return {
|
||||
...DEFAULT_SUBJECT_CONFIG,
|
||||
inventory_warning_rules: getDefaultInventoryWarningRules(),
|
||||
};
|
||||
}
|
||||
|
||||
let parsed = rawValue;
|
||||
@ -160,6 +239,8 @@ module.exports = app => {
|
||||
normalized.callback_strategy = strategy || 'async';
|
||||
}
|
||||
|
||||
normalized.inventory_warning_rules = normalizeInventoryWarningRules(parsed.inventory_warning_rules);
|
||||
|
||||
return normalized;
|
||||
};
|
||||
|
||||
@ -394,6 +475,7 @@ module.exports = app => {
|
||||
};
|
||||
|
||||
Store.DEFAULT_MAX_NOTICE_WEBHOOK_COUNT = DEFAULT_MAX_NOTICE_WEBHOOK_COUNT;
|
||||
Store.DEFAULT_INVENTORY_WARNING_RULES = DEFAULT_INVENTORY_WARNING_RULES;
|
||||
Store.DEFAULT_SUBJECT_CONFIG = DEFAULT_SUBJECT_CONFIG;
|
||||
Store.normalizeSubjectConfig = normalizeSubjectConfig;
|
||||
|
||||
|
||||
@ -1280,6 +1280,7 @@ class WaveService extends OrderBaseService {
|
||||
'wave_no',
|
||||
'wave_type',
|
||||
'wave_status',
|
||||
'source',
|
||||
'creator',
|
||||
'logistics_name',
|
||||
'item_part_type',
|
||||
@ -1367,6 +1368,16 @@ class WaveService extends OrderBaseService {
|
||||
appendLikeCondition('wave_no', 'wave_no', 'whereWaveNo');
|
||||
appendIntegerEqualCondition('wave_type', 'wave_type', 'whereWaveType');
|
||||
appendIntegerEqualCondition('wave_status', 'wave_status', 'whereWaveStatus');
|
||||
const source = normalizedWhere.source;
|
||||
if (source !== undefined && source !== null && source !== '') {
|
||||
const normalizedSource = Number(source);
|
||||
if (!Number.isInteger(normalizedSource) || ![ 0, 1, 2 ].includes(normalizedSource)) {
|
||||
throw new Error('source 参数不合法');
|
||||
}
|
||||
|
||||
conditions.push('source = :whereSource');
|
||||
replacements.whereSource = normalizedSource;
|
||||
}
|
||||
appendLikeCondition('creator', 'creator', 'whereCreator');
|
||||
appendLikeCondition('logistics_name', 'logistics_name', 'whereLogisticsName');
|
||||
appendLikeCondition('item_part', 'item_part', 'whereItemPart');
|
||||
|
||||
@ -28,6 +28,20 @@ class PrintService extends MainService {
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送打印任务到打印电脑。
|
||||
* 仅在 type=customize 时要求 template、page_width、page_height、template_content。
|
||||
* @param {Object} _data 打印请求参数
|
||||
* @param {string} _data.printer 打印机名称
|
||||
* @param {string} _data.type 打印类型,支持 customize/express
|
||||
* @param {Object} [_data.template] 打印模板对象(customize 必填)
|
||||
* @param {Object} _data.data 打印业务数据
|
||||
* @param {string} _data.computerId 打印电脑 ID
|
||||
* @param {string} [_data.timestamp] 业务时间戳
|
||||
* @param {number} [_data.count] 打印份数
|
||||
* @returns {Promise<Object>} 统一返回结构
|
||||
*/
|
||||
async send(_data) {
|
||||
const { ctx } = this;
|
||||
const warehouseId = ctx.user.store_id;
|
||||
@ -45,7 +59,19 @@ class PrintService extends MainService {
|
||||
let processedData = {};
|
||||
switch (type) {
|
||||
case 'customize': {
|
||||
if (!template || typeof template !== 'object' || Array.isArray(template)) {
|
||||
return this.fail('type=customize 时 template 参数不能为空', { code: 400 });
|
||||
}
|
||||
const { page_width, page_height, template_content } = template;
|
||||
if (page_width === undefined || page_width === null || page_width === '') {
|
||||
return this.fail('type=customize 时 page_width 参数不能为空', { code: 400 });
|
||||
}
|
||||
if (page_height === undefined || page_height === null || page_height === '') {
|
||||
return this.fail('type=customize 时 page_height 参数不能为空', { code: 400 });
|
||||
}
|
||||
if (!Array.isArray(template_content)) {
|
||||
return this.fail('type=customize 时 template_content 必须是数组', { code: 400 });
|
||||
}
|
||||
let htm = `<body style="position:relative; width:${page_width}mm; height:${page_height}mm;margin:0; padding:0;overflow:hidden;">`;
|
||||
for (const item of template_content) {
|
||||
let content = item.content;
|
||||
|
||||
@ -4,6 +4,14 @@ const { Service } = require('egg');
|
||||
|
||||
const INTERNAL_WAVE_STRATEGY_VERSION_KEY = 'wave_strategy_version';
|
||||
const DEFAULT_WAVE_STRATEGY_VERSION = 1;
|
||||
const DEFAULT_INVENTORY_WARNING_RULES = Object.freeze([
|
||||
Object.freeze({ name: '01级', key: '#ff4d4f', expire_days: 0, tip_text: '立即报废' }),
|
||||
Object.freeze({ name: '02级', key: '#ff7a45', expire_days: 30, tip_text: '优先处理' }),
|
||||
Object.freeze({ name: '03级', key: '#faad14', expire_days: 90, tip_text: '尽快处理' }),
|
||||
Object.freeze({ name: '04级', key: '#fadb14', expire_days: 180, tip_text: '建议处理' }),
|
||||
Object.freeze({ name: '05级', key: '#95de64', expire_days: 365, tip_text: '库存关注' }),
|
||||
Object.freeze({ name: '06级', key: '#52c41a', expire_days: 999999, tip_text: '常规监控' }),
|
||||
]);
|
||||
|
||||
// 仓库设置字段元信息(对应 wms_store.subject_config)
|
||||
const WAREHOUSE_SETTING_FIELDS = Object.freeze([
|
||||
@ -71,6 +79,20 @@ const WAREHOUSE_SETTING_FIELDS = Object.freeze([
|
||||
description: '散单波次编号前缀,最多 5 个字符,默认 A',
|
||||
maxLength: 5,
|
||||
}),
|
||||
Object.freeze({
|
||||
key: 'inventory_warning_rules',
|
||||
name: '库存预警配置',
|
||||
type: 'array',
|
||||
item_keys: [
|
||||
Object.freeze({ key: 'name', name: '名称', type: 'string', required: true, description: '预警等级名称' }),
|
||||
Object.freeze({ key: 'key', name: '颜色标识', type: 'color', required: true, description: '预警颜色值' }),
|
||||
Object.freeze({ key: 'expire_days', name: '过期天数', type: 'integer', required: true, description: '距过期天数阈值' }),
|
||||
Object.freeze({ key: 'tip_text', name: '提示语', type: 'string', required: false, description: '预警提示文案' }),
|
||||
],
|
||||
default: DEFAULT_INVENTORY_WARNING_RULES,
|
||||
required: false,
|
||||
description: '可配置多个库存预警项,每项包含 name、key、expire_days、tip_text',
|
||||
}),
|
||||
Object.freeze({
|
||||
key: 'webhook_enabled',
|
||||
name: '是否启用 Webhook',
|
||||
@ -311,6 +333,13 @@ class SettingWarehouseService extends Service {
|
||||
key: field.key,
|
||||
name: field.name,
|
||||
type: field.type,
|
||||
...(field.item_keys !== undefined
|
||||
? {
|
||||
item_keys: Array.isArray(field.item_keys)
|
||||
? field.item_keys.map(itemKey => (itemKey && typeof itemKey === 'object' ? { ...itemKey } : itemKey))
|
||||
: field.item_keys,
|
||||
}
|
||||
: {}),
|
||||
default: field.default,
|
||||
required: field.required,
|
||||
description: field.description,
|
||||
|
||||
@ -25,6 +25,87 @@ function getShelfLifeWarningState(stock = {}, expiryDate = null, nowMs = Date.no
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 将库存预警规则转换为按到期天数递增的区间配置。
|
||||
*
|
||||
* @param {Array<Object>} warningRules 库存预警规则
|
||||
* @return {Array<{name: string, key: string, tip_text: string, min_days: number, max_days: number}>} 规则区间列表
|
||||
*/
|
||||
function buildInventoryWarningRanges(warningRules = []) {
|
||||
if (!Array.isArray(warningRules) || !warningRules.length) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const sortedRules = warningRules
|
||||
.filter(rule => rule && typeof rule === 'object')
|
||||
.map(rule => ({
|
||||
name: rule.name,
|
||||
key: rule.key,
|
||||
tip_text: rule.tip_text === undefined || rule.tip_text === null ? '' : String(rule.tip_text),
|
||||
expire_days: Number(rule.expire_days),
|
||||
}))
|
||||
.filter(rule => Number.isInteger(rule.expire_days) && rule.expire_days >= 0)
|
||||
.sort((a, b) => a.expire_days - b.expire_days);
|
||||
|
||||
let previousMax = null;
|
||||
return sortedRules.map(rule => {
|
||||
const minDays = previousMax === null ? 0 : previousMax + 1;
|
||||
const maxDays = rule.expire_days;
|
||||
previousMax = maxDays;
|
||||
return {
|
||||
name: rule.name,
|
||||
key: rule.key,
|
||||
tip_text: rule.tip_text,
|
||||
min_days: minDays,
|
||||
max_days: maxDays,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据剩余天数命中库存预警规则并返回响应字段。
|
||||
*
|
||||
* @param {number|null} remainingDays 到期剩余天数
|
||||
* @param {Array<Object>} warningRanges 预警规则区间
|
||||
* @return {{expiry_remaining_days: (number|null), inventory_warning_level: (string|null), inventory_warning_color: (string|null), inventory_warning_range: ({min_days: number, max_days: number}|null), inventory_warning_tip_text: (string|null)}} 预警输出字段
|
||||
*/
|
||||
function getInventoryWarningPayload(remainingDays, warningRanges = []) {
|
||||
if (!Number.isInteger(remainingDays) || !Array.isArray(warningRanges) || !warningRanges.length) {
|
||||
return {
|
||||
expiry_remaining_days: null,
|
||||
inventory_warning_level: null,
|
||||
inventory_warning_color: null,
|
||||
inventory_warning_range: null,
|
||||
inventory_warning_tip_text: null,
|
||||
};
|
||||
}
|
||||
|
||||
const matchedRange = warningRanges.find(rule => (
|
||||
remainingDays >= rule.min_days && remainingDays <= rule.max_days
|
||||
));
|
||||
|
||||
if (!matchedRange) {
|
||||
return {
|
||||
expiry_remaining_days: remainingDays,
|
||||
inventory_warning_level: null,
|
||||
inventory_warning_color: null,
|
||||
inventory_warning_range: null,
|
||||
inventory_warning_tip_text: null,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
expiry_remaining_days: remainingDays,
|
||||
inventory_warning_level: matchedRange.name || null,
|
||||
inventory_warning_color: matchedRange.key || null,
|
||||
inventory_warning_range: {
|
||||
min_days: matchedRange.min_days,
|
||||
max_days: matchedRange.max_days,
|
||||
},
|
||||
inventory_warning_tip_text: matchedRange.tip_text,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 将库存商品上的标记值规范化为布尔值。
|
||||
*
|
||||
@ -2016,6 +2097,23 @@ class InventoryService extends Service {
|
||||
const limit = pageSize;
|
||||
|
||||
if (groupByBatch) {
|
||||
let inventoryWarningRanges = [];
|
||||
try {
|
||||
const store = await ctx.model.Store.findByPk(warehouseId, {
|
||||
attributes: [ 'id', 'subject_config' ],
|
||||
});
|
||||
const normalizedSubjectConfig = ctx.model.Store.normalizeSubjectConfig(
|
||||
store ? store.subject_config : null
|
||||
);
|
||||
inventoryWarningRanges = buildInventoryWarningRanges(
|
||||
normalizedSubjectConfig && Array.isArray(normalizedSubjectConfig.inventory_warning_rules)
|
||||
? normalizedSubjectConfig.inventory_warning_rules
|
||||
: []
|
||||
);
|
||||
} catch (error) {
|
||||
ctx.logger.warn(`[getInventoryList] 库存预警规则读取失败: ${error.message}`);
|
||||
}
|
||||
|
||||
// 按批次明细:逐条返回
|
||||
const { count: total, rows } = await ctx.model.Storage.findAndCountAll({
|
||||
where: baseWhere,
|
||||
@ -2058,6 +2156,10 @@ class InventoryService extends Service {
|
||||
const list = rows.map(item => {
|
||||
const row = item.get({ plain: true });
|
||||
const shelfLifeWarningState = getShelfLifeWarningState(row.stock || {}, row.expiry_date, nowMs);
|
||||
const inventoryWarningPayload = getInventoryWarningPayload(
|
||||
shelfLifeWarningState.remainingDays,
|
||||
inventoryWarningRanges
|
||||
);
|
||||
if (row.stock) {
|
||||
row.stock.is_batch_mgmt_enabled = toBooleanFlag(row.stock.is_batch_mgmt);
|
||||
row.stock.store_name = row.stock.store_stock?.store_remask || null;
|
||||
@ -2070,6 +2172,11 @@ class InventoryService extends Service {
|
||||
row.production_date = row.production_date ? app.formatDate(row.production_date) : '';
|
||||
row.expiry_date = row.expiry_date ? app.formatDate(row.expiry_date) : '';
|
||||
row.is_shelf_life_warning_triggered = shelfLifeWarningState.isTriggered;
|
||||
row.expiry_remaining_days = inventoryWarningPayload.expiry_remaining_days;
|
||||
row.inventory_warning_level = inventoryWarningPayload.inventory_warning_level;
|
||||
row.inventory_warning_color = inventoryWarningPayload.inventory_warning_color;
|
||||
row.inventory_warning_range = inventoryWarningPayload.inventory_warning_range;
|
||||
row.inventory_warning_tip_text = inventoryWarningPayload.inventory_warning_tip_text;
|
||||
return row;
|
||||
});
|
||||
|
||||
|
||||
@ -7146,9 +7146,9 @@
|
||||
{
|
||||
"name": "template",
|
||||
"type": "object",
|
||||
"required": true,
|
||||
"required": false,
|
||||
"source": "body",
|
||||
"description": "打印模板对象;type=customize 时通常需要包含 page_width、page_height、template_content",
|
||||
"description": "打印模板对象;仅 type=customize 时必填且应包含 page_width、page_height、template_content,type=express 可不传",
|
||||
"children": [
|
||||
{
|
||||
"name": "page_width",
|
||||
|
||||
@ -126,6 +126,11 @@ describe('test/app/controller/system.storage.inventory.list.test.js', () => {
|
||||
assert.ok('batch_no' in row, '明细模式应包含 batch_no');
|
||||
assert.ok('production_date' in row, '明细模式应包含 production_date');
|
||||
assert.ok('expiry_date' in row, '明细模式应包含 expiry_date');
|
||||
assert.ok('expiry_remaining_days' in row, '明细模式应包含 expiry_remaining_days');
|
||||
assert.ok('inventory_warning_level' in row, '明细模式应包含 inventory_warning_level');
|
||||
assert.ok('inventory_warning_color' in row, '明细模式应包含 inventory_warning_color');
|
||||
assert.ok('inventory_warning_range' in row, '明细模式应包含 inventory_warning_range');
|
||||
assert.ok('inventory_warning_tip_text' in row, '明细模式应包含 inventory_warning_tip_text');
|
||||
assert.ok('inbound_time' in row, '明细模式应包含 inbound_time');
|
||||
assert.ok('inbound_order_no' in row, '明细模式应包含 inbound_order_no');
|
||||
assert.ok('supplier_batch_no' in row, '明细模式应包含 supplier_batch_no');
|
||||
|
||||
@ -72,4 +72,102 @@ describe('test/app/model/store.test.js', () => {
|
||||
assert.equal(normalized.auto_grouping_enabled, true);
|
||||
});
|
||||
});
|
||||
|
||||
it('should include six-level inventory_warning_rules by default', () => {
|
||||
const Store = require('../../../app/model/store')(createApp());
|
||||
|
||||
const normalized = Store.normalizeSubjectConfig({});
|
||||
|
||||
assert.ok(Array.isArray(normalized.inventory_warning_rules));
|
||||
assert.equal(normalized.inventory_warning_rules.length, 6);
|
||||
assert.deepEqual(normalized.inventory_warning_rules[0], {
|
||||
name: '01级',
|
||||
key: '#ff4d4f',
|
||||
expire_days: 0,
|
||||
tip_text: '立即报废',
|
||||
});
|
||||
assert.deepEqual(normalized.inventory_warning_rules[5], {
|
||||
name: '06级',
|
||||
key: '#52c41a',
|
||||
expire_days: 999999,
|
||||
tip_text: '常规监控',
|
||||
});
|
||||
assert.ok(
|
||||
normalized.inventory_warning_rules.every(rule => typeof rule.tip_text === 'string' && rule.tip_text.trim() !== ''),
|
||||
'默认库存预警规则六级 tip_text 应全部为非空字符串'
|
||||
);
|
||||
});
|
||||
|
||||
it('should normalize valid inventory_warning_rules entries with key and tip_text', () => {
|
||||
const Store = require('../../../app/model/store')(createApp());
|
||||
|
||||
const normalized = Store.normalizeSubjectConfig({
|
||||
inventory_warning_rules: [
|
||||
{
|
||||
name: ' 临期预警 ',
|
||||
level: '1',
|
||||
key: '#000000',
|
||||
expire_days: '10',
|
||||
tip_text: 123,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
assert.deepEqual(normalized.inventory_warning_rules, [
|
||||
{
|
||||
name: '临期预警',
|
||||
key: '#000000',
|
||||
expire_days: 10,
|
||||
tip_text: '123',
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('should throw for invalid inventory_warning_rules entries', () => {
|
||||
const Store = require('../../../app/model/store')(createApp());
|
||||
|
||||
assert.throws(
|
||||
() => {
|
||||
Store.normalizeSubjectConfig({
|
||||
inventory_warning_rules: [
|
||||
{
|
||||
name: '临期预警',
|
||||
key: '#ff4d4f',
|
||||
expire_days: -1,
|
||||
},
|
||||
],
|
||||
});
|
||||
},
|
||||
/subject_config\.inventory_warning_rules\[0\]\.expire_days 必须是非负整数/
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => {
|
||||
Store.normalizeSubjectConfig({
|
||||
inventory_warning_rules: [
|
||||
{
|
||||
level: '高',
|
||||
key: '#ff4d4f',
|
||||
expire_days: 7,
|
||||
},
|
||||
],
|
||||
});
|
||||
},
|
||||
/subject_config\.inventory_warning_rules\[0\]\.name 不能为空/
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => {
|
||||
Store.normalizeSubjectConfig({
|
||||
inventory_warning_rules: [
|
||||
{
|
||||
name: '临期预警',
|
||||
expire_days: 7,
|
||||
},
|
||||
],
|
||||
});
|
||||
},
|
||||
/subject_config\.inventory_warning_rules\[0\]\.key 不能为空/
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@ -364,6 +364,41 @@ describe('test/app/service/systemMethods/order/wave.batchList.assignmentStatus.t
|
||||
);
|
||||
});
|
||||
|
||||
it('should support where.source and include source filter in SQL', async () => {
|
||||
const { result, capturedSqls } = await getBatchListResult([
|
||||
{ wave_id: 1, storage_status: 'none', post_status: 'none', print_status: 'none' },
|
||||
], {
|
||||
source: 1,
|
||||
});
|
||||
|
||||
assert.equal(result.flag, 'success');
|
||||
assert.equal(result.list.length, 1);
|
||||
assert.ok(capturedSqls.length >= 2);
|
||||
for (const sql of capturedSqls) {
|
||||
assert.ok(sql.includes('source = :whereSource'));
|
||||
}
|
||||
});
|
||||
|
||||
it('should throw when where.source is invalid', async () => {
|
||||
await assert.rejects(
|
||||
async () => {
|
||||
await getBatchListResult([], {
|
||||
source: 9,
|
||||
});
|
||||
},
|
||||
/source 参数不合法/
|
||||
);
|
||||
|
||||
await assert.rejects(
|
||||
async () => {
|
||||
await getBatchListResult([], {
|
||||
source: 'abc',
|
||||
});
|
||||
},
|
||||
/source 参数不合法/
|
||||
);
|
||||
});
|
||||
|
||||
it('should use aggregate join SQL and avoid correlated subquery when filtering 未分配 with wave_type', async () => {
|
||||
const { capturedSqls } = await getBatchListResult([
|
||||
{ wave_id: 1, storage_status: 'error', post_status: 'none', print_status: 'none' },
|
||||
|
||||
102
test/app/service/systemMethods/print.send.test.js
Normal file
102
test/app/service/systemMethods/print.send.test.js
Normal file
@ -0,0 +1,102 @@
|
||||
'use strict';
|
||||
|
||||
const assert = require('node:assert/strict');
|
||||
const PrintService = require('../../../../app/service/systemMethods/print');
|
||||
|
||||
/**
|
||||
* 构建 print.send 测试上下文。
|
||||
* @returns {{ctx: Object, calls: Object}}
|
||||
*/
|
||||
function createContext() {
|
||||
const calls = {
|
||||
printRecordCreatePayloads: [],
|
||||
printRecordUpdatePayloads: [],
|
||||
sendToComputerPayloads: [],
|
||||
};
|
||||
|
||||
const ctx = {
|
||||
app: {
|
||||
config: {
|
||||
jwt: {},
|
||||
},
|
||||
Sequelize: {},
|
||||
},
|
||||
user: {
|
||||
store_id: 100000,
|
||||
},
|
||||
model: {
|
||||
PrintRecord: {
|
||||
async create(payload) {
|
||||
calls.printRecordCreatePayloads.push(payload);
|
||||
return {
|
||||
id: 9001,
|
||||
async update(updatePayload) {
|
||||
calls.printRecordUpdatePayloads.push(updatePayload);
|
||||
},
|
||||
};
|
||||
},
|
||||
},
|
||||
},
|
||||
service: {
|
||||
systems: {
|
||||
print: {
|
||||
index: {
|
||||
async sendToComputer(payload) {
|
||||
calls.sendToComputerPayloads.push(payload);
|
||||
return {
|
||||
status: 'success',
|
||||
};
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
return { ctx, calls };
|
||||
}
|
||||
|
||||
describe('test/app/service/systemMethods/print.send.test.js', () => {
|
||||
it('type=express 不传 template 时应发送成功并调用 sendToComputer', async () => {
|
||||
const { ctx, calls } = createContext();
|
||||
const service = new PrintService(ctx);
|
||||
|
||||
const result = await service.send({
|
||||
computerId: 'PC-01',
|
||||
printer: 'Printer-A',
|
||||
type: 'express',
|
||||
data: {
|
||||
order_no: 'SO10001',
|
||||
},
|
||||
timestamp: '1710000000',
|
||||
count: 1,
|
||||
});
|
||||
|
||||
assert.equal(result.flag, 'success');
|
||||
assert.equal(result.code, 200);
|
||||
assert.equal(calls.sendToComputerPayloads.length, 1);
|
||||
assert.equal(calls.printRecordUpdatePayloads.length, 1);
|
||||
assert.equal(calls.printRecordUpdatePayloads[0].status, 'sent');
|
||||
});
|
||||
|
||||
it('type=customize 不传 template 时应返回失败且提示 template', async () => {
|
||||
const { ctx, calls } = createContext();
|
||||
const service = new PrintService(ctx);
|
||||
|
||||
const result = await service.send({
|
||||
computerId: 'PC-02',
|
||||
printer: 'Printer-B',
|
||||
type: 'customize',
|
||||
data: {
|
||||
order_no: 'SO10002',
|
||||
},
|
||||
timestamp: '1710000001',
|
||||
count: 1,
|
||||
});
|
||||
|
||||
assert.equal(result.flag, 'failure');
|
||||
assert.equal(result.code, 400);
|
||||
assert.match(result.message, /template/);
|
||||
assert.equal(calls.sendToComputerPayloads.length, 0);
|
||||
});
|
||||
});
|
||||
@ -210,4 +210,46 @@ describe('test/app/service/systems/setting/warehouse.test.js', () => {
|
||||
assert.deepEqual(defaultExpressField.options, []);
|
||||
assert.deepEqual(defaultPlatformField.options, []);
|
||||
});
|
||||
|
||||
it('getFieldMeta should include inventory_warning_rules with six-level defaults', async () => {
|
||||
const { ctx } = createHarness();
|
||||
const service = new SettingWarehouseService(ctx);
|
||||
|
||||
const list = await service.getFieldMeta();
|
||||
const inventoryWarningRulesField = list.find(item => item.key === 'inventory_warning_rules');
|
||||
|
||||
assert.ok(inventoryWarningRulesField, '字段信息应包含 inventory_warning_rules');
|
||||
assert.equal(inventoryWarningRulesField.name, '库存预警配置');
|
||||
assert.equal(inventoryWarningRulesField.type, 'array');
|
||||
assert.ok(Array.isArray(inventoryWarningRulesField.item_keys), 'inventory_warning_rules 应包含 item_keys 数组');
|
||||
assert.deepEqual(inventoryWarningRulesField.item_keys, [
|
||||
{ key: 'name', name: '名称', type: 'string', required: true, description: '预警等级名称' },
|
||||
{ key: 'key', name: '颜色标识', type: 'color', required: true, description: '预警颜色值' },
|
||||
{ key: 'expire_days', name: '过期天数', type: 'integer', required: true, description: '距过期天数阈值' },
|
||||
{ key: 'tip_text', name: '提示语', type: 'string', required: false, description: '预警提示文案' },
|
||||
]);
|
||||
assert.ok(
|
||||
inventoryWarningRulesField.item_keys.every(item => !Object.prototype.hasOwnProperty.call(item, 'default')),
|
||||
'inventory_warning_rules.item_keys 子项不应包含 default 字段'
|
||||
);
|
||||
assert.ok(Array.isArray(inventoryWarningRulesField.default));
|
||||
assert.equal(inventoryWarningRulesField.default.length, 6);
|
||||
assert.deepEqual(inventoryWarningRulesField.default[0], {
|
||||
name: '01级',
|
||||
key: '#ff4d4f',
|
||||
expire_days: 0,
|
||||
tip_text: '立即报废',
|
||||
});
|
||||
assert.deepEqual(inventoryWarningRulesField.default[5], {
|
||||
name: '06级',
|
||||
key: '#52c41a',
|
||||
expire_days: 999999,
|
||||
tip_text: '常规监控',
|
||||
});
|
||||
assert.ok(
|
||||
inventoryWarningRulesField.default.every(rule => typeof rule.tip_text === 'string' && rule.tip_text.trim() !== ''),
|
||||
'默认库存预警规则的 tip_text 应全部为非空字符串'
|
||||
);
|
||||
assert.equal(inventoryWarningRulesField.required, false);
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user