feat(storage): 新增按库位查询有量库存商品列表
- systems/storage/inventory.js 新增 getStorageListBySpaceId(spaceId): 校验库位归属当前仓库(区分「库位不存在」与「库位属于其他仓库」), 返回该库位 quantity>0 的库存并按 stock_id 汇总, 商品信息复用 stock_warehouse 通用链路(getStockDetails)补全属性 - systemMethods/storage/inventory.js 开放 listBySpaceId, 免权限/免鉴权/免日志名单同步补充
This commit is contained in:
parent
018bbd379d
commit
74b3f04b79
@ -13,9 +13,9 @@ class InventoryMethodService extends MainService {
|
||||
constructor(ctx) {
|
||||
super(ctx);
|
||||
const _noNeedLogin = '';
|
||||
const _noNeedRight = 'list,export,exportTaskProgress,listByStockId';
|
||||
const _noNeedLog = 'list,export,exportTaskProgress,listByStockId';
|
||||
const _noNeedAuth = 'list,quickPutaway,directPutaway,export,exportTaskProgress,listByStockId';
|
||||
const _noNeedRight = 'list,export,exportTaskProgress,listByStockId,listBySpaceId';
|
||||
const _noNeedLog = 'list,export,exportTaskProgress,listByStockId,listBySpaceId';
|
||||
const _noNeedAuth = 'list,quickPutaway,directPutaway,export,exportTaskProgress,listByStockId,listBySpaceId';
|
||||
this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth);
|
||||
}
|
||||
|
||||
@ -122,6 +122,20 @@ class InventoryMethodService extends MainService {
|
||||
return this.success('ok', result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 按库位查询库存商品列表:传入 space_id,判断该库位是否为当前仓库的库位,
|
||||
* 是则返回该库位下有量库存(quantity > 0)的商品列表(按 stock_id 汇总为一条)。
|
||||
* 商品信息统一走 stock_warehouse 通用链路(getStockDetails),返回 stock_warehouse 通用属性。
|
||||
* @param {Object} data 请求体
|
||||
* @param {number} data.space_id 库位ID(必填)
|
||||
* @return {Promise<Object>} 统一 success 包装
|
||||
*/
|
||||
async listBySpaceId(data = {}) {
|
||||
const { service } = this;
|
||||
const result = await service.systems.storage.inventory.getStorageListBySpaceId(data.space_id);
|
||||
return this.success('ok', result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 收货单快捷入库:将收货单的明细直接转为入库单,相同商品自动合并数量。
|
||||
* 要求货主配置中允许收货单快捷入库,否则直接返回失败。
|
||||
|
||||
@ -1531,6 +1531,202 @@ class InventoryService extends Service {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 按库位查询库存商品列表:传入 space_id,先判断该库位是否为当前仓库的库位,
|
||||
* 是则返回该库位下有量库存(quantity > 0)的商品列表,按商品(stock_id)汇总为一条。
|
||||
* 商品信息统一走 stock_warehouse 通用链路(getStockDetails),返回 stock_warehouse 通用属性
|
||||
* (item_code/item_name/sku_property/stock_unit/is_batch_mgmt/is_shelf_life_mgmt/
|
||||
* packing_requirements/need_recheck/barcodes/image_url 等)。
|
||||
*
|
||||
* @param {number} spaceId 库位ID(wms_location_space.id,必填)
|
||||
* @return {Promise<Object>} code/message/data 结构,data 为 { space, list }
|
||||
*/
|
||||
async getStorageListBySpaceId(spaceId) {
|
||||
const { ctx, app } = this;
|
||||
const { Op } = app.Sequelize;
|
||||
|
||||
// ────────── 1. 参数校验 ──────────
|
||||
const normalizedSpaceId = Number(spaceId);
|
||||
if (!Number.isInteger(normalizedSpaceId) || normalizedSpaceId <= 0) {
|
||||
return { code: 400, message: 'space_id 必填且必须为正整数', data: null };
|
||||
}
|
||||
|
||||
const warehouseId = ctx.user && ctx.user.store_id;
|
||||
if (!warehouseId) {
|
||||
return { code: 400, message: '无法获取当前仓库信息', data: null };
|
||||
}
|
||||
|
||||
// ────────── 2. 判断该库位是否属于当前仓库 ──────────
|
||||
const space = await ctx.model.LocationSpace.findOne({
|
||||
where: {
|
||||
id: normalizedSpaceId,
|
||||
warehouse_id: warehouseId,
|
||||
deleted_at: null,
|
||||
},
|
||||
attributes: [ 'id', 'warehouse_id', 'space_code', 'custom_name', 'space_status' ],
|
||||
raw: true,
|
||||
});
|
||||
|
||||
if (!space) {
|
||||
// 区分「库位不存在」与「库位存在但不属于当前仓库」
|
||||
const spaceInOtherWarehouse = await ctx.model.LocationSpace.findOne({
|
||||
where: { id: normalizedSpaceId, deleted_at: null },
|
||||
attributes: [ 'id', 'warehouse_id' ],
|
||||
raw: true,
|
||||
});
|
||||
if (spaceInOtherWarehouse) {
|
||||
return { code: 400, message: `库位 ${normalizedSpaceId} 不属于当前仓库`, data: null };
|
||||
}
|
||||
return { code: 404, message: `库位不存在: space_id=${normalizedSpaceId}`, data: null };
|
||||
}
|
||||
|
||||
const spaceInfo = {
|
||||
id: Number(space.id),
|
||||
warehouse_id: Number(space.warehouse_id),
|
||||
space_code: space.space_code || '',
|
||||
custom_name: space.custom_name || '',
|
||||
space_status: space.space_status || '',
|
||||
};
|
||||
|
||||
// ────────── 3. 查询该库位下有量库存明细(quantity > 0) ──────────
|
||||
const rows = await ctx.model.Storage.findAll({
|
||||
where: {
|
||||
warehouse_id: warehouseId,
|
||||
space_id: normalizedSpaceId,
|
||||
quantity: { [Op.gt]: 0 },
|
||||
is_enabled: 1,
|
||||
delete_at: null,
|
||||
},
|
||||
attributes: [
|
||||
'id', 'warehouse_id', 'store_id', 'space_id', 'stock_id',
|
||||
'batch_no', 'inventory_type', 'quantity', 'locked_quantity',
|
||||
'intercept_locked_quantity', 'available_quantity',
|
||||
'production_date', 'expiry_date', 'inbound_time',
|
||||
],
|
||||
order: [[ 'id', 'DESC' ]],
|
||||
raw: true,
|
||||
});
|
||||
|
||||
if (!rows.length) {
|
||||
return { code: 200, message: 'ok', data: { space: spaceInfo, list: [] } };
|
||||
}
|
||||
|
||||
// ────────── 4. 商品通用属性:统一走 stock_warehouse 通用链路(getStockDetails) ──────────
|
||||
const stockIds = [ ...new Set(rows.map(row => Number(row.stock_id)).filter(Boolean)) ];
|
||||
const stockDetailMap = new Map();
|
||||
try {
|
||||
const stockDetails = await ctx.service.systems.stockWarehouse.getStockDetails(stockIds, { warehouseId });
|
||||
for (const detail of (Array.isArray(stockDetails) ? stockDetails : [])) {
|
||||
stockDetailMap.set(Number(detail && detail.id || 0), detail);
|
||||
}
|
||||
} catch (error) {
|
||||
ctx.logger?.warn?.('[storage.inventory] getStorageListBySpaceId 查询商品通用属性失败,stock 降级为 null', {
|
||||
error: error.message,
|
||||
});
|
||||
}
|
||||
|
||||
// ────────── 5. 货主名称:按当前仓库收敛的签约表 store_remask ──────────
|
||||
const storeRemarkMap = new Map();
|
||||
const storeIds = [ ...new Set(rows.map(row => Number(row.store_id || 0)).filter(Boolean)) ];
|
||||
if (storeIds.length > 0) {
|
||||
try {
|
||||
const storeRows = await ctx.model.StoreWarehouse.findAll({
|
||||
where: { warehouse_id: warehouseId, store_id: { [Op.in]: storeIds } },
|
||||
attributes: [ 'store_id', 'store_remask' ],
|
||||
raw: true,
|
||||
});
|
||||
for (const row of storeRows) {
|
||||
storeRemarkMap.set(Number(row.store_id), row.store_remask || '');
|
||||
}
|
||||
} catch (error) {
|
||||
ctx.logger?.warn?.('[storage.inventory] getStorageListBySpaceId 查询货主名称失败,store_name 降级为 null', {
|
||||
error: error.message,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// ────────── 6. 按商品(stock_id)汇总为一条 ──────────
|
||||
const list = this._aggregateStorageRowsByStock(rows, spaceInfo, stockDetailMap, storeRemarkMap);
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
message: 'ok',
|
||||
data: { space: spaceInfo, list },
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 将同一库位下的库存明细行按商品(stock_id)汇总:汇总数量并统计批次数与最早/最晚入库时间。
|
||||
* 商品字段取自 stock_warehouse 通用属性(getStockDetails 返回值,未命中时为 null)。
|
||||
*
|
||||
* @param {Array<Object>} rows wms_storage 明细行(Sequelize 实例或 plain 对象)
|
||||
* @param {Object} [spaceInfo] 库位信息(space_code/custom_name 平铺到每行)
|
||||
* @param {Map<number, Object>} [stockDetailMap] stock_id → stock_warehouse 通用属性
|
||||
* @param {Map<number, string>} [storeRemarkMap] store_id → 货主名称(store_remask)
|
||||
* @return {Array<Object>} 按商品汇总后的列表
|
||||
*/
|
||||
_aggregateStorageRowsByStock(rows = [], spaceInfo = null, stockDetailMap = null, storeRemarkMap = null) {
|
||||
const { app } = this;
|
||||
const location = spaceInfo || {};
|
||||
const stockMap = stockDetailMap || new Map();
|
||||
const storeMap = storeRemarkMap || new Map();
|
||||
const groupedMap = new Map();
|
||||
|
||||
for (const item of rows) {
|
||||
const row = item && typeof item.get === 'function' ? item.get({ plain: true }) : item;
|
||||
const stockId = Number(row.stock_id || 0);
|
||||
if (!stockId) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const quantity = Number(row.quantity || 0);
|
||||
const lockedQuantity = Number(row.locked_quantity || 0);
|
||||
const interceptLockedQuantity = Number(row.intercept_locked_quantity || 0);
|
||||
const availableQuantity = Number(row.available_quantity || 0);
|
||||
const inboundTimeMs = row.inbound_time ? new Date(row.inbound_time).getTime() : null;
|
||||
|
||||
const existing = groupedMap.get(stockId);
|
||||
if (!existing) {
|
||||
groupedMap.set(stockId, {
|
||||
warehouse_id: row.warehouse_id,
|
||||
store_id: row.store_id,
|
||||
space_id: Number(row.space_id || 0),
|
||||
stock_id: stockId,
|
||||
quantity,
|
||||
locked_quantity: lockedQuantity,
|
||||
intercept_locked_quantity: interceptLockedQuantity,
|
||||
available_quantity: availableQuantity,
|
||||
batch_count: 1,
|
||||
first_inbound_time: inboundTimeMs,
|
||||
last_inbound_time: inboundTimeMs,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
existing.quantity += quantity;
|
||||
existing.locked_quantity += lockedQuantity;
|
||||
existing.intercept_locked_quantity += interceptLockedQuantity;
|
||||
existing.available_quantity += availableQuantity;
|
||||
existing.batch_count += 1;
|
||||
if (inboundTimeMs !== null && (existing.first_inbound_time === null || inboundTimeMs < existing.first_inbound_time)) {
|
||||
existing.first_inbound_time = inboundTimeMs;
|
||||
}
|
||||
if (inboundTimeMs !== null && (existing.last_inbound_time === null || inboundTimeMs > existing.last_inbound_time)) {
|
||||
existing.last_inbound_time = inboundTimeMs;
|
||||
}
|
||||
}
|
||||
|
||||
return [ ...groupedMap.values() ].map(row => ({
|
||||
...row,
|
||||
space_code: location.space_code || '',
|
||||
space_name: location.custom_name || location.space_code || '',
|
||||
store_name: storeMap.get(Number(row.store_id || 0)) || null,
|
||||
stock: stockMap.get(row.stock_id) || null,
|
||||
first_inbound_time: row.first_inbound_time ? app.formatDate(row.first_inbound_time) : '',
|
||||
last_inbound_time: row.last_inbound_time ? app.formatDate(row.last_inbound_time) : '',
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* 将库存明细行按库位(space_id)汇总:汇总 quantity/locked_quantity/available_quantity,
|
||||
* 统计批次数并取最早/最晚入库时间。汇总行不含批次与效期字段,保留商品/库位关联信息。
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user