'use strict'; /** * 创建售后自定义回复表 wms_after_sale_reply。 * * 语义:消息关键词命中售后子类型并完成处理后,按子类型返回自定义回复文案。 * - 系统级(warehouse_id=NULL):全部仓库可读的默认回复。 * - 仓库级(warehouse_id=仓库ID):该仓库自定义回复,设置了自动覆盖系统级 * (读取时优先仓库级,无仓库级再回退系统级)。 * - 每个仓库每种子类型只允许一条仓库级回复(唯一索引 uk_warehouse_sub_type 保证); * 系统级每种子类型只允许一条,由应用层校验(MySQL 唯一索引对 NULL 不生效)。 */ const TABLE = 'wms_after_sale_reply'; module.exports = { up: async (queryInterface, Sequelize) => { const tables = await queryInterface.showAllTables(); const exists = tables.some(t => (typeof t === 'string' ? t : t.tableName) === TABLE); if (exists) return; await queryInterface.createTable(TABLE, { id: { type: Sequelize.BIGINT(20).UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false, comment: '主键', }, warehouse_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: true, defaultValue: null, comment: '仓库ID(NULL=系统级默认回复;非NULL=仓库级自定义回复,覆盖系统级)', }, sub_type_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: false, comment: '售后子类型ID(关联 wms_after_sale_sub_type.id)', }, reply: { type: Sequelize.TEXT, allowNull: false, comment: '自定义回复文案(支持换行,可含占位符)', }, is_reply: { type: Sequelize.TINYINT(1), allowNull: false, defaultValue: 1, comment: '是否回复:1=命中后回复,0=静默不回复', }, no_number_hint: { type: Sequelize.STRING(500), allowNull: true, defaultValue: null, comment: '未取到单据号时的引导语(如:请提供运单号;为空则用默认引导语)', }, is_enabled: { type: Sequelize.TINYINT(1), allowNull: false, defaultValue: 1, comment: '是否启用:1=启用,0=停用(停用视为未设置,回退系统级)', }, created_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'), comment: '创建时间', }, updated_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'), comment: '更新时间', }, }); // 仓库级:每仓库每子类型唯一(系统级 warehouse_id=NULL 由应用层保证唯一) await queryInterface.addIndex(TABLE, [ 'warehouse_id', 'sub_type_id' ], { unique: true, name: 'uk_warehouse_sub_type', }); await queryInterface.addIndex(TABLE, [ 'sub_type_id' ], { name: 'idx_sub_type_id', }); await queryInterface.addIndex(TABLE, [ 'is_enabled' ], { name: 'idx_is_enabled', }); }, down: async queryInterface => { await queryInterface.dropTable(TABLE); }, };