From ea5686cada5a3e63edaa0d3e8c2db8def4f9ff31 Mon Sep 17 00:00:00 2001
From: Li Jun <buli@deloitte.com.cn>
Date: 星期一, 11 四月 2022 11:52:41 +0800
Subject: [PATCH] PIPLCloneButton

---
 force-app/main/default/classes/RentalApplyController.cls |   81 ++++++++++++++++++++++++++--------------
 1 files changed, 52 insertions(+), 29 deletions(-)

diff --git a/force-app/main/default/classes/RentalApplyController.cls b/force-app/main/default/classes/RentalApplyController.cls
index f9b442d..fac0fb0 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-23-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,13 +18,24 @@
     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);}}
     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());  
-        controller.addFields(fieldList);
+        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();        
         if(obj.Id != null){
             //鏇存柊
@@ -45,7 +50,7 @@
             contactsInfo = JSON.serialize(sfIdToAWSIdMap);
         }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'));
@@ -84,9 +89,14 @@
         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);
+        }
     }
 
     global class Response{
@@ -106,24 +116,28 @@
         Rental_Apply__c rentalApplyInfo = new Rental_Apply__c();
         //鑷畾涔夋牸寮忚浆鎹�
         for (String fieldAPI: fieldValueMap.keySet()) {
-            Schema.DisplayType fielddataType = fieldAPIToTypeMap.get(fieldAPI).getDescribe().getType();  
-            String fieldValue = String.valueOf(fieldValueMap.get(fieldAPI)); 
-            if(String.isBlank(fieldValue)){
+            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'){
-                System.debug('DATE fieldAPI = '+fieldAPI+' filedData = '+String.valueOf(fieldValueMap.get(fieldAPI)));
-                rentalApplyInfo.put(fieldAPI, Date.valueOf(String.valueOf(fieldValueMap.get(fieldAPI)).replace('/', '-')));
+                rentalApplyInfo.put(fieldAPI,(String.isBlank(fieldValue)||String.isEmpty(fieldValue))? null:Date.valueOf(fieldValue.replace('/', '-'))); 
             }else if(String.valueOf(fielddataType)=='DATETIME'){
-                String dt = String.valueOf(fieldValueMap.get(fieldAPI));
-                if(String.isNotBlank(dt)&&dt.contains('T')){
-                    dt = dt.replace('T',' ');
-                    rentalApplyInfo.put(fieldAPI, Datetime.valueOfGmt(dt));
-                }else if(String.isNotBlank(dt))  {
-                    rentalApplyInfo.put(fieldAPI, Datetime.valueOf(dt));
-                }          
-            }else if(String.valueOf(fielddataType)=='Number'||String.valueOf(fielddataType)=='DOUBLE' ){
-                rentalApplyInfo.put(fieldAPI, Decimal.valueOf(String.valueOf(fieldValueMap.get(fieldAPI))));
+                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 {
@@ -136,17 +150,26 @@
         Response resp = new Response();
         Savepoint sp = Database.setSavepoint();
         String rid = '';
+        Boolean isClone = false;
+        String awsDataId = '';
         try{
             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;

--
Gitblit v1.9.1