/**
 * MaintenanceContractTrigger.trigger より NFM106Trigger() を呼び出す
 */
public without sharing class NFM106Controller {
    //CHAN-BSV3GZ SAP返回状态值 start
    public static String status;
    // CHAN-BSV3GZ end
    // HWAG-BE88UG 【委托】SFDC-SAP搭现有接口添加合同“付款计划”信息 by vivek start
    public class PaymentInformation_element {
        public String PaymentTime;
        public String PaymentDate;
        public String PaymentAmount;
    }
    public class GeneralData_element {
        public String SalesOfficeCode;
        public String ContractNo;
        public String EndUserNoorAgentNo;
        public String HospitalName;
        public String ContractDate;
        public String StartDate;
        public String EndDate;
        public String Amount;
        public String FSERemark;
        public String QuotationAmount;
        public String ContractPeriod;
        public PaymentInformation_element[] PaymentInformation;
        //URF限次合同2期 LY 20220811 start
        public SeriesInformation_element[] SeriesInformation;
        //URF限次合同2期 LY 20220811 end

    }
    //URF限次合同2期 LY 20220811 start
    public class SeriesInformation_element {
        public String SeriesNo;
        public String RepairTime;
        public String SeriesAmount;
    }
    //URF限次合同2期 LY 20220811 end
    public class RepairContractLogin_element {
        public NFMUtil.Monitoring Monitoring;
        public GeneralData_element[] GeneralData;
    }
    public class NFM106 {
        public RepairContractLogin_element RepairContractLogin;
    }
    
    // HWAG-BE88UG 【委托】SFDC-SAP搭现有接口添加合同“付款计划”信息 by vivek end
    // Repair__cのId
    public static Set<Id> NFM106_Ids = new Set<Id>();
    public static String debug_msg = '';

    // public static void aaa(){
    //     GeneralData_element gener = new GeneralData_element();
    //     gener.PaymentInformation = new List<PaymentInformation_element>();
    // }

    // on Maintenance_Contract__c (after insert, before update)
    public static void NFM106Trigger(List<Maintenance_Contract__c> newList, Map<Id, Maintenance_Contract__c> newMap, List<Maintenance_Contract__c> oldList, Map<Id, Maintenance_Contract__c> oldMap) {
System.debug('-----NFM106Trigger start');
        Map<Id, Id> mcIds = new Map<Id, Id>();           // 更新対象 Id をセット
        List<String> mcNoList = new List<String>();
        if (Trigger.isAfter) {
            if (Trigger.isInsert) {
                for(Maintenance_Contract__c mc : newList) {
                    // 虚拟维修合同跳过
                    if (mc.RecordType_DeveloperName__c == 'VM_Contract')continue ;
                    if (NFM106Controller.NFM106_Ids.contains(mc.Id) == false && mc.Status__c == '契約'
                    ) {
                        if (String.isBlank(mc.Maintenance_Contract_No__c)) {
                            mc.addError('状态变为合同时，必须输入「' + Schema.SObjectType.Maintenance_Contract__c.fields.Maintenance_Contract_No__c.label + '」' + mc.RecordType.DeveloperName);
                            continue;
                        }
                        mcIds.put(mc.Id, mc.Id);
                        mcNoList.add(mc.Maintenance_Contract_No__c);
                        NFM106Controller.debug_msg += 'NFM106_callout_insert_' + mc.Name;
                    }
                }
            }
        }
        //Repair测试类跳过 thh 20220401 start
        if(StaticParameter.EscapeNFM106Trigger){
            return;
        }
        //Repair测试类跳过 thh 20220401 end
        if (Trigger.isBefore) {
            if (Trigger.isUpdate) {
                for(Maintenance_Contract__c mc : newList) {
                    // 虚拟维修合同跳过
                    if (mc.RecordType_DeveloperName__c == 'VM_Contract')continue ;
                    
                    //update     wangweipeng                               2021/11/16   start
                    //新增先款后修功能，如果经销商是先款对象，需要先认款合同上传然后再上传SAP
                    //判断是否需要走认款的接口
                    boolean flag = false;
                    if(mc.old_Is_RecognitionModel__c){
                        //判断当前合同是否已经认款完成
                        if(!mc.Is_Recognition_Model_True__c){
                            flag = true;
                        }
                    }
                    //为true时，证明此合同必须先上传认款合同，然后再能走上传SAP接口
                    if(flag){
                        //如果是否认款合同字段为true,并且认款合同状态变为 认款中，那么就走认款接口
                        if(mc.upload_to_RM_time__c != null && (oldMap.get(mc.Id).upload_to_RM_time__c != mc.upload_to_RM_time__c)){
                            //走认款接口，状态不变
                            if(NFM106Controller.NFM106_Ids.contains(mc.Id) == false && mc.Status__c == '引合中'){
                                if (String.isBlank(mc.Maintenance_Contract_No__c)) {
                                    mc.addError('状态变为合同时，必须输入「' + Schema.SObjectType.Maintenance_Contract__c.fields.Maintenance_Contract_No__c.label + '」');
                                    continue;
                                }
                                mcIds.put(mc.Id, mc.Id);
                                mcNoList.add(mc.Maintenance_Contract_No__c);
                                NFM106Controller.debug_msg += 'NFM106_callout_update_' + mc.Name;
                            }
                        }
                    } else {//正常上传SAP接口
                        if (NFM106Controller.NFM106_Ids.contains(mc.Id) == false && mc.Status__c == '契約') {
                            if (oldMap.get(mc.Id).Status__c != mc.Status__c
                                    || oldMap.get(mc.Id).Contract_Conclusion_Date__c != mc.Contract_Conclusion_Date__c
                            ) {
                                if (String.isBlank(mc.Maintenance_Contract_No__c)) {
                                    mc.addError('状态变为合同时，必须输入「' + Schema.SObjectType.Maintenance_Contract__c.fields.Maintenance_Contract_No__c.label + '」');
                                    continue;
                                }
                                mcIds.put(mc.Id, mc.Id);
                                mcNoList.add(mc.Maintenance_Contract_No__c);
                                NFM106Controller.debug_msg += 'NFM106_callout_update_' + mc.Name;
                            }
                        }
                    }
                    //update     wangweipeng                          2021/11/16   end
                }
            }
        }
        if (mcIds.size() > 0) {
            Map<String, Integer> mcNoCntMap = new Map<String, Integer>();
            for (AggregateResult ar : [
                    select Count(Id) cnt,
                           Maintenance_Contract_No__c
                      from Maintenance_Contract__c
                     where Maintenance_Contract_No__c IN :mcNoList
                     group by Maintenance_Contract_No__c]
            ) {
                mcNoCntMap.put(String.valueOf(ar.get('Maintenance_Contract_No__c')), (Integer) ar.get('cnt'));
            }
            for(Maintenance_Contract__c mc : newList) {
                if (mcNoCntMap.get(mc.Maintenance_Contract_No__c) != 1) {
                    mc.addError(mc.Maintenance_Contract_No__c + '：在系统里有' + mcNoCntMap.get(mc.Maintenance_Contract_No__c) + '件，请确认!');
                    mcIds.remove(mc.Id);
                }
            }
            // MessageGroupNumber の採番
            BatchIF_Log__c iflog = new BatchIF_Log__c();
            iflog.Type__c = 'NFM106';
            iflog.Log__c  = 'callout start\n';
            insert iflog;
            iflog = [Select Id, Name from BatchIF_Log__c where Id = :iflog.Id];
            System.debug(Logginglevel.DEBUG, 'NFM106_' + iflog.Name + ' start');                  // callout の中 end のlogを出します
            NFM106Controller.callout(iflog.Id, mcIds.values());
        }
System.debug('-----NFM106Trigger end');
    }

    /**
     * NFM106の送信処理
     *
     * @param iflog_Id           ログテーブルのId
     * @param repairIds          送信対象修理
     */
    @future (callout=true)
    public static void callout(String iflog_Id, List<Id> mcIds) {
        if (mcIds == null || mcIds.size() == 0) {
            return;
        }
        NFM106_Ids.addAll(mcIds);
        
        // MessageGroupNumber の取得
        List<BatchIF_Log__c> iflogList = [Select Id, Name, Log__c, ErrorLog__c from BatchIF_Log__c where Id = :iflog_Id];
        BatchIF_Log__c iflog = null;
        if (iflogList.size() > 0) {
            iflog = iflogList.get(0);
            iflog.ErrorLog__c = '';
        } else {
            // データ取れってないとは、rollbackされていることです
            return;
        }
        String logstr = iflog.Log__c + ' ' + 'NumberOfRecord=' + mcIds.size() + '\n';

        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        // HWAG-BE88UG 【委托】SFDC-SAP搭现有接口添加合同“付款计划”信息 by vivek start
        // Nfm106Sync.RepairContractLogin_element repairContract = new Nfm106Sync.RepairContractLogin_element();
        RepairContractLogin_element repairContract = new RepairContractLogin_element();
        // repairContract.Monitoring = new OlympusCoJpCommonMessage.Monitoring();
        repairContract.Monitoring = new NFMUtil.Monitoring();
        // HWAG-BE88UG 【委托】SFDC-SAP搭现有接口添加合同“付款计划”信息 by vivek start
        repairContract.Monitoring.Tag                  = 'MSGH';
        repairContract.Monitoring.Sender               = '8402';
        repairContract.Monitoring.Receiver             = '1330';
        repairContract.Monitoring.MessageType          = 'NFM106';
        repairContract.Monitoring.MessageGroupNumber   = iflog.Name;
        repairContract.Monitoring.NumberOfRecord       = '' + mcIds.size();
        repairContract.Monitoring.TransmissionDateTime = nowStr;
        repairContract.Monitoring.Text = '';

        BatchIF_Log__c rowData = null;
        try {
            // 転送データを取得、参照先のデータがあるので、ここで検索必要です。
            List<Maintenance_Contract__c> mcList = [select
                    Id, Name, Maintenance_Contract_No__c, SalesOfficeCode__c,
                    Hospital__r.Name, Hospital__r.Management_Code__c,
                    Estimate_Target__c, Dealer__r.Management_Code__c,
                    Contract_Conclusion_Date__c, Contract_Start_Date__c, Contract_End_Date__c, Contract_Amount__c,
                    Estimate_Trial_Money__c, Contract_Period__c, 
                    Payment_Plan_Date_First__c, Payment_Plan_Sum_First__c,
                    Payment_Plan_Date_Second__c, Payment_Plan_Sum_Second__c,
                    Payment_Plan_Date_Third__c, Payment_Plan_Sum_Third__c,
                    Payment_Plan_Date_Forth__c, Payment_Plan_Sum_Forth__c,
                    Payment_Plan_Date_Fifth__c, Payment_Plan_Sum_Fifth__c,
                    Payment_Plan_Date_Sixth__c, Payment_Plan_Sum_Sixth__c,
                    // LJPH-BS89NQ start
                    upload_to_sap_time__c
                     // LJPH-BS89NQ end
                    //URF限次合同2期 LY 20220811 start
                    ,Estimation_Id__r.Total_Number_Asset_P__c
                    ,Estimation_Id__r.URF_P_RepairTime__c
                    ,Estimation_Id__r.Estimate_List_Price_Sum_P__c
                    ,Estimation_Id__r.Total_Number_Asset_V__c
                    ,Estimation_Id__r.URF_V_RepairTime__c
                    ,Estimation_Id__r.Estimate_List_Price_Sum_V__c
                    //URF限次合同2期 LY 20220811 end
               from Maintenance_Contract__c where Id IN :mcIds];          // 削除データを検索しないはず、All ROWS いらないはず
            System.debug(Logginglevel.DEBUG, 'NFM106_ recptList.size()=' + mcList.size());

            // GeneralDataのデータの設定
            // HWAG-BE88UG 【委托】SFDC-SAP搭现有接口添加合同“付款计划”信息 by vivek start
            // repairContract.GeneralData = new List<Nfm106Sync.GeneralData_element>();
            repairContract.GeneralData = new List<GeneralData_element>();
            // HWAG-BE88UG 【委托】SFDC-SAP搭现有接口添加合同“付款计划”信息 by vivek end
            for (Maintenance_Contract__c mc : mcList) {
                // HWAG-BE88UG 【委托】SFDC-SAP搭现有接口添加合同“付款计划”信息 by vivek start
                // Nfm106Sync.GeneralData_element element = new Nfm106Sync.GeneralData_element();
                GeneralData_element element = new GeneralData_element();
                // HWAG-BE88UG 【委托】SFDC-SAP搭现有接口添加合同“付款计划”信息 by vivek end
                repairContract.GeneralData.add(element);
                element.SalesOfficeCode    = mc.SalesOfficeCode__c;          // 数式
                element.ContractNo         = mc.Maintenance_Contract_No__c;
                if (mc.Estimate_Target__c == '经销商') {
                    element.EndUserNoorAgentNo = mc.Dealer__r.Management_Code__c;
                } else {
                    element.EndUserNoorAgentNo = mc.Hospital__r.Management_Code__c;
                }
                element.HospitalName       = mc.Hospital__r.Management_Code__c;
                // LJPH-BS89NQ start
                element.ContractDate       =  NFMUtil.formatDate2Str(mc.upload_to_sap_time__c !=null ?
                                                Date.valueOf(mc.upload_to_sap_time__c) :date.today());
                // LJPH-BS89NQ end
                element.StartDate          = NFMUtil.formatDate2Str(mc.Contract_Start_Date__c);
                element.EndDate            = NFMUtil.formatDate2Str(mc.Contract_End_Date__c);
                element.Amount             = mc.Contract_Amount__c == null ? '' : '' + mc.Contract_Amount__c;
                element.FSERemark          = '';           // TODO 項目追加？
                element.QuotationAmount    = mc.Estimate_Trial_Money__c == null ? '' : '' + mc.Estimate_Trial_Money__c;
                element.ContractPeriod     = '' + mc.Contract_Period__c;
                // HWAG-BE88UG 【委托】SFDC-SAP搭现有接口添加合同“付款计划”信息 by vivek start
                element.PaymentInformation = new List<PaymentInformation_element>();
                if(mc.Payment_Plan_Date_First__c != null || mc.Payment_Plan_Sum_First__c != null){
                    PaymentInformation_element paymentInfor1 = new PaymentInformation_element();
                    element.PaymentInformation.add(paymentInfor1);
                    paymentInfor1.PaymentTime = '1';
                    paymentInfor1.PaymentDate = NFMUtil.formatDate2Str(mc.Payment_Plan_Date_First__c);
                    paymentInfor1.PaymentAmount = mc.Payment_Plan_Sum_First__c.toPlainString();
                }
                if(mc.Payment_Plan_Date_Second__c != null || mc.Payment_Plan_Sum_Second__c != null){
                    PaymentInformation_element paymentInfor2 = new PaymentInformation_element();
                    element.PaymentInformation.add(paymentInfor2);
                    paymentInfor2.PaymentTime = '2';
                    paymentInfor2.PaymentDate = NFMUtil.formatDate2Str(mc.Payment_Plan_Date_Second__c);
                    paymentInfor2.PaymentAmount = mc.Payment_Plan_Sum_Second__c.toPlainString();
                }
                if(mc.Payment_Plan_Date_Third__c != null || mc.Payment_Plan_Sum_Third__c != null){
                    PaymentInformation_element paymentInfor3 = new PaymentInformation_element();
                    element.PaymentInformation.add(paymentInfor3);
                    paymentInfor3.PaymentTime = '3';
                    paymentInfor3.PaymentDate = NFMUtil.formatDate2Str(mc.Payment_Plan_Date_Third__c);
                    paymentInfor3.PaymentAmount = mc.Payment_Plan_Sum_Third__c.toPlainString();
                }
                if(mc.Payment_Plan_Date_Forth__c != null || mc.Payment_Plan_Sum_Forth__c != null){
                    PaymentInformation_element paymentInfor4 = new PaymentInformation_element();
                    element.PaymentInformation.add(paymentInfor4);
                    paymentInfor4.PaymentTime = '4';
                    paymentInfor4.PaymentDate = NFMUtil.formatDate2Str(mc.Payment_Plan_Date_Forth__c);
                    paymentInfor4.PaymentAmount = mc.Payment_Plan_Sum_Forth__c.toPlainString();
                }
                if(mc.Payment_Plan_Date_Fifth__c != null || mc.Payment_Plan_Sum_Fifth__c != null){
                    PaymentInformation_element paymentInfor5 = new PaymentInformation_element();
                    element.PaymentInformation.add(paymentInfor5);
                    paymentInfor5.PaymentTime = '5';
                    paymentInfor5.PaymentDate = NFMUtil.formatDate2Str(mc.Payment_Plan_Date_Fifth__c);
                    paymentInfor5.PaymentAmount = mc.Payment_Plan_Sum_Fifth__c.toPlainString();
                }
                if(mc.Payment_Plan_Date_Sixth__c != null || mc.Payment_Plan_Sum_Sixth__c != null){
                    PaymentInformation_element paymentInfor6 = new PaymentInformation_element();
                    element.PaymentInformation.add(paymentInfor6);
                    paymentInfor6.PaymentTime = '6';
                    paymentInfor6.PaymentDate = NFMUtil.formatDate2Str(mc.Payment_Plan_Date_Sixth__c);
                    paymentInfor6.PaymentAmount = mc.Payment_Plan_Sum_Sixth__c.toPlainString();
                }

                // HWAG-BE88UG 【委托】SFDC-SAP搭现有接口添加合同“付款计划”信息 by vivek end
                //URF限次合同2期 LY 20220811 start
                element.SeriesInformation = new List<SeriesInformation_element>();
                if(mc.Estimation_Id__r.Total_Number_Asset_P__c != null && mc.Estimation_Id__r.Total_Number_Asset_P__c != 0){
                    SeriesInformation_element seriesInformationP = new SeriesInformation_element();
                    element.SeriesInformation.add(seriesInformationP);
                    seriesInformationP.SeriesNo = 'P';
                    seriesInformationP.RepairTime = mc.Estimation_Id__r.URF_P_RepairTime__c.toPlainString();
                    seriesInformationP.SeriesAmount = mc.Estimation_Id__r.Estimate_List_Price_Sum_P__c.toPlainString();
                }
                if(mc.Estimation_Id__r.Total_Number_Asset_V__c != null && mc.Estimation_Id__r.Total_Number_Asset_V__c != 0){
                    SeriesInformation_element seriesInformationP = new SeriesInformation_element();
                    element.SeriesInformation.add(seriesInformationP);
                    seriesInformationP.SeriesNo = 'V';
                    seriesInformationP.RepairTime = mc.Estimation_Id__r.URF_V_RepairTime__c.toPlainString();
                    seriesInformationP.SeriesAmount = mc.Estimation_Id__r.Estimate_List_Price_Sum_V__c.toPlainString();
                }
                //URF限次合同2期 LY 20220811 end

                logstr += '[' + element.ContractNo + ']EndUserNoorAgentNo[' + element.EndUserNoorAgentNo + '] ';
            }

            if (repairContract.GeneralData.size() > 0) {
                logstr += '\ncallout count=' + repairContract.GeneralData.size();
                NFMUtil.Monitoring Monitoring   = new NFMUtil.Monitoring();
                Monitoring.Tag                  = repairContract.Monitoring.Tag;
                Monitoring.Sender               = repairContract.Monitoring.Sender;
                Monitoring.Receiver             = repairContract.Monitoring.Receiver;
                Monitoring.MessageType          = repairContract.Monitoring.MessageType;
                Monitoring.MessageGroupNumber   = repairContract.Monitoring.MessageGroupNumber;
                Monitoring.NumberOfRecord       = repairContract.Monitoring.NumberOfRecord;
                Monitoring.TransmissionDateTime = repairContract.Monitoring.TransmissionDateTime;
                Monitoring.Text = '';

                // HWAG-BE88UG 【委托】SFDC-SAP搭现有接口添加合同“付款计划”信息 by vivek start
                NFM106 nfm106 = new NFM106();
                nfm106.RepairContractLogin = new RepairContractLogin_element();
                nfm106.RepairContractLogin = repairContract;

                // rowData = NFMUtil.makeRowData(Monitoring, 'NFM106', repairContract);
                rowData = NFMUtil.makeRowData(Monitoring, 'NFM106', nfm106);
                // HWAG-BE88UG 【委托】SFDC-SAP搭现有接口添加合同“付款计划”信息 by vivek end
                execute(rowData, iflog);
            }
            // CHAN-BSV3GZ start
            // 保存 status 状态
            logstr += '\nStatus:' + status;
            //CHAN-BSV3GZ end
            logstr += '\nend';
        } catch (Exception ex) {
            // エラーが発生した場合
            System.debug(Logginglevel.ERROR, 'NFM106_' + iflog.Name + ':' + ex.getMessage());
            System.debug(Logginglevel.ERROR, 'NFM106_' + iflog.Name + ':' + ex.getStackTraceString());
            logstr += ex.getMessage();
            iflog.ErrorLog__c += ex.getMessage() + '\n';
            iflog.ErrorLog__c += ex.getStackTraceString() + '\n';
        }
        if (rowData != null) {
            insert rowData;
        }
        System.debug(Logginglevel.DEBUG, 'NFM106_' + iflog.Name + ' end');
        iflog.Log__c = logstr;
        update iflog;
    }

    public static void execute(BatchIF_Log__c rowData, BatchIF_Log__c iflog) {
        //gaozw
        Integer batch_retry_max_cnt = Integer.valueOf(System.Label.batch_retry_max_cnt);
        String rowDataStr = NFMUtil.getRowDataStr(rowData);
        // HWAG-BE88UG 【委托】SFDC-SAP搭现有接口添加合同“付款计划”信息 by vivek start
        // Nfm106Sync.RepairContractLogin_element repairContract = (Nfm106Sync.RepairContractLogin_element) JSON.deserialize(rowDataStr, Nfm106Sync.RepairContractLogin_element.class);
        NFM106 nfm106 = (NFM106) JSON.deserialize(rowDataStr, NFM106.class);
        // String logstr = repairContract.Monitoring.MessageGroupNumber + ' start\n';
        String logstr = nfm106.RepairContractLogin.Monitoring.MessageGroupNumber + ' start\n';
        // HWAG-BE88UG 【委托】SFDC-SAP搭现有接口添加合同“付款计划”信息 by vivek end
        Boolean needUpdateIflog = false;
        if  (iflog == null) {
            needUpdateIflog = true;
            iflog = new BatchIF_Log__c();
            iflog.Type__c = 'NFM106';
            // HWAG-BE88UG 【委托】SFDC-SAP搭现有接口添加合同“付款计划”信息 by vivek start
            // iflog.MessageGroupNumber__c = repairContract.Monitoring.MessageGroupNumber;
            iflog.MessageGroupNumber__c = nfm106.RepairContractLogin.Monitoring.MessageGroupNumber;
            // HWAG-BE88UG 【委托】SFDC-SAP搭现有接口添加合同“付款计划”信息 by vivek end
            iflog.Log__c = logstr;
            iflog.ErrorLog__c = '';
            // insert iflog;
            // iflog = [Select Id, Name, Log__c, ErrorLog__c from BatchIF_Log__c where Id = :iflog.Id];
        } else {
            logstr = iflog.Log__c;
        }

        try {
            // HWAG-BE88UG 【委托】SFDC-SAP搭现有接口添加合同“付款计划”信息 by vivek end
            // Nfm106Sync.HTTPS_Port stub = new Nfm106Sync.HTTPS_Port();
            // stub.timeout_x = 100000; // timeout in milliseconds
            // stub.endpoint_x = NFMUtil.NFM106_ENDPOINT;
            // stub.inputHttpHeaders_x = new Map<String, String>();
            // stub.inputHttpHeaders_x.put('Authorization', NFMUtil.Authorization);
            // if (NFMUtil.CLIENT_CERT_NAME != null) {
            //     stub.clientCertName_x = NFMUtil.CLIENT_CERT_NAME;
            // }
            // Nfm106Sync.RepairContractLogin_element[] pRepairRequest = new Nfm106Sync.RepairContractLogin_element[] { repairContract };
            // NFM106Controller.debug_msg += ', execute()';
            // System.debug('106');
            // OlympusCoJpCommonMessage.LOG_element[] logs = stub.NFM106_Sync_BC2GPI(pRepairRequest);
            //         System.debug('106log' + logs);

           
            
            // CHAN-BSV3GZ start
            // 原逻辑
            //NFMUtil.sendToSap(rowDataStr, NFMUtil.NFM106_ENDPOINT);
            // 接收 SAP返回状态
            status =
             NFMUtil.sendToSapRet(rowDataStr, NFMUtil.NFM106_ENDPOINT);
            // CHAN-BSV3GZ end

            // HWAG-BE88UG 【委托】SFDC-SAP搭现有接口添加合同“付款计划”信息 by vivek end

            // 原則非同期ですので、logsを確認する必要がないでしょう。
             //CHAN-BDPBVY 20190703 end
            // CHAN-BSV3GZ start
            // 原代码
            // rowData.retry_cnt__c=0;
            system.debug('status15122021'+status);
            if (status == 'ok') {
                rowData.retry_cnt__c = 0;
            } else {
                if (rowData.retry_cnt__c == null) rowData.retry_cnt__c = 0;
                if (rowData.retry_cnt__c < batch_retry_max_cnt) {
                    rowData.retry_cnt__c++;
                    LogAutoSendSchedule.assignOneMinute();
                }
                if (rowData.retry_cnt__c >= batch_retry_max_cnt) {
                    rowData.ErrorLog__c = 'status:' + status +
                                          '\n错误次数已经超过自动送信设定的最大次数，请手动送信';
                }
            }
            // CHAN-BSV3GZ end
        } catch (Exception ex) {
            // TODO IOException
            // エラーが発生した場合
            System.debug(Logginglevel.ERROR, 'NFM106_' + iflog.Name + ':' + ex.getMessage());
            System.debug(Logginglevel.ERROR, 'NFM106_' + iflog.Name + ':' + ex.getStackTraceString());
            logstr += ex.getMessage();
            iflog.ErrorLog__c += ex.getMessage() + '\n';
            iflog.ErrorLog__c += ex.getStackTraceString() + '\n';

            //---Gaozw---add
            if (rowData.retry_cnt__c == null) rowData.retry_cnt__c = 0;
            if (rowData.retry_cnt__c < batch_retry_max_cnt){
                rowData.retry_cnt__c ++;
                LogAutoSendSchedule.assignOneMinute();
            }
            if (rowData.retry_cnt__c >= batch_retry_max_cnt){
                rowData.ErrorLog__c = ex.getMessage() + '\n' + ex.getStackTraceString() + '\n' + rowData.ErrorLog__c+'错误次数已经超过自动送信设定的最大次数，请手动送信';
            }
        }
        iflog.Log__c = logstr;
        if (needUpdateIflog) {
            upsert iflog;
            update rowData;
        }
    }
}