liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
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
public without sharing class AfterCreateHospitalTrigger {
    static List<RecordType> hRecs; 
    static List<RecordType> dcRecs;
 
    public static void setDefaultOwner(List<Account> newList, Map<Id, Account> newMap, List<Account> oldList, Map<Id, Account> oldMap) {
        //Add By HZK 根据developername 获取记录类型Id
        //病院记录类型Id
        Id hpRtId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId();
        for (Account a : newList) {
            //if (a.RecordTypeId != '01210000000QemG') {
            if (a.RecordTypeId != hpRtId) {
                continue;
            }
            // 病院の場合
            /*
            String ownerid = a.OwnerId;
            a.GI_Main__c = ownerid;
            a.BF_owner__c = ownerid;
            a.SP_Main__c = ownerid;
            a.ENT_owner_ID__c = ownerid;
            a.URO_owner_ID__c = ownerid;
            a.GYN_owner__c = ownerid;
            a.ET_owner__c = ownerid;
            */
        }
    }
 
    // 病院が新規されたら、自動で戦略科室を作成
    public static void generateDepartmentClass(List<Account> newList, Map<Id, Account> newMap, List<Account> oldList, Map<Id, Account> oldMap) {
        if (Trigger.isUpdate) {
            Id hpRtId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId();
            String[] departmentClassTypes = new String[] {'戦略科室分類 消化科', '戦略科室分類 耳鼻喉科', '戦略科室分類 泌尿科', '戦略科室分類 普外科', '戦略科室分類 婦人科', '戦略科室分類 呼吸科', '戦略科室分類 その他', '戦略科室分類ET'};
            String[] dcLabelNames = new String[] {'消化科', '耳鼻喉科', '泌尿科', '普外科', '妇科', '呼吸科', '其他', 'ET'};
            List<String> idList = new List<String>();
            Map<String,String> exsitMapAcc = new Map<String,String>();
            for(Account acc : newList){
                //if (acc.RecordTypeId == '012C600000003HZIAY' ) {
                if (acc.RecordTypeId == hpRtId ) {
                     idList.add(acc.id);
                }
            }
            System.debug('departmentClassTypes:'+departmentClassTypes);
            List<Account> exsitAcc = [select id,Hospital_Department_Class__c,name from Account where Hospital_Department_Class__c in:idList];
            for(Account acc: exsitAcc){
                exsitMapAcc.put(acc.Hospital_Department_Class__c, acc.name);
            }
            if (dcRecs == null) {
                dcRecs = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN :departmentClassTypes];
            }
            Map<String, String> rectMap = new Map<String, String>();
            for (RecordType rect : dcRecs) {
                rectMap.put(rect.Name, rect.Id);
            }
            System.debug('rectMap:'+rectMap);
            List<Account> dcAccList = new List<Account>();
            for (Account a : newList) {
                //if (a.RecordTypeId != '012C600000003HZIAY' ) {
                if (a.RecordTypeId != hpRtId ) {
                    continue;
                }
                if(exsitMapAcc.containsKey(a.id)){
                    continue;
                }
                System.debug(a.Is_Active__c+'################'+oldMap.get(a.Id).Is_Active__c);
                if(a.Is_Active__c =='有効' && oldMap.get(a.Id).Is_Active__c =='申请中'){
 
                // 病院の場合
                //调一次反应权限
                
                    for (Integer i = 0; i < departmentClassTypes.size(); i++) {
                        String dcLabel = departmentClassTypes[i];
                        System.debug('dcLabel:'+dcLabel);
                        String owneridLine = '';
                        if(dcLabel == '戦略科室分類ET'){
                            //戦略科室分類ET
                            owneridLine = a.ET_owner__c;                
                        }else if(dcLabel == '戦略科室分類 消化科'){
                            //戦略科室分類 消化科
                            owneridLine = a.GI_Main__c;
                        }else if(dcLabel == '戦略科室分類 泌尿科'){
                            //戦略科室分類 泌尿科
                            owneridLine = a.URO_owner_ID__c;
                        }else if(dcLabel == '戦略科室分類 普外科'
                                || dcLabel == '戦略科室分類 その他'){
                            //戦略科室分類 普外科&戦略科室分類 その他
                            System.debug('SP_Main__c===='+a.SP_Main__c);
                            owneridLine = a.SP_Main__c;
                        }else if(dcLabel == '戦略科室分類 婦人科'){
                            //戦略科室分類 婦人科
                            owneridLine = a.GYN_owner__c;
                        }else if(dcLabel == '戦略科室分類 呼吸科'){
                            //戦略科室分類 呼吸科
                            owneridLine = a.BF_owner__c;
                        }else if(dcLabel == '戦略科室分類 耳鼻喉科'){
                            //戦略科室分類 耳鼻喉科
                            owneridLine = a.ENT_owner_ID__c;
                        }
                        Account dc = new Account(
                                RecordTypeId = rectMap.get(dcLabel),
                                Department_Class_Label__c = dcLabelNames[i],
                                Name = '*',
                                Hospital_Department_Class__c = a.Id,
                                ParentId = a.Id,
                                OCM_Category__c = a.OCM_Category__c,
                                ownerid = owneridLine
                        );
                        dcAccList.add(dc);
                    }
                }
                if (System.Test.isRunningTest()) {
                    for (Integer i = 0; i < departmentClassTypes.size(); i++) {
                        String dcLabel = departmentClassTypes[i];
                        Account dc = new Account(
                                RecordTypeId = rectMap.get(dcLabel),
                                Department_Class_Label__c = dcLabelNames[i],
                                Name = '*',
                                Hospital_Department_Class__c = a.Id,
                                ParentId = a.Id,
                                OCM_Category__c = a.OCM_Category__c,
                                ownerid = a.ownerid
                        );
                        dcAccList.add(dc);
                    }
                }
                //a.Account_Set_Compelet__c = true;
            }
            if (dcAccList.size() > 0) {
                insert dcAccList;
                Integer i =0;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
                i++;
            }
        }
    }
}