buli
2023-07-14 e6068da47c1bef5517c9e5fdc8c726766867ad4e
force-app/main/default/classes/NewRepairController.cls
@@ -1,63 +1,79 @@
/*
 *@Description:
 *@Author: Dennis Rodman
 *@Description:
 *@Author: Dennis Rodman
 *@Date: 2022-03-10 10:26:47
*/
 */
global without sharing class NewRepairController {
    public String layoutSectionsStr {get; set;}//for dynamic add readonly attribute
    public List <LayoutDescriberHelper.LayoutSection > layoutSections{set;get;}
    public String awsToken{set;get;}
    public String layoutSectionsStr { get; set; } //for dynamic add readonly attribute
    public List<LayoutDescriberHelper.LayoutSection> layoutSections { get; set; }
    public String awsToken { get; set; }
    public static Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    public static String sobjectTypeValue = 'Repair__c';
    public Boolean isNewMode{set;get;}
    public String rtTypeId {get; set;}
    public String AWSDataId{set;get;}
    public String DecryptAWSDataId{set;get;}
    public String ContactAWSDataId{set;get;}
    public List<String> encryptedAPIList{set;get;}
    public String staticResource {get; set;}
    public String staticResourceContact {get; set;}
    public String staticResourceAddress {get; set;}
    public String requiredFieldAPIListStr {get; set;}
    public String fieldAPIToLabelMapStr {get; set;}
    public String Input_Required_Field_Msg{set;get;}
    public String PIPL_Name_Label{set;get;}
    public String PIPL_Input_Account_Error_Msg{set;get;}
    public String sobjectPrefix{set;get;}
    public String sobjecttypeForFrontEnd{set;get;}
    public String sobjectId{set;get;}
    public String workLocationSelect{set;get;}
    public String RepairSubOrderAWSDataId{set;get;}
    public String staticResourceRepairSubOrder{set;get;}
    public String VLookUpFieldsJson{get;private set;}
    public Boolean isNewMode { get; set; }
    public String rtTypeId { get; set; }
    public String AWSDataId { get; set; }
    public String DecryptAWSDataId { get; set; }
    public String ContactAWSDataId { get; set; }
    public List<String> encryptedAPIList { get; set; }
    public String staticResource { get; set; }
    public String staticResourceContact { get; set; }
    public String staticResourceAddress { get; set; }
    public String requiredFieldAPIListStr { get; set; }
    public String fieldAPIToLabelMapStr { get; set; }
    public String Input_Required_Field_Msg { get; set; }
    public String PIPL_Name_Label { get; set; }
    public String PIPL_Input_Account_Error_Msg { get; set; }
    public String sobjectPrefix { get; set; }
    public String sobjecttypeForFrontEnd { get; set; }
    public String sobjectId { get; set; }
    public String workLocationSelect { get; set; }
    public String RepairSubOrderAWSDataId { get; set; }
    public String staticResourceRepairSubOrder { get; set; }
    public String VLookUpFieldsJson { get; private set; }
    //zhj MEBG新方案改造 2022-11-29 start
    public String staticResourceV2 {get; set;}
    public String staticResourceV2 { get; set; }
    //zhj MEBG新方案改造 2022-11-29 end
    public NewRepairController(ApexPages.StandardController controller) {
        sobjectId = [SELECT CustomObjectId,CustomObjectName  FROM CustomObjectUserLicenseMetrics   where CustomObjectName ='Repair' limit 1].CustomObjectId;
        //sobjectId = [SELECT CustomObjectId,CustomObjectName  FROM CustomObjectUserLicenseMetrics   where CustomObjectName ='Repair' limit 1].CustomObjectId;
        sobjectId = [SELECT id, QualifiedApiName, DurableId, IsCustomizable FROM EntityDefinition WHERE QualifiedApiName = 'Repair__c']
        .DurableId;
        isNewMode = true;
        Input_Required_Field_Msg = Label.Input_Required_Field_Msg;
        PIPL_Name_Label = Label.PIPL_Name_Label;
        PIPL_Input_Account_Error_Msg = label.PIPL_Input_Account_Error_Msg;
        sobjecttypeForFrontEnd = sobjectTypeValue;
        //获取所有字段
        List<String> fieldList = new List<String>(Schema.getGlobalDescribe().get('Repair__c').getDescribe().fields.getMap().keyset());
        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);
        if (!Test.isRunningTest()) {
            controller.addFields(fieldList);
        }
        SObject obj = controller.getRecord();
        if(obj.Id != null){
        SObject obj = controller.getRecord();
        if (obj.Id != null) {
            //更新
            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 =: obj.Id];
            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 = :obj.Id
            ];
            rtTypeId = repairData.RecordTypeId;
            AWSDataId = repairData.AWS_Data_Id__c;
            DecryptAWSDataId = String.valueOf(repairData.Address_AWS_Data_Id__c);
            ContactAWSDataId = String.valueOf(repairData.Contact_AWS_Data_Id__c);
        }else{
        } else {
            //新建
            // //修理品返送地
            // if(mso.containsKey('00N10000006P6SM')){
            //     controller.getRecord().put('SalesOfficeCode_selection__c',mso.get('00N10000006P6SM'));
@@ -81,7 +97,7 @@
            //     String o = mso.get('00N10000008rsVQ');
            //     controller.getRecord().put('DateReceiptQuestions__c',(String.isBlank(o)||String.isEmpty(o))? null:Date.valueOf(o.replace('/', '-')));
            // }
            // //是否有死亡、伤害、感染00N10000008rsW7
            // if(mso.containsKey('00N10000008rsW7')){
            //     controller.getRecord().put('ifDeadHurt__c',mso.get('00N10000008rsW7'));
@@ -157,9 +173,7 @@
            // if(mso.containsKey('00N10000006P1dw')){
            //     controller.getRecord().put('PaperRepairRequestNo__c',mso.get('00N10000006P1dw'));
            // }
            // //市场多年保修 有偿/无偿修理00N10000008rG4p
            // if(mso.containsKey('00N10000008rG4p')){
            //     controller.getRecord().put('RepairCostType__c',mso.get('00N10000008rG4p'));
@@ -183,7 +197,7 @@
            // if(mso.containsKey(RepairApplicant)){
            //     controller.getRecord().put('RepairApplicant__c',mso.get(RepairApplicant));
            // }
            // //报修人医院00N1m0000054ufU
            // //报修人医院00N1m0000054ufU
            // String RepairApplicantHospital = isTest==true?'00N1m0000054ufU':'00N10000009H1rP';
            // if(mso.containsKey(RepairApplicantHospital)){
            //     controller.getRecord().put('RepairApplicantHospital__c',mso.get(RepairApplicantHospital));
@@ -197,11 +211,11 @@
            // String RepairSubOrder = isTest==true?'CF00N1m0000054ufZ_lkid':'CF00N10000009H1rR_lkid';
            // if(mso.containsKey(RepairSubOrder)){
            //     controller.getRecord().put('RepairSubOrder__c',mso.get(RepairSubOrder));
                // //查询AWSDataId
                // List<RepairSubOrder__c> rso = [select AWS_Data_Id__c from RepairSubOrder__c where Id=:mso.get(RepairSubOrder)];
                // if(rso.size() > 0){
                //     RepairSubOrderAWSDataId = rso[0].AWS_Data_Id__c;
                // }
            // //查询AWSDataId
            // List<RepairSubOrder__c> rso = [select AWS_Data_Id__c from RepairSubOrder__c where Id=:mso.get(RepairSubOrder)];
            // if(rso.size() > 0){
            //     RepairSubOrderAWSDataId = rso[0].AWS_Data_Id__c;
            // }
            // }
            // //报修发起日
            // String RepairTime = isTest==true?'00N1m0000054ufa':'00N10000009H1rS';
@@ -251,7 +265,7 @@
            //     String o = mso.get(SubOrderCreatedDate);
            //     controller.getRecord().put('SubOrderCreatedDate__c',(String.isBlank(o)||String.isEmpty(o))? null:Date.valueOf(o.replace('/', '-')));
            // }
            // //送修物流方式00N1m0000054ufE
            // if(mso.containsKey('00N1m0000054ufE')){
            //     controller.getRecord().put('DeliveryLogisticsMode__c',mso.get('00N1m0000054ufE'));
@@ -273,9 +287,9 @@
            //     controller.getRecord().put('DelayReportReason__c',mso.get('00N10000008rsVS'));
            // }
            Map<string,string> mso = ApexPages.currentPage().getParameters();
            system.debug('mso='+mso);
            AssignValueFromUrl(mso,controller.getRecord());
            Map<string, string> mso = ApexPages.currentPage().getParameters();
            system.debug('mso=' + mso);
            AssignValueFromUrl(mso, controller.getRecord());
            //通过id查      问题
            //PIPL zhj 2022-11-11 start
            //从报修子单过来00N10000009H1rR
@@ -283,8 +297,8 @@
                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){
                List<RepairSubOrder__c> rso = [SELECT AWS_Data_Id__c FROM RepairSubOrder__c WHERE Id = :repairSubOrderId];
                if (rso.size() > 0) {
                    RepairSubOrderAWSDataId = rso[0].AWS_Data_Id__c;
                }
            }
