oms/backend/app/model/productStock.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

47 lines
2.2 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 表
* 库存按 item_type(存储类型,与 WMS itemType 对齐)分多行存储:ZC=正品 / CC=残次品
* 每个 (product_id, item_type) 一行:总数量(quantity) / 锁定数量(lock_quantity) / 可用数量(available = quantity - lock_quantity)
* - 入库单完成 -> 增加对应类型 quantity
* - 出库单推送 -> 增加对应类型 lock_quantity(占用可用)
* - WMS 发货通知(notify.delivery) -> 扣减对应类型 quantity 与 lock_quantity
* @param {Egg.Application} app egg application
* @return {object} productStock model
*/
module.exports = app => {
const { BIGINT, STRING, DECIMAL, DATE } = app.Sequelize;
const ProductStock = app.model.define('product_stock', {
id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' },
principal_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '所属主体ID' },
product_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '系统商品ID' },
item_type: { type: STRING(4), allowNull: false, defaultValue: 'ZC', comment: '存储类型:ZC=正品 CC=残次品(与 WMS itemType 对齐)' },
quantity: { type: DECIMAL(12, 3), allowNull: false, defaultValue: 0, comment: '总数量' },
lock_quantity: { type: DECIMAL(12, 3), allowNull: false, defaultValue: 0, comment: '锁定数量' },
available: { type: DECIMAL(12, 3), allowNull: false, defaultValue: 0, comment: '可用数量 = 总数量 - 锁定数量' },
created_at: {
type: DATE,
allowNull: false,
defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'),
},
updated_at: {
type: DATE,
allowNull: false,
defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
},
}, {
indexes: [
{ unique: true, name: 'uniq_product_stock_type', fields: [ 'product_id', 'item_type' ] },
],
});
ProductStock.associate = () => {
app.model.ProductStock.belongsTo(app.model.Principal, { foreignKey: 'principal_id', as: 'principal' });
app.model.ProductStock.belongsTo(app.model.Product, { foreignKey: 'product_id', as: 'product' });
};
return ProductStock;
};