'use strict'; module.exports = app => { const { STRING, INTEGER, BIGINT, ENUM, DATE, TINYINT } = app.Sequelize; const Storage = app.model.define('wms_storage', { id: { type: BIGINT(20).UNSIGNED, primaryKey: true, autoIncrement: true, comment: '库存ID', }, warehouse_id: { type: BIGINT(20).UNSIGNED, allowNull: false, comment: '仓库ID', references: { model: 'wms_store', key: 'id', }, }, store_id: { type: BIGINT(20).UNSIGNED, allowNull: false, comment: '货主ID', references: { model: 'wms_store', key: 'id', }, }, space_id: { type: BIGINT(20).UNSIGNED, allowNull: false, comment: '库位ID', references: { model: 'wms_location_space', key: 'id', }, }, stock_id: { type: BIGINT(20), allowNull: false, comment: '商品SKU ID', references: { model: 'wms_stock_warehouse', key: 'id', }, }, batch_no: { type: STRING(100), allowNull: false, comment: '批次号', validate: { notEmpty: true, }, }, inventory_type: { type: ENUM('ZP', 'CC', 'JS', 'XS', 'ZT', 'DJ', 'FP'), allowNull: false, defaultValue: 'ZP', comment: '库存类型:ZP=正品, CC=残次, JS=机损, XS=箱损, ZT=在途库存, DJ=冻结, FP=废品', }, quantity: { type: INTEGER(12), allowNull: false, defaultValue: 0, comment: '库存数量', }, locked_quantity: { type: INTEGER(12), allowNull: false, defaultValue: 0, comment: '锁定数量', }, available_quantity: { type: INTEGER(12), allowNull: false, defaultValue: 0, comment: '可用数量', }, production_date: { type: DATE, allowNull: true, comment: '生产日期', }, expiry_date: { type: DATE, allowNull: true, comment: '过期日期', }, inbound_time: { type: DATE, allowNull: false, comment: '入库时间', }, inbound_order_no: { type: STRING(100), allowNull: false, comment: '入库单号', validate: { notEmpty: true, }, }, supplier_id: { type: BIGINT(20).UNSIGNED, allowNull: true, comment: '供应商ID', }, supplier_batch_no: { type: STRING(100), allowNull: true, comment: '供应商批次号', }, is_enabled: { type: TINYINT(1), allowNull: false, defaultValue: 1, comment: '是否启用', }, created_at: { type: DATE, allowNull: true, defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'), comment: '创建时间', }, updated_at: { type: DATE, allowNull: true, defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'), comment: '更新时间', }, delete_at: { type: DATE, allowNull: true, comment: '删除时间', }, }, { tableName: 'wms_storage', comment: '库存表', timestamps: false, indexes: [ { name: 'uk_location_sku_batch_type', unique: true, fields: [ 'space_id', 'stock_id', 'batch_no', 'inventory_type' ], }, { name: 'idx_warehouse_location', fields: [ 'warehouse_id', 'space_id' ], }, { name: 'idx_sku_batch', fields: [ 'stock_id', 'batch_no' ], }, { name: 'idx_inventory_type', fields: [ 'inventory_type' ], }, { name: 'idx_expiry_date', fields: [ 'expiry_date' ], }, { name: 'idx_inbound_order', fields: [ 'inbound_order_no' ], }, { name: 'idx_store_id', fields: [ 'store_id' ], }, { name: 'idx_is_enabled', fields: [ 'is_enabled' ], }, { name: 'idx_delete_at', fields: [ 'delete_at' ], }, { name: 'idx_warehouse_stock', fields: [ 'warehouse_id', 'stock_id' ], }, { name: 'idx_available_quantity', fields: [ 'available_quantity' ], }, ], }); // 关联关系 Storage.associate = function() { const { Storage, Store, LocationSpace, StockWarehouse } = app.model; // 关联仓库 Storage.belongsTo(Store, { foreignKey: 'warehouse_id', as: 'warehouse', }); // 关联货主 Storage.belongsTo(Store, { foreignKey: 'store_id', as: 'store', }); // 关联库位 Storage.belongsTo(LocationSpace, { foreignKey: 'space_id', as: 'space', }); // 关联商品SKU Storage.belongsTo(StockWarehouse, { foreignKey: 'stock_id', as: 'stock', }); }; // 实例方法 Storage.prototype.getAvailableQuantity = function() { return this.quantity - this.locked_quantity; }; Storage.prototype.isAvailable = function() { return this.is_enabled === 1 && this.available_quantity > 0; }; Storage.prototype.isLocked = function() { return this.locked_quantity > 0; }; Storage.prototype.canAllocate = function(quantity) { return this.is_enabled === 1 && this.available_quantity >= quantity; }; Storage.prototype.isNormalProduct = function() { return this.inventory_type === 'ZP'; }; Storage.prototype.isDefective = function() { return this.inventory_type === 'CC'; }; Storage.prototype.isExpired = function() { if (!this.expiry_date) return false; return new Date(this.expiry_date) < new Date(); }; // 静态方法 Storage.findByStockAndBatch = async function(app, stockId, batchNo, inventoryType = 'ZP') { return await app.model.Storage.findOne({ where: { stock_id: stockId, batch_no: batchNo, inventory_type: inventoryType, is_enabled: 1, delete_at: null, }, include: [ { model: app.model.LocationSpace, as: 'space', attributes: [ 'space_code', 'custom_name' ], }, { model: app.model.Store, as: 'warehouse', attributes: [ 'store_name' ], }, ], }); }; Storage.findBySpace = async function(app, spaceId) { return await app.model.Storage.findAll({ where: { space_id: spaceId, is_enabled: 1, delete_at: null, }, include: [ { model: app.model.StockWarehouse, as: 'stock', attributes: [ 'product_code', 'product_name' ], }, ], order: [[ 'inbound_time', 'ASC' ]], }); }; Storage.findAvailableByWarehouse = async function(app, warehouseId, stockId = null) { const where = { warehouse_id: warehouseId, is_enabled: 1, delete_at: null, available_quantity: { [app.Sequelize.Op.gt]: 0 }, }; if (stockId) { where.stock_id = stockId; } return await app.model.Storage.findAll({ where, include: [ { model: app.model.LocationSpace, as: 'space', attributes: [ 'space_code', 'custom_name' ], }, { model: app.model.StockWarehouse, as: 'stock', attributes: [ 'product_code', 'product_name' ], }, ], order: [ [ 'expiry_date', 'ASC' ], [ 'inbound_time', 'ASC' ], ], }); }; Storage.findExpiringSoon = async function(app, days = 30) { const expiryDate = new Date(); expiryDate.setDate(expiryDate.getDate() + days); return await app.model.Storage.findAll({ where: { expiry_date: { [app.Sequelize.Op.between]: [ new Date(), expiryDate ], }, is_enabled: 1, delete_at: null, quantity: { [app.Sequelize.Op.gt]: 0 }, }, include: [ { model: app.model.Store, as: 'warehouse', attributes: [ 'store_name' ], }, { model: app.model.StockWarehouse, as: 'stock', attributes: [ 'product_code', 'product_name' ], }, ], order: [[ 'expiry_date', 'ASC' ]], }); }; Storage.updateQuantity = async function(app, id, quantityChange, lockedChange = 0) { const storage = await app.model.Storage.findByPk(id); if (!storage) { throw new Error('库存记录不存在'); } const newQuantity = storage.quantity + quantityChange; const newLocked = storage.locked_quantity + lockedChange; if (newQuantity < 0) { throw new Error('库存数量不能为负数'); } if (newLocked < 0 || newLocked > newQuantity) { throw new Error('锁定数量无效'); } const updateData = { quantity: newQuantity, locked_quantity: newLocked, available_quantity: newQuantity - newLocked, updated_at: new Date(), }; await storage.update(updateData); return storage; }; Storage.lockQuantity = async function(app, id, quantity) { return await Storage.updateQuantity(app, id, 0, quantity); }; Storage.unlockQuantity = async function(app, id, quantity) { return await Storage.updateQuantity(app, id, 0, -quantity); }; Storage.allocateQuantity = async function(app, id, quantity) { const storage = await app.model.Storage.findByPk(id); if (!storage) { throw new Error('库存记录不存在'); } if (storage.available_quantity < quantity) { throw new Error('可用数量不足'); } return await Storage.updateQuantity(app, id, 0, quantity); }; Storage.getStockSummary = async function(app, warehouseId) { return await app.model.Storage.findAll({ attributes: [ 'stock_id', 'inventory_type', [ app.Sequelize.fn('SUM', app.Sequelize.col('quantity')), 'total_quantity' ], [ app.Sequelize.fn('SUM', app.Sequelize.col('locked_quantity')), 'total_locked' ], [ app.Sequelize.fn('SUM', app.Sequelize.col('available_quantity')), 'total_available' ], ], where: { warehouse_id: warehouseId, is_enabled: 1, delete_at: null, }, group: [ 'stock_id', 'inventory_type' ], raw: true, }); }; Storage.getSpaceUsage = async function(app, spaceId) { const result = await app.model.Storage.findAll({ attributes: [ 'inventory_type', [ app.Sequelize.fn('COUNT', app.Sequelize.col('id')), 'record_count' ], [ app.Sequelize.fn('SUM', app.Sequelize.col('quantity')), 'total_quantity' ], ], where: { space_id: spaceId, is_enabled: 1, delete_at: null, }, group: [ 'inventory_type' ], raw: true, }); const usage = { normal: 0, defective: 0, damaged: 0, frozen: 0, total: 0, }; result.forEach(item => { let type; switch (item.inventory_type) { case 'ZP': type = 'normal'; break; case 'CC': type = 'defective'; break; case 'JS': case 'XS': type = 'damaged'; break; case 'DJ': type = 'frozen'; break; default: return; } usage[type] = parseInt(item.total_quantity) || 0; usage.total += usage[type]; }); return usage; }; return Storage; };