WMS_MAIN/database/migrations/20260916150000-create-wms-after-sale-keyword.js
liufumingren c2a761ca26 feat(inbox): 新增开门/到货/进场/离场车牌登记与 extend_props 持久化
- functions 新增 openDoor/registerLicensePlate/enter/departure(规则表+权限分组)
- 到货登记表 wms_inbox_arrival_register(含消费字段,离场闭环,货主收货群优先推送)
- 回执 extend_props 持久化落库与定时补发送下发(重推兜底回读)
- 车牌提取正则兼容异体字(粤/粵)
- 售后类型/关键字/函数/回复 v2 模型与迁移
- INBOX V2 HTTP 测试报告与指南
2026-09-17 02:41:43 +08:00

163 lines
6.3 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';
/**
* 创建售后关键词表 wms_after_sale_keyword:关键词 → 售后子类型 映射。
*
* 语义:
* - 多个关键词可对应同一个售后子类型(1:N),一个关键词在同一仓库内唯一(只映射一个子类型)。
* - 仓库维度隔离:warehouse_id=NULL + is_system=1 为系统级关键词,全部仓库可见;
* warehouse_id=仓库ID + is_system=0 为仓库自定义关键词,仅该仓库可见。
* - 匹配优先级:sort_order 小者优先(AI 按关键词推断子类型时,先取仓库自定义+系统级合并列表,
* 按关键词长度降序、sort_order 升序匹配)。
*
* 数据播种:将 app/service/inbox/rules.js 中硬编码的 EXPRESS_SUB_TYPE_RULES
* (快递反馈→订单售后子类型 8 组关键词)作为系统级写入本表。
* 由于 MySQL 唯一索引对 NULL 不生效,播种采用"先查后插"保证幂等。
*/
const TABLE = 'wms_after_sale_keyword';
const SUB_TYPE_TABLE = 'wms_after_sale_sub_type';
/**
* 系统级预设关键词(来源:inbox/rules.js EXPRESS_SUB_TYPE_RULES,type=order)。
* @type {Array<{sub_type: string, keywords: string[]}>}
*/
const SYSTEM_KEYWORD_RULES = [
{ sub_type: 'verify_delivery', keywords: [ '核实签收', '签收核实', '已签收', '签收' ] },
{ sub_type: 'damage', keywords: [ '破损', '坏了', '破损件' ] },
{ sub_type: 'missing_item', keywords: [ '少发', '漏发', '发少' ] },
{ sub_type: 'change_address', keywords: [ '改地址', '地址更改', '改址' ] },
{ sub_type: 'change_carrier', keywords: [ '换快递', '改快递' ] },
{ sub_type: 'urge', keywords: [ '催件', '催派', '催促' ] },
{ sub_type: 'delivery_delay', keywords: [ '发货延迟', '延迟发货', '催发货' ] },
{ sub_type: 'rejected', keywords: [ '拒收' ] },
];
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) {
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)',
},
keyword: {
type: Sequelize.STRING(100),
allowNull: false,
comment: '关键词,命中即映射到对应售后子类型',
},
is_system: {
type: Sequelize.TINYINT(1),
allowNull: false,
defaultValue: 0,
comment: '是否系统级:1=系统级(全部仓库可读,不允许编辑/删除),0=仓库自定义',
},
is_enabled: {
type: Sequelize.TINYINT(1),
allowNull: false,
defaultValue: 1,
comment: '是否启用:1=启用,0=停用',
},
sort_order: {
type: Sequelize.INTEGER(11),
allowNull: false,
defaultValue: 0,
comment: '匹配优先级,数值小者优先(同长度命中时生效)',
},
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, [ 'keyword', 'warehouse_id' ], {
unique: true,
name: 'uk_keyword_warehouse',
});
await queryInterface.addIndex(TABLE, [ 'sub_type_id' ], {
name: 'idx_sub_type_id',
});
await queryInterface.addIndex(TABLE, [ 'warehouse_id' ], {
name: 'idx_warehouse_id',
});
await queryInterface.addIndex(TABLE, [ 'is_system' ], {
name: 'idx_is_system',
});
}
// ─── 播种系统级关键词(先查后插,幂等) ────────────────────
const { sequelize } = queryInterface;
const now = new Date();
for (const [ ruleIndex, rule ] of SYSTEM_KEYWORD_RULES.entries()) {
// 定位系统级订单子类型(warehouse_id IS NULL)
const subRows = await sequelize.query(
`SELECT id FROM \`${SUB_TYPE_TABLE}\`
WHERE type = 'order' AND sub_type = :subType AND warehouse_id IS NULL AND is_system = 1
LIMIT 1`,
{ replacements: { subType: rule.sub_type }, type: sequelize.QueryTypes.SELECT }
);
if (subRows.length === 0) {
console.warn(`[MIGRATION] 跳过播种:未找到系统级子类型 order.${rule.sub_type}`);
continue;
}
const subTypeId = subRows[0].id;
const rows = [];
for (const [ kwIndex, kw ] of rule.keywords.entries()) {
const existsRows = await sequelize.query(
`SELECT id FROM \`${TABLE}\`
WHERE keyword = :kw AND warehouse_id IS NULL AND is_system = 1
LIMIT 1`,
{ replacements: { kw }, type: sequelize.QueryTypes.SELECT }
);
if (existsRows.length === 0) {
rows.push({
warehouse_id: null,
sub_type_id: subTypeId,
keyword: kw,
is_system: 1,
is_enabled: 1,
sort_order: ruleIndex * 100 + kwIndex,
created_at: now,
updated_at: now,
});
}
}
if (rows.length > 0) {
await queryInterface.bulkInsert(TABLE, rows);
}
}
},
down: async queryInterface => {
await queryInterface.dropTable(TABLE);
},
};