@RestResource(urlMapping = '/NFM624RestAbout2/*')
global with sharing class NFM624RestAbout2 {
    private static final String LOG_TYPE = 'NFM624RestAbout2';

    global class GeDatas {
        public NFMUtil.Monitoring Monitoring;
        public GeData[] GeData;
      }
  global class GeData{

         public String PersonManagementCode; //人员管理编码 
        public String HospitalManagementCode2; //医院平台编码  平台编码 PlatformCode__c
        public String DepartmentManagementCode2; //科室平台编码  平台编码 PlatformCode__c
        //public String Name; //姓名
        //public String NameEncrypted;//姓名密文 add 20220215 
        public String RelatedHospital; //关联SFDC医院 客户管理编码(手写) AgentCode_Ext__c
        public String DepartmentClass; //战略科室
        public String DepartmentName; //所属科室
        public String RelatedDepartment; //关联SFDC科室 客户管理编码(手写) AgentCode_Ext__c
        //public Boolean AgentFlag; //经销商标识
        public String dataId;
        public String AccountName; //医院名
        //public String MobileEncrypted; //电话密文 add 20220519 thh
    }



    global class Response {
        public List<ResponseDetail> ResponseDetails {get;set;}
    }
   
    global class ResponseDetail{
      public String  dataId;
      public String  awsDataId; //=CONTACT.AWS_Data_Id__c
      public String  sfId;
    }  
 
    @HttpPost
    global static void execute() {
 
        // 取得接口传输内容
        String strData = RestContext.request.requestBody.toString();
        system.debug('strData = ' + strData);
        GeDatas ges = (GeDatas) JSON.deserializeStrict(strData, GeDatas.class);
        system.debug('ges---'+JSON.serialize(ges) );
 
        if (ges == null) {
            return;
        }
 
        NFMUtil.Monitoring Monitoring = ges.Monitoring;
        if (Monitoring == null) {
            system.debug('monitoring62');
            return;
        }
        
        String result;
        try {
            BatchIF_Log__c rowData = NFMUtil.saveRowData(Monitoring, LOG_TYPE, ges.GeData);
            system.debug('rowData = '+rowData);
            system.debug('String.isBlank(rowData.Log__c) == false?');
            if (String.isBlank(rowData.Log__c) == false) {
             System.debug('进入if方法，成功调用executefuture');
                result = JSON.serialize(main(rowData.Id));
                system.debug('result'+result);
             // result main(rowData.Id);
            }
        } catch (Exception ex) {
            System.debug(Logginglevel.ERROR, LOG_TYPE + ':' + ex.getMessage());
            System.debug(Logginglevel.ERROR, LOG_TYPE + ':' + ex.getStackTraceString());
        }
        // BatchIF_Log__c rowData = NFMUtil.saveRowData(Monitoring, LOG_TYPE, ges.GeData);
        
 
        // JSONを戻す
        RestResponse res = RestContext.response;
        res.addHeader('Content-Type', 'application/json');
        res.statusCode = 200;
        //  NFMUtil.NFMResponse result = NFMUtil.getNFMResponse();
        //  result.SFStatus=SFStatus;
        //  result.SFMessage=SFMessage;
        String jsonResponse = result;
        //for test sushanhu 20220401 start
        //ResponseDetail red =new ResponseDetail();
        // red.dataId =ges.[0].dataId;
        //red.awsDataId ='959174215334887424';
        //red.sfId ='0031m00000N7leyAAB';
        // Response resp =new Response();
        // List<ResponseDetail> listName = new List<ResponseDetail>();
        // listName.add(red);
        // resp= (Response)listName;
        //for test sushanhu 20220401 end
        //String jsonResponse = '{"dataID": ""}';        
        //system.debug('result---'+jsonResponse);
        res.responseBody = blob.valueOf(jsonResponse);//need
        //String resp ='{"ResponseDetails":[{"dataId":"959573117074669568","awsDataId":"959174215334887424","sfId":"0031m00000N7leyAAB"}]}';
        //res.responseBody = blob.valueOf(resp);
        

         //updata response toAWS 20220225 sushanhu end
        return;
    }
 
 
    @future
    global static void executefuture(String rowData_Id) {
        system.debug('进入executefuture方法' + rowData_Id);
        main(rowData_Id);
    }
 
    global static Response main(String rowData_Id) {
        Integer batch_retry_max_cnt = Integer.valueOf(System.Label.batch_retry_max_cnt);
        BatchIF_Log__c rowData = [Select Id, Name, Log__c,Is_Error__c, ErrorLog__c, Log2__c, Log3__c, Log4__c, Log5__c, Log6__c, Log7__c, Log8__c, Log9__c, Log10__c, Log11__c, Log12__c, MessageGroupNumber__c, retry_cnt__c,NFM624_Secondary_processing__c  from BatchIF_Log__c where RowDataFlg__c = true and Id =: rowData_Id];
        String logstr = rowData.MessageGroupNumber__c + ' start\n';
        BatchIF_Log__c iflog = new BatchIF_Log__c();
        iflog.Type__c = LOG_TYPE;
        iflog.MessageGroupNumber__c = rowData.MessageGroupNumber__c;
        iflog.Log__c = logstr;
        iflog.ErrorLog__c = '';
        insert iflog;
        System.debug('iflog = ' + iflog);
        Response responseList = new Response();//返回体List
        responseList.ResponseDetails = new List<ResponseDetail>(); 
        try { 
            rowData.retry_cnt__c = 0;
            if (System.Test.isRunningTest()) {
                throw new ControllerUtil.myException('aaa');
            }
    }catch (Exception ex) {
            // エラーが発生した場合
           
            logstr += ex.getMessage();
            iflog.ErrorLog__c += ex.getMessage() + '\n';
            iflog.ErrorLog__c += ex.getStackTraceString() + '\n';
        }
        update rowData;
        system.debug('Rowdata'+rowData);   
        logstr += responseList +'\n';
        logstr += 'end';
        iflog.Log__c = logstr;
        update iflog;
        system.debug('responseList.ResponseDetails'+responseList.ResponseDetails);
        return responseList;  
    }
}