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
public without sharing class UserFaultInfoHandler {
 
    public static void UserFaultInfoWorkingHoursAssignment(List<User_FaultInfo__c> newList, Map<Id, User_FaultInfo__c> newMap, List<User_FaultInfo__c> oldList, Map<Id, User_FaultInfo__c> oldMap, String type) {
        // 获取用户/故障信息下的业务活动
        List<String> selectUserFaultInfoIdList = new List<String>();
        for (User_FaultInfo__c uf : newList) {
            selectUserFaultInfoIdList.add(uf.Id);
        }
 
        // 查找用户故障信息下所有的业务活动
        List<BusinessActivity__c> businessActivityList = TSRepairUtil.GetBusinessActivityList(null, selectUserFaultInfoIdList, null, null, null, '');
        // 用户/故障信息 根据 审批意见 类型与 工时组成的Map
        Map<String, Decimal> OpinionMap = new Map<String, Decimal>();
        // 用户/故障信息 Id与工时组成的Map
        Map<String, Map<String, Decimal>> WorkingHourMap = new Map<String, Map<String, Decimal>>();
        if (businessActivityList != null && businessActivityList.size() > 0) {
 
            for (BusinessActivity__c getba : businessActivityList) {
 
                String ufId =  getba.User_FaultInfo__c;
                String opinion = getba.BusinessAApprovalOpinion__c;
                Decimal workingHours = 0;
                Map<String, Decimal> ufOpinionMap = new Map<String, Decimal>();
 
                if (WorkingHourMap.containsKey(ufId)) {
 
                    ufOpinionMap = WorkingHourMap.get(ufId);
 
                    if (ufOpinionMap.containsKey(opinion)) {
 
                        workingHours = ufOpinionMap.get(opinion);
                        workingHours += getba.workingHours__c;
                        ufOpinionMap.put(opinion, workingHours);
                    } else {
 
                        workingHours += getba.workingHours__c;
                        ufOpinionMap.put(opinion, workingHours);
                    }
                } else {
 
                    workingHours += getba.workingHours__c;
                    ufOpinionMap.put(opinion, workingHours);
                }
                WorkingHourMap.put(ufId, ufOpinionMap);
 
            }
            List<User_FaultInfo__c> updateUserFaultInfoList = new List<User_FaultInfo__c>();
 
            for (User_FaultInfo__c uf : newList) {
 
                for (String ufId : WorkingHourMap.keySet() ) {
 
                    if (uf.Id.equals(ufId)) {
 
                        Map<String, Decimal> ufOpinionMap = WorkingHourMap.get(ufId);
                        uf.UserFaultInfoWorkingHours__c = 0;
                        uf.UserFaultInfoUnconfirmedTime__c = 0;
 
                        for (String opinion : ufOpinionMap.keySet()) {
                            if ('同意'.equals(opinion)) {
                                uf.UserFaultInfoWorkingHours__c = ufOpinionMap.get(opinion);
                            } else {
                                uf.UserFaultInfoUnconfirmedTime__c = ufOpinionMap.get(opinion);
                            }
                        }
                    }
 
                }
 
                if ('insert'.equals(type)) {
                    updateUserFaultInfoList.add(uf);
                }
            }
 
            if (updateUserFaultInfoList.size() > 0) {
                update updateUserFaultInfoList;
            }
 
 
        }
 
    }
 
    public static void BeforeSendEmail(List<User_FaultInfo__c> newList, Map<Id, User_FaultInfo__c> newMap, List<User_FaultInfo__c> oldList, Map<Id, User_FaultInfo__c> oldMap) {
        for (User_FaultInfo__c newUf : newList) {
            newUf.IsSendEmail__c = true;
        }
 
    }
 
    // Case 上 ASSIGNED TO 变更 发送邮件
    public static void ASSIGNEDTOChange(List<User_FaultInfo__c> newList, Map<Id, User_FaultInfo__c> newMap, List<User_FaultInfo__c> oldList, Map<Id, User_FaultInfo__c> oldMap) {
        List<User_FaultInfo__Share> insertShareList = new List<User_FaultInfo__Share>();
        List<String> deleteShareList = new List<String>();
 
        for (User_FaultInfo__c newUf : newList) {
 
            Boolean beforUpdate = false;
            Boolean isSendEmail = false;
 
            Boolean afterInsert = false;
            Boolean changeASSIGNED_TO = false;
            Boolean changeCONTACT = false;
 
            if (Trigger.isBefore && Trigger.isUpdate) {
                isSendEmail = true;
                User_FaultInfo__c oldUf = oldMap.get(newUf.Id);
                // ASSIGNED TO 变更时发送邮件
                if (String.isNotBlank(newUf.ASSIGNED_TO__c) && oldUf.ASSIGNED_TO__c != newUf.ASSIGNED_TO__c) {
                    //变更时发送邮件
                    beforUpdate = true;
                    changeASSIGNED_TO = true;
                    insertShareList.add(ShareToUser(newUf.Id, newUf.ASSIGNED_TO__c));
                    deleteShareList.add(newUf.Id);
                }
                // CONTACT 变更时发送邮件
                if (String.isNotBlank(newUf.CONTACT__c) && oldUf.CONTACT__c != newUf.CONTACT__c) {
                    beforUpdate = true;
                    changeCONTACT = true;
                }
 
                if (oldUf.INTERNAL_ONLY__c != newUf.INTERNAL_ONLY__c && newUf.INTERNAL_ONLY__c) {
                    beforUpdate = true;
                    changeASSIGNED_TO = true;
                }
 
                if (oldUf.SEND_TO_CUSTOMER__c != newUf.SEND_TO_CUSTOMER__c && newUf.SEND_TO_CUSTOMER__c) {
                    beforUpdate = true;
                    changeCONTACT = true;
                }
            } else if (Trigger.isAfter && Trigger.isInsert) {
                if (String.isNotBlank(newUf.ASSIGNED_TO__c)) {
                    insertShareList.add(ShareToUser(newUf.Id, newUf.ASSIGNED_TO__c));
                    //新增时发送邮件
                    afterInsert = true;
 
                }
            }
 
 
            String userId = '';
            String contactId = '';
            String subjectStr = '';
            String messageStr = newUf.Interactions__c  == null ? '' : newUf.Interactions__c + '\n';
            messageStr += 'Case Details Link:\n';
            messageStr += System.Label.EnvironmentLink + newUf.Id;
 
            if (newUf.INTERNAL_ONLY__c || changeASSIGNED_TO) {
                userId = newUf.ASSIGNED_TO__c;
            }
 
            if (newUf.SEND_TO_CUSTOMER__c || changeCONTACT) {
                contactId =  newUf.CONTACT__c;
            }
            if (newUf.Interactions__c != null) {
 
                if (afterInsert) {
 
                } else {
                    newUf.interactionsHistory__c =  interactionsHistory(newUf);
                    newUf.Interactions__c = null;
                    beforUpdate = true;
                    if (newUf.IsSendEmail__c) {
                        beforUpdate = false;
                        newUf.IsSendEmail__c = false;
                    }
 
                }
            } else {
                if (isSendEmail) {
                    if (newUf.IsSendEmail__c) {
                        newUf.IsSendEmail__c = false;
                    }
                }
 
            }
            if (beforUpdate) {
                subjectStr = 'You have connected a new case, please follow up in time.';
                SendEmail(userId, contactId, newUf.Id, subjectStr, messageStr, 'update');
 
            }
            if (afterInsert) {
                subjectStr = 'A case has been created, please follow up in time.';
                SendEmail(userId, contactId , newUf.Id, subjectStr, messageStr, 'insert');
            }
        }
        // 删除数据共享
        if (deleteShareList.size() > 0) {
            String rowCause = Schema.User_FaultInfo__Share.RowCause.shareToUser__c;
            List<User_FaultInfo__Share> deleteList = [SELECT Id FROM User_FaultInfo__Share WHERE RowCause = :rowCause AND ParentId IN :deleteShareList];
            if (deleteList.size() > 0) {
                delete deleteList;
            }
        }
        // 新增数据共享
        if (insertShareList.size() > 0) {
            insert insertShareList;
        }
    }
 
    public static void SendEmail(String userId, String contactId, String ufId, String subjectStr, String messageStr, String type) {
        User_FaultInfo__c getCase = [ SELECT Id, Name, NUMBER__c, SUBJECT__c, COMPANY__r.Name FROM User_FaultInfo__c WHERE Id = :ufId];
        String caseName =  getCase.Name;
        // 保存之后要拼接 Case 的Name
        if ('insert'.equals(type)) {
            caseName = getCase.NUMBER__c + '-' + getCase.SUBJECT__c + '-' + getCase.COMPANY__r.Name;
            if (caseName.length() > 80) {
                caseName = caseName.substring(0, 80);
            }
 
        }
 
        List<String> toAddresses = new List<String>();
        if (String.isNotBlank(userId)) {
            User user = [select Name, Email from User where id = :userId limit 1];
            if (user != null && String.isNotBlank(user.Email)) {
                toAddresses.add(user.Email);
            }
        }
 
        if (String.isNotBlank(contactId)) {
            List<Contact> contactList = [SELECT Id, Name, AccountId__c, EmailD__c FROM Contact WHERE Id = :contactId limit 1];
            if (contactList.size() > 0 && String.isNotBlank(contactList[0].EmailD__c)) {
                toAddresses.add(contactList[0].EmailD__c);
            }
        }
 
        if (toAddresses.size() > 0) {
 
            String subject = subjectStr + ' ~Case:' + caseName;
            String message = messageStr;
            List<String> ccAddresses = new List<String>();
            Messaging.SingleEmailMessage sendMail = new Messaging.SingleEmailMessage();
            sendMail.setToAddresses(toAddresses);
            sendMail.setSubject(subject);
            sendMail.setPlainTextBody(message);
            sendMail.setBccSender(false);
            sendMail.setUseSignature(false);
            ccAddresses.add(System.label.EmailComeBackListen);
            ccAddresses.add('Dai_Prectech@olympus.com.cn');
            // ccAddresses.add('xinhonglu@prec-tech.com');
            ccAddresses.add('Xin_Prectech@olympus.com.cn');
 
            sendMail.setCcAddresses(ccAddresses);
            Messaging.SendEmailResult[] result_list = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { sendMail });
        }
    }
    /*
    parentId:数据Id
    userOrGroupId:待分享的用户Id
    创建数据共享
     */
    public static User_FaultInfo__Share ShareToUser (String parentId, String userOrGroupId) {
        String rowCause = Schema.User_FaultInfo__Share.RowCause.shareToUser__c;
        User_FaultInfo__Share apexShare = new User_FaultInfo__Share(
            RowCause = rowCause,
            ParentId = parentId,
            UserOrGroupId = userOrGroupId,
            AccessLevel = 'Edit');
        return apexShare;
    }
    public static String interactionsHistory(User_FaultInfo__c uf) {
        // 送信者
        String username = UserInfo.getName();
        // 发送时间
        Datetime dt = Datetime.now();
 
        // 现留言
        String temp = '';
        temp += '****** ' + username + ' ' + dt.format() + ' ******\n';
        temp += uf.Interactions__c;
        String oldResponse =  uf.interactionsHistory__c;
        if (oldResponse != null && oldResponse.trim().length() > 0) {
            temp += '\n\n' + oldResponse;
        }
        // uf.Interactions__c = null;
        // uf.interactionsHistory__c = temp;
        return temp;
    }
 
}