'use strict'; const MainService = require('../main'); /** * 出库相关 method 层入口。 * 负责鉴权配置与参数解构,业务逻辑下沉到 systems。 */ class OutboundService extends MainService { /** * 初始化 outbound 模块鉴权配置。 * * @param {Egg.Context} ctx 上下文对象。 */ constructor(ctx) { super(ctx); const _noNeedLogin = ''; const _noNeedRight = 'autoOutbound'; const _noNeedLog = 'autoOutbound'; const _noNeedAuth = 'autoOutbound'; this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth); } /** * 手动触发自动化出库扫描。 * * 扫描当前仓库下满足自动化出库规则的订单并执行出库操作(任一规则命中即出库)。 * 开关与规则由仓库 subject_config 控制: * - auto_outbound_enabled:是否启用(默认关闭); * - auto_outbound_rules:规则数组(空数组时不执行),每项仅校验: * require_pack(pack_status='fin')/ require_weigh(wms_order_express.weighing_weight>0) * / require_sync(sync_status='fin')。 * 固定基线条件:已复核(recheck_status='fin')+ 已分配储位(storage_status='fin')。 * * @param {Object} data 请求参数。 * @param {number} [data.warehouseId] 指定仓库ID,不传则使用当前用户上下文仓库。 * @return {Promise} 执行结果,包含处理数量和仓库详情。 */ async autoOutbound(data = {}) { const requestData = this.isPlainObject(data) ? data : {}; const { ctx } = this; const warehouseId = Number(requestData.warehouseId) || this.getWarehouseId(); if (!Number.isInteger(warehouseId) || warehouseId <= 0) { throw new Error('缺少有效的仓库ID'); } const storeId = this.getWarehouseId(); if (!Number.isInteger(storeId) || storeId <= 0) { throw new Error('缺少有效的货主ID'); } const store = await ctx.model.Store.findOne({ where: { id: storeId, status: 1, deleted_at: null }, attributes: [ 'id', 'subject_config' ], raw: true, }); if (!store) { throw new Error('货主不存在或已禁用'); } let subjectConfig = store.subject_config; if (typeof subjectConfig === 'string') { try { subjectConfig = JSON.parse(subjectConfig); } catch (error) { throw new Error('subject_config 配置解析失败'); } } const orderAutoOutboundService = ctx.service.systems.orderAutoOutbound; if (!orderAutoOutboundService.isAutoOutboundEnabled(subjectConfig)) { throw new Error('当前仓库未启用自动化出库'); } const autoOutboundRules = orderAutoOutboundService.resolveAutoOutboundRules(subjectConfig); if (autoOutboundRules.length === 0) { throw new Error('当前仓库未配置自动化出库规则'); } const result = await orderAutoOutboundService.processWarehouseAutoOutbound( warehouseId, autoOutboundRules ); return result; } } module.exports = OutboundService;