Li Jun
2022-04-24 fc8a8cea62e5d248834482a1ade9db6ab0758bf2
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
/**********************************************************************
 * 
 *
 * @url: /services/apexrest/NFM624Rest
 * @data:
 *  {
        
    }
*************************************************************************/
 
@RestResource(urlMapping = '/NFM624/*')
global with sharing class NFM624Rest {
    //add to AWS 回复 start sushanhu 20220225 
    static Boolean SFStatus=true;
    static String SFMessage='';
   //add to AWS 回复 end sushanhu 20220225
    private static final String LOG_TYPE = 'NFM624';
    // global class GeDatasRest {
    //     public GeDatas GeDatas;
    // }
 
    global class GeDatas {
        public NFMUtil.Monitoring Monitoring;
        public GeData[] GeData;
    }
 
    global class GeData {
        public String ContactId; //统一平台ID//Update to aws 存储的AWSContactId 20220225 SUSHANHU
        public String ServiceUserId; //服务平台用户ID
        public String PersonManagementCode; //人员管理编码 
        public String HospitalManagementCode2; //医院平台编码  平台编码 PlatformCode__c
        public String DepartmentManagementCode2; //科室平台编码  平台编码 PlatformCode__c
        public String Name; //姓名
        public String NameEncrypted;//姓名密文 add 20220215
        public String Mobile; //手机号码
        public String MobileEncrypted;//手机号码密文 add 20220215
        public String State; //单位所在省份
        public String City; //市
        public String AccountName; //医院名
        public String RelatedHospital; //关联SFDC医院 客户管理编码(手写) AgentCode_Ext__c
        public String DepartmentClass; //战略科室
        public String DepartmentName; //所属科室
        public String RelatedDepartment; //关联SFDC科室 客户管理编码(手写) AgentCode_Ext__c
        public String Type; //职种
        public String TypeEncrypted;//职种Hash add for pipl 20220316 sushanhu
        public String ContactAddress; //联系地址
        public String ContactAddressEncrypted;//联系地址密文
        public Boolean ForbiddenStatus; //平台用户禁用标识 
        public String RegSource; //注册平台来源
        public Boolean AgentFlag; //经销商标识
        public String ApproverID; //审核人员员工编码
        public String DataId;//aws存储凭据
    }
 
    @HttpPost
    global static void execute() {
 
        // 取得接口传输内容
        String strData = RestContext.request.requestBody.toString();
        GeDatas ges = (GeDatas) JSON.deserializeStrict(strData, GeDatas.class);
        system.debug('ges---'+ges);
 
        if (ges == null) {
            return;
        }
 
        NFMUtil.Monitoring Monitoring = ges.Monitoring;
        if (Monitoring == null) {
            return;
        }
 
        BatchIF_Log__c rowData = NFMUtil.saveRowData(Monitoring, LOG_TYPE, ges.GeData);
        system.debug('String.isBlank(rowData.Log__c) == false?');
        if (String.isBlank(rowData.Log__c) == false) {
            System.debug('进入if方法,成功调用executefuture');
            executefuture(rowData.Id);
        }
 
        // JSONを戻す
        RestResponse res = RestContext.response;
        res.addHeader('Content-Type', 'application/json');
        res.statusCode = 200;
        // String jsonResponse = '{"status": "Success", "Message":""}';
        // res.responseBody = blob.valueOf(jsonResponse);
         //updata response toAWS 20220225 sushanhu start
         NFMUtil.NFMResponse result = NFMUtil.getNFMResponse();
         result.SFStatus=SFStatus;
         result.SFMessage=SFMessage;
         String jsonResponse =JSON.serialize(result);
         system.debug('result---'+jsonResponse);
         res.responseBody = blob.valueOf(jsonResponse);
         //updata response toAWS 20220225 sushanhu end
        return;
    }
 
 
    @future
    global static void executefuture(String rowData_Id) {
        system.debug('进入executefuture方法' + rowData_Id);
        main(rowData_Id);
    }
 
    global static void main(String rowData_Id) {
 
        // Map<String,String> RecordTypeMap = new Map<String,String>();
        // RecordTypeMap.put('','')
//List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName IN ('Department_OTH','Department_GI', 'Department_BF','Department_GYN','Department_GS','Department_URO','Department_ENT','Department_Class_ET') order by DeveloperName desc];        
        Map < string, string > RecordTypeMap = new Map < string, string > (); //存放记录类型Id 
        //for(ID temp : rectDpt){
        //
        //}        
        RecordTypeMap.put('呼吸科', '01210000000QfmR');
        RecordTypeMap.put('其他', '01210000000Qfmb');
        RecordTypeMap.put('妇科', '01210000000QfmM');
        RecordTypeMap.put('普外科', '01210000000QfmH');
        RecordTypeMap.put('泌尿科', '01210000000QfmC');
        RecordTypeMap.put('消化科', '01210000000Qfm7');
        RecordTypeMap.put('耳鼻喉科', '01210000000Qfm2');
        RecordTypeMap.put('ET', '01210000000QemQ');
        Integer batch_retry_max_cnt = Integer.valueOf(System.Label.batch_retry_max_cnt);
        BatchIF_Log__c rowData = [Select Id, Name, Log__c,Is_Error__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,NFM624_Secondary_processing__c  from BatchIF_Log__c where RowDataFlg__c = true and Id =: rowData_Id];
        String logstr = rowData.MessageGroupNumber__c + ' start\n';
        BatchIF_Log__c iflog = new BatchIF_Log__c();
        iflog.Type__c = LOG_TYPE;
        iflog.MessageGroupNumber__c = rowData.MessageGroupNumber__c;
        iflog.Log__c = logstr;
        iflog.ErrorLog__c = '';
        insert iflog;
 
        String rowDataStr = NFMUtil.getRowDataStr(rowData);
        List < GeData > GeDataList = (List < GeData > ) JSON.deserialize(rowDataStr, List < GeData > .class);
        if (GeDataList == null || GeDataList.size() == 0) {
            return;
        }
        //判空
        // for(GeData gda : GeDataList){
        //     string dataComplete = verify(gda);
        //     if(!String.isBlank(dataComplete)){
        //         logstr += dataComplete;
        //         continue;
        //     }
        // }
 
        Savepoint sp = Database.setSavepoint();
        List<string> ApproverIDList = new List <string>();//
        List < string > PersonManagementCodeList = new List < String > (); //人员管理编码List
        List < String > SFDCCodeList = new List < String > (); //关联SFDC医院、科室List
        List < String > ManagementList = new List < String > (); //医院、科室平台编码List
        List < String > StateList = new List < String > ();
        List < String > CityList = new List < String > ();
        List < String > NameList = new List < String >();
        try { //根据传过来的管理编码查询如果可以查询得到结果则更新,查询不到则新增
            List < Gedata > newGeDataList = new List < Gedata > ();
            for (Gedata gedata: GeDataList) {
                string dataComplete = verify(gedata);
                
                if (!String.isBlank(dataComplete)) {
                    logstr += dataComplete;
                    rowData.Is_Error__c = 1;
                    rowData.retry_cnt__c = 0;
                    continue;
                }
                if (!gedata.AgentFlag) {
                    StateList.add(gedata.State);
                    CityList.add(gedata.City);
                    ApproverIDList.add(gedata.ApproverID);
                    if(string.isnotblank(gedata.HospitalManagementCode2)){
                        ManagementList.add(gedata.HospitalManagementCode2);
                    }
                    if(string.isnotblank(gedata.DepartmentManagementCode2)){
                        ManagementList.add(gedata.DepartmentManagementCode2);
                    }
                    //system.debug('AccountCodeList'+AccountCodeList);
                    if (string.isnotblank(gedata.RelatedHospital)) {
                        SFDCCodeList.add(gedata.RelatedHospital);
                    }
                    if (string.isnotblank(gedata.RelatedDepartment)) {
                        SFDCCodeList.add(gedata.RelatedDepartment);
                    }
                }
                if (string.isnotblank(gedata.PersonManagementCode)) {
                    PersonManagementCodeList.add(gedata.PersonManagementCode);
                }
                if(String.isnotBlank(gedata.NameEncrypted)){
                    NameList.add(gedata.NameEncrypted);
                }
                newGeDataList.add(gedata);
                System.debug('ManagementList'+ManagementList);
                System.debug('SFDCCodeList'+SFDCCodeList);
                
            }
            if (newGeDataList.size() > 0) {
                //查询省份Id
                Map < String, String > StateMap = new Map < String, String > (); //省Map
                List < Address_Level__c > statetempList = [select Id, Name from Address_Level__c where Name IN: StateList];
                for (Address_Level__c temp: statetempList) {
                    StateMap.put(temp.Name, temp.Id);
                }
                //查询市区Id 
                Map < String, String > CityMap = new Map < String, String > (); //市Map
                List < Address_Level2__c > citytempList = [select Id, Name from Address_Level2__c where Name IN: CityList];
                for (Address_Level2__c temp: citytempList) {
                    CityMap.put(temp.Name, temp.Id);
                }
 
                //查询医院所有人
                Map<string,String> ownerMap = new Map<string,String>();
                List<User> ownerList = [select Id,Employee_No__c from User where Employee_No__c  IN:ApproverIDList];
                for(User temp : ownerList){
                    ownerMap.put(temp.Employee_No__c,temp.Id);
                }
 
                //查询OCSM营业窗口
                Map<string,Id> OCSMMap = new Map<string,Id>();
                List<OCM_Management_Province__c> OCSMList = [select Id,Name,Window1__c from OCM_Management_Province__c];
                for(OCM_Management_Province__c ocsm : OCSMList){
                    OCSMMap.put(ocsm.name,ocsm.Window1__c);
                }
 
                //查询联系人
                Map < String, Map < String, Contact >> nameMap = new Map < String, Map < String, Contact >> (); //外层战略科室,内层科室名+联系人名
                List < Contact > peopleList = [select Id, 
                                                      Account.Name,
                                                      Department__c, 
                                                      Account.parent.Name, 
                                                      Account.PlatformCode__c, 
                                                      Account.Management_Code__c, 
                                                      Account.parent.Department_Class_Label__c, 
                                                      Name,LastName_Encrypted__c,
                                                      Account.Parent_Management_Code__c, 
                                                      Account.Parent_PlatformCode__c, 
                                                      CManageCode__c, 
                                                      AccountId 
                                                      from Contact 
                                                      where CManageCode__c IN: PersonManagementCodeList 
                                                      OR (Account.Parent_PlatformCode__c IN: ManagementList AND LastName_Encrypted__c IN:NameList)
                                                      OR (Account.Parent_Management_Code__c IN: SFDCCodeList AND LastName_Encrypted__c IN:NameList )
                                                      ORDER BY  MobilePhone ASC,CManageCode__c ASC]; //根据人员管理编码检索联系人
                Map < String, Contact > peopleMap = new Map < String, Contact > (); //联系人
                system.debug('peopleList  '+peopleList);
                for (Contact ct: peopleList) {
                    if (string.isnotblank(ct.Account.parent.Name)) {
                        Map < String, Contact > snameMap = new Map < String, Contact > ();
                        string namekey = ct.Account.Name + ' ' + ct.LastName_Encrypted__c.trim();
                        string nameKey2 = ct.Account.Management_Code__c + ' ' + ct.LastName_Encrypted__c.trim();
                        // snameMap.put(namekey,ct);
                        if (nameMap.containsKey(ct.Account.parent.Name)) {
                            snameMap = nameMap.get(ct.Account.parent.Name);
                        }
                        snameMap.put(namekey, ct);
                        snameMap.put(nameKey2,ct);
                        snameMap.put(ct.LastName_Encrypted__c.trim(), ct);
                        nameMap.put(ct.Account.parent.Name, snameMap);
                        system.debug('snameMap'+snameMap);
                    }
                    peopleMap.put(ct.CManageCode__c, ct);
                    System.debug('peopleMap'+peopleMap);
                    // peopleMap.put(ct.Name, ct);
                    //string temp = ct.Name + ct.Account.parent.Department_Class_Label__c;
                    // peopleMap.put(temp, ct);
                }
 
                //查询医院科室
                //List<Account> AccountList = [select Id,PlatformCode__c,PlatformCode__c,Department_Class_Label__c,Name,Management_Code__c,Is_Active__c,AgentCode_Ext__c,ParentId,Parent.ParentId from Account where PlatformCode__c IN :HpCodeList OR parent.PlatformCode__c IN :HpCodeList OR PlatformCode__c IN :HpCodeList OR  Management_Code__c IN :RelatedHospitalList OR Parent_Management_Code__c IN :RelatedHospitalList];
                //List < Account > AccountList = [select Id, PlatformCode__c, Department_Class_Label__c, Name, Management_Code__c, Is_Active__c, AgentCode_Ext__c, ParentId, Parent.ParentId from Account where Management_Code__c IN :AccountCodeList OR Parent_Management_Code__c IN :AccountCodeList OR PlatformCode__c IN :AccountCodeList OR Parent.Parent.PlatformCode__c IN :AccountCodeList];
                List < Account > AccountList = [select Id, 
                                                       PlatformCode__c, 
                                                       Is_Active_Formula__c, 
                                                       Department_Class_Label__c,
                                                       OCM_man_province_HP__c, 
                                                       Name, Management_Code__c, 
                                                       Is_Active__c, AgentCode_Ext__c, 
                                                       ParentId, 
                                                       Parent.ParentId, 
                                                       OwnerId
                                                       from Account 
                                                       where Parent_PlatformCode__c IN: ManagementList 
                                                       OR Parent_Management_Code__c IN: SFDCCodeList];
                Map <String,Account> DepartmentClassMap = new Map <String,Account>();//战略科室Map
                Map < String, Account > AccountMap = new Map < String, Account > (); //医院科室
                for (Account ac: AccountList) {
                    AccountMap.put(ac.Management_Code__c, ac);
                    if(string.isNotBlank(ac.Department_Class_Label__c)){
                        DepartmentClassMap.put(ac.Department_Class_Label__c,ac);
                    }
                    if (string.isNotBlank(ac.PlatformCode__c)) {
                        AccountMap.put(ac.PlatformCode__c, ac);
                    }
                }
                system.debug('AccountMap       '+AccountMap);
                System.debug('DepartmentClassMap     '+DepartmentClassMap);
 
 
                //客户List(更新用)
                List < Account > upsertAccountList = new List < Account > ();
                //联系人List(更新用) 
                List < Contact > upsertContactList = new List < Contact > ();
 
                for (Gedata gedata: newGeDataList) {
                    logstr += gedata.Name;
                    logstr += gedata.AccountName;
                    logstr += gedata.DepartmentClass;
                    logstr += gedata.DepartmentName;
 
 
                    string comefrom = gedata.RegSource == '1' ? '智慧医疗' : (gedata.RegSource == '2' ? '服务客户端' :'医拓网'); //平台来源
 
                    //新建医院
                    Account hp = new Account();
 
                    //新建联系人
                    Contact ct = new Contact();
                    if(gedata.AgentFlag){
                        //ct.FirstName = ''; 2022-4-7 yjk 注释掉
                        ct.UnifiedI_Contact_ID__c = gedata.ContactId; //统一平台Id
                        ct.ServicePlatformCode__c = gedata.ServiceUserId; //服务平台用户Id
                        //ct.CManageCode__c = gedata.PersonManagementCode;//人员管理编码
                    }else{
                        ct.LastName = gedata.Name;
                        ct.FirstName = '';
                        ct.Type__c = gedata.Type;
                        ct.Type_Encrypted__c = gedata.TypeEncrypted;//add for pipl sushanhu 20220316
                        ct.MobilePhone = gedata.Mobile;
                        ct.RegSource__c = comefrom;
                        ct.UnifiedI_Contact_ID__c = gedata.ContactId; //统一平台Id
                        // //UPDATE TO AWS 存储的统一平台id
                        // ct.AWS_UnifiedI_Contact_ID__c=gedata.ContactId; //统一平台Id
                        ct.ServicePlatformCode__c = gedata.ServiceUserId; //服务平台用户Id
                        ct.Contact_address__c = gedata.ContactAddress;//联系地址
                        ct.Contact_address_Encrypted__c=gedata.ContactAddressEncrypted;//联系地址密文 add 20220215
                        ct.Platform_disabled_representation__c = gedata.ForbiddenStatus;//平台禁用标识
                        //ct.IsFromPlatform__c = true; //来自智慧医疗创建
                        ct.Ignore_Same_Name__c = true; //不是重复的客户名 
                        //ct.SendToComPlat__c = true;
                        ct.AWS_Data_Id__c =gedata.DataId;//add 20220215 aws存储凭证
                        ct.MobilePhone_Encrypted__c =gedata.MobileEncrypted;//add 20220215 手机密文
                        ct.LastName_Encrypted__c =gedata.NameEncrypted;//add 20220215 姓名密文
                    }
                    
 
                    String personCode = gedata.PersonManagementCode;
 
                    Map < String, Contact > contactMap = new Map < String, Contact > ();//用于暂存更新联系人
                    //1经销商true
                    if (gedata.AgentFlag == true) {
                        //更新经销商联系人
                        if (!peopleMap.containsKey(personCode)) {
                            rowData.Is_Error__c = 1;
                            logstr += 'error:人员管理编码 [PersonManagementCode] 对应的联系人不存在,此条数据跳过';
                            continue;
                        }
                        ct.id = peopleMap.get(personCode).id;
                        upsertContactList.add(ct);
                    } else { //经销商flase
                        string HospitalCode = string.isNotBlank(gedata.RelatedHospital) ? gedata.RelatedHospital : gedata.HospitalManagementCode2; //RelatedHospital有值用RelatedHospital,否则用HospitalManagementCode2
                        //医院存在
                        System.debug('AccountMap.containsKey(HospitalCode      )'+AccountMap.containsKey(HospitalCode));
                        if (AccountMap.containsKey(HospitalCode)) {
                            //新建科室
                            Account dpt = new Account(); 
                            Account hospital = AccountMap.get(HospitalCode);
                            dpt.PlatformCode__c = gedata.DepartmentManagementCode2;
                            dpt.Hospital__c = hospital.Id;
                            
                            //判断医院是否有效
                            if ('有效'.equals(hospital.Is_Active_Formula__c)) { 
                                string DepartmentCode = string.isNotBlank(gedata.RelatedDepartment) ? gedata.RelatedDepartment : gedata.DepartmentManagementCode2;
                                string DepartmentClasskey = gedata.AccountName + ' ' + gedata.DepartmentClass;
                                system.debug('DepartmentClasskey------->'+DepartmentClasskey);
                                //科室存在
                                if (AccountMap.containsKey(DepartmentCode)) {
                                    //人员管理编码存在
                                    ct.AccountId = AccountMap.get(DepartmentCode).id;//科室确定,将联系人挂在该科室下
                                    ct.OwnerId = AccountMap.get(DepartmentCode).OwnerId;//可是确定,将将联系人所有人的值赋为科室所有人
                                    system.debug('personCode' + personCode);
                                    if (string.isnotblank(personCode)) {
                                        system.debug('人员管理编码存在');
                                        //联系人存在 1 完成
                                        if (peopleMap.containsKey(personCode)) {
                                            system.debug('1逻辑 有医院 有科室 有人员管理编码且查找有此人 动作:更新');
                                            ct.Id = peopleMap.get(personCode).Id;
                                            upsertContactList.add(ct);
                                        } else { //联系人不存在 2 完成
                                            system.debug('2逻辑 有医院 有科室 有人员管理编码但查找无此人 动作:报错');
                                            rowData.Is_Error__c = 1;
                                            logstr += 'error:人员管理编码 [PersonManagementCode] 对应的联系人不存在,此条数据跳过';
                                            continue;
                                        }
                                    } else { //人员管理编码不存在
                                        //搜索人名/且在当前战略科室科室下
                                        System.debug('人员管理编码不存在');
                                        string namekey = gedata.RelatedDepartment + ' ' + gedata.NameEncrypted;
                                        system.debug('DepartmentClasskey     =    '+DepartmentClasskey);
                                        system.debug('nameMap22222222    '+nameMap);
                                        if (nameMap.containskey(DepartmentClasskey)) { // 6 完成 更新操作
                                            Map < String, Contact > sMap = nameMap.get(DepartmentClasskey);
                                            System.debug('sMap'+sMap);
                                            System.debug('sMap.containsKey(namekey)'+sMap.containsKey(namekey));
                                            if (sMap.containsKey(namekey)) {
                                                system.debug('Id赋值'+sMap);
                                                ct.id = sMap.get(namekey).id;
                                            } else if(sMap.containsKey(gedata.NameEncrypted)){
                                                ct.id = sMap.get(gedata.NameEncrypted).id;
                                            }else { // 7 完成
                                                system.debug('7逻辑 无人员管理编码 有医院 有科室 人名查找无 动作:新建联系人 ');
                                            }
                                        }
                                        upsertContactList.add(ct);
                                    }
                                    dpt.Id = AccountMap.get(DepartmentCode).Id;
                                    upsertAccountList.add(dpt);
                                } else { //科室不存在 
                                    //为科室赋值
                                    dpt.Name = gedata.DepartmentName;
                                    dpt.RecordTypeId = RecordTypeMap.get(gedata.DepartmentClass);
                                    dpt.Department_Class__c = DepartmentClassMap.get(gedata.DepartmentClass).Id;
                                    dpt.ParentId = DepartmentClassMap.get(gedata.DepartmentClass).Id;
                                    if (string.isnotblank(personCode)) { //人员管理编码存在
                                        if (peopleMap.containsKey(personCode)) { //人员管理编码查找有值 3 完成
                                            System.debug('3逻辑');
                                            //新建科室+新建联系人, 
                                            ct.id = peopleMap.get(personCode).id;
                                            contactMap.put(Gedata.DepartmentManagementCode2, ct);
                                            system.debug('contactMap        '  +  contactMap);
                                            dpt.Department_Name__c = gedata.DepartmentName;
                                            upsertAccountList.add(dpt);
                                            //upsertContactList.add(ct);
                                        } else { //人员管理编码查找无值 4 完成
                                            system.debug('4逻辑');
                                            rowData.Is_Error__c = 1;
                                            logstr += 'error:人员管理编码 [PersonManagementCode] 对应的联系人不存在,此条数据跳过';
                                            continue;
                                        }
                                    } else { //人员管理编码不存在
                                        system.debug('人员管理编码不存在');
                                        string namekey = gedata.DepartmentName+ ' ' + gedata.NameEncrypted;
                                        //string namekey = gedata.AccountName + ' ' + gedata.DepartmentName + ' ' + gedata.DepartmentClass + ' ' + gedata.Name;
                                        system.debug('DepartmentClasskey'+DepartmentClasskey);
                                        system.debug('351nameMap     '+nameMap);
                                        if (nameMap.containsKey(DepartmentClasskey)) { //查找是否存在该战略科室 
                                            System.debug('人员管理编码不存在的情况下战略科室存在');
                                            system.debug('战略科室存在' + nameMap.get(DepartmentClasskey));
                                            Map < String, Contact > sMap = nameMap.get(DepartmentClasskey);
                                            system.debug('namekey'+namekey);
                                            system.debug('sMap.containsKey(namekey)'+sMap.containsKey(namekey));
                                            if (sMap.containsKey(namekey)) { //查找存在 8 完成
                                                system.debug('8逻辑');
                                                system.debug('该联系人存在');
                                                ct.id = sMap.get(namekey).id;
                                                //upsertAccountList.add(dpt);//可以注释
                                                upsertContactList.add(ct);
                                                // if(sMap.containsKey(gedata.Name)){
                                                //     ct.Id = sMap.get(gedata.Name).Id;
                                                // }
                                            }else if(sMap.containsKey(gedata.NameEncrypted)){
                                                system.debug('1234567890');
                                                ct.Id = sMap.get(gedata.NameEncrypted).Id;
                                                //upsertAccountList.add(dpt);
                                            }
                                            system.debug('contactMap        '  +  contactMap);
                                            System.debug('9逻辑');
                                            contactMap.put(Gedata.DepartmentManagementCode2, ct);
                                            system.debug('contactMap        '  +  contactMap);
                                            //upsertContactList.add(ct);
                                        }
                                        system.debug('创建联系人和科室');
                                        dpt.Department_Name__c = gedata.DepartmentName;
                                        upsertAccountList.add(dpt);
                                        contactMap.put(Gedata.DepartmentManagementCode2, ct);
                                    }
                                }
                            }else if('无效'.equals(hospital.Is_Active_Formula__c)){
                                rowData.Is_Error__c = 1;
                                logstr += 'error:该医院无效,此条数据跳过';
                                continue;
                            }else{
                                logstr += '该医院处于草案中,请等待审批完成';
                                rowData.NFM624_Secondary_processing__c = false;
                                continue;
                            }
                        } else { //医院不存在
                            system.debug('新建医院 逻辑5逻辑10 无论人员管理编码是否有值都需要新建医院');
                            hp.Name = gedata.AccountName; //医院名赋值  
                            if(ownerMap.containsKey(gedata.ApproverID)){
                                hp.OwnerId = ownerMap.get(gedata.ApproverID);//审核人员员工编码赋值给用户所有人
                            }
                            hp.RecordTypeId = '01210000000QemG';    
                            hp.OCM_Category__c = 'L';
                            hp.PlatformCode__c = gedata.HospitalManagementCode2;
                            hp.State_Master__c = StateMap.get(gedata.State); //  省
                            hp.City_Master__c = CityMap.get(gedata.City); //     市
                            hp.Hospital_Source__c = '智慧医疗';
                            upsertAccountList.add(hp);
                            System.debug('upsertAccountList'+upsertAccountList);
                            rowData.NFM624_Secondary_processing__c = false;//数据需要二次处理标记
                        }
                    }
                    system.debug('upsertContactList      ' + upsertContactList);
                    system.debug('upsertAccountList      ' + upsertAccountList);
                    if (upsertAccountList.size() > 0) {
                        ControllerUtil.EscapeNFM001Trigger = true;
                        StaticParameter.EscapeNFM001AgencyContractTrigger2 = true;
                        system.debug('upsertAccountList                           ' + upsertAccountList);
                        upsert upsertAccountList;
                        system.debug('SFDCCodeList'+SFDCCodeList);
                        system.debug('SFDCCodeList'+ManagementList);
                        List < Account > List1 = [select Id,RecordTypeId,Is_Active__c,PlatformCode__c,OCM_man_province_HP__c,OwnerId from Account where PlatformCode__c In: SFDCCodeList OR PlatformCode__c IN: ManagementList];//客户所有人修改11.18 11.35 未完成
                        system.debug('List1'+List1);
                        List<Account> updateHPList = new  List<Account>(); 
                        for (Account ac: List1) {
                            Contact c = new contact();
                            system.debug('ac.PlatformCode__c'+ac.PlatformCode__c);
                            system.debug('ContactMap'+ContactMap);
                            system.debug('ContactMap.containsKey(ac.PlatformCode__c)'+ContactMap.containsKey(ac.PlatformCode__c));
                            if(ContactMap.containsKey(ac.PlatformCode__c)){
                                c = contactMap.get(ac.PlatformCode__c);
                                if(string.isblank(c.Id)|| c.AccountId != ac.Id){
                                    c.Accountid = ac.id;
                                }
                                c.OwnerId = ac.OwnerId;
                                upsertContactList.add(c);
                                system.debug('OwnerId'+ac.OwnerId);
                                system.debug('upsertContactList'+upsertContactList);
                            }
 
                            if(ac.RecordTypeId == '01210000000QemGAAS' && ac.Is_Active__c == '草案中' && string.isNotBlank( OCSMMap.get(ac.OCM_man_province_HP__c))){
                                ac.OwnerId = OCSMMap.get(ac.OCM_man_province_HP__c);
                                updateHPList.add(ac);
                            }
                            if(updateHPList.size()>0){
                                update updateHPList;
                            }
                        }
                    }
                    
                    upsert upsertAccountList;
                    system.debug('upsertContactList11111111111      '+upsertContactList);
                    if(upsertContactList.size()>0){
                        upsert upsertContactList;
                        rowData.NFM624_Secondary_processing__c = true;//执行完之后修改为true,不需要再次执行
                        //2022/2/10 修改 在新建联系人之后手动执行NFM620RestBatch NFM623RestBatch
                        // Database.executeBatch(new NFM620RestBatch(), 1);
                        // Database.executeBatch(new NFM623RestBatch(), 1);
                        supplement(gedata.ContactId);
                    }
                }
            }
            rowData.retry_cnt__c = 0;
            
            logstr += '\nend';
        } catch (Exception ex) {
            // エラーが発生した場合
            Database.rollback(sp);
            System.debug(Logginglevel.ERROR, LOG_TYPE + '_' + rowData.MessageGroupNumber__c + ':' + ex.getMessage());
            System.debug(Logginglevel.ERROR, LOG_TYPE + '_' + rowData.MessageGroupNumber__c + ':' + ex.getStackTraceString());
            logstr += '\n' + ex.getMessage();
            iflog.ErrorLog__c = ex.getMessage() + '\n' + ex.getStackTraceString() + '\n' + iflog.ErrorLog__c;
            if (rowData.retry_cnt__c == null) rowData.retry_cnt__c = 0;
            if (rowData.retry_cnt__c < batch_retry_max_cnt) {
                rowData.retry_cnt__c++;
                LogAutoSendSchedule.assignOneMinute();
            }
            if (rowData.retry_cnt__c >= batch_retry_max_cnt) {
                rowData.ErrorLog__c = ex.getMessage() + '\n' + ex.getStackTraceString() + '\n' + rowData.ErrorLog__c + '错误次数已经超过自动收信设定的最大次数,请手动收信';
            }
        }
        update rowData;
        iflog.Log__c = logstr;
        if (iflog.Log__c.length() > 131072) {
            iflog.Log__c = iflog.Log__c.subString(0, 131065) + ' ...';
        }
        if (iflog.ErrorLog__c.length() > 32768) {
            iflog.ErrorLog__c = iflog.ErrorLog__c.subString(0, 32760) + ' ...';
        }
        update iflog;
    }
 
    private static String verify(GeData gda) {
        String result = '';
        if (gda.AgentFlag) {
            if (string.isblank(gda.ContactId)) {
                result += 'DataError: ContactId 统一平台ID为空\n';
            }
            if (string.isblank(gda.PersonManagementCode)) {
                result += 'DataError:PersonManagementCode 人员管理编码为空\n';
            }
            if (string.isblank(gda.ServiceUserId)) {
                result += 'ServiceUserId 服务平台用户Id为空\n';
            }
        }else{
            if (string.isblank(gda.ContactId)) {
                result += 'DataError: ContactId 统一平台ID为空\n';
            }
            if (gda.AgentFlag == null) {
                result += 'DataError:经销商标识为空\n';
            }
            if (string.isblank(gda.Name)) {
                result += 'DataError:姓名为空\n';
            }
            if (string.isblank(gda.Mobile)) {
                result += 'DataError:手机号为空\n';
            }
            if (string.isblank(gda.RegSource)) {
                result += 'DataError:注册平台来源为空\n';
            }
            if(string.isBlank(gda.RelatedHospital) && String.isBlank(gda.HospitalManagementCode2) && String.isBlank(gda.ApproverID)){
                result += 'DataError: 新建医院时,审核人员员工编码[ApproverID]必填\n';
            }
            if (string.isblank(gda.HospitalManagementCode2)) {
                result += 'DataError:医院平台编码为空\n';
            }
            if (string.isblank(gda.DepartmentManagementCode2)) {
                result += 'DataError:科室平台编码为空\n';
            }
            if (string.isblank(gda.State)) {
                result += 'DataError:省字段为空\n';
            }
            if (string.isblank(gda.City)) {
                result += 'DataError:市字段为空\n';
            }
            if (string.isblank(gda.AccountName)) {
                result += 'DataError:医院名为空\n';
            }
            if (string.isblank(gda.DepartmentName)) {
                result += 'DataError:所属科室为空\n';
            }
            if (string.isblank(gda.Type)) {
                result += 'DataError:职种分类为空\n';
            }
        }
        return result;
    }
    
    private static void supplement (String ContactId){
        //把620Batch 和 623Batch 搬过来
        //补充询问单部分 start
        //更新用
        List<CampaignMember__c> campaignMemberList = new List<CampaignMember__c>();
        List<Inquiry_form__c> inquiryFormList = new List<Inquiry_form__c>();
        //根据统一用户Id查询询问单
        List<Inquiry_form__c> InquiryList = [select Id,Inquiry_No__c,ContactId__c,Hospital_Name__c,Department_Class__c,Contact_Name__c from Inquiry_form__c where ContactId__c = :ContactId];
        system.debug('InquiryList'+InquiryList);
        //根据统一用户Id查询联系人
        List<Contact> contactList = [select Id,Name,UnifiedI_Contact_ID__c,Account.Id,Strategic_dept_Class__c,AccountId,Account.Name,Strategic_dept_Class__r.OwnerId from Contact where UnifiedI_Contact_ID__c = :ContactId];
        system.debug('contactList' + contactList);
 
        if(InquiryList.size()>0){
            //统一用户ID存入List、询问单ID存Map
            Map<String, String> inquiryMap = new Map<String, String>();
            //List<String> contactIdList = new List<String>();
            for (Inquiry_form__c inquiryInfo : inquiryList) {
                //contactIdList.add(inquiryInfo.ContactId__c);
                inquiryMap.put(inquiryInfo.ContactId__c, inquiryInfo.Id);
            }
            if (contactList.size() >0 ) {
                for (Contact contactInfo : contactList) {
                    Inquiry_form__c inquiry = new Inquiry_form__c();
                    inquiry.Id = inquiryMap.get(contactInfo.UnifiedI_Contact_ID__c);      //ID
                    inquiry.Hospital_Name__c = contactInfo.Account.Id;                    //科室名   
                    inquiry.Department_Class__c = contactInfo.Strategic_dept_Class__c;    //战略科室分类
                    inquiry.Contact_Name__c = contactInfo.Id;                             //客户姓名
                    inquiry.OwnerId = contactInfo.Strategic_dept_Class__r.OwnerId;        //所有人
                    inquiryFormList.add(inquiry);
                }
            }
            system.debug('inquiryFormList=========>'+inquiryFormList);
            if(inquiryFormList.size()>0){
                update inquiryFormList;
            }
        }
        //补充询问单部分 end
        
        //补充学会部分 start
        //List<CampaignMember__c> campaignMemberList = new List<CampaignMember__c>();
        List<CampaignMember__c> capMemList = [select Id,Contact_ID__c,Campaign__c,Campaign__r.Num__c,ViewContactId__c from CampaignMember__c where Contact_ID__c = :ContactId];
        if(capMemList.size() > 0){
            system.debug('capMemList=================>'+capMemList);
            Map<String, String> capMemMap = new Map<String, String>(); 
            //统一用户ID存入List,参会人员记录ID存入MAP
            //List<String> contactIdList = new List<String>();
            for (CampaignMember__c capMemInfo : capMemList) {
                //contactIdList.add(capMemInfo.ViewContactId__c);
                capMemMap.put(capMemInfo.ViewContactId__c,capMemInfo.Id);
            }
            system.debug('capMemMap======>'+capMemMap);
            //List<Contact> contactList = [select Id,Name,UnifiedI_Contact_ID__c,Account.Id,Strategic_dept_Class__c,AccountId,Account.Name,Strategic_dept_Class__r.OwnerId,Id from Contact where UnifiedI_Contact_ID__c = ContactId];
            if (contactList.size() > 0) {
                for (Contact contactInfo : contactList) {
                    CampaignMember__c camMemberInfo = new CampaignMember__c();
                    camMemberInfo.Id = capMemMap.get(contactInfo.UnifiedI_Contact_ID__c);
                    camMemberInfo.Contact_ID__c = contactInfo.Id;                   //参会人员ID
                    camMemberInfo.Department_ID__c = contactInfo.AccountId;         //医院科室
                    camMemberInfo.Department__c = contactInfo.Account.Name;         //医院科室(文本)
                    camMemberInfo.Contact__c = contactInfo.Name;                    //客户人员姓名
                    campaignMemberList.add(camMemberInfo);
                }
            }
            system.debug('campaignMemberList================>'+campaignMemberList);
            if (campaignMemberList.size() > 0) {
                System.debug('--------1-------');
                update campaignMemberList;
            }
        }
        //补充学会部分 end
    }
}