71 lines
2.8 KiB
JavaScript
71 lines
2.8 KiB
JavaScript
'use strict';
|
||
|
||
const MainService = require('../../main');
|
||
|
||
/**
|
||
* 系统方法层:storage.management(库存管理接口包装层)。
|
||
*/
|
||
class StorageManagementMethodService extends MainService {
|
||
constructor(ctx) {
|
||
super(ctx);
|
||
const _noNeedLogin = '';
|
||
const _noNeedRight = 'replenishmentDemand';
|
||
const _noNeedLog = '';
|
||
const _noNeedAuth = 'replenishmentDemand';
|
||
this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth);
|
||
}
|
||
|
||
/**
|
||
* 分拣缺货补货需求计算。
|
||
*
|
||
* 仓库级别的计算,针对当前仓库所有未完成储位分配的订单,
|
||
* 计算每个 SKU 在分拣区与存货区的可用库存对比,
|
||
* 输出分拣区缺口及建议补货量,供仓库人员决策移库。
|
||
*
|
||
* 仓库 ID 从用户上下文(ctx.user.store_id / ctx.user.warehouse_id)自动获取,无需显式传入。
|
||
*
|
||
* @param {Object} [data={}] 请求参数。
|
||
* @param {Object} [data.pagination] 分页参数。
|
||
* @param {Object} [data.sort] 排序参数。
|
||
* @param {number|number[]} [data.store_id] 货主筛选:单值或数组(正整数),也可通过 data.where.store_id 传入;不传时返回全部货主。
|
||
* @return {Promise<Object>} 统一 success 包装的补货需求明细、汇总与分页信息。
|
||
*/
|
||
async replenishmentDemand(data = {}) {
|
||
const { service } = this;
|
||
const requestData = this.isPlainObject(data) ? data : {};
|
||
const pagination = requestData.pagination || {};
|
||
const sort = requestData.sort || requestData.order || {};
|
||
const where = this.isPlainObject(requestData.where) ? requestData.where : {};
|
||
|
||
// 货主筛选:支持顶层 store_id / store_ids,或 where.store_id(单值/数组)
|
||
const rawStoreIdValue = requestData.store_id !== undefined && requestData.store_id !== null
|
||
? requestData.store_id
|
||
: (requestData.store_ids !== undefined && requestData.store_ids !== null
|
||
? requestData.store_ids
|
||
: where.store_id);
|
||
|
||
let storeIds = [];
|
||
if (rawStoreIdValue !== undefined && rawStoreIdValue !== null && rawStoreIdValue !== '') {
|
||
const rawList = Array.isArray(rawStoreIdValue) ? rawStoreIdValue : [ rawStoreIdValue ];
|
||
const normalizedStoreIds = new Set();
|
||
for (const item of rawList) {
|
||
const value = Number(item);
|
||
if (!Number.isFinite(value) || value <= 0 || !Number.isInteger(value)) {
|
||
throw new Error(`store_id 必须是正整数,非法值: ${item}`);
|
||
}
|
||
normalizedStoreIds.add(value);
|
||
}
|
||
storeIds = Array.from(normalizedStoreIds);
|
||
}
|
||
|
||
const result = await service.systems.storage.management.replenishmentDemand({
|
||
pagination,
|
||
sort,
|
||
storeIds,
|
||
});
|
||
return this.success('补货需求计算完成', result);
|
||
}
|
||
}
|
||
|
||
module.exports = StorageManagementMethodService;
|