高章伟
2022-02-18 650847118307a1c9ae0ada15b7c69bbf5792c54c
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
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;
                }
            }
        }
    }
    
    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;
            }
            if (newCon.OtherPhone != oldCon.OtherPhone) {
                newCon.OtherPhoneD__c = newCon.OtherPhone;
            }
            if (newCon.Fax != oldCon.Fax) {
                newCon.FaxD__c = newCon.Fax;
            }
            if (newCon.Email != oldCon.Email) {
                newCon.EmailD__c = newCon.Email;
            }
            if (newCon.Phone != oldCon.Phone) {
                newCon.PhoneD__c = newCon.Phone;
            }
            if (newCon.Title != oldCon.Title) {
                newCon.TitleD__c = newCon.Title;
            }
            if (newCon.Address1__c != oldCon.Address1__c) {
                newCon.Address1D__c = newCon.Address1__c;
            }
            if (newCon.Address2__c != oldCon.Address2__c) {
                newCon.Address2D__c = newCon.Address2__c;
            }
            if (newCon.Address3__c != oldCon.Address3__c) {
                newCon.Address3D__c = newCon.Address3__c;
            }
            if (newCon.Postcode__c != oldCon.Postcode__c) {
                newCon.PostcodeD__c = newCon.Postcode__c;
            }
                
            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;
            }
        }
    }
    
    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;
            } 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;
                    
//                  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;
                }
            }
        }
        // 更新客户小组
//      if (updList!= null && updList.size() > 0) {
//          update updList;
//      }
    }
}