'use strict'; /** * 补齐 wms_order_delivery 基表相对模型缺失的 25 列。 * * 背景:app/model/order_delivery.js 声明 92 个字段,但基表只有 67 列, * 缺列导致按模型全字段 SELECT(如 afterSale._resolveOrderDetail 先查基表)时 * 报 Unknown column 'extend_props' 等错误。 * 分表(wms_order_delivery_100000 等)均已有这些列,本迁移以分表结构为参照补齐基表。 * * 修复后 afterSale._resolveOrderDetail 查基表不再抛错(查无数据时回退工厂分表)。 */ const TABLE_NAME = 'wms_order_delivery'; /** 缺失列定义(类型/是否可空/默认值/注释),与分表结构一致 */ const MISSING_COLUMNS = [ { name: 'extend_props', type: 'JSON', allowNull: true, defaultValue: null, comment: '订单扩展属性' }, { name: 'create_channel', type: 'STRING(20)', allowNull: false, defaultValue: 'MANUAL', comment: '创建来源渠道' }, { name: 'sys_platform_code', type: 'STRING(50)', allowNull: true, defaultValue: null, comment: '系统平台编码' }, { name: 'sys_platform_name', type: 'STRING(200)', allowNull: true, defaultValue: null, comment: '系统平台名称' }, { name: 'buyer_message', type: 'STRING(500)', allowNull: true, defaultValue: null, comment: '买家留言' }, { name: 'seller_message', type: 'STRING(500)', allowNull: true, defaultValue: null, comment: '卖家留言' }, { name: 'remark', type: 'STRING(500)', allowNull: true, defaultValue: null, comment: '备注' }, { name: 'sys_logistics_code', type: 'STRING(50)', allowNull: true, defaultValue: null, comment: '系统物流公司编码' }, { name: 'sys_logistics_name', type: 'STRING(200)', allowNull: true, defaultValue: null, comment: '系统物流公司名称' }, { name: 'basket_number', type: 'STRING(50)', allowNull: true, defaultValue: null, comment: '筐号' }, { name: 'from_key', type: 'INTEGER(11)', allowNull: true, defaultValue: null, comment: '来源键' }, { name: 'items', type: 'TEXT', allowNull: true, defaultValue: null, comment: '商品JSON字符串' }, { name: 'items_count', type: 'SMALLINT(5).UNSIGNED', allowNull: false, defaultValue: 0, comment: '商品种类数' }, { name: 'items_total', type: 'INTEGER(10).UNSIGNED', allowNull: false, defaultValue: 0, comment: '商品总数' }, { name: 'items_weight', type: 'INTEGER(11)', allowNull: false, defaultValue: 0, comment: '总重量(g)' }, { name: 'items_hash', type: 'CHAR(32)', allowNull: false, defaultValue: '', comment: 'MD5哈希值(32位)' }, { name: 'items_crc', type: 'INTEGER(10).UNSIGNED', allowNull: false, defaultValue: 0, comment: 'CRC32校验码' }, { name: 'wave_id', type: 'BIGINT(20)', allowNull: false, defaultValue: 0, comment: '波次ID,0表示未分配' }, { name: 'wave_no', type: 'STRING(50)', allowNull: false, defaultValue: '', comment: '波次号,空字符串表示未分配' }, { name: 'wave_strategy_version', type: 'INTEGER(10)', allowNull: false, defaultValue: 0, comment: '波次策略版本号' }, { name: 'wave_scan_result', type: 'INTEGER(10)', allowNull: false, defaultValue: 0, comment: '波次扫描结果,0未扫描' }, { name: 'sended_status', type: 'ENUM', enumValues: [ 'none', 'padding', 'fin', 'error' ], allowNull: true, defaultValue: 'none', comment: '发货状态机' }, { name: 'recheck_status', type: 'STRING(20)', allowNull: true, defaultValue: 'none', comment: '复核状态' }, { name: 'recheck_time', type: 'DATE', allowNull: true, defaultValue: null, comment: '复核时间' }, { name: 'print_lock_status', type: 'ENUM', enumValues: [ 'none', 'warehouse' ], allowNull: true, defaultValue: 'none', comment: '打印锁定状态机' }, ]; /** * 将列定义转为 Sequelize 字段描述。 * * @param {Object} Sequelize Sequelize 构造器。 * @param {Object} def 列定义(name/type/allowNull/defaultValue/comment)。 * @return {Object} Sequelize addColumn 字段描述。 */ function toColumnDef(Sequelize, def) { let type; if (def.type === 'ENUM') { type = Sequelize.ENUM(...def.enumValues); } else if (def.type === 'JSON') { type = Sequelize.JSON; } else if (def.type === 'DATE') { type = Sequelize.DATE; } else if (def.type === 'TEXT') { type = Sequelize.TEXT; } else if (def.type === 'CHAR(32)') { type = Sequelize.CHAR(32); } else { const match = def.type.match(/^([A-Z]+)(?:\(([^)]+)\))?(?:\.([A-Z]+))?$/); if (!match) { throw new Error(`无法解析列类型: ${def.type}`); } const base = match[1]; const args = match[2] ? match[2].split(',').map(s => Number(s.trim())) : []; const unsigned = Boolean(match[3] && match[3] === 'UNSIGNED'); switch (base) { case 'STRING': type = Sequelize.STRING(args[0]); break; case 'INTEGER': type = Sequelize.INTEGER(args[0]); break; case 'BIGINT': type = Sequelize.BIGINT(args[0]); break; case 'SMALLINT': type = Sequelize.SMALLINT(args[0]); break; default: throw new Error(`无法解析列类型: ${def.type}`); } if (unsigned) { type = type.UNSIGNED; } } const columnDef = { type, allowNull: def.allowNull, comment: def.comment, }; if (def.defaultValue !== null && def.defaultValue !== undefined) { columnDef.defaultValue = def.defaultValue; } return columnDef; } module.exports = { // eslint-disable-next-line no-unused-vars async up(queryInterface, Sequelize) { for (const def of MISSING_COLUMNS) { await queryInterface.addColumn(TABLE_NAME, def.name, toColumnDef(Sequelize, def)); } }, // eslint-disable-next-line no-unused-vars async down(queryInterface, Sequelize) { for (const def of MISSING_COLUMNS) { await queryInterface.removeColumn(TABLE_NAME, def.name); } }, };