/*
 *@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 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 NewRepairController(ApexPages.StandardController controller) {
        sobjectId = [SELECT CustomObjectId,CustomObjectName  FROM CustomObjectUserLicenseMetrics   where CustomObjectName ='Repair' limit 1].CustomObjectId;
        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());  
        // Add fields to controller. This is to avoid the SOQL error in visualforce page
        if(!Test.isRunningTest()){
           controller.addFields(fieldList);  
        }
        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];
            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{
            //新建
            Map<string,string> mso = ApexPages.currentPage().getParameters();
            system.debug('mso='+mso);
            //修理品返送地
            if(mso.containsKey('00N10000006P6SM')){
                controller.getRecord().put('SalesOfficeCode_selection__c',mso.get('00N10000006P6SM'));
            }
            if(mso.containsKey('00N10000002FH86')){
                //controller.getRecord().put('On_site_repair__c',mso.get('00N10000002FH86'));
                //Repair_Source__c
                controller.getRecord().put('Repair_Source__c',mso.get('00N10000002FH86'));
            }
            if(mso.containsKey('00N10000006P6Rn')){
                controller.getRecord().put('work_location_select__c',mso.get('00N10000006P6Rn'));
                workLocationSelect = mso.get('00N10000006P6Rn');
                System.debug('workLocationSelect = ' + workLocationSelect);
            }
            //问题内容描述00N10000002Dx6I
            if(mso.containsKey('00N10000002Dx6I')){
                controller.getRecord().put('Repair_Detail__c',mso.get('00N10000002Dx6I'));
            }
            //问题联络收到日00N10000008rsVQ
            if(mso.containsKey('00N10000008rsVQ')){
                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'));
            }
            //是否和这次的产品故障有关00N10000008rsVw
            if(mso.containsKey('00N10000008rsVw')){
                controller.getRecord().put('ProductFailureRelated__c',mso.get('00N10000008rsVw'));
            }
            //医院有没有向政府机关报告不良事件或疑似不良事件00N10000008rsW2
            if(mso.containsKey('00N10000008rsW2')){
                controller.getRecord().put('ReportAdverseEvents__c',mso.get('00N10000008rsW2'));
            }
            //做的是哪个项目00N10000008rsW6
            if(mso.containsKey('00N10000008rsW6')){
                controller.getRecord().put('WhatProject__c',mso.get('00N10000008rsW6'));
            }
            //手术/检查名称00N10000008rsVk
            if(mso.containsKey('00N10000008rsVk')){
                controller.getRecord().put('OperationOrExaminationName__c',mso.get('00N10000008rsVk'));
            }
            //是否有发生断裂或脱落00N10000008rsVN
            if(mso.containsKey('00N10000008rsVN')){
                controller.getRecord().put('BreakORFallOff__c',mso.get('00N10000008rsVN'));
            }
            //配套使用产品00N10000008rsW4
            if(mso.containsKey('00N10000008rsW4')){
                controller.getRecord().put('SupportingProducts__c',mso.get('00N10000008rsW4'));
            }
            //发现故障后，医院对患者的处理情况00N10000008rsVL
            if(mso.containsKey('00N10000008rsVL')){
                controller.getRecord().put('AfterFailureInformation__c',mso.get('00N10000008rsVL'));
            }
            //发现故障现象后，有延误15分钟以上的手术实施的情况发生吗00N10000008rsVR
            if(mso.containsKey('00N10000008rsVR')){
                controller.getRecord().put('Delay15Min__c',mso.get('00N10000008rsVR'));
            }
            //信息是从医院的谁那里得到的00N10000008rsVZ
            if(mso.containsKey('00N10000008rsVZ')){
                controller.getRecord().put('InformationFrom__c',mso.get('00N10000008rsVZ'));
            }
            //在医院，故障发生的频率是多少00N10000008rsVT
            if(mso.containsKey('00N10000008rsVT')){
                controller.getRecord().put('FailureQInHospital__c',mso.get('00N10000008rsVT'));
            }
            //问题发生时间情况00N10000008rsVv
            if(mso.containsKey('00N10000008rsVv')){
                controller.getRecord().put('ProblemOccurred__c',mso.get('00N10000008rsVv'));
            }
            //是否用该产品完成了手术/检查00N10000008rsW5
            if(mso.containsKey('00N10000008rsW5')){
                if(mso.get('00N10000008rsW5') != 'undefined'){
                    controller.getRecord().put('UseFailProductFinish__c',mso.get('00N10000008rsW5'));
                }
            }
            //故障发生日00N10000002Dx5y
            if(mso.containsKey('00N10000002Dx5y')){
                String o = mso.get('00N10000002Dx5y');
                controller.getRecord().put('Failure_Occurrence_Date__c',(String.isBlank(o)||String.isEmpty(o))? null:Date.valueOf(o.replace('/', '-')));
            }
            //修理委托者(FSE)CF00N10000002EMHw_lkid
            if(mso.containsKey('CF00N10000002EMHw_lkid')){
                controller.getRecord().put('Incharge_Staff__c',mso.get('CF00N10000002EMHw_lkid'));
            }
            //原修理受付番号00N1m000005gt1l
            if(mso.containsKey('00N1m000005gt1l')){
                controller.getRecord().put('Old_Name__c',mso.get('00N1m000005gt1l'));
            }
            //设备型号CF00N10000002Dx1X_lkid
            if(mso.containsKey('CF00N10000002Dx1X_lkid')){
                controller.getRecord().put('Delivered_Product__c',mso.get('CF00N10000002Dx1X_lkid'));
            }
            //维修委托书号码00N10000006P1dw
            if(mso.containsKey('00N10000006P1dw')){
                controller.getRecord().put('PaperRepairRequestNo__c',mso.get('00N10000006P1dw'));
            }
            
            //通过id查
            Repair__c rc = null;
            if (mso.containsKey('RepairId')) {
                String RepairId = mso.get('RepairId');
                rc = [select Hospital__c,Department_Class__c,Account__c,Dealer__c,Incharge_Staff_Contact__c,Incharge_Staff__c from Repair__c where id =:RepairId ];
                //医院
                controller.getRecord().put('Hospital__c',rc.Hospital__c);
                //战略科室分类
                controller.getRecord().put('Department_Class__c',rc.Department_Class__c);
                //科室
                controller.getRecord().put('Account__c',rc.Account__c);
                //经销商名
                controller.getRecord().put('Dealer__c',rc.Dealer__c);
                //修理委托者（员工）
                controller.getRecord().put('Incharge_Staff_Contact__c',rc.Incharge_Staff_Contact__c);
                //修理委托者(FSE)
                controller.getRecord().put('Incharge_Staff__c',rc.Incharge_Staff__c);
            }
            
            //市场多年保修 有偿/无偿修理00N10000008rG4p
            if(mso.containsKey('00N10000008rG4p')){
                controller.getRecord().put('RepairCostType__c',mso.get('00N10000008rG4p'));
            }
            //服务方式00N10000002F6dW
            if(mso.containsKey('00N10000002F6dW')){
                controller.getRecord().put('On_site_repair__c',mso.get('00N10000002F6dW'));
            }
            //返品方式00N1m000004QmcJ
            if(mso.containsKey('00N1m000004QmcJ')){
                controller.getRecord().put('Returns_Product_way__c',mso.get('00N1m000004QmcJ'));
            }
            //问题内容描述（图片）00N10000008rsVuEAI
            if(mso.containsKey('00N10000008rsVuEAI')){
                controller.getRecord().put('ProblemDescription__c',mso.get('00N10000008rsVuEAI'));
            }
            //如果是测试环境
            Boolean isTest = NFMUtil.isSandbox();
            //报修人00N1m0000054ufW
            String RepairApplicant = isTest==true?'00N1m0000054ufW':'00N10000009H1rQ';
            if(mso.containsKey(RepairApplicant)){
                controller.getRecord().put('RepairApplicant__c',mso.get(RepairApplicant));
            }
            //报修人医院00N1m0000054ufU
            String RepairApplicantHospital = isTest==true?'00N1m0000054ufU':'00N10000009H1rP';
            if(mso.containsKey(RepairApplicantHospital)){
                controller.getRecord().put('RepairApplicantHospital__c',mso.get(RepairApplicantHospital));
            }
            //报修人科室00N1m0000054ufT
            String RepairApplicantDepartment = isTest==true?'00N1m0000054ufT':'00N10000009H1rO';
            if(mso.containsKey(RepairApplicantDepartment)){
                controller.getRecord().put('RepairApplicantDepartment__c',mso.get(RepairApplicantDepartment));
            }
            //报修子单CF00N1m0000054ufZ_lkid
            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;
                }
            }
            //报修发起日
            String RepairTime = isTest==true?'00N1m0000054ufa':'00N10000009H1rS';
            if(mso.containsKey(RepairTime)){
                String o = mso.get(RepairTime);
                controller.getRecord().put('RepairTime__c',(String.isBlank(o)||String.isEmpty(o))? null:Date.valueOf(o.replace('/', '-')));
            }
            //报修单受理日ReceiverTime__c
            String ReceiverTime = isTest==true?'00N1m0000054ufS':'00N10000009H1rN';
            if(mso.containsKey(ReceiverTime)){
                String o = mso.get(ReceiverTime);
                controller.getRecord().put('ReceiverTime__c',(String.isBlank(o)||String.isEmpty(o))? null:Date.valueOf(o.replace('/', '-')));
            }
            //计划上门日
            String PlannedVisitDay = isTest==true?'00N1m000005SRiF':'00N10000009H1rM';
            if(mso.containsKey(PlannedVisitDay)){
                String o = mso.get(PlannedVisitDay);
                controller.getRecord().put('PlannedVisitDay__c',(String.isBlank(o)||String.isEmpty(o))? null:Date.valueOf(o.replace('/', '-')));
            }
            //一次上门日
            String ActualVisitDateFirst = isTest==true?'00N1m000005SRiK':'00N10000009H1r6';
            if(mso.containsKey(ActualVisitDateFirst)){
                String o = mso.get(ActualVisitDateFirst);
                controller.getRecord().put('ActualVisitDateFirst__c',(String.isBlank(o)||String.isEmpty(o))? null:Date.valueOf(o.replace('/', '-')));
            }
            //二次上门日
            String ActualVisitDateSecond = isTest==true?'00N1m000005SRiP':'00N10000009H1r7';
            if(mso.containsKey(ActualVisitDateSecond)){
                String o = mso.get(ActualVisitDateSecond);
                controller.getRecord().put('ActualVisitDateSecond__c',(String.isBlank(o)||String.isEmpty(o))? null:Date.valueOf(o.replace('/', '-')));
            }
            //三次上门日
            String ActualVisitDateThird = isTest==true?'00N1m000005SRiU':'00N10000009H1r8';
            if(mso.containsKey(ActualVisitDateThird)){
                String o = mso.get(ActualVisitDateThird);
                controller.getRecord().put('ActualVisitDateThird__c',(String.isBlank(o)||String.isEmpty(o))? null:Date.valueOf(o.replace('/', '-')));
            }
            //移动端申请修理日
            String SubOrderCompleteDate = isTest==true?'00N1m000005SRmg':'00N10000009H1ri';
            if(mso.containsKey(SubOrderCompleteDate)){
                String o = mso.get(SubOrderCompleteDate);
                controller.getRecord().put('SubOrderCompleteDate__c',(String.isBlank(o)||String.isEmpty(o))? null:Date.valueOf(o.replace('/', '-')));
            }
            //报修单处理完成日
            String SubOrderCreatedDate = isTest==true?'00N1m000005SRmW':'00N10000009H1rj';
            if(mso.containsKey(SubOrderCreatedDate)){
                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'));
            }
            //送修物流最新状态00N1m0000054ufk
            if(mso.containsKey('00N1m0000054ufk')){
                controller.getRecord().put('LogisticsLatestStatus__c',mso.get('00N1m0000054ufk'));
            }
            //送修物流单号00N1m0000054ufF
            if(mso.containsKey('00N1m0000054ufF')){
                controller.getRecord().put('DeliveryLogisticsNo__c',mso.get('00N1m0000054ufF'));
            }
            //送修物流信息备注00N1m0000054ufD
            if(mso.containsKey('00N1m0000054ufD')){
                controller.getRecord().put('DeliveryLogisticsAnnotation__c',mso.get('00N1m0000054ufD'));
            }
            //超时报告的理由00N10000008rsVS
            if(mso.containsKey('00N10000008rsVS')){
                controller.getRecord().put('DelayReportReason__c',mso.get('00N10000008rsVS'));
            }
            
            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']);
                rtTypeId = rtList[0].Id;
            }
        }
        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;
        requiredFieldAPIListStr = JSON.serialize(requiredFieldAPIList);
        fieldAPIToLabelMapStr = JSON.serialize(fieldAPIToLabelMap);
        PIHelper.PIIntegration piIntegration = PIHelper.getPIIntegrationInfo('Repair__c');
        PIHelper.PIIntegration piIntegrationAddress = PIHelper.getPIIntegrationInfo('Address__c');
        PIHelper.PIIntegration piIntegrationContact = PIHelper.getPIIntegrationInfo('Contact');
        PIHelper.PIIntegration piIntegrationRepairSubOrder = PIHelper.getPIIntegrationInfo('RepairSubOrder__c');
        staticResource = JSON.serialize(piIntegration);
        staticResourceAddress = JSON.serialize(piIntegrationAddress);
        staticResourceContact = JSON.serialize(piIntegrationContact);
        staticResourceRepairSubOrder = JSON.serialize(piIntegrationRepairSubOrder);
        encryptedAPIList = piIntegration.PIFields;
        sobjectPrefix = piIntegration.sobjectPrefix;
    }

    global class Response{
        public String recordId{set;get;}
        public String message{set;get;}
        public String status{set;get;}
    }
    
    @RemoteAction
    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));
            }
        }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){
            r.status = 'success';
            r.message = '';
            return r;
        }
        
        boolean b = AWSServiceTool2.EncryptPushData(new string[]{rid});
        rep.Encrypt_Update_Flag__c = false;
        if(!Test.isRunningTest()){
            update rep;
        }      
        r.status = b ? 'success' : 'failed';
        r.message = b ? '' : '加密推送失败';
        r.recordId = rid;
        return r;
    }

    @RemoteAction
    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);
        Repair__c repairInfo = new Repair__c();
        Boolean updateRepairCheck = false;
        //自定义格式转换
        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',' ');
                    repairInfo.put(fieldAPI, Datetime.valueOfGmt(fieldValue));
                }else if(String.isNotBlank(fieldValue))  {
                    fieldValue = fieldValue.replace('/', '-') + ':00';
                    System.debug('fieldValue = ' + fieldValue);
                    repairInfo.put(fieldAPI, Datetime.valueOf(fieldValue));
                }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))){
                    updateRepairCheck = true;
                }
                repairInfo.put(fieldAPI, fieldValueMap.get(fieldAPI));
            }else {
                repairInfo.put(fieldAPI, String.valueOf(fieldValueMap.get(fieldAPI)));
            }                  
        }
        //2. Save Record Process
        String status = 'success';    
        Response resp = new Response();
        Savepoint sp = Database.setSavepoint();
        String rid = '';
        try{
            System.debug('abcde');
            if(isNew){
                System.debug('repairInfozhj = ' + repairInfo);
                insert repairInfo;
            }else{
                System.debug('into update');
                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);
                }
                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;
                update repairInfo;
            }
            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;
            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,(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());
            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();            
            resp.status = status;
            return resp;
        }
    }
}