'use strict'; const OrderBaseService = require('../systemMethods/order/base'); const OUTBOUND_STATUS = 'fin'; const OUTBOUND_STEP = 4; /** * 订单出库状态服务。 * 仅用于内部把订单标记为出库,不对外暴露通用 method。 */ class OrderOutboundService extends OrderBaseService { /** * 将订单标记为出库。 * * 兼容单条与批量入参,支持 id、order_id、ids 三种订单 ID 写法。 * 仅更新出库状态相关字段:outbound_status、step、delivery_time、updated_at。 * * @param {Object} data 请求体。 * @return {Promise} 更新后的订单信息,包含出库状态与更新数量。 */ async markOutboundOrder(data = {}) { const requestData = this.isPlainObject(data) ? data : {}; const warehouseId = this.getWarehouseId(); const orderIds = this.normalizeOrderIds(requestData); if (orderIds.length === 0) { throw new Error('订单ID不能为空'); } const model = await this.getOrderModel('order', warehouseId); const now = new Date(); const where = this.buildOrderWhere(orderIds, warehouseId); const updateValues = { outbound_status: OUTBOUND_STATUS, step: OUTBOUND_STEP, delivery_time: now, 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, step: Number(order.step ?? OUTBOUND_STEP), delivery_time: order.delivery_time, 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, step: firstOrder.step, delivery_time: firstOrder.delivery_time, updated_at: firstOrder.updated_at, updated_count: affectedRows, ids: normalizedOrders.map(order => order.id), }; } } module.exports = OrderOutboundService;