oms/backend/database/migrations/20260912000100-create-product-stocks.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

59 lines
2.3 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';
/**
* 商品系统库存副表 product_stocks:
* 每个系统商品一行,记录总数量 / 锁定数量 / 可用数量(可用 = 总 - 锁定)
* - 入库单完成 -> 增加总数量
* - 出库单推送 -> 增加锁定数量(占用可用)
* - WMS 发货通知(notify.delivery) -> 扣减总数量与锁定数量
*/
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.createTable(
'product_stocks',
{
id: { type: Sequelize.BIGINT.UNSIGNED, autoIncrement: true, primaryKey: true },
principal_id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
comment: '所属主体ID',
references: { model: 'principals', key: 'id' },
onDelete: 'CASCADE',
},
product_id: {
type: Sequelize.BIGINT.UNSIGNED,
allowNull: false,
comment: '系统商品ID',
references: { model: 'products', key: 'id' },
onDelete: 'CASCADE',
},
quantity: { type: Sequelize.DECIMAL(12, 3), allowNull: false, defaultValue: 0, comment: '总数量' },
lock_quantity: { type: Sequelize.DECIMAL(12, 3), allowNull: false, defaultValue: 0, comment: '锁定数量' },
available: { type: Sequelize.DECIMAL(12, 3), allowNull: false, defaultValue: 0, 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('product_stocks', { fields: [ 'principal_id' ], name: 'idx_product_stocks_principal' });
await queryInterface.addIndex('product_stocks', { fields: [ 'product_id' ], name: 'idx_product_stocks_product' });
await queryInterface.addIndex('product_stocks', {
fields: [ 'principal_id', 'product_id' ],
unique: true,
name: 'uk_product_stocks_principal_product',
});
},
async down(queryInterface) {
await queryInterface.dropTable('product_stocks');
},
};