From 907228297dcbea608d2036bab5f413e95de54145 Mon Sep 17 00:00:00 2001 From: tikkhun Date: Tue, 6 Jan 2026 10:33:55 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E8=AE=A2=E5=8D=95):=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=AE=A2=E5=8D=95=E5=AF=BC=E5=87=BA=E5=8A=9F=E8=83=BD=E5=B9=B6?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=95=B0=E6=8D=AE=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在订单服务中添加导出功能,支持通过ID列表导出订单数据为CSV格式。同时优化了ID列表的验证逻辑,过滤无效ID并添加空值检查,确保数据安全性和可靠性。 --- src/controller/order.controller.ts | 11 +++++++++++ src/service/order.service.ts | 17 +++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) 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({