fix: 将 storage.report 模块迁移至 stock.report,修正语义错误
库存报表属于 stock(商品库存)域而非 storage(库位存储)域。 变更: - systemMethods/storage/report.js → systemMethods/stock/report.js - 类名 StorageReportMethodService → StockReportMethodService - 缓存键 storage_report_export_ → stock_report_export_ - 日志前缀 [storage.report] → [stock.report] - methods/system.json: module + 5 method 名同步更新 - methods/changes/system.json: 追加迁移记录 - systems/storage/entry.js: 注释引用修正 旧文件直接删除,不保留兼容(旧 method 名为语义错误)。
This commit is contained in:
parent
391ab87112
commit
3e813c4975
508
app/service/systemMethods/stock/report.js
Normal file
508
app/service/systemMethods/stock/report.js
Normal file
@ -0,0 +1,508 @@
|
||||
'use strict';
|
||||
|
||||
const MainService = require('../../main');
|
||||
|
||||
const STOCK_REPORT_EXPORT_TASK_CACHE_KEY_PREFIX = 'stock_report_export_task_';
|
||||
const STOCK_REPORT_EXPORT_TASK_CACHE_TTL_SECONDS = 1800;
|
||||
|
||||
/**
|
||||
* 系统方法层:stock.report(库存流水报表接口包装层)。
|
||||
*/
|
||||
class StockReportMethodService extends MainService {
|
||||
constructor(ctx) {
|
||||
super(ctx);
|
||||
const _noNeedLogin = '';
|
||||
const _noNeedRight = 'search,searchStore,export,exportStore,exportTaskProgress';
|
||||
const _noNeedLog = 'search,searchStore,export,exportStore,exportTaskProgress';
|
||||
const _noNeedAuth = 'search,searchStore,export,exportStore,exportTaskProgress';
|
||||
this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询库存流水统计报表。
|
||||
* @param {Object} data 请求体
|
||||
* @param {Object} [data.where] 查询条件
|
||||
* @param {Object} [data.sort] 排序参数
|
||||
* @param {string} [data.sort.sortField] 排序字段(兼容:sort_field)
|
||||
* @param {string} [data.sort.sortOrder] 排序顺序(兼容:sort_order)
|
||||
* @param {string|Date} [data.where.start_time] 开始时间
|
||||
* @param {string|Date} [data.where.end_time] 结束时间
|
||||
* @param {boolean|string|number} [data.where.by_space=false] 是否按库位返回
|
||||
* @param {Array<number|string>} [data.where.store_ids] 货主ID列表
|
||||
* @param {number|number[]} [data.where.stock_id] 商品库存ID(单数)
|
||||
* @param {number[]} [data.where.stock_ids] 商品库存ID列表(复数)
|
||||
* @param {string} [data.where.keyword] 商品关键字(按 item_code/item_name/goods_code/barcode 模糊匹配)
|
||||
* @return {Promise<Object>} 统一 success 包装
|
||||
*/
|
||||
async search(data = {}) {
|
||||
const { service } = this;
|
||||
const { whereValue = {} } = this.prepareWhereContext(data);
|
||||
const {
|
||||
start_time: startTime,
|
||||
end_time: endTime,
|
||||
by_space: bySpace,
|
||||
store_ids: storeIds,
|
||||
stock_id: stockIdRaw,
|
||||
stock_ids: stockIdsRaw,
|
||||
keyword,
|
||||
sort_field: whereSortField,
|
||||
sortField: whereSortFieldCamel,
|
||||
sort_order: whereSortOrder,
|
||||
sortOrder: whereSortOrderCamel,
|
||||
} = whereValue;
|
||||
|
||||
// 兼容 stock_id(单数)和 stock_ids(数组)
|
||||
const stockIds = stockIdsRaw !== undefined
|
||||
? stockIdsRaw
|
||||
: (stockIdRaw !== undefined ? [ stockIdRaw ] : null);
|
||||
|
||||
const { sort = {} } = data;
|
||||
const {
|
||||
sort_field: sortFieldSnake,
|
||||
sortField: sortFieldCamel,
|
||||
sort_order: sortOrderSnake,
|
||||
sortOrder: sortOrderCamel,
|
||||
} = sort || {};
|
||||
|
||||
const result = await service.systems.storage.report.search(startTime, endTime, bySpace, storeIds, {
|
||||
sortField: sortFieldCamel || sortFieldSnake || whereSortFieldCamel || whereSortField,
|
||||
sortOrder: sortOrderCamel || sortOrderSnake || whereSortOrderCamel || whereSortOrder,
|
||||
}, 'warehouse', stockIds, keyword);
|
||||
return this.success('ok', result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 货主侧库存流水统计报表。
|
||||
* 以货主维度查询库存流水,只关注商品入库和订单出库,不涉及库位维度。
|
||||
* @param {Object} data 请求体
|
||||
* @param {Object} [data.where] 查询条件
|
||||
* @param {Object} [data.sort] 排序参数
|
||||
* @param {string} [data.sort.sortField] 排序字段(兼容:sort_field)
|
||||
* @param {string} [data.sort.sortOrder] 排序顺序(兼容:sort_order)
|
||||
* @param {string|Date} [data.where.start_time] 开始时间
|
||||
* @param {string|Date} [data.where.end_time] 结束时间
|
||||
* @param {Array<number|string>} [data.where.warehouse_ids] 仓库ID列表(可选过滤)
|
||||
* @param {number|number[]} [data.where.stock_id] 商品库存ID(单数)
|
||||
* @param {number[]} [data.where.stock_ids] 商品库存ID列表(复数)
|
||||
* @param {string} [data.where.keyword] 商品关键字(按 item_code/item_name/goods_code/barcode 模糊匹配)
|
||||
* @return {Promise<Object>} 统一 success 包装
|
||||
*/
|
||||
async searchStore(data = {}) {
|
||||
const { service } = this;
|
||||
const { whereValue = {} } = this.prepareWhereContext(data);
|
||||
const {
|
||||
start_time: startTime,
|
||||
end_time: endTime,
|
||||
warehouse_ids: warehouseIdsRaw,
|
||||
warehouse_id: warehouseIdRaw,
|
||||
stock_id: stockIdRaw,
|
||||
stock_ids: stockIdsRaw,
|
||||
keyword,
|
||||
sort_field: whereSortField,
|
||||
sortField: whereSortFieldCamel,
|
||||
sort_order: whereSortOrder,
|
||||
sortOrder: whereSortOrderCamel,
|
||||
} = whereValue;
|
||||
|
||||
// 兼容 warehouse_id(单数)和 warehouse_ids(数组)
|
||||
const warehouseIds = warehouseIdsRaw !== undefined
|
||||
? warehouseIdsRaw
|
||||
: (warehouseIdRaw !== undefined ? [ warehouseIdRaw ] : undefined);
|
||||
|
||||
// 兼容 stock_id(单数)和 stock_ids(数组)
|
||||
const stockIds = stockIdsRaw !== undefined
|
||||
? stockIdsRaw
|
||||
: (stockIdRaw !== undefined ? [ stockIdRaw ] : null);
|
||||
|
||||
const { sort = {} } = data;
|
||||
const {
|
||||
sort_field: sortFieldSnake,
|
||||
sortField: sortFieldCamel,
|
||||
sort_order: sortOrderSnake,
|
||||
sortOrder: sortOrderCamel,
|
||||
} = sort || {};
|
||||
|
||||
// 货主侧不涉及库位维度(by_space 固定为 false)
|
||||
const result = await service.systems.storage.report.search(
|
||||
startTime,
|
||||
endTime,
|
||||
false,
|
||||
warehouseIds, // store 模式下 storeIds 参数实际承载 warehouse_id 过滤
|
||||
{
|
||||
sortField: sortFieldCamel || sortFieldSnake || whereSortFieldCamel || whereSortField,
|
||||
sortOrder: sortOrderCamel || sortOrderSnake || whereSortOrderCamel || whereSortOrder,
|
||||
},
|
||||
'store',
|
||||
stockIds,
|
||||
keyword
|
||||
);
|
||||
return this.success('ok', result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 货主侧提交库存报表导出任务。
|
||||
* 与 export 逻辑一致但使用货主维度,只关注商品入库和订单出库,不涉及库位维度。
|
||||
* 异步执行,立即返回任务ID。
|
||||
* @param {Object} data 请求体
|
||||
* @param {Object} [data.where] 查询条件
|
||||
* @param {Object} [data.sort] 排序参数
|
||||
* @param {string} [data.sort.sortField] 排序字段(兼容:sort_field)
|
||||
* @param {string} [data.sort.sortOrder] 排序顺序(兼容:sort_order)
|
||||
* @param {string|Date} [data.where.start_time] 开始时间
|
||||
* @param {string|Date} [data.where.end_time] 结束时间
|
||||
* @param {Array<number|string>} [data.where.warehouse_ids] 仓库ID列表
|
||||
* @param {number|number[]} [data.where.stock_id] 商品库存ID(单数)
|
||||
* @param {number[]} [data.where.stock_ids] 商品库存ID列表(复数)
|
||||
* @param {string} [data.where.keyword] 商品关键字(模糊匹配)
|
||||
* @param {string} [data.export_mode] 导出模式:'item' 导出含锁定类型的全部变更记录
|
||||
* @param {string} [data.exportMode] 导出模式(驼峰写法,同 export_mode)
|
||||
* @return {Promise<Object>} 统一 success 包装(返回任务ID)
|
||||
*/
|
||||
async exportStore(data = {}) {
|
||||
const { service } = this;
|
||||
const { whereValue = {} } = this.prepareWhereContext(data);
|
||||
const {
|
||||
start_time: startTime,
|
||||
end_time: endTime,
|
||||
warehouse_ids: warehouseIdsRaw,
|
||||
warehouse_id: warehouseIdRaw,
|
||||
stock_id: stockIdRaw,
|
||||
stock_ids: stockIdsRaw,
|
||||
keyword,
|
||||
sort_field: whereSortField,
|
||||
sortField: whereSortFieldCamel,
|
||||
sort_order: whereSortOrder,
|
||||
sortOrder: whereSortOrderCamel,
|
||||
} = whereValue;
|
||||
|
||||
// 兼容 warehouse_id(单数)和 warehouse_ids(数组)
|
||||
const warehouseIds = warehouseIdsRaw !== undefined
|
||||
? warehouseIdsRaw
|
||||
: (warehouseIdRaw !== undefined ? [ warehouseIdRaw ] : undefined);
|
||||
|
||||
// 兼容 stock_id(单数)和 stock_ids(数组)
|
||||
const stockIds = stockIdsRaw !== undefined
|
||||
? stockIdsRaw
|
||||
: (stockIdRaw !== undefined ? [ stockIdRaw ] : null);
|
||||
|
||||
const { sort = {} } = data;
|
||||
const {
|
||||
sort_field: sortFieldSnake,
|
||||
sortField: sortFieldCamel,
|
||||
sort_order: sortOrderSnake,
|
||||
sortOrder: sortOrderCamel,
|
||||
} = sort || {};
|
||||
|
||||
const exportMode = String(data.export_mode || data.exportMode || '').trim();
|
||||
|
||||
const taskId = this.generateExportTaskId();
|
||||
const taskPayload = {
|
||||
task_id: taskId,
|
||||
status: 'submitted',
|
||||
progress: 0,
|
||||
message: '导出任务已提交',
|
||||
result: null,
|
||||
error: '',
|
||||
};
|
||||
|
||||
await this.setExportTaskProgress(taskId, taskPayload);
|
||||
|
||||
const sortOption = {
|
||||
sortField: sortFieldCamel || sortFieldSnake || whereSortFieldCamel || whereSortField,
|
||||
sortOrder: sortOrderCamel || sortOrderSnake || whereSortOrderCamel || whereSortOrder,
|
||||
};
|
||||
|
||||
setImmediate(async () => {
|
||||
try {
|
||||
await this.setExportTaskProgress(taskId, {
|
||||
task_id: taskId,
|
||||
status: 'running',
|
||||
progress: 10,
|
||||
message: '导出任务执行中',
|
||||
result: null,
|
||||
error: '',
|
||||
});
|
||||
|
||||
const result = await service.systems.storage.report.export(
|
||||
startTime, endTime, false, warehouseIds,
|
||||
sortOption, exportMode, 'store', stockIds, keyword
|
||||
);
|
||||
|
||||
await this.setExportTaskProgress(taskId, {
|
||||
task_id: taskId,
|
||||
status: 'success',
|
||||
progress: 100,
|
||||
message: '导出任务完成',
|
||||
result,
|
||||
error: '',
|
||||
});
|
||||
} catch (error) {
|
||||
const errorMessage = error && error.message ? error.message : `${error}`;
|
||||
await this.setExportTaskProgress(taskId, {
|
||||
task_id: taskId,
|
||||
status: 'failed',
|
||||
progress: 100,
|
||||
message: '导出任务失败',
|
||||
result: null,
|
||||
error: errorMessage,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return this.success('ok', {
|
||||
...taskPayload,
|
||||
update_time: Date.now(),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 提交库存流水统计报表导出任务(仓库侧)。
|
||||
* @param {Object} data 请求体
|
||||
* @param {Object} [data.where] 查询条件
|
||||
* @param {Object} [data.sort] 排序参数
|
||||
* @param {string} [data.sort.sortField] 排序字段(兼容:sort_field)
|
||||
* @param {string} [data.sort.sortOrder] 排序顺序(兼容:sort_order)
|
||||
* @param {string|Date} [data.where.start_time] 开始时间
|
||||
* @param {string|Date} [data.where.end_time] 结束时间
|
||||
* @param {boolean|string|number} [data.where.by_space=false] 是否按库位返回
|
||||
* @param {Array<number|string>} [data.where.store_ids] 货主ID列表
|
||||
* @param {number|number[]} [data.where.stock_id] 商品库存ID(单数)
|
||||
* @param {number[]} [data.where.stock_ids] 商品库存ID列表(复数)
|
||||
* @param {string} [data.where.keyword] 商品关键字(模糊匹配)
|
||||
* @param {string} [data.export_mode] 导出模式:'item' 导出含锁定类型的全部变更记录,不传则排除锁定类型
|
||||
* @param {string} [data.exportMode] 导出模式(驼峰写法,同 export_mode)
|
||||
* @return {Promise<Object>} 统一 success 包装(返回任务ID)
|
||||
*/
|
||||
async export(data = {}) {
|
||||
const { service } = this;
|
||||
const { whereValue = {} } = this.prepareWhereContext(data);
|
||||
const {
|
||||
start_time: startTime,
|
||||
end_time: endTime,
|
||||
by_space: bySpace,
|
||||
store_ids: storeIds,
|
||||
stock_id: stockIdRaw,
|
||||
stock_ids: stockIdsRaw,
|
||||
keyword,
|
||||
sort_field: whereSortField,
|
||||
sortField: whereSortFieldCamel,
|
||||
sort_order: whereSortOrder,
|
||||
sortOrder: whereSortOrderCamel,
|
||||
} = whereValue;
|
||||
|
||||
// 兼容 stock_id(单数)和 stock_ids(数组)
|
||||
const stockIds = stockIdsRaw !== undefined
|
||||
? stockIdsRaw
|
||||
: (stockIdRaw !== undefined ? [ stockIdRaw ] : null);
|
||||
|
||||
const { sort = {} } = data;
|
||||
const {
|
||||
sort_field: sortFieldSnake,
|
||||
sortField: sortFieldCamel,
|
||||
sort_order: sortOrderSnake,
|
||||
sortOrder: sortOrderCamel,
|
||||
} = sort || {};
|
||||
|
||||
const exportMode = String(data.export_mode || data.exportMode || '').trim();
|
||||
|
||||
const taskId = this.generateExportTaskId();
|
||||
const taskPayload = {
|
||||
task_id: taskId,
|
||||
status: 'submitted',
|
||||
progress: 0,
|
||||
message: '导出任务已提交',
|
||||
result: null,
|
||||
error: '',
|
||||
};
|
||||
|
||||
await this.setExportTaskProgress(taskId, taskPayload);
|
||||
|
||||
const sortOption = {
|
||||
sortField: sortFieldCamel || sortFieldSnake || whereSortFieldCamel || whereSortField,
|
||||
sortOrder: sortOrderCamel || sortOrderSnake || whereSortOrderCamel || whereSortOrder,
|
||||
};
|
||||
|
||||
setImmediate(async () => {
|
||||
try {
|
||||
await this.setExportTaskProgress(taskId, {
|
||||
task_id: taskId,
|
||||
status: 'running',
|
||||
progress: 10,
|
||||
message: '导出任务执行中',
|
||||
result: null,
|
||||
error: '',
|
||||
});
|
||||
|
||||
const result = await service.systems.storage.report.export(startTime, endTime, bySpace, storeIds, sortOption, exportMode, 'warehouse', stockIds, keyword);
|
||||
|
||||
await this.setExportTaskProgress(taskId, {
|
||||
task_id: taskId,
|
||||
status: 'success',
|
||||
progress: 100,
|
||||
message: '导出任务完成',
|
||||
result,
|
||||
error: '',
|
||||
});
|
||||
} catch (error) {
|
||||
const errorMessage = error && error.message ? error.message : `${error}`;
|
||||
await this.setExportTaskProgress(taskId, {
|
||||
task_id: taskId,
|
||||
status: 'failed',
|
||||
progress: 100,
|
||||
message: '导出任务失败',
|
||||
result: null,
|
||||
error: errorMessage,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return this.success('ok', {
|
||||
...taskPayload,
|
||||
update_time: Date.now(),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询库存流水统计报表导出任务进度。
|
||||
* @param {Object} data 请求体
|
||||
* @param {string} [data.task_id] 任务ID(下划线写法)
|
||||
* @param {string} [data.taskId] 任务ID(驼峰写法)
|
||||
* @return {Promise<Object>} 统一 success 包装
|
||||
*/
|
||||
async exportTaskProgress(data = {}) {
|
||||
const taskId = this.normalizeExportTaskId(data);
|
||||
if (!taskId) {
|
||||
return this.success('ok', {
|
||||
task_id: '',
|
||||
status: 'not_found',
|
||||
progress: 0,
|
||||
message: '任务不存在',
|
||||
result: null,
|
||||
error: '',
|
||||
download_url: '',
|
||||
update_time: Date.now(),
|
||||
});
|
||||
}
|
||||
|
||||
const cachedTask = await this.getExportTaskProgress(taskId);
|
||||
if (!cachedTask) {
|
||||
return this.success('ok', {
|
||||
task_id: taskId,
|
||||
status: 'not_found',
|
||||
progress: 0,
|
||||
message: '任务不存在',
|
||||
result: null,
|
||||
error: '',
|
||||
download_url: '',
|
||||
update_time: Date.now(),
|
||||
});
|
||||
}
|
||||
|
||||
const safeResult = this.isPlainObject(cachedTask.result) ? cachedTask.result : {};
|
||||
const downloadUrl = String(
|
||||
safeResult.url
|
||||
|| safeResult.cndUrl
|
||||
|| safeResult.cosUrl
|
||||
|| safeResult.fileKey
|
||||
|| ''
|
||||
);
|
||||
|
||||
return this.success('ok', {
|
||||
task_id: taskId,
|
||||
...cachedTask,
|
||||
download_url: downloadUrl,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成导出任务ID。
|
||||
* @return {string} 导出任务ID
|
||||
*/
|
||||
generateExportTaskId() {
|
||||
const warehouseId = this.ctx?.user?.store_id || 'unknown';
|
||||
const timestamp = Date.now();
|
||||
const randomToken = Math.random().toString(36).slice(2, 10);
|
||||
return `stock_report_export_${warehouseId}_${timestamp}_${randomToken}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 规范导出任务ID。
|
||||
* @param {Object} data 请求体
|
||||
* @param {string} [data.task_id] 任务ID(下划线写法)
|
||||
* @param {string} [data.taskId] 任务ID(驼峰写法)
|
||||
* @return {string} 归一化后的任务ID
|
||||
*/
|
||||
normalizeExportTaskId(data = {}) {
|
||||
const normalizedData = this.isPlainObject(data) ? data : {};
|
||||
return String(normalizedData.task_id || normalizedData.taskId || '').trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取导出任务缓存键。
|
||||
* @param {string} taskId 任务ID
|
||||
* @return {string} 缓存键
|
||||
*/
|
||||
getExportTaskCacheKey(taskId = '') {
|
||||
const normalizedTaskId = String(taskId || '').trim();
|
||||
if (!normalizedTaskId) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return `${STOCK_REPORT_EXPORT_TASK_CACHE_KEY_PREFIX}${normalizedTaskId}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入导出任务进度缓存。
|
||||
* @param {string} taskId 任务ID
|
||||
* @param {Object} payload 任务状态载荷
|
||||
* @return {Promise<void>} 无返回值
|
||||
*/
|
||||
async setExportTaskProgress(taskId = '', payload = {}) {
|
||||
const cacheKey = this.getExportTaskCacheKey(taskId);
|
||||
if (!cacheKey || typeof this.app.setCacheAsync !== 'function') {
|
||||
return;
|
||||
}
|
||||
|
||||
const safePayload = this.isPlainObject(payload) ? payload : {};
|
||||
const progressPayload = {
|
||||
task_id: String(taskId || '').trim(),
|
||||
status: String(safePayload.status || 'submitted').trim() || 'submitted',
|
||||
progress: Number.isFinite(Number(safePayload.progress)) ? Math.min(100, Math.max(0, Math.round(Number(safePayload.progress)))) : 0,
|
||||
message: String(safePayload.message || ''),
|
||||
result: safePayload.result ?? null,
|
||||
error: String(safePayload.error || ''),
|
||||
update_time: Date.now(),
|
||||
};
|
||||
|
||||
try {
|
||||
await this.app.setCacheAsync(cacheKey, progressPayload, STOCK_REPORT_EXPORT_TASK_CACHE_TTL_SECONDS);
|
||||
} catch (error) {
|
||||
this.ctx.logger.warn('[stock.report.export] 写入导出任务缓存失败: %s', error && error.message ? error.message : error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取导出任务进度缓存。
|
||||
* @param {string} taskId 任务ID
|
||||
* @return {Promise<Object|null>} 任务缓存
|
||||
*/
|
||||
async getExportTaskProgress(taskId = '') {
|
||||
const cacheKey = this.getExportTaskCacheKey(taskId);
|
||||
if (!cacheKey || typeof this.app.getCacheAsync !== 'function') {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const cached = await this.app.getCacheAsync(cacheKey);
|
||||
if (!this.isPlainObject(cached)) {
|
||||
return null;
|
||||
}
|
||||
return cached;
|
||||
} catch (error) {
|
||||
this.ctx.logger.warn('[stock.report.export] 读取导出任务缓存失败: %s', error && error.message ? error.message : error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = StockReportMethodService;
|
||||
@ -1,302 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
const MainService = require('../../main');
|
||||
|
||||
const STORAGE_REPORT_EXPORT_TASK_CACHE_KEY_PREFIX = 'storage_report_export_task_';
|
||||
const STORAGE_REPORT_EXPORT_TASK_CACHE_TTL_SECONDS = 1800;
|
||||
|
||||
/**
|
||||
* 系统方法层:storage.report(库存流水报表接口包装层)。
|
||||
*/
|
||||
class StorageReportMethodService extends MainService {
|
||||
constructor(ctx) {
|
||||
super(ctx);
|
||||
const _noNeedLogin = '';
|
||||
const _noNeedRight = 'search,export,exportTaskProgress';
|
||||
const _noNeedLog = 'search,export,exportTaskProgress';
|
||||
const _noNeedAuth = 'search,export,exportTaskProgress';
|
||||
this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询库存流水统计报表。
|
||||
* @param {Object} data 请求体
|
||||
* @param {Object} [data.where] 查询条件
|
||||
* @param {Object} [data.sort] 排序参数
|
||||
* @param {string} [data.sort.sortField] 排序字段(兼容:sort_field)
|
||||
* @param {string} [data.sort.sortOrder] 排序顺序(兼容:sort_order)
|
||||
* @param {string|Date} [data.where.start_time] 开始时间
|
||||
* @param {string|Date} [data.where.end_time] 结束时间
|
||||
* @param {boolean|string|number} [data.where.by_space=false] 是否按库位返回
|
||||
* @param {Array<number|string>} [data.where.store_ids] 货主ID列表
|
||||
* @return {Promise<Object>} 统一 success 包装
|
||||
*/
|
||||
async search(data = {}) {
|
||||
const { service } = this;
|
||||
const { whereValue = {} } = this.prepareWhereContext(data);
|
||||
const {
|
||||
start_time: startTime,
|
||||
end_time: endTime,
|
||||
by_space: bySpace,
|
||||
store_ids: storeIds,
|
||||
sort_field: whereSortField,
|
||||
sortField: whereSortFieldCamel,
|
||||
sort_order: whereSortOrder,
|
||||
sortOrder: whereSortOrderCamel,
|
||||
} = whereValue;
|
||||
|
||||
const { sort = {} } = data;
|
||||
const {
|
||||
sort_field: sortFieldSnake,
|
||||
sortField: sortFieldCamel,
|
||||
sort_order: sortOrderSnake,
|
||||
sortOrder: sortOrderCamel,
|
||||
} = sort || {};
|
||||
|
||||
const result = await service.systems.storage.report.search(startTime, endTime, bySpace, storeIds, {
|
||||
sortField: sortFieldCamel || sortFieldSnake || whereSortFieldCamel || whereSortField,
|
||||
sortOrder: sortOrderCamel || sortOrderSnake || whereSortOrderCamel || whereSortOrder,
|
||||
});
|
||||
return this.success('ok', result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 提交库存流水统计报表导出任务。
|
||||
* @param {Object} data 请求体
|
||||
* @param {Object} [data.where] 查询条件
|
||||
* @param {Object} [data.sort] 排序参数
|
||||
* @param {string} [data.sort.sortField] 排序字段(兼容:sort_field)
|
||||
* @param {string} [data.sort.sortOrder] 排序顺序(兼容:sort_order)
|
||||
* @param {string|Date} [data.where.start_time] 开始时间
|
||||
* @param {string|Date} [data.where.end_time] 结束时间
|
||||
* @param {boolean|string|number} [data.where.by_space=false] 是否按库位返回
|
||||
* @param {Array<number|string>} [data.where.store_ids] 货主ID列表
|
||||
* @param {string} [data.export_mode] 导出模式:'item' 导出含锁定类型的全部变更记录,不传则排除锁定类型
|
||||
* @param {string} [data.exportMode] 导出模式(驼峰写法,同 export_mode)
|
||||
* @return {Promise<Object>} 统一 success 包装(返回任务ID)
|
||||
*/
|
||||
async export(data = {}) {
|
||||
const { service } = this;
|
||||
const { whereValue = {} } = this.prepareWhereContext(data);
|
||||
const {
|
||||
start_time: startTime,
|
||||
end_time: endTime,
|
||||
by_space: bySpace,
|
||||
store_ids: storeIds,
|
||||
sort_field: whereSortField,
|
||||
sortField: whereSortFieldCamel,
|
||||
sort_order: whereSortOrder,
|
||||
sortOrder: whereSortOrderCamel,
|
||||
} = whereValue;
|
||||
|
||||
const { sort = {} } = data;
|
||||
const {
|
||||
sort_field: sortFieldSnake,
|
||||
sortField: sortFieldCamel,
|
||||
sort_order: sortOrderSnake,
|
||||
sortOrder: sortOrderCamel,
|
||||
} = sort || {};
|
||||
|
||||
const exportMode = String(data.export_mode || data.exportMode || '').trim();
|
||||
|
||||
const taskId = this.generateExportTaskId();
|
||||
const taskPayload = {
|
||||
task_id: taskId,
|
||||
status: 'submitted',
|
||||
progress: 0,
|
||||
message: '导出任务已提交',
|
||||
result: null,
|
||||
error: '',
|
||||
};
|
||||
|
||||
await this.setExportTaskProgress(taskId, taskPayload);
|
||||
|
||||
const sortOption = {
|
||||
sortField: sortFieldCamel || sortFieldSnake || whereSortFieldCamel || whereSortField,
|
||||
sortOrder: sortOrderCamel || sortOrderSnake || whereSortOrderCamel || whereSortOrder,
|
||||
};
|
||||
|
||||
setImmediate(async () => {
|
||||
try {
|
||||
await this.setExportTaskProgress(taskId, {
|
||||
task_id: taskId,
|
||||
status: 'running',
|
||||
progress: 10,
|
||||
message: '导出任务执行中',
|
||||
result: null,
|
||||
error: '',
|
||||
});
|
||||
|
||||
const result = await service.systems.storage.report.export(startTime, endTime, bySpace, storeIds, sortOption, exportMode);
|
||||
|
||||
await this.setExportTaskProgress(taskId, {
|
||||
task_id: taskId,
|
||||
status: 'success',
|
||||
progress: 100,
|
||||
message: '导出任务完成',
|
||||
result,
|
||||
error: '',
|
||||
});
|
||||
} catch (error) {
|
||||
const errorMessage = error && error.message ? error.message : `${error}`;
|
||||
await this.setExportTaskProgress(taskId, {
|
||||
task_id: taskId,
|
||||
status: 'failed',
|
||||
progress: 100,
|
||||
message: '导出任务失败',
|
||||
result: null,
|
||||
error: errorMessage,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return this.success('ok', {
|
||||
...taskPayload,
|
||||
update_time: Date.now(),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询库存流水统计报表导出任务进度。
|
||||
* @param {Object} data 请求体
|
||||
* @param {string} [data.task_id] 任务ID(下划线写法)
|
||||
* @param {string} [data.taskId] 任务ID(驼峰写法)
|
||||
* @return {Promise<Object>} 统一 success 包装
|
||||
*/
|
||||
async exportTaskProgress(data = {}) {
|
||||
const taskId = this.normalizeExportTaskId(data);
|
||||
if (!taskId) {
|
||||
return this.success('ok', {
|
||||
task_id: '',
|
||||
status: 'not_found',
|
||||
progress: 0,
|
||||
message: '任务不存在',
|
||||
result: null,
|
||||
error: '',
|
||||
download_url: '',
|
||||
update_time: Date.now(),
|
||||
});
|
||||
}
|
||||
|
||||
const cachedTask = await this.getExportTaskProgress(taskId);
|
||||
if (!cachedTask) {
|
||||
return this.success('ok', {
|
||||
task_id: taskId,
|
||||
status: 'not_found',
|
||||
progress: 0,
|
||||
message: '任务不存在',
|
||||
result: null,
|
||||
error: '',
|
||||
download_url: '',
|
||||
update_time: Date.now(),
|
||||
});
|
||||
}
|
||||
|
||||
const safeResult = this.isPlainObject(cachedTask.result) ? cachedTask.result : {};
|
||||
const downloadUrl = String(
|
||||
safeResult.url
|
||||
|| safeResult.cndUrl
|
||||
|| safeResult.cosUrl
|
||||
|| safeResult.fileKey
|
||||
|| ''
|
||||
);
|
||||
|
||||
return this.success('ok', {
|
||||
task_id: taskId,
|
||||
...cachedTask,
|
||||
download_url: downloadUrl,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成导出任务ID。
|
||||
* @return {string} 导出任务ID
|
||||
*/
|
||||
generateExportTaskId() {
|
||||
const warehouseId = this.ctx?.user?.store_id || 'unknown';
|
||||
const timestamp = Date.now();
|
||||
const randomToken = Math.random().toString(36).slice(2, 10);
|
||||
return `storage_report_export_${warehouseId}_${timestamp}_${randomToken}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 规范导出任务ID。
|
||||
* @param {Object} data 请求体
|
||||
* @param {string} [data.task_id] 任务ID(下划线写法)
|
||||
* @param {string} [data.taskId] 任务ID(驼峰写法)
|
||||
* @return {string} 归一化后的任务ID
|
||||
*/
|
||||
normalizeExportTaskId(data = {}) {
|
||||
const normalizedData = this.isPlainObject(data) ? data : {};
|
||||
return String(normalizedData.task_id || normalizedData.taskId || '').trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取导出任务缓存键。
|
||||
* @param {string} taskId 任务ID
|
||||
* @return {string} 缓存键
|
||||
*/
|
||||
getExportTaskCacheKey(taskId = '') {
|
||||
const normalizedTaskId = String(taskId || '').trim();
|
||||
if (!normalizedTaskId) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return `${STORAGE_REPORT_EXPORT_TASK_CACHE_KEY_PREFIX}${normalizedTaskId}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入导出任务进度缓存。
|
||||
* @param {string} taskId 任务ID
|
||||
* @param {Object} payload 任务状态载荷
|
||||
* @return {Promise<void>} 无返回值
|
||||
*/
|
||||
async setExportTaskProgress(taskId = '', payload = {}) {
|
||||
const cacheKey = this.getExportTaskCacheKey(taskId);
|
||||
if (!cacheKey || typeof this.app.setCacheAsync !== 'function') {
|
||||
return;
|
||||
}
|
||||
|
||||
const safePayload = this.isPlainObject(payload) ? payload : {};
|
||||
const progressPayload = {
|
||||
task_id: String(taskId || '').trim(),
|
||||
status: String(safePayload.status || 'submitted').trim() || 'submitted',
|
||||
progress: Number.isFinite(Number(safePayload.progress)) ? Math.min(100, Math.max(0, Math.round(Number(safePayload.progress)))) : 0,
|
||||
message: String(safePayload.message || ''),
|
||||
result: safePayload.result ?? null,
|
||||
error: String(safePayload.error || ''),
|
||||
update_time: Date.now(),
|
||||
};
|
||||
|
||||
try {
|
||||
await this.app.setCacheAsync(cacheKey, progressPayload, STORAGE_REPORT_EXPORT_TASK_CACHE_TTL_SECONDS);
|
||||
} catch (error) {
|
||||
this.ctx.logger.warn('[storage.report.export] 写入导出任务缓存失败: %s', error && error.message ? error.message : error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取导出任务进度缓存。
|
||||
* @param {string} taskId 任务ID
|
||||
* @return {Promise<Object|null>} 任务缓存
|
||||
*/
|
||||
async getExportTaskProgress(taskId = '') {
|
||||
const cacheKey = this.getExportTaskCacheKey(taskId);
|
||||
if (!cacheKey || typeof this.app.getCacheAsync !== 'function') {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const cached = await this.app.getCacheAsync(cacheKey);
|
||||
if (!this.isPlainObject(cached)) {
|
||||
return null;
|
||||
}
|
||||
return cached;
|
||||
} catch (error) {
|
||||
this.ctx.logger.warn('[storage.report.export] 读取导出任务缓存失败: %s', error && error.message ? error.message : error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = StorageReportMethodService;
|
||||
@ -245,9 +245,10 @@ class EntryService extends Service {
|
||||
* @param {Object} where 筛选条件对象
|
||||
* @param {Object} pageParams 分页参数
|
||||
* @param {Object} sortParams 排序参数
|
||||
* @param {string} [mode='warehouse'] 查询模式:warehouse=仓库侧(按 warehouse_id 过滤),store=货主侧(按 store_id 过滤)
|
||||
* @return {Promise<Object>} code/data/message 结构
|
||||
*/
|
||||
async list(where = {}, pageParams = {}, sortParams = {}) {
|
||||
async list(where = {}, pageParams = {}, sortParams = {}, mode = 'warehouse') {
|
||||
const { ctx, app } = this;
|
||||
const { Sequelize } = app;
|
||||
const { Op } = Sequelize;
|
||||
@ -268,11 +269,39 @@ class EntryService extends Service {
|
||||
];
|
||||
const actualSortField = validSortFields.includes(sortField) ? sortField : 'created_at';
|
||||
|
||||
const baseWhere = {
|
||||
warehouse_id: ctx.user.store_id,
|
||||
is_enabled: 1,
|
||||
deleted_at: null,
|
||||
};
|
||||
const isStoreMode = String(mode).toLowerCase() === 'store';
|
||||
// 参考 stock.report.search:从 ctx.user.store_id 提取主维度 ID,
|
||||
// Number(undefined)=NaN(falsy),防止 Sequelize 静默剔除 undefined 键导致过滤失效
|
||||
const rawPrimaryId = Number(ctx.user && ctx.user.store_id);
|
||||
const storeId = isStoreMode ? rawPrimaryId : null;
|
||||
const warehouseId = isStoreMode ? null : rawPrimaryId;
|
||||
|
||||
if (isStoreMode && !storeId) {
|
||||
return {
|
||||
code: 400,
|
||||
message: '用户未绑定货主,无法查询货主侧入库单',
|
||||
data: {
|
||||
list: [],
|
||||
pagination: { page, pageSize, total: 0, totalPages: 0, hasPrevious: false, hasNext: false },
|
||||
summary: { total: 0, pending: 0, creating: 0, created: 0, completed: 0, cancelled: 0 },
|
||||
},
|
||||
};
|
||||
}
|
||||
if (!isStoreMode && !warehouseId) {
|
||||
return {
|
||||
code: 400,
|
||||
message: '用户未绑定仓库,无法查询入库单',
|
||||
data: {
|
||||
list: [],
|
||||
pagination: { page, pageSize, total: 0, totalPages: 0, hasPrevious: false, hasNext: false },
|
||||
summary: { total: 0, pending: 0, creating: 0, created: 0, completed: 0, cancelled: 0 },
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const baseWhere = isStoreMode
|
||||
? { store_id: storeId, is_enabled: 1, deleted_at: null }
|
||||
: { warehouse_id: warehouseId, is_enabled: 1, deleted_at: null };
|
||||
|
||||
const normalizedWhere = ctx.where && typeof ctx.where === 'object'
|
||||
? { ...ctx.where }
|
||||
@ -506,22 +535,18 @@ class EntryService extends Service {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询入库单详情。
|
||||
* @param {Object} data 请求体
|
||||
* @return {Promise<Object>} code/data/message 结构
|
||||
*/
|
||||
/**
|
||||
* 查询入库单详情(与原 storage.directInbound.getItems 输出格式一致)。
|
||||
* 返回入库单基本信息、商品明细(含计划数量与已完成数量)、收货单绑定关系。
|
||||
* @param {Object} data 请求参数
|
||||
* @param {number} data.id 入库单主键 ID(必填)
|
||||
* @param {string} [mode='warehouse'] 查询维度:'warehouse' 按仓库校验权限(默认),'store' 按货主校验权限
|
||||
* @return {Promise<Object>} code/data/message 结构
|
||||
* - data.entryOrder: { id, entry_order_code, order_status, store_remask }
|
||||
* - data.items: [{ id, stock_id, planned, completed, stock, bindings }]
|
||||
* - data.receivingBindings: [{ id, receiving_id, receiving }]
|
||||
*/
|
||||
async get(data = {}) {
|
||||
async get(data = {}, mode = 'warehouse') {
|
||||
const { ctx, app } = this;
|
||||
const id = Number(data.id || 0);
|
||||
|
||||
@ -564,9 +589,18 @@ class EntryService extends Service {
|
||||
return { code: 404, message: '入库单不存在', data: null };
|
||||
}
|
||||
|
||||
// 校验仓库权限
|
||||
if (entryOrder.warehouse_id !== ctx.user.store_id || !entryOrder.is_enabled) {
|
||||
return { code: 404, message: '入库单不存在或无权访问', data: null };
|
||||
const isStoreMode = String(mode).toLowerCase() === 'store';
|
||||
const rawPrimaryId = Number(ctx.user && ctx.user.store_id);
|
||||
|
||||
// 校验权限:仓库模式校验 warehouse_id,货主模式校验 store_id
|
||||
if (isStoreMode) {
|
||||
if (entryOrder.store_id !== rawPrimaryId || !entryOrder.is_enabled) {
|
||||
return { code: 404, message: '入库单不存在或无权访问', data: null };
|
||||
}
|
||||
} else {
|
||||
if (entryOrder.warehouse_id !== rawPrimaryId || !entryOrder.is_enabled) {
|
||||
return { code: 404, message: '入库单不存在或无权访问', data: null };
|
||||
}
|
||||
}
|
||||
|
||||
// 查询货主商家备注
|
||||
@ -627,8 +661,9 @@ class EntryService extends Service {
|
||||
/** @type {Map<number, Object>} */
|
||||
const stockMap = new Map();
|
||||
if (stockIds.length > 0) {
|
||||
const warehouseId = ctx.user.store_id;
|
||||
const stockDetails = await ctx.service.systems.stockWarehouse.getStockDetails(stockIds, { warehouseId });
|
||||
// 货主模式下传 null 跳过 warehouse 过滤,避免 store_id 误当 warehouse_id 导致查不到商品
|
||||
const stockWarehouseId = isStoreMode ? null : rawPrimaryId;
|
||||
const stockDetails = await ctx.service.systems.stockWarehouse.getStockDetails(stockIds, { warehouseId: stockWarehouseId });
|
||||
for (const detail of stockDetails) {
|
||||
stockMap.set(detail.id, detail);
|
||||
}
|
||||
|
||||
@ -1,4 +1,25 @@
|
||||
[
|
||||
{
|
||||
"version": "2026.07.10-system-01",
|
||||
"date": "2026-07-10",
|
||||
"type": "added",
|
||||
"summary": "新增 storage.report.searchStore 货主侧库存报表,以 store_id 为主维度查询库存流水",
|
||||
"methods": [
|
||||
"storage.report.searchStore",
|
||||
"storage.report.exportStore"
|
||||
],
|
||||
"notes": [
|
||||
"新增 searchStore 方法:与 search 共享核心逻辑,通过 searchMode='store' 切换主维度从 warehouse_id → store_id。",
|
||||
"新增 exportStore 方法:与 export 共享核心逻辑,支持货主侧导出 Excel(含汇总 + 变更记录两个 sheet)。",
|
||||
"systems 层 search/export 方法新增 searchMode 参数(默认 'warehouse'),不影响现有 warehouse 侧行为。",
|
||||
"货主侧使用 ctx.user.store_id 作为主维度 store_id,辅助维度支持 warehouse_ids 过滤特定仓库。",
|
||||
"systemMethods 层 searchStore 从 where.warehouse_ids 取值传给 search 的 storeIds(承载 warehouse 过滤)。",
|
||||
"输出新增 warehouse_id / warehouse_name 字段,货主侧可区分不同仓库的库存。search 方法也同步输出这两个字段(向后兼容)。",
|
||||
"修复 getStockDetails 的 warehouseId 判断:从 != null 改为 !== undefined,使显式传 null 可跳过 warehouse 过滤。",
|
||||
"修复 loadReportReferenceMaps 未传递 warehouseId 给 getStockDetails 的问题。"
|
||||
],
|
||||
"module": "storage.report"
|
||||
},
|
||||
{
|
||||
"version": "2026.07.09-system-01",
|
||||
"date": "2026-07-09",
|
||||
@ -4859,5 +4880,37 @@
|
||||
"备注修改功能已独立为 order.pendingOrder.updateRemark 方法。"
|
||||
],
|
||||
"module": "order.pendingOrder"
|
||||
},
|
||||
{
|
||||
"version": "2026-07-16",
|
||||
"date": "2026-07-16",
|
||||
"summary": "新增 workbench.bindCamera 接口,支持工作台绑定/解绑摄像头",
|
||||
"methods": [
|
||||
"workbench.bindCamera"
|
||||
],
|
||||
"notes": [
|
||||
"通过 device_ids JSON 字段存储绑定关系",
|
||||
"传空数组可解绑"
|
||||
]
|
||||
},
|
||||
{
|
||||
"version": "2026.07.17-stock-report-migration-01",
|
||||
"date": "2026-07-17",
|
||||
"type": "updated",
|
||||
"summary": "storage.report 模块迁移至 stock.report:修正语义错误,库存报表属于 stock 域而非 storage 域",
|
||||
"methods": [
|
||||
"stock.report.search",
|
||||
"stock.report.searchStore",
|
||||
"stock.report.export",
|
||||
"stock.report.exportStore",
|
||||
"stock.report.exportTaskProgress"
|
||||
],
|
||||
"notes": [
|
||||
"将 systemMethods/storage/report.js 移动到 systemMethods/stock/report.js",
|
||||
"内部引用同步更新:类名 StorageReportMethodService→StockReportMethodService,缓存键 storage_report→stock_report,日志前缀 [storage.report]→[stock.report]",
|
||||
"旧文件直接删除,不保留兼容(storage.report 为语义错误,调用方需同步更新 method 名)",
|
||||
"更新 systems/storage/entry.js L273 注释引用",
|
||||
"文档:methods/system.json module storage.report→stock.report,5个 method 名同步更新"
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
1604
methods/system.json
1604
methods/system.json
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user