feat(订单): 添加订单导出功能并优化数据验证

在订单服务中添加导出功能,支持通过ID列表导出订单数据为CSV格式。同时优化了ID列表的验证逻辑,过滤无效ID并添加空值检查,确保数据安全性和可靠性。
This commit is contained in:
tikkhun 2026-01-06 10:33:55 +08:00
parent 934085fd64
commit 907228297d
2 changed files with 24 additions and 4 deletions

View File

@ -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 || '导出失败');
}
}
}

View File

@ -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({