87 lines
2.5 KiB
JavaScript
87 lines
2.5 KiB
JavaScript
'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_rules 控制:
|
||
* - recheck_status = 'fin'(已复核)
|
||
* - pack_status = 'fin'(已打包)
|
||
* - wms_order_express.weighing_weight > 0(已称重)
|
||
*
|
||
* @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 autoOutboundRules = subjectConfig && subjectConfig.auto_outbound_rules;
|
||
|
||
if (!autoOutboundRules || !autoOutboundRules.enabled) {
|
||
throw new Error('当前仓库未启用自动化出库');
|
||
}
|
||
|
||
const result = await ctx.service.systems.orderAutoOutbound.processWarehouseAutoOutbound(
|
||
warehouseId,
|
||
autoOutboundRules
|
||
);
|
||
|
||
return result;
|
||
}
|
||
}
|
||
|
||
module.exports = OutboundService;
|