WMS_MAIN/database/migrations/20260917130000-fix-v2-rule-gaps.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

86 lines
3.8 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';
/**
* 修复 v2 回信引擎配置链数据缺口(幂等,先查后插)。
*
* 1. change_carrier(换快递)子类型关联"快递名称提取"取号规则:
* 换快递函数 changeExpress 需要目标快递(express 入参),
* 原种子只关联了运单号 + 平台单号,缺 express_name。
* 2. 关键词表补充 intercept(拦截)/ pickup(揽收)子类型关键词:
* 原 22 个关键词仅覆盖 8 个子类型,拦截/揽收在 v2 引擎命中不了。
*/
const SUB_TYPE_TABLE = 'wms_after_sale_sub_type';
const RULE_TABLE = 'wms_inbox_number_rule';
const RELATION_TABLE = 'wms_after_sale_sub_type_rule';
const KEYWORD_TABLE = 'wms_after_sale_keyword';
/** 补充关键词(与 v1 函数 keywords 语义一致)。 */
const MISSING_KEYWORDS = [
{ sub_type: 'intercept', keywords: [ '拦截', '退回', '召回', '追回', '取消快递' ] },
{ sub_type: 'pickup', keywords: [ '揽收', '上门取件', '取件', '退货取件' ] },
];
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(SUB_TYPE_TABLE) || !exists(RULE_TABLE)) return;
// ─── 1. change_carrier 关联 express_name 规则 ─────────────────
const [ subRows ] = await sequelize.query(
'SELECT id FROM `' + SUB_TYPE_TABLE + '` WHERE type = :type AND sub_type = :subType AND warehouse_id IS NULL LIMIT 1',
{ replacements: { type: 'order', subType: 'change_carrier' } }
);
const [ ruleRows ] = await sequelize.query(
'SELECT id FROM `' + RULE_TABLE + '` WHERE target = :target AND name = :name LIMIT 1',
{ replacements: { target: 'express_name', name: '快递名称提取' } }
);
if (subRows.length > 0 && ruleRows.length > 0 && exists(RELATION_TABLE)) {
const subTypeId = subRows[0].id;
const ruleId = ruleRows[0].id;
const [ relRows ] = await sequelize.query(
'SELECT id FROM `' + RELATION_TABLE + '` WHERE sub_type_id = :sid AND number_rule_id = :rid LIMIT 1',
{ replacements: { sid: subTypeId, rid: ruleId } }
);
if (relRows.length === 0) {
await sequelize.query(
'INSERT INTO `' + RELATION_TABLE + '` (sub_type_id, number_rule_id, sort_order, created_at, updated_at) VALUES (:sid, :rid, 3, NOW(), NOW())',
{ replacements: { sid: subTypeId, rid: ruleId } }
);
}
}
// ─── 2. 关键词补充(先查后插,MySQL 唯一索引对 NULL warehouse 不生效) ───
if (exists(KEYWORD_TABLE)) {
for (const rule of MISSING_KEYWORDS) {
const [ subRows2 ] = await sequelize.query(
'SELECT id FROM `' + SUB_TYPE_TABLE + '` WHERE type = :type AND sub_type = :subType AND warehouse_id IS NULL LIMIT 1',
{ replacements: { type: 'order', subType: rule.sub_type } }
);
if (subRows2.length === 0) continue;
const subTypeId = subRows2[0].id;
let sortOrder = 0;
for (const kw of rule.keywords) {
const [ kwRows ] = await sequelize.query(
'SELECT id FROM `' + KEYWORD_TABLE + '` WHERE keyword = :kw AND warehouse_id IS NULL LIMIT 1',
{ replacements: { kw } }
);
if (kwRows.length === 0) {
await sequelize.query(
'INSERT INTO `' + KEYWORD_TABLE + '` (warehouse_id, sub_type_id, keyword, is_system, is_enabled, sort_order, created_at, updated_at) VALUES (NULL, :sid, :kw, 1, 1, :so, NOW(), NOW())',
{ replacements: { sid: subTypeId, kw, so: sortOrder } }
);
}
sortOrder += 1;
}
}
}
},
down: async () => {
// 不回退(种子类数据)
},
};