perf(stock.report): 库存报表导出改游标分批+ExcelJS 流式,参考订单导出架构
- 原实现:明细一条大 SQL 全量拉回(货主 1000305 单次 16.9 万行),
SheetJS aoa_to_sheet 全内存构建 490 万单元格后一次性 XLSX.write 序列化,
生产实测总耗时 81.7s(其中 Excel 生成约 30~50s、内存峰值约 2GB),
且进度只有 10/100 两档,中间 80s 零反馈被误判卡死
- 改造(对齐 order_export 流式架构):
1) 明细按主键 id 游标分批拉取(batchSize=5000,走 PRIMARY 索引,
生产实测每批约 95ms、末批不满即止无空探测),行序由 created_at,id
变更为 id 升序(自增序≈插入序)
2) ExcelJS.stream.xlsx.WorkbookWriter 流式落盘(useStyles/useSharedStrings
关闭),逐批行级 commit,内存占用从约 2GB 降到批级
3) 富化映射(商品/货主/条码/入库单/订单/运单)改为按批加载
4) onProgress 每批上报 current_count/total_count,进度 5→95→100 平滑
- excel.js 新增 uploadExistingFile:流式生成场景复用上传链路
(归一化文件名/buildUploadKey/uploadFile/uploadSuccessNotify),
供 WorkbookWriter 落盘后 createReadStream 上传
- systemMethods 层 export/exportStore 接 onProgress 并持久化
current_count/total_count 字段(向前端兼容订单导出进度组件)
- 验证:storage_report_export.test.js 2/2 PASS(12000 行游标 3 批、
进度序列 5/95/100、回读 Excel 校验 sheet/表头/行数/单元格值、
空数据仅表头);生产只读探测 EXPLAIN 走 PRIMARY range、
首尾页约 95ms;ESLint 4 文件 0 error
This commit is contained in:
parent
a123009159
commit
8112430c37
@ -228,12 +228,26 @@ class StockReportMethodService extends MainService {
|
||||
sortOrder: sortOrderCamel || sortOrderSnake || whereSortOrderCamel || whereSortOrder,
|
||||
};
|
||||
|
||||
const onProgress = async progress => {
|
||||
const safeProgress = this.isPlainObject(progress) ? progress : {};
|
||||
await this.setExportTaskProgress(taskId, {
|
||||
task_id: taskId,
|
||||
status: safeProgress.status || 'running',
|
||||
progress: safeProgress.progress,
|
||||
current_count: safeProgress.currentCount ?? safeProgress.current_count ?? 0,
|
||||
total_count: safeProgress.totalCount ?? safeProgress.total_count ?? 0,
|
||||
message: safeProgress.message || '导出任务执行中',
|
||||
result: safeProgress.result || null,
|
||||
error: '',
|
||||
});
|
||||
};
|
||||
|
||||
setImmediate(async () => {
|
||||
try {
|
||||
await this.setExportTaskProgress(taskId, {
|
||||
task_id: taskId,
|
||||
status: 'running',
|
||||
progress: 10,
|
||||
progress: 5,
|
||||
message: '导出任务执行中',
|
||||
result: null,
|
||||
error: '',
|
||||
@ -241,7 +255,7 @@ class StockReportMethodService extends MainService {
|
||||
|
||||
const result = await service.systems.storage.report.export(
|
||||
startTime, endTime, false, warehouseIds,
|
||||
sortOption, exportMode, 'store', stockIds, keyword
|
||||
sortOption, exportMode, 'store', stockIds, keyword, onProgress
|
||||
);
|
||||
|
||||
await this.setExportTaskProgress(taskId, {
|
||||
@ -338,18 +352,35 @@ class StockReportMethodService extends MainService {
|
||||
sortOrder: sortOrderCamel || sortOrderSnake || whereSortOrderCamel || whereSortOrder,
|
||||
};
|
||||
|
||||
const onProgress = async progress => {
|
||||
const safeProgress = this.isPlainObject(progress) ? progress : {};
|
||||
await this.setExportTaskProgress(taskId, {
|
||||
task_id: taskId,
|
||||
status: safeProgress.status || 'running',
|
||||
progress: safeProgress.progress,
|
||||
current_count: safeProgress.currentCount ?? safeProgress.current_count ?? 0,
|
||||
total_count: safeProgress.totalCount ?? safeProgress.total_count ?? 0,
|
||||
message: safeProgress.message || '导出任务执行中',
|
||||
result: safeProgress.result || null,
|
||||
error: '',
|
||||
});
|
||||
};
|
||||
|
||||
setImmediate(async () => {
|
||||
try {
|
||||
await this.setExportTaskProgress(taskId, {
|
||||
task_id: taskId,
|
||||
status: 'running',
|
||||
progress: 10,
|
||||
progress: 5,
|
||||
message: '导出任务执行中',
|
||||
result: null,
|
||||
error: '',
|
||||
});
|
||||
|
||||
const result = await service.systems.storage.report.export(startTime, endTime, bySpace, storeIds, sortOption, exportMode, 'warehouse', stockIds, keyword);
|
||||
const result = await service.systems.storage.report.export(
|
||||
startTime, endTime, bySpace, storeIds, sortOption, exportMode,
|
||||
'warehouse', stockIds, keyword, onProgress
|
||||
);
|
||||
|
||||
await this.setExportTaskProgress(taskId, {
|
||||
task_id: taskId,
|
||||
@ -480,10 +511,16 @@ class StockReportMethodService extends MainService {
|
||||
}
|
||||
|
||||
const safePayload = this.isPlainObject(payload) ? payload : {};
|
||||
const normalizeCount = value => {
|
||||
const numericValue = Number(value);
|
||||
return Number.isFinite(numericValue) && numericValue >= 0 ? Math.round(numericValue) : 0;
|
||||
};
|
||||
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,
|
||||
current_count: normalizeCount(safePayload.current_count),
|
||||
total_count: normalizeCount(safePayload.total_count),
|
||||
message: String(safePayload.message || ''),
|
||||
result: safePayload.result ?? null,
|
||||
error: String(safePayload.error || ''),
|
||||
|
||||
@ -740,6 +740,101 @@ class ExcelService extends Service {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传已经生成到本地临时目录的 Excel 文件(供流式生成场景复用上传链路)。
|
||||
* 与 generateAndUpload 的区别:文件由调用方(如 ExcelJS.stream.WorkbookWriter)落盘,
|
||||
* 这里只负责归一化文件名、构建上传 key、流式上传与可选的落库记录。
|
||||
* @param {Object} options 参数
|
||||
* @param {string} options.filePath 本地文件绝对路径
|
||||
* @param {string} [options.filename] 原始文件名
|
||||
* @param {string} [options.dir='exports/excel'] 上传目录
|
||||
* @param {string} [options.key] 自定义上传 key(优先于 dir)
|
||||
* @param {string} [options.category='document'] 文件分类
|
||||
* @param {boolean} [options.saveRecord=false] 是否落库记录
|
||||
* @param {string} [options.businessType] 业务类型(落库记录用)
|
||||
* @param {string} [options.businessId] 业务ID(落库记录用)
|
||||
* @param {string} [options.uploaderDept] 上传部门(落库记录用)
|
||||
* @param {string|number} [options.expireTime] 过期时间(落库记录用)
|
||||
* @param {number} [options.expireDays] 过期天数(落库记录用)
|
||||
* @param {Function} [options.onProgress] 上传进度回调
|
||||
* @return {Promise<Object>} 上传结果(含 filename/fileKey/size/contentType 与 uploadFile 返回值)
|
||||
*/
|
||||
async uploadExistingFile({
|
||||
filePath,
|
||||
filename = 'export.xlsx',
|
||||
dir = 'exports/excel',
|
||||
key,
|
||||
category = 'document',
|
||||
saveRecord = false,
|
||||
businessType,
|
||||
businessId,
|
||||
uploaderDept,
|
||||
expireTime,
|
||||
expireDays,
|
||||
onProgress,
|
||||
...options
|
||||
} = {}) {
|
||||
const uploadService = this.ctx && this.ctx.service && this.ctx.service.systems && this.ctx.service.systems.upload;
|
||||
|
||||
if (!uploadService || typeof uploadService.uploadFile !== 'function') {
|
||||
throw new Error('上传服务不可用,无法上传 Excel 文件');
|
||||
}
|
||||
|
||||
if (typeof filePath !== 'string' || !filePath) {
|
||||
throw new Error('filePath 不能为空');
|
||||
}
|
||||
|
||||
const bookType = String(options.bookType || path.extname(filename).replace('.', '') || 'xlsx').toLowerCase();
|
||||
const safeFilename = this.sanitizeFilename(filename, `.${bookType}`);
|
||||
const contentType = this.getContentType(bookType);
|
||||
const stat = await fs.promises.stat(filePath);
|
||||
const uploadKey = this.buildUploadKey(safeFilename, { dir, key });
|
||||
const uploadStream = fs.createReadStream(filePath);
|
||||
|
||||
uploadStream.filename = safeFilename;
|
||||
uploadStream.mimeType = contentType;
|
||||
uploadStream.fileSize = stat.size;
|
||||
|
||||
const uploadResult = await uploadService.uploadFile(uploadStream, {
|
||||
key: uploadKey,
|
||||
category,
|
||||
mimetype: 'xls,xlsx,csv',
|
||||
onProgress,
|
||||
});
|
||||
|
||||
let fileRecord = null;
|
||||
if ((saveRecord || businessType || businessId) && typeof uploadService.uploadSuccessNotify === 'function') {
|
||||
const recordResult = await uploadService.uploadSuccessNotify({
|
||||
url: uploadResult.fileName,
|
||||
fileKey: uploadResult.fileName,
|
||||
fileName: safeFilename,
|
||||
name: safeFilename,
|
||||
fileSize: stat.size,
|
||||
size: stat.size,
|
||||
fileType: category,
|
||||
mimeType: contentType,
|
||||
type: category,
|
||||
category,
|
||||
businessType,
|
||||
businessId,
|
||||
uploaderDept,
|
||||
expireTime,
|
||||
expireDays,
|
||||
});
|
||||
|
||||
fileRecord = recordResult && (recordResult.data || recordResult);
|
||||
}
|
||||
|
||||
return {
|
||||
filename: safeFilename,
|
||||
fileKey: uploadResult.fileName,
|
||||
size: stat.size,
|
||||
contentType,
|
||||
...uploadResult,
|
||||
fileRecord,
|
||||
};
|
||||
}
|
||||
|
||||
async sendFile({ ctx = this.ctx, filename = 'export.xlsx', ...options } = {}) {
|
||||
if (!ctx) {
|
||||
throw new Error('ctx 不存在,无法输出下载响应');
|
||||
|
||||
@ -1,6 +1,10 @@
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
const Service = require('egg').Service;
|
||||
const ExcelJS = require('exceljs');
|
||||
|
||||
/**
|
||||
* 将输入转换为时间戳。
|
||||
@ -614,9 +618,10 @@ class StorageReportService extends Service {
|
||||
* @param {string} [searchMode='warehouse'] 查询维度:'warehouse' 按仓库维度(默认),'store' 按货主维度
|
||||
* @param {number[]} [stockIds=null] 商品库存ID列表(精确过滤)
|
||||
* @param {string} [keyword=''] 商品关键字(模糊匹配)
|
||||
* @param {Function} [onProgress=null] 进度回调:async ({ status, progress, currentCount, totalCount, message, result }) => void
|
||||
* @return {Promise<Object>} excel 上传结果
|
||||
*/
|
||||
async export(startTime, endTime, bySpace = false, storeIds = [], sort = null, exportMode = '', searchMode = 'warehouse', stockIds = null, keyword = '') {
|
||||
async export(startTime, endTime, bySpace = false, storeIds = [], sort = null, exportMode = '', searchMode = 'warehouse', stockIds = null, keyword = '', onProgress = null) {
|
||||
const { ctx, app } = this;
|
||||
const isStoreMode = searchMode === 'store';
|
||||
// 导出必须全量:显式传大分页,避免默认分页(pageSize=20)截断汇总与明细白名单
|
||||
@ -692,70 +697,29 @@ class StorageReportService extends Service {
|
||||
const detailReceivingIdSelectSql = isBySpace ? 'NULL AS receiving_id' : 't.receiving_id';
|
||||
const detailEntryOrderIdSelectSql = isBySpace ? 'NULL AS entry_order_id' : 't.entry_order_id';
|
||||
|
||||
const detailRows = await ctx.model.query(`
|
||||
SELECT
|
||||
t.id,
|
||||
t.created_at,
|
||||
t.transaction_type,
|
||||
t.store_id,
|
||||
t.warehouse_id,
|
||||
t.stock_id,
|
||||
${detailSpaceSelectSql},
|
||||
t.quantity,
|
||||
t.before_quantity,
|
||||
t.after_quantity,
|
||||
t.before_locked_quantity,
|
||||
t.after_locked_quantity,
|
||||
t.batch_no,
|
||||
${detailInventoryTypeSelectSql},
|
||||
t.order_type,
|
||||
t.order_no,
|
||||
t.order_id,
|
||||
${detailReceivingIdSelectSql},
|
||||
${detailEntryOrderIdSelectSql},
|
||||
t.operator_name,
|
||||
t.remark
|
||||
FROM ${detailTransactionTable} t
|
||||
WHERE ${whereClauses.join(' AND ')}
|
||||
ORDER BY t.created_at ASC, t.id ASC
|
||||
`, {
|
||||
replacements,
|
||||
type: app.Sequelize.QueryTypes.SELECT,
|
||||
});
|
||||
const progressHandler = typeof onProgress === 'function' ? onProgress : async () => {};
|
||||
|
||||
const safeDetailRows = Array.isArray(detailRows) ? detailRows : [];
|
||||
const detailStockIds = [ ...new Set(safeDetailRows.map(item => Number(item.stock_id)).filter(item => Number.isInteger(item) && item > 0)) ];
|
||||
const detailStoreIds = [ ...new Set(safeDetailRows.map(item => Number(item.store_id)).filter(item => Number.isInteger(item) && item > 0)) ];
|
||||
const detailSpaceIds = [ ...new Set(safeDetailRows.map(item => Number(item.space_id)).filter(item => Number.isInteger(item) && item > 0)) ];
|
||||
const detailEntryOrderIds = [ ...new Set(safeDetailRows.map(item => Number(item.entry_order_id)).filter(item => Number.isInteger(item) && item > 0)) ];
|
||||
const detailOrderIds = [ ...new Set(safeDetailRows.map(item => Number(item.order_id)).filter(item => Number.isInteger(item) && item > 0)) ];
|
||||
|
||||
const summaryStoreIds = [ ...new Set(summaryList.map(item => Number(item.store_id)).filter(item => Number.isInteger(item) && item > 0)) ];
|
||||
const summarySpaceIds = isBySpace
|
||||
? [ ...new Set(summaryList.map(item => Number(item.space_id)).filter(item => Number.isInteger(item) && item > 0)) ]
|
||||
: [];
|
||||
|
||||
// 货主模式:从流水明细按订单ID收集所属仓库,用于运单查询强制同仓校验
|
||||
const detailOrderWarehouseMap = new Map();
|
||||
for (const item of safeDetailRows) {
|
||||
const orderId = Number(item.order_id);
|
||||
const rowWarehouseId = Number(item.warehouse_id);
|
||||
if (Number.isInteger(orderId) && orderId > 0 && Number.isInteger(rowWarehouseId) && rowWarehouseId > 0) {
|
||||
detailOrderWarehouseMap.set(orderId, rowWarehouseId);
|
||||
// 明细总数:仅用于进度展示,COUNT 一次(秒级)
|
||||
const totalCountRows = await ctx.model.query(
|
||||
`SELECT COUNT(*) AS cnt FROM ${detailTransactionTable} t WHERE ${whereClauses.join(' AND ')}`,
|
||||
{
|
||||
replacements,
|
||||
type: app.Sequelize.QueryTypes.SELECT,
|
||||
}
|
||||
}
|
||||
);
|
||||
const detailTotalCount = Array.isArray(totalCountRows) && totalCountRows.length > 0
|
||||
? Number(totalCountRows[0].cnt)
|
||||
: 0;
|
||||
|
||||
const maps = await this.loadReportReferenceMaps({
|
||||
stockIds: [ ...new Set([ ...summaryStockIds, ...detailStockIds ]) ],
|
||||
storeIds: [ ...new Set([ ...summaryStoreIds, ...detailStoreIds ]) ],
|
||||
spaceIds: [ ...new Set([ ...summarySpaceIds, ...detailSpaceIds ]) ],
|
||||
includeBarcode: true,
|
||||
entryOrderIds: detailEntryOrderIds,
|
||||
orderIds: detailOrderIds,
|
||||
warehouseId,
|
||||
orderWarehouseMap: detailOrderWarehouseMap,
|
||||
await progressHandler({
|
||||
status: 'running',
|
||||
progress: detailTotalCount > 0 ? 5 : 90,
|
||||
currentCount: 0,
|
||||
totalCount: detailTotalCount,
|
||||
message: detailTotalCount > 0 ? `已命中 ${detailTotalCount} 条变更记录,开始导出` : '未命中变更记录,开始生成文件',
|
||||
});
|
||||
|
||||
// 汇总 sheet 数据(汇总量小,全内存构建)
|
||||
const summaryColumns = [
|
||||
{ header: '开始时间', key: 'start_time' },
|
||||
{ header: '结束时间', key: 'end_time' },
|
||||
@ -802,58 +766,6 @@ class StorageReportService extends Service {
|
||||
end_total: toNumber(item.end_total),
|
||||
}));
|
||||
|
||||
const detailSheetRows = safeDetailRows.map(item => {
|
||||
const stockId = toNumber(item.stock_id);
|
||||
const storeId = toNumber(item.store_id);
|
||||
const stock = maps.stockMap.get(stockId) || {};
|
||||
const store = maps.storeMap.get(storeId) || {};
|
||||
const barcodeSet = maps.barcodeMap.get(stockId);
|
||||
|
||||
const entryOrderId = toNumber(item.entry_order_id);
|
||||
const entryOrder = entryOrderId > 0 ? (maps.entryOrderMap.get(entryOrderId) || {}) : {};
|
||||
const isQimenEntry = entryOrder.source_type === 'QM';
|
||||
|
||||
const orderId = toNumber(item.order_id);
|
||||
const orderDelivery = orderId > 0 ? (maps.orderDeliveryMap.get(orderId) || {}) : {};
|
||||
const waybillCode = maps.orderExpressMap.get(orderId) || '';
|
||||
|
||||
// 运单号:出库取 order_express.waybill_code,入库取 entry_order.express_code
|
||||
const generalWaybillNo = waybillCode
|
||||
|| (entryOrderId > 0 ? String(entryOrder.express_code || '').trim() : '');
|
||||
|
||||
return {
|
||||
created_at: formatDateTime(item.created_at),
|
||||
transaction_type: formatTransactionType(item.transaction_type),
|
||||
store_id: storeId,
|
||||
store_name: store.company_name || '',
|
||||
store_remask: maps.storeWarehouseMap.get(storeId) || '',
|
||||
stock_id: stockId,
|
||||
item_code: stock.item_code || '',
|
||||
item_name: stock.item_name || '',
|
||||
barcode: barcodeSet ? [ ...barcodeSet ].join(',') : '',
|
||||
quantity: toNumber(item.quantity),
|
||||
before_quantity: toNumber(item.before_quantity),
|
||||
after_quantity: toNumber(item.after_quantity),
|
||||
before_locked_quantity: toNumber(item.before_locked_quantity),
|
||||
after_locked_quantity: toNumber(item.after_locked_quantity),
|
||||
batch_no: item.batch_no || '',
|
||||
inventory_type: item.inventory_type || '',
|
||||
order_type: item.order_type || '',
|
||||
order_no: item.order_no || '',
|
||||
order_id: orderId,
|
||||
receiving_id: toNumber(item.receiving_id),
|
||||
entry_order_id: entryOrderId,
|
||||
operator_name: item.operator_name || '',
|
||||
remark: item.remark || '',
|
||||
// 新增字段
|
||||
qimen_entry_order_code: isQimenEntry ? String(entryOrder.entry_order_code || '').trim() : '',
|
||||
platform_trade_no: String(orderDelivery.source_order_codes || item.order_no || '').trim(),
|
||||
entry_order_waybill_no: entryOrderId > 0 ? String(entryOrder.express_code || '').trim() : '',
|
||||
entry_order_express_company: entryOrderId > 0 ? String(entryOrder.logistics_name || '').trim() : '',
|
||||
waybill_no: String(generalWaybillNo || '').trim(),
|
||||
};
|
||||
});
|
||||
|
||||
const detailColumns = [
|
||||
{ header: '时间', key: 'created_at' },
|
||||
{ header: '类型', key: 'transaction_type' },
|
||||
@ -884,23 +796,221 @@ class StorageReportService extends Service {
|
||||
{ header: '备注', key: 'remark' },
|
||||
];
|
||||
|
||||
return await ctx.service.systems.excel.generateAndUpload({
|
||||
filename: this.buildExportFilename(startTime, endTime),
|
||||
dir: `exports/storage/report/${warehouseId || storeId || 'common'}`,
|
||||
category: 'document',
|
||||
sheets: [
|
||||
{
|
||||
name: '汇总',
|
||||
columns: summaryColumns,
|
||||
rows: summarySheetRows,
|
||||
},
|
||||
{
|
||||
name: '变更记录',
|
||||
columns: detailColumns,
|
||||
rows: detailSheetRows,
|
||||
},
|
||||
],
|
||||
});
|
||||
// 明细游标分页:按主键 id 升序分批拉取(id 自增序≈插入序),
|
||||
// 流式写入 ExcelJS stream,避免一次性把全部明细与单元格留在内存。
|
||||
const detailBatchSize = 5000;
|
||||
const filename = this.buildExportFilename(startTime, endTime);
|
||||
let tempDir = null;
|
||||
let workbook = null;
|
||||
let currentCount = 0;
|
||||
let cursorId = 0;
|
||||
|
||||
try {
|
||||
tempDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'wms-stock-report-export-'));
|
||||
const filePath = path.join(tempDir, filename);
|
||||
workbook = new ExcelJS.stream.xlsx.WorkbookWriter({
|
||||
filename: filePath,
|
||||
useStyles: false,
|
||||
useSharedStrings: false,
|
||||
});
|
||||
|
||||
const summaryWorksheet = workbook.addWorksheet('汇总');
|
||||
summaryWorksheet.columns = summaryColumns.map(column => ({ header: column.header, key: column.key }));
|
||||
for (const row of summarySheetRows) {
|
||||
summaryWorksheet.addRow(row);
|
||||
}
|
||||
summaryWorksheet.commit();
|
||||
|
||||
const detailWorksheet = workbook.addWorksheet('变更记录');
|
||||
detailWorksheet.columns = detailColumns.map(column => ({ header: column.header, key: column.key }));
|
||||
|
||||
for (;;) {
|
||||
const batchReplacements = { ...replacements, cursorId };
|
||||
const batchWhereClauses = [ ...whereClauses, 't.id > :cursorId' ];
|
||||
const batchRows = await ctx.model.query(`
|
||||
SELECT
|
||||
t.id,
|
||||
t.created_at,
|
||||
t.transaction_type,
|
||||
t.store_id,
|
||||
t.warehouse_id,
|
||||
t.stock_id,
|
||||
${detailSpaceSelectSql},
|
||||
t.quantity,
|
||||
t.before_quantity,
|
||||
t.after_quantity,
|
||||
t.before_locked_quantity,
|
||||
t.after_locked_quantity,
|
||||
t.batch_no,
|
||||
${detailInventoryTypeSelectSql},
|
||||
t.order_type,
|
||||
t.order_no,
|
||||
t.order_id,
|
||||
${detailReceivingIdSelectSql},
|
||||
${detailEntryOrderIdSelectSql},
|
||||
t.operator_name,
|
||||
t.remark
|
||||
FROM ${detailTransactionTable} t
|
||||
WHERE ${batchWhereClauses.join(' AND ')}
|
||||
ORDER BY t.id ASC
|
||||
LIMIT ${detailBatchSize}
|
||||
`, {
|
||||
replacements: batchReplacements,
|
||||
type: app.Sequelize.QueryTypes.SELECT,
|
||||
});
|
||||
|
||||
const safeBatchRows = Array.isArray(batchRows) ? batchRows : [];
|
||||
if (safeBatchRows.length === 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
// 本批富化:仅针对本批涉及的商品/货主/库位/入库单/订单加载映射
|
||||
const batchStockIds = [ ...new Set(safeBatchRows.map(item => Number(item.stock_id)).filter(item => Number.isInteger(item) && item > 0)) ];
|
||||
const batchStoreIds = [ ...new Set(safeBatchRows.map(item => Number(item.store_id)).filter(item => Number.isInteger(item) && item > 0)) ];
|
||||
const batchSpaceIds = [ ...new Set(safeBatchRows.map(item => Number(item.space_id)).filter(item => Number.isInteger(item) && item > 0)) ];
|
||||
const batchEntryOrderIds = [ ...new Set(safeBatchRows.map(item => Number(item.entry_order_id)).filter(item => Number.isInteger(item) && item > 0)) ];
|
||||
const batchOrderIds = [ ...new Set(safeBatchRows.map(item => Number(item.order_id)).filter(item => Number.isInteger(item) && item > 0)) ];
|
||||
|
||||
// 货主模式:从流水明细按订单ID收集所属仓库,用于运单查询强制同仓校验
|
||||
const batchOrderWarehouseMap = new Map();
|
||||
for (const item of safeBatchRows) {
|
||||
const orderId = Number(item.order_id);
|
||||
const rowWarehouseId = Number(item.warehouse_id);
|
||||
if (Number.isInteger(orderId) && orderId > 0 && Number.isInteger(rowWarehouseId) && rowWarehouseId > 0) {
|
||||
batchOrderWarehouseMap.set(orderId, rowWarehouseId);
|
||||
}
|
||||
}
|
||||
|
||||
const maps = await this.loadReportReferenceMaps({
|
||||
stockIds: batchStockIds,
|
||||
storeIds: batchStoreIds,
|
||||
spaceIds: batchSpaceIds,
|
||||
includeBarcode: true,
|
||||
entryOrderIds: batchEntryOrderIds,
|
||||
orderIds: batchOrderIds,
|
||||
warehouseId,
|
||||
orderWarehouseMap: batchOrderWarehouseMap,
|
||||
});
|
||||
|
||||
let lastRow = null;
|
||||
for (const item of safeBatchRows) {
|
||||
const exportRow = this.buildDetailExportRow(item, maps);
|
||||
lastRow = detailWorksheet.addRow(exportRow);
|
||||
currentCount += 1;
|
||||
const rowId = Number(item.id);
|
||||
if (Number.isInteger(rowId) && rowId > cursorId) {
|
||||
cursorId = rowId;
|
||||
}
|
||||
}
|
||||
// 行级 commit:仅 flush 到当前批次末行(worksheet.commit 是终态,不能在中间调用)
|
||||
if (lastRow) {
|
||||
lastRow.commit();
|
||||
}
|
||||
|
||||
await progressHandler({
|
||||
status: 'running',
|
||||
progress: detailTotalCount > 0
|
||||
? Math.min(95, Math.max(5, Math.round(5 + (currentCount / detailTotalCount) * 90)))
|
||||
: 95,
|
||||
currentCount,
|
||||
totalCount: detailTotalCount,
|
||||
message: detailTotalCount > 0 ? `已完成 ${currentCount}/${detailTotalCount} 条变更记录` : '变更记录生成完成',
|
||||
});
|
||||
|
||||
// 本批不满一页说明已到末尾,避免再发一次空查询
|
||||
if (safeBatchRows.length < detailBatchSize) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
detailWorksheet.commit();
|
||||
await workbook.commit();
|
||||
|
||||
const uploadResult = await ctx.service.systems.excel.uploadExistingFile({
|
||||
filePath,
|
||||
filename,
|
||||
dir: `exports/storage/report/${warehouseId || storeId || 'common'}`,
|
||||
category: 'document',
|
||||
});
|
||||
|
||||
await progressHandler({
|
||||
status: 'success',
|
||||
progress: 100,
|
||||
currentCount,
|
||||
totalCount: detailTotalCount,
|
||||
message: '导出任务完成',
|
||||
result: uploadResult,
|
||||
});
|
||||
|
||||
return uploadResult;
|
||||
} catch (error) {
|
||||
if (workbook && workbook.stream && typeof workbook.stream.destroy === 'function') {
|
||||
workbook.stream.destroy();
|
||||
}
|
||||
throw error;
|
||||
} finally {
|
||||
if (tempDir) {
|
||||
await fs.promises.rm(tempDir, { recursive: true, force: true }).catch(() => {});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将一条流水明细行映射为导出 sheet 行。
|
||||
* @param {Object} item 流水明细行
|
||||
* @param {Object} maps 名称映射集合(loadReportReferenceMaps 返回值)
|
||||
* @return {Object} 导出 sheet 行
|
||||
*/
|
||||
buildDetailExportRow(item, maps = {}) {
|
||||
const stockId = toNumber(item.stock_id);
|
||||
const storeId = toNumber(item.store_id);
|
||||
const stock = maps.stockMap && maps.stockMap.get(stockId) || {};
|
||||
const store = maps.storeMap && maps.storeMap.get(storeId) || {};
|
||||
const barcodeSet = maps.barcodeMap ? maps.barcodeMap.get(stockId) : null;
|
||||
|
||||
const entryOrderId = toNumber(item.entry_order_id);
|
||||
const entryOrder = entryOrderId > 0 && maps.entryOrderMap ? (maps.entryOrderMap.get(entryOrderId) || {}) : {};
|
||||
const isQimenEntry = entryOrder.source_type === 'QM';
|
||||
|
||||
const orderId = toNumber(item.order_id);
|
||||
const orderDelivery = orderId > 0 && maps.orderDeliveryMap ? (maps.orderDeliveryMap.get(orderId) || {}) : {};
|
||||
const waybillCode = maps.orderExpressMap ? (maps.orderExpressMap.get(orderId) || '') : '';
|
||||
|
||||
// 运单号:出库取 order_express.waybill_code,入库取 entry_order.express_code
|
||||
const generalWaybillNo = waybillCode
|
||||
|| (entryOrderId > 0 ? String(entryOrder.express_code || '').trim() : '');
|
||||
|
||||
return {
|
||||
created_at: formatDateTime(item.created_at),
|
||||
transaction_type: formatTransactionType(item.transaction_type),
|
||||
store_id: storeId,
|
||||
store_name: store.company_name || '',
|
||||
store_remask: maps.storeWarehouseMap ? (maps.storeWarehouseMap.get(storeId) || '') : '',
|
||||
stock_id: stockId,
|
||||
item_code: stock.item_code || '',
|
||||
item_name: stock.item_name || '',
|
||||
barcode: barcodeSet ? [ ...barcodeSet ].join(',') : '',
|
||||
quantity: toNumber(item.quantity),
|
||||
before_quantity: toNumber(item.before_quantity),
|
||||
after_quantity: toNumber(item.after_quantity),
|
||||
before_locked_quantity: toNumber(item.before_locked_quantity),
|
||||
after_locked_quantity: toNumber(item.after_locked_quantity),
|
||||
batch_no: item.batch_no || '',
|
||||
inventory_type: item.inventory_type || '',
|
||||
order_type: item.order_type || '',
|
||||
order_no: item.order_no || '',
|
||||
order_id: orderId,
|
||||
receiving_id: toNumber(item.receiving_id),
|
||||
entry_order_id: entryOrderId,
|
||||
operator_name: item.operator_name || '',
|
||||
remark: item.remark || '',
|
||||
// 新增字段
|
||||
qimen_entry_order_code: isQimenEntry ? String(entryOrder.entry_order_code || '').trim() : '',
|
||||
platform_trade_no: String(orderDelivery.source_order_codes || item.order_no || '').trim(),
|
||||
entry_order_waybill_no: entryOrderId > 0 ? String(entryOrder.express_code || '').trim() : '',
|
||||
entry_order_express_company: entryOrderId > 0 ? String(entryOrder.logistics_name || '').trim() : '',
|
||||
waybill_no: String(generalWaybillNo || '').trim(),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
276
test/app/service/systems/storage_report_export.test.js
Normal file
276
test/app/service/systems/storage_report_export.test.js
Normal file
@ -0,0 +1,276 @@
|
||||
'use strict';
|
||||
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const ExcelJS = require('exceljs');
|
||||
const StorageReportService = require('../../../../app/service/systems/storage/report');
|
||||
|
||||
/**
|
||||
* 库存报表导出(storage.report.export)流式改造测试。
|
||||
*
|
||||
* 覆盖:
|
||||
* - 明细按主键 id 游标分批拉取(batchSize=5000),12000 行 = 3 批;
|
||||
* - 进度回调序列:5(命中总量)→ 分批推进 → 95 → 100(含 result 上传结果);
|
||||
* - Excel 流式生成:汇总/变更记录两个 sheet、表头、行数与抽样单元格;
|
||||
* - 汇总无数据时(明细 0 行)仍生成含表头的空文件。
|
||||
*/
|
||||
|
||||
function createDetailRows(total) {
|
||||
return Array.from({ length: total }, (_, index) => {
|
||||
const id = index + 1;
|
||||
return {
|
||||
id,
|
||||
created_at: new Date(Date.UTC(2026, 8, 1) + id * 1000).toISOString().replace('T', ' ')
|
||||
.slice(0, 19),
|
||||
transaction_type: index % 3 === 0 ? 'OUTBOUND' : 'INBOUND',
|
||||
store_id: 1000305,
|
||||
warehouse_id: 1000028,
|
||||
stock_id: id % 2 === 0 ? 101 : 102,
|
||||
space_id: 0,
|
||||
quantity: 2,
|
||||
before_quantity: 100 + id,
|
||||
after_quantity: 102 + id,
|
||||
before_locked_quantity: 0,
|
||||
after_locked_quantity: 0,
|
||||
batch_no: `B-${id}`,
|
||||
inventory_type: '',
|
||||
order_type: index % 3 === 0 ? 'OUTBOUND' : 'INBOUND',
|
||||
order_no: `NO-${id}`,
|
||||
order_id: 0,
|
||||
receiving_id: 0,
|
||||
entry_order_id: 0,
|
||||
operator_name: '测试操作员',
|
||||
remark: `备注${id}`,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function createSummaryList() {
|
||||
return [
|
||||
{
|
||||
start_time: '',
|
||||
end_time: '',
|
||||
store_id: 1000305,
|
||||
store_name: '测试货主',
|
||||
warehouse_id: 1000028,
|
||||
warehouse_name: '测试仓',
|
||||
stock_id: 101,
|
||||
item_code: 'SKU-A',
|
||||
item_name: '商品A',
|
||||
barcode: 'B1',
|
||||
begin_total: 100,
|
||||
inbound_total: 10,
|
||||
outbound_total: 5,
|
||||
end_total: 105,
|
||||
},
|
||||
{
|
||||
start_time: '',
|
||||
end_time: '',
|
||||
store_id: 1000305,
|
||||
store_name: '测试货主',
|
||||
warehouse_id: 1000028,
|
||||
warehouse_name: '测试仓',
|
||||
stock_id: 102,
|
||||
item_code: 'SKU-B',
|
||||
item_name: '商品B',
|
||||
barcode: 'B2',
|
||||
begin_total: 50,
|
||||
inbound_total: 8,
|
||||
outbound_total: 3,
|
||||
end_total: 55,
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
function createHarness({ detailRows = [], summaryList = [] } = {}) {
|
||||
const captured = {
|
||||
queries: [],
|
||||
upload: null,
|
||||
uploadedBuffer: null,
|
||||
};
|
||||
|
||||
const ctx = {
|
||||
app: {
|
||||
Sequelize: {
|
||||
QueryTypes: { SELECT: 'SELECT' },
|
||||
},
|
||||
logger: {
|
||||
warn() {},
|
||||
info() {},
|
||||
error() {},
|
||||
},
|
||||
},
|
||||
user: {
|
||||
store_id: 1000305,
|
||||
},
|
||||
logger: {
|
||||
warn() {},
|
||||
info() {},
|
||||
error() {},
|
||||
},
|
||||
service: {
|
||||
systems: {
|
||||
stockWarehouse: {
|
||||
async getStockDetails() {
|
||||
return [
|
||||
{ id: 101, item_code: 'SKU-A', item_name: '商品A', barcodes: [{ barcode: 'B1' }] },
|
||||
{ id: 102, item_code: 'SKU-B', item_name: '商品B', barcodes: [{ barcode: 'B2' }] },
|
||||
];
|
||||
},
|
||||
},
|
||||
excel: {
|
||||
async uploadExistingFile({ filePath, filename }) {
|
||||
captured.upload = { filePath, filename };
|
||||
captured.uploadedBuffer = fs.readFileSync(filePath);
|
||||
return {
|
||||
filename,
|
||||
fileKey: `exports/storage/report/1000305/20261008/${filename}`,
|
||||
size: captured.uploadedBuffer.length,
|
||||
url: `https://cdn.example.com/${filename}`,
|
||||
cdnUrl: `https://cdn.example.com/${filename}`,
|
||||
cosUrl: `https://cdn.example.com/${filename}`,
|
||||
};
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
model: {
|
||||
async query(sql, options = {}) {
|
||||
captured.queries.push({ sql, replacements: options.replacements });
|
||||
if (sql.includes('COUNT(*) AS cnt')) {
|
||||
return [{ cnt: detailRows.length }];
|
||||
}
|
||||
if (sql.includes('wms_stock_transaction')) {
|
||||
const cursorId = Number(options.replacements && options.replacements.cursorId) || 0;
|
||||
return detailRows
|
||||
.filter(row => row.id > cursorId)
|
||||
.slice(0, 5000)
|
||||
.map(row => ({ ...row }));
|
||||
}
|
||||
if (sql.includes('wms_entry_order') || sql.includes('wms_order_delivery') || sql.includes('wms_order_express')) {
|
||||
return [];
|
||||
}
|
||||
return [];
|
||||
},
|
||||
Store: {
|
||||
async findAll() {
|
||||
return [{ id: 1000305, company_name: '测试货主' }];
|
||||
},
|
||||
},
|
||||
LocationSpace: {
|
||||
async findAll() {
|
||||
return [];
|
||||
},
|
||||
},
|
||||
StoreWarehouse: {
|
||||
async findAll() {
|
||||
return [];
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const service = new StorageReportService(ctx);
|
||||
service.search = async () => ({
|
||||
code: 200,
|
||||
message: '查询成功',
|
||||
data: { list: summaryList },
|
||||
});
|
||||
|
||||
return { service, captured };
|
||||
}
|
||||
|
||||
describe('test/app/service/systems/storage_report_export.test.js', () => {
|
||||
it('明细 12000 行按游标 3 批流式导出,进度回调与 Excel 内容正确', async () => {
|
||||
const detailRows = createDetailRows(12000);
|
||||
const { service, captured } = createHarness({
|
||||
detailRows,
|
||||
summaryList: createSummaryList(),
|
||||
});
|
||||
const progressEvents = [];
|
||||
|
||||
const result = await service.export(
|
||||
null, null, false, [], null, '', 'store', null, '',
|
||||
async progress => {
|
||||
progressEvents.push({ ...progress });
|
||||
}
|
||||
);
|
||||
|
||||
// 上传结果
|
||||
assert.ok(result && result.filename, '应返回文件名');
|
||||
assert.ok(String(result.filename).startsWith('库存报表_'), '文件名应以 库存报表_ 开头');
|
||||
assert.ok(captured.upload, '应调用 uploadExistingFile');
|
||||
|
||||
// 进度回调序列
|
||||
assert.ok(progressEvents.length >= 4, `进度事件应不少于 4 次,实际 ${progressEvents.length}`);
|
||||
assert.equal(progressEvents[0].progress, 5);
|
||||
assert.equal(progressEvents[0].totalCount, 12000);
|
||||
assert.equal(progressEvents[0].currentCount, 0);
|
||||
const lastEvent = progressEvents[progressEvents.length - 1];
|
||||
assert.equal(lastEvent.progress, 100);
|
||||
assert.equal(lastEvent.totalCount, 12000);
|
||||
assert.ok(lastEvent.result && lastEvent.result.fileKey, '末次进度应携带上传结果');
|
||||
assert.ok(progressEvents.some(event => event.progress === 95 && event.currentCount === 12000), '应有 95 进度事件');
|
||||
|
||||
// 游标批次数:COUNT 1 次 + 明细 3 批
|
||||
const detailQueries = captured.queries.filter(query => query.sql.includes('wms_stock_transaction') && !query.sql.includes('COUNT(*)'));
|
||||
assert.equal(detailQueries.length, 3, '明细应分 3 批拉取');
|
||||
assert.equal(detailQueries[0].replacements.cursorId, 0, '第一批游标应为 0');
|
||||
|
||||
// 回读生成的 Excel
|
||||
const buffer = captured.uploadedBuffer;
|
||||
const workbook = new ExcelJS.Workbook();
|
||||
await workbook.xlsx.load(buffer);
|
||||
|
||||
assert.deepEqual(
|
||||
workbook.worksheets.map(sheet => sheet.name),
|
||||
[ '汇总', '变更记录' ]
|
||||
);
|
||||
|
||||
const summarySheet = workbook.getWorksheet('汇总');
|
||||
assert.equal(summarySheet.rowCount, 3, '汇总 sheet 应为表头+2 行');
|
||||
assert.equal(summarySheet.getCell('A1').value, '开始时间');
|
||||
|
||||
const detailSheet = workbook.getWorksheet('变更记录');
|
||||
assert.equal(detailSheet.rowCount, 12001, '变更记录 sheet 应为表头+12000 行');
|
||||
assert.equal(detailSheet.getCell('A1').value, '时间');
|
||||
assert.equal(detailSheet.getCell('A2').value, detailRows[0].created_at);
|
||||
assert.equal(detailSheet.getCell('I2').value, 2, '变动数量应为数值 2');
|
||||
assert.equal(detailSheet.getCell('B2').value, '出库', '第一条为 OUTBOUND 应显示 出库');
|
||||
assert.equal(detailSheet.getCell('F2').value, 'SKU-B', '商品编码来自富化映射');
|
||||
assert.equal(detailSheet.getCell('N2').value, 'B-1', '批次号');
|
||||
});
|
||||
|
||||
it('汇总无数据时明细为 0 行,仍生成含表头的文件并正常完成', async () => {
|
||||
const { service, captured } = createHarness({
|
||||
detailRows: [],
|
||||
summaryList: [],
|
||||
});
|
||||
const progressEvents = [];
|
||||
|
||||
const result = await service.export(
|
||||
null, null, false, [], null, '', 'store', null, '',
|
||||
async progress => {
|
||||
progressEvents.push({ ...progress });
|
||||
}
|
||||
);
|
||||
|
||||
assert.ok(result && result.filename, '应返回文件名');
|
||||
assert.ok(captured.upload, '应调用 uploadExistingFile');
|
||||
|
||||
const firstEvent = progressEvents[0];
|
||||
assert.equal(firstEvent.totalCount, 0, '明细总数应为 0');
|
||||
assert.equal(progressEvents[progressEvents.length - 1].progress, 100, '末次进度应为 100');
|
||||
|
||||
const buffer = captured.uploadedBuffer;
|
||||
const workbook = new ExcelJS.Workbook();
|
||||
await workbook.xlsx.load(buffer);
|
||||
assert.deepEqual(
|
||||
workbook.worksheets.map(sheet => sheet.name),
|
||||
[ '汇总', '变更记录' ]
|
||||
);
|
||||
const detailSheet = workbook.getWorksheet('变更记录');
|
||||
assert.equal(detailSheet.rowCount, 1, '空明细 sheet 应只有表头行');
|
||||
assert.equal(detailSheet.getCell('A1').value, '时间');
|
||||
});
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user