19626
2023-04-24 d7ac8beb8661d04356647196f6555c2eeed9055e
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/**
 * 2023-04-14  
 * Lightning new Repair
 */
public with sharing class lexNewRepairAuraController {
    public static String sobjectTypeValue = 'Repair__c';
    //查询Repair数据  修理对象的new和update 
    @AuraEnabled
    public static ReturnData init(String recordId,String urlStr) {
        ReturnData res = new ReturnData();
        String sobjectId = [SELECT CustomObjectId,CustomObjectName  FROM CustomObjectUserLicenseMetrics   where CustomObjectName ='Repair' limit 1].CustomObjectId;
        String Input_Required_Field_Msg = Label.Input_Required_Field_Msg;
        String PIPL_Name_Label = Label.PIPL_Name_Label;
        String PIPL_Input_Account_Error_Msg = label.PIPL_Input_Account_Error_Msg;
        Map<string,string> mso = (Map<string,string>)JSON.deserialize(urlStr, Map<string,string>.class);
        system.debug('mso='+mso);
        if (recordId != null) {
            res.recordTypeId = [SELECT Id,RecordTypeId FROM Repair__c WHERE Id = :recordId].RecordTypeId;
            res.status = true;
        }
        String RepairSubOrderAWSDataId;
            // AssignValueFromUrl(mso,controller.getRecord());
            //通过id查      问题
            //PIPL zhj 2022-11-11 start
            //从报修子单过来00N10000009H1rR
        if (mso.containsKey('CF00N10000009H1rR_lkid')) {
            String repairSubOrderId = mso.get('CF00N10000009H1rR_lkid');
            System.debug('repairSubOrderId = ' + repairSubOrderId);
            //查询AWSDataId
            List<RepairSubOrder__c> rso = [select AWS_Data_Id__c from RepairSubOrder__c where Id=:repairSubOrderId];
            if(rso.size() > 0){
                //todo 从报修子单过来,需要解密报修人  lwc是否需要进行解密处理?
               RepairSubOrderAWSDataId = rso[0].AWS_Data_Id__c;
               res.AWS_Data_Id = rso[0].AWS_Data_Id__c;
               res.status = true;
            }
        }
        //PIPL zhj 2022-11-11 end
        List<Repair__c> rc = new List<Repair__c>();
        system.debug('retURL=' + mso.containsKey('retURL'));
        if (mso.containsKey('retURL')) {
            String RepairId = mso.get('retURL').substring(1);
            System.debug('RepairId = ' + RepairId);
            rc = [select Hospital__c,Department_Class__c,Account__c,Dealer__c,Incharge_Staff_Contact__c,Incharge_Staff__c,  RepairSubOrder__c,On_Call_ID__c,QIS_ID__c,  InsReport__c,Rental_Apply_Equipment_Set_Detail__c from Repair__c where id =:RepairId ];
            System.debug('rc = ' + rc);
            if(rc.size() > 0){
                //已有数据赋值
                 //医院
                res.Hospital = rc[0].Hospital__c;
                //战略科室分类
                res.Department_Class = rc[0].Department_Class__c;
                //科室
                res.Account = rc[0].Account__c;
                //经销商名
                res.Dealer = rc[0].Dealer__c;
                //修理委托者(员工)
                res.Incharge_Staff_Contact = rc[0].Incharge_Staff_Contact__c;
                //修理委托者(FSE)
                res.Incharge_Staff = rc[0].Incharge_Staff__c;
                //报修子单RepairSubOrder__c
                res.RepairSubOrder = rc[0].RepairSubOrder__c;
                //查询AWSDataId
                List<RepairSubOrder__c> rso = [select AWS_Data_Id__c from RepairSubOrder__c where Id=:rc[0].RepairSubOrder__c];
                if(rso.size() > 0){
                    RepairSubOrderAWSDataId = rso[0].AWS_Data_Id__c;
                    res.AWS_Data_Id = rso[0].AWS_Data_Id__c;
                }
                //On-Call号码On_Call_ID__c
                res.On_Call_ID = rc[0].On_Call_ID__c;
                //QIS号码QIS_ID__c
                res.QIS_ID = rc[0].QIS_ID__c;
                //点检报告书InsReport__c
                res.InsReport = rc[0].InsReport__c;
                //借出备品配套一览明细Rental_Apply_Equipment_Set_Detail__c
                res.Rental_Apply_Equipment_Set_Detail = rc[0].Rental_Apply_Equipment_Set_Detail__c;
 
                res.status = true;
            }
           
        }
        //2023/04/23  lwc的保存并新建 保存后刷新页面
        // PIHelper.PIIntegration piIntegration = PIHelper.getPIIntegrationInfo('Repair__c');
        // PIHelper.PIIntegration piIntegrationAddress = PIHelper.getPIIntegrationInfo('Address__c');
        // PIHelper.PIIntegration piIntegrationContact = PIHelper.getPIIntegrationInfo('Contact');
        // PIHelper.PIIntegration piIntegrationRepairSubOrder = PIHelper.getPIIntegrationInfo('RepairSubOrder__c');
        // String staticResource = JSON.serialize(piIntegration);
        // //zhj MEBG新方案改造 2022-11-29 start
        // String staticResourceV2 = JSON.serialize(PIHelper.getPIIntegrationInfo('Repair__cV2'));
        // //zhj MEBG新方案改造 2022-11-29 end
        // String staticResourceAddress = JSON.serialize(piIntegrationAddress);
        // String staticResourceContact = JSON.serialize(piIntegrationContact);
        // String staticResourceRepairSubOrder = JSON.serialize(piIntegrationRepairSubOrder);
        // List<String> encryptedAPIList = piIntegration.PIFields;
        // String sobjectPrefix = piIntegration.sobjectPrefix;
 
        //save and new url   page处保存并新建跳转地址:new?recordTypeId=01210000000QmS9&additionalParams=retURL%3D%252Fa0J%252Fo%26&count=1
        // String newUrl = '/setup/ui/recordtypeselect.jsp?ent=' + sobjectId + '&retURL=/' + sobjectPrefix + '/o&save_new_url=/' + sobjectPrefix + '/e?retURL=%2F' + sobjectPrefix + '%2Fo';
        // res.newUrl = newUrl;
 
        return res;
    }
 
    public static String getAllFieldNames(String objName){
        Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        List<Schema.sObjectField> fields = schemaMap.get(objName).getDescribe().fields.getMap().values();
        
        String fieldString = '';
        for( Schema.sObjectField field : fields ){
            fieldString += field + ',';
        }
        fieldString = fieldString.removeEnd(',');
        
        return fieldString;
    }
 
    // @AuraEnabled
    public static List <LayoutDescriberHelper.LayoutSection > init1(String recordId,String urlStr) {
        String sobjectId = [SELECT CustomObjectId,CustomObjectName  FROM CustomObjectUserLicenseMetrics   where CustomObjectName ='Repair' limit 1].CustomObjectId;
        Boolean isNewMode = true;
        String Input_Required_Field_Msg = Label.Input_Required_Field_Msg;
        String PIPL_Name_Label = Label.PIPL_Name_Label;
        String PIPL_Input_Account_Error_Msg = label.PIPL_Input_Account_Error_Msg;
        String rtTypeId;
        // String sobjecttypeForFrontEnd = sobjectTypeValue;
        //获取所有字段
        // List<String> fieldList = new List<String>(Schema.getGlobalDescribe().get('Repair__c').getDescribe().fields.getMap().keyset());  
        // Add fields to controller. This is to avoid the SOQL error in visualforce page
        // if(!Test.isRunningTest()){
        //    controller.addFields(fieldList);  
        // }
        // SObject obj = controller.getRecord();        
        if(recordId == ''){
            //更新  页面判断
            // isNewMode = false;
            Repair__c repairData = [select Id,RecordTypeId,AWS_Data_Id__c,Address_AWS_Data_Id__c,Contact_AWS_Data_Id__c,Address_Contacts_Encrypt__c,Address_Contacts_Name_Encrypt__c,Address_Telephone_Encrypt__c,Address_ZipCode_Encrypt__c,Detailed_Address_Encrypt__c from Repair__c where id =: recordId];
            rtTypeId = repairData.RecordTypeId;
            String AWSDataId = repairData.AWS_Data_Id__c;
            String DecryptAWSDataId = String.valueOf(repairData.Address_AWS_Data_Id__c);
            String ContactAWSDataId = String.valueOf(repairData.Contact_AWS_Data_Id__c);
        }else{
            //get url 参数
            // Map<string,string> mso = ApexPages.currentPage().getParameters();
            Map<string,string> mso = (Map<string,string>)JSON.deserialize(urlStr, Map<string,string>.class);
            system.debug('mso='+mso);
            // AssignValueFromUrl(mso,controller.getRecord());
            //通过id查      问题
            //PIPL zhj 2022-11-11 start
            //从报修子单过来00N10000009H1rR
            if (mso.containsKey('CF00N10000009H1rR_lkid')) {
                String repairSubOrderId = mso.get('CF00N10000009H1rR_lkid');
                System.debug('repairSubOrderId = ' + repairSubOrderId);
                //查询AWSDataId
                List<RepairSubOrder__c> rso = [select AWS_Data_Id__c from RepairSubOrder__c where Id=:repairSubOrderId];
                if(rso.size() > 0){
                   String RepairSubOrderAWSDataId = rso[0].AWS_Data_Id__c;
                }
            }
            //PIPL zhj 2022-11-11 end
            List<Repair__c> rc = new List<Repair__c>();
            system.debug('retURL=' + mso.containsKey('retURL'));
            if (mso.containsKey('retURL')) {
                String RepairId = mso.get('retURL').substring(1);
                System.debug('RepairId = ' + RepairId);
                rc = [select Hospital__c,Department_Class__c,Account__c,Dealer__c,Incharge_Staff_Contact__c,Incharge_Staff__c,  RepairSubOrder__c,On_Call_ID__c,QIS_ID__c,  InsReport__c,Rental_Apply_Equipment_Set_Detail__c from Repair__c where id =:RepairId ];
                System.debug('rc = ' + rc);
                if(rc.size() > 0){
                    //已有数据赋值  defaultValue
                     /*//医院
                    controller.getRecord().put('Hospital__c',rc[0].Hospital__c);
                    //战略科室分类
                    controller.getRecord().put('Department_Class__c',rc[0].Department_Class__c);
                    //科室
                    controller.getRecord().put('Account__c',rc[0].Account__c);
                    //经销商名
                    controller.getRecord().put('Dealer__c',rc[0].Dealer__c);
                    //修理委托者(员工)
                    controller.getRecord().put('Incharge_Staff_Contact__c',rc[0].Incharge_Staff_Contact__c);
                    //修理委托者(FSE)
                    controller.getRecord().put('Incharge_Staff__c',rc[0].Incharge_Staff__c);
                    //报修子单RepairSubOrder__c
                    controller.getRecord().put('RepairSubOrder__c',rc[0].RepairSubOrder__c);
                    //查询AWSDataId
                    List<RepairSubOrder__c> rso = [select AWS_Data_Id__c from RepairSubOrder__c where Id=:rc[0].RepairSubOrder__c];
                    if(rso.size() > 0){
                        RepairSubOrderAWSDataId = rso[0].AWS_Data_Id__c;
                    }
                    //On-Call号码On_Call_ID__c
                    controller.getRecord().put('On_Call_ID__c',rc[0].On_Call_ID__c);
                    //QIS号码QIS_ID__c
                    controller.getRecord().put('QIS_ID__c',rc[0].QIS_ID__c);
                    //点检报告书InsReport__c
                    controller.getRecord().put('InsReport__c',rc[0].InsReport__c);
                    //借出备品配套一览明细Rental_Apply_Equipment_Set_Detail__c
                    controller.getRecord().put('Rental_Apply_Equipment_Set_Detail__c',rc[0].Rental_Apply_Equipment_Set_Detail__c);*/
                }
               
            }
            
            // rtTypeId = ApexPages.currentPage().getParameters().get('RecordType');
            rtTypeId = mso.get('RecordType');
            if(String.isBlank(rtTypeId)||String.isEmpty(rtTypeId)){
                List<RecordType> rtList = new List<RecordType>([select Id,DeveloperName from RecordType  where SobjectType ='Repair__c' and DeveloperName ='Repair']);
                rtTypeId = rtList[0].Id;
            }
        }
        LayoutDescriberHelper.LayoutWrapper LayoutWrapperValue = LayoutDescriberHelper.describeSectionWithFieldsWrapper(rtTypeId, 'Repair__c','classic');
        List <LayoutDescriberHelper.LayoutSection > layoutSections = LayoutWrapperValue.layoutSections;
        String layoutSectionsStr = JSON.serialize(layoutSections); //for dynamic add readonly attribute
        List<String> requiredFieldAPIList = LayoutWrapperValue.requiredFieldAPIList;
        Map<String,String> fieldAPIToLabelMap = LayoutWrapperValue.fieldAPIToLabelMap;
        String requiredFieldAPIListStr = JSON.serialize(requiredFieldAPIList);
        String fieldAPIToLabelMapStr = JSON.serialize(fieldAPIToLabelMap);
        PIHelper.PIIntegration piIntegration = PIHelper.getPIIntegrationInfo('Repair__c');
        PIHelper.PIIntegration piIntegrationAddress = PIHelper.getPIIntegrationInfo('Address__c');
        PIHelper.PIIntegration piIntegrationContact = PIHelper.getPIIntegrationInfo('Contact');
        PIHelper.PIIntegration piIntegrationRepairSubOrder = PIHelper.getPIIntegrationInfo('RepairSubOrder__c');
        String staticResource = JSON.serialize(piIntegration);
        //zhj MEBG新方案改造 2022-11-29 start
        String staticResourceV2 = JSON.serialize(PIHelper.getPIIntegrationInfo('Repair__cV2'));
        //zhj MEBG新方案改造 2022-11-29 end
        String staticResourceAddress = JSON.serialize(piIntegrationAddress);
        String staticResourceContact = JSON.serialize(piIntegrationContact);
        String staticResourceRepairSubOrder = JSON.serialize(piIntegrationRepairSubOrder);
        List<String> encryptedAPIList = piIntegration.PIFields;
        String sobjectPrefix = piIntegration.sobjectPrefix;
        //获取lookup字段
        List<String> VLookUpFields = new List<String>();
        for (LayoutDescriberHelper.LayoutSection ls : layoutSections) {
            for (LayoutDescriberHelper.LayoutField lf : ls.layoutFields) {
                if (lf.fieldAPI != '' && lf.fieldType == 'reference') {
                    VLookUpFields.add(lf.fieldAPI);
                }
            }
        }
        String VLookUpFieldsJson = Json.serialize(VLookUpFields);
        System.debug(LoggingLevel.INFO, '*** layoutSectionsLWC: ' + layoutSections);
        return layoutSections;
    }
 
    // 从url参数赋值到当前页面
    public static void AssignValueFromUrl(Map<string,string> mso, sobject sobj){
        
        String sobject_name = sobj.getSObjectType().getDescribe().getName();
        Map<string,object> temp = new Map<string,object>();
        Map<string,FieldDefinition> fdm = new Map<string,FieldDefinition>();
        List<FieldDefinition> fds = [SELECT Id, DurableId, QualifiedApiName,ValueTypeId , EntityDefinitionId, NamespacePrefix,EntityDefinition.NamespacePrefix, DeveloperName, MasterLabel, Label FROM FieldDefinition where EntityDefinition.QualifiedApiName = :sobject_name];
        for(FieldDefinition fd : fds){
            //system.debug(fd.DurableId);
            fdm.put(fd.DurableId.split('\\.')[1],fd);
        }
        
        for(string key : mso.keySet()){
            if (key.toLowerCase() == 'id') {
                System.debug('skip id assign');
                continue;
            }
            string new_key = key;
            system.debug('new_key='+new_key);
            if(new_key.contains('_lkid')){
                new_key = new_key.replace('_lkid', '');
                new_key = new_key.substring(2);
            }else{
                if(temp.containsKey(new_key)){
                    continue;
                }
            }
            
            system.debug('now new_key='+new_key);
            if(fdm.containsKey(new_key)){
                system.debug('fdm.get(new_key)='+fdm.get(new_key));
                string val_str = mso.get(key);
                system.debug('val_str='+val_str);
                /*无需做decode,sf内部已经做好
                try{
                    val_str = EncodingUtil.urlDecode(mso.get(key),'UTF-8');
                }catch(Exception e){
                    continue;
                    system.debug('Exception from get Key:'+e.getMessage());
                    system.debug(e.getStackTraceString());
                } */             
                object val = null;
                string type_id = fdm.get(new_key).ValueTypeId;
                // address, boolean, date, datetime, double, id, location, string, time
                if(string.isBlank(val_str)){
                    val = null;
                }else if(type_id == 'boolean'){
                    if(val_str == '1'){
                        val = true;
                    }else{
                        val = boolean.valueOf(val_str);
                    }
                }else if(type_id == 'date'){
                    //
                    try{
                        val = date.parse(val_str);
                    }catch(Exception e){
                        system.debug('val_str='+val_str);
                        system.debug(e.getMessage());
                        system.debug(e.getStackTraceString());
                        try{
                            val = date.valueOf(val_str);
                        }catch(Exception ee){
                            system.debug('val_str='+val_str);
                            system.debug(ee.getMessage());
                            system.debug(ee.getStackTraceString());
                            continue;
                        }
                    }
                }else if(type_id == 'datetime'){
                    //
                    try{
                        val = datetime.parse(val_str);
                    }catch(Exception e){
                        system.debug('val_str='+val_str);
                        system.debug(e.getMessage());
                        system.debug(e.getStackTraceString());
                        try{
                            val = datetime.valueOf(val_str);
                        }catch(Exception ee){
                            system.debug('val_str='+val_str);
                            system.debug(ee.getMessage());
                            system.debug(ee.getStackTraceString());
                            continue;
                        }
                    }
                }else if(type_id == 'double' || type_id == 'number'){
                    try{
                        val = decimal.valueOf(val_str.replace(',', ''));
                    }catch(Exception ee){
                        system.debug('val_str='+val_str);
                        system.debug(ee.getMessage());
                        system.debug(ee.getStackTraceString());
                        continue;
                    }
                    
                }else if(type_id == 'id' || type_id == 'string'){
                    val = val_str;
                }else{
                    system.debug('type_id='+type_id+' is not support to convert');
                    continue;
                }
                temp.put(fdm.get(new_key).QualifiedApiName,val);
            }else{
                system.debug(key+' is not in fdm');
            }
        }
        
        for(string key : temp.keySet()){
            system.debug('assign '+key+'='+temp.get(key));
            try{
                sobj.put(key, temp.get(key));
            }catch(Exception e){
                system.debug(e.getMessage());
                system.debug(e.getStackTraceString());
            }
        }
    }
 
    public class RetrievalData {
        @AuraEnabled
        public String Status1;
        @AuraEnabled
        public String Status2;
        @AuraEnabled
        public String RepairName;
        @AuraEnabled
        public String SAPRepairNo;
        @AuraEnabled
        public String SerialNumber;
        @AuraEnabled
        public String onSiteRepair;
        @AuraEnabled
        public String workLocationSelect;
        @AuraEnabled
        public String State_Hospital;
        @AuraEnabled
        public String HospitalName;
        @AuraEnabled
        public Repair__c repair;
 
        public RetrievalData(){
            repair = new Repair__c();
        }
    }
 
    public class RepairData {
        @AuraEnabled
        public Boolean IFCheck;
        @AuraEnabled
        public Repair__c repair;
        public RepairData(Repair__c RepairInfo){
            IFCheck = null;
            repair = RepairInfo;
        }
    }
    //2023/04/18 返回查询结果和状态
    public class ReturnData {
        //判断是否有Repair的默认值
        @AuraEnabled
        public Boolean status;
        // @AuraEnabled
        // public Repair__c repair;
        //todo 报修人
        @AuraEnabled
        public String AWS_Data_Id;
        //医院
        @AuraEnabled
        public String Hospital;
        @AuraEnabled
        public String Department_Class;
        @AuraEnabled
        public String Account;
        @AuraEnabled
        public String Dealer;
        @AuraEnabled
        public String Incharge_Staff_Contact;
        @AuraEnabled
        public String Incharge_Staff;
        @AuraEnabled
        public String RepairSubOrder;
        @AuraEnabled
        public String On_Call_ID;
        @AuraEnabled
        public String QIS_ID;
        @AuraEnabled
        public String InsReport;
        @AuraEnabled
        public String Rental_Apply_Equipment_Set_Detail;
        @AuraEnabled
        public String recordTypeId;
        // @AuraEnabled
        // public String newUrl;
 
        public ReturnData(){
            this.status = false;
        }
    }
}