WMS_MAIN/database/migrations/20260918000000-seed-help-instruction.js
liufumingren 806e739267 feat(inbox): v2 规则引擎完善(help 指令/建单开关/系统回复)并修复拦截日志误写仓库 ID
- functions.js: 拦截/取消拦截波次感知写订单日志 businessId 误用仓库 ID,改为订单 ID
  (生产订单 JY2026091813795 已发起拦截但订单详情查不到拦截日志的根因)
- rules/v2/processor/config/functionSync: 新增 help 指令、create_work_order=0 时跳过
  工单登记、系统级回复与无单号提示、白名单范围修正
- after_sale_reply 模型 + 6 个迁移: create_work_order 字段与相关配置种子
- return_unpack: 列表 count 统计修复、分页排序追加主键防重漏行、视频附加 camera_name
- methods/system.json + changes + sync 脚本: 接口文档与权限同步
2026-09-18 22:38:57 +08:00

124 lines
6.7 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';
/**
* 播种 v2 系统级「帮助 → 指令」链路(幂等,先查后插)。
*
* - wms_after_sale_type:系统级 type=help(帮助)
* - wms_after_sale_sub_type:系统级 type=help / sub_type=instruction(指令)
* - wms_after_sale_keyword:系统级关键词「指令」「帮助」→ instruction
* - wms_after_sale_function:插入 code=help 函数行(后续启动 sync 会刷新 name/description/is_enabled)
* - wms_after_sale_sub_type_function:instruction ↔ help 函数关联
* - wms_after_sale_sub_type_group:help 子类型白名单(store.after_sales / warehouse.customer_service / express.after_sales)
* - wms_after_sale_reply:系统级回复模板「支持以下命令:\n{back_text}」(仓库可自定义覆盖)
*
* help 子类型无需取号规则(不关联),help 函数由代码注册表硬编码启用。
*/
const TYPE_TABLE = 'wms_after_sale_type';
const SUB_TYPE_TABLE = 'wms_after_sale_sub_type';
const KEYWORD_TABLE = 'wms_after_sale_keyword';
const FUNCTION_TABLE = 'wms_after_sale_function';
const REL_FN_TABLE = 'wms_after_sale_sub_type_function';
const REL_GROUP_TABLE = 'wms_after_sale_sub_type_group';
const REPLY_TABLE = 'wms_after_sale_reply';
module.exports = {
up: async queryInterface => {
const { sequelize } = queryInterface;
const tables = await queryInterface.showAllTables();
const exists = t => tables.some(x => (typeof x === 'string' ? x : x.tableName) === t);
if (!exists(SUB_TYPE_TABLE)) return;
const q1 = async (sql, replacements) => {
const [ rows ] = await sequelize.query(sql, { replacements });
return rows && rows.length > 0 ? rows[0] : null;
};
// ─── 1. 系统级类型 help ─────────────────────────────────────
if (exists(TYPE_TABLE) && !(await q1('SELECT id FROM `' + TYPE_TABLE + '` WHERE type = :t AND warehouse_id IS NULL LIMIT 1', { t: 'help' }))) {
await sequelize.query(
'INSERT INTO `' + TYPE_TABLE + '` (warehouse_id, type, name, is_system, is_enabled, sort_order, created_at, updated_at) VALUES (NULL, :t, :n, 1, 1, 100, NOW(), NOW())',
{ replacements: { t: 'help', n: '帮助' } }
);
}
// ─── 2. 系统级子类型 instruction ────────────────────────────
let subId = await q1('SELECT id FROM `' + SUB_TYPE_TABLE + '` WHERE type = :t AND sub_type = :s AND warehouse_id IS NULL LIMIT 1', { t: 'help', s: 'instruction' });
if (!subId) {
await sequelize.query(
'INSERT INTO `' + SUB_TYPE_TABLE + '` (warehouse_id, type, sub_type, name, is_system, is_enabled, sort_order, created_at, updated_at) VALUES (NULL, :t, :s, :n, 1, 1, 0, NOW(), NOW())',
{ replacements: { t: 'help', s: 'instruction', n: '指令' } }
);
subId = await q1('SELECT id FROM `' + SUB_TYPE_TABLE + '` WHERE type = :t AND sub_type = :s AND warehouse_id IS NULL LIMIT 1', { t: 'help', s: 'instruction' });
}
const instructionId = subId.id;
// ─── 3. 系统级关键词「指令」「帮助」 ─────────────────────────
if (exists(KEYWORD_TABLE)) {
let order = 0;
for (const kw of [ '指令', '帮助' ]) {
const dup = await q1('SELECT id FROM `' + KEYWORD_TABLE + '` WHERE keyword = :k AND warehouse_id IS NULL LIMIT 1', { k: kw });
if (!dup) {
await sequelize.query(
'INSERT INTO `' + KEYWORD_TABLE + '` (warehouse_id, sub_type_id, keyword, is_system, is_enabled, sort_order, created_at, updated_at) VALUES (NULL, :sid, :k, 1, 1, :so, NOW(), NOW())',
{ replacements: { sid: instructionId, k: kw, so: order } }
);
}
order += 1;
}
}
// ─── 4. help 函数行(sync 启动后刷新 name/description/is_enabled) ───
let helpFn = null;
if (exists(FUNCTION_TABLE)) {
helpFn = await q1('SELECT id FROM `' + FUNCTION_TABLE + '` WHERE function_code = :c LIMIT 1', { c: 'help' });
if (!helpFn) {
await sequelize.query(
'INSERT INTO `' + FUNCTION_TABLE + '` (function_code, name, description, is_enabled, sort_order, created_at, updated_at) VALUES (:c, :n, :d, 1, 0, NOW(), NOW())',
{ replacements: { c: 'help', n: '帮助', d: '帮助指令:枚举当前仓库可见的售后类型与子类型(含关键词),返回可用指令清单(back_text)' } }
);
helpFn = await q1('SELECT id FROM `' + FUNCTION_TABLE + '` WHERE function_code = :c LIMIT 1', { c: 'help' });
}
}
// ─── 5. instruction ↔ help 函数关联 ─────────────────────────
if (exists(REL_FN_TABLE) && helpFn) {
const dup = await q1('SELECT id FROM `' + REL_FN_TABLE + '` WHERE sub_type_id = :s AND function_id = :f LIMIT 1', { s: instructionId, f: helpFn.id });
if (!dup) {
await sequelize.query(
'INSERT INTO `' + REL_FN_TABLE + '` (sub_type_id, function_id, sort_order, created_at, updated_at) VALUES (:s, :f, 1, NOW(), NOW())',
{ replacements: { s: instructionId, f: helpFn.id } }
);
}
}
// ─── 6. help 子类型群白名单(三类群均可触发) ────────────────
if (exists(REL_GROUP_TABLE)) {
for (const [ gt, sg ] of [[ 'store', 'after_sales' ], [ 'warehouse', 'customer_service' ], [ 'express', 'after_sales' ]]) {
const dup = await q1('SELECT id FROM `' + REL_GROUP_TABLE + '` WHERE sub_type_id = :s AND group_type = :g AND sub_group = :sg LIMIT 1', { s: instructionId, g: gt, sg });
if (!dup) {
await sequelize.query(
'INSERT INTO `' + REL_GROUP_TABLE + '` (sub_type_id, group_type, sub_group, is_system, sort_order, created_at, updated_at) VALUES (:s, :g, :sg, 1, 0, NOW(), NOW())',
{ replacements: { s: instructionId, g: gt, sg } }
);
}
}
}
// ─── 7. 系统级回复模板(仓库可自定义覆盖) ───────────────────
if (exists(REPLY_TABLE)) {
const dup = await q1('SELECT id FROM `' + REPLY_TABLE + '` WHERE sub_type_id = :s AND warehouse_id IS NULL LIMIT 1', { s: instructionId });
if (!dup) {
await sequelize.query(
'INSERT INTO `' + REPLY_TABLE + '` (warehouse_id, sub_type_id, reply, is_reply, no_number_hint, is_enabled, created_at, updated_at) VALUES (NULL, :s, :r, 1, NULL, 1, NOW(), NOW())',
{ replacements: { s: instructionId, r: '支持以下命令:\n{back_text}' } }
);
}
}
},
down: async () => {
// 不回退(系统预设种子)
},
};