1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
public with sharing class NFM502Controller implements Queueable {
    public String rowData_id;
    /*迁移ali sushanhu 20231018
    //add staic sushanhu 20220302 start
    public static String transUrl;
    public static String transId;
    public static String token;
    public static List<String> sfRecordIds =new List<String>();
    //add staic sushanhu 20220302 end
    */
    public NFM502Controller(String rowData_id) {
        this.rowData_id = rowData_id;
    }
 
    public static Integer batch_retry_max_cnt = Integer.valueOf(System.Label.batch_retry_max_cnt);
    public void execute(QueueableContext context) {
        // 通过Rowdata.Id来检索日志中的内容(千里马数据等)
        BatchIF_Log__c rowData = [Select Id, Name, Log__c, ErrorLog__c, Log2__c, Log3__c,
                                  Log4__c, Log5__c, Log6__c, Log7__c, Log8__c, Log9__c,
                                  Log10__c, Log11__c, Log12__c, MessageGroupNumber__c, retry_cnt__c,
                                  NFM501Future_Count__c,
                                  NFM501_Web_Annex_Count__c
                                  from BatchIF_Log__c where RowDataFlg__c = true and Id = :rowData_id];
        //存放报错信息
        BatchIF_Log__c iflog502 = new BatchIF_Log__c();
        iflog502.Type__c = 'NFM502'; // 区分一下501和502 2022-05-25 ssm
        iflog502.RowDataFlg__c = false;
        iflog502.Log__c = ' ';
        iflog502.ErrorLog__c = ' ';
        iflog502.MessageGroupNumber__c = rowData.MessageGroupNumber__c;
        insert iflog502;
        NFM502Controller.WebAnnexGain(rowData.Id, iflog502.Id, false);
    }
    @future(callout = true)
    public static void WebAnnexGain(String rowData_id, String iflog502_id, boolean Manual_execution502) {
        BatchIF_Log__c rowData = [Select Id, Name, Log__c, ErrorLog__c, Log2__c, Log3__c,
                                  Log4__c, Log5__c, Log6__c, Log7__c, Log8__c, Log9__c, Log10__c,
                                  Log11__c, Log12__c, MessageGroupNumber__c, retry_cnt__c,
                                  NFM501Future_Count__c,
                                  NFM501_Web_Annex_Count__c from BatchIF_Log__c
                                  where RowDataFlg__c = true and Id = :rowData_id];
        BatchIF_Log__c iflog502 = [Select Id, Name, Log__c, ErrorLog__c, Log2__c, Log3__c,
                                   Log4__c, Log5__c, Log6__c, Log7__c, Log8__c, Log9__c, Log10__c,
                                   Log11__c, Log12__c, MessageGroupNumber__c, retry_cnt__c,
                                   NFM501Future_Count__c,
                                   NFM501_Web_Annex_Count__c from BatchIF_Log__c
                                   where Id = :iflog502_id];
 
        iflog502.Log__c = iflog502.Log__c == null ? '' : iflog502.Log__c;
        iflog502.ErrorLog__c = iflog502.ErrorLog__c == null ? '' : iflog502.ErrorLog__c;
        rowData.Log__c = rowData.Log__c == null ? '' : rowData.Log__c;
        rowData.ErrorLog__c = rowData.ErrorLog__c == null ? '' : rowData.ErrorLog__c;
 
        // Savepoint sp = Database.setSavepoint();
        try {
            //update 同staic 20220302 satrt
            String token;
            //update 同staic 20220302 end
            Datetime oldTime;
            // 从转换表中获取token
            BatchIF_Transfer__c token502 = [Select ID, NFM501_Token__c
                                            FROM BatchIF_Transfer__c Where Table__c = 'NFM501Token'];
            token = token502.NFM501_Token__c;
            // 从转换表中获取获取完token的时间
            BatchIF_Transfer__c oldTime502 = [Select ID, NFM501_Gain_End_Time__c
                                              FROM BatchIF_Transfer__c Where Table__c = 'NFM501GainEndTime'];
            oldTime = oldTime502.NFM501_Gain_End_Time__c;
            // 对日志中的数据进行解析
            String WebUrl = NFMUtil.QLMgetRowDataStr(rowData);
            NFM501Controller.AllData getQLMData502 = (NFM501Controller.AllData)
                    JSON.deserialize(WebUrl, NFM501Controller.AllData.class);
            if (getQLMData502 == null) {
                return;
            }
 
            // 判断token是否失效(失效条件为30分钟之后),如果失效,重新获取
            Long timeslot;
            Datetime newTime = System.now();
            if (oldTime == null) {
                timeslot = 2800000;
            } else {
                // 当前时间与获取token结束时间的时间差
                timeslot = newTime.getTime() - oldTime.getTime();
            }
            // System.debug('++++1++++' + token + '  : ' + timeslot);
            if (string.isblank(token) || timeslot > 1800000) {
                NFMUtil.response response = NFMUtil.receiveToken();
                //判断rowdata中数据获取成功与否,如果失败重发三次,如果大于三次则手动操作
                if (String.isBlank(response.responseBody)) {
                    System.debug('response.responseBody:' + response.responseBody);
                    iflog502.ErrorLog__c = '502token:' + response.responseBody;
                    // rowData.NFM501_Web_Annex_Count__c = 0;
                    if (!Manual_execution502) {
                        NFM501Controller.againSendRequest(iflog502, 'NFM501_Web_Annex_Count__c', rowData);
                    }
                    //更新日志数据
                    System.debug('123@@@');
                    return;
                }
                token = response.responseBody;
                oldTime = Datetime.now();
                token502.NFM501_Token__c = token;
                oldTime502.NFM501_Gain_End_Time__c = oldTime;
                /*迁移ali sushanhu 20231018
                //update to aws token sushanhu 20220301  start
                NFMUtil.response response = NFMUtil.getAWSToken();
                //判断rowdata中数据获取成功与否,如果失败重发三次,如果大于三次则手动操作
                if (String.isBlank(response.responseBody)) {
                    System.debug('response.responseBody:' + response.responseBody);
                    iflog502.ErrorLog__c = '502token:' + response.responseBody;
                    // rowData.NFM501_Web_Annex_Count__c = 0;
                    if (!Manual_execution502) {
                        NFM501Controller.againSendRequest(iflog502, 'NFM501_Web_Annex_Count__c', rowData);
                    }
                    //更新日志数据
                    System.debug('123@@@');
                    return;
                }
                token = response.responseBody;
                oldTime = Datetime.now();
                token502.NFM501_Token__c = token;
                oldTime502.NFM501_Gain_End_Time__c = oldTime;
                //update to aws token sushanhu 20220301  end
                */
            }
 
            //关联附件与招投标项目(通过Id)
            //1.读出招投标中的唯一标识(projecId),将全部招投标projectId存入ProjectIdList
            List<String> ProjectIdList = new List<String>();
            for (NFM501Controller.ListItem ProId : getQLMData502.data.list1) {
                ProjectIdList.add(ProId.projectId);
            }
            System.debug('---===ProjectIdList' + ProjectIdList);
            //2.取其对应的
            List<Tender_information__c> TIList =
                [Select Id, ProjectId__c, InfoType__c
                // SWAG-C9S9P6 新增字段 2022-05-25 ssm start
                , InfoId__c, subInfoType__c  
                // SWAG-C9S9P6 新增字段 2022-05-25 ssm end 
                 FROM Tender_information__c
                 Where ProjectId__c in :ProjectIdList];
            System.debug('---===2345TIList' + TIList);
            Set<Id> TenIdSet = new Set<Id>();
 
            Map<String, Tender_information__c> TenMap = new Map<String, Tender_information__c>();
            for (Tender_information__c Ten : TIList) {
                TenMap.put(Ten.ProjectId__c, Ten);
                TenIdSet.add(Ten.Id);
            }
 
            //循环URL
            List<Attachment> TenAttList = new List<Attachment>();
            List<ContentVersion> insertCvList = new List<ContentVersion>();//迁移ali add by sushanhu 20231023
            Map<String, String> parentMap = new Map<String, String>();//迁移ali add by sushanhu 20231023
            for (NFM501Controller.ListItem QLMWebAtt : getQLMData502.data.list1) {
                if (QLMWebAtt.projectId == null) {
                    iflog502.ErrorLog__c += 'Error! [' + QLMWebAtt.projectId + ']NotExist. This information is skipped.\n';
                    continue;
                }
                if (QLMWebAtt.areaProvince.equals('香港特别行政区')
                        || QLMWebAtt.areaProvince.equals('澳门特别行政区')
                        || QLMWebAtt.areaProvince.equals('台湾省')) {
                    iflog502.ErrorLog__c += 'Error! [' + QLMWebAtt.areaProvince +
                                            ']Is 香港特别行政区(澳门特别行政区,台湾省). This information is skipped.\n';
                    continue;
                }
                //调用接口3
                NFMUtil.response response = NFMUtil.getQLMData(NFMUtil.NFM502_ENDPOINT + QLMWebAtt.infoQianlimaUrl, token);
                if (String.isBlank(response.responseBody)) {
                    System.debug('response.responseBody:' + response.responseBody);
                    iflog502.ErrorLog__c = '502接口调用:' + response.status;
                    rowData.NFM501_Web_Annex_Count__c = 0;
                    if (!Manual_execution502) {
                        NFM501Controller.againSendRequest(iflog502, 'NFM501_Web_Annex_Count__c', rowData);
                    }
                    //更新日志数据
                    update token502;
                    update oldTime502;
                    return;
                }
            /* 迁移ali 20231018 sushanhu
            //update to aws pi sushanhu 20220301 start
            List<FileAddress__c> fileList = new List<FileAddress__c>();
            List<String> queryUrlList = new List<String>();
            Map<String, NFM501Controller.ListItem> queryMap = new Map<String, NFM501Controller.ListItem>();
            //update to aws pi sushanhu 20220301 end
            for (NFM501Controller.ListItem QLMWebAtt : getQLMData502.data.list1) {
                if (QLMWebAtt.projectId == null) {
                    iflog502.ErrorLog__c += 'Error! [' + QLMWebAtt.projectId + ']NotExist. This information is skipped.\n';
                    continue;
                }
                if (QLMWebAtt.areaProvince.equals('香港特别行政区')
                        || QLMWebAtt.areaProvince.equals('澳门特别行政区')
                        || QLMWebAtt.areaProvince.equals('台湾省')) {
                    iflog502.ErrorLog__c += 'Error! [' + QLMWebAtt.areaProvince +
                                            ']Is 香港特别行政区(澳门特别行政区,台湾省). This information is skipped.\n';
                    continue;
                }
                //update to aws pi sushanhu 20220301 start
                queryUrlList.add(QLMWebAtt.infoQianlimaUrl);
                queryMap.put(QLMWebAtt.infoQianlimaUrl,QLMWebAtt);
                //update to aws pi sushanhu 20220301 start
            //  //调用接口3
            //  NFMUtil.response response = NFMUtil.getQLMData(NFMUtil.NFM502_ENDPOINT + QLMWebAtt.infoQianlimaUrl, token);
            //  if (String.isBlank(response.responseBody)) {
            //      System.debug('response.responseBody:' + response.responseBody);
            //      iflog502.ErrorLog__c = '502接口调用:' + response.status;
            //      rowData.NFM501_Web_Annex_Count__c = 0;
            //      if (!Manual_execution502) {
            //          NFM501Controller.againSendRequest(iflog502, 'NFM501_Web_Annex_Count__c', rowData);
            //      }
            //      //更新日志数据
            //      update token502;
            //      update oldTime502;
            //      return;
            //  }
 
            //  //解析后的code报错处理
            //  string NFM502responseBody = response.responseBody;
            //  Map<String, Object> Body502 = (Map<String, Object>) JSON.deserializeUntyped(NFM502responseBody);
            //  if (!Body502.get('code').equals('0')) {
            //      System.debug('-------9-------');
            //      iflog502.ErrorLog__c = '502解析:' + Body502.get('msg').tostring() ;
            //      if (!Manual_execution502) {
            //          NFM501Controller.againSendRequest(iflog502, 'NFM501_Web_Annex_Count__c', rowData);
            //      }
            //      update token502;
            //      update oldTime502;
            //      return;
            //  }
            //  System.debug('Body502.data:' + Body502.get('data').tostring() + '---------'
            //               + Body502.get('msg').tostring() + '-------' + Body502.get('code').tostring());
 
            //  //获取网页信息转存为附件
            //  //截切数据(使数据成为解析的格式)
            //  Integer start = NFM502responseBody.indexOf('"infoHtml":"');
            //  Integer theEnd = NFM502responseBody.lastIndexOf('"},"msg');
            //  NFM502responseBody = NFM502responseBody.substring(start + 12, theEnd);
            //  //将其转换为附件
            //  // System.debug('---------' + NFM502responseBody);
            //  Attachment WebAtt = new Attachment();
            //  // System.debug('projectId:' + QLMWebAtt.projectId);
                // if (TenMap.containskey(QLMWebAtt.projectId)) {
                //  WebAtt.ParentId = TenMap.get(QLMWebAtt.projectId).Id;
                //  WebAtt.Body = Blob.valueOf(NFM502responseBody);
                //  WebAtt.Name = TenMap.get(QLMWebAtt.projectId).InfoType__c + ':' + QLMWebAtt.infoTitle + '.html';
                //  TenAttList.add(WebAtt);
                // }
             }
            //update to aws pi  sushanhu 20220301 start
            PIHelper.piIntegration pi =PIHelper.getPIIntegrationInfo('NFM502');
            transUrl=pi.searchUrl;
            //调用接口3
            system.debug('Payload for NFM 520:'+JSON.serialize(queryUrlList));
            NFMUtil.response response = NFMUtil.getAWSQLMData(pi.newUrl ,JSON.serialize(queryUrlList), token);
            Map<String, Object> result = (Map<String, Object>)JSON.deserializeUntyped(response.responseBody);
            String statusCode =(String)result.get('status');
            transId =(String)result.get('txId');
            if (!'0'.equals(statusCode)) {
                System.debug('response.responseBody:' + response.responseBody);
                iflog502.ErrorLog__c = '502接口调用:' + (String)result.get('message');
                rowData.NFM501_Web_Annex_Count__c = 0;
                if (!Manual_execution502) {
                    NFM501Controller.againSendRequest(iflog502, 'NFM501_Web_Annex_Count__c', rowData);
                }
                //更新日志数据
                update token502;
                update oldTime502;
                return;
            }
            */
            //解析后的code报错处理
            string NFM502responseBody = response.responseBody;
            Map<String, Object> Body502 = (Map<String, Object>) JSON.deserializeUntyped(NFM502responseBody);
            system.debug('Body502---' + json.serialize(Body502));// 迁移ali sushanhu 20231020
            //if (!String.valueOf(Body502.get('status')).equals('0')) {// 迁移ali sushanhu 20231020
            if (!String.valueOf(Body502.get('code')).equals('0')) {    
                System.debug('-------9-------');
                iflog502.ErrorLog__c = '502解析:' + Body502.get('message').tostring() ;
                if (!Manual_execution502) {
                    NFM501Controller.againSendRequest(iflog502, 'NFM501_Web_Annex_Count__c', rowData);
                }
                update token502;
                update oldTime502;
                return;
            }
            System.debug(//'Body502.data:' + Body502.get('object').tostring() + '---------'
                         //+ Body502.get('message').tostring() + '-------' + Body502.get('status').tostring());// 迁移ali sushanhu 20231020
                         'Body502.data:' + Body502.get('data').tostring() + '---------'
                         + Body502.get('msg').tostring() + '-------' + Body502.get('code').tostring());
 
 
             //获取网页信息转存为附件
                //截切数据(使数据成为解析的格式)
                Integer start = NFM502responseBody.indexOf('"infoHtml":"');
                Integer theEnd = NFM502responseBody.lastIndexOf('"},"msg');
                NFM502responseBody = NFM502responseBody.substring(start + 12, theEnd);
                //将其转换为附件
                /*迁移ALI update contentdocment sushanhu 20231020
                // System.debug('---------' + NFM502responseBody);
                Attachment WebAtt = new Attachment();
                // System.debug('projectId:' + QLMWebAtt.projectId);
                if (TenMap.containskey(QLMWebAtt.projectId)) {
                    WebAtt.ParentId = TenMap.get(QLMWebAtt.projectId).Id;
                    WebAtt.Body = Blob.valueOf(NFM502responseBody);
                    WebAtt.Name = TenMap.get(QLMWebAtt.projectId).InfoType__c + ':' + QLMWebAtt.infoTitle + '.html';
                    TenAttList.add(WebAtt);
                }
                */
                if (TenMap.containskey(QLMWebAtt.projectId)) {
                ContentVersion CV =  new ContentVersion();
                //20240304 lt 群里提的附件名字太长导致的接口报错 优化附件名的逻辑 start 
                if(QLMWebAtt.infoTitle.length() > 100){
                    QLMWebAtt.infoTitle = QLMWebAtt.infoTitle.substring(0, 100);
                }
                //20240304 lt 群里提的附件名字太长导致的接口报错 优化附件名的逻辑 end
                CV.Title = TenMap.get(QLMWebAtt.projectId).InfoType__c + ':' + QLMWebAtt.infoTitle + '.html';
                CV.PathOnClient = TenMap.get(QLMWebAtt.projectId).InfoType__c + ':' + QLMWebAtt.infoTitle + '.html';
                CV.VersionData = Blob.valueOf(NFM502responseBody);
                //新加的字段 存到descrption add by tiger 20231019
                CV.Description = TenMap.get(QLMWebAtt.projectId).InfoId__c +';' 
                + TenMap.get(QLMWebAtt.projectId).InfoType__c +';'
                + TenMap.get(QLMWebAtt.projectId).subInfoType__c +';'
                + String.valueOf(Date.today()) ;
                CV.IsMajorVersion = true;
                insertCvList.add(CV);
                //parentMap.put(CV.Title,TenMap.get(QLMWebAtt.projectId).Id);//update by tiger 20240221
                parentMap.put(TenMap.get(QLMWebAtt.projectId).InfoId__c,TenMap.get(QLMWebAtt.projectId).Id);
                }
            }
            //删除同名的附件
            List<String> UrlList = new List<String>();
            for (NFM501Controller.ListItem UrlName : getQLMData502.data.list1) {
                if (TenMap.containskey(UrlName.projectId)) {
                    UrlList.add(TenMap.get(UrlName.projectId).InfoType__c + ':' + UrlName.infoTitle + '.html');
                }
            }
            /*update contentdocment sushanhu 20231020
            List<Attachment> DeleAttList = [select id, name, ParentId from Attachment
                                            where name in :UrlList and ParentId in :TenIdSet];
                                            */
            Set<String> deleteDocIdSet = new Set<String>();
            for (ContentDocumentLink docLink : [select id ,LinkedEntityId,ContentDocumentId,ContentDocument.Title from ContentDocumentLink  
            where ContentDocument.Title in :UrlList and LinkedEntityId in:TenIdSet]) {
                deleteDocIdSet.add(docLink.ContentDocumentId);
            }
            List<ContentDocument> DeleAttList = new List<ContentDocument>();
            if (deleteDocIdSet.size()>0) {
                DeleAttList = [select id, Title, ParentId from ContentDocument
                where id =:deleteDocIdSet];
            }
            if (DeleAttList.size() > 0) {
                delete DeleAttList;
            }
            System.debug('----1----' + TenAttList);
            /* 迁移ali sushanhu 20231023
            if (TenAttList.size() > 0) {
                upsert TenAttList;
            }
            */
            if (insertCvList.size()>0) {
                upsert insertCvList;
                //关联文件
                Set<String> cvIdList = new Set<String>();
                for (ContentVersion cv : insertCvList) {
                    cvIdList.add(cv.Id);
                }
                Map<String, String> docMap = new Map<String, String>();
                List<ContentDocumentLink> upsertDocmentLinkList = new List<ContentDocumentLink>();
                for (ContentDocument cd : [SELECT Id, LatestPublishedVersionId,Title
                                            ,Description// add by tiger 20240221
                                    FROM ContentDocument
                                    WHERE LatestPublishedVersionId = :cvIdList 
                                    ]) {               
                    //ContentDocumentLink cdl = new ContentDocumentLink(ContentDocumentId = cd.Id, LinkedEntityId= parentMap.get(cd.Title), ShareType='V');
                    String inforId =  cd.Description.split(';')[0];
                    ContentDocumentLink cdl = new ContentDocumentLink(ContentDocumentId = cd.Id, LinkedEntityId= parentMap.get(inforId), ShareType='V');//change by tiger 20240221  
                    upsertDocmentLinkList.add(cdl);
                }
                
                upsert upsertDocmentLinkList;
            }
            
            rowData.NFM501_Web_Annex_Count__c = 0;            
            /*迁移ali 20231018 sushanhu
            //获取aws返回的地址并存储
            
            Map<String, Object> fileMap = (Map<String, Object >)result.get('object');
            for(String url:queryUrlList){
                NFM501Controller.ListItem QLMWebAtt = queryMap.get(url);
                if (TenMap.containskey(QLMWebAtt.projectId)) {
                    FileAddress__c file =new FileAddress__c();
                    file.ParentRecordId__c = TenMap.get(QLMWebAtt.projectId).Id;
                    file.FileName__c = TenMap.get(QLMWebAtt.projectId).InfoType__c + ':' + sub_file_name(QLMWebAtt.infoTitle) + '.html';
                    file.DownloadLink__c =pi.undeleteUrl+(String)fileMap.get(url)+'&fileName='+file.FileName__c;
                    file.ViewLink__c = pi.queryUrl+(String)fileMap.get(url) ;
                    file.AWS_File_Key__c =(String)fileMap.get(url) ;
                    // SWAG-C9S9P6 新增字段 start
                    file.InfoId__c = TenMap.get(QLMWebAtt.projectId).InfoId__c;
                    file.InfoType__c = TenMap.get(QLMWebAtt.projectId).InfoType__c;
                    file.subInfoType__c = TenMap.get(QLMWebAtt.projectId).subInfoType__c;
                    file.UpdateDate__c = Date.today(); 
                    // SWAG-C9S9P6 新增字段 end
                    fileList.add(file);
                }
            }
            
            //删除同名的附件
            List<String> UrlList = new List<String>();
            for (NFM501Controller.ListItem UrlName : getQLMData502.data.list1) {
                if (TenMap.containskey(UrlName.projectId)) {
                    UrlList.add(TenMap.get(UrlName.projectId).InfoType__c + ':' + sub_file_name(UrlName.infoTitle) + '.html');
                }
            }
 
            List<FileAddress__c> DeleFileList = [select id, FileName__c, ParentRecordId__c from FileAddress__c
                                            where FileName__c in :UrlList and ParentRecordId__c in :TenIdSet];
            if (DeleFileList.size() > 0) {
                delete DeleFileList;
            }
            System.debug('----1----' + fileList);
            if (fileList.size() > 0) {
                upsert fileList;
            }
            //确认事务
             
            for (FileAddress__c file : fileList) {
                system.debug('file--'+json.serialize(file));
                system.debug('file.id'+file.Id);
                sfRecordIds.add(file.Id);
            }
            //update to aws pi  sushanhu 20220301 end
            //  PIHelper.confirmFileTrans('NFM502',1,JSON.serialize(sfRecordIds),transId,token,transUrl);
            if (fileList.size() > 0) {
                PIHelper.insertConfirmTrans('NFM502',1,JSON.serialize(sfRecordIds),transId,0,transUrl,null);
            }else{
                PIHelper.insertConfirmTrans('NFM502',0,JSON.serialize(sfRecordIds),transId,0,transUrl,null);
            }
 
            
            // if (!confirm) {
            //  //回滚
            // }
            rowData.NFM501_Web_Annex_Count__c = 0;
            */
        } catch (Exception ex) {
            // Database.rollback(sp);
            // System.debug(Logginglevel.ERROR, 'QLMData_' + rowData.MessageGroupNumber__c + ':' + ex.getMessage());
            // System.debug(Logginglevel.ERROR, 'QLMData_' + rowData.MessageGroupNumber__c + ':' + ex.getStackTraceString());
            // logstr += '\n' + ex.getMessage();
            /* 迁移ali sushanhu 20231019
            //add 事务确认 sushanhu 20220302 satrt
            //  PIHelper.confirmFileTrans('NFM502',0,'',transId,token,transUrl);
             PIHelper.insertConfirmTrans('NFM502',0,JSON.serialize(sfRecordIds),transId,0,transUrl,null);
            //add 事务确认 sushanhu 20220302 end
            */
            iflog502.ErrorLog__c = '502抛出异常:' + ex.getMessage() + '\n'
                                   + ex.getStackTraceString() + '\n' + iflog502.ErrorLog__c;
            if (!Manual_execution502) {
                NFM501Controller.againSendExceptionRequest(iflog502, 'NFM501_Web_Annex_Count__c', rowData,
                        '502抛出异常:' + ex.getMessage() + '\n' + ex.getStackTraceString()
                        + '\n' + rowData.ErrorLog__c +
                        '错误次数已经超过自动收信设定的最大次数,请手动收信');
            }
        }
        update rowData;
        System.debug('+++++++5+++++++' + rowData);
        System.debug('+++++++3+++++++' + iflog502.Log__c);
        System.debug('+++++++2+++++++' + iflog502.ErrorLog__c);
        //如果存入信息超出限制,用省略号代替
        if (iflog502.Log__c.length() > 131072) {
            iflog502.Log__c = iflog502.Log__c.subString(0, 131065) + ' ...';
        }
        if (iflog502.ErrorLog__c.length() > 32768) {
            iflog502.ErrorLog__c = iflog502.ErrorLog__c.subString(0, 32760) + ' ...';
        }
        upsert iflog502;
    }
 
    // 控制文件名长度
    public static String sub_file_name(String file_name) {
        return String.isNotBlank(file_name) && file_name.length() > 240 ? file_name.substring(0, 240) : file_name;
    }
 
    public static void test() {
        integer i = 0;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
 
    }
           
}