@@ -28,6 +28,7 @@ impl CompanyImporter {
2828 ) -> Result < ImportResult , ImportError > {
2929 let start_time = std:: time:: Instant :: now ( ) ;
3030
31+ // 空数据处理
3132 if data. is_empty ( ) {
3233 return Ok ( ImportResult {
3334 success : true ,
@@ -47,6 +48,7 @@ impl CompanyImporter {
4748
4849 let headers = & data[ 0 ] ;
4950
51+ // 验证表头
5052 let ( valid, version, actual_version, lack_columns, warnings) =
5153 FileParser :: validate_company_headers ( headers) ;
5254 if !valid {
@@ -78,6 +80,7 @@ impl CompanyImporter {
7880 let total = rows. len ( ) ;
7981 let now = Utc :: now ( ) ;
8082
83+ // 使用闭包事务,自动处理提交/回滚
8184 let result = conn
8285 . transaction :: < _ , _ , DbErr > ( |txn| {
8386 let mapping = mapping. clone ( ) ;
@@ -88,6 +91,7 @@ impl CompanyImporter {
8891 let mut company_source_map: HashMap < String , CompanySourceActiveModel > =
8992 HashMap :: new ( ) ;
9093
94+ // 根据文件的rows构建company source列表
9195 let mut errors: Vec < ImportErrorType > = Vec :: new ( ) ;
9296 for ( row_index, row) in rows. iter ( ) . enumerate ( ) {
9397 let company_name = get_field_value ( & mapping. name , row) ;
@@ -126,6 +130,7 @@ impl CompanyImporter {
126130 }
127131 }
128132
133+ // 如果有解析错误,直接返回
129134 if !errors. is_empty ( ) {
130135 return Ok ( ImportResult {
131136 success : false ,
@@ -143,12 +148,14 @@ impl CompanyImporter {
143148 } ) ;
144149 }
145150
151+ // 根据company_source id获取已存在的数据
146152 let exists_company_source = Self :: batch_query_company_sources (
147153 company_ids_from_data. into_iter ( ) . collect ( ) ,
148154 txn,
149155 )
150156 . await ?;
151157
158+ // 过滤数据库中已存在的company_source记录
152159 let exists_company_source_ids: Vec < String > = exists_company_source
153160 . iter ( )
154161 . map ( |item| item. id . clone ( ) )
@@ -160,8 +167,10 @@ impl CompanyImporter {
160167 let filter_company_source: Vec < entity:: company_source:: ActiveModel > =
161168 company_source_map. into_values ( ) . collect ( ) ;
162169
170+ // 保存过滤后的company_source记录
163171 Self :: batch_insert_company_sources ( filter_company_source. clone ( ) , txn) . await ?;
164172
173+ // 构建 company_id 到 company_source 的映射
165174 let mut filter_company_id_and_source_map = HashMap :: new ( ) ;
166175 let mut company_ids = Vec :: new ( ) ;
167176 for item in filter_company_source {
@@ -178,13 +187,15 @@ impl CompanyImporter {
178187 company_ids. push ( company_id) ;
179188 }
180189
190+ // 查询已存在的company记录
181191 let exists_company = Self :: batch_query_companies ( company_ids, txn) . await ?;
182192
183193 let exists_company_map: HashMap < & str , & entity:: company:: Model > = exists_company
184194 . iter ( )
185195 . map ( |item| ( item. id . as_str ( ) , item) )
186196 . collect ( ) ;
187197
198+ // 区分已存在和不存在company的记录
188199 let mut exists_company_source = Vec :: new ( ) ;
189200 let mut not_exists_company_source = Vec :: new ( ) ;
190201 for ( _, company_id) in filter_company_id_and_source_map. values ( ) {
@@ -197,6 +208,7 @@ impl CompanyImporter {
197208
198209 let mut insert_company = Vec :: new ( ) ;
199210
211+ // 如果company不存在,则进行插入逻辑
200212 for company_id in not_exists_company_source {
201213 if let Some ( ( source_model, _) ) =
202214 filter_company_id_and_source_map. get ( & company_id)
@@ -212,6 +224,7 @@ impl CompanyImporter {
212224
213225 let mut update_company_list = Vec :: new ( ) ;
214226
227+ // 如果company已存在,则进行更新处理逻辑
215228 for company_id in exists_company_source {
216229 let ( source_model, _) = filter_company_id_and_source_map
217230 . get ( & company_id)
@@ -234,6 +247,7 @@ impl CompanyImporter {
234247 |e| DbErr :: Query ( sea_orm:: RuntimeErr :: Internal ( e. to_string ( ) ) ) ,
235248 ) ?;
236249
250+ // 规则1: source_refresh_datetime更新时,从company_source重建company
237251 let existing_refresh = existing_company. source_refresh_datetime ;
238252 let new_refresh = source_model. source_refresh_datetime ;
239253 if let ( Some ( existing_dt) , Some ( new_dt) ) = ( existing_refresh, new_refresh)
@@ -252,8 +266,10 @@ impl CompanyImporter {
252266 let imported = insert_company. len ( ) ;
253267 let updated = update_company_list. len ( ) ;
254268
269+ // 批量插入新company
255270 Self :: batch_insert_companies ( insert_company, txn) . await ?;
256271
272+ // 更新已有company
257273 for item in update_company_list {
258274 item. update ( txn) . await ?;
259275 }
@@ -283,6 +299,7 @@ impl CompanyImporter {
283299 Ok ( result)
284300 }
285301
302+ // 根据company_source id批量查询
286303 async fn batch_query_company_sources < C > (
287304 ids : Vec < String > ,
288305 conn : & C ,
@@ -301,6 +318,7 @@ impl CompanyImporter {
301318 Ok ( results)
302319 }
303320
321+ // 批量插入company_source记录
304322 async fn batch_insert_company_sources < C > (
305323 sources : Vec < entity:: company_source:: ActiveModel > ,
306324 conn : & C ,
@@ -316,6 +334,7 @@ impl CompanyImporter {
316334 Ok ( ( ) )
317335 }
318336
337+ // 根据company id批量查询
319338 async fn batch_query_companies < C > (
320339 ids : Vec < String > ,
321340 conn : & C ,
@@ -334,6 +353,7 @@ impl CompanyImporter {
334353 Ok ( results)
335354 }
336355
356+ // 批量插入company记录
337357 async fn batch_insert_companies < C > (
338358 companies : Vec < entity:: company:: ActiveModel > ,
339359 conn : & C ,
@@ -349,6 +369,7 @@ impl CompanyImporter {
349369 Ok ( ( ) )
350370 }
351371
372+ // 根据CSV行构建company_source模型
352373 fn build_company_source (
353374 id : & str ,
354375 company_id : & str ,
@@ -408,6 +429,7 @@ impl CompanyImporter {
408429 update_datetime : ActiveValue :: set ( None ) ,
409430 } ;
410431
432+ // 解析 source_refresh_datetime
411433 if let Some ( dt_str) = get_string ( row, mapping. source_refresh_datetime ) {
412434 if let Ok ( dt) = DateTime :: parse_from_rfc3339 ( & dt_str) {
413435 model. source_refresh_datetime =
@@ -417,6 +439,7 @@ impl CompanyImporter {
417439 }
418440 }
419441
442+ // 解析 create_datetime
420443 if let Some ( dt_str) = get_string ( row, mapping. create_datetime )
421444 && let Ok ( dt) = DateTime :: parse_from_rfc3339 ( & dt_str)
422445 {
@@ -433,6 +456,7 @@ impl CompanyImporter {
433456 Ok ( model)
434457 }
435458
459+ // 根据company_source构建company模型
436460 fn build_company (
437461 source : & CompanySourceModel ,
438462 update_datetime : & DateTime < FixedOffset > ,
@@ -476,5 +500,4 @@ impl CompanyImporter {
476500 update_datetime : ActiveValue :: set ( Some ( * update_datetime) ) ,
477501 } )
478502 }
479- }
480-
503+ }
0 commit comments