107 lines
3.2 KiB
JavaScript
107 lines
3.2 KiB
JavaScript
'use strict';
|
||
|
||
const MainService = require('../main');
|
||
|
||
/**
|
||
* OpenAPI 库存服务。
|
||
* 提供库存查询能力,映射到 StockStore 底层模型。
|
||
*/
|
||
class OpenapiInventoryService extends MainService {
|
||
|
||
/**
|
||
* 通过 app_key 解析仓库与货主信息。
|
||
* 查询 ApiApp 并关联 StoreWarehouse 以获取 storeId、warehouseId。
|
||
* warehouseCode 优先使用 qimen_warehouse_code,为空时回退使用 warehouseId;
|
||
* ownerCode 优先使用 store_id,为空时回退使用空字符串。
|
||
* @param {string} appKey 开放接口 app_key。
|
||
* @return {Promise<Object>} 包含 storeId、warehouseId、appKey、customerId、warehouseCode、ownerCode 的信息对象。
|
||
*/
|
||
async resolveAppInfo(appKey) {
|
||
const { ctx, app } = this;
|
||
const normalizedAppKey = String(appKey || '').trim();
|
||
if (!normalizedAppKey) {
|
||
throw new Error('app_key 缺失');
|
||
}
|
||
|
||
const appConfig = await ctx.model.ApiApp.findOne({
|
||
attributes: [
|
||
'id',
|
||
'warehouse_id',
|
||
'qimen_warehouse_code',
|
||
[ app.Sequelize.col('contract.store_id'), 'store_id' ],
|
||
],
|
||
where: {
|
||
self_app_key: normalizedAppKey,
|
||
is_enabled: 1,
|
||
},
|
||
include: [
|
||
{
|
||
model: ctx.model.StoreWarehouse,
|
||
as: 'contract',
|
||
attributes: [],
|
||
required: false,
|
||
},
|
||
],
|
||
raw: true,
|
||
});
|
||
|
||
if (!appConfig || !appConfig.warehouse_id) {
|
||
throw new Error(`无法解析仓库信息,请检查 app_key: ${normalizedAppKey}`);
|
||
}
|
||
|
||
const warehouseId = appConfig.warehouse_id;
|
||
const storeId = appConfig.store_id || null;
|
||
|
||
return {
|
||
storeId,
|
||
warehouseId,
|
||
appKey: normalizedAppKey,
|
||
customerId: null,
|
||
warehouseCode: appConfig.qimen_warehouse_code || String(warehouseId),
|
||
ownerCode: storeId ? String(storeId) : '',
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 库存查询。
|
||
* 根据商品 ID 列表查询对应的库存信息,包括总库存、锁定量和可用量。
|
||
* @param {Object} payload 调用参数。
|
||
* @param {Object} payload.body 请求体数据,需包含 itemIds(商品 ID 数组)。
|
||
* @param {Object} payload.query 请求 query 数据,需包含 app_key。
|
||
* @return {Promise<Object>} 查询结果,成功时返回 items 数组。
|
||
*/
|
||
async query(payload = {}) {
|
||
const { body = {}, query = {} } = payload;
|
||
const { itemIds } = body;
|
||
|
||
if (!Array.isArray(itemIds) || itemIds.length === 0) {
|
||
return this.fail('itemIds 参数缺失或为空');
|
||
}
|
||
|
||
await this.resolveAppInfo(query.app_key);
|
||
const items = [];
|
||
|
||
for (const itemId of itemIds) {
|
||
const result = await this.ctx.service.systems.stockStore.getItemStorages(itemId);
|
||
if (result.success && result.data) {
|
||
items.push({
|
||
itemId: result.data.stock.id,
|
||
itemCode: result.data.stock.item_code,
|
||
warehouseCode: result.data.stock.warehouse_code,
|
||
quantity: result.data.storage.quantity,
|
||
lockQuantity: result.data.storage.lockedQuantity,
|
||
inventoryType: 'ZP',
|
||
});
|
||
}
|
||
}
|
||
|
||
if (items.length === 0) {
|
||
return this.fail('未查询到有效库存信息');
|
||
}
|
||
|
||
return this.success('库存查询成功', { items });
|
||
}
|
||
}
|
||
|
||
module.exports = OpenapiInventoryService;
|