'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; };