@@ -294,51 +308,71 @@
            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 ];
                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){
                     //医院
                    controller.getRecord().put('Hospital__c',rc[0].Hospital__c);
                if (rc.size() > 0) {
                    //医院
                    controller.getRecord().put('Hospital__c', rc[0].Hospital__c);
                    //战略科室分类
                    controller.getRecord().put('Department_Class__c',rc[0].Department_Class__c);
                    controller.getRecord().put('Department_Class__c', rc[0].Department_Class__c);
                    //科室
                    controller.getRecord().put('Account__c',rc[0].Account__c);
                    controller.getRecord().put('Account__c', rc[0].Account__c);
                    //经销商名
                    controller.getRecord().put('Dealer__c',rc[0].Dealer__c);
                    controller.getRecord().put('Dealer__c', rc[0].Dealer__c);
                    //修理委托者(员工)
                    controller.getRecord().put('Incharge_Staff_Contact__c',rc[0].Incharge_Staff_Contact__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);
                    controller.getRecord().put('Incharge_Staff__c', rc[0].Incharge_Staff__c);
                    //报修子单RepairSubOrder__c
                    controller.getRecord().put('RepairSubOrder__c',rc[0].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){
                    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);
                    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);
                    controller.getRecord().put('QIS_ID__c', rc[0].QIS_ID__c);
                    //点检报告书InsReport__c
                    controller.getRecord().put('InsReport__c',rc[0].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);
                    controller.getRecord().put('Rental_Apply_Equipment_Set_Detail__c', rc[0].Rental_Apply_Equipment_Set_Detail__c);
                }
            }
            rtTypeId = ApexPages.currentPage().getParameters().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']);
            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');
        LayoutDescriberHelper.LayoutWrapper LayoutWrapperValue = LayoutDescriberHelper.describeSectionWithFieldsWrapper(
            rtTypeId,
            'Repair__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;
        Map<String, String> fieldAPIToLabelMap = LayoutWrapperValue.fieldAPIToLabelMap;
        requiredFieldAPIListStr = JSON.serialize(requiredFieldAPIList);
        fieldAPIToLabelMapStr = JSON.serialize(fieldAPIToLabelMap);
        PIHelper.PIIntegration piIntegration = PIHelper.getPIIntegrationInfo('Repair__c');
@@ -366,33 +400,33 @@
        VLookUpFieldsJson = Json.serialize(VLookUpFields);
    }
    global class Response{
        public String recordId{set;get;}
        public String message{set;get;}
        public String status{set;get;}
    global class Response {
        public String recordId { get; set; }
        public String message { get; set; }
        public String status { get; set; }
    }
    @RemoteAction
    global static Response EncryptUpdate(string rid){
        system.debug('rid='+rid);
    global static Response EncryptUpdate(string rid) {
        system.debug('rid=' + rid);
        Response r = new Response();
        List<Repair__c> reps = new List<Repair__c>();
        if(Test.isRunningTest()){
            if(rid=='a0J1m000001QqXk'){
                reps.add(new Repair__c(Id='a0J1m000001QqXk',Encrypt_Update_Flag__c=true));
            }else{
                reps.add(new Repair__c(Id='a0J1m000001QqXk',Encrypt_Update_Flag__c=false));
        if (Test.isRunningTest()) {
            if (rid == 'a0J1m000001QqXk') {
                reps.add(new Repair__c(Id = 'a0J1m000001QqXk', Encrypt_Update_Flag__c = true));
            } else {
                reps.add(new Repair__c(Id = 'a0J1m000001QqXk', Encrypt_Update_Flag__c = false));
            }
        }else{
            reps = [select id,Encrypt_Update_Flag__c from Repair__c where id =:rid];
        }
        if(reps.size()==0){
        } else {
            reps = [SELECT id, Encrypt_Update_Flag__c FROM Repair__c WHERE id = :rid];
        }
        if (reps.size() == 0) {
            r.status = 'failed';
            r.message = '符合条件的数据未找到';
            return r;
        }
        Repair__c rep = reps[0];
        if(!rep.Encrypt_Update_Flag__c){
        if (!rep.Encrypt_Update_Flag__c) {
            r.status = 'success';
            r.message = '';
            return r;
@@ -400,17 +434,17 @@
        //zhj MEBG新方案改造 2022-11-29 start
        //boolean b = AWSServiceTool2.EncryptPushData(new string[]{rid});
        System.debug('AWSServiceTool2V2.EncryptPushDataRepair start');
        //AWSServiceTool2V2.EncryptPushDataRepair(rid);
        boolean b = false;
        if(!Test.isRunningTest()){
        if (!Test.isRunningTest()) {
            b = AWSServiceTool2V2.EncryptPushDataRepair(rid);
        }
        //zhj MEBG新方案改造 2022-11-29 end
        rep.Encrypt_Update_Flag__c = false;
        if(!Test.isRunningTest()){
        if (!Test.isRunningTest()) {
            update rep;
        }
        }
        r.status = b ? 'success' : 'failed';
        r.message = b ? '' : '加密推送失败';
        r.recordId = rid;
@@ -418,83 +452,96 @@
    }
    @RemoteAction
    global static Response saveRepair(String repairJson,String transId,Boolean isNew) {
    global static Response saveRepair(String repairJson, String transId, Boolean isNew) {
        System.debug('Repair__c Info:' + JSON.serialize(repairJson));
        //1. Prepare the payload for  Repair__c
        Schema.SObjectType repairSchema = schemaMap.get(sobjectTypeValue);
        Map<String, Schema.SObjectField> fieldAPIToTypeMap = repairSchema.getDescribe().fields.getMap();
        Map<String,Object> fieldValueMap = (Map<String,Object>)JSON.deserializeUntyped(repairJson);
        Map<String, Object> fieldValueMap = (Map<String, Object>) JSON.deserializeUntyped(repairJson);
        Repair__c repairInfo = new Repair__c();
        Boolean updateRepairCheck = false;
        //自定义格式转换
        for (String fieldAPI: fieldValueMap.keySet()) {
            system.debug('field API'+fieldAPI);
            if(!fieldAPIToTypeMap.containskey(fieldAPI)){
        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'){
                repairInfo.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',' ');
            Schema.DisplayType fielddataType = fieldAPIToTypeMap.get(fieldAPI).getDescribe().getType();
            String fieldValue = String.valueOf(fieldValueMap.get(fieldAPI));
            if (String.valueOf(fielddataType) == 'DATE') {
                repairInfo.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', ' ');
                    repairInfo.put(fieldAPI, Datetime.valueOfGmt(fieldValue));
                }else if(String.isNotBlank(fieldValue))  {
                } else if (String.isNotBlank(fieldValue)) {
                    fieldValue = fieldValue.replace('/', '-') + ':00';
                    System.debug('fieldValue = ' + fieldValue);
                    repairInfo.put(fieldAPI, Datetime.valueOf(fieldValue));
                }else{
                } else {
                    repairInfo.put(fieldAPI, null);
                }
            }else if(String.valueOf(fielddataType)=='PERCENT'||String.valueOf(fielddataType)=='CURRENCY'||String.valueOf(fielddataType)=='Number'||String.valueOf(fielddataType)=='DOUBLE' ){
                repairInfo.put(fieldAPI, (String.isBlank(fieldValue)||String.isEmpty(fieldValue))?null:Decimal.valueOf(fieldValue.replace(',', '')));
            } else if(String.valueof(fielddataType)=='BOOLEAN'){
                 //check repair check value
                if(fieldAPI == 'Return_Without_Repair__c'&&Boolean.valueof(fieldValueMap.get(fieldAPI))){
            } else if (
                String.valueOf(fielddataType) == 'PERCENT' ||
                String.valueOf(fielddataType) == 'CURRENCY' ||
                String.valueOf(fielddataType) == 'Number' ||
                String.valueOf(fielddataType) == 'DOUBLE'
            ) {
                repairInfo.put(
                    fieldAPI,
                    (String.isBlank(fieldValue) || String.isEmpty(fieldValue)) ? null : Decimal.valueOf(fieldValue.replace(',', ''))
                );
            } else if (String.valueof(fielddataType) == 'BOOLEAN') {
                //check repair check value
                if (fieldAPI == 'Return_Without_Repair__c' && Boolean.valueof(fieldValueMap.get(fieldAPI))) {
                    updateRepairCheck = true;
                }
                repairInfo.put(fieldAPI, fieldValueMap.get(fieldAPI));
            }else {
            } else {
                repairInfo.put(fieldAPI, String.valueOf(fieldValueMap.get(fieldAPI)));
            }
            }
        }
        //2. Save Record Process
        String status = 'success';
        String status = 'success';
        Response resp = new Response();
        Savepoint sp = Database.setSavepoint();
        String rid = '';
        try{
        try {
            System.debug('abcde');
            if(isNew){
            if (isNew) {
                System.debug('repairInfozhj = ' + repairInfo);
                insert repairInfo;
            }else{
            } else {
                System.debug('into update');
                String awsDataId = (String)repairInfo.get('AWS_Data_Id__c');
                String awsDataId = (String) repairInfo.get('AWS_Data_Id__c');
                System.debug('awsDataId = ' + awsDataId);
                Repair__c[] repairs = [select id,Return_Without_Repair_Reason__c,SAP_Transfer_time__c from Repair__c where AWS_Data_Id__c =:awsDataId];
                if(!updateRepairCheck){
                    repairInfo.put('Return_Without_Repair_Reason__c',repairs[0].Return_Without_Repair_Reason__c);
                Repair__c[] repairs = [
                    SELECT id, Return_Without_Repair_Reason__c, SAP_Transfer_time__c
                    FROM Repair__c
                    WHERE AWS_Data_Id__c = :awsDataId
                ];
                if (!updateRepairCheck) {
                    repairInfo.put('Return_Without_Repair_Reason__c', repairs[0].Return_Without_Repair_Reason__c);
                }
                if(repairs!=null&&repairs[0]!=null){
                    repairInfo.put('SAP_Transfer_time__c',repairs[0].SAP_Transfer_time__c);
                if (repairs != null && repairs[0] != null) {
                    repairInfo.put('SAP_Transfer_time__c', repairs[0].SAP_Transfer_time__c);
                }
                System.debug('repairs[0].id = ' + repairs[0].id);
                repairInfo.put('Id',repairs[0].id);//For testing;
                repairInfo.put('Id', repairs[0].id); //For testing;
                update repairInfo;
            }
            rid=repairInfo.Id;
            PIHelper.saveTransLog(sobjectTypeValue,(String)repairInfo.get('AWS_Data_Id__c'),rid,transId, repairJson ,status,'');
            rid = repairInfo.Id;
            PIHelper.saveTransLog(sobjectTypeValue, (String) repairInfo.get('AWS_Data_Id__c'), rid, transId, repairJson, status, '');
            resp.recordId = repairInfo.Id;
            resp.message = '';
            resp.status = status;
            System.debug('resp from sfdx back-end' + resp);
            return resp;
        } catch(DmlException e) {
            rid=repairInfo.Id;
        } catch (DmlException e) {
            rid = repairInfo.Id;
            Integer index = 0;
            System.debug(e.getNumDml());
            System.debug(e.getDmlFields(index));
@@ -503,60 +550,88 @@
            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());
            System.debug('into catch' + e.getMessage());
            Database.rollback(sp);
            resp.status = 'Exception';
            resp.message ='保存失败,原因:'+ e.getDmlMessage(index);
            PIHelper.saveTransLog(sobjectTypeValue,(String)repairInfo.get('AWS_Data_Id__c'),rid,transId, repairJson ,status,e.getMessage()+e.getStackTraceString());
            resp.message = '保存失败,原因:' + e.getDmlMessage(index);
            PIHelper.saveTransLog(
                sobjectTypeValue,
                (String) repairInfo.get('AWS_Data_Id__c'),
                rid,
                transId,
                repairJson,
                status,
                e.getMessage() + e.getStackTraceString()
            );
            return resp;
        } catch(Exception e) {
            System.debug('into catch'+e.getMessage());
        } catch (Exception e) {
            System.debug('into catch' + e.getMessage());
            Database.rollback(sp);
            status = 'fail';
            PIHelper.saveTransLog(sobjectTypeValue,(String)repairInfo.get('AWS_Data_Id__c'),rid,transId, repairJson ,status,e.getMessage()+e.getStackTraceString());
            resp.message = e.getMessage();
            PIHelper.saveTransLog(
                sobjectTypeValue,
                (String) repairInfo.get('AWS_Data_Id__c'),
                rid,
                transId,
                repairJson,
                status,
                e.getMessage() + e.getStackTraceString()
            );
            resp.message = e.getMessage();
            resp.status = status;
            return resp;
        }
    }
    // 从url参数赋值到当前页面
    public static void AssignValueFromUrl(Map<string,string> mso, sobject sobj){
    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){
        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);
            fdm.put(fd.DurableId.split('\\.')[1], fd);
        }
        for(string key : mso.keySet()){
        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')){
            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)){
            } 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));
            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);
                system.debug('val_str=' + val_str);
                /*无需做decode,sf内部已经做好
                try{
                    val_str = EncodingUtil.urlDecode(mso.get(key),'UTF-8');
@@ -564,82 +639,81 @@
                    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)){
                if (string.isBlank(val_str)) {
                    val = null;
                }else if(type_id == 'boolean'){
                    if(val_str == '1'){
                } else if (type_id == 'boolean') {
                    if (val_str == '1') {
                        val = true;
                    }else{
                    } else {
                        val = boolean.valueOf(val_str);
                    }
                }else if(type_id == 'date'){
                } else if (type_id == 'date') {
                    //
                      try{
                    try {
                        val = date.parse(val_str);
                    }catch(Exception e){
                        system.debug('val_str='+val_str);
                    } catch (Exception e) {
                        system.debug('val_str=' + val_str);
                        system.debug(e.getMessage());
                        system.debug(e.getStackTraceString());
                        try{
                        try {
                            val = date.valueOf(val_str);
                        }catch(Exception ee){
                            system.debug('val_str='+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'){
                } else if (type_id == 'datetime') {
                    //
                      try{
                    try {
                        val = datetime.parse(val_str);
                    }catch(Exception e){
                        system.debug('val_str='+val_str);
                    } catch (Exception e) {
                        system.debug('val_str=' + val_str);
                        system.debug(e.getMessage());
                        system.debug(e.getStackTraceString());
                        try{
                        try {
                            val = datetime.valueOf(val_str);
                        }catch(Exception ee){
                            system.debug('val_str='+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{
                } else if (type_id == 'double' || type_id == 'number') {
                    try {
                        val = decimal.valueOf(val_str.replace(',', ''));
                    }catch(Exception ee){
                        system.debug('val_str='+val_str);
                    } 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'){
                } else if (type_id == 'id' || type_id == 'string') {
                    val = val_str;
                }else{
                    system.debug('type_id='+type_id+' is not support to convert');
                } 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');
                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{
        for (string key : temp.keySet()) {
            system.debug('assign ' + key + '=' + temp.get(key));
            try {
                sobj.put(key, temp.get(key));
            }catch(Exception e){
            } catch (Exception e) {
                system.debug(e.getMessage());
                system.debug(e.getStackTraceString());
            }
        }
    }
}
}