liufumingren fdec622f2d feat(auto-outbound): 自动出库改为「开关 + 规则数组」,支持多条规则与跨天放行
- subject_config 新增 auto_outbound_enabled(默认 false);auto_outbound_rules 由对象改为数组,
  每项仅含 require_pack / require_weigh / require_sync / exclude_same_day_outbound /
  same_day_outbound_deadline_time / same_day_outbound_release_time
- 固定基线:已复核(recheck_status=fin)+ 已分配储位(storage_status=fin);已打印、已拣货不再校验
- 规则内 AND、规则间 OR(任一命中即出库);空数组不出库且不告警
- 旧对象结构不再兼容:读路径按默认(关闭)处理,写路径返回 400 提示重新配置
- 放行时间支持跨天 00:00~47:59(27:00 = 次日 03:00);按订单复核时间所在北京日计算未放行窗口
- 定时任务与手动触发统一走新规则;自动出库写入操作日志 ORDER_AUTO_OUTBOUND
- 同步 methods/system.json、methods/changes/system.json 与 AI_METHOD_ROUTING.md

涉及接口:setting.warehouse.getSettings / getFieldMeta / saveSettings、outbound.autoOutbound
2026-10-16 05:27:30 +08:00

95 lines
3.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 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<Object>} 执行结果,包含处理数量和仓库详情。
*/
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;