'use strict'; /** * 商品表对齐外部商品结构: * 1) 重命名:code→item_code、name→item_name、sku_code→goods_code、type→item_type、 * category→category_name、spec→sku_property、unit→stock_unit、price→retail_price * 2) 新增外部字段:is_area_sale、length、width、height、volume、gross_weight、net_weight、 * category_id、tag_price、is_sn_mgmt、is_shelf_life_mgmt、is_batch_mgmt、brand_name、 * create_from、stock_id、warehouse_id、store_id * 3) OMS 专有字段(principal_id / status / deleted_at / image / remark / cost_price / barcode)原样保留; * 外部表的 is_valid、delete_at 与 OMS 的 status、deleted_at 语义重复,不再新增,避免出现两份真值 */ module.exports = { async up(queryInterface, Sequelize) { // ---------- 1. 重命名列,保持与外部商品表一致 ---------- await queryInterface.renameColumn('products', 'code', 'item_code'); await queryInterface.renameColumn('products', 'name', 'item_name'); await queryInterface.renameColumn('products', 'sku_code', 'goods_code'); await queryInterface.renameColumn('products', 'type', 'item_type'); await queryInterface.renameColumn('products', 'category', 'category_name'); await queryInterface.renameColumn('products', 'spec', 'sku_property'); await queryInterface.renameColumn('products', 'unit', 'stock_unit'); await queryInterface.renameColumn('products', 'price', 'retail_price'); // 按外部结构放宽长度(只放宽不收窄,避免历史数据被截断) await queryInterface.changeColumn('products', 'item_name', { type: Sequelize.STRING(255), allowNull: false, comment: '商品名称', }); await queryInterface.changeColumn('products', 'category_name', { type: Sequelize.STRING(100), allowNull: true, comment: '分类名称', }); await queryInterface.changeColumn('products', 'goods_code', { type: Sequelize.STRING(100), allowNull: true, comment: '货物编码', }); await queryInterface.changeColumn('products', 'sku_property', { type: Sequelize.STRING(100), allowNull: true, comment: 'SKU规格', }); await queryInterface.changeColumn('products', 'stock_unit', { type: Sequelize.STRING(20), allowNull: true, comment: '库存单位', }); await queryInterface.changeColumn('products', 'item_type', { type: Sequelize.TINYINT, allowNull: false, defaultValue: 1, comment: '商品类型', }); await queryInterface.changeColumn('products', 'retail_price', { type: Sequelize.DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '零售价', }); // ---------- 2. 索引名与列名对齐(先建新索引,再删旧索引,保证外键列始终有索引) ---------- await queryInterface.addIndex('products', { fields: [ 'principal_id', 'item_code' ], unique: true, name: 'uk_products_principal_item_code', }); await queryInterface.addIndex('products', { fields: [ 'goods_code' ], name: 'idx_products_goods_code' }); await queryInterface.removeIndex('products', 'uk_products_principal_code'); await queryInterface.removeIndex('products', 'idx_products_sku_code'); // ---------- 3. 新增外部字段 ---------- await queryInterface.addColumn('products', 'is_area_sale', { type: Sequelize.STRING(1), allowNull: true, defaultValue: 'N', comment: '是否区域销售', }); await queryInterface.addColumn('products', 'length', { type: Sequelize.DECIMAL(10, 2), allowNull: true, defaultValue: 0, comment: '长度', }); await queryInterface.addColumn('products', 'width', { type: Sequelize.DECIMAL(10, 2), allowNull: true, defaultValue: 0, comment: '宽度', }); await queryInterface.addColumn('products', 'height', { type: Sequelize.DECIMAL(10, 2), allowNull: true, defaultValue: 0, comment: '高度', }); await queryInterface.addColumn('products', 'volume', { type: Sequelize.DECIMAL(10, 2), allowNull: true, defaultValue: 0, comment: '体积', }); await queryInterface.addColumn('products', 'gross_weight', { type: Sequelize.INTEGER, allowNull: true, defaultValue: 0, comment: '毛重(g)', }); await queryInterface.addColumn('products', 'net_weight', { type: Sequelize.INTEGER, allowNull: true, defaultValue: 0, comment: '净重(g)', }); await queryInterface.addColumn('products', 'category_id', { type: Sequelize.INTEGER, allowNull: true, comment: '分类ID', }); await queryInterface.addColumn('products', 'tag_price', { type: Sequelize.DECIMAL(10, 2), allowNull: true, defaultValue: 0, comment: '标价', }); await queryInterface.addColumn('products', 'is_sn_mgmt', { type: Sequelize.STRING(1), allowNull: true, defaultValue: 'N', comment: '是否序列号管理', }); await queryInterface.addColumn('products', 'is_shelf_life_mgmt', { type: Sequelize.STRING(1), allowNull: true, defaultValue: 'N', comment: '是否保质期管理', }); await queryInterface.addColumn('products', 'is_batch_mgmt', { type: Sequelize.STRING(1), allowNull: true, defaultValue: 'N', comment: '是否批次管理', }); await queryInterface.addColumn('products', 'brand_name', { type: Sequelize.STRING(100), allowNull: true, comment: '品牌名称', }); await queryInterface.addColumn('products', 'create_from', { type: Sequelize.ENUM('self', 'qimen', 'api'), allowNull: true, comment: '数据来源', }); await queryInterface.addColumn('products', 'stock_id', { type: Sequelize.BIGINT.UNSIGNED, allowNull: true, comment: '库存ID', }); await queryInterface.addColumn('products', 'warehouse_id', { type: Sequelize.BIGINT.UNSIGNED, allowNull: true, comment: '仓库ID', }); await queryInterface.addColumn('products', 'store_id', { type: Sequelize.BIGINT.UNSIGNED, allowNull: true, comment: '门店ID', }); }, async down(queryInterface, Sequelize) { // 删除新增字段 const dropColumns = [ 'is_area_sale', 'length', 'width', 'height', 'volume', 'gross_weight', 'net_weight', 'category_id', 'tag_price', 'is_sn_mgmt', 'is_shelf_life_mgmt', 'is_batch_mgmt', 'brand_name', 'create_from', 'stock_id', 'warehouse_id', 'store_id', ]; for (const column of dropColumns) { await queryInterface.removeColumn('products', column); } // 还原索引名 await queryInterface.addIndex('products', { fields: [ 'principal_id', 'item_code' ], unique: true, name: 'uk_products_principal_code', }); await queryInterface.addIndex('products', { fields: [ 'goods_code' ], name: 'idx_products_sku_code' }); await queryInterface.removeIndex('products', 'uk_products_principal_item_code'); await queryInterface.removeIndex('products', 'idx_products_goods_code'); // 还原长度 await queryInterface.changeColumn('products', 'item_name', { type: Sequelize.STRING(100), allowNull: false, comment: '商品名称', }); await queryInterface.changeColumn('products', 'category_name', { type: Sequelize.STRING(50), allowNull: true, comment: '分类', }); }, };