'use strict'; const OrderBaseService = require('./base'); class ExpectionOrderService extends OrderBaseService { constructor(ctx) { super(ctx); const _noNeedLogin = ''; const _noNeedRight = 'list,expectionOrder,resolveException'; const _noNeedLog = 'list'; const _noNeedAuth = 'list,expectionOrder,resolveException'; this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth); } /** * 待处理异常订单列表。 * @param {Object} data 请求体。 * @return {Promise} 列表与分页信息。 */ async list(data = {}) { return await this.listByModule('expectionOrder', data); } /** * 将待处理异常订单转回正常待处理状态(step 3 -> 2)。 * * 只操作当前仓库的当前订单表,且仅允许 step=3 的订单转出。 * * @param {Object} data 请求体。 * @param {number[]} data.ids 订单ID 数组。 * @return {Promise} 统一 success 包装的更新结果。 */ async resolveException(data = {}) { const requestData = this.isPlainObject(data) ? data : {}; const warehouseId = this.getWarehouseId(); const operator = this.getOrderLogOperator(); const logWarehouseId = this.getOrderLogWarehouseId(warehouseId); if (!Array.isArray(requestData.ids) || requestData.ids.length === 0) { throw new Error('ids 不能为空'); } const orderIds = Array.from(new Set(requestData.ids.map(v => { const n = Number(v); if (!Number.isInteger(n) || n <= 0) { throw new Error('ids 仅支持正整数数组'); } return n; }))); const model = await this.getOrderModel('order', warehouseId); const where = this.buildOrderWhere(orderIds, warehouseId); where.step = 3; const [ affectedRows ] = await model.update({ step: 2, updated_at: new Date(), }, { where }); const count = Number(affectedRows || 0); // 记录操作日志 try { await this.ctx.service.systems.operationLog.createOperationLog({ businessType: 'order', businessId: logWarehouseId, operationCode: 'ORDER_RESOLVE_EXCEPTION', operator, extras: { warehouseId: logWarehouseId, orderIds, updated_count: count, message: `异常订单转正常:订单ID [${orderIds.join(', ')}],step 3 -> 2,共更新 ${count} 条`, }, beforeChanges: null, }); } catch (logErr) { this.ctx.logger.warn('[ExpectionOrderService.resolveException] 写入操作日志失败,不影响业务', logErr); } return this.success('ok', { updated_count: count, ids: orderIds, }); } } module.exports = ExpectionOrderService;