liufumingren e76a6e3fa4 fix(order): 奇门/openapi 查单与取消接口跨分表查单
新增 app/lib/order_lookup.js 共享辅助函数(preloadOrderModels/findOrderAcrossTables/
findOrderByCodeAcrossTables),按 current→dispatch→history→outsourced 顺序命中即停;
模型预取置于事务外(工厂首次获取可能执行 DDL 隐式提交),事务内查询支持透传行锁。

改造 4 处接口:
- qimen deliveryorder.query / openapi deliveryorder.query:跨表查单,
  订单流转到配送/历史表后不再误报"未找到订单"(避免货主误判重推);
- qimen order.cancel / openapi order.cancel:跨表查找,命中非 current 表
  且 status=cancel 时幂等返回取消成功,其余情况返回"未找到对应的订单"。

回归验证(真实网关,脚本在 test/ 不入库):
- openapi:query 命中 dispatch、cancel 幂等成功、不存在单号报错
- qimen:同 4 场景全部符合预期
2026-09-20 22:12:35 +08:00

117 lines
3.7 KiB
JavaScript
Raw Permalink 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');
const { findOrderByCodeAcrossTables } = require('../../lib/order_lookup');
class OpenapiDeliveryorderService extends MainService {
/**
* 查询发货单。
* 上下文(storeId/warehouseId/appKey)已由 dispatchOpenapiMethod 统一注入。
* @param {Object} payload 调用参数。
* @param {Object} payload.body 请求体数据,需包含 deliveryOrderCode。
* @param {Object} payload.query 请求 query 数据。
* @return {Promise<Object>} 查询结果,成功时返回订单详情,失败时返回错误信息。
*/
async query(payload = {}) {
const { body = {} } = payload;
const { deliveryOrderCode } = body;
if (!deliveryOrderCode) {
return this.fail('deliveryOrderCode 参数缺失');
}
try {
const { storeId, warehouseId } = this.getInfo();
// 跨表查找:订单可能已流转到 dispatch/history/outsourced,仅查 current 会误报未找到
const { order } = await findOrderByCodeAcrossTables(this.ctx, warehouseId, {
delivery_order_code: deliveryOrderCode,
store_id: storeId,
});
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}`);
}
}
/**
* 创建发货单。
* 上下文(storeId/warehouseId/appKey)已由 dispatchOpenapiMethod 统一注入。
* @param {Object} payload 调用参数。
* @param {Object} payload.body 请求体数据,需包含 deliveryOrder 与 orderLines。
* @param {Object} payload.query 请求 query 数据。
* @return {Promise<Object>} 创建结果,成功时返回订单ID等,失败时返回错误信息。
*/
async create(payload = {}) {
const { body = {} } = payload;
const {
deliveryOrder,
orderLines,
need_oms_waybill: needOmsWaybillSnake,
needOmsWaybill,
} = body;
if (!deliveryOrder || !deliveryOrder.deliveryOrderCode) {
return this.fail('deliveryOrder 参数缺失或不完整');
}
if (!orderLines) {
return this.fail('orderLines 参数缺失');
}
try {
// need_oms_waybill 在请求根节点传入(不在 deliveryOrder 内),
// 兼容 snake_case / camelCase 两种写法,仅 openapi(API) 来源会落库生效。
const data = {
request: {
deliveryOrder,
orderLines,
need_oms_waybill: needOmsWaybillSnake ?? needOmsWaybill,
},
};
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: this.warehouseCode,
logisticsCode: '',
});
}
return this.fail(res.message, { createTime: this.app.formatDate(new Date()) });
} catch (error) {
return this.fail(`创建失败: ${error.message}`);
}
}
}
module.exports = OpenapiDeliveryorderService;