diff --git a/src/controller/order.controller.ts b/src/controller/order.controller.ts index 04ecf2f..1f51d86 100644 --- a/src/controller/order.controller.ts +++ b/src/controller/order.controller.ts @@ -253,4 +253,15 @@ export class OrderController { return errorResponse(error?.message || '获取失败'); } } + + @ApiOkResponse() + @Post('/export') + async exportOrder(@Body('ids') ids: number[]) { + try { + const csvContent = await this.orderService.exportOrder(ids); + return successResponse(csvContent); + } catch (error) { + return errorResponse(error?.message || '导出失败'); + } + } } diff --git a/src/service/order.service.ts b/src/service/order.service.ts index 02ab0a1..e75ea8e 100644 --- a/src/service/order.service.ts +++ b/src/service/order.service.ts @@ -2461,16 +2461,25 @@ export class OrderService { } try { - // 空值检查 + // 空值检查和数据清理 + if (!ids || !Array.isArray(ids)) { + throw new Error('订单ID列表不能为空'); + } + + // 过滤掉NaN和非数字值,只保留有效的数字ID + const validIds = ids.filter(id => Number.isFinite(id) && id > 0); + + if (validIds.length === 0) { + throw new Error('未提供有效的订单ID'); + } + const dataSource = this.dataSourceManager.getDataSource('default'); // 优化事务使用 return await dataSource.transaction(async manager => { // 准备查询条件 const whereCondition: any = {}; - if (ids && ids.length > 0) { - whereCondition.id = In(ids); - } + whereCondition.id = In(validIds); // 获取订单、订单项和物流信息 const orders = await manager.getRepository(Order).find({