feat(order): 新增 order.cancelB2b 接口,取消 B2B 出库流转回正常订单
- systems/order_b2b.js cancelOrdersB2b:
1) 仅限待处理表(current 分表)中 is_b2b=Y 的订单,事务内 SELECT ... FOR UPDATE
后逐行校验:待处理(step!=0)/是 B2B(is_b2b=Y)/未打印(print_status!=fin)/
未锁定(lock_status=none)/未拦截(intercept_status=none)/B2B 面单未打印
(wms_order_b2b.is_print!=Y),不符合条件跳过(skipped_reasons 返回原因),
不存在订单计入 not_found_order_ids
2) 事务内:订单 is_b2b 置回 N + B2B 作业明细软删除(deleted_at);提交后写
操作日志 ORDER_B2B_CANCEL(失败不影响主流程)
3) 单次上限 200,仓库隔离
- systemMethods/order/index.js cancelB2b:ids/order_ids/orderIds 数组与
id/order_id/orderId 单值兼容,空/超限 400;白名单三处加 cancelB2b
- 验证:临时集成测试 3/3 PASS(转 B2B→取消链路:is_b2b 置回 N+明细软删除
+日志、已打印 B2B 面单跳过分流、400 边界,测后数据还原);ESLint 0 error
This commit is contained in:
parent
183e542d74
commit
b10f8f7583
@ -13,9 +13,9 @@ class OrderIndexService extends OrderBaseService {
|
||||
constructor(ctx) {
|
||||
super(ctx);
|
||||
const _noNeedLogin = '';
|
||||
const _noNeedRight = 'list,badgeCount,manualOutboundOrder,queryOrderLogs,queryOrderLogList,detail,updateExpressWeighingWeight,export,exportTaskProgress,bindExpressPackageMaterialByWaybill,convertToB2b';
|
||||
const _noNeedLog = 'list,badgeCount,manualOutboundOrder,queryOrderLogs,queryOrderLogList,detail,updateExpressWeighingWeight,export,exportTaskProgress,bindExpressPackageMaterialByWaybill,convertToB2b';
|
||||
const _noNeedAuth = 'list,badgeCount,manualOutboundOrder,queryOrderLogs,queryOrderLogList,detail,updateExpressWeighingWeight,export,exportTaskProgress,bindExpressPackageMaterialByWaybill,convertToB2b';
|
||||
const _noNeedRight = 'list,badgeCount,manualOutboundOrder,queryOrderLogs,queryOrderLogList,detail,updateExpressWeighingWeight,export,exportTaskProgress,bindExpressPackageMaterialByWaybill,convertToB2b,cancelB2b';
|
||||
const _noNeedLog = 'list,badgeCount,manualOutboundOrder,queryOrderLogs,queryOrderLogList,detail,updateExpressWeighingWeight,export,exportTaskProgress,bindExpressPackageMaterialByWaybill,convertToB2b,cancelB2b';
|
||||
const _noNeedAuth = 'list,badgeCount,manualOutboundOrder,queryOrderLogs,queryOrderLogList,detail,updateExpressWeighingWeight,export,exportTaskProgress,bindExpressPackageMaterialByWaybill,convertToB2b,cancelB2b';
|
||||
this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth);
|
||||
}
|
||||
|
||||
@ -66,6 +66,54 @@ class OrderIndexService extends OrderBaseService {
|
||||
return this.success('ok', result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消 B2B 出库:订单流转回正常订单。
|
||||
*
|
||||
* 仅限待处理表中 is_b2b=Y 且未打印、未锁定、未拦截、B2B 面单未打印的订单;
|
||||
* 不符合条件跳过。事务内:订单表 is_b2b 置回 N + B2B 作业明细软删除,
|
||||
* 提交后写操作日志。
|
||||
*
|
||||
* @param {Object} data 请求体。
|
||||
* @param {Array<number|string>|number|string} [data.ids] 订单ID集合(优先)。
|
||||
* @param {Array<number|string>|number|string} [data.order_ids] 订单ID集合别名。
|
||||
* @param {Array<number|string>|number|string} [data.orderIds] 订单ID集合别名。
|
||||
* @param {number|string} [data.id] 订单ID单值别名。
|
||||
* @param {number|string} [data.order_id] 订单ID单值别名。
|
||||
* @param {number|string} [data.orderId] 订单ID单值别名。
|
||||
* @return {Promise<Object>} 统一成功包装响应(data 含 canceled/skipped/not_found 分流结果)。
|
||||
*/
|
||||
async cancelB2b(data = {}) {
|
||||
const requestData = this.isPlainObject(data) ? data : {};
|
||||
const rawIds = requestData.ids !== undefined ? requestData.ids
|
||||
: requestData.order_ids !== undefined ? requestData.order_ids
|
||||
: requestData.orderIds !== undefined ? requestData.orderIds
|
||||
: requestData.id !== undefined ? requestData.id
|
||||
: requestData.order_id !== undefined ? requestData.order_id
|
||||
: requestData.orderId;
|
||||
const orderIds = [ ...new Set(
|
||||
(Array.isArray(rawIds) ? rawIds : [ rawIds ])
|
||||
.map(item => Number(item))
|
||||
.filter(item => Number.isInteger(item) && item > 0)
|
||||
) ];
|
||||
if (orderIds.length === 0) {
|
||||
return this.fail('订单ID不能为空', {}, 400);
|
||||
}
|
||||
if (orderIds.length > 200) {
|
||||
return this.fail('单次最多取消 200 个订单', {}, 400);
|
||||
}
|
||||
|
||||
const warehouseId = this.getWarehouseId();
|
||||
const operator = this.getOrderLogOperator();
|
||||
|
||||
const result = await this.service.systems.orderB2b.cancelOrdersB2b({
|
||||
warehouseId,
|
||||
orderIds,
|
||||
operator,
|
||||
});
|
||||
|
||||
return this.success('ok', result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 提交订单导出任务。
|
||||
*
|
||||
|
||||
@ -4,6 +4,8 @@ const Service = require('egg').Service;
|
||||
|
||||
/** 订单转 B2B 操作码 */
|
||||
const ORDER_B2B_CONVERT_OPERATION_CODE = 'ORDER_B2B_CONVERT';
|
||||
/** 取消 B2B(流转回正常订单)操作码 */
|
||||
const ORDER_B2B_CANCEL_OPERATION_CODE = 'ORDER_B2B_CANCEL';
|
||||
/** 单次转换订单数上限 */
|
||||
const ORDER_B2B_CONVERT_BATCH_LIMIT = 200;
|
||||
|
||||
@ -199,6 +201,208 @@ class OrderB2bService extends Service {
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消 B2B 出库:将 B2B 订单流转回正常订单。
|
||||
*
|
||||
* 仅限待处理表(current 分表)中 is_b2b=Y 且未打印、未锁定、未拦截、
|
||||
* B2B 面单未打印(wms_order_b2b.is_print=N)的订单;不符合条件的订单跳过,
|
||||
* 不报错。事务内完成 is_b2b 置回 N 与 B2B 作业明细软删除;
|
||||
* 操作日志在事务提交后写入。
|
||||
*
|
||||
* @param {Object} params 取消参数。
|
||||
* @param {number|string} params.warehouseId 仓库ID。
|
||||
* @param {Array<number>} params.orderIds 订单ID数组。
|
||||
* @param {Object|null} [params.operator=null] 操作人 { id, name }。
|
||||
* @return {Promise<Object>} { canceled_order_ids, canceled_count, skipped_order_ids,
|
||||
* skipped_count, skipped_reasons, not_found_order_ids }
|
||||
*/
|
||||
async cancelOrdersB2b(params = {}) {
|
||||
const { ctx, app } = this;
|
||||
const normalizedWarehouseId = Number(params && params.warehouseId || 0);
|
||||
if (!Number.isInteger(normalizedWarehouseId) || normalizedWarehouseId <= 0) {
|
||||
throw new Error('未识别到当前仓库上下文');
|
||||
}
|
||||
|
||||
const rawIds = params && params.orderIds;
|
||||
const normalizedOrderIds = [ ...new Set(
|
||||
(Array.isArray(rawIds) ? rawIds : [ rawIds ])
|
||||
.map(item => Number(item))
|
||||
.filter(item => Number.isInteger(item) && item > 0)
|
||||
) ];
|
||||
if (normalizedOrderIds.length === 0) {
|
||||
throw new Error('订单ID不能为空');
|
||||
}
|
||||
if (normalizedOrderIds.length > ORDER_B2B_CONVERT_BATCH_LIMIT) {
|
||||
throw new Error(`单次最多取消 ${ORDER_B2B_CONVERT_BATCH_LIMIT} 个订单`);
|
||||
}
|
||||
|
||||
const operator = params && params.operator && typeof params.operator === 'object'
|
||||
? params.operator
|
||||
: null;
|
||||
const { Op } = app.Sequelize;
|
||||
const orderModel = await ctx.model.OrderDeliveryFactory.getCurrentModel(normalizedWarehouseId);
|
||||
|
||||
const transaction = await ctx.model.transaction();
|
||||
const canceledRows = [];
|
||||
const skippedReasons = [];
|
||||
let notFoundOrderIds = [];
|
||||
|
||||
try {
|
||||
const lockRows = await orderModel.findAll({
|
||||
attributes: [
|
||||
'id', 'delivery_order_code', 'store_id', 'step',
|
||||
'print_status', 'lock_status', 'intercept_status', 'is_b2b',
|
||||
],
|
||||
where: {
|
||||
id: { [Op.in]: normalizedOrderIds },
|
||||
warehouse_id: normalizedWarehouseId,
|
||||
},
|
||||
raw: true,
|
||||
transaction,
|
||||
lock: transaction.LOCK.UPDATE,
|
||||
});
|
||||
|
||||
const rowMap = new Map((Array.isArray(lockRows) ? lockRows : []).map(row => [ Number(row && row.id || 0), row ]));
|
||||
notFoundOrderIds = normalizedOrderIds.filter(orderId => !rowMap.has(orderId));
|
||||
|
||||
// B2B 作业明细打印状态(is_print):已打印 B2B 面单的订单不允许取消
|
||||
const b2bRows = await ctx.model.OrderB2b.findAll({
|
||||
where: {
|
||||
order_id: { [Op.in]: normalizedOrderIds },
|
||||
deleted_at: null,
|
||||
},
|
||||
attributes: [ 'order_id', 'is_print' ],
|
||||
raw: true,
|
||||
transaction,
|
||||
});
|
||||
const b2bPrintMap = new Map((Array.isArray(b2bRows) ? b2bRows : [])
|
||||
.map(row => [ Number(row && row.order_id || 0), String(row && row.is_print || 'N').trim() ]));
|
||||
|
||||
for (const orderId of normalizedOrderIds) {
|
||||
const row = rowMap.get(orderId);
|
||||
if (!row) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const skipReason = this.resolveCancelSkipReason(row, b2bPrintMap.get(orderId));
|
||||
if (skipReason) {
|
||||
skippedReasons.push({
|
||||
order_id: orderId,
|
||||
delivery_order_code: String(row.delivery_order_code || '').trim(),
|
||||
reason: skipReason,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
canceledRows.push(row);
|
||||
}
|
||||
|
||||
if (canceledRows.length > 0) {
|
||||
const canceledOrderIds = canceledRows.map(row => Number(row.id));
|
||||
const now = new Date();
|
||||
|
||||
await orderModel.update(
|
||||
{ is_b2b: 'N', updated_at: now },
|
||||
{
|
||||
where: {
|
||||
id: { [Op.in]: canceledOrderIds },
|
||||
warehouse_id: normalizedWarehouseId,
|
||||
},
|
||||
transaction,
|
||||
}
|
||||
);
|
||||
|
||||
// B2B 作业明细软删除(保留历史)
|
||||
await ctx.model.OrderB2b.update(
|
||||
{ deleted_at: now, updated_at: now },
|
||||
{
|
||||
where: {
|
||||
order_id: { [Op.in]: canceledOrderIds },
|
||||
deleted_at: null,
|
||||
},
|
||||
transaction,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
await transaction.commit();
|
||||
} catch (error) {
|
||||
await transaction.rollback();
|
||||
throw error;
|
||||
}
|
||||
|
||||
// 操作日志在事务提交后写入,失败不影响主流程
|
||||
for (const row of canceledRows) {
|
||||
try {
|
||||
await ctx.service.systems.operationLog.createOperationLog({
|
||||
businessType: 'order',
|
||||
businessId: Number(row.id),
|
||||
operationCode: ORDER_B2B_CANCEL_OPERATION_CODE,
|
||||
operationAction: 'UPDATE',
|
||||
changeSummary: '取消B2B出库,订单流转回正常订单',
|
||||
operator,
|
||||
extras: {
|
||||
warehouseId: normalizedWarehouseId,
|
||||
orderId: Number(row.id),
|
||||
orderNo: String(row.delivery_order_code || '').trim(),
|
||||
},
|
||||
beforeChanges: null,
|
||||
});
|
||||
} catch (error) {
|
||||
ctx.logger.warn(`[orderB2b.cancelOrdersB2b] 写入操作日志失败,不影响业务, orderId=${row.id}, error=${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
const skippedOrderIds = [ ...new Set(skippedReasons.map(item => item.order_id)) ];
|
||||
return {
|
||||
canceled_order_ids: [ ...new Set(canceledRows.map(row => Number(row.id))) ],
|
||||
canceled_count: canceledRows.length,
|
||||
skipped_order_ids: skippedOrderIds,
|
||||
skipped_count: skippedOrderIds.length,
|
||||
skipped_reasons: skippedReasons,
|
||||
not_found_order_ids: notFoundOrderIds,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断 B2B 订单是否满足取消条件,返回跳过原因;可取消时返回空串。
|
||||
*
|
||||
* 条件:待处理(step != 0)、是 B2B(is_b2b = Y)、未打印(print_status != fin)、
|
||||
* 未锁定(lock_status = none)、未拦截(intercept_status = none)、
|
||||
* B2B 面单未打印(is_print != Y)。
|
||||
*
|
||||
* @param {Object} row 订单行。
|
||||
* @param {string} b2bIsPrint B2B 作业明细打印状态(无明细行时为空串,视为未打印)。
|
||||
* @return {string} 跳过原因,可取消返回空字符串。
|
||||
*/
|
||||
resolveCancelSkipReason(row = {}, b2bIsPrint = '') {
|
||||
if (Number(row && row.step || 0) === 0) {
|
||||
return '订单待审核,不允许取消B2B';
|
||||
}
|
||||
|
||||
if (String(row && row.is_b2b || '').trim() !== 'Y') {
|
||||
return '订单不是B2B订单';
|
||||
}
|
||||
|
||||
if (String(row && row.print_status || '').trim() === 'fin') {
|
||||
return '订单已打印,不允许取消B2B';
|
||||
}
|
||||
|
||||
if (String(row && row.lock_status || 'none').trim() !== 'none') {
|
||||
return '订单已锁定,不允许取消B2B';
|
||||
}
|
||||
|
||||
if (String(row && row.intercept_status || 'none').trim() !== 'none') {
|
||||
return '订单已拦截,不允许取消B2B';
|
||||
}
|
||||
|
||||
if (String(b2bIsPrint || '').trim() === 'Y') {
|
||||
return 'B2B面单已打印,不允许取消B2B';
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = OrderB2bService;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user