From 425962839651f96b592a71b6da73048353880306 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Thu, 3 Sep 2026 17:03:52 +0800 Subject: [PATCH] =?UTF-8?q?perf(afterSale):=20=E5=94=AE=E5=90=8E=E5=AF=BC?= =?UTF-8?q?=E5=87=BA=E6=B5=81=E5=BC=8F=E5=86=99=E4=B8=B4=E6=97=B6=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=B8=8A=E4=BC=A0=EF=BC=8C=E9=81=BF=E5=85=8D=E5=86=85?= =?UTF-8?q?=E5=AD=98=20Excel=20=E5=A0=86=E7=A7=AF=EF=BC=8C=E8=BF=9B?= =?UTF-8?q?=E5=BA=A6=E5=9B=9E=E8=B0=83=20await=20=E5=8C=96=E7=A1=AE?= =?UTF-8?q?=E4=BF=9D=E7=BC=93=E5=AD=98=E8=BF=9B=E5=BA=A6=E4=B8=8D=E4=B8=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/service/systems/afterSale.js | 123 ++++++++++++++++++++----------- 1 file changed, 82 insertions(+), 41 deletions(-) diff --git a/app/service/systems/afterSale.js b/app/service/systems/afterSale.js index dd586fc..a8eb7c2 100644 --- a/app/service/systems/afterSale.js +++ b/app/service/systems/afterSale.js @@ -1,5 +1,10 @@ '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; /** @@ -2015,13 +2020,13 @@ class AfterSaleService extends Service { async exportAfterSales(options = {}) { const { where = {}, sort = {}, onProgress } = options; - const reportProgress = (status, progress, message, extra = {}) => { + const reportProgress = async (status, progress, message, extra = {}) => { 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({ @@ -2029,7 +2034,7 @@ class AfterSaleService extends Service { raw: true, }); - reportProgress('running', 10, `共 ${totalCount} 条记录,开始导出...`, { totalCount, currentCount: 0 }); + await reportProgress('running', 10, `共 ${totalCount} 条记录,开始导出...`, { totalCount, currentCount: 0 }); // 分页查询并导出 const pageSize = 500; @@ -2042,7 +2047,7 @@ class AfterSaleService extends Service { allRows.push(...rows); const progress = 10 + Math.floor(((page / totalPages) * 80)); - reportProgress('running', progress, `正在导出... (${allRows.length}/${totalCount})`, { + await reportProgress('running', progress, `正在导出... (${allRows.length}/${totalCount})`, { totalCount, currentCount: allRows.length, }); @@ -2051,12 +2056,12 @@ class AfterSaleService extends Service { await new Promise(resolve => setImmediate(resolve)); } - reportProgress('running', 90, '正在上传文件...', { totalCount, currentCount: allRows.length }); + await reportProgress('running', 90, '正在上传文件...', { totalCount, currentCount: allRows.length }); // 生成 Excel 文件并上传 COS const exportResult = await this._generateAndUploadExcel(allRows); - reportProgress('success', 100, '导出完成', { + await reportProgress('success', 100, '导出完成', { totalCount, currentCount: allRows.length, result: exportResult, @@ -2071,16 +2076,28 @@ class AfterSaleService extends Service { /** * 生成 Excel 并上传 COS,返回下载信息。 + * 与其他导出(order/return_received 等)口径一致:流式写临时文件 → uploadFile → uploadSuccessNotify。 * * @param {Array} rows 售后单数据行 - * @return {Promise} { url, fileKey, fileName } + * @return {Promise} { filename, fileKey, url, cdnUrl, cosUrl } */ async _generateAndUploadExcel(rows = []) { 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 { - const ExcelJS = require('exceljs'); - const workbook = new ExcelJS.Workbook(); const worksheet = workbook.addWorksheet('售后单列表'); // 定义列 @@ -2110,46 +2127,70 @@ class AfterSaleService extends Service { // 写入数据行 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 buffer = await workbook.xlsx.writeBuffer(); + const stat = await fsp.stat(filePath); + 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 now = new Date(); - const dateStr = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}`; - const timestamp = Date.now(); - const fileName = `after_sale_export_${dateStr}_${timestamp}.xlsx`; - const fileKey = `export/after_sale/${fileName}`; + const uploadResult = await uploadService.uploadFile(uploadStream, { + key: uploadKey, + category: 'document', + mimetype: 'xls,xlsx,csv', + }); - let url = ''; - try { - if (ctx.service.systems && ctx.service.systems.upload && typeof ctx.service.systems.upload.uploadToCos === 'function') { - const uploadResult = await ctx.service.systems.upload.uploadToCos({ - buffer, - fileKey, - fileName, - mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', - }); - url = uploadResult && uploadResult.url ? uploadResult.url : ''; - } - } catch (uploadError) { - ctx.logger.warn(`[afterSale.export] COS 上传失败: ${uploadError.message}`); + let fileRecord = null; + if (typeof uploadService.uploadSuccessNotify === 'function') { + fileRecord = await uploadService.uploadSuccessNotify({ + url: uploadResult.fileName, + fileKey: uploadResult.fileName, + fileName: filename, + fileType: 'document', + mimeType: uploadStream.mimeType, + fileSize: stat.size, + category: 'document', + businessType: 'afterSaleExport', + description: `售后单导出,共 ${rows.length} 条记录`, + }); } + // 优先取 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 { - url, - fileKey, - fileName, + filename, + fileKey: safeUploadResult.fileName || uploadKey, + url: uploadResultUrl || fileUrlFromRecord || safeUploadResult.fileName || uploadKey, + cdnUrl: safeUploadResult.cdnUrl || safeUploadResult.url || '', + cosUrl: safeUploadResult.cosUrl || '', }; } catch (error) { - ctx.logger.error(`[afterSale.export] 生成 Excel 失败: ${error.message}`); - return { - url: '', - fileKey: '', - fileName: '', - }; + const workbookStream = workbook.stream; + if (workbookStream && typeof workbookStream.destroy === 'function') { + workbookStream.destroy(); + } + ctx.logger.error(`[afterSale.export] 生成/上传 Excel 失败: ${error.message}`); + throw error; + } finally { + await fsp.rm(tempDir, { recursive: true, force: true }); } }