- 统计列 items/items_hash/items_crc 均为 wms_order_delivery_* 二级索引列:
先插占位值再 UPDATE 回写会让并发建单在同一索引区间反复「删标记旧条目 + 插入新条目」,
批量推单高并发下必然死锁(生产 2026-09-10 单批 354 个事务因此失败)
→ 新增 buildOrderStats,统计列与主表 INSERT 同一条语句写入;
快递策略命中时只 UPDATE 非索引的快递字段
- 建单顺序调整为先写明细、后写主表(库存预占失败直接回滚,不留悬挂主表行)
- 重试策略:最多 3 次;主键冲突修复水位后重试,新增 isRetryableLockError
(1213/1205/deadlock/lock wait timeout)带抖动退避重试,
失败文案统一为「重试后仍未成功,请稍后重试」
- 同文件一并包含 need_oms_waybill 写入与取号分流(新增 normalizeNeedOmsWaybill、
prepareOrderData 第 7 参、appendNeedOmsWaybillWaitLog 与 asyncProcessOrderCreated 分流),
该字段的模型/迁移/入口见相邻提交
- test: delivery_order.addOrder.test.js 补 allocateOrderId 桩、按新写入顺序断言,
并覆盖 need_oms_waybill 根节点取值/归一化
372 lines
11 KiB
JavaScript
372 lines
11 KiB
JavaScript
'use strict';
|
||
|
||
const assert = require('node:assert/strict');
|
||
const DeliveryOrderService = require('../../../../app/service/systems/delivery_order');
|
||
|
||
/**
|
||
* 创建 addOrder 测试桩。
|
||
*
|
||
* @return {Object} 测试上下文、服务实例与调用记录。
|
||
*/
|
||
function createHarness() {
|
||
const sequence = [];
|
||
const operationLogCalls = [];
|
||
const cacheStore = new Map();
|
||
const captures = {};
|
||
|
||
const transaction = {
|
||
commitCalls: 0,
|
||
rollbackCalls: 0,
|
||
async commit() {
|
||
this.commitCalls += 1;
|
||
sequence.push('transaction.commit');
|
||
},
|
||
async rollback() {
|
||
this.rollbackCalls += 1;
|
||
sequence.push('transaction.rollback');
|
||
},
|
||
};
|
||
|
||
const deliveryModel = {
|
||
async findOne() {
|
||
sequence.push('delivery.findOne');
|
||
return null;
|
||
},
|
||
async create(orderData) {
|
||
sequence.push('delivery.create');
|
||
captures.deliveryCreateData = { ...orderData };
|
||
return {
|
||
id: 7788,
|
||
...orderData,
|
||
};
|
||
},
|
||
async update(values) {
|
||
sequence.push('delivery.update');
|
||
return [ 1, [ values ]];
|
||
},
|
||
};
|
||
|
||
const appLogger = {
|
||
debug() {},
|
||
info() {},
|
||
warn() {},
|
||
error() {},
|
||
};
|
||
|
||
const app = {
|
||
logger: appLogger,
|
||
getCacheAsync: async cacheKey => cacheStore.get(cacheKey),
|
||
setCacheAsync: async (cacheKey, value) => {
|
||
cacheStore.set(cacheKey, value);
|
||
},
|
||
delCacheAsync: async cacheKey => {
|
||
cacheStore.delete(cacheKey);
|
||
},
|
||
};
|
||
|
||
const ctx = {
|
||
app,
|
||
logger: appLogger,
|
||
user: {
|
||
employees_id: 10001,
|
||
employees_name: '测试员',
|
||
},
|
||
model: {
|
||
async transaction() {
|
||
return transaction;
|
||
},
|
||
OrderDeliveryFactory: {
|
||
async getCurrentModel() {
|
||
return deliveryModel;
|
||
},
|
||
async ensureLocationSequenceTable() {
|
||
sequence.push('delivery.ensureLocationSequenceTable');
|
||
},
|
||
async allocateOrderId() {
|
||
sequence.push('delivery.allocateOrderId');
|
||
return 7788;
|
||
},
|
||
},
|
||
Store: {
|
||
async findByPk() {
|
||
return {
|
||
id: 1000000,
|
||
subject_config: {
|
||
default_platform_code: 'TB',
|
||
default_express_code: 'STO',
|
||
},
|
||
};
|
||
},
|
||
},
|
||
StoreWarehouse: {
|
||
async findOne() {
|
||
return {
|
||
id: 77,
|
||
};
|
||
},
|
||
},
|
||
},
|
||
service: {
|
||
systems: {
|
||
platform: {
|
||
async getPlatformList() {
|
||
return [
|
||
{ code: 'TB', name: '淘宝' },
|
||
];
|
||
},
|
||
},
|
||
express: {
|
||
async getExpressList() {
|
||
return [
|
||
{ code: 'STO', name: '申通' },
|
||
];
|
||
},
|
||
},
|
||
storeWarehouse: {
|
||
async getContractConfig() {
|
||
return {
|
||
policy_config: {
|
||
need_order_approval: 0,
|
||
default_platform_code: 'TB',
|
||
default_express_code: 'STO',
|
||
},
|
||
};
|
||
},
|
||
},
|
||
operationLog: {
|
||
async createOperationLogs(paramsList = []) {
|
||
for (const params of paramsList) {
|
||
operationLogCalls.push(params);
|
||
sequence.push(`operationLog:${params.operationCode}`);
|
||
}
|
||
return paramsList.map((params, index) => ({
|
||
id: index + 1,
|
||
...params,
|
||
}));
|
||
},
|
||
async createOperationLog(params) {
|
||
operationLogCalls.push(params);
|
||
sequence.push(`operationLog:${params.operationCode}`);
|
||
return {
|
||
id: operationLogCalls.length,
|
||
};
|
||
},
|
||
},
|
||
},
|
||
},
|
||
};
|
||
|
||
app.model = ctx.model;
|
||
|
||
const service = new DeliveryOrderService(ctx);
|
||
service.addOrderLines = async () => ({
|
||
items_str: 'sku-1',
|
||
stock_count: 1,
|
||
item_total_count: 1,
|
||
total_weight: 2,
|
||
});
|
||
service.addOrderToQueue = async () => {
|
||
sequence.push('order.queue');
|
||
};
|
||
|
||
return {
|
||
app,
|
||
ctx,
|
||
service,
|
||
transaction,
|
||
sequence,
|
||
operationLogCalls,
|
||
captures,
|
||
};
|
||
}
|
||
|
||
describe('test/app/service/systems/delivery_order.addOrder.test.js', () => {
|
||
it('should create order with default platform fallback log after commit', async () => {
|
||
const { service, sequence, operationLogCalls, transaction, captures } = createHarness();
|
||
const result = await service.addOrder({
|
||
request: {
|
||
deliveryOrder: {
|
||
deliveryOrderCode: 'ORDER-001',
|
||
sourcePlatformCode: 'INVALID_PLATFORM',
|
||
logisticsCode: 'INVALID_EXPRESS',
|
||
receiverInfo: {
|
||
name: '测试收件人',
|
||
},
|
||
},
|
||
orderLines: {
|
||
orderLine: [
|
||
{ itemCode: 'SKU-1', itemName: '商品1', planQty: 1 },
|
||
],
|
||
},
|
||
},
|
||
}, {
|
||
storeId: 1000000,
|
||
warehouseId: 100000,
|
||
appKey: 'APP-KEY',
|
||
});
|
||
|
||
assert.equal(result.success, true);
|
||
assert.equal(result.data.orderId, 7788);
|
||
assert.equal(transaction.commitCalls, 1);
|
||
assert.equal(transaction.rollbackCalls, 0);
|
||
assert.ok(sequence.indexOf('delivery.create') < sequence.indexOf('transaction.commit'));
|
||
assert.ok(sequence.indexOf('transaction.commit') < sequence.indexOf('operationLog:ORDER_CREATE'));
|
||
// 注意:asyncProcessOrderCreated 为 fire-and-forget(不 await),
|
||
// 入队(order.queue)发生在 addOrder 返回之后,不能再断言其与
|
||
// operationLog:ORDER_CREATE 的先后顺序。
|
||
// 未启用 enable_external_express_code 时,快递解析直接回填合约默认快递,
|
||
// 不产生 ORDER_EXPRESS_FALLBACK 日志,因此此处只有创建 + 平台兜底两条。
|
||
assert.equal(operationLogCalls.length, 2);
|
||
|
||
const createLog = operationLogCalls.find(item => item.operationCode === 'ORDER_CREATE');
|
||
const platformFallbackLog = operationLogCalls.find(item => item.operationCode === 'ORDER_PLATFORM_FALLBACK');
|
||
const expressFallbackLog = operationLogCalls.find(item => item.operationCode === 'ORDER_EXPRESS_FALLBACK');
|
||
|
||
assert.ok(createLog);
|
||
assert.ok(platformFallbackLog);
|
||
assert.equal(expressFallbackLog, undefined);
|
||
assert.equal(createLog.businessType, 'order');
|
||
assert.equal(createLog.businessId, 7788);
|
||
assert.equal(createLog.extras.message, '订单创建成功,orderId:7788');
|
||
assert.equal(captures.deliveryCreateData.create_channel, 'MANUAL');
|
||
assert.equal(platformFallbackLog.businessId, 7788);
|
||
assert.equal(platformFallbackLog.extras.message, '平台编码INVALID_PLATFORM匹配不到平台,已转为默认平台:淘宝(来源:policy_config)');
|
||
// 快递仍按合约默认快递兜底(只是不再写兜底日志)
|
||
assert.equal(captures.deliveryCreateData.logistics_code, 'STO');
|
||
assert.equal(captures.deliveryCreateData.logistics_name, '申通');
|
||
});
|
||
|
||
it('should persist explicit create channel from qimen caller', async () => {
|
||
const { service, captures } = createHarness();
|
||
|
||
const result = await service.addOrder({
|
||
request: {
|
||
deliveryOrder: {
|
||
deliveryOrderCode: 'ORDER-002',
|
||
sourcePlatformCode: 'TAOBAO',
|
||
logisticsCode: 'STO',
|
||
receiverInfo: {
|
||
name: '测试收件人',
|
||
},
|
||
},
|
||
orderLines: {
|
||
orderLine: [
|
||
{ itemCode: 'SKU-1', itemName: '商品1', planQty: 1 },
|
||
],
|
||
},
|
||
},
|
||
}, {
|
||
storeId: 1000000,
|
||
warehouseId: 100000,
|
||
appKey: 'APP-KEY',
|
||
}, 'QM');
|
||
|
||
assert.equal(result.success, true);
|
||
assert.equal(captures.deliveryCreateData.create_channel, 'QM');
|
||
});
|
||
|
||
it('should persist need_oms_waybill=1 from openapi root param', async () => {
|
||
const { service, captures } = createHarness();
|
||
|
||
const result = await service.addOrder({
|
||
request: {
|
||
deliveryOrder: {
|
||
deliveryOrderCode: 'ORDER-003',
|
||
sourcePlatformCode: 'TAOBAO',
|
||
logisticsCode: 'STO',
|
||
receiverInfo: {
|
||
name: '测试收件人',
|
||
},
|
||
},
|
||
orderLines: {
|
||
orderLine: [
|
||
{ itemCode: 'SKU-1', itemName: '商品1', planQty: 1 },
|
||
],
|
||
},
|
||
// openapi 在请求根节点传入(与 deliveryOrder 平级)
|
||
need_oms_waybill: 1,
|
||
},
|
||
}, {
|
||
storeId: 1000000,
|
||
warehouseId: 100000,
|
||
appKey: 'APP-KEY',
|
||
}, 'API');
|
||
|
||
assert.equal(result.success, true);
|
||
assert.equal(captures.deliveryCreateData.create_channel, 'API');
|
||
assert.equal(captures.deliveryCreateData.need_oms_waybill, 1);
|
||
});
|
||
|
||
it('should default need_oms_waybill to 0 when openapi does not pass it', async () => {
|
||
const { service, captures } = createHarness();
|
||
|
||
const result = await service.addOrder({
|
||
request: {
|
||
deliveryOrder: {
|
||
deliveryOrderCode: 'ORDER-004',
|
||
sourcePlatformCode: 'TAOBAO',
|
||
logisticsCode: 'STO',
|
||
receiverInfo: {
|
||
name: '测试收件人',
|
||
},
|
||
},
|
||
orderLines: {
|
||
orderLine: [
|
||
{ itemCode: 'SKU-1', itemName: '商品1', planQty: 1 },
|
||
],
|
||
},
|
||
},
|
||
}, {
|
||
storeId: 1000000,
|
||
warehouseId: 100000,
|
||
appKey: 'APP-KEY',
|
||
}, 'API');
|
||
|
||
assert.equal(result.success, true);
|
||
assert.equal(captures.deliveryCreateData.need_oms_waybill, 0);
|
||
});
|
||
|
||
it('should force need_oms_waybill to 0 for non-openapi channels', async () => {
|
||
for (const channel of [ 'QM', 'MANUAL', 'STOCKOUT' ]) {
|
||
const { service, captures } = createHarness();
|
||
|
||
const result = await service.addOrder({
|
||
request: {
|
||
deliveryOrder: {
|
||
deliveryOrderCode: `ORDER-${channel}`,
|
||
sourcePlatformCode: 'TAOBAO',
|
||
logisticsCode: 'STO',
|
||
receiverInfo: {
|
||
name: '测试收件人',
|
||
},
|
||
},
|
||
orderLines: {
|
||
orderLine: [
|
||
{ itemCode: 'SKU-1', itemName: '商品1', planQty: 1 },
|
||
],
|
||
},
|
||
need_oms_waybill: 1,
|
||
},
|
||
}, {
|
||
storeId: 1000000,
|
||
warehouseId: 100000,
|
||
appKey: 'APP-KEY',
|
||
}, channel);
|
||
|
||
assert.equal(result.success, true);
|
||
assert.equal(captures.deliveryCreateData.need_oms_waybill, 0, `channel ${channel} should force 0`);
|
||
}
|
||
});
|
||
|
||
it('should normalize need_oms_waybill variants', () => {
|
||
const { service } = createHarness();
|
||
|
||
for (const value of [ 1, '1', true, 'true', 'TRUE', 'Y', 'yes' ]) {
|
||
assert.equal(service.normalizeNeedOmsWaybill(value), 1, `${JSON.stringify(value)} should be 1`);
|
||
}
|
||
for (const value of [ 0, '0', false, 'false', 'N', 'no', '', null, undefined, 'abc', {}]) {
|
||
assert.equal(service.normalizeNeedOmsWaybill(value), 0, `${JSON.stringify(value)} should be 0`);
|
||
}
|
||
});
|
||
});
|