高章伟
2023-03-03 d8dc84a3d56df839895f1c417a4d9cbee763d262
force-app/main/default/classes/NewAndEditBaseController.cls
@@ -6,6 +6,7 @@
    public static Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    public String sobjectTypeValue {private set; get;}
    public Boolean isNewMode{set;get;}
    public Boolean isCloneMode{protected set;get;}
    public String rtTypeId {get; set;}
    public String AWSDataId{set;get;}
    public String CurrentUserId{private set;get;}
@@ -30,7 +31,9 @@
        get{
            Map<string,string> temp = new Map<string,string>();
            temp.putAll(AWSToSobjectNonEncryptedMap);
            temp.putAll(AWSToSobjectEncryptedMap);
            //zhj MEBG新方案改造 2022-11-29 start
            //temp.putAll(AWSToSobjectEncryptedMap);
            //zhj MEBG新方案改造 2022-11-29 end
            return temp;
        }
    }
@@ -53,7 +56,7 @@
    public List<String> LookUpOverrideFields{get;private set;}
    public string LookUpOverrideFieldsMapJson{get; set;}
    public string recordId{get;private set;}
    public NewAndEditBaseController(){
        ApiPrefix = 'PIBackApi';
        AWSToSobjectNonEncryptedMap = new Map<string,string>();
@@ -71,8 +74,12 @@
        system.debug('obj='+sobjectTypeValue);
        
        isNewMode = true;
        isCloneMode = false;
        List<Sobject> lso = Database.query('select id from RecordType where SobjectType = :sobjectTypeValue');
        Map<string,string> mso = ApexPages.currentPage().getParameters();
        if(mso != null && mso.containsKey('newclone')){
            isCloneMode = true;
        }
        if(obj.Id != null){
            recordId = obj.Id;
            isNewMode = false;
@@ -187,13 +194,20 @@
        System.debug('sobjectTypeValue:'+sobjectTypeValue+' Info:' + JSON.serialize(leadJson));
        System.debug('json length='+leadJson.length());
        System.debug('leadJson---------'+leadJson);
        System.debug('isNew---------'+isNew);
        //1. Prepare the payload for  Lead
        Schema.SObjectType leadSchema = schemaMap.get(sobjectTypeValue);
        Map<String, Schema.SObjectField> fieldAPIToTypeMap = leadSchema.getDescribe().fields.getMap();
        Map<String,Object> fieldValueMap = (Map<String,Object>)JSON.deserializeUntyped(leadJson);
        
        system.debug('enter Foo');
        string rtid = null;
        if (fieldValueMap.containsKey('RecordTypeId')) {
            rtid = String.valueOf(fieldValueMap.get('RecordTypeId'));
        }
        List<string> invalid_fields = GetInvalidFieldFromLayout(rtid,sobjectTypeValue);
        Boolean isClone = false;
        //2. Save Record Process
        String status = 'success';    
        Response resp = new Response();
@@ -201,9 +215,15 @@
        Savepoint sp = Database.setSavepoint();
        Sobject leadInfo = sobj;
        try{
            System.debug('abcde');
            for (String fieldAPI: fieldValueMap.keySet()) {
                system.debug('field API='+fieldAPI);
                if(invalid_fields.contains(fieldAPI)){
                    system.debug(fieldAPI+' is invalid');
                    continue;
                }
                if(!fieldAPIToTypeMap.containskey(fieldAPI)){
                    continue;
                }
@@ -216,6 +236,11 @@
                    if(String.isNotBlank(fieldValue)&&fieldValue.contains('T')){
                        fieldValue = fieldValue.replace('T',' ');
                        leadInfo.put(fieldAPI, Datetime.valueOfGmt(fieldValue));
                    //20220405 By ChenYanan Start
                    }else if(String.isNotBlank(fieldValue))  {
                        fieldValue = fieldValue.replace('/', '-') + ':00';
                        leadInfo.put(fieldAPI, Datetime.valueOf(fieldValue));
                    //20220405 By ChenYanan End
                    }else{
                        leadInfo.put(fieldAPI, null);
                    }                    
@@ -229,22 +254,28 @@
            }
            system.debug('for (String fieldAPI: fieldValueMap.keySet()) end');
            awsDataId = (String)leadInfo.get('AWS_Data_Id__c');
            if (string.isBlank(awsDataId)) {
                throw new DMLException('更新时AWS_Data_Id__c不能为空');
            }
            System.debug('awsDataId = ' + awsDataId);
            Sobject[] sobjects = Database.query('select id from '+sobjectTypeValue+' where AWS_Data_Id__c =:awsDataId');
            
            if(isNew){
            if(!isNew){
                isClone = sobjects.size() == 0;
            }
            System.debug('isNew---------'+isNew);
            if(isNew || isClone){
                System.debug('leadInfozhj = ' + leadInfo);
                if(!Test.isRunningTest()){
                    insert leadInfo;
                }
            }else{
                System.debug('into update');
                awsDataId = (String)leadInfo.get('AWS_Data_Id__c');
                if (string.isBlank(awsDataId)) {
                    throw new DMLException('更新时AWS_Data_Id__c不能为空');
                }
                System.debug('awsDataId = ' + awsDataId);
                Sobject[] leads = Database.query('select id from '+sobjectTypeValue+' where AWS_Data_Id__c =:awsDataId');
                System.debug('leads[0].id = ' + leads[0].id);
                leadInfo.put('Id',leads[0].id);//For testing;
                System.debug('sobjects[0].id = ' + sobjects[0].id);
                leadInfo.put('Id',sobjects[0].id);//For testing;
                if(!Test.isRunningTest()){
                    update leadInfo;
                }
@@ -274,7 +305,7 @@
            System.debug(e.getDmlMessage(index));
            System.debug(e.getDmlStatusCode(index));
            System.debug(e.getDmlType(index));
         system.debug(e.getMessage());
            system.debug(e.getMessage());
            system.debug(e.getStackTraceString());
            System.debug('into catch'+e.getMessage());
@@ -288,11 +319,50 @@
            System.debug('into catch'+e.getMessage());
            Database.rollback(sp);
            resp.status = 'Exception';
            resp.message = e.getMessage()+e.getStackTraceString();
            //resp.message = e.getMessage()+e.getStackTraceString();
            System.debug('错误:' + e.getMessage()+e.getStackTraceString());
            resp.message = e.getMessage();
            PIHelper.saveTransLog(sobjectTypeValue,awsDataId,leadInfo.Id,transId, leadJson ,status,resp.message);
            // PIHelper.saveTransLog(sobjectTypeValue,(String)leadInfo.get('AWS_Data_Id__c'),transId, leadJson,status,e.getStackTraceString());
            return resp;
        }
    }
    
    public static List<string> GetInvalidFieldFromLayout(string rtid, string sobject_name){
        List<string> ls = new List<string>();
        string[] only_type = new string[]{'QIS_Report__c'};
        if(!only_type.contains(sobject_name)){
            system.debug('not allow');
            return ls;
        }
        List<Metadata.LayoutSection> sections = MetaDataUtility.GetRecordTypePageLayout(rtid, sobject_name);
        if (sections == null) {
            System.debug('sections=null');
            return ls;
        }
        system.debug(Json.serialize(sections));
        for (Metadata.LayoutSection section : sections) {
            if (section.layoutColumns != null) {
                for (Metadata.LayoutColumn layoutColumn : section.layoutColumns) {
                    if(layoutColumn.layoutItems != null){
                        for (Metadata.LayoutItem item : layoutColumn.layoutItems) {
                            System.debug(item);
                            if(item.field==null)continue;
                            if (item.behavior == Metadata.UiBehavior.READONLY  ) {
                                ls.add(item.field);
                            }
                        }
                    }
                }
            }
        }
        return ls;
    }
}