涂煌豪
2022-05-23 78ac43791ac9e9d75ce4c33e9dddbdffa88fec2f
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/**********************************************************************
 * 
 *
 * @url: /services/apexrest/NFM624RestAbout
 * @data:
 *  {
        
    }
*************************************************************************/
 
@RestResource(urlMapping = '/NFM624About/*')
global with sharing class NFM624RestAbout {
    
    private static final String LOG_TYPE = 'NFM624About';
 
    global class GeDatas {
        public NFMUtil.Monitoring Monitoring;
        public GeData[] GeData;
        }
    global class GeData{
 
           public String PersonManagementCode; //人员管理编码 
        public String HospitalManagementCode2; //医院平台编码  平台编码 PlatformCode__c
        public String DepartmentManagementCode2; //科室平台编码  平台编码 PlatformCode__c
        //public String Name; //姓名
        public String NameEncrypted;//姓名密文 add 20220215 
        public String RelatedHospital; //关联SFDC医院 客户管理编码(手写) AgentCode_Ext__c
        public String DepartmentClass; //战略科室
        public String DepartmentName; //所属科室
        public String RelatedDepartment; //关联SFDC科室 客户管理编码(手写) AgentCode_Ext__c
        //public Boolean AgentFlag; //经销商标识
        public String dataId;
        public String AccountName; //医院名
        public String MobileEncrypted; //电话密文 add 20220519 thh
    }
 
 
 
    global class Response {
        public List<ResponseDetail> ResponseDetails {get;set;}
    }
     
    global class ResponseDetail{
        public String  dataId;
        public String  awsDataId; //=CONTACT.AWS_Data_Id__c
        public String  sfId;
    }  
 
    @HttpPost
    global static void execute() {
 
        // 取得接口传输内容
        String strData = RestContext.request.requestBody.toString();
        GeDatas ges = (GeDatas) JSON.deserializeStrict(strData, GeDatas.class);
        system.debug('ges---'+JSON.serialize(ges) );
 
        if (ges == null) {
            return;
        }
 
        NFMUtil.Monitoring Monitoring = ges.Monitoring;
        if (Monitoring == null) {
            system.debug('monitoring62');
            return;
        }
        
        String result;
        try {
            BatchIF_Log__c rowData = NFMUtil.saveRowData(Monitoring, LOG_TYPE, ges.GeData);
            system.debug('rowData = '+rowData);
            system.debug('String.isBlank(rowData.Log__c) == false?');
            if (String.isBlank(rowData.Log__c) == false) {
             System.debug('进入if方法,成功调用executefuture');
                result = JSON.serialize(main(rowData.Id));
                system.debug('result'+result);
             // result main(rowData.Id);
            }
        } catch (Exception ex) {
            System.debug(Logginglevel.ERROR, LOG_TYPE + ':' + ex.getMessage());
            System.debug(Logginglevel.ERROR, LOG_TYPE + ':' + ex.getStackTraceString());
        }
        // BatchIF_Log__c rowData = NFMUtil.saveRowData(Monitoring, LOG_TYPE, ges.GeData);
        
 
        // JSONを戻す
        RestResponse res = RestContext.response;
        res.addHeader('Content-Type', 'application/json');
        res.statusCode = 200;
        //  NFMUtil.NFMResponse result = NFMUtil.getNFMResponse();
        //  result.SFStatus=SFStatus;
        //  result.SFMessage=SFMessage;
        String jsonResponse = result;
        //for test sushanhu 20220401 start
        //ResponseDetail red =new ResponseDetail();
        // red.dataId =ges.[0].dataId;
        //red.awsDataId ='959174215334887424';
        //red.sfId ='0031m00000N7leyAAB';
        // Response resp =new Response();
        // List<ResponseDetail> listName = new List<ResponseDetail>();
        // listName.add(red);
        // resp= (Response)listName;
        //for test sushanhu 20220401 end
        //String jsonResponse = '{"dataID": ""}';        
        //system.debug('result---'+jsonResponse);
        res.responseBody = blob.valueOf(jsonResponse);//need
        //String resp ='{"ResponseDetails":[{"dataId":"959573117074669568","awsDataId":"959174215334887424","sfId":"0031m00000N7leyAAB"}]}';
        //res.responseBody = blob.valueOf(resp);
        
 
         //updata response toAWS 20220225 sushanhu end
        return;
    }
 
 
    @future
    global static void executefuture(String rowData_Id) {
        system.debug('进入executefuture方法' + rowData_Id);
        main(rowData_Id);
    }
 
    global static Response main(String rowData_Id) {
        Integer batch_retry_max_cnt = Integer.valueOf(System.Label.batch_retry_max_cnt);
        BatchIF_Log__c rowData = [Select Id, Name, Log__c,Is_Error__c, ErrorLog__c, Log2__c, Log3__c, Log4__c, Log5__c, Log6__c, Log7__c, Log8__c, Log9__c, Log10__c, Log11__c, Log12__c, MessageGroupNumber__c, retry_cnt__c,NFM624_Secondary_processing__c  from BatchIF_Log__c where RowDataFlg__c = true and Id =: rowData_Id];
        String logstr = rowData.MessageGroupNumber__c + ' start\n';
        BatchIF_Log__c iflog = new BatchIF_Log__c();
        iflog.Type__c = LOG_TYPE;
        iflog.MessageGroupNumber__c = rowData.MessageGroupNumber__c;
        iflog.Log__c = logstr;
        iflog.ErrorLog__c = '';
        insert iflog;
        String rowDataStr = NFMUtil.getRowDataStr(rowData);
        List < GeData > GeDataList = (List < GeData > ) JSON.deserialize(rowDataStr, List < GeData > .class);
        // if (GeDataList == null || GeDataList.size() == 0) {
        //     return null;
        // }
        Savepoint sp = Database.setSavepoint();
        List < string > PersonManagementCodeList = new List < String > (); //人员管理编码List
        List < String > SFDCCodeList = new List < String > (); 
        List < String > ManagementList = new List < String > (); //医院、科室平台编码List
        List < String > NameList = new List < String >();
        Response responseList = new Response();//返回体List
        ResponseDetail red = new ResponseDetail();
        responseList.ResponseDetails = new List<ResponseDetail>(); 
        try { 
        //根据传过来的管理编码查询如果可以查询得到结果则更新,查询不到则新增
        // for(GeData gedata : GeDataList){
        //     ResponseDetail temp = new ResponseDetail();
        //     temp.dataID = gedata.dataID;
        //     temp.awsDataId = null;
        //     temp.sfId = null;
        //     responseList.ResponseDetails.add(temp);
        // }
             //根据传过来的管理编码查询如果可以查询得到结果则更新,查询不到则新增
             List < Gedata > newGeDataList = new List < Gedata > ();
             for (Gedata gedata: GeDataList) {
                system.debug('gedata.NameEncrypted'+gedata.NameEncrypted);
                red.dataID = gedata.dataID;
                red.awsDataId = null;
                red.sfid = null;
                 if(string.isnotblank(gedata.HospitalManagementCode2)){
                     ManagementList.add(gedata.HospitalManagementCode2);
                 }
                 if(string.isnotblank(gedata.DepartmentManagementCode2)){
                     ManagementList.add(gedata.DepartmentManagementCode2);
                 }
                 if (string.isnotblank(gedata.RelatedHospital)) {
                     SFDCCodeList.add(gedata.RelatedHospital);
                 }
                 if (string.isnotblank(gedata.RelatedDepartment)) {
                     SFDCCodeList.add(gedata.RelatedDepartment);
                 }
                 if (string.isnotblank(gedata.PersonManagementCode)) {
                     PersonManagementCodeList.add(gedata.PersonManagementCode);
                 }
                if(String.isnotBlank(gedata.NameEncrypted)){
                   NameList.add(gedata.NameEncrypted);
                }
                 newGeDataList.add(gedata);
                 System.debug('ManagementList'+ManagementList);
                 System.debug('SFDCCodeList'+SFDCCodeList);
             }
             if (newGeDataList.size() > 0){
                 system.debug('newGeDataList.size()'+newGeDataList.size());
                 //查询联系人
                 Map < String, Map < String, Contact >> nameMap = new Map < String, Map < String, Contact >> (); //外层战略科室,内层科室名+联系人名
                 List < Contact > peopleList = [select Id, 
                                                       Account.Name, 
                                                       //Department__c, 
                                                       Account.parent.Name, 
                                                       UnifiedI_Contact_ID__c,
                                                       //Account.PlatformCode__c, 
                                                       Account.Management_Code__c, 
                                                       //Account.parent.Department_Class_Label__c, 
                                                       Name,LastName_Encrypted__c,
                                                       //Account.Parent_Management_Code__c, 
                                                       //Account.Parent_PlatformCode__c, 
                                                       CManageCode__c,AWS_Data_Id__c,
                                                       // 通过姓名+手机号判断人员是否重复 thh 20220518 start
                                                       MobilePhone_Encrypted__c
                                                       // 通过姓名+手机号判断人员是否重复 thh 20220518 end
                                                       //AccountId 
                                                       from Contact 
                                                       where CManageCode__c IN: PersonManagementCodeList 
                                                      OR (Account.Parent_PlatformCode__c IN: ManagementList AND LastName_Encrypted__c IN:NameList)
                                                      OR (Account.Parent_Management_Code__c IN: SFDCCodeList AND LastName_Encrypted__c IN:NameList )
                                                      ORDER BY  MobilePhone ASC,CManageCode__c ASC]; //根据人员管理编码检索联系人
                 Map < String, Contact > peopleMap = new Map < String, Contact > (); //联系人
                 system.debug('peopleList  '+peopleList);
                 for (Contact ct: peopleList) {
                     if (string.isnotblank(ct.Account.parent.Name)) {
                         Map < String, Contact > snameMap = new Map < String, Contact > ();
                         string namekey;
                         string nameKey2;
                         if(String.isBlank(ct.MobilePhone_Encrypted__c)){
                            namekey = ct.Account.Name + ' ' + ct.LastName_Encrypted__c.trim();
                            nameKey2 = ct.Account.Management_Code__c + ' ' + ct.LastName_Encrypted__c.trim();
                         } else{
                            namekey = ct.Account.Name + ' ' + ct.LastName_Encrypted__c.trim() + ' ' + ct.MobilePhone_Encrypted__c.trim();
                            nameKey2 = ct.Account.Management_Code__c + ' ' + ct.LastName_Encrypted__c.trim() + ' ' + ct.MobilePhone_Encrypted__c.trim();
                         }
                         snameMap.put(namekey,ct);
                         if (nameMap.containsKey(ct.Account.parent.Name)) {
                             snameMap = nameMap.get(ct.Account.parent.Name);
                         }
                         snameMap.put(nameKey2,ct); 
                         snameMap.put(namekey, ct);
                         // // 通过姓名+手机号判断人员是否重复 thh 20220518 start
                         // System.System.debug('ct.MobilePhone_Encrypted__c1:' + ct.MobilePhone_Encrypted__c);
                         // if(String.isBlank(ct.MobilePhone_Encrypted__c)){
                         //    snameMap.put(ct.LastName_Encrypted__c.trim(), ct);
                         // }else{
                         //    snameMap.put(ct.LastName_Encrypted__c.trim() + ct.MobilePhone_Encrypted__c.trim(), ct);
                         // }
                         // System.System.debug('ct.MobilePhone_Encrypted__c2:' + ct.MobilePhone_Encrypted__c);
                         // // 通过姓名+手机号判断人员是否重复 thh 20220518 end
                         nameMap.put(ct.Account.parent.Name, snameMap);
                         system.debug('snameMap'+snameMap.keyset());
                     }
                     peopleMap.put(ct.CManageCode__c, ct);
                     System.debug('peopleMap'+peopleMap);
                 }
                 for (Gedata gedata: newGeDataList) {
                     String personCode = gedata.PersonManagementCode;
                     string DepartmentClasskey = gedata.AccountName + ' ' + gedata.DepartmentClass;
                     if(string.isnotblank(personCode)){//判断有人员管理编码
                         //有人员管理编码
                         system.debug('有人员管理编码');
                         if (peopleMap.containsKey(personCode)) {
                            system.debug('有人员管理编码取到值');
                            peopleMap.get(personCode);
                            //red.dataID = gedata.dataID;
                            red.awsDataId = peopleMap.get(personCode).AWS_Data_Id__c;
                            red.sfid = peopleMap.get(personCode).id;
                         }else{
                            system.debug('有人员管理编码未取到值');
                            // red.dataID = gedata.dataID;
                            // red.awsDataId = null;
                            // red.sfid = null;
                         }
                         system.debug('red----------->'+red);
                         responseList.ResponseDetails.add(red);
                     }else{
                        //无人员管理编码
                        string namekey = gedata.RelatedDepartment + ' ' + gedata.NameEncrypted + ' ' + gedata.MobileEncrypted;
                        string namekey1 = gedata.RelatedDepartment + ' ' + gedata.NameEncrypted;
                        system.debug('DepartmentClasskey     =    '+DepartmentClasskey);
                        system.debug('nameMap22222222    '+nameMap);
                        if (nameMap.containskey(DepartmentClasskey)) { 
                            system.debug('nameMap.containskey(DepartmentClasskey'+nameMap.containskey(DepartmentClasskey));
                             Map < String, Contact > sMap = nameMap.get(DepartmentClasskey);
                             System.debug('sMap'+sMap);
                             System.debug('sMap.containsKey(namekey)'+sMap.containsKey(namekey));
                             System.debug('Namekey'+namekey);
                             if (sMap.containsKey(namekey)) {
                                system.debug('根据名字匹配存在');
                                red.awsDataId = sMap.get(namekey).AWS_Data_Id__c;
                                red.sfid = sMap.get(namekey).id;
                            } else if(sMap.containsKey(namekey1) && sMap.get(namekey1).UnifiedI_Contact_ID__c == null) {
                                red.awsDataId = sMap.get(namekey1).AWS_Data_Id__c;
                                red.sfid = sMap.get(namekey1).id;
                                
                            // // 通过姓名+手机号判断人员是否重复 thh 20220518 start
                            // } else if(sMap.containsKey(gedata.NameEncrypted + gedata.MobileEncrypted) && sMap.get(gedata.NameEncrypted + gedata.MobileEncrypted).UnifiedI_Contact_ID__c == null){
                                // red.awsDataId = sMap.get(gedata.NameEncrypted + gedata.MobileEncrypted).AWS_Data_Id__c;
                                // red.sfid = sMap.get(gedata.NameEncrypted + gedata.MobileEncrypted).id;
                            // // 通过姓名+手机号判断人员是否重复 thh 20220518 end
                            }else { // 7 完成
                                system.debug('根据名字匹配不存在');
                                //red.dataID = gedata.dataID;
                                //red.awsDataId = null;
                                //red.sfid = null;
                            }
                            system.debug('red----------->'+red);
                            responseList.ResponseDetails.add(red);
                            system.debug('responseList.ResponseDetails'+responseList.ResponseDetails);
                        }else{
                            responseList.ResponseDetails.add(red);
                        }
                    }    
                }
            }
            rowData.retry_cnt__c = 0;
            if (System.Test.isRunningTest()) {
                throw new ControllerUtil.myException('aaa');
            }
    }catch (Exception ex) {
            // エラーが発生した場合
           
            logstr += ex.getMessage();
            iflog.ErrorLog__c += ex.getMessage() + '\n';
            iflog.ErrorLog__c += ex.getStackTraceString() + '\n';
        }
        update rowData;
        system.debug('Rowdata'+rowData);   
        logstr += responseList +'\n';
        logstr += 'end';
        iflog.Log__c = logstr;
        update iflog;
        system.debug('responseList.ResponseDetails'+responseList.ResponseDetails);
        return responseList;  
    }
}