'use strict'; const TABLE_NAME = 'wms_store_express_binding_policy'; module.exports = { /** * 创建订单自定义店铺-快递绑定策略表。 * @param {Object} queryInterface Sequelize QueryInterface 实例。 * @param {Object} Sequelize Sequelize 类。 * @return {Promise} 迁移执行结果。 */ up: async (queryInterface, Sequelize) => { const tables = await queryInterface.showAllTables(); const exists = tables.some(t => (typeof t === 'string' ? t : t.tableName) === TABLE_NAME); if (exists) return; await queryInterface.createTable(TABLE_NAME, { id: { type: Sequelize.BIGINT(20).UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false, comment: '主键ID', }, warehouse_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: false, comment: '仓库ID,策略所属仓库', }, custom_name: { type: Sequelize.STRING(100), allowNull: false, comment: '自定义策略名称,便于人工识别', }, store_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: true, comment: '货主ID(stores.id),NULL=适用所有货主', }, shop_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: true, comment: '店铺ID(shops.id),NULL=适用所有店铺', }, express_code: { type: Sequelize.STRING(50), allowNull: false, comment: '绑定的快递编码(wms_express.code)', }, priority: { type: Sequelize.INTEGER(11), allowNull: false, defaultValue: 0, comment: '优先级,数值越大越优先匹配', }, is_enabled: { type: Sequelize.TINYINT(1), allowNull: false, defaultValue: 1, comment: '是否启用:0=停用,1=启用', }, match_condition: { type: Sequelize.JSON, allowNull: true, comment: '附加匹配条件(JSON):platform_codes/provinces/cities/item_codes/min_weight_g/max_weight_g/min_amount/max_amount', }, remark: { type: Sequelize.STRING(500), allowNull: true, comment: '备注说明', }, creator_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: true, comment: '创建人账户ID(accounts.id)', }, 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: '更新时间', }, deleted_at: { type: Sequelize.DATE, allowNull: true, comment: '软删除时间', }, }, { comment: '订单自定义店铺-快递绑定策略表', engine: 'InnoDB', charset: 'utf8mb4', collate: 'utf8mb4_general_ci', }); // 索引:按仓库查询启用策略 await queryInterface.addIndex(TABLE_NAME, { name: 'idx_warehouse_enabled', fields: [ 'warehouse_id', 'is_enabled', 'deleted_at' ], }); // 索引:按仓库+货主+店铺精确匹配 await queryInterface.addIndex(TABLE_NAME, { name: 'idx_warehouse_store_shop', fields: [ 'warehouse_id', 'store_id', 'shop_id', 'deleted_at' ], }); // 索引:按仓库+快递反查绑定店铺 await queryInterface.addIndex(TABLE_NAME, { name: 'idx_warehouse_express', fields: [ 'warehouse_id', 'express_code', 'deleted_at' ], }); }, /** * 回滚:删除表。 * @param {Object} queryInterface Sequelize QueryInterface 实例。 * @param {Object} Sequelize Sequelize 类。 * @return {Promise} 迁移执行结果。 */ down: async (queryInterface, Sequelize) => { await queryInterface.dropTable(TABLE_NAME); }, };