'use strict'; /** * 仓库快递映射表 warehouse_express_mappings * * 背景:外部(WMS 等)回传的快递编码不一定是 OMS/标准编码,可能是仓库维度的「自定义编码」 * (对齐 WMS `wms_express_setting`:`code`=业务自定义编码如 SF.KY/KYSF,`express_code`=系统标准编码)。 * 出库确认(stockout.confirm)/ 发货确认(deliveryorder.confirm)回传时按以下顺序解析: * ① 命中本表映射(warehouse_id + custom_code)→ 用映射到的「默认快递编码」; * ② 未命中映射 → 外部编码若本身就在快递公司字典(express_companies)里,直接兜底使用; * ③ 两者都没有 → 报错「外部传入的快递编码(xxx)没有关联映射,请到 OMS 外部仓库完成映射配置」。 * * 幂等:showTable 判断表是否存在。 */ module.exports = { async up(queryInterface, Sequelize) { const tables = await queryInterface.showAllTables(); const exists = tables .map(t => (typeof t === 'string' ? t : (t.tableName || t.TABLE_NAME))) .includes('warehouse_express_mappings'); if (exists) return; const { BIGINT, STRING, TINYINT, DATE } = Sequelize; await queryInterface.createTable('warehouse_express_mappings', { id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' }, principal_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '所属主体ID(冗余仓库主体,便于隔离)' }, warehouse_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '仓库ID(关联 warehouses,映射按仓库维度)' }, express_code: { type: STRING(50), allowNull: false, comment: '默认快递编码(OMS/标准编码,关联 express_companies.code,如 SF/ZT)' }, custom_code: { type: STRING(50), allowNull: false, comment: '自定义编码(外部传入的快递编码,如 SF.KY/KYSF)' }, status: { type: TINYINT, allowNull: false, defaultValue: 1, comment: '状态:1启用 0禁用(仅启用参与回传解析)' }, remark: { type: STRING(255), allowNull: true, comment: '备注' }, created_at: { type: DATE, allowNull: true, comment: '创建时间' }, updated_at: { type: DATE, allowNull: true, comment: '更新时间' }, }, { comment: '仓库快递映射表(外部快递编码 → OMS 默认快递编码)', indexes: [ { name: 'uk_wem_warehouse_custom', unique: true, fields: [ 'warehouse_id', 'custom_code' ] }, { name: 'idx_wem_principal_warehouse', fields: [ 'principal_id', 'warehouse_id' ] }, ], }); }, async down(queryInterface) { const tables = await queryInterface.showAllTables(); const exists = tables .map(t => (typeof t === 'string' ? t : (t.tableName || t.TABLE_NAME))) .includes('warehouse_express_mappings'); if (exists) await queryInterface.dropTable('warehouse_express_mappings'); }, };