oms/backend/database/migrations/20260910000400-create-combo-product-items.js
liufumingren cbd16752a5 feat(backend): 商品、店铺、组合商品、仓库与库存模块
- 商品/商品图片、店铺、店铺商品、组合商品及明细
- 仓库、仓库商品(WMS itemId 映射)、仓库接口配置与字段定义(warehouseApi/warehouseApiField)
- 库存:stock_items、product_stocks(ZC/CC 双维度)与库存重算工具(utils/recalcStock.js)
- 库存单据与对接日志:stock orders、api_outbound_logs
- 相关迁移:shops/shop_products/stock_orders/warehouses/warehouse_apis/warehouse_products/product_stocks/files-images 等表与字段
2026-09-13 02:23:39 +08:00

104 lines
3.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 组合商品明细表:把「组合商品 ← 系统商品」的组合关系(系统商品ID + 数量)单独落库,
* 取代原先 combo_products.items 的 JSON 冗余存储,便于库存展开、销量统计等后续复用。
* 约束:同一组合内同一系统商品仅一条记录。
*/
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable(
'combo_product_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' },
},
combo_product_id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
comment: '所属组合商品ID',
references: { model: 'combo_products', key: 'id' },
onDelete: 'CASCADE',
},
product_id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
comment: '系统商品ID(products.id)',
references: { model: 'products', key: 'id' },
onDelete: 'CASCADE',
},
quantity: {
type: Sequelize.INTEGER.UNSIGNED,
allowNull: false,
defaultValue: 1,
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('combo_product_items', {
fields: [ 'combo_product_id', 'product_id' ],
unique: true,
name: 'uk_combo_product_items_combo_product',
});
await queryInterface.addIndex('combo_product_items', { fields: [ 'principal_id' ], name: 'idx_combo_product_items_principal' });
await queryInterface.addIndex('combo_product_items', { fields: [ 'product_id' ], name: 'idx_combo_product_items_product' });
// 迁移历史 JSON 明细(combo_products.items)到关系表,确保存量数据不丢
const [ rows ] = await queryInterface.sequelize.query(
'SELECT id, principal_id, items FROM combo_products WHERE items IS NOT NULL'
);
for (const row of rows) {
let items = row.items;
if (typeof items === 'string') {
try {
items = JSON.parse(items);
} catch (e) {
items = null;
}
}
if (!Array.isArray(items)) continue;
for (const it of items) {
const productId = Number(it && it.product_id);
if (!productId) continue;
const quantity = Number(it.quantity) > 0 ? Math.floor(Number(it.quantity)) : 1;
await queryInterface.sequelize.query(
'INSERT INTO combo_product_items (principal_id, combo_product_id, product_id, quantity) VALUES (?, ?, ?, ?)',
{ replacements: [ row.principal_id, row.id, productId, quantity ] }
);
}
}
await queryInterface.removeColumn('combo_products', 'items');
},
async down(queryInterface, Sequelize) {
await queryInterface.addColumn('combo_products', 'items', {
type: Sequelize.JSON,
allowNull: true,
comment: '组合明细:[{ "product_id": 1, "quantity": 2 }],关联 products.id',
});
await queryInterface.dropTable('combo_product_items');
},
};