liufumingren 99367e5043 1
2026-06-06 05:15:46 +08:00

138 lines
4.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const MainService = require('../main');
/**
* OpenAPI 单品服务。
* 提供商品(单品)的同步与查询能力,映射到 StockStore 底层模型。
*/
class OpenapiSingleitemService 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) : '',
};
}
/**
* 同步(新增或更新)单品信息。
* 将外部系统的商品数据写入 StockStore,并自动同步到库存表。
* @param {Object} payload 调用参数。
* @param {Object} payload.body 请求体数据,仅需包含 item;warehouseCode 和 ownerCode 由 app_key 自动解析,调用方不得传入。
* @param {Object} payload.query 请求 query 数据,需包含 app_key。
* @return {Promise<Object>} 同步结果,成功时返回 itemId。
*/
async synchronize(payload = {}) {
const { body = {}, query = {} } = payload;
const { item } = body;
if (!item || !item.itemCode) {
return this.fail('item 参数缺失或不完整(缺少 itemCode)');
}
try {
const { storeId, warehouseId, appKey, warehouseCode, ownerCode } = await this.resolveAppInfo(query.app_key);
const requestData = {
warehouseCode: warehouseCode || '',
ownerCode: ownerCode || '',
item,
};
const stockData = await this.ctx.service.systems.stockStore.initQimenStockData(
requestData,
appKey,
storeId,
warehouseId
);
const result = await this.ctx.service.systems.stockStore.create(stockData);
return this.success('同步完成', { itemId: result.id });
} catch (error) {
return this.fail(`同步失败: ${error.message}`);
}
}
/**
* 查询单品详情。
* 根据 itemId 查询 StockStore 记录并转换为 OpenAPI 统一格式。
* @param {Object} payload 调用参数。
* @param {Object} payload.body 请求体数据,需包含 itemId。
* @param {Object} payload.query 请求 query 数据,需包含 app_key。
* @return {Promise<Object>} 查询结果,成功时返回商品详情。
*/
async query(payload = {}) {
const { body = {}, query = {} } = payload;
const { itemId } = body;
if (!itemId) {
return this.fail('itemId 参数缺失');
}
try {
// 验证 app_key 有效性
await this.resolveAppInfo(query.app_key);
const row = await this.ctx.model.StockStore.findByPk(itemId);
if (!row) {
return this.fail(`商品不存在,itemId: ${itemId}`);
}
const result = await this.ctx.service.systems.stockStore.convertToQimenFormat(row);
return this.success('查询成功', result);
} catch (error) {
return this.fail(`查询失败: ${error.message}`);
}
}
}
module.exports = OpenapiSingleitemService;