WMS_MAIN/database/migrations/20260917110000-create-after-sale-function-and-relation.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

228 lines
9.5 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_function 与 子类型-函数关系表
* wms_after_sale_sub_type_function,并播种系统预设数据。
*
* 语义(新消息处理链路):
* 消息命中售后子类型 → 查关系表取该子类型的处理方法(function_code)
* → 从 inbox functions registry 按 function_code 调用 handler。
* - 系统预设子类型:固定关联(本迁移 seed,不可改)。
* - 仓库自定义子类型:可自行选择处理方法(增删关系)。
* 处理函数在代码注册(registry)时同步 upsert 到函数表;本迁移先播种
* 现有 register 的售后处理类函数,保证表不空。
*
* 注:queryOrder/queryReturn/queryPackVideo/queryWeight 等查询类函数
* 走规则引擎关键词路由,不属于售后子类型处理链路,不入此表。
*/
const FUNCTION_TABLE = 'wms_after_sale_function';
const RELATION_TABLE = 'wms_after_sale_sub_type_function';
const SUB_TYPE_TABLE = 'wms_after_sale_sub_type';
/** 系统预设处理函数(function_code 与 inbox/functions.js register 名一致) */
const SYSTEM_FUNCTIONS = [
{ function_code: 'interceptWaybill', name: '拦截处理', description: '批量创建拦截售后工单,设置订单/运单拦截状态并通知快递售后群' },
{ function_code: 'requestPickup', name: '揽收处理', description: '创建揽收售后工单,原文转发快递揽收群(无揽收群回退售后群)' },
{ function_code: 'cancelIntercept', name: '取消拦截处理', description: '取消拦截:已有工单追加记录,无工单按拦截规则创建并通知快递群' },
{ function_code: 'handleStoreFeedback', name: '货主问题反馈', description: '错发/破损/多发/少发类反馈建工单(货主售后群,不转发快递群)' },
{ function_code: 'changeAddr', name: '改地址/催退回/催件', description: '改地址/催退回/催件诉求建工单并原文转发快递售后群' },
{ function_code: 'weishoudao', name: '核实签收', description: '核实签收诉求建工单并转发快递售后群核实' },
{ function_code: 'changeExpress', name: '改快递', description: '未分配快递的待处理订单修改目标快递' },
{ function_code: 'createPostnum', name: '出单/分配快递号', description: '已审核未取号订单提交分配快递号并回传平台' },
{ function_code: 'handleExpressFeedback', name: '快递群通用反馈', description: '快递群反馈:已有工单追加记录,无工单按运单自动建单(AI 传入 type/sub_type)' },
];
/** 系统预设子类型 → 处理函数(固定关联) */
const SYSTEM_SUB_TYPE_FUNCTIONS = [
{ sub_type: 'intercept', functions: [ 'interceptWaybill' ] },
{ sub_type: 'pickup', functions: [ 'requestPickup' ] },
{ sub_type: 'damage', functions: [ 'handleStoreFeedback' ] },
{ sub_type: 'missing_item', functions: [ 'handleStoreFeedback' ] },
{ sub_type: 'mis_shipment', functions: [ 'handleStoreFeedback' ] },
{ sub_type: 'extra_item', functions: [ 'handleStoreFeedback' ] },
{ sub_type: 'change_address', functions: [ 'changeAddr' ] },
{ sub_type: 'rejected', functions: [ 'changeAddr' ] },
{ sub_type: 'urge', functions: [ 'changeAddr' ] },
{ sub_type: 'verify_delivery', functions: [ 'weishoudao' ] },
{ sub_type: 'change_carrier', functions: [ 'changeExpress' ] },
{ sub_type: 'delivery_delay', functions: [ 'createPostnum' ] },
];
module.exports = {
up: async (queryInterface, Sequelize) => {
const tables = await queryInterface.showAllTables();
const exists = t => tables.some(x => (typeof x === 'string' ? x : x.tableName) === t);
const { sequelize } = queryInterface;
// ─── 1. 函数表 ─────────────────────────────────────────────
if (!exists(FUNCTION_TABLE)) {
await queryInterface.createTable(FUNCTION_TABLE, {
id: {
type: Sequelize.BIGINT(20).UNSIGNED,
primaryKey: true,
autoIncrement: true,
allowNull: false,
comment: '主键',
},
function_code: {
type: Sequelize.STRING(50),
allowNull: false,
comment: '处理函数编码(与 inbox functions registry 注册名一致)',
},
name: {
type: Sequelize.STRING(100),
allowNull: false,
comment: '处理函数名称',
},
description: {
type: Sequelize.STRING(500),
allowNull: true,
defaultValue: null,
comment: '处理函数说明',
},
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: '更新时间',
},
});
await queryInterface.addIndex(FUNCTION_TABLE, [ 'function_code' ], {
unique: true,
name: 'uk_function_code',
});
}
// ─── 2. 关系表 ─────────────────────────────────────────────
if (!exists(RELATION_TABLE)) {
await queryInterface.createTable(RELATION_TABLE, {
id: {
type: Sequelize.BIGINT(20).UNSIGNED,
primaryKey: true,
autoIncrement: true,
allowNull: false,
comment: '主键',
},
sub_type_id: {
type: Sequelize.BIGINT(20).UNSIGNED,
allowNull: false,
comment: '售后子类型ID(关联 wms_after_sale_sub_type.id)',
},
function_id: {
type: Sequelize.BIGINT(20).UNSIGNED,
allowNull: false,
comment: '处理函数ID(关联 wms_after_sale_function.id)',
},
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: '更新时间',
},
});
await queryInterface.addIndex(RELATION_TABLE, [ 'sub_type_id', 'function_id' ], {
unique: true,
name: 'uk_sub_type_function',
});
await queryInterface.addIndex(RELATION_TABLE, [ 'function_id' ], {
name: 'idx_function_id',
});
}
// ─── 3. 播种系统预设函数(INSERT IGNORE 基于 uk_function_code) ──
for (const [ idx, fn ] of SYSTEM_FUNCTIONS.entries()) {
await sequelize.query(
'INSERT IGNORE INTO `' + FUNCTION_TABLE + '` '
+ '(function_code, name, description, is_enabled, sort_order, created_at, updated_at) '
+ 'VALUES (:code, :name, :description, 1, :sortOrder, NOW(), NOW())',
{
replacements: {
code: fn.function_code,
name: fn.name,
description: fn.description,
sortOrder: idx + 1,
},
type: sequelize.QueryTypes.INSERT,
}
);
}
// ─── 4. 播种系统预设子类型 → 函数固定关联 ────────────────────
for (const item of SYSTEM_SUB_TYPE_FUNCTIONS) {
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: item.sub_type },
type: sequelize.QueryTypes.SELECT,
}
);
if (subRows.length === 0) {
console.warn(`[MIGRATION] 跳过:未找到系统级子类型 order.${item.sub_type}`);
continue;
}
const subTypeId = subRows[0].id;
for (const [ fnIndex, code ] of item.functions.entries()) {
const fnRows = await sequelize.query(
'SELECT id FROM `' + FUNCTION_TABLE + '` WHERE function_code = :code LIMIT 1',
{ replacements: { code }, type: sequelize.QueryTypes.SELECT }
);
if (fnRows.length === 0) {
console.warn(`[MIGRATION] 跳过:未找到处理函数 ${code}`);
continue;
}
const functionId = fnRows[0].id;
await sequelize.query(
'INSERT IGNORE INTO `' + RELATION_TABLE + '` '
+ '(sub_type_id, function_id, sort_order, created_at, updated_at) '
+ 'VALUES (:subTypeId, :functionId, :sortOrder, NOW(), NOW())',
{
replacements: { subTypeId, functionId, sortOrder: fnIndex + 1 },
type: sequelize.QueryTypes.INSERT,
}
);
}
}
},
down: async queryInterface => {
await queryInterface.dropTable(RELATION_TABLE);
await queryInterface.dropTable(FUNCTION_TABLE);
},
};