Li Jun
2022-04-19 ced601e566d84624364f8e3a698eddd427a85f85
force-app/main/default/classes/RentalApplyController.cls
@@ -1,9 +1,11 @@
global without sharing class RentalApplyController {
    public String layoutSectionsStr {get; set;}//for dynamic add readonly attribute
    public List <LayoutDescriberHelper.LayoutSection > layoutSections{set;get;}
    public String awsToken{set;get;}
    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;}   
@@ -16,36 +18,105 @@
    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);
        SObject obj = controller.getRecord();
        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){
            //更新
            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];
            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>();
            sfIdToAWSIdMap.put(String.valueof(rentalApplyData.Loaner_medical_Staff__r.Id).subString(0,15),rentalApplyData.Loaner_medical_Staff__r.AWS_Data_Id__c);
            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);
        }else{
            //新建
            // Map<string,string> mso = ApexPages.currentPage().getParameters();
            system.debug('mso='+mso);
            if(mso.containsKey('Name')){
                controller.getRecord().put('Name',mso.get('Name'));
            }
            if(mso.containsKey('CF00N10000008ps61_lkid')){
                controller.getRecord().put('OPDPlan__c',mso.get('CF00N10000008ps61_lkid'));
            }
            if(mso.containsKey('CF00N10000003Mp1d_lkid')){
                controller.getRecord().put('Hospital__c',mso.get('CF00N10000003Mp1d_lkid'));
            }
            if(mso.containsKey('CF00N10000003O3V6_lkid')){
                controller.getRecord().put('Strategic_dept__c',mso.get('CF00N10000003O3V6_lkid'));
            }
            if(mso.containsKey('CF00N10000003Mp2R_lkid')){
                controller.getRecord().put('Account__c',mso.get('CF00N10000003Mp2R_lkid'));
            }
            if(mso.containsKey('00N10000003Msk0')){
                controller.getRecord().put('Demo_purpose1__c',mso.get('00N10000003Msk0'));
            }
            if(mso.containsKey('00N10000003Msk5')){
                controller.getRecord().put('demo_purpose2__c',mso.get('00N10000003Msk5'));
            }
            if(mso.containsKey('00N100000098amW')){
                controller.getRecord().put('NoOpp_Reason__c',mso.get('00N100000098amW'));
            }
            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());
        }
        if(mso.containsKey('00N10000003Mp2H')){
            system.debug('00N10000003Mp2H='+mso.get('00N10000003Mp2H'));
            controller.getRecord().put('Status__c',mso.get('00N10000003Mp2H'));
        }
        if(mso.containsKey('00N10000007oQKh')){
            system.debug('00N10000007oQKh='+mso.get('00N10000007oQKh'));
            controller.getRecord().put('Split_Apply_Reason__c',mso.get('00N10000007oQKh'));
        }
        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
        List<String> requiredFieldAPIList = LayoutWrapperValue.requiredFieldAPIList;
        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{
@@ -65,50 +136,33 @@
        Rental_Apply__c rentalApplyInfo = new Rental_Apply__c();
        //自定义格式转换
        for (String fieldAPI: fieldValueMap.keySet()) {
            system.debug('field API='+fieldAPI);
            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)); 
            system.debug('Field Type:'+fielddataType+' field Value='+fieldValue);
            if(String.valueOf(fielddataType)=='DATE'){
                rentalApplyInfo.put(fieldAPI,(String.isBlank(fieldValue)||String.isEmpty(fieldValue))? null:Date.valueOf(fieldValue.replace('/', '-')));
                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)=='CURRENCY'|| String.valueof(fielddataType)=='PERCENT'||String.valueOf(fielddataType)=='Number'||String.valueOf(fielddataType)=='DOUBLE' ){
                rentalApplyInfo.put(fieldAPI, (String.isBlank(fieldValue)||String.isEmpty(fieldValue))?0:Decimal.valueOf(fieldValue));
                }
            }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,fieldValue);
                rentalApplyInfo.put(fieldAPI, String.valueOf(fieldValueMap.get(fieldAPI)));
            }
            // system.debug('field API'+fieldAPI);
            // String fieldValue = String.valueOf(fieldAPIToTypeMap.get(fieldAPI));
            // if(String.isBlank(fieldValue) || String.isEmpty(fieldValue)){
            //     continue;
            // }
            // Schema.DescribeFieldResult fielddataType = fieldAPIToTypeMap.get(fieldAPI).getDescribe();
            // Schema.DisplayType fieldDatas = fielddataType.getType();
            // system.debug('fieldValue='+fieldValue);
            // if(String.valueOf(fielddatas)=='DATE'){
            //     rentalApplyInfo.put(fieldAPI, Date.valueOf(String.valueOf(fieldValueMap.get(fieldAPI)).replace('/', '-')));
            // }else if(String.valueOf(fielddatas)=='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.valueOf(fielddatas)=='Number'||String.valueOf(fielddatas)=='DOUBLE'){
            //     system.debug('decimal error:'+ String.valueOf(fieldValueMap.get(fieldAPI)));
            //     rentalApplyInfo.put(fieldAPI, decimal.valueOf(String.valueOf(fieldValueMap.get(fieldAPI))));
            // } else if(String.valueof(fielddatas)=='BOOLEAN'){
            //     rentalApplyInfo.put(fieldAPI, fieldValueMap.get(fieldAPI));
            // }else {
            //     rentalApplyInfo.put(fieldAPI, String.valueOf(fieldValueMap.get(fieldAPI)));
            // }
        }
        
        //2. Save Record Process
@@ -116,17 +170,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;
@@ -139,6 +202,26 @@
            resp.status = status;
            System.debug('respzhj = ' + resp);
            return resp;
        } catch(DmlException e) {
            rid=rentalApplyInfo.Id;
            Integer index = 0;
            System.debug(e.getNumDml());
            System.debug(e.getDmlFields(index));
            System.debug(e.getDmlId(index));
            System.debug(e.getDmlIndex(index));
            System.debug(e.getDmlMessage(index));
            System.debug(e.getDmlStatusCode(index));
            System.debug(e.getDmlType(index));
         system.debug(e.getMessage());
            system.debug(e.getStackTraceString());
            System.debug('into catch'+e.getMessage());
            Database.rollback(sp);
            resp.status = 'Exception';
            resp.message ='保存失败,原因:'+ e.getDmlMessage(index);
            PIHelper.saveTransLog(sobjectTypeValue,rid,transId, (String)rentalApplyInfo.get('AWS_Data_Id__c'),rentalApplyJson ,status,'');
            return resp;
        } catch(Exception e) {
            System.debug('into catch'+e.getMessage());
            Database.rollback(sp);