黄千龙
2022-04-08 01f207d979d6be17c8cdec293feab48828c0ec3e
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
//BEFORE -- 设置GI/SP 助理和5个战略科室所有人的共享
//NOW ----- 设置GI/SP助理 和5个医院下所有战略科室所有人的共享
// { 1. 根据OCSM管理省 给GI和SP助理  信息负责人(助理),行政窗口 ,备品窗口 赋值 信息负责人助理设置GI助理
//3.给关联医院的所有启用战略科室下的所有启用所有人设置销售共享 ,给招投标项目的行政窗口和备品窗口设置共享 ,给招投标项目的GI/SP助理设置共享}
 
//设置GI/SP/销售助理的值 和共享
public without sharing class TenderInformationHandler extends Oly_TriggerHandler {
    private Map<Id, Tender_information__c> newMap;
    private Map<Id, Tender_information__c> oldMap;
    private List<Tender_information__c> newList;
    private List<Tender_information__c> oldList;
 
 
    public TenderInformationHandler() {
        this.newMap = (Map<Id, Tender_information__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Tender_information__c>) Trigger.oldMap;
        this.newList = (List<Tender_information__c>) Trigger.new;
        this.oldList = (List<Tender_information__c>) Trigger.old;
    }
 
    //根据新建的OCSM管理省字段,根据OCSM管理省对象GI,SP助理, 行政窗口 ,备品窗口  给这个招投标项目的GI,SP助理,行政窗口,备品窗口 ,信息负责人(助理)赋值;
    protected override void beforeInsert() {
        AssignValueToAssistant();
        // add 【委托】P-招标项目-手动创建的招标项目增加必填字段 2021/11/03 fxk Star
        for (Tender_information__c ten : newList) {
            if (ten.RecordTypeId == '0121m000000bKzA' || ten.RecordTypeId == '01210000000VLZ8') {
                ten.OwnerId = '00510000000gmxH';
                if(ten.Hospital__c != null
                    || ten.Hospital1__c != null
                    || ten.Hospital2__c != null
                    || ten.Hospital3__c != null
                    || ten.Hospital4__c != null){
                    ten.IsRelateProject__c = '是';
                    ten.relativeTime__c = Datetime.now();
                }
                updateTenOwner();
            }
        }
        // add 【委托】P-招标项目-手动创建的招标项目增加必填字段 2021/11/03 fxk End
    }
 
    protected override void beforeUpdate() {
        //反逻辑删除 fxk
        updateTenDel();
        AssignValueToAssistant();
        if (!StaticParameter.EscapeOtherUpdateTenOwner) {
            updateTenOwner();
        }
                
    }
 
    protected override void afterInsert() {
        // 根据上面赋值的GI、SP助理,行政窗口,备品窗口,更新共享,共享原因不一样
        addShare();
        sednMessage();
        // updateTenOwner();
    }
    protected override void afterUpdate() {
        addShare();
        sednMessage();
        updateWin();
 
        //2022-3-29 yjk SWAG-CCL6R7
        updateOpportunity();
    }
 
 
    //2022-3-29 yjk SWAG-CCL6R7
    private void updateOpportunity(){
        for(Tender_information__c newOne : newList){
            Tender_information__c oldOne = oldMap.get(newOne.id);
            if(oldOne.ResultDate__c == null && newOne.ResultDate__c != null){
                List<Opportunity> oppList = [select id,Closing_Bid_Date__c  from Opportunity where Bidding_Project_Name_Bid__c = :newOne.id];
                if(oppList.size() > 0){
                    for(Opportunity opp : oppList){
                        opp.Closing_Bid_Date__c = newOne.ResultDate__c;
                    }
                    update oppList;
                }
            }
        }
    }
 
    public void sednMessage() {
        //规则条件
        //ISCHANGED(InfoType__c) && Text(InfoType__c) = '3:结果' && (Text(IsBid__c) = '是' || ( Text(IsBid__c) = '否' && OpportunityNum__c != null && OpportunityNum__c > 0) )
        // 1)没有关联询价,确认相关性,那么就发给GI和SP助理;
        // 2)关联询价后,询价的所有人、项目所有人,GI和SP助理,收邮件;
        //关联询价 发给询价所有人 招标项目所有人
        //没有关联询价并且做了相关性确认 发给招标项目所有人
 
        //1.判断是否符合大条件 拿到招标id
        Set<Id> zbIds = new Set<Id>();
        //拿到招标id , 招标所有人 GI/SP助理 的邮件地址
        Map<Id, Set<String>> zbOppMap = new Map<Id, Set<String>>();
        List<Tender_information__c> tlist = new List<Tender_information__c>();
 
        for (Tender_information__c ten : newList) {
            // if (ten.InfoType__c == '3:结果' &&
            //     (ten.IsBid__c == '是' || (ten.IsBid__c == '否' && ten.OpportunityNum__c != null && ten.OpportunityNum__c > 0))
            //     && (Trigger.isInsert || (Trigger.isUpdate && oldMap.get(ten.Id).InfoType__c != '3:结果'))) {
            //     zbIds.add(ten.Id);
            // }
            // 判断条件修改
            if (ten.InfoType__c == '3:结果' && (Trigger.isInsert || (Trigger.isUpdate && oldMap.get(ten.Id).InfoType__c != '3:结果'))) {
                // 如果招标所有人为系统管理员(OlympusSystem)时需要跳过
                if (ten.Id != '00510000000gmxH') {
                    zbIds.add(ten.Id);
                }
            }
        }
        if (zbIds.size() > 0) {
            tlist = [select Id, GI_assistant__r.Email, SP_assistant__r.Email, Hospital__r.Name, BudgetAmount__c, Name, department__r.Name, OpenBidingTime__c, Owner.Alias, Owner.Email from Tender_information__c where id in :zbIds];
            for (Tender_information__c ten : tlist) {
                if (!zbOppMap.containsKey(ten.Id)) {
                    zbOppMap.put(ten.Id, new Set<String>());
                }
                zbOppMap.get(ten.Id).add(ten.Owner.Email);
 
            }
            //取得询价
            // 招标-询价关联修改 20210817 start
            // List<Opportunity> oppList = new List<Opportunity>();
            // oppList = [select id,Owner.Email,Bidding_Project_Name_Bid__c,Bidding_Project_Name_Bid__r.owner.Email from Opportunity where Bidding_Project_Name_Bid__c in :zbIds];
            // if (oppList.size() > 0) {
            //     for (Opportunity opp : oppList) {
            //         // 能走到这说明 询价的所有人、项目所有人 也得发邮件
            //         if (!zbOppMap.containsKey(opp.Bidding_Project_Name_Bid__c)) {
            //             zbOppMap.put(opp.Bidding_Project_Name_Bid__c, new Set<String>());
            //         }
            //         zbOppMap.get(opp.Bidding_Project_Name_Bid__c).add(opp.Owner.Email);
            //         // zbOppMap.get(opp.Bidding_Project_Name_Bid__c).add(opp.Bidding_Project_Name_Bid__r.owner.Email);
            //     }
            // }
            List<Tender_Opportunity_Link__c> link_list = [select id, Tender_information__c, Opportunity__r.Owner.Email from Tender_Opportunity_Link__c where Tender_information__c in :zbIds];
            if (link_list != null && link_list.size() > 0) {
                for (Tender_Opportunity_Link__c link : link_list) {
                    // 能走到这说明 询价的所有人、项目所有人 也得发邮件
                    if (!zbOppMap.containsKey(link.Tender_information__c)) {
                        zbOppMap.put(link.Tender_information__c, new Set<String>());
                    }
                    zbOppMap.get(link.Tender_information__c).add(link.Opportunity__r.Owner.Email);
                }
            }
            // 招标-询价关联修改 20210817 end
        }
 
        if (zbOppMap.size() > 0) {
            if (tlist.size() > 0) {
                List<Messaging.SingleEmailMessage> sendMails = new List<Messaging.SingleEmailMessage>();
 
                for (Tender_information__c ten : tlist) {
                    if (zbOppMap.containsKey(ten.Id)) {
                        String body = '';
                        String title = '';
                        String BudgetAmount = ten.BudgetAmount__c == null ? '' : ten.BudgetAmount__c + '';
                        String HospitalName = ten.Hospital__c == null ? '' : ten.Hospital__r.Name;
                        String departmentName = ten.department__c == null ? '' : ten.department__r.Name;
                        String OpenBidingTime = ten.OpenBidingTime__c == null ? '' : ten.OpenBidingTime__c.format();
 
                        title = '中标结果:' + ten.Hospital__r.Name + ' 预算金额' + BudgetAmount + '已有中标结果请确认';
 
                        body += '项目名:' + ten.Name + '<br/>';
                        body += '预算金额:' + BudgetAmount + '<br/>';
                        body += '医院:' + HospitalName + '<br/>';
                        // body += '战略科室:'+departmentName + '<br/>';
                        body += '中标日:' + OpenBidingTime + '<br/>';
                        body += '主担当:' + ten.Owner.Alias + '<br/>';
                        body += '招投标链接: <br/>';
                        body += '<a href="' + URL.getSalesforceBaseUrl().toExternalForm() + '/' + ten.Id  + '">' + URL.getSalesforceBaseUrl().toExternalForm() + '/' + ten.Id + '</a><br/>';
                        // body += '收件人: '+zbOppMap.get(ten.Id);
 
                        List<String> toMailList = new List<String>();
                        // List<String> toccList = new List<String>();
                        // emas.add(UserInfo.getUserEmail());
                        //收信人
                        toMailList.addAll(zbOppMap.get(ten.Id));
                        // if (toMailList.contains(null)) {
                        // toMailList.remove(null);
                        // }
                        // toMailList.add('rentongxiao@prec-tech.com');
                        //抄送人
                        // toccList.add('gzw@prec-tech.com');
                        // toccList.add('rentongxiao@prec-tech.com');
                        Messaging.SingleEmailMessage messageNEW = new Messaging.SingleEmailMessage();
                        messageNEW.subject = title;
                        //messageNEW.plainTextBody = body;
                        messageNEW.htmlBody = body;
                        messageNEW.setCharset('UTF-8');
                        messageNEW.toAddresses = toMailList;
                        // messageNEW.ccAddresses = toccList;
                        sendMails.add(messageNEW);
                    }
                }
 
                if (sendMails.size() > 0) {
                    for (Messaging.SingleEmailMessage mc : sendMails) {
                        List<Messaging.SingleEmailMessage> tempsendMails = new List<Messaging.SingleEmailMessage>();
                        tempsendMails.add(mc);  
                        Messaging.SendEmailResult[] results = null;                      
                        if (!NFMUtil.isSandbox()) {
                            results = messaging.sendEmail(tempsendMails);
                        }
                        if (results != null && results.size() > 0) {
                            for (Integer i = 0; i < results.size(); i++) {
                                if (results[i].success == false) {
                                    System.debug('邮件:::' + mc.getSubject() + '发送失败');
                                } else {
                                    System.debug('邮件:::' + mc.getSubject() + '发送成功');
                                }
                            }
                        }
                    }
                }
            }
        }
    }
 
 
 
    public void addShare() {
        //1.获得医院
        Set<Id> hpIdsets = new Set<Id>();
        for (Tender_information__c info : newList) {
            if (info.Hospital__c != null) {
                hpIdsets.add(info.Hospital__c);
            }
            if (info.Hospital1__c != null) {
                hpIdsets.add(info.Hospital1__c);
            }
            if (info.Hospital2__c != null) {
                hpIdsets.add(info.Hospital2__c);
            }
            if (info.Hospital3__c != null) {
                hpIdsets.add(info.Hospital3__c);
            }
            if (info.Hospital4__c != null) {
                hpIdsets.add(info.Hospital4__c);
            }
 
        }
 
        if (hpIdsets.contains(null)) {
            hpIdsets.remove(null);
        }
 
        List<Account> accList = new List<Account>();
        if (hpIdsets.size() > 0) {
            //获取医院下所有启用战略科室的启用所有人
            //取得战略科室:
            // accList = [select id,OwnerId,ParentId,OwnerIsActive__c from Account where Parent.RecordType_DeveloperName__c = 'HP'
            //                                          AND OwnerIsActive__c = true
            //                                          AND ParentId  in :hpIdsets];
            //获取医院上的主担当
            accList = [select id, GI_Main__c, GI_Main__r.IsActive, BF_owner__c, BF_owner__r.IsActive,
                       ET_owner__c, ET_owner__r.IsActive, SP_Main__c, SP_Main__r.IsActive, URO_owner_ID__c, URO_owner_ID__r.IsActive,
                       GYN_owner__c, GYN_owner__r.IsActive, ENT_owner_ID__c, ENT_owner_ID__r.IsActive, Energy_LeaderStr__c
                       from Account where  Id in :hpIdsets ];
            // updateTenOwner(newList, accList);
        }
 
        //获取共享原因
        String rowCauseGI = Schema.Tender_information__Share.RowCause.GI_assistant__c;
        String rowCauseSP = Schema.Tender_information__Share.RowCause.SP_assistant__c;
        String rowCauseSale = Schema.Tender_information__Share.RowCause.SalesOwner__c;
        String rowCauseAdministrative = Schema.Tender_information__Share.RowCause.Administrative_assistant__c;
        String rowCauseBeipin = Schema.Tender_information__Share.RowCause.Beipin_assistant__c;
        String rowCauseTender = Schema.Tender_information__Share.RowCause.TenderAssistant__c;
        //营业窗口的共享原因
        String rowCauseYingye = Schema.Tender_information__Share.RowCause.YingyeWindow__c;
 
        List<String> rowCauseList = new List<String>();
        rowCauseList.add(rowCauseGI);
        rowCauseList.add(rowCauseSP);
        rowCauseList.add(rowCauseSale);
        rowCauseList.add(rowCauseAdministrative);
        rowCauseList.add(rowCauseBeipin);
        rowCauseList.add(rowCauseTender);
        rowCauseList.add(rowCauseYingye);
 
 
        //以 医院id 和战略科室所有人id集合为map
        // Map<Id,Set<Id>> hpDepartmentMap = new Map<Id,Set<Id>>();
        // if (accList.size() > 0) {
        //     for (Account department : accList) {
        //         if (!hpDepartmentMap.containsKey(department.ParentId)) {
        //             hpDepartmentMap.put(department.ParentId, new Set<Id>());
        //         }
        //         hpDepartmentMap.get(department.ParentId).add(department.OwnerId);
        //     }
        // }
        //能量担当需要单独查一下
        List<String> ddNameListtrim = new List<String>();
        List<String> ddNameList = new List<String>();
        for (Account hp : accList) {
            if (hp.Energy_LeaderStr__c != null && hp.Energy_LeaderStr__c != '') {
                // update fxk 2021/10/26 star
                String str = hp.Energy_LeaderStr__c.replaceAll(' ', '');
                String name = str.subString(0, 1) + ' ' + str.subString(1, str.length());
                ddNameListtrim.add(name);
                ddNameList.add(str);
                // update fxk 2021/10/26 end
            }
        }
        //查询用户 获取id
        List<User> ulist = new List<User>();
        ulist = [select id, Alias, IsActive, Name from user where IsActive = true and (Alias in :ddNameList or Alias in :ddNameListtrim)];
        Map<String, Id> uidmap = new Map<String, Id>();
        if (ulist != null && ulist.size() > 0) {
            for (User u : ulist) {
                uidmap.put(u.Name, u.Id);
            }
        }
        // upd 用户勾选了战略科室的话,这条招标信息只有关联医院对应的战略科室主担当可以看到,否则医院所有科室主担当都能看到 2021、9、22 fxk Star
        // Map<Id, Set<Id>> hpDepartmentMap = new Map<Id, Set<Id>>();
        Map<Id, Map<String, Id>> hpDepartmentMap = new Map<Id, Map<String, Id>>();
        if (accList.size() > 0) {
            for (Account hp : accList) {
                //消化科主担当
                // Set<Id> ddIds = new Set<Id>();
                Map<String, Id> ddmap = new Map<String, Id>();
                if (hp.GI_Main__c != null && hp.GI_Main__r.IsActive ) {
                    // System.debug('-------2------' + tenDepartMap.get(info.Id));
                    // ddIds.add(hp.GI_Main__c);
                    ddmap.put('01210000000QemLAAS', hp.GI_Main__c);
                }
                // 呼吸科主担当
                if (hp.BF_owner__c != null && hp.BF_owner__r.IsActive ) {
                    // System.debug('-------3------' + hp.BF_owner__c);
                    // ddIds.add(hp.BF_owner__c);
                    ddmap.put('01210000000QezZAAS', hp.BF_owner__c);
                }
                // ET科主担当
                // if (hp.ET_owner__c != null && hp.ET_owner__r.IsActive) {
                //     // System.debug('-------3------' + hp.ET_owner__r);
                //     // ddIds.add(hp.ET_owner__c);
                //     ddmap.put(hp.Department_Class__r.RecordTypeId, hp.GI_Main__c);
                // }
                // 普外科主担当
                if (hp.SP_Main__c != null && hp.SP_Main__r.IsActive) {
                    // System.debug('-------3------' + hp.SP_Main__r);
                    // ddIds.add(hp.SP_Main__c);
                    ddmap.put('01210000000QezeAAC', hp.SP_Main__c);
                }
                // 泌尿科主担当
                if (hp.URO_owner_ID__c != null && hp.URO_owner_ID__r.IsActive) {
                    // System.debug('-------3------' + hp.URO_owner_ID__r);
                    // ddIds.add(hp.URO_owner_ID__c);
                    ddmap.put('01210000000QezjAAC', hp.URO_owner_ID__c);
                }
                // 妇科主担当
                if (hp.GYN_owner__c != null && hp.GYN_owner__r.IsActive) {
                    // System.debug('-------3------' + hp.GYN_owner__r);
                    // ddIds.add(hp.GYN_owner__c);
                    ddmap.put('01210000000QezoAAC', hp.GYN_owner__c);
                }
                // 耳鼻喉科主担当
                if (hp.ENT_owner_ID__c != null && hp.ENT_owner_ID__r.IsActive) {
                    // System.debug('-------3------' + hp.ENT_owner_ID__r);
                    // ddIds.add(hp.ENT_owner_ID__c);
                    ddmap.put('01210000000QeztAAC', hp.ENT_owner_ID__c);
                }
                //能量担当
 
                if (hp.Energy_LeaderStr__c != null) {
 
                    String str = hp.Energy_LeaderStr__c.replaceAll(' ', '');
                    String name = str.subString(0, 1) + ' ' + str.subString(1, str.length());
                    // update fxk 2021/10/26 star
                    if (uidmap.containsKey(hp.Energy_LeaderStr__c)) {
                        // ddIds.add(uidmap.get(hp.Energy_LeaderStr__c));
                        // ddIds.add(uidmap.get(name));
                        ddmap.put('nengliang', uidmap.get(hp.Energy_LeaderStr__c));
 
                    }else if(uidmap.containsKey(name)){
                        ddmap.put('nengliang', uidmap.get(name));
                    }
                    // update fxk 2021/10/26 end
                }
                // ddIds.remove(null);
                ddmap.remove(null);
                if (ddmap.size() > 0) {
                    // hpDepartmentMap.put(hp.Id, ddIds);
                    hpDepartmentMap.put(hp.Id, ddmap);
                }
            }
        }
        // upd 用户勾选了战略科室的话,这条招标信息只有关联医院对应的战略科室主担当可以看到,否则医院所有科室主担当都能看到 2021、9、22 fxk End
 
 
        //待共享的数据
        //取值顺序是:所有人,之后是gi助理,接下来是sp助理,接下来是战略科室所有人 所以倒着来
        List<Tender_information__Share> tenShareList = new List<Tender_information__Share>();
        for (Tender_information__c tenc : newList) {
            //一个map为 针对一条招投标计划的共享
            Map<Id, Tender_information__Share> sharemap = new Map<Id, Tender_information__Share>();
            //1.先设置 医院担当的共享
            //获取5个医院
            Map<Id, String> hmap = new Map<Id, String>();
            hmap.put(tenc.Hospital__c, tenc.Hospital__c);
            hmap.put(tenc.Hospital1__c, tenc.Hospital1__c);
            hmap.put(tenc.Hospital2__c, tenc.Hospital2__c);
            hmap.put(tenc.Hospital3__c, tenc.Hospital3__c);
            hmap.put(tenc.Hospital4__c, tenc.Hospital4__c);
            hmap.remove(null);
            if (hpDepartmentMap.size() > 0) {
                for (Id hpId : hpDepartmentMap.keySet()) {
                    if (hmap.containsKey(hpId)) {
                        // upd 用户勾选了战略科室的话,这条招标信息只有关联医院对应的战略科室主担当可以看到,否则医院所有科室主担当都能看到 2021、9、22 fxk Star
                        Map<String, String> tempMap = new Map<String, String>();
                        if (tenc.department_selection__c != null) {
                            System.debug('=========1=======' + tenc.department_selection__c);
                            List<String> tenDepartList = tenc.department_selection__c.split(';');
                            for (String departs : tenDepartList) {
                                tempMap.put(departs, '');
                            }
                            // tempMap.put('nengliang', '');
                        } else {
                            System.debug('=========2=======' + tenc.department_selection__c);
                            tempMap.put('01210000000QemLAAS', '');
                            tempMap.put('01210000000QezZAAS', '');
                            tempMap.put('01210000000QezeAAC', '');
                            tempMap.put('01210000000QezjAAC', '');
                            tempMap.put('01210000000QezoAAC', '');
                            tempMap.put('01210000000QeztAAC', '');
                            tempMap.put('nengliang', '');
                        }
                        Map<String, Id> departToMainmap = hpDepartmentMap.get(hpId);
                        for (String depart : tempMap.keySet()) {
                            if (departToMainmap.containsKey(depart)) {
                                if (tenc.OwnerId != departToMainmap.get(depart)) {
                                    Tender_information__Share aos = new Tender_information__Share(
                                        RowCause = rowCauseSale,
                                        ParentId = tenc.Id,
                                        UserOrGroupId = departToMainmap.get(depart),
                                        AccessLevel = 'Edit');
                                    sharemap.put(departToMainmap.get(depart), aos);
                                }
                            }
                        }
                        // for (Id uId : hpDepartmentMap.get(hpId)) {
                        //     if (tenc.OwnerId != uId) {
                        //         Tender_information__Share aos = new Tender_information__Share(
                        //             RowCause = rowCauseSale,
                        //             ParentId = tenc.Id,
                        //             UserOrGroupId = uId,
                        //             AccessLevel = 'Edit');
                        //         sharemap.put(uId, aos);
 
                        //     }
                        // }
                        // upd 用户勾选了战略科室的话,这条招标信息只有关联医院对应的战略科室主担当可以看到,否则医院所有科室主担当都能看到 2021、9、22 fxk End
 
                    }
                }
            }
            //行政窗口
            if (tenc.OwnerId != tenc.Window2__c  && tenc.Window2__c != null) {
                Tender_information__Share aos = new Tender_information__Share(
                    RowCause = rowCauseAdministrative,
                    ParentId = tenc.Id,
                    UserOrGroupId = tenc.Window2__c,
                    AccessLevel = 'Edit');
                sharemap.put(tenc.Window2__c, aos);
            }
            //备品窗口
            // if (tenc.OwnerId != tenc.beiPinwindow__c  && tenc.beiPinwindow__c != null){
            //     Tender_information__Share aos = new Tender_information__Share(
            //         RowCause = rowCauseBeipin,
            //         ParentId = tenc.Id,
            //         UserOrGroupId = tenc.beiPinwindow__c,
            //         AccessLevel = 'Edit');
            //     sharemap.put(tenc.beiPinwindow__c, aos);
            // }
            //招标项目助理2
            if (tenc.OwnerId != tenc.TenderAssistant2__c && tenc.TenderAssistant2__c != null) {
                Tender_information__Share aos = new Tender_information__Share(
                    RowCause = rowCauseTender,
                    ParentId = tenc.Id,
                    UserOrGroupId = tenc.TenderAssistant2__c,
                    AccessLevel = 'Edit');
                sharemap.put(tenc.TenderAssistant2__c, aos);
            }
            //招标项目助理1
            if (tenc.OwnerId != tenc.TenderAssistant1__c && tenc.TenderAssistant1__c != null) {
                Tender_information__Share aos = new Tender_information__Share(
                    RowCause = rowCauseTender,
                    ParentId = tenc.Id,
                    UserOrGroupId = tenc.TenderAssistant1__c,
                    AccessLevel = 'Edit');
                sharemap.put(tenc.TenderAssistant1__c, aos);
            }
            //营业窗口
            if (tenc.OwnerId != tenc.YingyeWindow__c && tenc.YingyeWindow__c != null) {
                Tender_information__Share aos = new Tender_information__Share(
                    RowCause = rowCauseYingye,
                    ParentId = tenc.Id,
                    UserOrGroupId = tenc.YingyeWindow__c,
                    AccessLevel = 'Edit');
                sharemap.put(tenc.YingyeWindow__c, aos);
            }
 
            //2.SP助理
            if (tenc.OwnerId != tenc.SP_assistant__c && tenc.SP_assistant__c != null) {
                Tender_information__Share aos = new Tender_information__Share(
                    RowCause = rowCauseSP,
                    ParentId = tenc.Id,
                    UserOrGroupId = tenc.SP_assistant__c,
                    AccessLevel = 'Edit');
                sharemap.put(tenc.SP_assistant__c, aos);
            }
            //3.GI助理
            if (tenc.OwnerId != tenc.GI_assistant__c  && tenc.GI_assistant__c != null) {
                Tender_information__Share aos = new Tender_information__Share(
                    RowCause = rowCauseGI,
                    ParentId = tenc.Id,
                    UserOrGroupId = tenc.GI_assistant__c,
                    AccessLevel = 'Edit');
                sharemap.put(tenc.GI_assistant__c, aos);
            }
            tenShareList.addAll(sharemap.values());
 
        }
 
        //现在用的是全删全加的方法,没有再判断是否发生改变了
        //删除
        List<Tender_information__Share> beforeShareList =
            [select id from Tender_information__Share
             where ParentId in :newMap.keySet()
             and RowCause in :rowCauseList];
        //先删
        if (beforeShareList != null && beforeShareList.size() > 0) {
            delete beforeShareList;
        }
        //再加
        if (tenShareList.size() > 0) {
            insert tenShareList;
        }
    }
 
    public void AssignValueToAssistant() {
        //根据ocsm管理省的助理设置当前招投标项目的gi/sp助理
        Map<String, OCM_Management_Province__c> mpMap = new Map<String, OCM_Management_Province__c>();
 
        for (OCM_Management_Province__c mp : [SELECT id, Name, GI_assistant__c, SP_assistant__c, SP_assistant__r.IsActive, GI_assistant__r.IsActive, Window2__c, Window2__r.IsActive, Admin_assistant3__c, Admin_assistant3__r.IsActive, TenderAssistant1__c, TenderAssistant1__r.IsActive, TenderAssistant2__c, TenderAssistant2__r.IsActive, Window1__c, Window1__r.IsActive FROM OCM_Management_Province__c]) {
            mpMap.put(mp.Name, mp);
        }
 
        //设置GI/SP助理  行政助理、备品窗口
        //直接设置 信息负责人(助理) 为GI助理
        for (Tender_information__c info : newList) {
            //用户自己选 所有人需要和用户选的人保持一致;
            // if (info.OwnerId != info.departmentOwner__c && info.departmentOwner__c != null) {
            //     info.OwnerId = info.departmentOwner__c;
            // }
            //不能判断ocsm管理省是否发生改变了 会有历史数据
            if (info.OCSMProvince__c != null) {
                OCM_Management_Province__c ocm = mpMap.get(info.OCSMProvince__c);
                if (ocm != null) {
                    //设置OCSM管理省查找字段的值 20210728 start  为了更新历史数据 直接赋值即可
                    // if (Trigger.isInsert || (Trigger.isUpdate && oldMap.get(info.Id).OCSMProvince__c != info.OCSMProvince__c)) {
                    info.OCSMProvinceS__c = ocm.Id;
                    // }
                    //设置OCSM管理省查找字段的值 20210728 end
                    // GI助理为空时__r.IsActive返回false 20210813
                    if (ocm.GI_assistant__r.IsActive ) {
                        if (info.GI_assistant__c != ocm.GI_assistant__c) {
                            info.GI_assistant__c = ocm.GI_assistant__c;
                        }
                        if (info.InfoOwner__c != ocm.GI_assistant__c) {
                            info.InfoOwner__c = ocm.GI_assistant__c;
                        }
                    }
                    // 增加GI助理为空时的处理,清空招标项目的GI助理和信息负责人(助理) 20210813
                    else if (ocm.GI_assistant__c == null) {
                        info.GI_assistant__c = null;
                        info.InfoOwner__c = ocm.GI_assistant__c;
                    }
                    if (ocm.SP_assistant__r.IsActive && info.SP_assistant__c != ocm.SP_assistant__c) {
                        info.SP_assistant__c = ocm.SP_assistant__c;
                    }
                    // 增加SP助理为空时的处理,清空招标项目的SP助理 20210813
                    else if (ocm.SP_assistant__c == null) {
                        info.SP_assistant__c = null;
                    }
                    if (ocm.Window2__r.IsActive && info.Window2__c != ocm.Window2__c) {
                        info.Window2__c = ocm.Window2__c;
                    }
                    // 清空行政窗口 20210813
                    else if (ocm.Window2__c == null) {
                        info.Window2__c = null;
                    }
                    //
                    // if (ocm.Admin_assistant3__r.IsActive && info.beiPinwindow__c != ocm.Admin_assistant3__c) {
                    //     info.beiPinwindow__c = ocm.Admin_assistant3__c;
                    // }
                    //设置招标助理1和招标助理2的值
                    if (ocm.TenderAssistant1__r.IsActive && info.TenderAssistant1__c != ocm.TenderAssistant1__c) {
                        info.TenderAssistant1__c = ocm.TenderAssistant1__c;
                    }
                    // 清空招标助理1 20210813
                    else if (ocm.TenderAssistant1__c == null) {
                        info.TenderAssistant1__c = null;
                    }
                    if (ocm.TenderAssistant2__r.IsActive && info.TenderAssistant2__c != ocm.TenderAssistant2__c) {
                        info.TenderAssistant2__c = ocm.TenderAssistant2__c;
                    }
                    // 清空招标助理2 20210813
                    else if (ocm.TenderAssistant2__c == null) {
                        info.TenderAssistant2__c = null;
                    }
 
                    //设置营业窗口  add by rentx 20210721
                    if (ocm.Window1__r.IsActive && info.YingyeWindow__c != ocm.Window1__c) {
                        info.YingyeWindow__c = ocm.Window1__c;
                    }
                    // 清空营业窗口 20210813
                    else if (ocm.Window1__c == null) {
                        info.YingyeWindow__c = null;
                    }
                }
                // add 战略科室分类为普外科、泌尿科、妇科、耳鼻喉科时,信息负责人(助理)修改为 SP助理 2021、11、11 fxk star
                if(info.department_selection__c != null){
                    List<String> tenDepartList = info.department_selection__c.split(';');
                    if(tenDepartList.contains('01210000000QezeAAC')
                        || tenDepartList.contains('01210000000QezjAAC')
                        || tenDepartList.contains('01210000000QezoAAC')
                        || tenDepartList.contains('01210000000QeztAAC')){
                        System.debug('=====-----'+info.department_selection__c);
                        info.InfoOwner__c = info.SP_assistant__c;
                    }
                }
                // add 战略科室分类为普外科、泌尿科、妇科、耳鼻喉科时,信息负责人(助理)修改为 SP助理 2021、11、11 fxk end
            }
        }
    }
// fxk 2021/8/3 反逻辑删除 Star
    private void updateTenDel() {
        List<Tender_information__c> updateTenList = new List<Tender_information__c>();
        for (Tender_information__c TenoldInfo : newList) {
            if (TenoldInfo.Logical_delete__c == false && (TenoldInfo.Logical_delete__c != oldMap.get(TenoldInfo.Id).Logical_delete__c)) {
                TenoldInfo.Retain_Tender__c = null;
            }
            if (TenoldInfo.Retain_Tender__c == null && (TenoldInfo.Retain_Tender__c != oldMap.get(TenoldInfo.Id).Retain_Tender__c)) {
                TenoldInfo.Logical_delete__c = false;
            }
        }
    }
// fxk 2021/8/3 反逻辑删除 end
// 20210824 中标时修改关联询价的招标项目名和中标时间
    private void updateWin() {
        List<String> tenderIds = new List<String>();
        List<String> lostWinIds = new List<String>();
        for (Tender_information__c ten : newList) {
            if ('3:结果'.equals(ten.InfoType__c) && !'3:结果'.equals((oldMap.get(ten.Id).InfoType__c))) {
                tenderIds.add(ten.Id);
            } else if (!'3:结果'.equals(ten.InfoType__c) && '3:结果'.equals((oldMap.get(ten.Id).InfoType__c))) {
                lostWinIds.add(ten.Id);
            }
        }
        if (tenderIds.size() > 0) {
            List<Tender_Opportunity_Link__c> links =
                [SELECT id,
                 Tender_information__c,
                 Tender_information__r.OpenBidingTime__c,
                 Tender_information__r.WinnerAmount__c,
                 Tender_information__r.ZhongBiaoUnit1__c,
                 Opportunity__c
                 FROM Tender_Opportunity_Link__c
                 WHERE Tender_information__c IN :tenderIds
                 AND Opportunity__r.Bidding_Project_Name_Bid__c NOT IN :tenderIds];
            if (links != null && links.size() > 0) {
                List<String> oppIds = new List<String>();
                for (Tender_Opportunity_Link__c link : links) {
                    oppIds.add(link.Opportunity__c);
                }
                if (oppIds.size() > 0) {
                    List<Opportunity> opps = [select id, Bidding_Project_Name_Bid__c, Bidding_Project_Name_Bid__r.InfoType__c, Closing_Bid_Date_Bid__c from Opportunity where Id in :oppIds and Bidding_Project_Name_Bid__r.InfoType__c != '3:结果'];
                    if (opps != null && opps.size() > 0) {
                        for (Opportunity opp : opps) {
                            for (Tender_Opportunity_Link__c link : links) {
                                if (link.Opportunity__c == opp.Id) {
                                    opp.Bidding_Project_Name_Bid__c = link.Tender_information__c;
                                    break;
                                }
                            }
                        }
                        // 需要跳过询价trigger 防止出现重复更新的情况
                        StaticParameter.EscapeOpportunityBefUpdTrigger = true;
                        update opps;
                        StaticParameter.EscapeOpportunityBefUpdTrigger = false;
                    }
                }
            }
        }
        if (lostWinIds.size() > 0) {
            List<Tender_Opportunity_Link__c> links_2 =
                [SELECT id,
                 Tender_information__c,
                 Opportunity__c
                 FROM Tender_Opportunity_Link__c
                 WHERE Tender_information__c NOT IN :lostWinIds
                 AND Tender_information__r.InfoType__c = '3:结果' 
                 AND Opportunity__r.Bidding_Project_Name_Bid__c IN :lostWinIds];
            if (links_2 != null && links_2.size() > 0) {
                List<String> oppIds = new List<String>();
                for (Tender_Opportunity_Link__c link : links_2) {
                    oppIds.add(link.Opportunity__c);
                }
                if (oppIds.size() > 0) {
                    List<Opportunity> opps_2 = [select id, Bidding_Project_Name_Bid__c, Bidding_Project_Name_Bid__r.InfoType__c, Closing_Bid_Date_Bid__c from Opportunity where Id in :oppIds and Bidding_Project_Name_Bid__r.InfoType__c != '3:结果'];
                    if (opps_2 != null && opps_2.size() > 0) {
                        for (Opportunity opp : opps_2) {
                            for (Tender_Opportunity_Link__c link : links_2) {
                                if (link.Opportunity__c == opp.Id) {
                                    opp.Bidding_Project_Name_Bid__c = link.Tender_information__c;
                                    break;
                                }
                            }
                        }
                        // 需要跳过询价trigger 防止出现重复更新的情况
                        StaticParameter.EscapeOpportunityBefUpdTrigger = true;
                        update opps_2;
                        StaticParameter.EscapeOpportunityBefUpdTrigger = false;
                    }
                }
            }
        }
    }
 
    public void updateTenOwner() {
        System.debug('12345678');
        //1.获得医院
        Set<Id> hpIdsets = new Set<Id>();
        // Map < 招投标Id,Map<关联医院 >> 对应招投标下的关联医院
        Map<Id, Map<Id, Id>> hpIdsetsMap = new Map<Id, Map<Id, Id>>();
        // 得到招投标的所有人
        Map<Id, Id> tenderinformationOwnerMap = new Map<Id, Id>();
        // 得到招投标的关联战略科室
        Map<Id, List<String>> department_selectionMap = new Map<Id, List<String>>();
        for (Tender_information__c info : newList) {
            Map<Id, Id> ressMap = new  Map<Id, Id>();
            // 多加一步判断 如果new的OwnerId和old的不同 就不再做判断了 start
            // add 【委托】P-招标项目-手动创建的招标项目增加必填字段 2021/11/03 fxk Star
            Tender_information__c oldTen = new Tender_information__c();
            if (oldMap!=null && oldMap.containskey(info.Id)) {
                oldTen = oldMap.get(info.Id);
            }
            // add 【委托】P-招标项目-手动创建的招标项目增加必填字段 2021/11/03 fxk End
            if (oldTen.OwnerId != null && !oldTen.OwnerId.equals(info.OwnerId)) {
                continue;
            }
            // 多加一步判断 如果new的OwnerId和old的不同 就不再做判断了 end
            tenderinformationOwnerMap.put(info.Id, info.OwnerId);
            System.debug('-------9=======' + info.Hospital__c);
            if (info.Hospital__c != null) {
                hpIdsets.add(info.Hospital__c);
                System.debug('-------7=======' + hpIdsets);
                ressMap.put(info.Hospital__c, info.Hospital__c);
                System.debug('-------8=======' + ressMap);
            }
            // if (info.Hospital1__c != null) {
            //     hpIdsets.add(info.Hospital1__c);
            //     ressMap.put(info.Hospital1__c, info.Hospital1__c);
            // }
            // if (info.Hospital2__c != null) {
            //     hpIdsets.add(info.Hospital2__c);
            //     ressMap.put(info.Hospital2__c, info.Hospital2__c);
            // }
            // if (info.Hospital3__c != null) {
            //     hpIdsets.add(info.Hospital3__c);
            //     ressMap.put(info.Hospital3__c, info.Hospital3__c);
            // }
            // if (info.Hospital4__c != null) {
            //     hpIdsets.add(info.Hospital4__c);
            //     ressMap.put(info.Hospital4__c, info.Hospital4__c);
            // }
            hpIdsetsMap.put(info.Id, ressMap);
            if (info.department_selection__c != null) {
                List<String> tenDepartList = info.department_selection__c.split(';');
                department_selectionMap.put(info.Id, tenDepartList);
            }
        }
 
        if (hpIdsets.contains(null)) {
            hpIdsets.remove(null);
        }
 
        List<Account> accList = new List<Account>();
        System.debug('-------4=======' + hpIdsets);
        if (hpIdsets.size() > 0) {
            System.debug('-------5=======');
            //获取医院下所有启用战略科室的启用所有人
            //取得战略科室:
            // accList = [select id,OwnerId,ParentId,OwnerIsActive__c from Account where Parent.RecordType_DeveloperName__c = 'HP'
            //                                          AND OwnerIsActive__c = true
            //                                          AND ParentId  in :hpIdsets];
            //获取医院上的主担当
            accList = [select id, GI_Main__c, GI_Main__r.IsActive, BF_owner__c, BF_owner__r.IsActive,
                       ET_owner__c, ET_owner__r.IsActive, SP_Main__c, SP_Main__r.IsActive, URO_owner_ID__c, URO_owner_ID__r.IsActive,
                       GYN_owner__c, GYN_owner__r.IsActive, ENT_owner_ID__c, ENT_owner_ID__r.IsActive, Energy_LeaderStr__c
                       from Account where  Id in :hpIdsets ];
        }
 
 
 
 
        // List<Tender_information__c> TenInfoList = new List<Tender_information__c>();
        // Map<Id,Map<String,Id>> ddd = new Map<Id,Map<String,Id>>();
        // for (Id  tenderId : hpIdsetsMap.keySet()) {
        //     // Tender_information__c ten = new Tender_information__c();
        //     // ten.Id = tenderId;
        //     Id tenderownerId = tenderinformationOwnerMap.get(tenderId);
        //     Map<Id, Id> ressMap = hpIdsetsMap.get(tenderId);
        //     for (Account hp : accList) {
        //         Map<String, Id> hptempMap = new Map<String, Id>();
 
        //         Map<String, Id> hptempMap1 = new Map<String, Id>();
        //         if (ressMap.containsKey(hp.Id) && ddd.containsKey(tenderId)) {
        //             hptempMap1 = ddd.get()
        //         }
        //         if (hp.GI_Main__c != null && hp.GI_Main__r.IsActive ) {
        //             hptempMap.put('01210000000QemLAAS', hp.GI_Main__c);
        //             // ttt = tenderownerId == hp.GI_Main__c;
        //             hptempMap1.put(hp.GI_Main__c,hp.GI_Main__c);
        //         }
        //         // 呼吸科主担当
        //         if (hp.BF_owner__c != null && hp.BF_owner__r.IsActive ) {
        //             hptempMap.put('01210000000QezZAAS', hp.BF_owner__c);
        //         }
 
        //         // 普外科主担当
        //         if (hp.SP_Main__c != null && hp.SP_Main__r.IsActive) {
        //             hptempMap.put('01210000000QezeAAC', hp.SP_Main__c);
        //         }
        //         // 泌尿科主担当
        //         if (hp.URO_owner_ID__c != null && hp.URO_owner_ID__r.IsActive) {
        //             hptempMap.put('01210000000QezjAAC', hp.URO_owner_ID__c);
        //         }
        //         // 妇科主担当
        //         if (hp.GYN_owner__c != null && hp.GYN_owner__r.IsActive) {
        //             hptempMap.put('01210000000QezoAAC', hp.GYN_owner__c);
        //         }
        //         // 耳鼻喉科主担当
        //         if (hp.ENT_owner_ID__c != null && hp.ENT_owner_ID__r.IsActive) {
        //             hptempMap.put('01210000000QeztAAC', hp.ENT_owner_ID__c);
        //         }
 
        //         // if (ressMap.containsKey(hp.Id) && !hptempMap1.containsKey(tenderownerId)) {
        //         //     ddd.put(tenderId, hptempMap);
        //         // }
        //     }
 
 
        // }
        // 用于更新招投标
        List<Tender_information__c> TenInfoList = new List<Tender_information__c>();
 
        Map<Id, Map<String, Id>> hpTypeToMainMap = new Map<Id, Map<String, Id>>();
        // 存招投标对应的医院主担当
        Map<Id, Map<String, Id>> hpMainMap = new Map<Id, Map<String, Id>>();
        for (Id  tenderId : hpIdsetsMap.keySet()) {
            // 招投标 所有人
            Id tenderownerId = tenderinformationOwnerMap.get(tenderId);
            // 存医院
            Map<Id, Id> ressMap = hpIdsetsMap.get(tenderId);
            System.debug('-------3=======' + accList);
            Map<String, Id> hpTypeToMaintempMap = new Map<String, Id>();
            if (accList.size() > 0) {
                for (Account hp : accList) {
                    // 医院主担当 记录类型Id 对应 医院主担当
                    hpTypeToMaintempMap = new Map<String, Id>();
                    // 存放 医院主担当
                    Map<String, Id> hpMaintempMap = new Map<String, Id>();
                    // 关联医院
                    if (ressMap.containsKey(hp.Id) && hpMainMap.containsKey(tenderId)) {
                        hpMaintempMap = hpMainMap.get(tenderId);
                    }
                    if (hp.GI_Main__c != null && hp.GI_Main__r.IsActive ) {
                        hpTypeToMaintempMap.put('01210000000QemLAAS', hp.GI_Main__c);
                        hpMaintempMap.put(hp.GI_Main__c, hp.GI_Main__c);
                    }
                    // 呼吸科主担当
                    if (hp.BF_owner__c != null && hp.BF_owner__r.IsActive ) {
                        hpTypeToMaintempMap.put('01210000000QezZAAS', hp.BF_owner__c);
                        hpMaintempMap.put(hp.BF_owner__c, hp.BF_owner__c);
                    }
                    // 普外科主担当
                    if (hp.SP_Main__c != null && hp.SP_Main__r.IsActive) {
                        hpTypeToMaintempMap.put('01210000000QezeAAC', hp.SP_Main__c);
                        hpMaintempMap.put(hp.SP_Main__c, hp.SP_Main__c);
                    }
                    // 泌尿科主担当
                    if (hp.URO_owner_ID__c != null && hp.URO_owner_ID__r.IsActive) {
                        hpTypeToMaintempMap.put('01210000000QezjAAC', hp.URO_owner_ID__c);
                        hpMaintempMap.put(hp.URO_owner_ID__c, hp.URO_owner_ID__c);
                    }
                    // 妇科主担当
                    if (hp.GYN_owner__c != null && hp.GYN_owner__r.IsActive) {
                        hpTypeToMaintempMap.put('01210000000QezoAAC', hp.GYN_owner__c);
                        hpMaintempMap.put(hp.GYN_owner__c, hp.GYN_owner__c);
                    }
                    // 耳鼻喉科主担当
                    if (hp.ENT_owner_ID__c != null && hp.ENT_owner_ID__r.IsActive) {
                        hpTypeToMaintempMap.put('01210000000QeztAAC', hp.ENT_owner_ID__c);
                        hpMaintempMap.put(hp.ENT_owner_ID__c, hp.ENT_owner_ID__c);
                    }
                    // 存招投标下所有关联医院 的所有主担当
                    if (ressMap.containsKey(hp.Id) ) {
                        hpMainMap.put(tenderId, hpMaintempMap);
                    }
                }
            }
            // 所有关联医院的主担当不包含 招投标所有人
            System.debug('-------2=======' + hpMainMap);
            if (hpMainMap.get(tenderId) != null) {
                // 增加判断战略科室
                if (department_selectionMap.get(tenderId) != null) {
                    boolean changeOwner = true;
                    Map<String, Id> tenHpMainMap = hpMainMap.get(tenderId);
                    for (String type : department_selectionMap.get(tenderId)) {
                        if (tenHpMainMap.get(type) != null && tenHpMainMap.get(type).equals(tenderownerId)) {
                            changeOwner = false;
                            break;
                        }
                    }
                    if (changeOwner) {
                        hpTypeToMainMap.put(tenderId, hpTypeToMaintempMap);
                    }
                } else {
                    hpTypeToMaintempMap.put('00510000000gmxH', '00510000000gmxH');
                    hpTypeToMainMap.put(tenderId, hpTypeToMaintempMap);
                }
            } else {
                hpTypeToMaintempMap.put('00510000000gmxH', '00510000000gmxH');
                hpTypeToMainMap.put(tenderId, hpTypeToMaintempMap);
            }
 
        }
        System.debug('-------1=======' + hpTypeToMainMap);
        Map<Id, Id> UpdateTenMap = new Map<Id, Id>();
        if (hpTypeToMainMap.size() > 0) {
            // 查招投标-询价关联表
            List<Tender_Opportunity_Link__c> TenOppLinkList = [select Id, Opportunity__c, Tender_information__c, Opportunity__r.OwnerId, Opportunity__r.Owner.IsActive, Name
                    from Tender_Opportunity_Link__c where Tender_information__c in :hpTypeToMainMap.keySet() and  Opportunity__r.Owner.IsActive = true
                                    order by Tender_information__c, Name asc];
            Map<Id, Id> oppMainMap = new Map<Id, Id>();
            for (Tender_Opportunity_Link__c oppLinks : TenOppLinkList) {
                if (!oppMainMap.containsKey(oppLinks.Tender_information__c)) {
                    oppMainMap.put(oppLinks.Tender_information__c, oppLinks.Opportunity__r.OwnerId );
                }
            }
 
            for (Id  tenderId : hpTypeToMainMap.keySet()) {
                for (Tender_information__c info : newList) {
                    // add 【委托】P-招标项目-手动创建的招标项目增加必填字段 2021/11/03 fxk Star
                    Tender_information__c oldTen = new Tender_information__c();
                    if (oldMap!=null && oldMap.containskey(info.Id)) {
                        oldTen = oldMap.get(info.Id);
                    }
                    // add 【委托】P-招标项目-手动创建的招标项目增加必填字段 2021/11/03 fxk End
                    // 招投标没关联询价
                    if (!oppMainMap.containsKey(tenderId)) {
                        System.debug('----------5---------' + oppMainMap);
                        // 招投标 关联战略科室(字段) 有值
                        if (department_selectionMap.containsKey(tenderId)) {
                            System.debug('----------4---------' + department_selectionMap);
                            // 战略科室的 记录类型
                            String typeId = department_selectionMap.get(tenderId) == null ? '' : department_selectionMap.get(tenderId)[0];
                            System.debug('----------6---------' + typeId);
                            Map<String, Id> hpTypeToMaintempMap = hpTypeToMainMap.get(tenderId);
                            System.debug('----------7---------' + hpTypeToMaintempMap);
                            if (hpTypeToMaintempMap.containsKey(typeId)) {
                                UpdateTenMap.put(tenderId, hpTypeToMaintempMap.get(typeId));
                                System.debug('----------8---------' + UpdateTenMap);
                            } else {
                                UpdateTenMap.put(tenderId, '00510000000gmxH');
                                System.debug('----------9---------' + UpdateTenMap);
                            }
                        } else {
                            UpdateTenMap.put(tenderId, '00510000000gmxH');
                            System.debug('----------10---------' + UpdateTenMap);
                        }
                    } else if ((oldTen.OwnerId != null && oldTen.OwnerId.equals(info.OwnerId)) 
                            && !hpMainMap.get(tenderId).containskey(info.ownerId)) {
                       UpdateTenMap.put(tenderId, oppMainMap.get(tenderId));
                       System.debug('----------11---------' + UpdateTenMap);
                    }
                }
            }
        }
        // StaticParameter.EscapeOtherUpdateTenOwner = false;
        if (UpdateTenMap.size() > 0) {
            for (Tender_information__c info : newList) {
                if (UpdateTenMap.containsKey(info.Id)) {
                    info.OwnerId = UpdateTenMap.get(info.Id);
                }
            }
        }
        // StaticParameter.EscapeOtherUpdateTenOwner = true;
    }
}