'use strict'; /** * 商品系统库存(product_stocks)操作工具 * 库存按 item_type(存储类型,与 WMS itemType 对齐)分多行存储: * ZC=正品 / CC=残次品,每行:可用 = 总数量 - 锁定数量 * - increase / decrease:增加/减少某类型总数量 * - lock / unlock / deduct:占用与扣减(默认 ZC 正品维度,残次品一般只增减小计不动锁定) * 全部在事务内使用行锁(SELECT ... FOR UPDATE)保证并发安全 */ async function getOrCreate(ctx, principalId, productId, itemType = 'ZC', transaction) { let row = await ctx.model.ProductStock.findOne({ where: { principal_id: principalId, product_id: productId, item_type: itemType }, lock: true, transaction, }); if (!row) { row = await ctx.model.ProductStock.create( { principal_id: principalId, product_id: productId, item_type: itemType, quantity: 0, lock_quantity: 0, available: 0, }, { transaction }, ); } return row; } function recompute(row) { const qty = Number(row.quantity) || 0; const lock = Number(row.lock_quantity) || 0; row.available = qty - lock; } /** 增加某类型总数量 */ async function increase(ctx, principalId, productId, qty, transaction, itemType = 'ZC') { const n = Number(qty) || 0; if (n <= 0) return null; const row = await getOrCreate(ctx, principalId, productId, itemType, transaction); row.quantity = Number(row.quantity) + n; recompute(row); await row.save({ transaction }); return row; } /** 锁定某类型库存:可用不足时抛出「库存不足」(err.code = INSUFFICIENT_STOCK) */ async function lock(ctx, principalId, productId, qty, transaction, itemType = 'ZC') { const n = Number(qty) || 0; if (n <= 0) return null; const row = await getOrCreate(ctx, principalId, productId, itemType, transaction); const available = Number(row.quantity) - Number(row.lock_quantity); if (available < n) { const err = new Error('库存不足'); err.code = 'INSUFFICIENT_STOCK'; err.productId = productId; err.available = available; err.require = n; throw err; } row.lock_quantity = Number(row.lock_quantity) + n; recompute(row); await row.save({ transaction }); return row; } /** 解锁某类型库存(出库单取消/恢复时回滚锁定) */ async function unlock(ctx, principalId, productId, qty, transaction, itemType = 'ZC') { const n = Number(qty) || 0; if (n <= 0) return null; const row = await getOrCreate(ctx, principalId, productId, itemType, transaction); row.lock_quantity = Math.max(0, Number(row.lock_quantity) - n); recompute(row); await row.save({ transaction }); return row; } /** 发货扣减:总数量与锁定各减 qty */ async function deduct(ctx, principalId, productId, qty, transaction, itemType = 'ZC') { const n = Number(qty) || 0; if (n <= 0) return null; const row = await getOrCreate(ctx, principalId, productId, itemType, transaction); row.quantity = Math.max(0, Number(row.quantity) - n); row.lock_quantity = Math.max(0, Number(row.lock_quantity) - n); recompute(row); await row.save({ transaction }); return row; } /** 仅减少某类型总数量(库存同步覆盖用:系统多于外部,已直接完成的出库单,不动锁定) */ async function decrease(ctx, principalId, productId, qty, transaction, itemType = 'ZC') { const n = Number(qty) || 0; if (n <= 0) return null; const row = await getOrCreate(ctx, principalId, productId, itemType, transaction); row.quantity = Math.max(0, Number(row.quantity) - n); recompute(row); await row.save({ transaction }); return row; } /** 残次品:入库增加总数量(等价于 increase(..., 'CC')) */ async function increaseDefect(ctx, principalId, productId, qty, transaction) { return increase(ctx, principalId, productId, qty, transaction, 'CC'); } /** 残次品:仅减少总数量(等价于 decrease(..., 'CC')) */ async function decreaseDefect(ctx, principalId, productId, qty, transaction) { return decrease(ctx, principalId, productId, qty, transaction, 'CC'); } module.exports = { getOrCreate, increase, lock, unlock, deduct, decrease, increaseDefect, decreaseDefect };