feat(order): 订单导出支持并发分页查询、多sheet续写、表头列白名单与上传大小上限
This commit is contained in:
parent
4ddcbe60a4
commit
d6d7b23f52
@ -32,6 +32,8 @@ class OrderIndexService extends OrderBaseService {
|
||||
* @param {string} [data.module_name] 订单模块名(下划线写法)。
|
||||
* @param {string} [data.export_mode] 导出模式:waybill(默认,按运单展开)或 item(按商品明细展开)。
|
||||
* @param {string} [data.exportMode] 导出模式(驼峰写法)。
|
||||
* @param {Array<string>} [data.export_columns] 导出表头字段白名单(中文表头数组),不传或空数组输出全部列;非法表头会报错。
|
||||
* @param {Array<string>} [data.exportColumns] 导出表头字段白名单(驼峰写法)。
|
||||
* @param {Object} [data.where] 查询条件。
|
||||
* @param {Object} [data.sort] 排序参数。
|
||||
* @return {Promise<Object>} 统一成功包装响应。
|
||||
@ -45,6 +47,8 @@ class OrderIndexService extends OrderBaseService {
|
||||
module_name: moduleNameSnake,
|
||||
export_mode: exportModeSnake,
|
||||
exportMode: exportModeCamel,
|
||||
export_columns: exportColumnsSnake,
|
||||
exportColumns: exportColumnsCamel,
|
||||
where,
|
||||
sort,
|
||||
} = requestData;
|
||||
@ -78,10 +82,14 @@ class OrderIndexService extends OrderBaseService {
|
||||
});
|
||||
|
||||
const exportMode = exportModeSnake || exportModeCamel || '';
|
||||
const exportColumns = Array.isArray(exportColumnsSnake)
|
||||
? exportColumnsSnake
|
||||
: (Array.isArray(exportColumnsCamel) ? exportColumnsCamel : []);
|
||||
const result = await this.service.systems.orderExport.exportOrders({
|
||||
tableType,
|
||||
moduleName: module,
|
||||
export_mode: exportMode,
|
||||
export_columns: exportColumns,
|
||||
where: this.isPlainObject(where) ? { ...where } : {},
|
||||
sort: this.isPlainObject(sort) ? { ...sort } : {},
|
||||
onProgress: async progress => {
|
||||
|
||||
@ -8,7 +8,10 @@ const path = require('node:path');
|
||||
const ExcelJS = require('exceljs');
|
||||
const OrderBaseService = require('../systemMethods/order/base');
|
||||
|
||||
const DEFAULT_EXPORT_PAGE_SIZE = 200;
|
||||
const DEFAULT_EXPORT_PAGE_SIZE = 1000;
|
||||
const ORDER_EXPORT_QUERY_CONCURRENCY = 2;
|
||||
const ORDER_EXPORT_MAX_ROWS_PER_SHEET = 1000000;
|
||||
const ORDER_EXPORT_UPLOAD_MAX_SIZE_BYTES = 1024 * 1024 * 1024; // 订单导出上传文件上限 1GB
|
||||
const ORDER_EXPORT_WORKSHEET_NAME = '订单导出';
|
||||
const ORDER_EXPORT_ALLOWED_TABLE_TYPES = new Set([ 'current', 'history', 'dispatch', 'outsourced' ]);
|
||||
const ORDER_EXPORT_MODULE_MAP = Object.freeze({
|
||||
@ -118,6 +121,14 @@ const ORDER_EXPORT_ITEM_COLUMNS = Object.freeze([
|
||||
{ header: '归档时间', key: 'archive_time', width: 20 },
|
||||
]);
|
||||
|
||||
/** 全部可导出表头字段(waybill 与 item 两种模式的并集,用于入参校验) */
|
||||
const ORDER_EXPORT_ALL_COLUMN_HEADERS = Object.freeze(
|
||||
new Set([
|
||||
...ORDER_EXPORT_COLUMNS.map(column => column.header),
|
||||
...ORDER_EXPORT_ITEM_COLUMNS.map(column => column.header),
|
||||
])
|
||||
);
|
||||
|
||||
/** 订单状态中文翻译映射 */
|
||||
const ORDER_STATUS_LABELS = Object.freeze({
|
||||
status: {
|
||||
@ -251,12 +262,46 @@ class OrderExportService extends OrderBaseService {
|
||||
/**
|
||||
* 获取订单导出列配置。
|
||||
* @param {string} [mode='waybill'] 导出模式:'waybill'(按运单展开)或 'item'(按商品明细展开)。
|
||||
* @param {Array<string>} [selectedHeaders=[]] 表头字段白名单(中文);不传/空数组返回全部列。
|
||||
* @return {Array<Object>} 导出列配置。
|
||||
*/
|
||||
getOrderExportColumns(mode = 'waybill') {
|
||||
getOrderExportColumns(mode = 'waybill', selectedHeaders = []) {
|
||||
const columns = mode === 'item' ? ORDER_EXPORT_ITEM_COLUMNS : ORDER_EXPORT_COLUMNS;
|
||||
const safeHeaders = Array.isArray(selectedHeaders) ? selectedHeaders : [];
|
||||
const headerSet = new Set(
|
||||
safeHeaders.map(header => String(header || '').trim()).filter(Boolean)
|
||||
);
|
||||
if (headerSet.size === 0) {
|
||||
return columns.map(column => ({ ...column }));
|
||||
}
|
||||
return columns
|
||||
.filter(column => headerSet.has(column.header))
|
||||
.map(column => ({ ...column }));
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验并规范导出表头字段入参(中文表头数组)。
|
||||
* 不传/空数组返回空数组(表示输出全部列);存在非法表头时抛错。
|
||||
* @param {Array<string>|*} rawColumns 原始入参。
|
||||
* @return {Array<string>} 去重后的表头数组。
|
||||
*/
|
||||
normalizeExportColumns(rawColumns) {
|
||||
if (!Array.isArray(rawColumns)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const selectedHeaders = [ ...new Set(
|
||||
rawColumns.map(header => String(header || '').trim()).filter(Boolean)
|
||||
) ];
|
||||
const invalidHeaders = selectedHeaders.filter(
|
||||
header => !ORDER_EXPORT_ALL_COLUMN_HEADERS.has(header)
|
||||
);
|
||||
if (invalidHeaders.length > 0) {
|
||||
throw new Error(`不支持的导出表头字段: ${invalidHeaders.join(', ')}`);
|
||||
}
|
||||
|
||||
return selectedHeaders;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成订单导出文件名。
|
||||
@ -330,7 +375,7 @@ class OrderExportService extends OrderBaseService {
|
||||
|
||||
/**
|
||||
* 映射订单导出数据行。
|
||||
* - waybill 模式:按运单号展开,每个运单一组。
|
||||
* - waybill 模式:按运单号展开,每个运单一组;商品种数/件数仅主单号行体现,子单号行置 0。
|
||||
* - item 模式:按商品明细展开,每个商品一行;运单信息汇总填充。
|
||||
* @param {Object} orderRow 订单行。
|
||||
* @param {Object} [context] 导出上下文,包含批量查询的辅助数据。
|
||||
@ -507,11 +552,16 @@ class OrderExportService extends OrderBaseService {
|
||||
}];
|
||||
}
|
||||
|
||||
return expressList.map(exp => ({
|
||||
return expressList.map(exp => {
|
||||
const isMainWaybill = Number(exp.business_key) === 1;
|
||||
return {
|
||||
...commonFields,
|
||||
items_detail: this.normalizeExportCellValue(itemsDetail),
|
||||
// 商品种数/件数为订单级数据,仅主单号行体现;子单号行置 0,避免与主单号重复
|
||||
items_count: isMainWaybill ? commonFields.items_count : 0,
|
||||
items_total: isMainWaybill ? commonFields.items_total : 0,
|
||||
waybill_code: exp.waybill_code || '',
|
||||
business_type: Number(exp.business_key) === 1 ? '主单号' : '子单号',
|
||||
business_type: isMainWaybill ? '主单号' : '子单号',
|
||||
logistics_interface_name: this.normalizeExportCellValue(exp.logistics_interface_name),
|
||||
package_material_id: exp.package_material_id != null ? Number(exp.package_material_id) : '',
|
||||
package_material_code: this.normalizeExportCellValue(
|
||||
@ -520,7 +570,8 @@ class OrderExportService extends OrderBaseService {
|
||||
package_material_name: this.normalizeExportCellValue(exp.package_material_name_snapshot),
|
||||
waybill_allocated_at: formatDateSafe(exp.create_at),
|
||||
weighing_weight: exp.weighing_weight != null ? Number(exp.weighing_weight) : '',
|
||||
}));
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@ -557,6 +608,7 @@ class OrderExportService extends OrderBaseService {
|
||||
key: uploadKey,
|
||||
category: 'document',
|
||||
mimetype: 'xls,xlsx,csv',
|
||||
maxSizeBytes: ORDER_EXPORT_UPLOAD_MAX_SIZE_BYTES,
|
||||
});
|
||||
|
||||
let fileRecord = null;
|
||||
@ -587,110 +639,31 @@ class OrderExportService extends OrderBaseService {
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出订单列表。
|
||||
* @param {Object} options 导出参数。
|
||||
* @param {string} [options.tableType] 表类型。
|
||||
* @param {string} [options.moduleName] 模块名。
|
||||
* @param {Object} [options.where] 查询条件。
|
||||
* @param {Object} [options.sort] 排序参数。
|
||||
* @param {Function} [options.onProgress] 进度回调。
|
||||
* @return {Promise<Object>} 上传结果与统计信息。
|
||||
* 构建单个导出页:分页查询订单 + 批量富化 + 映射导出行。
|
||||
* @param {Object} options 分页构建参数。
|
||||
* @param {number} options.page 页码(从 1 开始)。
|
||||
* @param {number} options.pageSize 分页大小。
|
||||
* @param {Object} options.queryWhere 查询条件。
|
||||
* @param {Array<Array<string>>} options.order 排序参数。
|
||||
* @param {Object} options.orderModel 订单头模型。
|
||||
* @param {Object} options.target 导出目标。
|
||||
* @param {number|string} options.warehouseId 仓库ID。
|
||||
* @param {string} options.mode 导出模式:'waybill' 或 'item'。
|
||||
* @return {Promise<{exportRows: Array<Object>, orderCount: number}>} 导出行数组与订单数。
|
||||
*/
|
||||
async exportOrders(options = {}) {
|
||||
async buildOrderExportPage(options = {}) {
|
||||
const safeOptions = this.isPlainObject(options) ? options : {};
|
||||
const target = this.resolveExportTarget({
|
||||
tableType: safeOptions.tableType,
|
||||
moduleName: safeOptions.moduleName,
|
||||
});
|
||||
const warehouseId = this.getWarehouseId();
|
||||
const requestData = {
|
||||
where: this.isPlainObject(safeOptions.where) ? { ...safeOptions.where } : {},
|
||||
sort: this.isPlainObject(safeOptions.sort) ? { ...safeOptions.sort } : {},
|
||||
};
|
||||
const progressHandler = typeof safeOptions.onProgress === 'function'
|
||||
? safeOptions.onProgress
|
||||
: async () => {};
|
||||
const { where } = this.prepareWhereContext(requestData, {
|
||||
specialFieldMap: {
|
||||
quickWhere: {
|
||||
bucket: 'quick',
|
||||
},
|
||||
},
|
||||
});
|
||||
const { where: sanitizedWhere, expressNumbers } = this.extractOrderListExpressVirtualFilter(where);
|
||||
const { where: whereAfterBuyerMessage, buyerMessageHas } = this.extractBuyerMessageHasVirtualFilter(sanitizedWhere);
|
||||
const { where: whereAfterStockQuery, stockQuery } = this.extractStockQueryVirtualFilter(whereAfterBuyerMessage);
|
||||
const { where: whereAfterWaveIndex, waveIndexCondition } = this.extractWaveIndexVirtualFilter(whereAfterStockQuery);
|
||||
const { where: whereAfterWaveScope, waveScope } = this.extractWaveScopeVirtualFilter(whereAfterWaveIndex);
|
||||
const mergedWhere = this.mergeWhereObjects(
|
||||
target.defaultWhereModuleName ? this.getDefaultWhere(target.defaultWhereModuleName) : {},
|
||||
whereAfterWaveScope
|
||||
);
|
||||
let queryWhere = this.buildWhere(mergedWhere, warehouseId, {
|
||||
specialFieldMap: {
|
||||
quickWhere: {
|
||||
bucket: 'quick',
|
||||
},
|
||||
},
|
||||
});
|
||||
const {
|
||||
page,
|
||||
pageSize,
|
||||
queryWhere,
|
||||
order,
|
||||
orderModel,
|
||||
target,
|
||||
warehouseId,
|
||||
mode,
|
||||
} = safeOptions;
|
||||
|
||||
if (expressNumbers.length > 0) {
|
||||
const orderIds = await this.findOrderIdsByExpressNumbers(expressNumbers, warehouseId);
|
||||
queryWhere = this.appendOrderIdsFilterToWhere(queryWhere, orderIds);
|
||||
}
|
||||
|
||||
if (buyerMessageHas !== null) {
|
||||
const buyerMessageCondition = this.buildBuyerMessageHasCondition(buyerMessageHas);
|
||||
queryWhere = this.appendConditionToWhere(queryWhere, buyerMessageCondition);
|
||||
}
|
||||
|
||||
if (stockQuery) {
|
||||
// 仅传 table_type 时推导出与当前表类型一致的模块名,保证 order_items 表类型正确
|
||||
const stockQueryModuleName = target.defaultWhereModuleName || target.sortModuleName || 'order';
|
||||
const stockOrderIds = await this.findOrderIdsByStockQuery(stockQuery, stockQueryModuleName, warehouseId);
|
||||
queryWhere = this.appendOrderIdsFilterToWhere(queryWhere, stockOrderIds);
|
||||
}
|
||||
|
||||
if (waveIndexCondition) {
|
||||
const waveIndexOrderIds = await this.findOrderIdsByWaveIndexCondition(waveIndexCondition, warehouseId);
|
||||
queryWhere = this.appendOrderIdsFilterToWhere(queryWhere, waveIndexOrderIds);
|
||||
}
|
||||
|
||||
if (waveScope) {
|
||||
const waveScopeCondition = await this.buildWaveScopeWhereCondition(waveScope, warehouseId);
|
||||
queryWhere = this.appendConditionToWhere(queryWhere, waveScopeCondition);
|
||||
}
|
||||
|
||||
const orderModel = await this.getOrderModelByTableType(target.tableType, warehouseId);
|
||||
const pageSize = this.getExportPageSize();
|
||||
const order = this.buildListOrder(requestData.sort, target.sortModuleName);
|
||||
const totalCount = await orderModel.count({ where: queryWhere });
|
||||
const tempDir = await fsp.mkdtemp(path.join(os.tmpdir(), 'wms-order-export-'));
|
||||
const filename = this.buildOrderExportFilename(target);
|
||||
const filePath = path.join(tempDir, filename);
|
||||
const workbook = new ExcelJS.stream.xlsx.WorkbookWriter({
|
||||
filename: filePath,
|
||||
useStyles: false,
|
||||
useSharedStrings: false,
|
||||
});
|
||||
const worksheet = workbook.addWorksheet(ORDER_EXPORT_WORKSHEET_NAME);
|
||||
const exportMode = String(safeOptions.export_mode || '').trim() || 'waybill';
|
||||
const mode = exportMode === 'item' ? 'item' : 'waybill';
|
||||
worksheet.columns = this.getOrderExportColumns(mode);
|
||||
|
||||
let currentCount = 0;
|
||||
let page = 1;
|
||||
|
||||
try {
|
||||
await progressHandler({
|
||||
status: 'running',
|
||||
progress: totalCount > 0 ? 5 : 90,
|
||||
currentCount,
|
||||
totalCount,
|
||||
message: totalCount > 0 ? `已命中 ${totalCount} 条订单,开始导出` : '未命中数据,开始生成空文件',
|
||||
});
|
||||
|
||||
while (currentCount < totalCount) {
|
||||
const rows = await orderModel.findAll({
|
||||
where: queryWhere,
|
||||
limit: pageSize,
|
||||
@ -700,7 +673,7 @@ class OrderExportService extends OrderBaseService {
|
||||
});
|
||||
|
||||
if (!Array.isArray(rows) || rows.length === 0) {
|
||||
break;
|
||||
return { exportRows: [], orderCount: 0 };
|
||||
}
|
||||
|
||||
const normalizedRows = rows.map(row =>
|
||||
@ -868,27 +841,268 @@ class OrderExportService extends OrderBaseService {
|
||||
|
||||
const exportContext = { itemsMap, storeRemarkMap, stockNameMap, barcodeMap, imageUrlMap, expressMap, packageMaterialMap };
|
||||
|
||||
const exportRows = [];
|
||||
for (const row of rowsWithWaveIndex) {
|
||||
const exportRows = this.mapOrderExportRow(row, exportContext, mode);
|
||||
const rowExportRows = this.mapOrderExportRow(row, exportContext, mode);
|
||||
exportRows.push(...rowExportRows);
|
||||
}
|
||||
|
||||
return { exportRows, orderCount: rowsWithWaveIndex.length };
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出订单列表。
|
||||
* 查询采用 ORDER_EXPORT_QUERY_CONCURRENCY 个 worker 并发分页查询,
|
||||
* 先查询完成的页先入 FIFO 队列,由单一串行 writer 按入队顺序写入文件(行序按完成顺序,非列表顺序)。
|
||||
* 单个 sheet 最多写入 ORDER_EXPORT_MAX_ROWS_PER_SHEET 行数据,超出后自动新建下一个 sheet 续写。
|
||||
* @param {Object} options 导出参数。
|
||||
* @param {string} [options.tableType] 表类型。
|
||||
* @param {string} [options.moduleName] 模块名。
|
||||
* @param {Object} [options.where] 查询条件。
|
||||
* @param {Object} [options.sort] 排序参数。
|
||||
* @param {string} [options.export_mode] 导出模式:waybill 或 item。
|
||||
* @param {Array<string>} [options.export_columns] 导出表头字段白名单(中文表头数组),不传输出全部列。
|
||||
* @param {Array<string>} [options.exportColumns] 导出表头字段白名单(驼峰写法)。
|
||||
* @param {Function} [options.onProgress] 进度回调。
|
||||
* @return {Promise<Object>} 上传结果与统计信息。
|
||||
*/
|
||||
async exportOrders(options = {}) {
|
||||
const safeOptions = this.isPlainObject(options) ? options : {};
|
||||
const target = this.resolveExportTarget({
|
||||
tableType: safeOptions.tableType,
|
||||
moduleName: safeOptions.moduleName,
|
||||
});
|
||||
const warehouseId = this.getWarehouseId();
|
||||
const requestData = {
|
||||
where: this.isPlainObject(safeOptions.where) ? { ...safeOptions.where } : {},
|
||||
sort: this.isPlainObject(safeOptions.sort) ? { ...safeOptions.sort } : {},
|
||||
};
|
||||
const progressHandler = typeof safeOptions.onProgress === 'function'
|
||||
? safeOptions.onProgress
|
||||
: async () => {};
|
||||
const { where } = this.prepareWhereContext(requestData, {
|
||||
specialFieldMap: {
|
||||
quickWhere: {
|
||||
bucket: 'quick',
|
||||
},
|
||||
},
|
||||
});
|
||||
const { where: sanitizedWhere, expressNumbers } = this.extractOrderListExpressVirtualFilter(where);
|
||||
const { where: whereAfterBuyerMessage, buyerMessageHas } = this.extractBuyerMessageHasVirtualFilter(sanitizedWhere);
|
||||
const { where: whereAfterStockQuery, stockQuery } = this.extractStockQueryVirtualFilter(whereAfterBuyerMessage);
|
||||
const { where: whereAfterWaveIndex, waveIndexCondition } = this.extractWaveIndexVirtualFilter(whereAfterStockQuery);
|
||||
const { where: whereAfterWaveScope, waveScope } = this.extractWaveScopeVirtualFilter(whereAfterWaveIndex);
|
||||
const mergedWhere = this.mergeWhereObjects(
|
||||
target.defaultWhereModuleName ? this.getDefaultWhere(target.defaultWhereModuleName) : {},
|
||||
whereAfterWaveScope
|
||||
);
|
||||
let queryWhere = this.buildWhere(mergedWhere, warehouseId, {
|
||||
specialFieldMap: {
|
||||
quickWhere: {
|
||||
bucket: 'quick',
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (expressNumbers.length > 0) {
|
||||
const orderIds = await this.findOrderIdsByExpressNumbers(expressNumbers, warehouseId);
|
||||
queryWhere = this.appendOrderIdsFilterToWhere(queryWhere, orderIds);
|
||||
}
|
||||
|
||||
if (buyerMessageHas !== null) {
|
||||
const buyerMessageCondition = this.buildBuyerMessageHasCondition(buyerMessageHas);
|
||||
queryWhere = this.appendConditionToWhere(queryWhere, buyerMessageCondition);
|
||||
}
|
||||
|
||||
if (stockQuery) {
|
||||
// 仅传 table_type 时推导出与当前表类型一致的模块名,保证 order_items 表类型正确
|
||||
const stockQueryModuleName = target.defaultWhereModuleName || target.sortModuleName || 'order';
|
||||
const stockOrderIds = await this.findOrderIdsByStockQuery(stockQuery, stockQueryModuleName, warehouseId);
|
||||
queryWhere = this.appendOrderIdsFilterToWhere(queryWhere, stockOrderIds);
|
||||
}
|
||||
|
||||
if (waveIndexCondition) {
|
||||
const waveIndexOrderIds = await this.findOrderIdsByWaveIndexCondition(waveIndexCondition, warehouseId);
|
||||
queryWhere = this.appendOrderIdsFilterToWhere(queryWhere, waveIndexOrderIds);
|
||||
}
|
||||
|
||||
if (waveScope) {
|
||||
const waveScopeCondition = await this.buildWaveScopeWhereCondition(waveScope, warehouseId);
|
||||
queryWhere = this.appendConditionToWhere(queryWhere, waveScopeCondition);
|
||||
}
|
||||
|
||||
const orderModel = await this.getOrderModelByTableType(target.tableType, warehouseId);
|
||||
const pageSize = this.getExportPageSize();
|
||||
const order = this.buildListOrder(requestData.sort, target.sortModuleName);
|
||||
const totalCount = await orderModel.count({ where: queryWhere });
|
||||
const tempDir = await fsp.mkdtemp(path.join(os.tmpdir(), 'wms-order-export-'));
|
||||
const filename = this.buildOrderExportFilename(target);
|
||||
const filePath = path.join(tempDir, filename);
|
||||
const workbook = new ExcelJS.stream.xlsx.WorkbookWriter({
|
||||
filename: filePath,
|
||||
useStyles: false,
|
||||
useSharedStrings: false,
|
||||
});
|
||||
const worksheet = workbook.addWorksheet(ORDER_EXPORT_WORKSHEET_NAME);
|
||||
const exportMode = String(safeOptions.export_mode || '').trim() || 'waybill';
|
||||
const mode = exportMode === 'item' ? 'item' : 'waybill';
|
||||
const selectedHeaders = this.normalizeExportColumns(
|
||||
safeOptions.export_columns ?? safeOptions.exportColumns
|
||||
);
|
||||
const resolvedColumns = this.getOrderExportColumns(mode, selectedHeaders);
|
||||
worksheet.columns = resolvedColumns;
|
||||
|
||||
let currentCount = 0;
|
||||
// 分 sheet 写入:每个 sheet 最多 ORDER_EXPORT_MAX_ROWS_PER_SHEET 行数据,
|
||||
// 超出后提交当前 sheet 并新建下一个 sheet(名称:订单导出、订单导出2、…),新 sheet 同样带表头。
|
||||
let currentWorksheet = worksheet;
|
||||
let currentSheetIndex = 1;
|
||||
let currentSheetRows = 0;
|
||||
|
||||
const ensureSheetCapacity = () => {
|
||||
if (currentSheetRows < ORDER_EXPORT_MAX_ROWS_PER_SHEET) {
|
||||
return;
|
||||
}
|
||||
currentWorksheet.commit();
|
||||
currentSheetIndex += 1;
|
||||
currentWorksheet = workbook.addWorksheet(`${ORDER_EXPORT_WORKSHEET_NAME}${currentSheetIndex}`);
|
||||
currentWorksheet.columns = resolvedColumns;
|
||||
currentSheetRows = 0;
|
||||
};
|
||||
|
||||
const writeExportRow = exportRow => {
|
||||
ensureSheetCapacity();
|
||||
currentWorksheet.addRow(exportRow).commit();
|
||||
currentSheetRows += 1;
|
||||
};
|
||||
|
||||
try {
|
||||
await progressHandler({
|
||||
status: 'running',
|
||||
progress: totalCount > 0 ? 5 : 90,
|
||||
currentCount,
|
||||
totalCount,
|
||||
message: totalCount > 0 ? `已命中 ${totalCount} 条订单,开始导出` : '未命中数据,开始生成空文件',
|
||||
});
|
||||
|
||||
const totalPages = totalCount > 0 ? Math.ceil(totalCount / pageSize) : 0;
|
||||
|
||||
// FIFO 队列:各 worker 查询完成的页按完成顺序入队,由单一 writer 串行落盘
|
||||
const exportPageQueue = [];
|
||||
let exportQueueHead = 0;
|
||||
let pagesCompleted = 0;
|
||||
let queueDone = false;
|
||||
let queueWake = null;
|
||||
let firstError = null;
|
||||
|
||||
const wakeWriter = () => {
|
||||
if (queueWake) {
|
||||
const resolveWake = queueWake;
|
||||
queueWake = null;
|
||||
resolveWake();
|
||||
}
|
||||
};
|
||||
const enqueueExportRows = exportRows => {
|
||||
if (Array.isArray(exportRows) && exportRows.length > 0) {
|
||||
exportPageQueue.push(exportRows);
|
||||
wakeWriter();
|
||||
}
|
||||
};
|
||||
const finishExportQueue = () => {
|
||||
queueDone = true;
|
||||
wakeWriter();
|
||||
};
|
||||
const waitForQueueWake = () => new Promise(resolve => {
|
||||
queueWake = resolve;
|
||||
});
|
||||
|
||||
// 串行 writer:按入队顺序写入 ExcelJS 流,避免并发 commit 导致的行序竞态
|
||||
const writerLoop = (async () => {
|
||||
try {
|
||||
for (;;) {
|
||||
while (exportQueueHead < exportPageQueue.length) {
|
||||
const exportRows = exportPageQueue[exportQueueHead];
|
||||
exportQueueHead += 1;
|
||||
for (const exportRow of exportRows) {
|
||||
worksheet.addRow(exportRow).commit();
|
||||
writeExportRow(exportRow);
|
||||
}
|
||||
}
|
||||
if (exportQueueHead > 0) {
|
||||
if (exportQueueHead >= exportPageQueue.length) {
|
||||
exportPageQueue.length = 0;
|
||||
exportQueueHead = 0;
|
||||
} else {
|
||||
exportPageQueue.splice(0, exportQueueHead);
|
||||
exportQueueHead = 0;
|
||||
}
|
||||
}
|
||||
if (queueDone || firstError) {
|
||||
break;
|
||||
}
|
||||
await waitForQueueWake();
|
||||
}
|
||||
} catch (err) {
|
||||
if (!firstError) {
|
||||
firstError = err;
|
||||
}
|
||||
queueDone = true;
|
||||
wakeWriter();
|
||||
}
|
||||
})();
|
||||
|
||||
currentCount += rowsWithWaveIndex.length;
|
||||
|
||||
// 有界并发查询:限制 2 个 worker,先查询完成的页先入队
|
||||
let nextPage = 1;
|
||||
const runWorker = async () => {
|
||||
while (!firstError) {
|
||||
const page = nextPage;
|
||||
nextPage += 1;
|
||||
if (page > totalPages) {
|
||||
break;
|
||||
}
|
||||
try {
|
||||
const pageResult = await this.buildOrderExportPage({
|
||||
page,
|
||||
pageSize,
|
||||
queryWhere,
|
||||
order,
|
||||
orderModel,
|
||||
target,
|
||||
warehouseId,
|
||||
mode,
|
||||
});
|
||||
enqueueExportRows(pageResult.exportRows);
|
||||
currentCount += pageResult.orderCount;
|
||||
pagesCompleted += 1;
|
||||
await progressHandler({
|
||||
status: 'running',
|
||||
progress: Math.min(99, Math.max(5, Math.round((currentCount / Math.max(totalCount, 1)) * 90))),
|
||||
currentCount,
|
||||
totalCount,
|
||||
message: `正在导出第 ${page} 页,共 ${Math.ceil(totalCount / pageSize) || 1} 页`,
|
||||
message: `已完成 ${currentCount}/${totalCount} 条订单查询(${pagesCompleted}/${totalPages} 页)`,
|
||||
});
|
||||
} catch (err) {
|
||||
if (!firstError) {
|
||||
firstError = err;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
page += 1;
|
||||
const workers = Array.from(
|
||||
{ length: Math.min(ORDER_EXPORT_QUERY_CONCURRENCY, totalPages) },
|
||||
() => runWorker()
|
||||
);
|
||||
|
||||
await Promise.all(workers);
|
||||
finishExportQueue();
|
||||
await writerLoop;
|
||||
|
||||
if (firstError) {
|
||||
throw firstError;
|
||||
}
|
||||
|
||||
worksheet.commit();
|
||||
currentWorksheet.commit();
|
||||
await workbook.commit();
|
||||
|
||||
const uploadResult = await this.uploadOrderExportFile({
|
||||
@ -913,6 +1127,7 @@ class OrderExportService extends OrderBaseService {
|
||||
module_name: target.defaultWhereModuleName || '',
|
||||
current_count: currentCount,
|
||||
total_count: totalCount,
|
||||
sheet_count: currentSheetIndex,
|
||||
};
|
||||
} catch (error) {
|
||||
const workbookStream = workbook.stream;
|
||||
|
||||
@ -220,6 +220,21 @@ class UploadService extends Service {
|
||||
policyHash,
|
||||
};
|
||||
}
|
||||
/**
|
||||
* 解析单次上传的文件大小上限。
|
||||
* 优先使用调用方传入的 options.maxSizeBytes,否则回退到 cos.maxsize 配置。
|
||||
* @param {Object} options 上传选项。
|
||||
* @return {number} 文件大小上限(字节)。
|
||||
*/
|
||||
resolveUploadMaxSizeBytes(options = {}) {
|
||||
const requested = options && options.maxSizeBytes;
|
||||
if (typeof requested === 'number' && Number.isFinite(requested) && requested > 0) {
|
||||
return requested;
|
||||
}
|
||||
|
||||
const { maxsize } = this.getCosConfig();
|
||||
return this.parseSizeToBytes(maxsize);
|
||||
}
|
||||
/**
|
||||
* 验证文件信息
|
||||
* @param {Object} root0 文件信息对象
|
||||
@ -229,7 +244,7 @@ class UploadService extends Service {
|
||||
validateFileInfo({ fileName, fileSize }) {
|
||||
const { maxsize, mimetype } = this.getCosConfig();
|
||||
|
||||
// 验证文件大小
|
||||
// 验证文件大小(使用 cos.maxsize 配置)
|
||||
const maxSizeBytes = this.parseSizeToBytes(maxsize);
|
||||
if (fileSize > maxSizeBytes) {
|
||||
throw new Error(`文件大小不能超过 ${this.formatFileSize(maxSizeBytes)}`);
|
||||
@ -394,10 +409,11 @@ class UploadService extends Service {
|
||||
* @param {Object} options 上传选项
|
||||
*/
|
||||
validateFile(fileStream, options = {}) {
|
||||
const { maxsize, mimetype } = this.getCosConfig();
|
||||
const { mimetype } = this.getCosConfig();
|
||||
const { filename, fileSize } = fileStream;
|
||||
|
||||
const maxSizeBytes = this.parseSizeToBytes(maxsize);
|
||||
// 验证文件大小:单次上传可通过 options.maxSizeBytes 覆盖上限,否则使用 cos.maxsize 配置
|
||||
const maxSizeBytes = this.resolveUploadMaxSizeBytes(options);
|
||||
if (fileSize > maxSizeBytes) {
|
||||
throw new Error(`文件大小不能超过 ${this.formatFileSize(maxSizeBytes)}`);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user