2026-04-17 19:21:57 +08:00

296 lines
9.0 KiB
JavaScript
Raw 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 OrderBaseService = require('./base');
const PENDING_OUTBOUND_STATUS = 'padding';
const PENDING_CANCEL_STATUS = 'cancel';
const PENDING_CANCEL_STEP = 9;
const PENDING_CANCEL_ALLOWED_STEPS = new Set([ 0, 1, 2, 3 ]);
const PENDING_CANCEL_PENDING_STEPS = new Set([ 1, 2 ]);
class PendingOrderService extends OrderBaseService {
constructor(ctx) {
super(ctx);
const _noNeedLogin = '';
const _noNeedRight = 'list,pendingOrder,reverseReviewOrder,markPendingOutboundOrder,cancelOrder';
const _noNeedLog = 'list,markPendingOutboundOrder,cancelOrder';
const _noNeedAuth = 'list,pendingOrder,reverseReviewOrder,markPendingOutboundOrder,cancelOrder';
this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth);
}
/**
* 待处理订单列表。
* @param {Object} data 请求体。
* @return {Promise<Object>} 列表与分页信息。
*/
async list(data = {}) {
return await this.listByModule('pendingOrder', data);
}
/**
* 将待处理订单反审回待审核状态。
* @param {Object} data 请求体。
* @return {Promise<Object>} 更新后的订单信息。
*/
async reverseReviewOrder(data = {}) {
return await this.updateOrderInspectionState(data, 'none');
}
/**
* 将待处理订单批量标记为待出库。
* @param {Object} data 请求体。
* @param {number[]} data.ids 订单ID 数组。
* @return {Promise<Object>} 更新后的订单信息。
*/
async markPendingOutboundOrder(data = {}) {
const requestData = this.isPlainObject(data) ? data : {};
const warehouseId = this.getWarehouseId();
const orderIds = this.normalizePendingOutboundOrderIds(requestData);
if (orderIds.length === 0) {
throw new Error('订单ID不能为空');
}
const model = await this.getOrderModel('order', warehouseId);
const where = this.buildOrderWhere(orderIds, warehouseId);
const existingOrders = await model.findAll({
where,
raw: true,
});
if (!Array.isArray(existingOrders) || existingOrders.length === 0) {
return this.success('取消成功', {
updated_count: 0,
ids: [],
});
}
if (existingOrders.length !== orderIds.length) {
throw new Error('订单不存在或不属于当前仓库');
}
const now = new Date();
const updateValues = {
outbound_status: PENDING_OUTBOUND_STATUS,
updated_at: now,
};
const updateResult = await model.update(updateValues, {
where,
});
const affectedRows = Array.isArray(updateResult) ? Number(updateResult[0] || 0) : Number(updateResult || 0);
if (affectedRows <= 0) {
throw new Error('订单不存在或不属于当前仓库');
}
const updatedOrders = await model.findAll({
where,
raw: true,
});
if (!Array.isArray(updatedOrders) || updatedOrders.length === 0) {
throw new Error('订单不存在或不属于当前仓库');
}
const orderMap = new Map(updatedOrders.map(order => [ Number(order.id), order ]));
const normalizedOrders = orderIds
.map(orderId => orderMap.get(orderId))
.filter(Boolean)
.map(order => ({
id: order.id,
outbound_status: order.outbound_status,
updated_at: order.updated_at,
}));
if (normalizedOrders.length === 0) {
throw new Error('订单不存在或不属于当前仓库');
}
const firstOrder = normalizedOrders[0];
return {
id: firstOrder.id,
outbound_status: firstOrder.outbound_status,
updated_at: firstOrder.updated_at,
updated_count: affectedRows,
ids: normalizedOrders.map(order => order.id),
};
}
/**
* 将待处理订单取消,并关闭到已取消状态。
* 仅允许取消当前仓库内处于待审核、待处理、待处理异常语义的订单。
* @param {Object} data 请求体。
* @param {number[]} data.ids 订单ID 数组。
* @return {Promise<Object>} 更新后的订单信息。
*/
async cancelOrder(data = {}) {
const requestData = this.isPlainObject(data) ? data : {};
const warehouseId = this.getWarehouseId();
const orderIds = this.normalizePendingCancelOrderIds(requestData);
if (orderIds.length === 0) {
throw new Error('订单ID不能为空');
}
const model = await this.getOrderModel('order', warehouseId);
const where = this.buildOrderWhere(orderIds, warehouseId);
const existingOrders = await model.findAll({
where,
raw: true,
});
if (!Array.isArray(existingOrders) || existingOrders.length === 0) {
return this.success('取消成功', {
updated_count: 0,
ids: [],
});
}
if (existingOrders.length !== orderIds.length) {
throw new Error('订单不存在或不属于当前仓库');
}
const nonCancelableOrder = existingOrders.find(order => !this.isCancelablePendingOrder(order));
if (nonCancelableOrder) {
throw new Error('订单当前状态不允许取消');
}
const now = new Date();
const updateValues = {
status: PENDING_CANCEL_STATUS,
step: PENDING_CANCEL_STEP,
updated_at: now,
};
const updateResult = await model.update(updateValues, {
where,
});
const affectedRows = Array.isArray(updateResult) ? Number(updateResult[0] || 0) : Number(updateResult || 0);
if (affectedRows <= 0) {
throw new Error('订单不存在或不属于当前仓库');
}
const updatedOrders = await model.findAll({
where,
raw: true,
});
if (!Array.isArray(updatedOrders) || updatedOrders.length === 0) {
throw new Error('订单不存在或不属于当前仓库');
}
const orderMap = new Map(updatedOrders.map(order => [ Number(order.id), order ]));
const normalizedOrders = orderIds
.map(orderId => orderMap.get(orderId))
.filter(Boolean)
.map(order => ({
id: order.id,
status: order.status,
step: Number(order.step ?? PENDING_CANCEL_STEP),
updated_at: order.updated_at,
}));
if (normalizedOrders.length === 0) {
throw new Error('订单不存在或不属于当前仓库');
}
const firstOrder = normalizedOrders[0];
return {
id: firstOrder.id,
status: firstOrder.status,
step: firstOrder.step,
updated_at: firstOrder.updated_at,
updated_count: affectedRows,
ids: normalizedOrders.map(order => order.id),
};
}
/**
* 规范待处理出库订单ID入参,仅接受 ids 数组且元素必须为正整数。
* @param {Object} data 请求体。
* @return {number[]} 订单ID 数组。
*/
normalizePendingOutboundOrderIds(data = {}) {
const requestData = this.isPlainObject(data) ? data : {};
const unexpectedKeys = Object.keys(requestData).filter(key => key !== 'ids');
if (unexpectedKeys.length > 0) {
throw new Error('待处理出库接口仅支持 ids 数组入参,不再支持 id、order_id、逗号字符串或其他兼容字段');
}
if (!Array.isArray(requestData.ids)) {
throw new Error('待处理出库接口仅支持 ids 数组入参');
}
const normalizedIds = requestData.ids.map(value => {
if (!Number.isInteger(value) || value <= 0) {
throw new Error('ids 仅支持正整数数组');
}
return value;
});
return Array.from(new Set(normalizedIds));
}
/**
* 规范待处理取消订单ID入参,仅接受 ids 数组且元素必须为正整数。
* @param {Object} data 请求体。
* @return {number[]} 订单ID 数组。
*/
normalizePendingCancelOrderIds(data = {}) {
const requestData = this.isPlainObject(data) ? data : {};
const unexpectedKeys = Object.keys(requestData).filter(key => key !== 'ids');
if (unexpectedKeys.length > 0) {
throw new Error('待处理取消接口仅支持 ids 数组入参,不再支持 id、order_id、逗号字符串或其他兼容字段');
}
if (!Array.isArray(requestData.ids)) {
throw new Error('待处理取消接口仅支持 ids 数组入参');
}
const normalizedIds = requestData.ids.map(value => {
if (!Number.isInteger(value) || value <= 0) {
throw new Error('ids 仅支持正整数数组');
}
return value;
});
return Array.from(new Set(normalizedIds));
}
/**
* 判断订单是否允许从待处理模块取消。
* 仅允许处于待审核、待处理、待处理异常语义的订单取消。
* @param {Object} order 订单记录。
* @return {boolean} 是否允许取消。
*/
isCancelablePendingOrder(order = {}) {
const step = Number(order.step);
const status = String(order.status || '').trim().toLowerCase();
const outboundStatus = String(order.outbound_status || '').trim();
if (status === PENDING_CANCEL_STATUS) {
return false;
}
if (!PENDING_CANCEL_ALLOWED_STEPS.has(step)) {
return false;
}
if (PENDING_CANCEL_PENDING_STEPS.has(step) && outboundStatus === PENDING_OUTBOUND_STATUS) {
return false;
}
return true;
}
}
module.exports = PendingOrderService;