buli
2022-05-14 ead4df22dca33a867279471821ca675f91dec760
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
public without sharing class ContactTriggerHandler {
    
    public static void setIsNew(List<Contact> newList, Map<Id, Contact> newMap, List<Contact> oldList, Map<Id, Contact> oldMap) {
        for (Contact newCon : newList) {
            // String new_profileId = UserInfo.getProfileId().subString(0,15);
            //calendarUtil.getMemberProfileID  这里用到的人员ID 和获取到的简档ID都是15位的
            String new_profileId = calendarUtil.getMemberProfileID(UserInfo.getUserId().subString(0,15));
 
            if (new_profileId.subString(0,15) == System.label.Market_Department || new_profileId.subString(0,15) == System.label.Market_Department1 || new_profileId.subString(0,15) == System.label.Service_Owner ||
                UserInfo.getUserType() == 'PowerPartner') {
                newCon.IsNew__c = true;
            } else {
                newCon.MobilePhoneD__c = newCon.MobilePhone;
                newCon.OtherPhoneD__c = newCon.OtherPhone;
                newCon.FaxD__c = newCon.Fax;
                newCon.EmailD__c = newCon.Email;
                newCon.PhoneD__c = newCon.Phone;
                newCon.TitleD__c = newCon.Title;
                newCon.Address1D__c = newCon.Address1__c;
                newCon.Address2D__c = newCon.Address2__c;
                newCon.Address3D__c = newCon.Address3__c;
                newCon.PostcodeD__c = newCon.Postcode__c;
                newCon.ContactStatusD__c = newCon.ContactStatus__c;
                newCon.CancelReasonD__c = newCon.CancelReason__c;
                if(new_profileId.subString(0,15) == '00e28000000YKLo'){
                    newCon.IsNew__c = true;
                }
                // PIPL Update 20220420 By Chen Yanan Start
                newCon.MobilePhoneD_Encrypted__c = newCon.MobilePhone_Encrypted__c;
                newCon.OtherPhoneD_Encrypted__c = newCon.OtherPhone_Encrypted__c;
                newCon.FaxD_Encrypted__c = newCon.Fax_Encrypted__c;
                newCon.EmailD_Encrypted__c = newCon.Email_Encrypted__c;
                newCon.PhoneD_Encrypted__c = newCon.Phone_Encrypted__c;
                newCon.TitleD_Encrypted__c = newCon.Title_Encrypted__c;
                newCon.Address1D_Encrypted__c = newCon.Address1_Encrypted__c;
                newCon.Address2D_Encrypted__c = newCon.Address2_Encrypted__c;
                newCon.Address3D_Encrypted__c = newCon.Address3_Encrypted__c;
                newCon.PostcodeD_Encrypted__c = newCon.Postcode_Encrypted__c;
                // PIPL Update 20220420 By Chen Yanan End
            }
            System.debug('setIsNew newCon: ' + newCon);
        }
            
    }
    
    public static void updateForDealer(List<Contact> newList, Map<Id, Contact> newMap, List<Contact> oldList, Map<Id, Contact> oldMap) {
        //王鹏伟 添加userId 服务部修改联系人重新审批 判断当前修改的登陆人是否是孙学洋  吴晓东
        String userId = UserInfo.getUserId(); 
        for (Contact newCon : newList) {
            Contact oldCon = oldMap.get(newCon.Id);
            
            if(newCon.AccountStaut__c != 'Pass' && newCon.StatusD__c != oldCon.StatusD__c && newCon.StatusD__c == 'Pass' ){
                newCon.addError('请先审批对应的客户');
            }
 
            if (newCon.MobilePhoneD__c != oldCon.MobilePhoneD__c ||
                newCon.OtherPhoneD__c != oldCon.OtherPhoneD__c ||
                newCon.FaxD__c != oldCon.FaxD__c ||
                newCon.EmailD__c != oldCon.EmailD__c ||
                newCon.PhoneD__c != oldCon.PhoneD__c ||
                newCon.TitleD__c != oldCon.TitleD__c ||
                newCon.Address1D__c != oldCon.Address1D__c ||
                newCon.Address2D__c != oldCon.Address2D__c ||
                newCon.Address3D__c != oldCon.Address3D__c ||
                newCon.PostcodeD__c != oldCon.PostcodeD__c ||
                newCon.ContactStatusD__c != oldCon.ContactStatusD__c ||
                newCon.CancelReasonD__c != oldCon.CancelReasonD__c) {
                // String new_profileId = UserInfo.getProfileId().subString(0,15);
                //新的获取简档ID  calendarUtil.getMemberProfileID  这里用到的人员ID 和获取到的简档ID都是15位的
                String new_profileId = calendarUtil.getMemberProfileID(UserInfo.getUserId().subString(0,15));
                //王鹏伟 添加 && !((oldCon.ProductSegment__c == 'BS' || oldCon.ProductSegment__c == 'NDT' || oldCon.ProductSegment__c = 'ANI') && oldCon.isServiceCreate__c == true)
                //服务部修改联系人信息 LS、NDT、ANI无需审批,跳过审批状态变草案
                if (( new_profileId.subString(0,15) == System.label.Service_Owner
                    || UserInfo.getUserType() == 'PowerPartner' || userId =='00528000000YWC6' || userId =='00528000000YWE7')
                    && !((oldCon.ProductSegment__c == 'BS' || oldCon.ProductSegment__c == 'NDT' || oldCon.ProductSegment__c == 'ANI') && oldCon.isServiceCreate__c == true)) {
                    newCon.StatusD__c = 'Draft';
                }
            }
            
            if (newCon.MobilePhone != oldCon.MobilePhone) {
                newCon.MobilePhoneD__c = newCon.MobilePhone;
                // PIPL Update 20220420 By Chen Yanan Start
                System.debug('MobilePhoneD_Encrypted__c');
                newCon.MobilePhoneD_Encrypted__c = newCon.MobilePhone_Encrypted__c;
                // PIPL Update 20220420 By Chen Yanan End
            }
            if (newCon.OtherPhone != oldCon.OtherPhone) {
                newCon.OtherPhoneD__c = newCon.OtherPhone;
                // PIPL Update 20220420 By Chen Yanan Start
                System.debug('OtherPhoneD_Encrypted__c');
                newCon.OtherPhoneD_Encrypted__c = newCon.OtherPhone_Encrypted__c;
                // PIPL Update 20220420 By Chen Yanan End
            }
            if (newCon.Fax != oldCon.Fax) {
                newCon.FaxD__c = newCon.Fax;
                // PIPL Update 20220420 By Chen Yanan Start
                System.debug('FaxD_Encrypted__c');
                newCon.FaxD_Encrypted__c = newCon.Fax_Encrypted__c;
                // PIPL Update 20220420 By Chen Yanan End
            }
            if (newCon.Email != oldCon.Email) {
                newCon.EmailD__c = newCon.Email;
                // PIPL Update 20220420 By Chen Yanan Start
                System.debug('EmailD_Encrypted__c');
                newCon.EmailD_Encrypted__c = newCon.Email_Encrypted__c;
                // PIPL Update 20220420 By Chen Yanan End
            }
            if (newCon.Phone != oldCon.Phone) {
                newCon.PhoneD__c = newCon.Phone;
                // PIPL Update 20220420 By Chen Yanan Start
                System.debug('PhoneD_Encrypted__c');
                newCon.PhoneD_Encrypted__c = newCon.Phone_Encrypted__c;
                // PIPL Update 20220420 By Chen Yanan End
            }
            if (newCon.Title != oldCon.Title) {
                newCon.TitleD__c = newCon.Title;
                // PIPL Update 20220420 By Chen Yanan Start
                System.debug('TitleD_Encrypted__c');
                newCon.TitleD_Encrypted__c = newCon.Title_Encrypted__c;
                // PIPL Update 20220420 By Chen Yanan End
            }
            if (newCon.Address1__c != oldCon.Address1__c) {
                newCon.Address1D__c = newCon.Address1__c;
                System.debug('Address1D_Encrypted__c');
                // PIPL Update 20220420 By Chen Yanan Start
                newCon.Address1D_Encrypted__c = newCon.Address1_Encrypted__c;
                // PIPL Update 20220420 By Chen Yanan End
            }
            if (newCon.Address2__c != oldCon.Address2__c) {
                newCon.Address2D__c = newCon.Address2__c;
                System.debug('Address2D_Encrypted__c');
                // PIPL Update 20220420 By Chen Yanan Start
                newCon.Address2D_Encrypted__c = newCon.Address2_Encrypted__c;
                // PIPL Update 20220420 By Chen Yanan End
            }
            if (newCon.Address3__c != oldCon.Address3__c) {
                newCon.Address3D__c = newCon.Address3__c;
                System.debug('Address3D_Encrypted__c');
                // PIPL Update 20220420 By Chen Yanan Start
                newCon.Address3D_Encrypted__c = newCon.Address3_Encrypted__c;
                // PIPL Update 20220420 By Chen Yanan End
            }
            if (newCon.Postcode__c != oldCon.Postcode__c) {
                newCon.PostcodeD__c = newCon.Postcode__c;
                System.debug('PostcodeD_Encrypted__c');
                // PIPL Update 20220420 By Chen Yanan Start
                newCon.PostcodeD_Encrypted__c = newCon.Postcode_Encrypted__c;
                // PIPL Update 20220420 By Chen Yanan End
            }
                
            if (newCon.ContactStatus__c != oldCon.ContactStatus__c) {
                newCon.ContactStatusD__c = newCon.ContactStatus__c;
            }
            if (newCon.CancelReason__c != oldCon.CancelReason__c) {
                newCon.CancelReasonD__c = newCon.CancelReason__c;
            }
            System.debug('updateForDealer newCon: ' + newCon);
        }
            
 
    }
    
    public static void dealerContactApproval(List<Contact> newList, Map<Id, Contact> newMap, List<Contact> oldList, Map<Id, Contact> oldMap) {
        List<String> accIdList = new List<String>();
        for (Contact newCon : newList) {
            accIdList.add(newCon.AccountId);
        }
        // 现有客户小组
        Map<String, String> accsMap = new Map<String, String>();
        //List<AccountTeamMember> atmList = [select Id, AccountId, UserId from AccountTeamMember where AccountId in :accIdList];
        List<AccountShare> accsList = [select id, accountId, UserOrGroupId, AccountAccessLevel, RowCause from AccountShare where AccountId in :accIdList and RowCause = 'Team'];
        for (AccountShare accs : accsList) {
            accsMap.put(accs.AccountId + '' + accs.UserOrGroupId, accs.Id);
        }
        // 需要更新客户小组
//      List<AccountShare> updList = new List<AccountShare>();
 
        //新的获取简档ID calendarUtil.getMemberProfileID  这里用到的人员ID 和获取到的简档ID都是15位的  
        // String new_profileId = UserInfo.getProfileId().subString(0,15);
        String new_profileId = calendarUtil.getMemberProfileID(UserInfo.getUserId().subString(0,15));
 
        for (Contact newCon : newList) {
            Contact oldCon = oldMap.get(newCon.Id);
 
            if (new_profileId.subString(0,15) == System.label.Market_Department || new_profileId.subString(0,15) == System.label.Market_Department1){
                newCon.MobilePhone = newCon.MobilePhoneD__c;
                newCon.OtherPhone = newCon.OtherPhoneD__c;
                newCon.Fax = newCon.FaxD__c;
                newCon.Email = newCon.EmailD__c;
                newCon.Phone = newCon.PhoneD__c;
                newCon.Title = newCon.TitleD__c;
                newCon.Address1__c = newCon.Address1D__c;
                newCon.Address2__c = newCon.Address2D__c;
                newCon.Address3__c = newCon.Address3D__c;
                newCon.Postcode__c = newCon.PostcodeD__c;
                
                newCon.ContactStatus__c = newCon.ContactStatusD__c;
                newCon.CancelReason__c = newCon.CancelReasonD__c;
                // PIPL Update 20220420 By Chen Yanan Start
                newCon.MobilePhone_Encrypted__c = newCon.MobilePhoneD_Encrypted__c;
                newCon.OtherPhone_Encrypted__c = newCon.OtherPhoneD_Encrypted__c;
                newCon.Fax_Encrypted__c = newCon.FaxD_Encrypted__c;
                newCon.Email_Encrypted__c = newCon.EmailD_Encrypted__c;
                newCon.Phone_Encrypted__c = newCon.PhoneD_Encrypted__c;
                newCon.Title_Encrypted__c = newCon.TitleD_Encrypted__c;
                newCon.Address1_Encrypted__c = newCon.Address1D_Encrypted__c;
                newCon.Address2_Encrypted__c = newCon.Address2D_Encrypted__c;
                newCon.Address3_Encrypted__c = newCon.Address3D_Encrypted__c;
                newCon.Postcode_Encrypted__c = newCon.PostcodeD_Encrypted__c;
                // PIPL Update 20220420 By Chen Yanan End
            } else if (newCon.StatusD__c != oldCon.StatusD__c) {
                if (newCon.StatusD__c == 'Submit') {
                    //newCon.DealerSelectOwner__c = newCon.Account.DealerSelectOwner__c;
                }
                if (newCon.StatusD__c == 'Pass') {
                    // 批准过程肯定是一条一条批准的,所以这里再循环中写了select文
                    
                    List<Account> accList = [select Id, IsNew__c, AccountStatus__c from Account where Id = :newCon.AccountId];
                    if (accList.size() > 0 ) {
                        Account acc = accList[0];
                        if (acc.IsNew__c == true || (acc.AccountStatus__c == 'Cancel' && newCon.ContactStatusD__c != 'Cancel')) {
                            newCon.addError('客户无效或未通过审批,不能批准当前联系人。');
                        }
                    }
                    
                    newCon.MobilePhone = newCon.MobilePhoneD__c;
                    newCon.OtherPhone = newCon.OtherPhoneD__c;
                    newCon.Fax = newCon.FaxD__c;
                    newCon.Email = newCon.EmailD__c;
                    newCon.Phone = newCon.PhoneD__c;
                    newCon.Title = newCon.TitleD__c;
                    newCon.Address1__c = newCon.Address1D__c;
                    newCon.Address2__c = newCon.Address2D__c;
                    newCon.Address3__c = newCon.Address3D__c;
                    newCon.Postcode__c = newCon.PostcodeD__c;
                    
                    newCon.ContactStatus__c = newCon.ContactStatusD__c;
                    newCon.CancelReason__c = newCon.CancelReasonD__c;
                    // PIPL Update 20220420 By Chen Yanan Start
                    System.debug('Pass');
                    newCon.MobilePhone_Encrypted__c = newCon.MobilePhoneD_Encrypted__c;
                    newCon.OtherPhone_Encrypted__c = newCon.OtherPhoneD_Encrypted__c;
                    newCon.Fax_Encrypted__c = newCon.FaxD_Encrypted__c;
                    newCon.Email_Encrypted__c = newCon.EmailD_Encrypted__c;
                    newCon.Phone_Encrypted__c = newCon.PhoneD_Encrypted__c;
                    newCon.Title_Encrypted__c = newCon.TitleD_Encrypted__c;
                    newCon.Address1_Encrypted__c = newCon.Address1D_Encrypted__c;
                    newCon.Address2_Encrypted__c = newCon.Address2D_Encrypted__c;
                    newCon.Address3_Encrypted__c = newCon.Address3D_Encrypted__c;
                    newCon.Postcode_Encrypted__c = newCon.PostcodeD_Encrypted__c;
                    // PIPL Update 20220420 By Chen Yanan End
                    
//                  if (accsMap.containsKey(newCon.AccountId + '' + newCon.OwnerId) == true && newCon.IsNew__c == true) {
//                      AccountShare upd = new AccountShare(
//                          Id = accsMap.get(newCon.AccountId + '' + newCon.OwnerId),
//                          AccountAccessLevel = 'Edit'
//                      );
//                      updList.add(upd);
//                  }
                    
                    if (newCon.IsNew__c = true) {
                        newCon.IsNew__c = false;
                    }
                }
                if (newCon.StatusD__c == 'Reject' && newCon.IsNew__c == false) {
                    newCon.MobilePhoneD__c = newCon.MobilePhone;
                    newCon.OtherPhoneD__c = newCon.OtherPhone;
                    newCon.FaxD__c = newCon.Fax;
                    newCon.EmailD__c = newCon.Email;
                    newCon.PhoneD__c = newCon.Phone;
                    newCon.TitleD__c = newCon.Title;
                    newCon.Address1D__c = newCon.Address1__c;
                    newCon.Address2D__c = newCon.Address2__c;
                    newCon.Address3D__c = newCon.Address3__c;
                    newCon.PostcodeD__c = newCon.Postcode__c;
                    
                    newCon.ContactStatusD__c = newCon.ContactStatus__c;
                    newCon.CancelReasonD__c = newCon.CancelReason__c;
                    // PIPL Update 20220420 By Chen Yanan Start
                    System.debug('Reject');
                    newCon.MobilePhoneD_Encrypted__c = newCon.MobilePhone_Encrypted__c;
                    newCon.OtherPhoneD_Encrypted__c = newCon.OtherPhone_Encrypted__c;
                    newCon.FaxD_Encrypted__c = newCon.Fax_Encrypted__c;
                    newCon.EmailD_Encrypted__c = newCon.Email_Encrypted__c;
                    newCon.PhoneD_Encrypted__c = newCon.Phone_Encrypted__c;
                    newCon.TitleD_Encrypted__c = newCon.Title_Encrypted__c;
                    newCon.Address1D_Encrypted__c = newCon.Address1_Encrypted__c;
                    newCon.Address2D_Encrypted__c = newCon.Address2_Encrypted__c;
                    newCon.Address3D_Encrypted__c = newCon.Address3_Encrypted__c;
                    newCon.PostcodeD_Encrypted__c = newCon.Postcode_Encrypted__c;
                    // PIPL Update 20220420 By Chen Yanan End
                }
            System.debug('dealerContactApproval newCon: ' + newCon);
            }
        }
        // 更新客户小组
//      if (updList!= null && updList.size() > 0) {
//          update updList;
//      }
    }
}