'use strict'; const Service = require('egg').Service; class IndexService extends Service { async findByStockAndBatch(stockId, batchNo, inventoryType = 'ZP') { const { ctx } = this; return await ctx.model.Storage.findOne({ where: { stock_id: stockId, batch_no: batchNo, inventory_type: inventoryType, is_enabled: 1, delete_at: null, }, include: [ { model: ctx.model.LocationSpace, as: 'space', attributes: [ 'space_code', 'custom_name' ], }, { model: ctx.model.Store, as: 'warehouse', attributes: [[ 'company_name', 'store_name' ]], }, ], }); } async findBySpace(spaceId) { const { ctx } = this; return await ctx.model.Storage.findAll({ where: { space_id: spaceId, is_enabled: 1, delete_at: null, }, include: [ { model: ctx.model.StockWarehouse, as: 'stock', attributes: [ 'product_code', 'product_name' ], }, ], order: [[ 'inbound_time', 'ASC' ]], }); } async findAvailableByWarehouse(warehouseId, stockId = null) { const { ctx, app } = this; 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 ctx.model.Storage.findAll({ where, include: [ { model: ctx.model.LocationSpace, as: 'space', attributes: [ 'space_code', 'custom_name' ], }, { model: ctx.model.StockWarehouse, as: 'stock', attributes: [ 'product_code', 'product_name' ], }, ], order: [ [ 'expiry_date', 'ASC' ], [ 'inbound_time', 'ASC' ], ], }); } async findExpiringSoon(days = 30) { const { ctx, app } = this; const expiryDate = new Date(); expiryDate.setDate(expiryDate.getDate() + days); return await ctx.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: ctx.model.Store, as: 'warehouse', attributes: [[ 'company_name', 'store_name' ]], }, { model: ctx.model.StockWarehouse, as: 'stock', attributes: [ 'product_code', 'product_name' ], }, ], order: [[ 'expiry_date', 'ASC' ]], }); } async updateQuantity(id, quantityChange, lockedChange = 0, options = {}) { const { ctx } = this; const applyUpdate = async transaction => { const storage = await ctx.model.Storage.findByPk(id, { transaction, lock: transaction ? transaction.LOCK.UPDATE : undefined, }); 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, { transaction, }); return storage; }; if (options.transaction) { return await applyUpdate(options.transaction); } return await ctx.model.transaction(async transaction => { return await applyUpdate(transaction); }); } /** * 锁定指定库存记录的可用数量。 * * @param {number} id 库存记录 ID。 * @param {number} quantity 需要锁定的数量。 * @param {Object} [options={}] 执行上下文与事务参数。 * @param {Object} [options.transaction] Sequelize 事务对象。 * @param {string} [options.source] 调用来源标识。 * @param {string} [options.caller] 调用方标识。 * @param {string} [options.from] 来源模块标识。 * @param {string} [options.action] 操作标识。 * @return {Promise} 更新后的库存记录。 */ async lockQuantity(id, quantity, options = {}) { return await this.updateQuantity(id, 0, quantity, options); } async unlockQuantity(id, quantity, options = {}) { return await this.updateQuantity(id, 0, -quantity, options); } async allocateQuantity(id, quantity, options = {}) { const { ctx } = this; const storage = await ctx.model.Storage.findByPk(id, { transaction: options.transaction, }); if (!storage) { throw new Error('库存记录不存在'); } if (storage.available_quantity < quantity) { throw new Error('可用数量不足'); } return await this.updateQuantity(id, 0, quantity, options); } async getStockSummary(warehouseId) { const { ctx, app } = this; return await ctx.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, }); } async getSpaceUsage(spaceId) { const { ctx, app } = this; const result = await ctx.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, 10) || 0; usage.total += usage[type]; }); return usage; } } module.exports = IndexService;