'use strict'; /** * 修正系统级「取号失败引导语」范围(幂等,按子类型取号规则重算): * - 子类型关联了 target='express_name' 取号规则(如 change_carrier)→ 快递指定提示; * - 子类型关联了其他取号规则 → 默认运单号提示; * - 子类型无任何取号规则(如 help 指令、无取号环节的类型)→ 清空(取号失败分支不会触发,展示空)。 */ const REPLY_TABLE = 'wms_after_sale_reply'; const REL_TABLE = 'wms_after_sale_sub_type_rule'; const RULE_TABLE = 'wms_inbox_number_rule'; const HINT_EXPRESS = '请指定目标快递(圆通/中通/申通/顺丰/韵达/极兔/邮政/EMS/京东/德邦)'; const HINT_DEFAULT = '请提供运单号(发送:指令+运单号,多个单号用逗号或空格分隔)'; 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(REPLY_TABLE)) return; let expressIds = new Set(); let ruleIds = new Set(); if (exists(REL_TABLE) && exists(RULE_TABLE)) { const [ exRows ] = await sequelize.query( 'SELECT DISTINCT rel.sub_type_id FROM `' + REL_TABLE + '` rel ' + 'JOIN `' + RULE_TABLE + '` r ON r.id = rel.number_rule_id WHERE r.target = :t', { replacements: { t: 'express_name' } } ); expressIds = new Set(exRows.map(r => Number(r.sub_type_id))); const [ anyRows ] = await sequelize.query( 'SELECT DISTINCT sub_type_id FROM `' + REL_TABLE + '`' ); ruleIds = new Set(anyRows.map(r => Number(r.sub_type_id))); } const [ rows ] = await sequelize.query( 'SELECT id, sub_type_id FROM `' + REPLY_TABLE + '` WHERE warehouse_id IS NULL' ); let updated = 0; let cleared = 0; for (const row of rows) { const sid = Number(row.sub_type_id); let hint = null; if (expressIds.has(sid)) hint = HINT_EXPRESS; else if (ruleIds.has(sid)) hint = HINT_DEFAULT; if (hint === null) cleared += 1; await sequelize.query( 'UPDATE `' + REPLY_TABLE + '` SET no_number_hint = :hint WHERE id = :id', { replacements: { hint, id: row.id } } ); updated += 1; } console.log(`[fix-no-number-hint-scope] updated=${updated} cleared=${cleared}`); }, down: async () => { // 不回退(数据修正) }, };