高章伟
2022-02-24 2aa8da8af66aa8ae00f25831aed6bb0364176e7b
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
/**
 * wangweipeng     2021/09/02
 * update          2021/11/10
 * 用于把ods__c表的数据同步到 user表中
 */
public with sharing class UpsertContactHandler {
 
    //新增或修改user表数据
    public static void UpsertContact(List<ODS__c> newList, Map<Id, ODS__c> newMap, List<ODS__c> oldList, Map<Id, ODS__c> oldMap){
        Map<Id,ODS__c> updateMap = new Map<Id,ODS__c>();
        if(newList != null && newList.size() > 0){
            //用于存放触发trigger的ods数据的员工编号
            List<String> employeeNoList = new List<String>();
            //获取自定义标签,自己定义,用来空更新数据或处理历史数据
            Integer buffer = Integer.valueOf(System.Label.ODS_Null_Update_Sign);
            for (ODS__c os : newList) {
                if(String.isNotBlank(os.Employee_No__c) && os.Job_Type__c != '劳务-派遣' && os.Job_Type__c != '劳务-临时' && os.Job_Type__c != '无' && String.isNotBlank(os.Job_Type__c) && String.isNotBlank(os.Stay_or_not__c) && os.Stay_or_not__c != '待入职'){
                    //如果为update,那么判断值是否发生变化
                    //判断值数据发生变化
                    if(Trigger.isUpdate){
                        //统括本部、本部、部、课、员工编码、入职日期、电子邮件、手机号码、职位、职种、负责产品(主)、销售工作内容、工作地、在职/已离职、离职日期
                        //以上字段的值是否发生变换
                        //如果发生变化,那么更新user表数据,user表更新contact表数据
                        if(buffer == 1 || os.Category3__c != oldMap.get(os.id).Category3__c || os.Category4__c != oldMap.get(os.id).Category4__c 
                            || os.Category5__c != oldMap.get(os.id).Category5__c || os.Category6__c != oldMap.get(os.id).Category6__c 
                            || os.Employee_No__c != oldMap.get(os.id).Employee_No__c || os.Hire_Date__c != oldMap.get(os.id).Hire_Date__c 
                            || os.Email__c != oldMap.get(os.id).Email__c || os.MobilePhone__c != oldMap.get(os.id).MobilePhone__c 
                            || os.Post__c != oldMap.get(os.id).Post__c || os.Job_Category__c != oldMap.get(os.id).Job_Category__c 
                            || os.Product_specialist_incharge_product__c != oldMap.get(os.id).Product_specialist_incharge_product__c 
                            || os.Sales_Speciality__c != oldMap.get(os.id).Sales_Speciality__c || os.Work_Location__c != oldMap.get(os.id).Work_Location__c 
                            || os.Stay_or_not__c != oldMap.get(os.id).Stay_or_not__c || os.LeaveDate__c != oldMap.get(os.id).LeaveDate__c )
                        {
                            employeeNoList.add(os.Employee_No__c.trim());
                        }
                    }else{
                        employeeNoList.add(os.Employee_No__c.trim());
                    }
                }
            }
 
            //判断是否有需要更新的数据
            if(employeeNoList.size() > 0){
                //存放需要修改的user表数据
                List<User> updateUserDate = new List<User>();
                //存放需要新增的user数据
                List<User> insertUserDate = new List<User>();
                //查询user
                List<User> userList = [SELECT ID,NAME,Group_sales_dept__c,Dept__c,Category5__c,Category6__c,Employee_No__c,Hire_Date__c,Email,Mobile_Phone__c,Post__c,Job_Category__c,Product_specialist_incharge_product__c,Sales_Speciality__c,Work_Location__c,Stay_or_not__c,QuitDate__c,IsMEBG__c FROM USER WHERE Employee_No__c IN : employeeNoList and Employee_No__c != null ];
 
                //以下根据ods传过来的数据来判断user表到底是update还是insert
                if(userList != null && userList.size() > 0){
                    for(ODS__c osc : newList){
                        if(String.isNotBlank(osc.Employee_No__c) && osc.Job_Type__c != '劳务-派遣' && osc.Job_Type__c != '劳务-临时' && osc.Job_Type__c != '无' && String.isNotBlank(osc.Job_Type__c) && String.isNotBlank(osc.Stay_or_not__c) && osc.Stay_or_not__c != '待入职'){
                            User userData = new User();
                            boolean falg = true;
                            //循环user表
                            for(User us : userList){
                                //判断员工编码在user表中是否存在
                                if(osc.Employee_No__c.trim() == us.Employee_No__c.trim()){
                                    userData = setUserData(osc,us);
                                    if(String.isNotBlank(userData.Employee_No__c)){
                                        updateUserDate.add(userData);
                                    }
                                    falg = false;
                                    break;
                                }
                            }
                            //判断user表中是否有这个员工编码,如果没有,那么就需要新增user
                            if(falg){
                                userData = setUserData(osc,new User());
                                if(String.isNotBlank(userData.Employee_No__c)){
                                    insertUserDate.add(userData);
                                }
                            }
                        }
                    }
                }else{
                    //防止直接没有在user查到数据
                    for(ODS__c osc : newList){
                        if(String.isNotBlank(osc.Employee_No__c) && osc.Job_Type__c != '劳务-派遣' && osc.Job_Type__c != '劳务-临时' && osc.Job_Type__c != '无' && String.isNotBlank(osc.Job_Type__c) && String.isNotBlank(osc.Stay_or_not__c) && osc.Stay_or_not__c != '待入职'){
                            User userData = setUserData(osc,new User());
                            if(String.isNotBlank(userData.Employee_No__c)){
                                insertUserDate.add(userData);
                            }
                        }
                    }
                }
 
                Savepoint sp = Database.setSavepoint();
                try{
                    StaticParameter.EscapeContactInsUpdUser = true;
                    System.debug('**1*****************'+updateUserDate.size());
                    //修改用户信息
                    if(updateUserDate.size() > 0){
                        update updateUserDate;
                        /*Database.SaveResult[] lsr = Database.update(updateUserDate, false);
                        if(lsr.size() > 0){
                            for (Integer tIdx = 0; tIdx < lsr.size(); tIdx++) {
                                Database.SaveResult sr = lsr[tIdx];
                                if (!sr.isSuccess()) {
                                    System.debug('-------------ods数据保存到user表时修改报错:'+updateUserDate[tIdx].Employee_No__c+'。报错日志为:' + sr.getErrors()[0]);
                                }
                            }
                        }*/
                    }
                    //新增用户信息
                    if(insertUserDate.size() > 0){
                        insert insertUserDate;
                        /*Database.SaveResult[] lsr2 = Database.insert(insertUserDate, false);
                        for (Integer tIdx = 0; tIdx < lsr2.size(); tIdx++) {
                            Database.SaveResult sr = lsr2[tIdx];
                            if (!sr.isSuccess()) {
                                System.debug('-------------ods数据保存到user表时新增报错:'+updateUserDate[tIdx].Employee_No__c+'。报错日志为:' + sr.getErrors()[0]);
                            }
                        }*/
                    }
 
                    StaticParameter.EscapeContactInsUpdUser = false;
                }catch(exception e){
                    //失败提示
                    System.debug('----------------------出错了'+e.getMessage());
                     newList[0].addError( e.getMessage());
                     //ApexPages.addMessages(e);
                    Database.rollback(sp);
                }
            }
        }
    }
 
    /**
     *   ODS__c           USER
     * Category3        统括本部 
     * Category4        本部
     * Category5        部
     * Category6        课
     * Employee_No      员工编码
     * Hire_Date        入职日期
     * FirstName   
     * LastName    
     * Name             别名
     * Email            电子邮件
     * MobilePhone      手机号码
     * Post             职位
     * Job_Category     职种
     * Job_Type    
     * Product_specialist_incharge_product 负责产品(主)
     * Sales_Speciality     销售工作内容
     * Work_Location        工作地
     * Stay_or_not          在职/已离职
     * LeaveDate            离职日期
    */
    public static user setUserData(ODS__c odsc,User u){
        User userData = new User();
        //判断是否要新增
        //如果为空,那么就需要新增,不为空更新
        if(String.isNotBlank(u.Employee_No__c)){//修改user表数据
            //判断值是否发生变化
            /*if(odsc.Category3__c != u.Group_sales_dept__c || odsc.Category4__c != u.Dept__c || odsc.Category5__c != u.Category5__c 
                 || odsc.Category6__c != u.Category6__c || odsc.Employee_No__c != u.Employee_No__c || odsc.Hire_Date__c != u.Hire_Date__c 
                 || odsc.Email__c != u.Email || odsc.MobilePhone__c != u.Mobile_Phone__c || odsc.Post__c != u.Post__c 
                 || odsc.Job_Category__c != u.Job_Category__c || odsc.Product_specialist_incharge_product__c != u.Product_specialist_incharge_product__c 
                 || odsc.Sales_Speciality__c != u.Sales_Speciality__c || odsc.Work_Location__c != u.Work_Location__c 
                 || odsc.Stay_or_not__c != u.Stay_or_not__c || odsc.LeaveDate__c != u.QuitDate__c || !u.IsMEBG__c)
            {
                userData.Group_sales_dept__c = odsc.Category3__c;//统括本部
                userData.Dept__c = odsc.Category4__c;//本部
                userData.Category5__c = odsc.Category5__c;//部
                userData.Category6__c = odsc.Category6__c;//课
                userData.Employee_No__c = odsc.Employee_No__c;//员工编码
                userData.Hire_Date__c = odsc.Hire_Date__c;//入职日期
                //userData.Email = odsc.Email__c;//电子邮件
                userData.Mobile_Phone__c = odsc.MobilePhone__c;//手机号码
                userData.Post__c = odsc.Post__c;//职位
                userData.Job_Category__c = odsc.Job_Category__c;//职种
                userData.Product_specialist_incharge_product__c = odsc.Product_specialist_incharge_product__c;//负责产品(主)
                userData.Sales_Speciality__c = odsc.Sales_Speciality__c;//销售工作内容
                userData.Work_Location__c = odsc.Work_Location__c;//工作地
                userData.Stay_or_not__c = odsc.Stay_or_not__c;//在职/已离职
                userData.QuitDate__c = odsc.LeaveDate__c;//离职日期
                //userData.Alias = odsc.Name__c;//别名
                userData.Username_Proxy__c = odsc.Email__c;//用户名
                userData.IsMEBG__c=true;//是否MEBG
                userData.id = u.id;
            }*/
 
            userData.Group_sales_dept__c = odsc.Category3__c;//统括本部
            userData.Dept__c = odsc.Category4__c;//本部
            userData.Category5__c = odsc.Category5__c;//部
            userData.Category6__c = odsc.Category6__c;//课
            userData.Employee_No__c = odsc.Employee_No__c;//员工编码
            userData.Hire_Date__c = odsc.Hire_Date__c;//入职日期
            //userData.Email = odsc.Email__c;//电子邮件
            userData.Mobile_Phone__c = odsc.MobilePhone__c;//手机号码
            userData.Post__c = odsc.Post__c;//职位
            userData.Job_Category__c = odsc.Job_Category__c;//职种
            userData.Product_specialist_incharge_product__c = odsc.Product_specialist_incharge_product__c;//负责产品(主)
            userData.Sales_Speciality__c = odsc.Sales_Speciality__c;//销售工作内容
            userData.Work_Location__c = odsc.Work_Location__c;//工作地
            userData.Stay_or_not__c = odsc.Stay_or_not__c;//在职/已离职
            userData.QuitDate__c = odsc.LeaveDate__c;//离职日期
            //userData.Alias = odsc.Name__c;//别名
            userData.Username_Proxy__c = odsc.Email__c;//用户名
            userData.IsMEBG__c=true;//是否MEBG
            userData.id = u.id;
 
        }else{//新增user
            
            userData.Group_sales_dept__c = odsc.Category3__c;//统括本部
            userData.Dept__c = odsc.Category4__c;//本部
            userData.Category5__c = odsc.Category5__c;//部
            userData.Category6__c = odsc.Category6__c;//课
            userData.Employee_No__c = odsc.Employee_No__c;//员工编码
            userData.Hire_Date__c = odsc.Hire_Date__c;//入职日期
            userData.Email_Proxy__c = odsc.Email__c;//电子邮件,新建时,填写电子邮件
            userData.Username_Proxy__c = odsc.Employee_No__c+odsc.FirstName__c + odsc.LastName__c+'@prec-tech1.com';//用户名
            userData.Mobile_Phone__c = odsc.MobilePhone__c;//手机号码
            userData.Post__c = odsc.Post__c;//职位
            userData.Job_Category__c = odsc.Job_Category__c;//职种
            userData.Product_specialist_incharge_product__c = odsc.Product_specialist_incharge_product__c;//负责产品(主)
            userData.Sales_Speciality__c = odsc.Sales_Speciality__c;//销售工作内容
            userData.Work_Location__c = odsc.Work_Location__c;//工作地
            userData.Stay_or_not__c = odsc.Stay_or_not__c;//在职/已离职
            userData.QuitDate__c = odsc.LeaveDate__c;//离职日期
            userData.IsMEBG__c=true;//是否MEBG
            //由于ods的姓和名都是拼音,所以获取name__c的值,并且首字母第一位为姓,其余为名
            if(String.isNotBlank(odsc.Name__c)){
                if(odsc.Name__c.length() > 1){
                    userData.LastName = odsc.Name__c.substring(0,1);
                    userData.FirstName = odsc.Name__c.substring(1,odsc.Name__c.length());
                    userData.Alias = odsc.Name__c;
                }
            }
            
            //以下是系统默认
            userData.FederationIdentifier_Proxy__c = odsc.Employee_No__c + '@olympus.com.cn';
            userData.EmailEncodingKey = 'UTF-8';//电子邮件编码
            userData.IsActive_Proxy__c = false;
            userData.TimeZoneSidKey = 'Asia/Shanghai';//时区
            userData.LocaleSidKey = 'ja_JP';//区域
            userData.LanguageLocaleKey = 'zh_CN';//语言
            //userData.DefaultCurrencyIsoCode = 'CNY';//默认币种 ISO 代码
            //userData.CurrencyIsoCode = 'CNY';//信息币种
            //默认ods创建的user用户的简档为  999_看不见Profile
            userData.Profileid_Proxy__c = '00e10000000dFYJ';
        }
        return userData;
 
        /*ctc.ODS__c = odsc.Id;
        ctc.Isactive__c = '有效';
        ctc.AccountId = '0011000000eTSPC';//Olympus社内 其他 社内用户
        ctc.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Internal_staff').getRecordTypeId();
        if(!''.equals(idStr)){
            ctc.Id = idStr;
        }
        return ctc;*/
    }
}