forked from yoone/API
feat(订单): 添加订单导出功能并优化数据验证
在订单服务中添加导出功能,支持通过ID列表导出订单数据为CSV格式。同时优化了ID列表的验证逻辑,过滤无效ID并添加空值检查,确保数据安全性和可靠性。
This commit is contained in:
parent
934085fd64
commit
907228297d
|
|
@ -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 || '导出失败');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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({
|
||||
|
|
|
|||
Loading…
Reference in New Issue