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 场景全部符合预期
This commit is contained in:
parent
ce3e0185e1
commit
e76a6e3fa4
105
app/lib/order_lookup.js
Normal file
105
app/lib/order_lookup.js
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 订单分表跨表查询共享工具。
|
||||||
|
*
|
||||||
|
* 订单按仓库分表且按生命周期流转于多张物理表之间:
|
||||||
|
* current(待出库)→ dispatch(配送)→ history(历史归档),另有 outsourced(外包)。
|
||||||
|
* 各分表唯一索引均为 (delivery_order_code, store_id, warehouse_id),跨表间约束独立。
|
||||||
|
* 任何"按单号判断订单是否存在/可用"的场景都必须跨表遍历,否则会误判
|
||||||
|
* (如订单已流转到 dispatch 后,仅查 current 会返回"未找到订单",诱导调用方重推)。
|
||||||
|
*
|
||||||
|
* 注意事项:
|
||||||
|
* - 分表模型首次获取可能执行 DDL(隐式提交),因此必须在事务外调用 preloadOrderModels
|
||||||
|
* 完成模型预取,再在事务内用 findOrderAcrossTables 查询;
|
||||||
|
* - findOrderByCodeAcrossTables 内部会预取模型,仅适用于非事务场景。
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** 默认跨表查找顺序:current → dispatch → history → outsourced。 */
|
||||||
|
const TABLE_TYPE_ORDER = [ 'current', 'dispatch', 'history', 'outsourced' ];
|
||||||
|
|
||||||
|
/** 表位 → OrderDeliveryFactory 模型获取方法名。 */
|
||||||
|
const MODEL_GETTER_BY_TYPE = {
|
||||||
|
current: 'getCurrentModel',
|
||||||
|
dispatch: 'getDispatchModel',
|
||||||
|
history: 'getHistoryModel',
|
||||||
|
outsourced: 'getOutsourcedModel',
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 预取订单分表模型。
|
||||||
|
* 必须在事务外调用:模型首次获取可能执行 DDL(隐式提交)。
|
||||||
|
*
|
||||||
|
* @param {Object} ctx egg ctx。
|
||||||
|
* @param {string|number} warehouseId 仓库ID(分表后缀)。
|
||||||
|
* @param {string[]} [tableTypes] 需要预取的表位,默认四表。
|
||||||
|
* @return {Promise<Object>} 表位 → 模型 的映射(缺失模型不会出现在结果中)。
|
||||||
|
*/
|
||||||
|
async function preloadOrderModels(ctx, warehouseId, tableTypes = TABLE_TYPE_ORDER) {
|
||||||
|
const factory = ctx && ctx.model && ctx.model.OrderDeliveryFactory;
|
||||||
|
if (!factory) {
|
||||||
|
throw new Error('OrderDeliveryFactory 不可用,无法跨表查询订单');
|
||||||
|
}
|
||||||
|
const models = {};
|
||||||
|
for (const tableType of tableTypes) {
|
||||||
|
const getter = MODEL_GETTER_BY_TYPE[tableType];
|
||||||
|
if (!getter || typeof factory[getter] !== 'function') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const model = await factory[getter](warehouseId);
|
||||||
|
if (model && typeof model.findOne === 'function') {
|
||||||
|
models[tableType] = model;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return models;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 跨表按条件查找订单,按 tableTypes 顺序命中即停。
|
||||||
|
*
|
||||||
|
* @param {Object} models preloadOrderModels 返回的表位 → 模型映射。
|
||||||
|
* @param {Object} where 查询条件(findOne where,例如 delivery_order_code + store_id + warehouse_id)。
|
||||||
|
* @param {Object} [options] 可选配置。
|
||||||
|
* @param {string[]} [options.tableTypes] 查找表位顺序,默认四表。
|
||||||
|
* @param {Object} [options.findOptions] 透传给 findOne 的其余选项(transaction/lock/attributes 等)。
|
||||||
|
* @return {Promise<{ order: Object|null, location: string|null }>} 命中订单与所在表位;未命中 order=null。
|
||||||
|
*/
|
||||||
|
async function findOrderAcrossTables(models, where, options = {}) {
|
||||||
|
const { tableTypes = TABLE_TYPE_ORDER, findOptions = {} } = options;
|
||||||
|
for (const tableType of tableTypes) {
|
||||||
|
const model = models && models[tableType];
|
||||||
|
if (!model || typeof model.findOne !== 'function') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const order = await model.findOne({
|
||||||
|
where,
|
||||||
|
...findOptions,
|
||||||
|
});
|
||||||
|
if (order) {
|
||||||
|
return { order, location: tableType };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return { order: null, location: null };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 便捷入口:预取模型后按条件跨表查找(仅限非事务场景)。
|
||||||
|
* 事务内请使用 preloadOrderModels + findOrderAcrossTables。
|
||||||
|
*
|
||||||
|
* @param {Object} ctx egg ctx。
|
||||||
|
* @param {string|number} warehouseId 仓库ID。
|
||||||
|
* @param {Object} where 查询条件。
|
||||||
|
* @param {Object} [options] 同 findOrderAcrossTables 的 options,另支持 tableTypes 用于预取过滤。
|
||||||
|
* @return {Promise<{ order: Object|null, location: string|null }>} 命中订单与所在表位;未命中 order 为 null。
|
||||||
|
*/
|
||||||
|
async function findOrderByCodeAcrossTables(ctx, warehouseId, where, options = {}) {
|
||||||
|
const models = await preloadOrderModels(ctx, warehouseId, options.tableTypes);
|
||||||
|
return findOrderAcrossTables(models, where, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
TABLE_TYPE_ORDER,
|
||||||
|
preloadOrderModels,
|
||||||
|
findOrderAcrossTables,
|
||||||
|
findOrderByCodeAcrossTables,
|
||||||
|
};
|
||||||
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
const MainService = require('../main');
|
const MainService = require('../main');
|
||||||
|
|
||||||
|
const { findOrderByCodeAcrossTables } = require('../../lib/order_lookup');
|
||||||
|
|
||||||
class OpenapiDeliveryorderService extends MainService {
|
class OpenapiDeliveryorderService extends MainService {
|
||||||
/**
|
/**
|
||||||
* 查询发货单。
|
* 查询发货单。
|
||||||
@ -21,12 +23,10 @@ class OpenapiDeliveryorderService extends MainService {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const { storeId, warehouseId } = this.getInfo();
|
const { storeId, warehouseId } = this.getInfo();
|
||||||
const DeliveryModel = await this.ctx.model.OrderDeliveryFactory.getCurrentModel(warehouseId);
|
// 跨表查找:订单可能已流转到 dispatch/history/outsourced,仅查 current 会误报未找到
|
||||||
const order = await DeliveryModel.findOne({
|
const { order } = await findOrderByCodeAcrossTables(this.ctx, warehouseId, {
|
||||||
where: {
|
delivery_order_code: deliveryOrderCode,
|
||||||
delivery_order_code: deliveryOrderCode,
|
store_id: storeId,
|
||||||
store_id: storeId,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (order) {
|
if (order) {
|
||||||
|
|||||||
@ -4,6 +4,7 @@ const MainService = require('../main');
|
|||||||
|
|
||||||
const { getInterceptRequestBlockReason, resolveInterceptRequestGuardInfo } = require('../../lib/intercept_guard');
|
const { getInterceptRequestBlockReason, resolveInterceptRequestGuardInfo } = require('../../lib/intercept_guard');
|
||||||
const { buildNoticeTimePatch } = require('../../lib/intercept_time');
|
const { buildNoticeTimePatch } = require('../../lib/intercept_time');
|
||||||
|
const { preloadOrderModels, findOrderAcrossTables } = require('../../lib/order_lookup');
|
||||||
|
|
||||||
class OpenapiOrderService extends MainService {
|
class OpenapiOrderService extends MainService {
|
||||||
/**
|
/**
|
||||||
@ -86,16 +87,20 @@ class OpenapiOrderService extends MainService {
|
|||||||
const { storeId, warehouseId, appKey: ctxAppKey } = this.getInfo();
|
const { storeId, warehouseId, appKey: ctxAppKey } = this.getInfo();
|
||||||
|
|
||||||
const DeliveryModel = await ctx.model.OrderDeliveryFactory.getCurrentModel(warehouseId);
|
const DeliveryModel = await ctx.model.OrderDeliveryFactory.getCurrentModel(warehouseId);
|
||||||
|
// 模型预取必须在事务外(首次获取可能执行 DDL 隐式提交)
|
||||||
|
const orderModels = await preloadOrderModels(ctx, warehouseId);
|
||||||
const transaction = await ctx.model.transaction();
|
const transaction = await ctx.model.transaction();
|
||||||
try {
|
try {
|
||||||
const existingOrder = await DeliveryModel.findOne({
|
// 跨表查找:current → dispatch → history → outsourced,命中即停
|
||||||
where: {
|
const { order: existingOrder, location: existingLocation } = await findOrderAcrossTables(orderModels, {
|
||||||
delivery_order_code: normalizedOrderCode,
|
delivery_order_code: normalizedOrderCode,
|
||||||
warehouse_id: warehouseId,
|
warehouse_id: warehouseId,
|
||||||
store_id: storeId,
|
store_id: storeId,
|
||||||
|
}, {
|
||||||
|
findOptions: {
|
||||||
|
transaction,
|
||||||
|
lock: transaction.LOCK.UPDATE,
|
||||||
},
|
},
|
||||||
transaction,
|
|
||||||
lock: transaction.LOCK.UPDATE,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!existingOrder) {
|
if (!existingOrder) {
|
||||||
@ -103,6 +108,17 @@ class OpenapiOrderService extends MainService {
|
|||||||
return this.fail('未找到对应的订单');
|
return this.fail('未找到对应的订单');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 订单已流转出 current(dispatch/history/outsourced):
|
||||||
|
// 仅当已取消时视为取消成功(幂等),否则无法在待出库表中取消
|
||||||
|
if (existingLocation !== 'current') {
|
||||||
|
if (String(existingOrder.status || '').trim() === 'cancel') {
|
||||||
|
await transaction.rollback();
|
||||||
|
return this.success('取消订单成功');
|
||||||
|
}
|
||||||
|
await transaction.rollback();
|
||||||
|
return this.fail('未找到对应的订单');
|
||||||
|
}
|
||||||
|
|
||||||
const pickStatus = String(existingOrder.pick_status || 'none').trim();
|
const pickStatus = String(existingOrder.pick_status || 'none').trim();
|
||||||
const printStatus = String(existingOrder.print_status || 'none').trim();
|
const printStatus = String(existingOrder.print_status || 'none').trim();
|
||||||
|
|
||||||
|
|||||||
@ -2,10 +2,18 @@
|
|||||||
|
|
||||||
const MainService = require('../main');
|
const MainService = require('../main');
|
||||||
|
|
||||||
|
const { findOrderByCodeAcrossTables } = require('../../lib/order_lookup');
|
||||||
|
|
||||||
class deliveryorderServer extends MainService {
|
class deliveryorderServer extends MainService {
|
||||||
async query(data) {
|
async query(data) {
|
||||||
const DeliveryModel = await this.ctx.model.OrderDeliveryFactory.getCurrentModel(this.warehouseId);
|
const orderCode = String(data?.request?.orderCode || '').trim();
|
||||||
const order = await DeliveryModel.findOne({ where: { delivery_order_code: data.request.orderCode } });
|
if (!orderCode) {
|
||||||
|
return this.fail('订单号不能为空');
|
||||||
|
}
|
||||||
|
// 跨表查找:订单可能已流转到 dispatch/history/outsourced,仅查 current 会误报未找到
|
||||||
|
const { order } = await findOrderByCodeAcrossTables(this.ctx, this.warehouseId, {
|
||||||
|
delivery_order_code: orderCode,
|
||||||
|
});
|
||||||
const totalLines = 0;
|
const totalLines = 0;
|
||||||
if (order) {
|
if (order) {
|
||||||
return this.success('成功', {
|
return this.success('成功', {
|
||||||
|
|||||||
@ -4,6 +4,7 @@ const MainService = require('../main');
|
|||||||
|
|
||||||
const { getInterceptRequestBlockReason, resolveInterceptRequestGuardInfo } = require('../../lib/intercept_guard');
|
const { getInterceptRequestBlockReason, resolveInterceptRequestGuardInfo } = require('../../lib/intercept_guard');
|
||||||
const { buildNoticeTimePatch } = require('../../lib/intercept_time');
|
const { buildNoticeTimePatch } = require('../../lib/intercept_time');
|
||||||
|
const { preloadOrderModels, findOrderAcrossTables } = require('../../lib/order_lookup');
|
||||||
|
|
||||||
class orderServer extends MainService {
|
class orderServer extends MainService {
|
||||||
|
|
||||||
@ -200,38 +201,34 @@ class orderServer extends MainService {
|
|||||||
return this.fail('订单号不能为空');
|
return this.fail('订单号不能为空');
|
||||||
}
|
}
|
||||||
|
|
||||||
const DeliveryModel = await ctx.model.OrderDeliveryFactory.getCurrentModel(info.warehouseId);
|
// 模型预取必须在事务外(首次获取可能执行 DDL 隐式提交)
|
||||||
|
const orderModels = await preloadOrderModels(ctx, info.warehouseId);
|
||||||
const transaction = await ctx.model.transaction();
|
const transaction = await ctx.model.transaction();
|
||||||
try {
|
try {
|
||||||
const existingOrder = await DeliveryModel.findOne({
|
// 跨表查找:current → dispatch → history → outsourced,命中即停
|
||||||
where: {
|
const { order: existingOrder, location: existingLocation } = await findOrderAcrossTables(orderModels, {
|
||||||
delivery_order_code: orderCode,
|
delivery_order_code: orderCode,
|
||||||
warehouse_id: info.warehouseId,
|
warehouse_id: info.warehouseId,
|
||||||
store_id: info.storeId,
|
store_id: info.storeId,
|
||||||
|
}, {
|
||||||
|
findOptions: {
|
||||||
|
transaction,
|
||||||
|
lock: transaction.LOCK.UPDATE,
|
||||||
},
|
},
|
||||||
transaction,
|
|
||||||
lock: transaction.LOCK.UPDATE,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!existingOrder) {
|
if (!existingOrder) {
|
||||||
// current 表未命中时回退查询配送域表(dispatch):
|
await transaction.rollback();
|
||||||
// 订单已流转到配送表中且已取消,视为取消成功(幂等),直接返回
|
return this.fail('未找到对应的订单');
|
||||||
const DispatchModel = await ctx.model.OrderDeliveryFactory.getDispatchModel(info.warehouseId);
|
}
|
||||||
const dispatchOrder = await DispatchModel.findOne({
|
|
||||||
where: {
|
|
||||||
delivery_order_code: orderCode,
|
|
||||||
warehouse_id: info.warehouseId,
|
|
||||||
store_id: info.storeId,
|
|
||||||
},
|
|
||||||
transaction,
|
|
||||||
lock: transaction.LOCK.UPDATE,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (dispatchOrder && String(dispatchOrder.status || '').trim() === 'cancel') {
|
// 订单已流转出 current(dispatch/history/outsourced):
|
||||||
|
// 仅当已取消时视为取消成功(幂等),否则无法在待出库表中取消
|
||||||
|
if (existingLocation !== 'current') {
|
||||||
|
if (String(existingOrder.status || '').trim() === 'cancel') {
|
||||||
await transaction.rollback();
|
await transaction.rollback();
|
||||||
return this.success('取消订单成功');
|
return this.success('取消订单成功');
|
||||||
}
|
}
|
||||||
|
|
||||||
await transaction.rollback();
|
await transaction.rollback();
|
||||||
return this.fail('未找到对应的订单');
|
return this.fail('未找到对应的订单');
|
||||||
}
|
}
|
||||||
@ -464,7 +461,7 @@ class orderServer extends MainService {
|
|||||||
await ctx.service.systemMethods.order.wave.replaceOrderWaveIndexByWaveIds({
|
await ctx.service.systemMethods.order.wave.replaceOrderWaveIndexByWaveIds({
|
||||||
warehouseId: info.warehouseId,
|
warehouseId: info.warehouseId,
|
||||||
waveIds: [ existingWaveId ],
|
waveIds: [ existingWaveId ],
|
||||||
orderModel: DeliveryModel,
|
orderModel: orderModels.current,
|
||||||
transaction,
|
transaction,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user