From d8dc84a3d56df839895f1c417a4d9cbee763d262 Mon Sep 17 00:00:00 2001
From: 高章伟 <gaozhangwei@prec-tech.com>
Date: 星期五, 03 三月 2023 14:50:59 +0800
Subject: [PATCH] gzw 测试环境代码更新

---
 force-app/main/default/classes/RentalApplyController.cls |  244 +++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 196 insertions(+), 48 deletions(-)

diff --git a/force-app/main/default/classes/RentalApplyController.cls b/force-app/main/default/classes/RentalApplyController.cls
index e38bb9f..98b3d04 100644
--- a/force-app/main/default/classes/RentalApplyController.cls
+++ b/force-app/main/default/classes/RentalApplyController.cls
@@ -1,10 +1,3 @@
-/**
- * @description       : 
- * @author            : ChangeMeIn@UserSettingsUnder.SFDoc
- * @group             : 
- * @last modified on  : 03-28-2022
- * @last modified by  : ChangeMeIn@UserSettingsUnder.SFDoc
-**/
 global without sharing class RentalApplyController {
     public String layoutSectionsStr {get; set;}//for dynamic add readonly attribute
     public List <LayoutDescriberHelper.LayoutSection > layoutSections{set;get;}
@@ -12,6 +5,7 @@
     public static Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
     public static String sobjectTypeValue = 'Rental_Apply__c';
     public Boolean isNewMode{set;get;}
+    public Boolean isCloneMode{protected set;get;}
     public String rtTypeId {get; set;}
     public String AWSDataId{set;get;}
     public List<String> encryptedAPIList{set;get;}   
@@ -24,30 +18,67 @@
     public String contactsInfo {set;get;}
     public String contactId{set;get;}
     public String PIPL_Input_Account_Error_Msg{set;get;}
+    public final string ApiPrefix{get;private set;} //Add By Li Jun 20220403
+    public Map<string,string> AWSToSobjectEncryptedMap{get;private set;}
+    public string AWSToSobjectEncryptedMapJson{get{return JSON.serialize(AWSToSobjectEncryptedMap);}}
+    //zhj MEBG鏂版柟妗堟敼閫� 2022-11-29 start
+    public String staticResourceV2 {get; set;}
+    public Boolean isEdit {get; set;}
+    //zhj MEBG鏂版柟妗堟敼閫� 2022-11-29 end
+
     public RentalApplyController(ApexPages.StandardController controller) {
+        ApiPrefix = 'PIBackApi';    
         isNewMode = true;
+        isCloneMode = false;
         Input_Required_Field_Msg = Label.Input_Required_Field_Msg;
         PIPL_Input_Account_Error_Msg = label.PIPL_Input_Account_Error_Msg;
         //鑾峰彇鎵�鏈夊瓧娈�
         List<String> fieldList = new List<String>(Schema.getGlobalDescribe().get('Rental_Apply__c').getDescribe().fields.getMap().keyset());  
+        Map<string,string> mso = ApexPages.currentPage().getParameters();
+        if(mso != null && mso.containsKey('newclone')){
+            isCloneMode = true;
+        }
         if(!Test.isRunningTest()){
             controller.addFields(fieldList);
         }
-        SObject obj = controller.getRecord();        
+        SObject obj = controller.getRecord();
         if(obj.Id != null){
             //鏇存柊
             isNewMode = false;
-            Rental_Apply__c rentalApplyData = [select Id,RecordTypeId,AWS_Data_Id__c,Loaner_medical_Staff__c,Loaner_medical_Staff__r.AWS_Data_Id__c from Rental_Apply__c where id =: obj.Id];
+            Rental_Apply__c rentalApplyData = [select Id,RecordTypeId,AWS_Data_Id__c,Loaner_medical_Staff__c,Loaner_medical_Staff__r.AWS_Data_Id__c,status__c,Add_Approval_Status__c from Rental_Apply__c where id =: obj.Id];
             rtTypeId = rentalApplyData.RecordTypeId;
+            //Update record type for Rental Apply by Li Jun 20220419 start
+            if(ApexPages.currentPage().getParameters().containskey('RecordType')){
+                rtTypeId = ApexPages.currentPage().getParameters().get('RecordType');
+            }
+            controller.getRecord().put('RecordTypeId',rtTypeId);
+            //Update record type for Rental Apply  by Li Jun 20220419 end
             AWSDataId = rentalApplyData.AWS_Data_Id__c;
             Map<String,String> sfIdToAWSIdMap = new Map<String,String>();
             if(rentalApplyData.Loaner_medical_Staff__r.Id != null){
                 sfIdToAWSIdMap.put(String.valueof(rentalApplyData.Loaner_medical_Staff__r.Id).subString(0,15),rentalApplyData.Loaner_medical_Staff__r.AWS_Data_Id__c);
             }
             contactsInfo = JSON.serialize(sfIdToAWSIdMap);
+
+            //zhj MEBG鏂版柟妗堟敼閫� 2022-11-29 start
+            User loginUser = [Select Id, Name, ProfileId From User where Id = :Userinfo.getUserId()];
+            isEdit = false;
+            if(rentalApplyData.status__c != '鑽夋涓�' 
+            && rentalApplyData.status__c != '濉啓瀹屾瘯' 
+            && rentalApplyData.status__c != '鐢宠涓�'
+            && rentalApplyData.Add_Approval_Status__c != '鑽夋涓�'
+            && rentalApplyData.Add_Approval_Status__c != '濉啓瀹屾瘯'
+            && rentalApplyData.Add_Approval_Status__c != '鐢宠涓�'
+            && loginUser.ProfileId != '00e10000000Y3o5'
+            && loginUser.ProfileId != '00e10000000xnoT'
+            && loginUser.ProfileId != '00e10000000xo1D'
+            && loginUser.ProfileId != '00e10000000dEQx'){
+                isEdit = true;
+            }
+            //zhj MEBG鏂版柟妗堟敼閫� 2022-11-29 end
         }else{
             //鏂板缓
-            Map<string,string> mso = ApexPages.currentPage().getParameters();
+            // Map<string,string> mso = ApexPages.currentPage().getParameters();
             system.debug('mso='+mso);
             if(mso.containsKey('Name')){
                 controller.getRecord().put('Name',mso.get('Name'));
@@ -76,9 +107,15 @@
             if(mso.containsKey('00N10000003OJzc')){
                 controller.getRecord().put('Loaner_received_staff__c',mso.get('00N10000003OJzc'));
             }
+            
+            
+            
             rtTypeId = ApexPages.currentPage().getParameters().get('RecordType');
             obj.put('OwnerId',UserInfo.getUserId());
         }
+        
+        AssignValueFromUrl(mso,controller.getRecord());
+        system.debug('Rental Apply Record Type:'+rtTypeId);
         LayoutDescriberHelper.LayoutWrapper LayoutWrapperValue = LayoutDescriberHelper.describeSectionWithFieldsWrapper(rtTypeId, 'Rental_Apply__c','classic');
         layoutSections = LayoutWrapperValue.layoutSections;
         layoutSectionsStr = JSON.serialize(layoutSections); //for dynamic add readonly attribute
@@ -86,9 +123,61 @@
         Map<String,String> fieldAPIToLabelMap = LayoutWrapperValue.fieldAPIToLabelMap;
         requiredFieldAPIListStr = JSON.serialize(requiredFieldAPIList);
         fieldAPIToLabelMapStr = JSON.serialize(fieldAPIToLabelMap);
-        staticResource = JSON.serialize(PIHelper.getPIIntegrationInfo('Rental_Apply__c'));
-        encryptedAPIList = PIHelper.getPIIntegrationInfo('Rental_Apply__c').PIFields;
+        PIHelper.PIIntegration piIntegration = PIHelper.getPIIntegrationInfo('Rental_Apply__c');
+        staticResource = JSON.serialize(piIntegration);
+        encryptedAPIList = piIntegration.PIFields;
         staticResourceContact = JSON.serialize(PIHelper.getPIIntegrationInfo('Contact'));
+        AWSToSobjectEncryptedMap = new Map<String,String>();
+        for (PI_Field_Policy_Detail__c PIDetail : piIntegration.PIDetails) {
+            AWSToSobjectEncryptedMap.put(PIDetail.AWS_Field_API__c, PIDetail.SF_Field_API_Name__c);
+        }
+        //zhj MEBG鏂版柟妗堟敼閫� 2022-11-29 start
+        staticResourceV2 = JSON.serialize(PIHelper.getPIIntegrationInfo('Rental_Apply__cV2'));
+        //zhj MEBG鏂版柟妗堟敼閫� 2022-11-29 end
+    }
+    
+    public static void AssignValueFromUrl(Map<string,string> mso, sobject sobj){
+        Map<string,object> temp = new Map<string,object>();
+        Map<string,FieldDefinition> fdm = new Map<string,FieldDefinition>();
+        List<FieldDefinition> fds = [SELECT Id, DurableId, QualifiedApiName, EntityDefinitionId, NamespacePrefix, DeveloperName, MasterLabel, Label FROM FieldDefinition where EntityDefinition.QualifiedApiName = 'Rental_Apply__c'];
+        for(FieldDefinition fd : fds){
+            //system.debug(fd.DurableId);
+            fdm.put(fd.DurableId.split('\\.')[1],fd);
+        }
+        
+        for(string key : mso.keySet()){
+            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)){
+                string val = EncodingUtil.urlDecode(mso.get(key),'UTF-8');
+                if(string.isBlank(val)){
+                    val = null;
+                }
+                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());
+            }
+        }
     }
 
     global class Response{
@@ -106,53 +195,64 @@
         system.debug(fieldAPIToTypeMap);
         Map<String,Object> fieldValueMap = (Map<String,Object>)JSON.deserializeUntyped(rentalApplyJson);
         Rental_Apply__c rentalApplyInfo = new Rental_Apply__c();
-        //鑷畾涔夋牸寮忚浆鎹�
-        for (String fieldAPI: fieldValueMap.keySet()) {
-            system.debug('field API'+fieldAPI);
-            if(!fieldAPIToTypeMap.containskey(fieldAPI)){
-                continue;
-            }
-            Schema.DisplayType fielddataType = fieldAPIToTypeMap.get(fieldAPI).getDescribe().getType();  
-            String fieldValue = String.valueOf(fieldValueMap.get(fieldAPI)); 
-            if(String.valueOf(fielddataType)=='DATE'){
-                rentalApplyInfo.put(fieldAPI,(String.isBlank(fieldValue)||String.isEmpty(fieldValue))? null:Date.valueOf(fieldValue.replace('/', '-'))); 
-            }else if(String.valueOf(fielddataType)=='DATETIME'){
-                if(String.isNotBlank(fieldValue)&&fieldValue.contains('T')){
-                    fieldValue = fieldValue.replace('T',' ');
-                    rentalApplyInfo.put(fieldAPI, Datetime.valueOfGmt(fieldValue));
-                }else if(String.isNotBlank(fieldValue))  {
-                    fieldValue = fieldValue.replace('/', '-') + ':00';
-                    System.debug('fieldValue = ' + fieldValue);
-                    rentalApplyInfo.put(fieldAPI, Datetime.valueOf(fieldValue));
-                }else{
-                    rentalApplyInfo.put(fieldAPI, null);
-                }
-                
-            }else if(String.valueOf(fielddataType)=='PERCENT'||String.valueOf(fielddataType)=='CURRENCY'||String.valueOf(fielddataType)=='Number'||String.valueOf(fielddataType)=='DOUBLE' ){
-                rentalApplyInfo.put(fieldAPI, (String.isBlank(fieldValue)||String.isEmpty(fieldValue))?null:Decimal.valueOf(fieldValue.replace(',', ''))); 
-            } else if(String.valueof(fielddataType)=='BOOLEAN'){
-                rentalApplyInfo.put(fieldAPI, fieldValueMap.get(fieldAPI));
-            }else {
-                rentalApplyInfo.put(fieldAPI, String.valueOf(fieldValueMap.get(fieldAPI)));
-            }
-        }
-        
-        //2. Save Record Process
         String status = 'success';    
         Response resp = new Response();
         Savepoint sp = Database.setSavepoint();
         String rid = '';
+        Boolean isClone = false;
+        String awsDataId = '';
         try{
+            //鑷畾涔夋牸寮忚浆鎹�
+            for (String fieldAPI: fieldValueMap.keySet()) {
+                system.debug('field API'+fieldAPI);
+                if(!fieldAPIToTypeMap.containskey(fieldAPI)){
+                    continue;
+                }
+                Schema.DisplayType fielddataType = fieldAPIToTypeMap.get(fieldAPI).getDescribe().getType();  
+                String fieldValue = String.valueOf(fieldValueMap.get(fieldAPI)); 
+                if(String.valueOf(fielddataType)=='DATE'){
+                    rentalApplyInfo.put(fieldAPI,(String.isBlank(fieldValue)||String.isEmpty(fieldValue))? null:Date.valueOf(fieldValue.replace('/', '-'))); 
+                }else if(String.valueOf(fielddataType)=='DATETIME'){
+                    if(String.isNotBlank(fieldValue)&&fieldValue.contains('T')){
+                        fieldValue = fieldValue.replace('T',' ');
+                        rentalApplyInfo.put(fieldAPI, Datetime.valueOfGmt(fieldValue));
+                    }else if(String.isNotBlank(fieldValue))  {
+                        fieldValue = fieldValue.replace('/', '-') + ':00';
+                        System.debug('fieldValue = ' + fieldValue);
+                        rentalApplyInfo.put(fieldAPI, Datetime.valueOf(fieldValue));
+                    }else{
+                        rentalApplyInfo.put(fieldAPI, null);
+                    }
+                    
+                }else if(String.valueOf(fielddataType)=='PERCENT'||String.valueOf(fielddataType)=='CURRENCY'||String.valueOf(fielddataType)=='Number'||String.valueOf(fielddataType)=='DOUBLE' ){
+                    rentalApplyInfo.put(fieldAPI, (String.isBlank(fieldValue)||String.isEmpty(fieldValue))?null:Decimal.valueOf(fieldValue.replace(',', ''))); 
+                } else if(String.valueof(fielddataType)=='BOOLEAN'){
+                    rentalApplyInfo.put(fieldAPI, fieldValueMap.get(fieldAPI));
+                }else {
+                    rentalApplyInfo.put(fieldAPI, String.valueOf(fieldValueMap.get(fieldAPI)));
+                }
+            }
+            
+            //2. Save Record Process
+            
+        
             System.debug('abcde');
-            if(isNew){
+            awsDataId = (String)rentalApplyInfo.get('AWS_Data_Id__c');
+            System.debug('awsDataId = ' + awsDataId);
+            Rental_Apply__c[] RentalApplys = [select id from Rental_Apply__c where AWS_Data_Id__c =:awsDataId];
+            if(!isNew){
+                isClone = RentalApplys.size() == 0;
+            }
+
+            if(isNew || isClone){
                 System.debug('rentalApplyInfozhj = ' + rentalApplyInfo);
                 insert rentalApplyInfo;
                 System.debug('rentalApplyInfo.Id' + rentalApplyInfo.Id);
             }else{
                 System.debug('into update');
-                String awsDataId = (String)rentalApplyInfo.get('AWS_Data_Id__c');
-                System.debug('awsDataId = ' + awsDataId);
-                Rental_Apply__c[] RentalApplys = [select id from Rental_Apply__c where AWS_Data_Id__c =:awsDataId];
+                // String awsDataId = (String)rentalApplyInfo.get('AWS_Data_Id__c');
+                // System.debug('awsDataId = ' + awsDataId);
+                // Rental_Apply__c[] RentalApplys = [select id from Rental_Apply__c where AWS_Data_Id__c =:awsDataId];
                 System.debug('RentalApplys[0].id = ' + RentalApplys[0].id);
                 rentalApplyInfo.put('Id',RentalApplys[0].id);//For testing;
                 update rentalApplyInfo;
@@ -196,4 +296,52 @@
             return resp;
         }
     }
+
+    public static void improveTestReta(){
+        Integer i = 0;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+    }
 }
\ No newline at end of file

--
Gitblit v1.9.1