高章伟
2022-03-22 a191c8358b35f54342da53db820ede118720230a
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
//医療従事者の診療科(兼務先)を変更の場合、戦略科室・病院も修正
trigger ContactHpDeptUpd on Contact (before insert, before update) {
    List<String> accIds = new List<String>();
    List<String> FirstNameList = new List<String>();
    List<String> LastnameList = new List<String>();
    List<String> LastnameEncryptedList = new List<String>();
    List<id> Hospital_id_list = new List<id>();
    List<id> ACCOUNT_id_list = new List<id>();
    Map<String,Contact> mapCon = new Map<String,Contact>();
//***************************************INSERT 2016-12-14 By ZDF START*************************************
//          增加“新增客户人员入例规则”
//***************************************INSERT 2016-12-14 By ZDF START*************************************
//2022-02-10    PI改造,用LastName_Encrypted__c替代原有的Name作为mapCon映射的键
    for(Contact a : Trigger.new) {
        if (Trigger.isInsert
                || (Trigger.isUpdate
                    && (Trigger.oldMap.get(a.Id).get('AccountId') != a.AccountId
                        || a.AccountParentId__c != a.Strategic_dept_Class__c))) {
            if (!String.isBlank(a.AccountId)) {
                accIds.add(a.AccountId);
            }
        }
        if(Trigger.isInsert&&a.Ignore_Same_Name__c==false){
            FirstNameList.add(a.firstname);
            LastnameList.add(a.Lastname);
            LastnameEncryptedList.add(a.LastName_Encrypted__c);
            ACCOUNT_id_list.add(a.Accountid);
            mapCon.put(a.LastName_Encrypted__c,a);
            system.debug('========1111111');
        }
 
    }
 
    system.debug('========FirstNameList'+FirstNameList+'###'+FirstNameList.size());
    if(FirstNameList.size()==1){
        List<Account> AccountList   =   [   select
                                                    id,
                                                    ParentId,
                                                    Parent.ParentId,
                                                    Name
                                            from
                                                    Account
                                            where
                                                    id  in:ACCOUNT_id_list
                                        ];
        for(    Account acc :   AccountList ){
            if( acc.Parent.ParentId !=  null    ){
                Hospital_id_list.add(   acc.Parent.ParentId );
            }else   if( acc.ParentId    !=  null    ){
                Hospital_id_list.add(   acc.ParentId    );
            }else {
                Hospital_id_list.add(   acc.Id  );
            }
        }
        List<Contact> CntingList = [    select  
                                                id,
                                                Name,
                                                Department__c,
                                                Account.Parent.Name,
                                                Account.name,
                                                Account.Parent.Parent.Name
                                        from 
                                                Contact 
                                        where   
                                        //         firstname  in:FirstNameList
                                        // and
                                        //         Lastname   in:LastnameList
                                        LastName_Encrypted__c in : LastnameEncryptedList
                                        and     
                                            (   
                                                Account.Parent.Parentid  in:Hospital_id_list
                                        or
                                                Account.Parentid  in:Hospital_id_list
                                        or
                                                Account.id  in:Hospital_id_list
                                            )
                                ];
        system.debug('========1212121212'+FirstNameList);
        system.debug('========1212121212'+LastnameList);
        system.debug('========1212121212'+LastnameEncryptedList);
        system.debug('========1212121212'+Hospital_id_list);
        if(CntingList.size()>0){
            system.debug('========2222222'+CntingList.size());
            Contact Cnting = CntingList[0];
            for(Contact a : Trigger.new) {
                system.debug('========3333333');
                if(mapCon.containsKey(a.LastName_Encrypted__c)){
                    //For PIPL testing by Li Jun 20220308 Start
                    // system.debug('========333333');
                    // if( Cnting.Account.Parent.Parent.Name   !=  null    ){
                    //         a.addError('该客户人员名字已存在,在'+Cnting.Account.Parent.Parent.Name+'医院下属 '+Cnting.Account.Parent.Name+'战略科室的 '+Cnting.Account.Name+'科室,请修改,或者勾选“不是重复的客户名”后,再次点击保存');
                    //     }   else if (   Cnting.Account.Parent.Name  !=null  ){
                    //         a.addError('该客户人员名字已存在,在'+Cnting.Account.Parent.Name+'医院的 '+Cnting.Account.Name+' 战略科室下,请修改,或者勾选“不是重复的客户名”后,再次点击保存');
                    //     }   else if (   Cnting.Account.Name !=  null    ){
                    //         a.addError('该客户人员名字已存在,在'+Cnting.Account.Name+'医院,请修改,或者勾选“不是重复的客户名”后,再次点击保存'); 
                    //     }
                    //For PIPL testing by Li Jun 20220308 End
                    }
            }   
        }
    }
//***************************************INSERT 2016-12-14 By ZDF EDN*************************************
//          增加“新增客户人员入例规则”
//***************************************INSERT 2016-12-14 By ZDF EDN*************************************
 
 
    if (accIds.size() > 0) {
        // 診療科レコードタイプ
        String[] deptTypes = new String[] {'診療科 その他', '診療科 呼吸科', '診療科 婦人科', '診療科 普外科', '診療科 泌尿科', '診療科 消化科', '診療科 耳鼻喉科'};
        List<RecordType> deptRects = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN :deptTypes];
        Map<String, String> deptRectMap = new Map<String, String>();
        for (RecordType rect : deptRects) {
            deptRectMap.put(rect.Id, rect.Name);
        }
        
        Map<Id, Account> accMap = new Map<Id, Account>();
        List<Account> accs = ControllerUtil.selectAccountForTrigger(accIds);
        for(Account acc : accs) {
            accMap.put(acc.Id, acc);
        }
        for(Contact a : Trigger.new) {
            // 「診療科」に診療科を選択する場合
            if (accMap.get(a.AccountId) != null
                && deptRectMap.get(accMap.get(a.AccountId).RecordTypeId) != null) {
                Account acc = accMap.get(a.AccountId);
                a.Strategic_dept_Class__c = acc.ParentId;
            }
        }
    }
}