涂煌豪
2022-03-21 59c783da381ee6437ef9d9ee496f91bff1f78c2f
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
public without sharing class InquiryFormHandler extends Oly_TriggerHandler {
        private Map<Id, Inquiry_form__c> newMap;
        private Map<Id, Inquiry_form__c> oldMap;
        private List<Inquiry_form__c> newList;
        private List<Inquiry_form__c> oldList;
 
    public InquiryFormHandler(){
 
         this.newMap = (Map<Id, Inquiry_form__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Inquiry_form__c>) Trigger.oldMap;
        this.newList = (List<Inquiry_form__c>) Trigger.new;
        this.oldList = (List<Inquiry_form__c>) Trigger.old;
        
    }
 
    protected override void beforeInsert() {
        beforeExecute();
        //客户人员录入后,【电话】【邮箱】自动录入 thh 20220321 start
        getContactInformation();
        //客户人员录入后,【电话】【邮箱】自动录入 thh 20220321 end
    }
    protected override void beforeUpdate() {
        beforeExecute();
        //客户人员录入后,【电话】【邮箱】自动录入 thh 20220321 start
        getContactInformation();
        //客户人员录入后,【电话】【邮箱】自动录入 thh 20220321 end
    }
 
    protected override void afterInsert() {
        //shareToOSCM();
    }
 
     protected override void afterUpdate() {
        //shareToOSCM();
    }
     //Before処理
    private void beforeExecute() {
        Map<Id,Account> accMap = new Map<Id,Account>();
        Inquiry_form__c oObj = null;
        List<String> depIDList = new List<String>();
        List<Account> accList = new List<Account>();
 
        for(Inquiry_form__c nnObj : newList) {
            if (oldMap != null && oldMap.containsKey(nnObj.Id)) {
                oObj = oldMap.get(nnObj.Id);
            }
            if (String.isNotBlank(nnObj.Hospital_Name__c) && (Trigger.isInsert || oObj.Hospital_Name__c != nnObj.Hospital_Name__c || System.Label.Inquiry_form_flag == 'True')) {
                depIDList.add(nnObj.Hospital_Name__c);
            }
            if (Trigger.isUpdate && String.isBlank(nnObj.Hospital_Name__c)){
                nnObj.Depart_Owner__c = null;     //战略科室所有人           
                nnObj.OwnerId = nnObj.CreatedById; //所有人
                nnObj.Department_Class__c = null; //战略科室
                nnObj.HospitalName__c = null;     //医院名
                nnObj.Hospital__c = null;         //医院
            }
        }
        if (depIDList.size()>0) {
            accList  = [Select Id,OwnerId,
                            Hospital__c,Hospital__r.Name,
                            Department_Class__c 
                            from Account where Id =: depIDList]; 
            if (accList.size()>0) {
                for(Account acc : accList){
                    accMap.put(acc.Id, acc);
                }
            }   
        }
        
        for(Inquiry_form__c nObj : newList) {
            if (accMap.containsKey(nObj.Hospital_Name__c)){
                nObj.Depart_Owner__c = accMap.get(nObj.Hospital_Name__c).OwnerId;                  //战略科室所有人
                nObj.OwnerId = accMap.get(nObj.Hospital_Name__c).OwnerId;                          //所有人
                nObj.Department_Class__c = accMap.get(nObj.Hospital_Name__c).Department_Class__c; //战略科室
                nObj.HospitalName__c = accMap.get(nObj.Hospital_Name__c).Hospital__r.Name;        //医院名
                nObj.Hospital__c = accMap.get(nObj.Hospital_Name__c).Hospital__c;                 //医院
            }
            
            //产品信息的拼接
            if (String.isNotBlank(nObj.Product1__c) && String.isNotBlank(nObj.Product1_Manual__c)){
                nObj.Product1__c = nObj.Product1__c +';'+nObj.Product1_Manual__c;
            }else if (String.isBlank(nObj.Product1__c) && String.isNotBlank(nObj.Product1_Manual__c)){
                nObj.Product1__c = nObj.Product1_Manual__c;
            }else if (String.isNotBlank(nObj.Product1__c) && String.isBlank(nObj.Product1_Manual__c)) {
                nObj.Product1__c = nObj.Product1__c;
            }else {
                nObj.Product1__c = '';
            }
 
            //产品咨询单名称
            if (String.isNotBlank(nObj.Product1__c)&& String.isNotBlank(nObj.HospitalName__c)){
                nObj.Name = nObj.HospitalName__c+'-'+nObj.Product1__c;         
            }else if (String.isBlank(nObj.HospitalName__c)&& String.isNotBlank(nObj.Product1__c)) {
                nObj.Name = nObj.Product1__c;
            }else if (String.isNotBlank(nObj.HospitalName__c)&& String.isBlank(nObj.Product1__c)){
                nObj.Name = nObj.HospitalName__c;                              
            }else{
                nObj.Name = '*';
            }
            
            nObj.Product1_Manual__c = '';
        }
    }
 
    //客户人员录入后,【电话】【邮箱】自动录入 thh 20220321 start
    private void getContactInformation(){
        List<String> ContactIdList = new List<String>();
        List<Contact> ContactList = new List<Contact>();
        Map<String, Contact> ContactMap = new Map<String, Contact>();
        for(Inquiry_form__c nnObj : newList) {
            if(String.isNotBlank(nnObj.Contact_Name__c)){
                ContactIdList.add(nnObj.Contact_Name__c);
            }
        }
        if(ContactIdList.size() > 0){
            ContactList = [select id, Phone, Email from Contact where Id IN :ContactIdList];
            for(Contact contact : ContactList){
                ContactMap.put(contact.Id, contact);
            }
        }
        for(Inquiry_form__c nnObj : newList) {
            if(String.isNotBlank(nnObj.Contact_Name__c)){
                if(String.isNotBlank(ContactMap.get(nnObj.Contact_Name__c).Phone)){
                    nnObj.Phone__c = ContactMap.get(nnObj.Contact_Name__c).Phone;
                }
                if(String.isNotBlank(ContactMap.get(nnObj.Contact_Name__c).Email)){
                    nnObj.Email__c = ContactMap.get(nnObj.Contact_Name__c).Email;
                }
            }
        }
    }
    //客户人员录入后,【电话】【邮箱】自动录入 thh 20220321 end
 
     // 战略科室的主担当 を取得、Inquiry_form__Shareに設定(Read)
    // private void shareToOSCM() {
    //     //存放用于新增的共享数据
    //     List<Inquiry_form__Share> insertList = new List<Inquiry_form__Share>();
    //     //存放(会议询问单id,共享对象)
    //     Map<Id,Inquiry_form__Share> insertMap = new Map<Id,Inquiry_form__Share>();
    //     //存放最后需要新增的共享数据
    //     List<Inquiry_form__Share> lastInsertList = new List<Inquiry_form__Share>();
    //     //存放已有的相同的共享原因的数据
    //     List<Id> deleteTargetAOIdList = new List<Id>();
    //     //存放用于的id
    //     List<Id> userIdList = new List<Id>();
    //     // String rowCause = 'Manual';
    //     //新增一个共享原因
    //     String rowCause = Schema.Inquiry_form__Share.RowCause.OCSM_Owner_c_User__c;
    //     System.debug('rowCause:'+rowCause);
    //     String ownerCause = 'Owner';
    //     //Apex共有の理由名OCSM_Owner_c_User
    //     for(Inquiry_form__c nObj : newList) {
    //         Inquiry_form__c oObj = null;
    //         System.debug('战略科室担当:'+nObj.Depart_Owner__c);
    //         if (oldMap != null && oldMap.containsKey(nObj.Id)) {
    //             oObj = oldMap.get(nObj.Id);
    //         }
            
 
    //         if ( nObj.Depart_Owner__c != null && (oObj == null || oObj.Depart_Owner__c != nObj.Depart_Owner__c)) {
    //             Inquiry_form__Share aos = new Inquiry_form__Share(
    //                     RowCause = rowCause,
    //                     ParentId = nObj.Id,
    //                     UserOrGroupId = nObj.Depart_Owner__c,
    //                     AccessLevel = 'Edit');
    //             //存放要新增的共享数据
    //             insertList.add(aos);
    //             System.debug('key:'+nObj.Id);
    //             //存放(会议询问单id,共享对象);
    //             insertMap.put(nObj.Id,aos);
    //             //存放用户id 用作检索条件
    //             userIdList.add(nObj.Depart_Owner__c);
    //             if (oObj != null && oObj.Depart_Owner__c != nObj.Depart_Owner__c) {
    //                 deleteTargetAOIdList.add(nObj.Id);
    //             }
    //         }
    //     }
    //     System.debug('insertList:'+insertList);
    //     System.debug('insertMap1:'+insertMap);
    //     System.debug('IDlIST:'+deleteTargetAOIdList);
    //     // 先 Delete 后 Insert
    //     if (deleteTargetAOIdList.size() > 0) {
    //         List<Inquiry_form__Share> deleteList = [SELECT Id
    //             FROM Inquiry_form__Share
    //             WHERE RowCause = :rowCause
    //               AND ParentId IN :deleteTargetAOIdList
    //         ];
    //         delete deleteList;
    //     }
    //     //判断需要共享的人  是不是创建人 如果是则说明有一条这个人的owner数据 则不新增
    //     if (insertMap!= null) {
    //         List<Inquiry_form__Share> ownerList = [SELECT Id,ParentId,UserOrGroupId
    //             FROM Inquiry_form__Share
    //             WHERE RowCause = :ownerCause
    //               AND ParentId IN :insertMap.keySet()
    //               AND UserOrGroupId IN :userIdList
    //         ];
    //         System.debug('ownerList:'+ownerList);
    //         if(ownerList.size() > 0){
    //             for( Inquiry_form__Share  inq:ownerList){
    //                 String id = String.valueOf(inq.ParentId);
    //                 System.debug('id:'+id);
    //                 if(insertMap.containsKey(id)){
    //                     insertMap.remove(inq.ParentId);
    //                 }
    //             }
    //         }
            
    //     }
    //     System.debug('insertMap2:'+insertMap);
        
    //     if(insertMap != null){
    //             for(Inquiry_form__Share inquiry : insertMap.values()){
    //                 lastInsertList.add(inquiry);
    //             }
    //     }
        
 
    //     System.debug('共享内容:' +lastInsertList);
    //     if(lastInsertList.size() > 0){
    //         insert lastInsertList;
    //     }
        
    // }
}