'use strict'; const TABLE_NAME = 'wms_package_material_item_relation'; module.exports = { 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', }, items_hash: { type: Sequelize.CHAR(32), allowNull: false, comment: '订单商品结构MD5指纹', }, items_crc: { type: Sequelize.INTEGER.UNSIGNED, allowNull: false, comment: '订单商品结构CRC32校验', }, package_material_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: false, comment: '包材ID', }, package_material_code: { type: Sequelize.STRING(64), allowNull: false, comment: '包材编码(快照)', }, package_material_name: { type: Sequelize.STRING(128), allowNull: false, comment: '包材名称(快照)', }, package_material_spec: { type: Sequelize.STRING(128), allowNull: true, defaultValue: '', comment: '规格(快照)', }, package_material_type: { type: Sequelize.STRING(32), allowNull: true, defaultValue: '', comment: '类型(快照)', }, length: { type: Sequelize.DECIMAL(10, 2), allowNull: true, defaultValue: 0, comment: '长(cm)快照', }, width: { type: Sequelize.DECIMAL(10, 2), allowNull: true, defaultValue: 0, comment: '宽(cm)快照', }, height: { type: Sequelize.DECIMAL(10, 2), allowNull: true, defaultValue: 0, comment: '高(cm)快照', }, weight: { type: Sequelize.DECIMAL(10, 2), allowNull: true, defaultValue: 0, comment: '重量(kg)快照', }, bind_count: { type: Sequelize.INTEGER.UNSIGNED, allowNull: false, defaultValue: 1, comment: '累计绑定次数', }, last_order_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: true, comment: '最近订单ID', }, last_waybill_code: { type: Sequelize.STRING(50), allowNull: true, defaultValue: '', comment: '最近运单号', }, last_bind_user_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: true, comment: '最近操作用户ID', }, last_bind_at: { type: Sequelize.DATE, allowNull: true, comment: '最近绑定时间', }, is_active: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 1, comment: '包材当前是否启用', }, 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: '更新时间', }, delete_at: { type: Sequelize.DATE, allowNull: true, comment: '软删除时间', }, }); await queryInterface.addIndex(TABLE_NAME, [ 'warehouse_id', 'items_hash', 'items_crc', 'package_material_id' ], { unique: true, name: 'uk_item_material', }); await queryInterface.addIndex(TABLE_NAME, [ 'warehouse_id', 'is_active', 'items_hash', 'bind_count' ], { name: 'idx_lookup', }); }, down: async queryInterface => { await queryInterface.dropTable(TABLE_NAME); }, };