'use strict'; /** * 11. 订单明细表:订单的副表,一个订单对应多条明细; * 明细记录了平台侧的原始商品信息,并单向关联系统商品/组合商品: * product_id 为空表示尚未解析出对应商品(parse_status=0 未解析 或 2 解析失败)。 */ module.exports = { async up(queryInterface, Sequelize) { await queryInterface.createTable( 'order_items', { id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: false, autoIncrement: true, primaryKey: true, }, principal_id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: false, comment: '所属主体ID', references: { model: 'principals', key: 'id' }, }, order_id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: false, comment: '所属订单ID', references: { model: 'orders', key: 'id' }, onDelete: 'CASCADE', }, shop_id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: true, comment: '所属店铺ID(冗余订单的店铺,便于按店统计)', references: { model: 'shops', key: 'id' }, onDelete: 'SET NULL', }, // 与系统商品的关联:均允许为空,表示未解析出商品 product_id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: true, comment: '系统商品ID,为空表示未解析出商品', references: { model: 'products', key: 'id' }, onDelete: 'SET NULL', }, combo_product_id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: true, comment: '组合商品ID,为空表示非组合商品', references: { model: 'combo_products', key: 'id' }, onDelete: 'SET NULL', }, shop_product_id: { type: Sequelize.BIGINT.UNSIGNED, allowNull: true, comment: '命中的店铺商品ID', references: { model: 'shop_products', key: 'id' }, onDelete: 'SET NULL', }, item_type: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 0, comment: '明细类型:0未识别 1普通商品 2组合商品' }, parse_status: { type: Sequelize.TINYINT, allowNull: false, defaultValue: 0, comment: '解析状态:0未解析 1已解析 2解析失败' }, parse_msg: { type: Sequelize.STRING(255), allowNull: true, comment: '解析失败原因' }, // 平台侧原始商品信息(快照) outer_item_id: { type: Sequelize.STRING(100), allowNull: true, comment: '平台商品ID' }, outer_sku_id: { type: Sequelize.STRING(100), allowNull: true, comment: '平台规格ID' }, sku_code: { type: Sequelize.STRING(100), allowNull: true, comment: '商家编码' }, name: { type: Sequelize.STRING(255), allowNull: true, comment: '商品名称(快照)' }, spec: { type: Sequelize.STRING(255), allowNull: true, comment: '规格(快照)' }, image: { type: Sequelize.STRING(255), allowNull: true, comment: '图片(快照)' }, // 数量与金额 quantity: { type: Sequelize.DECIMAL(12, 3), allowNull: false, defaultValue: 0, comment: '数量' }, price: { type: Sequelize.DECIMAL(12, 2), allowNull: true, comment: '单价' }, total_amount: { type: Sequelize.DECIMAL(12, 2), allowNull: false, defaultValue: 0, comment: '明细金额' }, raw: { type: Sequelize.JSON, allowNull: true, comment: '平台返回的明细原始数据' }, created_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'), }, updated_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'), }, }, { charset: 'utf8mb4', collate: 'utf8mb4_unicode_ci', comment: '订单明细表' } ); await queryInterface.addIndex('order_items', { fields: [ 'order_id' ], name: 'idx_order_items_order' }); await queryInterface.addIndex('order_items', { fields: [ 'principal_id' ], name: 'idx_order_items_principal' }); await queryInterface.addIndex('order_items', { fields: [ 'shop_id' ], name: 'idx_order_items_shop' }); await queryInterface.addIndex('order_items', { fields: [ 'product_id' ], name: 'idx_order_items_product' }); await queryInterface.addIndex('order_items', { fields: [ 'principal_id', 'parse_status' ], name: 'idx_order_items_parse' }); }, async down(queryInterface) { await queryInterface.dropTable('order_items'); }, };