@@ -9,123 +9,92 @@ interface PriceUpdate {
9
9
10
10
export async function POST ( request : NextRequest ) {
11
11
try {
12
- const body = await request . json ( ) ;
13
-
14
- // 处理单个更新的情况
15
- if ( body . id ) {
16
- console . log ( "单个价格更新请求:" , {
17
- id : body . id ,
18
- input_price : body . input_price ,
19
- output_price : body . output_price ,
20
- } ) ;
21
-
22
- const result = await updateModelPrice (
23
- body . id ,
24
- body . input_price ,
25
- body . output_price
26
- ) ;
27
-
28
- if ( ! result ) {
29
- console . log ( "模型不存在:" , body . id ) ;
30
- return NextResponse . json ( { error : "模型不存在" } , { status : 404 } ) ;
31
- }
32
-
33
- return NextResponse . json ( {
34
- input_price : result . input_price ,
35
- output_price : result . output_price ,
36
- } ) ;
12
+ const data = await request . json ( ) ;
13
+ console . log ( "收到的原始请求数据:" , data ) ;
14
+
15
+ // 从对象中提取模型数组
16
+ const updates = data . updates || data ;
17
+ if ( ! Array . isArray ( updates ) ) {
18
+ console . error ( "无效的数据格式 - 期望数组:" , updates ) ;
19
+ return NextResponse . json ( { error : "无效的数据格式" } , { status : 400 } ) ;
37
20
}
38
21
39
- // 处理批量更新的情况
40
- if ( body . updates && Array . isArray ( body . updates ) ) {
41
- console . log ( "收到批量更新请求,原始数据:" , body . updates ) ;
42
-
43
- // 验证并转换每个更新项
44
- const validUpdates = body . updates
45
- . map ( ( update : any ) => ( {
46
- id : update . id ,
47
- input_price : Number ( update . input_price ) ,
48
- output_price : Number ( update . output_price ) ,
49
- } ) )
50
- . filter (
51
- ( update : PriceUpdate ) =>
52
- update . id &&
53
- ! isNaN ( update . input_price ) &&
54
- ! isNaN ( update . output_price ) &&
55
- update . input_price >= 0 &&
56
- update . output_price >= 0
57
- ) ;
58
-
59
- console . log ( "验证后的有效更新数据:" , validUpdates ) ;
60
-
61
- // 执行批量更新并收集结果
62
- const results = await Promise . all (
63
- validUpdates . map ( async ( update : PriceUpdate ) => {
64
- try {
65
- // console.log("正在更新模型:", update.id, {
66
- // input_price: update.input_price,
67
- // output_price: update.output_price,
68
- // });
69
-
70
- const result = await updateModelPrice (
71
- update . id ,
72
- update . input_price ,
73
- update . output_price
74
- ) ;
75
-
76
- // if (!result) {
77
- // console.log("更新失败 - 模型不存在:", update.id);
78
- // } else {
79
- // console.log("更新成功:", {
80
- // id: update.id,
81
- // newPrices: {
82
- // input_price: result.input_price,
83
- // output_price: result.output_price,
84
- // },
85
- // });
86
- // }
87
-
88
- return {
89
- id : update . id ,
90
- success : ! ! result ,
91
- data : result ,
92
- } ;
93
- } catch ( error ) {
94
- console . error ( "更新模型时出错:" , update . id , error ) ;
95
- return {
96
- id : update . id ,
97
- success : false ,
98
- error : error instanceof Error ? error . message : "未知错误" ,
99
- } ;
100
- }
101
- } )
102
- ) ;
103
-
104
- // 过滤出成功更新的记录
105
- const successfulUpdates = results . filter ( ( r ) => r . success ) ;
106
- // console.log("成功更新的数量:", successfulUpdates.length);
107
- // console.log("更新结果汇总:", {
108
- // total: results.length,
109
- // successful: successfulUpdates.length,
110
- // failed: results.length - successfulUpdates.length,
111
- // });
112
-
113
- return NextResponse . json ( {
114
- success : true ,
115
- updatedCount : successfulUpdates . length ,
116
- results : successfulUpdates . map ( ( r ) => ( {
117
- id : r . id ,
118
- input_price : r . data ?. input_price ,
119
- output_price : r . data ?. output_price ,
120
- } ) ) ,
22
+ // 验证并转换数据格式
23
+ const validUpdates = updates
24
+ . map ( ( update : any ) => ( {
25
+ id : update . id ,
26
+ input_price : Number ( update . input_price ) ,
27
+ output_price : Number ( update . output_price ) ,
28
+ } ) )
29
+ . filter ( ( update : PriceUpdate ) => {
30
+ if ( ! update . id ) {
31
+ console . log ( "跳过 - ID 无效:" , update ) ;
32
+ return false ;
33
+ }
34
+
35
+ if ( isNaN ( update . input_price ) || isNaN ( update . output_price ) ) {
36
+ console . log ( "跳过 - 价格无效:" , update ) ;
37
+ return false ;
38
+ }
39
+
40
+ return true ;
121
41
} ) ;
122
- }
123
42
124
- console . log ( "无效的请求格式:" , body ) ;
125
- return NextResponse . json ( { error : "无效的请求格式" } , { status : 400 } ) ;
43
+ console . log ( "处理后的更新数据:" , validUpdates ) ;
44
+ console . log ( `成功验证 ${ validUpdates . length } 个模型的价格更新请求` ) ;
45
+
46
+ // 执行批量更新并收集结果
47
+ const results = await Promise . all (
48
+ validUpdates . map ( async ( update : PriceUpdate ) => {
49
+ try {
50
+ console . log ( "正在处理模型更新:" , {
51
+ id : update . id ,
52
+ input_price : update . input_price ,
53
+ output_price : update . output_price ,
54
+ } ) ;
55
+
56
+ const result = await updateModelPrice (
57
+ update . id ,
58
+ update . input_price ,
59
+ update . output_price
60
+ ) ;
61
+
62
+ console . log ( "更新结果:" , {
63
+ id : update . id ,
64
+ success : ! ! result ,
65
+ result,
66
+ } ) ;
67
+
68
+ return {
69
+ id : update . id ,
70
+ success : ! ! result ,
71
+ data : result ,
72
+ } ;
73
+ } catch ( error ) {
74
+ console . error ( "更新失败:" , {
75
+ id : update . id ,
76
+ error : error instanceof Error ? error . message : "未知错误" ,
77
+ } ) ;
78
+ return {
79
+ id : update . id ,
80
+ success : false ,
81
+ error : error instanceof Error ? error . message : "未知错误" ,
82
+ } ;
83
+ }
84
+ } )
85
+ ) ;
86
+
87
+ const successCount = results . filter ( ( r ) => r . success ) . length ;
88
+ console . log ( `成功更新 ${ successCount } 个模型的价格` ) ;
89
+
90
+ return NextResponse . json ( {
91
+ success : true ,
92
+ message : `成功更新 ${ successCount } 个模型的价格` ,
93
+ results,
94
+ } ) ;
126
95
} catch ( error ) {
127
- console . error ( "处理请求时发生错误 :" , error ) ;
128
- return NextResponse . json ( { error : "更新价格失败 " } , { status : 500 } ) ;
96
+ console . error ( "批量更新失败 :" , error ) ;
97
+ return NextResponse . json ( { error : "批量更新失败 " } , { status : 500 } ) ;
129
98
}
130
99
}
131
100
0 commit comments