WMS_MAIN/app/service/openapiMethods/deliveryorder.js
liufumingren 5ef99e3bed 1
2026-07-01 14:00:59 +08:00

160 lines
4.6 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');
class OpenapiDeliveryorderService extends MainService {
/**
* 通过 app_key 解析仓库与货主信息。
* 查询 ApiApp 并关联 StoreWarehouse 以获取 storeId、warehouseId。
* @param {string} appKey 开放接口 app_key。
* @return {Promise<Object>} 包含 storeId、warehouseId、appKey、customerId、warehouseCode 的信息对象。
*/
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',
[ 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}`);
}
return {
storeId: appConfig.store_id || null,
warehouseId: appConfig.warehouse_id,
appKey: normalizedAppKey,
customerId: null,
warehouseCode: '',
};
}
/**
* 查询发货单。
* @param {Object} payload 调用参数。
* @param {Object} payload.body 请求体数据,需包含 deliveryOrderCode。
* @param {Object} payload.query 请求 query 数据,需包含 app_key。
* @return {Promise<Object>} 查询结果,成功时返回订单详情,失败时返回错误信息。
*/
async query(payload = {}) {
const { body = {}, query = {} } = payload;
const { deliveryOrderCode } = body;
if (!deliveryOrderCode) {
return this.fail('deliveryOrderCode 参数缺失');
}
try {
const info = await this.resolveAppInfo(query.app_key);
const DeliveryModel = await this.ctx.model.OrderDeliveryFactory.getCurrentModel(info.warehouseId);
const order = await DeliveryModel.findOne({
where: { delivery_order_code: deliveryOrderCode },
});
if (order) {
return this.success('成功', {
totalLines: 0,
deliveryOrder: {
deliveryOrderCode: order.delivery_order_code,
deliveryOrderId: order.delivery_order_code,
warehouseCode: order.warehouse_code,
orderType: order.order_type,
status: order.status,
orderConfirmTime: order.order_confirm_time,
operatorCode: 0,
operatorName: '',
operateTime: order.update_at,
},
packages: {
package: {},
},
orderLines: [],
});
}
return this.fail('未找到订单');
} catch (error) {
return this.fail(`查询失败: ${error.message}`);
}
}
/**
* 创建发货单。
* @param {Object} payload 调用参数。
* @param {Object} payload.body 请求体数据,需包含 deliveryOrder 与 orderLines。
* @param {Object} payload.query 请求 query 数据,需包含 app_key。
* @return {Promise<Object>} 创建结果,成功时返回订单ID等,失败时返回错误信息。
*/
async create(payload = {}) {
const { body = {}, query = {} } = payload;
const { deliveryOrder, orderLines } = body;
if (!deliveryOrder || !deliveryOrder.deliveryOrderCode) {
return this.fail('deliveryOrder 参数缺失或不完整');
}
if (!orderLines) {
return this.fail('orderLines 参数缺失');
}
try {
const info = await this.resolveAppInfo(query.app_key);
this.setInfo(
info.storeId,
info.warehouseId,
info.appKey,
info.customerId,
info.warehouseCode
);
const data = {
request: {
deliveryOrder,
orderLines,
},
};
const res = await this.ctx.service.systems.deliveryOrder.addOrder(
data,
this.getInfo(),
'API'
);
if (res.success) {
return this.success(res.message, {
createTime: this.app.formatDate(new Date()),
deliveryOrderId: res.data.orderId,
warehouseCode: info.warehouseCode,
logisticsCode: '',
});
}
return this.fail(res.message, { createTime: this.app.formatDate(new Date()) });
} catch (error) {
return this.fail(`创建失败: ${error.message}`);
}
}
}
module.exports = OpenapiDeliveryorderService;