'use strict'; /** * 修复拦截/揽收子类型缺少「运单号提取」取号规则关联的问题。 * * 背景: * - 20260917130000 为 intercept(拦截)/ pickup(揽收)补充了关键词,使其能被 v2 引擎命中; * - 但未关联取号规则。v2 规则引擎 _extractNumbers 在该子类型无规则时返回 [](非 null), * 不会进入「取号失败」分支,而是直接执行 interceptWaybill / requestPickup, * 函数因缺运单号返回硬编码错误「请提供运单号」,绕过了回复表配置的取号失败引导语; * - 20260918150000 又因这两个子类型无取号规则而清空了其系统级 no_number_hint。 * * 本次修复(幂等): * 1. 为系统级 intercept / pickup 子类型关联「运单号提取」取号规则(依赖唯一索引 * uk_sub_type_rule 做 INSERT IGNORE); * 2. 将系统级回复行空的 no_number_hint 补为默认运单号引导语(只填空值行)。 * * 修复后链路:消息「拦截」→ 命中关键词 → 关联规则存在但未提取到运单号 → extracted=null * → 返回取号失败引导语(回复表 no_number_hint → 代码默认值兜底)。 */ const REL_TABLE = 'wms_after_sale_sub_type_rule'; const SUB_TYPE_TABLE = 'wms_after_sale_sub_type'; const RULE_TABLE = 'wms_inbox_number_rule'; const REPLY_TABLE = 'wms_after_sale_reply'; const HINT_DEFAULT = '请提供运单号(发送:指令+运单号,多个单号用逗号或空格分隔)'; const RULE_NAME = '运单号提取'; const SUB_TYPES = [ 'intercept', 'pickup' ]; 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(REL_TABLE) || !exists(SUB_TYPE_TABLE) || !exists(RULE_TABLE)) return; const [ ruleRows ] = await sequelize.query( 'SELECT id FROM `' + RULE_TABLE + '` WHERE name = :name LIMIT 1', { replacements: { name: RULE_NAME } } ); if (ruleRows.length === 0) { console.warn(`[MIGRATION] 跳过:未找到取号规则 ${RULE_NAME}`); return; } const ruleId = ruleRows[0].id; let inserted = 0; for (const subType of SUB_TYPES) { const [ subRows ] = await sequelize.query( 'SELECT id FROM `' + SUB_TYPE_TABLE + '` ' + 'WHERE type = :type AND sub_type = :subType AND warehouse_id IS NULL AND is_system = 1 LIMIT 1', { replacements: { type: 'order', subType } } ); if (subRows.length === 0) { console.warn(`[MIGRATION] 跳过:未找到系统级子类型 order.${subType}`); continue; } await sequelize.query( 'INSERT IGNORE INTO `' + REL_TABLE + '` ' + '(sub_type_id, number_rule_id, sort_order, created_at, updated_at) ' + 'VALUES (:sid, :rid, 1, NOW(), NOW())', { replacements: { sid: subRows[0].id, rid: ruleId } } ); inserted += 1; } console.log(`[MIGRATION] 取号规则关联插入 ${inserted} 条`); // 补充系统级回复行的取号失败引导语(只填空值行) if (exists(REPLY_TABLE)) { const [ replyRows ] = await sequelize.query( 'SELECT r.id FROM `' + REPLY_TABLE + '` r ' + 'JOIN `' + SUB_TYPE_TABLE + '` st ON st.id = r.sub_type_id ' + 'WHERE r.warehouse_id IS NULL AND st.type = :type AND st.sub_type IN (:subTypes) ' + 'AND (r.no_number_hint IS NULL OR r.no_number_hint = :empty)', { replacements: { type: 'order', subTypes: SUB_TYPES, empty: '' } } ); for (const row of replyRows) { await sequelize.query( 'UPDATE `' + REPLY_TABLE + '` SET no_number_hint = :hint WHERE id = :id', { replacements: { hint: HINT_DEFAULT, id: row.id } } ); } console.log(`[MIGRATION] 补充取号失败引导语 ${replyRows.length} 行`); } }, down: async queryInterface => { const { sequelize } = queryInterface; const [ ruleRows ] = await sequelize.query( 'SELECT id FROM `' + RULE_TABLE + '` WHERE name = :name LIMIT 1', { replacements: { name: RULE_NAME } } ); if (ruleRows.length === 0) return; for (const subType of SUB_TYPES) { const [ subRows ] = await sequelize.query( 'SELECT id FROM `' + SUB_TYPE_TABLE + '` ' + 'WHERE type = :type AND sub_type = :subType AND warehouse_id IS NULL AND is_system = 1 LIMIT 1', { replacements: { type: 'order', subType } } ); if (subRows.length === 0) continue; await sequelize.query( 'DELETE FROM `' + REL_TABLE + '` WHERE sub_type_id = :sid AND number_rule_id = :rid', { replacements: { sid: subRows[0].id, rid: ruleRows[0].id } } ); } // 引导语不回退(数据修正,与既有种子迁移 down 语义一致) }, };