feat(intercept): 拦截链路补齐操作日志并禁止对团购已复核/散单已称重订单写入拦截请求状态
- 补齐 qimen/OpenAPI/后台取消转拦截各分支的 ORDER_INTERCEPT_REQUEST 操作日志(此前已同步订单转拦截分支漏写) - 新增 intercept_guard 共享前置校验:团购单已复核(inspection_status=fin)、散单已称重(weighing_weight>0)不允许再写入 requested/intercepting 拦截请求状态 - qimen/OpenAPI 取消拦截命中校验时跳过状态与日志写入,保留售后工单与快递群通知;pendingOrder 取消已同步订单直接拒绝并回滚;批量拦截跳过并返回 skipped_order_ids
This commit is contained in:
parent
8db3629e2d
commit
c9fc06ebe6
@ -25,7 +25,172 @@ function isInterceptedOrderStatus(status) {
|
||||
return normalizedStatus !== INTERCEPT_STATUS_NONE;
|
||||
}
|
||||
|
||||
/** 团购波次类型。 */
|
||||
const WAVE_TYPE_GROUP_BUY = 1;
|
||||
|
||||
/** 散单波次类型。 */
|
||||
const WAVE_TYPE_SCATTER = 2;
|
||||
|
||||
/**
|
||||
* 返回不允许写入拦截请求状态(requested / intercepting)的阻断原因文案;空字符串表示允许写入。
|
||||
*
|
||||
* 规则:
|
||||
* - 团购波次(wave_type=1):inspection_status === 'fin'(已复核)→ 不允许写入拦截请求状态;
|
||||
* - 散单波次(wave_type=2):weighing_weight > 0(已称重)→ 不允许写入拦截请求状态;
|
||||
* - 其余情况(无波次 / 其他波次类型 / 未复核 / 未称重)不阻断。
|
||||
*
|
||||
* @param {Object} params 参数。
|
||||
* @param {number|string} [params.waveType] 波次类型。
|
||||
* @param {string} [params.inspectionStatus] 订单复核状态。
|
||||
* @param {number|string} [params.weighingWeight] 散单称重重量。
|
||||
* @return {string} 阻断原因;空字符串表示允许写入。
|
||||
*/
|
||||
function getInterceptRequestBlockReason({ waveType, inspectionStatus, weighingWeight } = {}) {
|
||||
const normalizedWaveType = Number(waveType || 0);
|
||||
if (normalizedWaveType === WAVE_TYPE_GROUP_BUY) {
|
||||
return String(inspectionStatus || '').trim() === 'fin' ? '团购单已复核' : '';
|
||||
}
|
||||
if (normalizedWaveType === WAVE_TYPE_SCATTER) {
|
||||
return Number(weighingWeight || 0) > 0 ? '散单已称重' : '';
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断订单是否因「团购已复核 / 散单已称重」而不允许写入拦截请求状态。
|
||||
*
|
||||
* @param {Object} params 参数,同 getInterceptRequestBlockReason。
|
||||
* @return {boolean} true 表示不允许写入拦截请求状态。
|
||||
*/
|
||||
function isInterceptRequestBlockedByRecheckOrWeigh(params = {}) {
|
||||
return getInterceptRequestBlockReason(params) !== '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 拉取单订单的拦截请求前置校验信息(波次类型 + 散单称重重量)。
|
||||
*
|
||||
* @param {Object} ctx Egg 上下文。
|
||||
* @param {Object} params 参数。
|
||||
* @param {number|string} params.orderId 订单 ID。
|
||||
* @param {number|string} [params.waveId] 波次 ID。
|
||||
* @param {number|string} params.warehouseId 仓库 ID。
|
||||
* @param {Object} [params.transaction] 可选事务对象。
|
||||
* @return {Promise<{ waveType: number, weighingWeight: number }>} 校验信息。
|
||||
*/
|
||||
async function resolveInterceptRequestGuardInfo(ctx, params = {}) {
|
||||
const { orderId, waveId, warehouseId, transaction } = params;
|
||||
let waveType = 0;
|
||||
if (Number(waveId || 0) > 0 && ctx && ctx.model && ctx.model.WaveBatch) {
|
||||
try {
|
||||
const waveRow = await ctx.model.WaveBatch.findOne({
|
||||
where: { wave_id: Number(waveId), warehouse_id: warehouseId },
|
||||
attributes: [ 'wave_id', 'wave_type' ],
|
||||
raw: true,
|
||||
transaction,
|
||||
});
|
||||
waveType = Number(waveRow && waveRow.wave_type || 0);
|
||||
} catch (error) { /* 查询失败不阻断主流程,按无波次处理 */ }
|
||||
}
|
||||
|
||||
let weighingWeight = 0;
|
||||
if (ctx && ctx.model && ctx.model.OrderExpress) {
|
||||
try {
|
||||
const expressRows = await ctx.model.OrderExpress.findAll({
|
||||
where: { order_id: Number(orderId || 0) },
|
||||
attributes: [ 'weighing_weight' ],
|
||||
raw: true,
|
||||
transaction,
|
||||
});
|
||||
for (const row of (Array.isArray(expressRows) ? expressRows : [])) {
|
||||
weighingWeight = Math.max(weighingWeight, Number(row && row.weighing_weight || 0));
|
||||
}
|
||||
} catch (error) { /* 查询失败不阻断主流程,按未称重处理 */ }
|
||||
}
|
||||
|
||||
return { waveType, weighingWeight };
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量拉取订单的拦截请求前置校验信息(波次类型 + 散单称重重量)。
|
||||
*
|
||||
* @param {Object} ctx Egg 上下文。
|
||||
* @param {Object} params 参数。
|
||||
* @param {Array<Object>} params.orderRows 订单行数组(需含 id、wave_id 字段)。
|
||||
* @param {number|string} params.warehouseId 仓库 ID。
|
||||
* @param {Object} [params.transaction] 可选事务对象。
|
||||
* @return {Promise<Map<number, { waveType: number, weighingWeight: number }>>} 按订单 ID 索引的校验信息。
|
||||
*/
|
||||
async function resolveInterceptRequestGuardInfoMap(ctx, params = {}) {
|
||||
const orderRows = Array.isArray(params.orderRows) ? params.orderRows : [];
|
||||
const { warehouseId, transaction } = params;
|
||||
|
||||
const infoMap = new Map();
|
||||
if (orderRows.length === 0 || !ctx) {
|
||||
return infoMap;
|
||||
}
|
||||
|
||||
const orderIds = orderRows
|
||||
.map(row => Number(row && row.id || 0))
|
||||
.filter(orderId => Number.isInteger(orderId) && orderId > 0);
|
||||
const waveIds = [ ...new Set(orderRows
|
||||
.map(row => Number(row && row.wave_id || 0))
|
||||
.filter(waveId => Number.isInteger(waveId) && waveId > 0)) ];
|
||||
|
||||
for (const orderId of orderIds) {
|
||||
infoMap.set(orderId, { waveType: 0, weighingWeight: 0 });
|
||||
}
|
||||
|
||||
if (waveIds.length > 0 && ctx.model && ctx.model.WaveBatch) {
|
||||
try {
|
||||
const { Op } = ctx.app && ctx.app.Sequelize || {};
|
||||
const waves = await ctx.model.WaveBatch.findAll({
|
||||
where: { wave_id: { [Op.in]: waveIds }, warehouse_id: warehouseId },
|
||||
attributes: [ 'wave_id', 'wave_type' ],
|
||||
raw: true,
|
||||
transaction,
|
||||
});
|
||||
const waveTypeMap = new Map((Array.isArray(waves) ? waves : [])
|
||||
.map(wave => [ Number(wave && wave.wave_id || 0), Number(wave && wave.wave_type || 0) ]));
|
||||
for (const row of orderRows) {
|
||||
const info = infoMap.get(Number(row.id));
|
||||
if (info) {
|
||||
info.waveType = waveTypeMap.get(Number(row.wave_id || 0)) || 0;
|
||||
}
|
||||
}
|
||||
} catch (error) { /* 查询失败不阻断主流程,按无波次处理 */ }
|
||||
}
|
||||
|
||||
if (orderIds.length > 0 && ctx.model && ctx.model.OrderExpress) {
|
||||
try {
|
||||
const { Op } = ctx.app && ctx.app.Sequelize || {};
|
||||
const expressRows = await ctx.model.OrderExpress.findAll({
|
||||
where: { order_id: { [Op.in]: orderIds }, warehouse_id: warehouseId },
|
||||
attributes: [ 'order_id', 'weighing_weight' ],
|
||||
raw: true,
|
||||
transaction,
|
||||
});
|
||||
for (const expressRow of (Array.isArray(expressRows) ? expressRows : [])) {
|
||||
const info = infoMap.get(Number(expressRow && expressRow.order_id || 0));
|
||||
if (info) {
|
||||
info.weighingWeight = Math.max(
|
||||
info.weighingWeight,
|
||||
Number(expressRow && expressRow.weighing_weight || 0)
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (error) { /* 查询失败不阻断主流程,按未称重处理 */ }
|
||||
}
|
||||
|
||||
return infoMap;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
INTERCEPT_STATUS_NONE,
|
||||
isInterceptedOrderStatus,
|
||||
WAVE_TYPE_GROUP_BUY,
|
||||
WAVE_TYPE_SCATTER,
|
||||
getInterceptRequestBlockReason,
|
||||
isInterceptRequestBlockedByRecheckOrWeigh,
|
||||
resolveInterceptRequestGuardInfo,
|
||||
resolveInterceptRequestGuardInfoMap,
|
||||
};
|
||||
|
||||
@ -2,6 +2,8 @@
|
||||
|
||||
const MainService = require('../main');
|
||||
|
||||
const { getInterceptRequestBlockReason, resolveInterceptRequestGuardInfo } = require('../../lib/intercept_guard');
|
||||
|
||||
class OpenapiOrderService extends MainService {
|
||||
/**
|
||||
* 取消发货单。
|
||||
@ -54,61 +56,82 @@ class OpenapiOrderService extends MainService {
|
||||
const printStatus = String(existingOrder.print_status || 'none').trim();
|
||||
|
||||
if (pickStatus !== 'none' || printStatus !== 'none') {
|
||||
await existingOrder.update({
|
||||
intercept_status: 'intercepting',
|
||||
updated_at: new Date(),
|
||||
}, {
|
||||
// 拦截前置校验:团购单已复核 / 散单已称重后不允许写入拦截请求状态
|
||||
const guardInfo = await resolveInterceptRequestGuardInfo(ctx, {
|
||||
orderId: existingOrder.id,
|
||||
waveId: existingOrder.wave_id,
|
||||
warehouseId,
|
||||
transaction,
|
||||
});
|
||||
const blockReason = getInterceptRequestBlockReason({
|
||||
waveType: guardInfo.waveType,
|
||||
inspectionStatus: String(existingOrder.inspection_status || '').trim(),
|
||||
weighingWeight: guardInfo.weighingWeight,
|
||||
});
|
||||
|
||||
// 同步更新运单表拦截标记
|
||||
try {
|
||||
const { ctx } = this;
|
||||
const warehouseId = ctx.user && ctx.user.store_id ? Number(ctx.user.store_id) : 0;
|
||||
if (warehouseId > 0) {
|
||||
await ctx.model.OrderExpress.update(
|
||||
{ is_intercept: 'Y' },
|
||||
{ where: { order_id: existingOrder.id, warehouse_id: warehouseId }, transaction }
|
||||
if (!blockReason) {
|
||||
await existingOrder.update({
|
||||
intercept_status: 'intercepting',
|
||||
updated_at: new Date(),
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
// 同步更新运单表拦截标记
|
||||
try {
|
||||
const { ctx: ctxExpress } = this;
|
||||
const expressWarehouseId = ctxExpress.user && ctxExpress.user.store_id ? Number(ctxExpress.user.store_id) : 0;
|
||||
if (expressWarehouseId > 0) {
|
||||
await ctxExpress.model.OrderExpress.update(
|
||||
{ is_intercept: 'Y' },
|
||||
{ where: { order_id: existingOrder.id, warehouse_id: expressWarehouseId }, transaction }
|
||||
);
|
||||
}
|
||||
} catch (expressErr) {
|
||||
this.ctx.logger.warn(
|
||||
`[openapi order cancel] 更新运单拦截标记失败, orderId=${existingOrder.id}, error=${expressErr.message}`
|
||||
);
|
||||
}
|
||||
} catch (expressErr) {
|
||||
this.ctx.logger.warn(
|
||||
`[openapi order cancel] 更新运单拦截标记失败, orderId=${existingOrder.id}, error=${expressErr.message}`
|
||||
} else {
|
||||
this.ctx.logger.info(
|
||||
`[openapi order cancel] 订单${blockReason},跳过写入拦截请求状态, orderId=${existingOrder.id}`
|
||||
);
|
||||
}
|
||||
|
||||
await transaction.commit();
|
||||
|
||||
// 写入拦截操作日志(失败不影响主流程)
|
||||
try {
|
||||
const { ctx: ctx2 } = this;
|
||||
if (ctx2.model && ctx2.model.OperationLog) {
|
||||
const whId2 = ctx2.user && ctx2.user.store_id ? Number(ctx2.user.store_id) : 0;
|
||||
await ctx2.model.OperationLog.create({
|
||||
business_type: 'order',
|
||||
business_id: String(existingOrder.id),
|
||||
operation_code: 'ORDER_INTERCEPT_REQUEST',
|
||||
operator_id: ctx2.user?.employees_id || ctx2.user?.id || null,
|
||||
operator_name: ctx2.user?.employees_name || 'OpenAPI',
|
||||
external_log_type: 'NONE',
|
||||
warehouse_id: whId2 || null,
|
||||
platform_code: 'OPENAPI',
|
||||
message: 'OpenAPI取消订单-触发拦截',
|
||||
});
|
||||
// 写入拦截操作日志(失败不影响主流程;未写入拦截请求状态时不记录)
|
||||
if (!blockReason) {
|
||||
try {
|
||||
const { ctx: ctx2 } = this;
|
||||
if (ctx2.model && ctx2.model.OperationLog) {
|
||||
const whId2 = ctx2.user && ctx2.user.store_id ? Number(ctx2.user.store_id) : 0;
|
||||
await ctx2.model.OperationLog.create({
|
||||
business_type: 'order',
|
||||
business_id: String(existingOrder.id),
|
||||
operation_code: 'ORDER_INTERCEPT_REQUEST',
|
||||
operator_id: ctx2.user?.employees_id || ctx2.user?.id || null,
|
||||
operator_name: ctx2.user?.employees_name || 'OpenAPI',
|
||||
external_log_type: 'NONE',
|
||||
warehouse_id: whId2 || null,
|
||||
platform_code: 'OPENAPI',
|
||||
message: 'OpenAPI取消订单-触发拦截',
|
||||
});
|
||||
}
|
||||
} catch (logErr) {
|
||||
this.ctx.logger.warn('[OpenAPIOrderCancel] 写入拦截操作日志失败,不影响主流程', logErr);
|
||||
}
|
||||
} catch (logErr) {
|
||||
this.ctx.logger.warn('[OpenAPIOrderCancel] 写入拦截操作日志失败,不影响主流程', logErr);
|
||||
}
|
||||
|
||||
// 创建售后工单 + 通知快递群(失败不影响主流程)
|
||||
try {
|
||||
const { ctx } = this;
|
||||
const whId = ctx.user && ctx.user.store_id ? Number(ctx.user.store_id) : 0;
|
||||
const { ctx: ctxWorkOrder } = this;
|
||||
const whId = ctxWorkOrder.user && ctxWorkOrder.user.store_id ? Number(ctxWorkOrder.user.store_id) : 0;
|
||||
if (whId > 0) {
|
||||
await ctx.service.systems.afterSale.createInterceptWorkOrdersAndNotify({
|
||||
await ctxWorkOrder.service.systems.afterSale.createInterceptWorkOrdersAndNotify({
|
||||
orderIds: [ existingOrder.id ],
|
||||
warehouseId: whId,
|
||||
creatorLabel: ctx.user?.employees_name || 'OpenAPI系统',
|
||||
creatorLabel: ctxWorkOrder.user?.employees_name || 'OpenAPI系统',
|
||||
});
|
||||
}
|
||||
} catch (workOrderErr) {
|
||||
@ -117,7 +140,9 @@ class OpenapiOrderService extends MainService {
|
||||
);
|
||||
}
|
||||
|
||||
return this.fail('取消失败,已通知拦截执行');
|
||||
return this.fail(blockReason
|
||||
? `订单${blockReason},无法拦截执行,已转快递拦截`
|
||||
: '取消失败,已通知拦截执行');
|
||||
}
|
||||
|
||||
// 检查是否在波次中,若在波次中需清除波次关联
|
||||
|
||||
@ -2,6 +2,8 @@
|
||||
|
||||
const MainService = require('../main');
|
||||
|
||||
const { getInterceptRequestBlockReason, resolveInterceptRequestGuardInfo } = require('../../lib/intercept_guard');
|
||||
|
||||
class orderServer extends MainService {
|
||||
|
||||
constructor(ctx) {
|
||||
@ -212,45 +214,66 @@ class orderServer extends MainService {
|
||||
const printStatus = String(existingOrder.print_status || 'none').trim();
|
||||
|
||||
if (pickStatus !== 'none' || printStatus !== 'none') {
|
||||
await existingOrder.update({
|
||||
intercept_status: 'intercepting',
|
||||
updated_at: new Date(),
|
||||
}, {
|
||||
// 拦截前置校验:团购单已复核 / 散单已称重后不允许写入拦截请求状态
|
||||
const guardInfo = await resolveInterceptRequestGuardInfo(ctx, {
|
||||
orderId: existingOrder.id,
|
||||
waveId: existingOrder.wave_id,
|
||||
warehouseId: info.warehouseId,
|
||||
transaction,
|
||||
});
|
||||
const blockReason = getInterceptRequestBlockReason({
|
||||
waveType: guardInfo.waveType,
|
||||
inspectionStatus: String(existingOrder.inspection_status || '').trim(),
|
||||
weighingWeight: guardInfo.weighingWeight,
|
||||
});
|
||||
|
||||
// 同步更新运单表拦截标记
|
||||
try {
|
||||
await ctx.model.OrderExpress.update(
|
||||
{ is_intercept: 'Y' },
|
||||
{ where: { order_id: existingOrder.id, warehouse_id: info.warehouseId }, transaction }
|
||||
);
|
||||
} catch (expressErr) {
|
||||
ctx.logger.warn(
|
||||
`[qimen order cancel] 更新运单拦截标记失败, orderId=${existingOrder.id}, error=${expressErr.message}`
|
||||
if (!blockReason) {
|
||||
await existingOrder.update({
|
||||
intercept_status: 'intercepting',
|
||||
updated_at: new Date(),
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
// 同步更新运单表拦截标记
|
||||
try {
|
||||
await ctx.model.OrderExpress.update(
|
||||
{ is_intercept: 'Y' },
|
||||
{ where: { order_id: existingOrder.id, warehouse_id: info.warehouseId }, transaction }
|
||||
);
|
||||
} catch (expressErr) {
|
||||
ctx.logger.warn(
|
||||
`[qimen order cancel] 更新运单拦截标记失败, orderId=${existingOrder.id}, error=${expressErr.message}`
|
||||
);
|
||||
}
|
||||
} else {
|
||||
ctx.logger.info(
|
||||
`[qimen order cancel] 订单${blockReason},跳过写入拦截请求状态, orderId=${existingOrder.id}`
|
||||
);
|
||||
}
|
||||
|
||||
await transaction.commit();
|
||||
|
||||
// 写入拦截操作日志(失败不影响主流程)
|
||||
try {
|
||||
if (ctx.model && ctx.model.OperationLog) {
|
||||
const customerId2 = String(info.customerId || '').trim();
|
||||
await ctx.model.OperationLog.create({
|
||||
business_type: 'order',
|
||||
business_id: String(existingOrder.id),
|
||||
operation_code: 'ORDER_INTERCEPT_REQUEST',
|
||||
operator_id: null,
|
||||
operator_name: customerId2 ? `qimen:${customerId2}` : 'qimen',
|
||||
external_log_type: 'NONE',
|
||||
warehouse_id: info.warehouseId || null,
|
||||
platform_code: customerId2 || 'SYSTEM',
|
||||
message: '奇门取消订单-触发拦截',
|
||||
});
|
||||
// 写入拦截操作日志(失败不影响主流程;未写入拦截请求状态时不记录)
|
||||
if (!blockReason) {
|
||||
try {
|
||||
if (ctx.model && ctx.model.OperationLog) {
|
||||
const customerId2 = String(info.customerId || '').trim();
|
||||
await ctx.model.OperationLog.create({
|
||||
business_type: 'order',
|
||||
business_id: String(existingOrder.id),
|
||||
operation_code: 'ORDER_INTERCEPT_REQUEST',
|
||||
operator_id: null,
|
||||
operator_name: customerId2 ? `qimen:${customerId2}` : 'qimen',
|
||||
external_log_type: 'NONE',
|
||||
warehouse_id: info.warehouseId || null,
|
||||
platform_code: customerId2 || 'SYSTEM',
|
||||
message: '奇门取消订单-触发拦截',
|
||||
});
|
||||
}
|
||||
} catch (logErr) {
|
||||
ctx.logger.warn('[QimenOrderCancel] 写入拦截操作日志失败,不影响主流程', logErr);
|
||||
}
|
||||
} catch (logErr) {
|
||||
ctx.logger.warn('[QimenOrderCancel] 写入拦截操作日志失败,不影响主流程', logErr);
|
||||
}
|
||||
|
||||
// 创建售后工单 + 通知快递群(仅限有运单的订单,与正常货主发起拦截一致;失败不影响主流程)
|
||||
@ -269,38 +292,83 @@ class orderServer extends MainService {
|
||||
);
|
||||
}
|
||||
|
||||
await this.sendCancelInterceptNotice({
|
||||
orderCode,
|
||||
existingOrder,
|
||||
info,
|
||||
});
|
||||
return this.fail('取消失败,已通知拦截执行');
|
||||
if (!blockReason) {
|
||||
await this.sendCancelInterceptNotice({
|
||||
orderCode,
|
||||
existingOrder,
|
||||
info,
|
||||
});
|
||||
}
|
||||
return this.fail(blockReason
|
||||
? `订单${blockReason},无法拦截执行,已转快递拦截`
|
||||
: '取消失败,已通知拦截执行');
|
||||
}
|
||||
|
||||
// 已同步订单不允许取消,转为拦截处理
|
||||
const syncStatus = String(existingOrder.sync_status || '').trim();
|
||||
if (syncStatus === 'fin') {
|
||||
await existingOrder.update({
|
||||
intercept_status: 'requested',
|
||||
updated_at: new Date(),
|
||||
}, {
|
||||
// 拦截前置校验:团购单已复核 / 散单已称重后不允许写入拦截请求状态
|
||||
const syncGuardInfo = await resolveInterceptRequestGuardInfo(ctx, {
|
||||
orderId: existingOrder.id,
|
||||
waveId: existingOrder.wave_id,
|
||||
warehouseId: info.warehouseId,
|
||||
transaction,
|
||||
});
|
||||
const syncBlockReason = getInterceptRequestBlockReason({
|
||||
waveType: syncGuardInfo.waveType,
|
||||
inspectionStatus: String(existingOrder.inspection_status || '').trim(),
|
||||
weighingWeight: syncGuardInfo.weighingWeight,
|
||||
});
|
||||
|
||||
// 同步更新运单表拦截标记
|
||||
try {
|
||||
await ctx.model.OrderExpress.update(
|
||||
{ is_intercept: 'Y' },
|
||||
{ where: { order_id: existingOrder.id, warehouse_id: info.warehouseId }, transaction }
|
||||
);
|
||||
} catch (expressErr) {
|
||||
ctx.logger.warn(
|
||||
`[qimen order cancel] 更新运单拦截标记失败(sync), orderId=${existingOrder.id}, error=${expressErr.message}`
|
||||
if (!syncBlockReason) {
|
||||
await existingOrder.update({
|
||||
intercept_status: 'requested',
|
||||
updated_at: new Date(),
|
||||
}, {
|
||||
transaction,
|
||||
});
|
||||
|
||||
// 同步更新运单表拦截标记
|
||||
try {
|
||||
await ctx.model.OrderExpress.update(
|
||||
{ is_intercept: 'Y' },
|
||||
{ where: { order_id: existingOrder.id, warehouse_id: info.warehouseId }, transaction }
|
||||
);
|
||||
} catch (expressErr) {
|
||||
ctx.logger.warn(
|
||||
`[qimen order cancel] 更新运单拦截标记失败(sync), orderId=${existingOrder.id}, error=${expressErr.message}`
|
||||
);
|
||||
}
|
||||
} else {
|
||||
ctx.logger.info(
|
||||
`[qimen order cancel] 订单${syncBlockReason},跳过写入拦截请求状态(sync), orderId=${existingOrder.id}`
|
||||
);
|
||||
}
|
||||
|
||||
await transaction.commit();
|
||||
|
||||
// 写入拦截操作日志(失败不影响主流程;未写入拦截请求状态时不记录)
|
||||
if (!syncBlockReason) {
|
||||
try {
|
||||
if (ctx.model && ctx.model.OperationLog) {
|
||||
const customerId2 = String(info.customerId || '').trim();
|
||||
await ctx.model.OperationLog.create({
|
||||
business_type: 'order',
|
||||
business_id: String(existingOrder.id),
|
||||
operation_code: 'ORDER_INTERCEPT_REQUEST',
|
||||
operator_id: null,
|
||||
operator_name: customerId2 ? `qimen:${customerId2}` : 'qimen',
|
||||
external_log_type: 'NONE',
|
||||
warehouse_id: info.warehouseId || null,
|
||||
platform_code: customerId2 || 'SYSTEM',
|
||||
message: '奇门取消订单-触发拦截(已同步订单转拦截)',
|
||||
});
|
||||
}
|
||||
} catch (logErr) {
|
||||
ctx.logger.warn('[QimenOrderCancel] 写入拦截操作日志失败(sync),不影响主流程', logErr);
|
||||
}
|
||||
}
|
||||
|
||||
// 创建售后工单 + 通知快递群(仅限有运单的订单,与正常货主发起拦截一致;失败不影响主流程)
|
||||
try {
|
||||
const interceptExpress = await this.getInterceptExpressInfo(existingOrder.id);
|
||||
@ -317,12 +385,16 @@ class orderServer extends MainService {
|
||||
);
|
||||
}
|
||||
|
||||
await this.sendCancelInterceptNotice({
|
||||
orderCode,
|
||||
existingOrder,
|
||||
info,
|
||||
});
|
||||
return this.fail('订单已同步,不可取消,已转为拦截处理');
|
||||
if (!syncBlockReason) {
|
||||
await this.sendCancelInterceptNotice({
|
||||
orderCode,
|
||||
existingOrder,
|
||||
info,
|
||||
});
|
||||
}
|
||||
return this.fail(syncBlockReason
|
||||
? `订单${syncBlockReason},无法取消,已转快递拦截`
|
||||
: '订单已同步,不可取消,已转为拦截处理');
|
||||
}
|
||||
|
||||
// 检查是否在波次中,若在波次中需清除波次关联
|
||||
|
||||
@ -2,6 +2,8 @@
|
||||
|
||||
const OrderBaseService = require('./base');
|
||||
|
||||
const { getInterceptRequestBlockReason, resolveInterceptRequestGuardInfoMap } = require('../../../lib/intercept_guard');
|
||||
|
||||
const PENDING_OUTBOUND_STATUS = 'padding';
|
||||
const PENDING_CANCEL_STATUS = 'cancel';
|
||||
const PENDING_CANCEL_STEP = 9;
|
||||
@ -2453,11 +2455,35 @@ class PendingOrderService extends OrderBaseService {
|
||||
throw new Error('订单当前状态不允许取消');
|
||||
}
|
||||
|
||||
// 拉取拦截请求前置校验信息(波次类型 + 散单称重重量):团购单已复核 / 散单已称重后不允许写入拦截请求状态
|
||||
const guardInfoMap = await resolveInterceptRequestGuardInfoMap(this.ctx, {
|
||||
orderRows: existingOrders,
|
||||
warehouseId,
|
||||
transaction,
|
||||
});
|
||||
|
||||
// 已同步订单不允许取消,转为拦截处理
|
||||
const syncedOrders = existingOrders.filter(
|
||||
order => String(order.sync_status || '').trim() === 'fin'
|
||||
);
|
||||
if (syncedOrders.length > 0) {
|
||||
// 已复核/已称重订单不允许写入拦截请求状态,直接拒绝取消
|
||||
const blockedSyncedOrders = syncedOrders.filter(order => {
|
||||
const guardInfo = guardInfoMap.get(Number(order.id)) || {};
|
||||
return getInterceptRequestBlockReason({
|
||||
waveType: guardInfo.waveType,
|
||||
inspectionStatus: String(order.inspection_status || '').trim(),
|
||||
weighingWeight: guardInfo.weighingWeight,
|
||||
}) !== '';
|
||||
});
|
||||
if (blockedSyncedOrders.length > 0) {
|
||||
await transaction.rollback();
|
||||
const blockedCodes = blockedSyncedOrders
|
||||
.map(order => String(order.delivery_order_code || order.pre_delivery_order_code || order.id || '').trim())
|
||||
.filter(Boolean)
|
||||
.join(', ');
|
||||
throw new Error(`订单已完成复核/称重,无法取消:${blockedCodes}`);
|
||||
}
|
||||
const syncedOrderIds = syncedOrders.map(order => Number(order.id)).filter(
|
||||
id => Number.isInteger(id) && id > 0
|
||||
);
|
||||
@ -2495,6 +2521,33 @@ class PendingOrderService extends OrderBaseService {
|
||||
// 拦截标记已写入,提交后抛出业务错误(调用方按"已转拦截"处理)
|
||||
await transaction.commit();
|
||||
|
||||
// 写入拦截操作日志(不影响主流程)
|
||||
try {
|
||||
const operator = this.getOrderLogOperator() || {};
|
||||
const logWarehouseId = this.getOrderLogWarehouseId(warehouseId);
|
||||
if (syncedOrderIds.length > 0 && this.ctx.model && this.ctx.model.OperationLog) {
|
||||
const operatorId = operator.id != null ? String(operator.id) : null;
|
||||
const operatorName = operator.name ? String(operator.name) : 'system';
|
||||
const logNow = new Date();
|
||||
await this.ctx.model.OperationLog.bulkCreate(
|
||||
syncedOrderIds.map(orderId => ({
|
||||
business_type: 'order',
|
||||
business_id: String(orderId),
|
||||
operation_code: 'ORDER_INTERCEPT_REQUEST',
|
||||
operator_id: operatorId,
|
||||
operator_name: operatorName,
|
||||
external_log_type: 'NONE',
|
||||
warehouse_id: logWarehouseId || null,
|
||||
platform_code: 'SYSTEM',
|
||||
message: '订单已同步,不可取消,转为拦截处理',
|
||||
created_at: logNow,
|
||||
}))
|
||||
);
|
||||
}
|
||||
} catch (logErr) {
|
||||
this.ctx.logger.warn('[PendingOrderService.cancelOrder] 写入拦截操作日志失败,不影响业务', logErr);
|
||||
}
|
||||
|
||||
const syncedCodes = syncedOrders
|
||||
.map(order => String(order.delivery_order_code || order.pre_delivery_order_code || order.id || '').trim())
|
||||
.filter(Boolean)
|
||||
@ -4320,6 +4373,7 @@ ${numbered}`;
|
||||
payload.total_order_count = Number(result.total_order_count || 0);
|
||||
payload.intercepted_order_count = Number(result.intercepted_order_count || 0);
|
||||
payload.intercepted_order_ids = this.normalizeIdsInput(result.intercepted_order_ids);
|
||||
payload.skipped_order_ids = this.normalizeIdsInput(result.skipped_order_ids);
|
||||
payload.duration_ms = Number(result.duration_ms || cachedProgress.duration_ms || 0);
|
||||
}
|
||||
|
||||
@ -4542,7 +4596,7 @@ ${numbered}`;
|
||||
}
|
||||
|
||||
const orderRows = await orderModel.findAll({
|
||||
attributes: [ 'id', 'delivery_order_code', 'pre_delivery_order_code' ],
|
||||
attributes: [ 'id', 'delivery_order_code', 'pre_delivery_order_code', 'wave_id', 'inspection_status' ],
|
||||
where: {
|
||||
warehouse_id: warehouseId,
|
||||
id: {
|
||||
@ -4561,7 +4615,33 @@ ${numbered}`;
|
||||
const operator = this.getOrderLogOperator();
|
||||
const logWarehouseId = this.getOrderLogWarehouseId(warehouseId);
|
||||
|
||||
// 拉取拦截请求前置校验信息(波次类型 + 散单称重重量):团购单已复核 / 散单已称重的订单不允许写入拦截请求状态,跳过拦截
|
||||
const guardInfoMap = await resolveInterceptRequestGuardInfoMap(ctx, {
|
||||
orderRows: validOrderRows,
|
||||
warehouseId,
|
||||
});
|
||||
|
||||
const skippedInterceptOrderIds = [];
|
||||
const eligibleOrderRows = [];
|
||||
for (const row of (Array.isArray(validOrderRows) ? validOrderRows : [])) {
|
||||
const guardInfo = guardInfoMap.get(Number(row.id)) || {};
|
||||
const blockReason = getInterceptRequestBlockReason({
|
||||
waveType: guardInfo.waveType,
|
||||
inspectionStatus: String(row.inspection_status || '').trim(),
|
||||
weighingWeight: guardInfo.weighingWeight,
|
||||
});
|
||||
if (blockReason) {
|
||||
skippedInterceptOrderIds.push(Number(row.id));
|
||||
ctx.logger.info(
|
||||
`[order.pendingOrder.batchIntercept] 订单${blockReason},跳过拦截, orderId=${row.id}`
|
||||
);
|
||||
} else {
|
||||
eligibleOrderRows.push(row);
|
||||
}
|
||||
}
|
||||
|
||||
const normalizedOrderRows = Array.isArray(validOrderRows) ? validOrderRows : [];
|
||||
const eligibleOrderCount = eligibleOrderRows.length;
|
||||
const totalOrderCount = orderIds.length;
|
||||
const validOrderCount = normalizedOrderRows.length;
|
||||
|
||||
@ -4592,10 +4672,10 @@ ${numbered}`;
|
||||
return;
|
||||
}
|
||||
|
||||
// 分批更新拦截状态,每批最多500条
|
||||
// 分批更新拦截状态,每批最多500条(仅处理未命中「已复核/已称重」前置校验的订单)
|
||||
const UPDATE_CHUNK_SIZE = 500;
|
||||
for (let i = 0; i < normalizedOrderRows.length; i += UPDATE_CHUNK_SIZE) {
|
||||
const chunkRows = normalizedOrderRows.slice(i, i + UPDATE_CHUNK_SIZE);
|
||||
for (let i = 0; i < eligibleOrderRows.length; i += UPDATE_CHUNK_SIZE) {
|
||||
const chunkRows = eligibleOrderRows.slice(i, i + UPDATE_CHUNK_SIZE);
|
||||
if (chunkRows.length === 0) {
|
||||
continue;
|
||||
}
|
||||
@ -4686,8 +4766,8 @@ ${numbered}`;
|
||||
}
|
||||
|
||||
// 更新进度
|
||||
const progressPercent = validOrderCount > 0
|
||||
? Math.min(99, Math.round((interceptedOrderIds.length / validOrderCount) * 100))
|
||||
const progressPercent = eligibleOrderCount > 0
|
||||
? Math.min(99, Math.round((interceptedOrderIds.length / eligibleOrderCount) * 100))
|
||||
: 0;
|
||||
|
||||
await this.setBatchInterceptTaskProgress(normalizedTaskId, 'running', {
|
||||
@ -4710,9 +4790,13 @@ ${numbered}`;
|
||||
order_ids: orderIds,
|
||||
intercepted_order_ids: interceptedOrderIds,
|
||||
intercepted_order_count: interceptedOrderIds.length,
|
||||
skipped_order_ids: skippedInterceptOrderIds,
|
||||
skipped_order_count: skippedInterceptOrderIds.length,
|
||||
total_order_count: totalOrderCount,
|
||||
duration_ms: durationMs,
|
||||
message: `已拦截 ${interceptedOrderIds.length} 条订单`,
|
||||
message: skippedInterceptOrderIds.length > 0
|
||||
? `已拦截 ${interceptedOrderIds.length} 条订单,跳过 ${skippedInterceptOrderIds.length} 条(已复核/已称重)`
|
||||
: `已拦截 ${interceptedOrderIds.length} 条订单`,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user