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,11 +262,45 @@ 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;
|
||||
return columns.map(column => ({ ...column }));
|
||||
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,20 +552,26 @@ class OrderExportService extends OrderBaseService {
|
||||
}];
|
||||
}
|
||||
|
||||
return expressList.map(exp => ({
|
||||
...commonFields,
|
||||
items_detail: this.normalizeExportCellValue(itemsDetail),
|
||||
waybill_code: exp.waybill_code || '',
|
||||
business_type: Number(exp.business_key) === 1 ? '主单号' : '子单号',
|
||||
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(
|
||||
exp.package_material_id != null ? (safePackageMaterialMap.get(Number(exp.package_material_id)) || '') : ''
|
||||
),
|
||||
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) : '',
|
||||
}));
|
||||
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: 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(
|
||||
exp.package_material_id != null ? (safePackageMaterialMap.get(Number(exp.package_material_id)) || '') : ''
|
||||
),
|
||||
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;
|
||||
@ -586,13 +638,231 @@ class OrderExportService extends OrderBaseService {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建单个导出页:分页查询订单 + 批量富化 + 映射导出行。
|
||||
* @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 buildOrderExportPage(options = {}) {
|
||||
const safeOptions = this.isPlainObject(options) ? options : {};
|
||||
const {
|
||||
page,
|
||||
pageSize,
|
||||
queryWhere,
|
||||
order,
|
||||
orderModel,
|
||||
target,
|
||||
warehouseId,
|
||||
mode,
|
||||
} = safeOptions;
|
||||
|
||||
const rows = await orderModel.findAll({
|
||||
where: queryWhere,
|
||||
limit: pageSize,
|
||||
offset: (page - 1) * pageSize,
|
||||
order,
|
||||
raw: true,
|
||||
});
|
||||
|
||||
if (!Array.isArray(rows) || rows.length === 0) {
|
||||
return { exportRows: [], orderCount: 0 };
|
||||
}
|
||||
|
||||
const normalizedRows = rows.map(row =>
|
||||
this.normalizeOrderListItem(this.toPlainRow(row))
|
||||
);
|
||||
const rowsWithWaveIndex = await this.attachOrderWaveIndex(normalizedRows);
|
||||
|
||||
const orderIds = rowsWithWaveIndex.map(r => Number(r.id || 0)).filter(id => id > 0);
|
||||
|
||||
// 批量查询商品明细(wms_order_items)
|
||||
const itemsMap = new Map();
|
||||
if (orderIds.length > 0) {
|
||||
try {
|
||||
const itemsModel = await this.getOrderItemsModelByTableType(target.tableType, warehouseId);
|
||||
const itemsRows = await itemsModel.findAll({
|
||||
where: { order_id: { [this.app.Sequelize.Op.in]: orderIds } },
|
||||
attributes: [ 'order_id', 'item_name', 'plan_qty', 'stock_id' ],
|
||||
order: [[ 'id', 'ASC' ]],
|
||||
raw: true,
|
||||
});
|
||||
for (const item of (Array.isArray(itemsRows) ? itemsRows : [])) {
|
||||
const oid = Number(item.order_id || 0);
|
||||
if (oid > 0) {
|
||||
if (!itemsMap.has(oid)) {
|
||||
itemsMap.set(oid, []);
|
||||
}
|
||||
itemsMap.get(oid).push(item);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
this.app.logger.warn(`[order_export] 批量查询订单商品明细失败: ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// 批量查询仓库商品名称、条码与图片(复用 getStockDetails 缓存)
|
||||
const stockNameMap = new Map();
|
||||
const barcodeMap = new Map();
|
||||
const imageUrlMap = new Map();
|
||||
if (itemsMap.size > 0 && this.ctx && this.ctx.model && this.ctx.model.StockWarehouse) {
|
||||
try {
|
||||
const stockIds = [
|
||||
...new Set(
|
||||
[ ...itemsMap.values() ]
|
||||
.flat()
|
||||
.map(item => Number(item.stock_id || 0))
|
||||
.filter(id => id > 0)
|
||||
),
|
||||
];
|
||||
if (stockIds.length > 0) {
|
||||
const stockDetails = await this.ctx.service.systems.stockWarehouse.getStockDetails(
|
||||
stockIds,
|
||||
{ warehouseId }
|
||||
);
|
||||
for (const detail of stockDetails) {
|
||||
const sid = Number(detail.id || 0);
|
||||
if (sid > 0) {
|
||||
stockNameMap.set(sid, String(detail.item_name || '').trim());
|
||||
// 从 getStockDetails 的 barcodes 中提取条码并转为逗号拼接
|
||||
const barcodesRaw = Array.isArray(detail.barcodes)
|
||||
? detail.barcodes.map(bc => String(bc.barcode || '').trim()).filter(Boolean)
|
||||
: [];
|
||||
if (barcodesRaw.length > 0) {
|
||||
barcodeMap.set(sid, [ ...new Set(barcodesRaw) ].join(', '));
|
||||
}
|
||||
// 图片字段(有图片标记但实际 URL 需要额外查询)
|
||||
if (detail.is_images === 'Y') {
|
||||
imageUrlMap.set(sid, '');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
this.app.logger.warn(`[order_export] 批量查询商品详情失败: ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// 批量查询货主备注(wms_store_warehouse.store_remask)
|
||||
const storeRemarkMap = new Map();
|
||||
if (orderIds.length > 0 && this.ctx && this.ctx.model && this.ctx.model.StoreWarehouse) {
|
||||
try {
|
||||
const storeIds = [
|
||||
...new Set(
|
||||
rowsWithWaveIndex
|
||||
.map(r => Number(r.store_id || 0))
|
||||
.filter(id => id > 0)
|
||||
),
|
||||
];
|
||||
if (storeIds.length > 0) {
|
||||
const swRows = await this.ctx.model.StoreWarehouse.findAll({
|
||||
where: {
|
||||
store_id: { [this.app.Sequelize.Op.in]: storeIds },
|
||||
warehouse_id: warehouseId,
|
||||
},
|
||||
attributes: [ 'store_id', 'store_remask' ],
|
||||
raw: true,
|
||||
});
|
||||
for (const sw of (Array.isArray(swRows) ? swRows : [])) {
|
||||
const sid = Number(sw.store_id || 0);
|
||||
if (sid > 0 && !storeRemarkMap.has(sid)) {
|
||||
storeRemarkMap.set(sid, String(sw.store_remask || '').trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
this.app.logger.warn(`[order_export] 批量查询货主备注失败: ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// 批量查询运单数据(wms_order_express),按订单ID分组
|
||||
const expressMap = new Map();
|
||||
if (orderIds.length > 0 && this.ctx && this.ctx.model && this.ctx.model.OrderExpress) {
|
||||
try {
|
||||
const expressRows = await this.ctx.model.OrderExpress.findAll({
|
||||
where: {
|
||||
order_id: { [this.app.Sequelize.Op.in]: orderIds },
|
||||
warehouse_id: warehouseId,
|
||||
},
|
||||
attributes: [ 'id', 'order_id', 'waybill_code', 'business_key', 'logistics_interface_name', 'package_material_id', 'package_material_name_snapshot', 'create_at', 'weighing_weight' ],
|
||||
order: [[ 'id', 'ASC' ]],
|
||||
raw: true,
|
||||
});
|
||||
for (const exp of (Array.isArray(expressRows) ? expressRows : [])) {
|
||||
const oid = Number(exp.order_id || 0);
|
||||
if (oid > 0) {
|
||||
if (!expressMap.has(oid)) {
|
||||
expressMap.set(oid, []);
|
||||
}
|
||||
expressMap.get(oid).push(exp);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
this.app.logger.warn(`[order_export] 批量查询运单数据失败: ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// 批量查询包材编码(wms_package_material),按 package_material_id 分组
|
||||
const packageMaterialMap = new Map();
|
||||
if (expressMap.size > 0 && this.ctx && this.ctx.model && this.ctx.model.PackageMaterial) {
|
||||
try {
|
||||
const materialIds = [
|
||||
...new Set(
|
||||
[ ...expressMap.values() ]
|
||||
.flat()
|
||||
.map(exp => Number(exp.package_material_id || 0))
|
||||
.filter(id => id > 0)
|
||||
),
|
||||
];
|
||||
if (materialIds.length > 0) {
|
||||
const pmRows = await this.ctx.model.PackageMaterial.findAll({
|
||||
where: { id: { [this.app.Sequelize.Op.in]: materialIds } },
|
||||
attributes: [ 'id', 'material_code' ],
|
||||
raw: true,
|
||||
});
|
||||
for (const pm of (Array.isArray(pmRows) ? pmRows : [])) {
|
||||
const pid = Number(pm.id || 0);
|
||||
if (pid > 0 && !packageMaterialMap.has(pid)) {
|
||||
packageMaterialMap.set(pid, String(pm.material_code || '').trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
this.app.logger.warn(`[order_export] 批量查询包材编码失败: ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
const exportContext = { itemsMap, storeRemarkMap, stockNameMap, barcodeMap, imageUrlMap, expressMap, packageMaterialMap };
|
||||
|
||||
const exportRows = [];
|
||||
for (const row of rowsWithWaveIndex) {
|
||||
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>} 上传结果与统计信息。
|
||||
*/
|
||||
@ -676,10 +946,35 @@ class OrderExportService extends OrderBaseService {
|
||||
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);
|
||||
const selectedHeaders = this.normalizeExportColumns(
|
||||
safeOptions.export_columns ?? safeOptions.exportColumns
|
||||
);
|
||||
const resolvedColumns = this.getOrderExportColumns(mode, selectedHeaders);
|
||||
worksheet.columns = resolvedColumns;
|
||||
|
||||
let currentCount = 0;
|
||||
let page = 1;
|
||||
// 分 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({
|
||||
@ -690,205 +985,124 @@ class OrderExportService extends OrderBaseService {
|
||||
message: totalCount > 0 ? `已命中 ${totalCount} 条订单,开始导出` : '未命中数据,开始生成空文件',
|
||||
});
|
||||
|
||||
while (currentCount < totalCount) {
|
||||
const rows = await orderModel.findAll({
|
||||
where: queryWhere,
|
||||
limit: pageSize,
|
||||
offset: (page - 1) * pageSize,
|
||||
order,
|
||||
raw: true,
|
||||
});
|
||||
const totalPages = totalCount > 0 ? Math.ceil(totalCount / pageSize) : 0;
|
||||
|
||||
if (!Array.isArray(rows) || rows.length === 0) {
|
||||
break;
|
||||
// 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;
|
||||
});
|
||||
|
||||
const normalizedRows = rows.map(row =>
|
||||
this.normalizeOrderListItem(this.toPlainRow(row))
|
||||
);
|
||||
const rowsWithWaveIndex = await this.attachOrderWaveIndex(normalizedRows);
|
||||
// 串行 writer:按入队顺序写入 ExcelJS 流,避免并发 commit 导致的行序竞态
|
||||
const writerLoop = (async () => {
|
||||
try {
|
||||
for (;;) {
|
||||
while (exportQueueHead < exportPageQueue.length) {
|
||||
const exportRows = exportPageQueue[exportQueueHead];
|
||||
exportQueueHead += 1;
|
||||
for (const exportRow of exportRows) {
|
||||
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();
|
||||
}
|
||||
})();
|
||||
|
||||
const orderIds = rowsWithWaveIndex.map(r => Number(r.id || 0)).filter(id => id > 0);
|
||||
|
||||
// 批量查询商品明细(wms_order_items)
|
||||
const itemsMap = new Map();
|
||||
if (orderIds.length > 0) {
|
||||
// 有界并发查询:限制 2 个 worker,先查询完成的页先入队
|
||||
let nextPage = 1;
|
||||
const runWorker = async () => {
|
||||
while (!firstError) {
|
||||
const page = nextPage;
|
||||
nextPage += 1;
|
||||
if (page > totalPages) {
|
||||
break;
|
||||
}
|
||||
try {
|
||||
const itemsModel = await this.getOrderItemsModelByTableType(target.tableType, warehouseId);
|
||||
const itemsRows = await itemsModel.findAll({
|
||||
where: { order_id: { [this.app.Sequelize.Op.in]: orderIds } },
|
||||
attributes: [ 'order_id', 'item_name', 'plan_qty', 'stock_id' ],
|
||||
order: [[ 'id', 'ASC' ]],
|
||||
raw: true,
|
||||
const pageResult = await this.buildOrderExportPage({
|
||||
page,
|
||||
pageSize,
|
||||
queryWhere,
|
||||
order,
|
||||
orderModel,
|
||||
target,
|
||||
warehouseId,
|
||||
mode,
|
||||
});
|
||||
for (const item of (Array.isArray(itemsRows) ? itemsRows : [])) {
|
||||
const oid = Number(item.order_id || 0);
|
||||
if (oid > 0) {
|
||||
if (!itemsMap.has(oid)) {
|
||||
itemsMap.set(oid, []);
|
||||
}
|
||||
itemsMap.get(oid).push(item);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
this.app.logger.warn(`[order_export] 批量查询订单商品明细失败: ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// 批量查询仓库商品名称、条码与图片(复用 getStockDetails 缓存)
|
||||
const stockNameMap = new Map();
|
||||
const barcodeMap = new Map();
|
||||
const imageUrlMap = new Map();
|
||||
if (itemsMap.size > 0 && this.ctx && this.ctx.model && this.ctx.model.StockWarehouse) {
|
||||
try {
|
||||
const stockIds = [
|
||||
...new Set(
|
||||
[ ...itemsMap.values() ]
|
||||
.flat()
|
||||
.map(item => Number(item.stock_id || 0))
|
||||
.filter(id => id > 0)
|
||||
),
|
||||
];
|
||||
if (stockIds.length > 0) {
|
||||
const stockDetails = await this.ctx.service.systems.stockWarehouse.getStockDetails(
|
||||
stockIds,
|
||||
{ warehouseId }
|
||||
);
|
||||
for (const detail of stockDetails) {
|
||||
const sid = Number(detail.id || 0);
|
||||
if (sid > 0) {
|
||||
stockNameMap.set(sid, String(detail.item_name || '').trim());
|
||||
// 从 getStockDetails 的 barcodes 中提取条码并转为逗号拼接
|
||||
const barcodesRaw = Array.isArray(detail.barcodes)
|
||||
? detail.barcodes.map(bc => String(bc.barcode || '').trim()).filter(Boolean)
|
||||
: [];
|
||||
if (barcodesRaw.length > 0) {
|
||||
barcodeMap.set(sid, [ ...new Set(barcodesRaw) ].join(', '));
|
||||
}
|
||||
// 图片字段(有图片标记但实际 URL 需要额外查询)
|
||||
if (detail.is_images === 'Y') {
|
||||
imageUrlMap.set(sid, '');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
this.app.logger.warn(`[order_export] 批量查询商品详情失败: ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// 批量查询货主备注(wms_store_warehouse.store_remask)
|
||||
const storeRemarkMap = new Map();
|
||||
if (orderIds.length > 0 && this.ctx && this.ctx.model && this.ctx.model.StoreWarehouse) {
|
||||
try {
|
||||
const storeIds = [
|
||||
...new Set(
|
||||
rowsWithWaveIndex
|
||||
.map(r => Number(r.store_id || 0))
|
||||
.filter(id => id > 0)
|
||||
),
|
||||
];
|
||||
if (storeIds.length > 0) {
|
||||
const swRows = await this.ctx.model.StoreWarehouse.findAll({
|
||||
where: {
|
||||
store_id: { [this.app.Sequelize.Op.in]: storeIds },
|
||||
warehouse_id: warehouseId,
|
||||
},
|
||||
attributes: [ 'store_id', 'store_remask' ],
|
||||
raw: true,
|
||||
});
|
||||
for (const sw of (Array.isArray(swRows) ? swRows : [])) {
|
||||
const sid = Number(sw.store_id || 0);
|
||||
if (sid > 0 && !storeRemarkMap.has(sid)) {
|
||||
storeRemarkMap.set(sid, String(sw.store_remask || '').trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
this.app.logger.warn(`[order_export] 批量查询货主备注失败: ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// 批量查询运单数据(wms_order_express),按订单ID分组
|
||||
const expressMap = new Map();
|
||||
if (orderIds.length > 0 && this.ctx && this.ctx.model && this.ctx.model.OrderExpress) {
|
||||
try {
|
||||
const expressRows = await this.ctx.model.OrderExpress.findAll({
|
||||
where: {
|
||||
order_id: { [this.app.Sequelize.Op.in]: orderIds },
|
||||
warehouse_id: warehouseId,
|
||||
},
|
||||
attributes: [ 'id', 'order_id', 'waybill_code', 'business_key', 'logistics_interface_name', 'package_material_id', 'package_material_name_snapshot', 'create_at', 'weighing_weight' ],
|
||||
order: [[ 'id', 'ASC' ]],
|
||||
raw: true,
|
||||
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: `已完成 ${currentCount}/${totalCount} 条订单查询(${pagesCompleted}/${totalPages} 页)`,
|
||||
});
|
||||
for (const exp of (Array.isArray(expressRows) ? expressRows : [])) {
|
||||
const oid = Number(exp.order_id || 0);
|
||||
if (oid > 0) {
|
||||
if (!expressMap.has(oid)) {
|
||||
expressMap.set(oid, []);
|
||||
}
|
||||
expressMap.get(oid).push(exp);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
this.app.logger.warn(`[order_export] 批量查询运单数据失败: ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// 批量查询包材编码(wms_package_material),按 package_material_id 分组
|
||||
const packageMaterialMap = new Map();
|
||||
if (expressMap.size > 0 && this.ctx && this.ctx.model && this.ctx.model.PackageMaterial) {
|
||||
try {
|
||||
const materialIds = [
|
||||
...new Set(
|
||||
[ ...expressMap.values() ]
|
||||
.flat()
|
||||
.map(exp => Number(exp.package_material_id || 0))
|
||||
.filter(id => id > 0)
|
||||
),
|
||||
];
|
||||
if (materialIds.length > 0) {
|
||||
const pmRows = await this.ctx.model.PackageMaterial.findAll({
|
||||
where: { id: { [this.app.Sequelize.Op.in]: materialIds } },
|
||||
attributes: [ 'id', 'material_code' ],
|
||||
raw: true,
|
||||
});
|
||||
for (const pm of (Array.isArray(pmRows) ? pmRows : [])) {
|
||||
const pid = Number(pm.id || 0);
|
||||
if (pid > 0 && !packageMaterialMap.has(pid)) {
|
||||
packageMaterialMap.set(pid, String(pm.material_code || '').trim());
|
||||
}
|
||||
}
|
||||
if (!firstError) {
|
||||
firstError = err;
|
||||
}
|
||||
} catch (err) {
|
||||
this.app.logger.warn(`[order_export] 批量查询包材编码失败: ${err.message}`);
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const exportContext = { itemsMap, storeRemarkMap, stockNameMap, barcodeMap, imageUrlMap, expressMap, packageMaterialMap };
|
||||
const workers = Array.from(
|
||||
{ length: Math.min(ORDER_EXPORT_QUERY_CONCURRENCY, totalPages) },
|
||||
() => runWorker()
|
||||
);
|
||||
|
||||
for (const row of rowsWithWaveIndex) {
|
||||
const exportRows = this.mapOrderExportRow(row, exportContext, mode);
|
||||
for (const exportRow of exportRows) {
|
||||
worksheet.addRow(exportRow).commit();
|
||||
}
|
||||
}
|
||||
await Promise.all(workers);
|
||||
finishExportQueue();
|
||||
await writerLoop;
|
||||
|
||||
currentCount += rowsWithWaveIndex.length;
|
||||
|
||||
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} 页`,
|
||||
});
|
||||
|
||||
page += 1;
|
||||
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