perf(afterSale): 售后导出流式写临时文件上传,避免内存 Excel 堆积,进度回调 await 化确保缓存进度不丢
This commit is contained in:
parent
b91c3f4124
commit
4259628396
@ -1,5 +1,10 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
const fs = require('fs');
|
||||||
|
const fsp = require('fs/promises');
|
||||||
|
const os = require('os');
|
||||||
|
const path = require('path');
|
||||||
|
const crypto = require('crypto');
|
||||||
const Service = require('egg').Service;
|
const Service = require('egg').Service;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -2015,13 +2020,13 @@ class AfterSaleService extends Service {
|
|||||||
async exportAfterSales(options = {}) {
|
async exportAfterSales(options = {}) {
|
||||||
const { where = {}, sort = {}, onProgress } = options;
|
const { where = {}, sort = {}, onProgress } = options;
|
||||||
|
|
||||||
const reportProgress = (status, progress, message, extra = {}) => {
|
const reportProgress = async (status, progress, message, extra = {}) => {
|
||||||
if (typeof onProgress === 'function') {
|
if (typeof onProgress === 'function') {
|
||||||
onProgress({ status, progress, message, ...extra });
|
await onProgress({ status, progress, message, ...extra });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
reportProgress('running', 5, '正在准备导出数据...');
|
await reportProgress('running', 5, '正在准备导出数据...');
|
||||||
|
|
||||||
// 先统计总数
|
// 先统计总数
|
||||||
const { count: totalCount } = await this.ctx.model.AfterSale.findAndCountAll({
|
const { count: totalCount } = await this.ctx.model.AfterSale.findAndCountAll({
|
||||||
@ -2029,7 +2034,7 @@ class AfterSaleService extends Service {
|
|||||||
raw: true,
|
raw: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
reportProgress('running', 10, `共 ${totalCount} 条记录,开始导出...`, { totalCount, currentCount: 0 });
|
await reportProgress('running', 10, `共 ${totalCount} 条记录,开始导出...`, { totalCount, currentCount: 0 });
|
||||||
|
|
||||||
// 分页查询并导出
|
// 分页查询并导出
|
||||||
const pageSize = 500;
|
const pageSize = 500;
|
||||||
@ -2042,7 +2047,7 @@ class AfterSaleService extends Service {
|
|||||||
allRows.push(...rows);
|
allRows.push(...rows);
|
||||||
|
|
||||||
const progress = 10 + Math.floor(((page / totalPages) * 80));
|
const progress = 10 + Math.floor(((page / totalPages) * 80));
|
||||||
reportProgress('running', progress, `正在导出... (${allRows.length}/${totalCount})`, {
|
await reportProgress('running', progress, `正在导出... (${allRows.length}/${totalCount})`, {
|
||||||
totalCount,
|
totalCount,
|
||||||
currentCount: allRows.length,
|
currentCount: allRows.length,
|
||||||
});
|
});
|
||||||
@ -2051,12 +2056,12 @@ class AfterSaleService extends Service {
|
|||||||
await new Promise(resolve => setImmediate(resolve));
|
await new Promise(resolve => setImmediate(resolve));
|
||||||
}
|
}
|
||||||
|
|
||||||
reportProgress('running', 90, '正在上传文件...', { totalCount, currentCount: allRows.length });
|
await reportProgress('running', 90, '正在上传文件...', { totalCount, currentCount: allRows.length });
|
||||||
|
|
||||||
// 生成 Excel 文件并上传 COS
|
// 生成 Excel 文件并上传 COS
|
||||||
const exportResult = await this._generateAndUploadExcel(allRows);
|
const exportResult = await this._generateAndUploadExcel(allRows);
|
||||||
|
|
||||||
reportProgress('success', 100, '导出完成', {
|
await reportProgress('success', 100, '导出完成', {
|
||||||
totalCount,
|
totalCount,
|
||||||
currentCount: allRows.length,
|
currentCount: allRows.length,
|
||||||
result: exportResult,
|
result: exportResult,
|
||||||
@ -2071,16 +2076,28 @@ class AfterSaleService extends Service {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成 Excel 并上传 COS,返回下载信息。
|
* 生成 Excel 并上传 COS,返回下载信息。
|
||||||
|
* 与其他导出(order/return_received 等)口径一致:流式写临时文件 → uploadFile → uploadSuccessNotify。
|
||||||
*
|
*
|
||||||
* @param {Array<Object>} rows 售后单数据行
|
* @param {Array<Object>} rows 售后单数据行
|
||||||
* @return {Promise<Object>} { url, fileKey, fileName }
|
* @return {Promise<Object>} { filename, fileKey, url, cdnUrl, cosUrl }
|
||||||
*/
|
*/
|
||||||
async _generateAndUploadExcel(rows = []) {
|
async _generateAndUploadExcel(rows = []) {
|
||||||
const { ctx } = this;
|
const { ctx } = this;
|
||||||
|
|
||||||
|
const tempDir = await fsp.mkdtemp(path.join(os.tmpdir(), 'wms-after-sale-export-'));
|
||||||
|
const now = new Date();
|
||||||
|
const dateToken = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}${String(now.getHours()).padStart(2, '0')}${String(now.getMinutes()).padStart(2, '0')}${String(now.getSeconds()).padStart(2, '0')}`;
|
||||||
|
const filename = `售后单导出_${dateToken}.xlsx`;
|
||||||
|
const filePath = path.join(tempDir, filename);
|
||||||
|
|
||||||
|
const ExcelJS = require('exceljs');
|
||||||
|
const workbook = new ExcelJS.stream.xlsx.WorkbookWriter({
|
||||||
|
filename: filePath,
|
||||||
|
useStyles: false,
|
||||||
|
useSharedStrings: false,
|
||||||
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const ExcelJS = require('exceljs');
|
|
||||||
const workbook = new ExcelJS.Workbook();
|
|
||||||
const worksheet = workbook.addWorksheet('售后单列表');
|
const worksheet = workbook.addWorksheet('售后单列表');
|
||||||
|
|
||||||
// 定义列
|
// 定义列
|
||||||
@ -2110,46 +2127,70 @@ class AfterSaleService extends Service {
|
|||||||
|
|
||||||
// 写入数据行
|
// 写入数据行
|
||||||
for (const row of rows) {
|
for (const row of rows) {
|
||||||
worksheet.addRow(row);
|
worksheet.addRow(row).commit();
|
||||||
|
}
|
||||||
|
worksheet.commit();
|
||||||
|
await workbook.commit();
|
||||||
|
|
||||||
|
// ---- 上传文件(与 order/return_received 导出口径一致)----
|
||||||
|
const uploadService = ctx?.service?.systems?.upload;
|
||||||
|
if (!uploadService || typeof uploadService.uploadFile !== 'function') {
|
||||||
|
throw new Error('上传服务不可用');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 生成 buffer
|
const stat = await fsp.stat(filePath);
|
||||||
const buffer = await workbook.xlsx.writeBuffer();
|
const randomToken = crypto.randomBytes(6).toString('hex');
|
||||||
|
const uploadKey = `exports/after_sale/${Date.now()}_${randomToken}.xlsx`;
|
||||||
|
const uploadStream = fs.createReadStream(filePath);
|
||||||
|
uploadStream.filename = filename;
|
||||||
|
uploadStream.mimeType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
|
||||||
|
uploadStream.fileSize = stat.size;
|
||||||
|
|
||||||
// 上传到 COS
|
const uploadResult = await uploadService.uploadFile(uploadStream, {
|
||||||
const now = new Date();
|
key: uploadKey,
|
||||||
const dateStr = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}`;
|
category: 'document',
|
||||||
const timestamp = Date.now();
|
mimetype: 'xls,xlsx,csv',
|
||||||
const fileName = `after_sale_export_${dateStr}_${timestamp}.xlsx`;
|
});
|
||||||
const fileKey = `export/after_sale/${fileName}`;
|
|
||||||
|
|
||||||
let url = '';
|
let fileRecord = null;
|
||||||
try {
|
if (typeof uploadService.uploadSuccessNotify === 'function') {
|
||||||
if (ctx.service.systems && ctx.service.systems.upload && typeof ctx.service.systems.upload.uploadToCos === 'function') {
|
fileRecord = await uploadService.uploadSuccessNotify({
|
||||||
const uploadResult = await ctx.service.systems.upload.uploadToCos({
|
url: uploadResult.fileName,
|
||||||
buffer,
|
fileKey: uploadResult.fileName,
|
||||||
fileKey,
|
fileName: filename,
|
||||||
fileName,
|
fileType: 'document',
|
||||||
mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
mimeType: uploadStream.mimeType,
|
||||||
});
|
fileSize: stat.size,
|
||||||
url = uploadResult && uploadResult.url ? uploadResult.url : '';
|
category: 'document',
|
||||||
}
|
businessType: 'afterSaleExport',
|
||||||
} catch (uploadError) {
|
description: `售后单导出,共 ${rows.length} 条记录`,
|
||||||
ctx.logger.warn(`[afterSale.export] COS 上传失败: ${uploadError.message}`);
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 优先取 uploadFile 返回的完整 URL,其次取 uploadSuccessNotify 落库记录,最后回退 COS key
|
||||||
|
const safeUploadResult = typeof uploadResult === 'object' && uploadResult !== null ? uploadResult : {};
|
||||||
|
const resolvedFileRecord = (fileRecord && (fileRecord.data || fileRecord)) || null;
|
||||||
|
const fileUrlFromRecord = resolvedFileRecord
|
||||||
|
? (resolvedFileRecord.file_url || resolvedFileRecord.fileUrl || resolvedFileRecord.url || '')
|
||||||
|
: '';
|
||||||
|
const uploadResultUrl = safeUploadResult.url || safeUploadResult.cdnUrl || safeUploadResult.cosUrl || '';
|
||||||
|
|
||||||
return {
|
return {
|
||||||
url,
|
filename,
|
||||||
fileKey,
|
fileKey: safeUploadResult.fileName || uploadKey,
|
||||||
fileName,
|
url: uploadResultUrl || fileUrlFromRecord || safeUploadResult.fileName || uploadKey,
|
||||||
|
cdnUrl: safeUploadResult.cdnUrl || safeUploadResult.url || '',
|
||||||
|
cosUrl: safeUploadResult.cosUrl || '',
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
ctx.logger.error(`[afterSale.export] 生成 Excel 失败: ${error.message}`);
|
const workbookStream = workbook.stream;
|
||||||
return {
|
if (workbookStream && typeof workbookStream.destroy === 'function') {
|
||||||
url: '',
|
workbookStream.destroy();
|
||||||
fileKey: '',
|
}
|
||||||
fileName: '',
|
ctx.logger.error(`[afterSale.export] 生成/上传 Excel 失败: ${error.message}`);
|
||||||
};
|
throw error;
|
||||||
|
} finally {
|
||||||
|
await fsp.rm(tempDir, { recursive: true, force: true });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user