/**
 * MaintenanceContractTrigger.trigger より NFM106Trigger() を呼び出す
 */
global without sharing class NFM401Controller {
    public static boolean isRunning = false;
    public static String status;

    public static  String logstr;
    public static List<Messaging.SingleEmailMessage> sendMails;

    public class RepairRequests_elementdummy {
        // 因为附件原因，没法保存全部日志信息，RowData和发送数据分开
        public RepairRequest_elementdummy[] GeneralDatadummy;
    }
    public class RepairRequests_element {
        public RepairRequest_element[] GeneralData;
    }
    public class RepairRequest_elementdummy {
        public String SYSTEM_SOURCEID;
        public String SYSTEM_STATUS;

    }
    public class RepairRequest_element {
        public String SYSTEM_SOURCE_REF_NO;
        public String SYSTEM_SOURCE;
        public String INITIATOR;
        public String EVENT_DATE;
        public String AWARE_DATE;
        public String REPORTING_PERSON;
        public String IS_THIS_COMPLAINT;
        public String EVENT_FOUND_AT;
        public String DEC_TREE_ANS_1;
        public String DEC_TREE_ANS_2;
        public String CUSTOMER_TELEPHONE;
        public String COUNTRY;
        public String ITEM_CODE;
        public String LOT_SERIAL;
        public String PRODUCT_RECEIPT_DATE;
        public String EVALUATION_RESULT;
        public String EVALUATION_COMPLETION_DATE;
        public String EVALUATION_COMPLETED_BY;
        public String INVESTIGATION_REQUIRED;
        public String INVESTIGATION_DECISION_BY;
        public String INVESTIGATION_DECISION_DATE;
        public String COMPLAINT_CLOSURE_DATE;
        public String EVALUATION_INITIATION_DATE;
        public String INITIATION_DATE;
        public String INSTALLATION_DATE;
        public String EVENTDATE_UNKNOWN;
        public String ASSIGN_TO;
        public String SEND_REPORT_COMPETENT_AUTH;
        public String PROC_THERAPEUTIC_DIAGNOSTIC;
        public String GLOBAL_ATTRIBUTE1;
        public String GLOBAL_ATTRIBUTE4;
        public String SYSTEM_SOURCE_REF_NO_2;

        public String PRODUCT_TO_BE_RETURNED;
        public String CORRECTION_ACTION;
        public String INITIATOR_LOCATION;
        public String RECORD_INSERTED_DATE;

        public String CUSTOMER_RESPONSE_REQUESTED;
        public String ORDER_TYPE;
        public String NAME_OF_COMPETENT_AUTH;
        public String NEED_TO_RTRN_ITEM_TO_CUST;
        public String GLOBAL_ATTRIBUTE3;


        public String NO_INVESTIGATION_RATIONALE;
        public String COMPLAINT_CLOSED_BY;
        public String EVALUATION_INITIATION_BY;

        public String SYSTEM_SOURCE_2;
        public String ACTIONS_TAKEN_AT_CUST_SITE;


        public Local_element[] Local;
        public Arcode_element[] AR_CODE;
        public AanaCaucode_element[] ANA_CAU_CODE;
        public Attachment_element[] ATTACHMENT;
    }

    public class Local_element {
        public String EVENT_DESCRIPTION_LOCAL_LANG;
        public String CUSTOMER_NAME_LOCAL_LANG;
        public String CUSTOMER_CITY_LOCAL_LANG;
        public String CUSTOMER_ADDRESS_LOCAL_LANG;
        public String CORRECTION_DESC_LOCAL_LANG;
        public String DEPARTMENT_LOCAL_LANG;
        public String REPORTING_PERSON_LOCAL_LANG;
        public String STATE_LOCAL_LANG;
        public String EVALUATION_RESULT_LOCAL_LANG;
        public String OTHR_PROD_INVOLVED_LOCAL_LANG;
        public String RECORD_INSERTED_DATE;

        public String CDS_METHOD_LOCAL_LANG;

    }
    public class Arcode_element {
        public String AS_REPORTED_CODE;
        public String AS_REPORTED_DESCRIPTION;
        public String RECORD_INSERTED_DATE;
    }
    public class AanaCaucode_element {
        public String PART_CODE;
        public String PART_CODE_DESC;
        public String AS_ANALYZED_CODE;
        public String AS_ANALYZED_CODE_DESC;
        public String CAUSE_CODE;
        public String CAUSE_CODE_DESC;
        public String RECORD_INSERTED_DATE;
    }
    public class Attachment_element {
        public String FILE_DATA;
        public String ATTACHMENT_FIELD_NAME;
        public String FILE_NAME;
        public String GLOBAL_ATTRIBUTE1;
        public String RECORD_INSERTED_DATE;
    }
    public class ContentDocmentCtl{
        public String title;
        public Blob versionData;
        public Integer contentSize;
        public String parentId;
        public String Id;
    }

    webservice static void sendToETQ(String iflog_Id,BatchIF_Log__c rowDataSFDC, List<String> repairIds,String statu){
        if(statu == '' || statu == null){
            statu = 'R3';
        }
        callout(iflog_Id,rowDataSFDC,repairIds,statu);
    }

    /**
     * NFMnewの送信処理
     *
     * @param iflog_Id           ログテーブルのId
     * @param repairIds             送信対象取引先
     */


    // @future (callout=true)
    public static void callout(String iflog_Id,BatchIF_Log__c rowDataSFDC, List<String> repairIds,String statu) {
        if (repairIds == null || repairIds.size() == 0) {
            return;
        }
        if(statu == '' || statu == null){
            statu = 'R3';
        }
        sendMails = new List<Messaging.SingleEmailMessage>();

        // 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 = new BatchIF_Log__c();
        if (iflogList.size() > 0) {
            iflog = iflogList.get(0);
            iflog.ErrorLog__c = '';
        } else {
            // データ取れってないとは、rollbackされていることです
            //return;
            iflog.Type__c = 'NFM401';
            iflog.ErrorLog__c = '';
            iflog.Log__c  = 'callout start\n';
            iflog.Log__c  = 'statu' + statu +'\n';
        }

        // String logstr = iflog.Log__c + '\nNumberOfRecord=' + repairIds.size() + ' ';
        logstr = iflog.Log__c + '\nNumberOfRecord=' + repairIds.size() + ' ';

        RepairRequests_element quotations = new RepairRequests_element();
        RepairRequests_elementdummy quotationsdummy = new RepairRequests_elementdummy();

        // BatchIF転送表 から、コード変換のMapを作成
        Map<String, String> transferMap = new Map<String, String>();
        List<BatchIF_Transfer__c> transferList = [select Table__c,
                                                         Column__c,
                                                         External_value__c,
                                                         Internal_value__c
                                                    from BatchIF_Transfer__c
                                                   where Dropped_Flag__c = false
                                                     and (Table__c = 'Repair__c' or Table__c = 'QIS_Report__c')];
        for (BatchIF_Transfer__c t : transferList) {
            transferMap.put(t.Column__c + t.Internal_value__c, t.External_value__c);
        }

        try {

            // 根据修理ID取得修理相关信息

            List<Repair__c> repairList = [select Id,
                                                Name,
                                                OSH_Affirmant__c,
                                                OSH_Affirmant__r.Username,
                                                Failure_Occurrence_Date__c,
                                                ProblemOccurred__c,
                                                ifDeadHurt__c,
                                                PAE_DetermineResultsText__c,
                                                HospitalPhone__c,
                                                OT_CODE__c,
                                                SerialNumber__c,
                                                On_Site_Repair_Order_Date__c,
                                                Failure_Source__c,
                                                Repair_Inspection_Date__c,
                                                Repair_Returned_To_HP_Date__c,
                                                CreatedDate,
                                                PurchaseOrInstallationDate__c,
                                                ReportAdverseEvents__c,
                                                ASReportedCodeAC__c,
                                                Repair_Ordered_Date__c,
                                                // local部分
                                                Incharge_Staff__r.Alias__c,
                                                Repair_Detail__c,
                                                HP_Name__c,
                                                State_Hospital__c,
                                                HospitalAddress_formula__c,
                                                Department_Name__c,
                                                SupportingProducts__c,
                                                // local部分
                                                //add by rentongxaio 2020-09-15 start
                                                AE_DetermineResult__c,
                                                DateReceiptQuestions__c,
                                                FSE_ApplyForRepair_Day__c,
                                                WhatProject__c,
                                                OCSM_RC_CordingDate__c,
                                                IISE_confirmed_person__c,
                                                OCSM_RC_CordingUser__r.Username,
                                                OCSM_RC_CordingUser__r.Name,
                                                OCSM_RC_CordingUser__c,
                                                Hospital__r.City_Master__r.Name,
                                                Incharge_Staff__r.Name__c,
                                                Asset_Owner__c,
                                                IS_AE__c,
                                                PAE_DetermineResults__c,
                                                Incharge_Staff__r.Username,
                                                IISE_confirmed_day__c,
                                                Incharge_Staff__r.Name,
                                                OSH_Affirmant__r.Name,
                                                OSH_ConfirmationDate__c,
                                                PAE_Determine__c,
                                                //add by rentongxaio 2020-09-15 end
                                                Return_Without_Repair_Date__c,
                                                PAE_DetermineAC__c,
                                                ASReportedCode__c,
                                                Delivered_Product__r.unknow_serial_NO_product__c,
                                                // add by lc WLIG-CGRBFX 2022-08-03 start
                                                Delivered_Product__r.Serinumber_Asset_number_Equ__c
                                                // add by lc WLIG-CGRBFX 2022-08-03 end
                                            from Repair__c
                                            where Id in : repairIds];

            
            // 根据修理和QIS ID取得修理附件相关信息
            // List<Attachment> attachmentList = [select Id,
            //                                     Name,
            //                                     Body,
            //                                     ParentId,
            //                                     BodyLength 
            //                                 from Attachment
            //                                 where ParentId in : repairIds];
            // Map<String,List<Attachment>> rAMap = new Map<String,List<Attachment>>();
            // for (Attachment ra: attachmentList ) {
            //     if (!rAMap.containsKey(ra.ParentId)) {
            //         rAMap.put(ra.ParentId, new List<Attachment>());
            //     }
            //     rAMap.get(ra.ParentId).add(ra);
            // }
            // 根据修理和QIS ID取得修理文件相关信息
            List<ContentDocumentLink> links = [
                select
                ContentDocumentId,
                LinkedEntityId
                from ContentDocumentLink where LinkedEntityId in: repairIds
            ];
            Map<String,List<ContentDocumentLink>> rAMap = new Map<String,List<ContentDocumentLink>>();
            for (ContentDocumentLink ra: links ) {
                if (!rAMap.containsKey(ra.LinkedEntityId)) {
                    rAMap.put(ra.LinkedEntityId, new List<ContentDocumentLink>());
                }
                rAMap.get(ra.LinkedEntityId).add(ra);
            }

            // 根据修理ID取得QIS相关信息
            List<QIS_Report__c> qISList = [select id,Name,
                                                OSH_Affirmant__c,
                                                OSH_Affirmant__r.Username,
                                                Faliour_date__c,
                                                failuer_situation__c,
                                                Damage_For_Doc_Or_Pat__c,
                                                PAE_DetermineResultsText__c,
                                                HospitalPhone__c,
                                                ProductCode_Ext__c,
                                                lot_or_serial__c,
                                                Product_Receipt_Date__c,
                                                QIS_Reply_Comment__c,
                                                QIS_Reply_Comment1__c,
                                                OSH_judgement__c,
                                                Evaluation_Completion_Date__c,
                                                CreatedDate,
                                                QISInstallDate__c,
                                                Report_For_Goz__c,
                                                problem_detail__c,
                                                Hospital__c,
                                                City_Master__c,
                                                HospitalAddress_formula__c,
                                                Hospital_Department__c,
                                                ASReportedCodeAC__c,
                                                OCM_RC_RecievedDate__c,
                                                OSH_Nogoods__c,
                                                OCSM_Nogoods__c,
                                                OSHRecievedDate__c,
                                                // local部分
                                                owner__c,
                                                accsessary_detail__c,
                                                // local部分
                                                AE_DetermineResult__c,
                                                Trable_occur_daY_collect__c,
                                                RC_inspection_date__c,
                                                QIS_ConfirmationDate__c,
                                                IS_AE__c,
                                                PAE_DetermineResults__c,
                                                OSH_ConfirmationDate__c,
                                                Which_Project__c,
                                                OSH_Affirmant__r.Name,
                                                PAE_Determine__c,
                                                next_action__c,
                                                PAE_DetermineAC__c,
                                                //add by rentongxiao 2020-09-21 start
                                                demo_or_asset__c,
                                                QIS_submit_date_for_MBC__c,
                                                Department_Name__c,
                                                usage_frequence__c,
                                                cds_methods__c,
                                                cleanning__c,
                                                disinfection__c,
                                                sterlization__c,
                                                Owner.Name,
                                                Hospital__r.Name,
                                                ASReportedCode__c,
                                                Hospital__r.Address__c,
                                                //add by rentongxiao 2020-09-21 end
                                                nonyushohin__r.unknow_serial_NO_product__c,
                                                // add by lc WLIG-CGRBFX 2022-08-03 start
                                                nonyushohin__r.Serinumber_Asset_number_Equ__c,
                                                // add by lc WLIG-CGRBFX 2022-08-03 end
                                                nonyushohin__c,
                                                QIS_Submit_day__c,
                                                QIS_no_for_m_BC__c
                                            from QIS_Report__c where Id in :repairIds ];

            // 实际发送数据
            quotations.GeneralData = new List<RepairRequest_element>();
            // Rowdata存储数据
            quotationsdummy.GeneralDatadummy = new List<RepairRequest_elementdummy>();

            String username = UserInfo.getName();

            for (Repair__c rr : repairList) {

                RepairRequest_element RepairRequest = new RepairRequest_element();
                RepairRequest_elementdummy RepairRequestdummy = new RepairRequest_elementdummy();
                quotations.GeneralData.add(RepairRequest);
                quotationsdummy.GeneralDatadummy.add(RepairRequestdummy);
                RepairRequestdummy.SYSTEM_SOURCEID = rr.Id;
                //add by rentongxiao 2020-09-15 start
                RepairRequestdummy.SYSTEM_STATUS = statu;
                //add by rentongxiao 2020-09-15 end

                RepairRequest.SYSTEM_SOURCE_REF_NO = rr.Name ;
                RepairRequest.SYSTEM_SOURCE = 'OCSM-SFDC' ;

                if (statu == 'R1') {
                    RepairRequest.INITIATOR = username;
                    // gzw 20211229 AE的时候，不管PAE判断结果，需要Aware Date。 fix start
                    // if (rr.PAE_Determine__c != 'Unknown') {
                    if (rr.PAE_Determine__c != 'Unknown' || rr.AE_DetermineResult__c == 'AE') {
                    // gzw 20211229 AE的时候，不管PAE判断结果，需要Aware Date。 fix end
                        RepairRequest.AWARE_DATE = NFMUtil.formatDate2Str(rr.DateReceiptQuestions__c) ;
                    }
                    RepairRequest.INVESTIGATION_REQUIRED = null ;
                    // 20220507 ljh WLIG-CF56M9 start
                    // RepairRequest.ASSIGN_TO = 'OSH RA' ; 
                    RepairRequest.ASSIGN_TO = 'OSH G-QARA' ;
                    // 20220507 ljh WLIG-CF56M9 end

                    RepairRequest.INITIATOR_LOCATION = 'Olympus Medical : OSH';

                }else if (statu == 'R2') {
                    RepairRequest.INITIATOR_LOCATION = 'Olympus Medical : OSH';

                    RepairRequest.INITIATOR = username;
                    // 20220507 ljh WLIG-CF56M9 start
                    // RepairRequest.ASSIGN_TO = 'OSH RA' ;
                    RepairRequest.ASSIGN_TO = 'OSH G-QARA' ;
                    // 20220507 ljh WLIG-CF56M9 end

                    RepairRequest.INVESTIGATION_REQUIRED = null ;
                    // gzw 20211229 AE的时候，不管PAE判断结果，需要Aware Date。 fix start
                    // if (rr.PAE_DetermineAC__c != 'Unknown') {
                    if (rr.PAE_DetermineAC__c != 'Unknown' || rr.AE_DetermineResult__c == 'AE') {
                    // gzw 20211229 AE的时候，不管PAE判断结果，需要Aware Date。 fix end
                        RepairRequest.AWARE_DATE = NFMUtil.formatDate2Str(rr.Repair_Inspection_Date__c) ;
                    }
                    
                    RepairRequest.EVALUATION_INITIATION_DATE = NFMUtil.formatDate2Str(Date.today()) ;
                    RepairRequest.EVALUATION_INITIATION_BY = username;
                }else if (statu == 'R3') {
                    RepairRequest.INITIATOR_LOCATION = 'Olympus Medical : OCSM';
                    if (String.isNotBlank(rr.Incharge_Staff__c)) {
                        RepairRequest.INITIATOR = rr.Incharge_Staff__r.Name;
                    }

                    if (rr.IISE_confirmed_day__c != null) {
                        RepairRequest.EVALUATION_COMPLETION_DATE = NFMUtil.formatDate2Str(rr.IISE_confirmed_day__c);
                        RepairRequest.INVESTIGATION_DECISION_DATE = NFMUtil.formatDate2Str(rr.IISE_confirmed_day__c) ;
                        RepairRequest.COMPLAINT_CLOSURE_DATE = NFMUtil.formatDate2Str(rr.IISE_confirmed_day__c) ;
                    }else {
                        RepairRequest.EVALUATION_COMPLETION_DATE = NFMUtil.formatDate2Str(rr.OCSM_RC_CordingDate__c);
                        RepairRequest.INVESTIGATION_DECISION_DATE = NFMUtil.formatDate2Str(rr.OCSM_RC_CordingDate__c) ;
                        RepairRequest.COMPLAINT_CLOSURE_DATE = NFMUtil.formatDate2Str(rr.OCSM_RC_CordingDate__c) ;
                    }

                    if (rr.IISE_confirmed_person__c != null) {
                        RepairRequest.EVALUATION_COMPLETED_BY = rr.IISE_confirmed_person__c;
                        RepairRequest.INVESTIGATION_DECISION_BY = rr.IISE_confirmed_person__c;
                        RepairRequest.COMPLAINT_CLOSED_BY = rr.IISE_confirmed_person__c;
                        RepairRequest.EVALUATION_INITIATION_BY = rr.IISE_confirmed_person__c;
                    }else {
                        RepairRequest.EVALUATION_COMPLETED_BY = rr.OCSM_RC_CordingUser__r.Name;
                        RepairRequest.INVESTIGATION_DECISION_BY = rr.OCSM_RC_CordingUser__r.Name;
                        RepairRequest.COMPLAINT_CLOSED_BY = rr.OCSM_RC_CordingUser__r.Name;
                        RepairRequest.EVALUATION_INITIATION_BY = rr.OCSM_RC_CordingUser__r.Name;
                    }

                    RepairRequest.INVESTIGATION_REQUIRED = 'N' ;
                    RepairRequest.EVALUATION_INITIATION_DATE = NFMUtil.formatDate2Str(rr.OCSM_RC_CordingDate__c) ;
                    RepairRequest.NO_INVESTIGATION_RATIONALE = 'This is a non-reportable malfunction complaint according to the RSS.  This failure mode will be monitored through on-going trending.';

                    if (rr.Return_Without_Repair_Date__c == null) {
                        RepairRequest.CORRECTION_ACTION = 'Routine repair; no corrective action required.';
                    }else if (rr.Return_Without_Repair_Date__c != null) {
                        RepairRequest.CORRECTION_ACTION = 'Return goods; no corrective action required.';
                    }


                    RepairRequest.ASSIGN_TO = null ;
                }

                RepairRequest.CUSTOMER_RESPONSE_REQUESTED = 'N';
                RepairRequest.ORDER_TYPE = NFMUtil.getMapValue(transferMap,'Asset_Owner__c', rr.Asset_Owner__c, iflog) ;
                RepairRequest.NAME_OF_COMPETENT_AUTH = rr.ReportAdverseEvents__c == '有' ? 'Local PMPA' : null;
                RepairRequest.NEED_TO_RTRN_ITEM_TO_CUST = 'N';

                RepairRequest.GLOBAL_ATTRIBUTE3 = String.valueOf(rr.DateReceiptQuestions__c);

                RepairRequest.EVENT_DATE = NFMUtil.formatDate2Str(rr.Failure_Occurrence_Date__c) ;
                RepairRequest.REPORTING_PERSON = null ;
                RepairRequest.IS_THIS_COMPLAINT = 'Y' ;

                RepairRequest.EVENT_FOUND_AT = NFMUtil.getMapValue(transferMap,
                            'ProblemOccurred__c', rr.ProblemOccurred__c, iflog);
                RepairRequest.DEC_TREE_ANS_1 = NFMUtil.getMapValue(transferMap,
                            'ifDeadHurt__c', rr.ifDeadHurt__c, iflog);
                if (rr.ifDeadHurt__c == '无') {
                    if (rr.PAE_DetermineResultsText__c == 'PAE') {
                        RepairRequest.DEC_TREE_ANS_2 = 'Y' ;
                    }
                    if (rr.PAE_DetermineResultsText__c == 'nonPAE') {
                        RepairRequest.DEC_TREE_ANS_2 = 'N' ;
                    }
                    if (rr.PAE_DetermineResultsText__c == 'Unknown') {
                        RepairRequest.DEC_TREE_ANS_2 = 'U' ;

                    }
                }
                RepairRequest.CUSTOMER_TELEPHONE =  String.isBlank(rr.HospitalPhone__c)  ? '/' : rr.HospitalPhone__c;
                RepairRequest.COUNTRY = 'China' ;
                RepairRequest.ITEM_CODE = rr.OT_CODE__c ;
                // add by lc WLIG-CGRBFX 2022-08-03 start
                //RepairRequest.LOT_SERIAL = rr.Delivered_Product__r.unknow_serial_NO_product__c == true ? 'Unknown' : rr.SerialNumber__c.indexOf('(') > 0 ? rr.SerialNumber__c.subString(0,rr.SerialNumber__c.indexOf('(')) : rr.SerialNumber__c ;
                RepairRequest.LOT_SERIAL = rr.Delivered_Product__r.unknow_serial_NO_product__c || rr.Delivered_Product__r.Serinumber_Asset_number_Equ__c ? 'Unknown' : rr.SerialNumber__c.indexOf('(') > 0 ? rr.SerialNumber__c.subString(0,rr.SerialNumber__c.indexOf('(')) : rr.SerialNumber__c ;
                // add by lc WLIG-CGRBFX 2022-08-03 end

                RepairRequest.PRODUCT_RECEIPT_DATE = NFMUtil.formatDate2Str(rr.Repair_Ordered_Date__c);
                RepairRequest.EVALUATION_RESULT = null;
                RepairRequest.INITIATION_DATE = NFMUtil.formatDate2Str(rr.FSE_ApplyForRepair_Day__c) ;
                
                // 精琢科技  2022-03-03 LJPH-CC5ANQ 【委托】之前会传一个1900-1-1的日期，导致报错，现在改为传null zxk  start
                // gzw fix 20220427 1900-1-1 send null
                if(rr.PurchaseOrInstallationDate__c != null && rr.PurchaseOrInstallationDate__c.year() != 1900){
                    RepairRequest.INSTALLATION_DATE = NFMUtil.formatDate2Str(rr.PurchaseOrInstallationDate__c) ;
                }else{
                    RepairRequest.INSTALLATION_DATE =  null;
                }
                // 精琢科技  2022-03-03 LJPH-CC5ANQ 【委托】之前会传一个1900-1-1的日期，导致报错，现在改为传null zxk  end
                
                RepairRequest.EVENTDATE_UNKNOWN = null ;
                RepairRequest.SEND_REPORT_COMPETENT_AUTH = NFMUtil.getMapValue(transferMap,'ReportAdverseEvents__c', rr.ReportAdverseEvents__c, iflog) ;
                RepairRequest.PROC_THERAPEUTIC_DIAGNOSTIC = NFMUtil.getMapValue(transferMap,'WhatProject__c', rr.WhatProject__c, iflog) ;
                
                RepairRequest.GLOBAL_ATTRIBUTE1 = 'Olympus Medical : OSH' ;

                RepairRequest.PRODUCT_TO_BE_RETURNED = rr.Repair_Ordered_Date__c == null ? 'N' : 'Y';
                RepairRequest.RECORD_INSERTED_DATE = NFMUtil.formatDate2Str(Date.today());
                
                // Local取修理上信息

                RepairRequest.Local = new List<Local_element>();
                Local_element Localdet = new Local_element();
                RepairRequest.Local.add(Localdet);
                Localdet.EVENT_DESCRIPTION_LOCAL_LANG = rr.Repair_Detail__c;
                Localdet.CUSTOMER_NAME_LOCAL_LANG = rr.HP_Name__c;
                Localdet.CUSTOMER_CITY_LOCAL_LANG = rr.Hospital__r.City_Master__r.Name;
                Localdet.CUSTOMER_ADDRESS_LOCAL_LANG = rr.HospitalAddress_formula__c;
                if (statu == 'R3') {
                
                    if (rr.Return_Without_Repair_Date__c == null) {
                        Localdet.CORRECTION_DESC_LOCAL_LANG = 'Routine repair; refer to Final Universal Code.';
                    }else if (rr.Return_Without_Repair_Date__c != null) {
                        Localdet.CORRECTION_DESC_LOCAL_LANG = 'Return goods; no correction required.';
                    }
                
                }
                Localdet.DEPARTMENT_LOCAL_LANG = rr.Department_Name__c;
                Localdet.REPORTING_PERSON_LOCAL_LANG = rr.Incharge_Staff__r.Name__c;
                Localdet.STATE_LOCAL_LANG = 'Refer Address' ;
                Localdet.EVALUATION_RESULT_LOCAL_LANG = rr.Failure_Source__c;
                
                Localdet.OTHR_PROD_INVOLVED_LOCAL_LANG = rr.SupportingProducts__c ;
                Localdet.RECORD_INSERTED_DATE = NFMUtil.formatDate2Str(Date.today());
                

                //AR_CODE
                RepairRequest.AR_CODE = new List<Arcode_element>();
                
                if (String.isNotBlank(rr.ASReportedCode__c)) {
                    List<String> ARCODEList = rr.ASReportedCode__c.split(',');
                    for(String str : ARCODEList){
                        Arcode_element Arcode = new Arcode_element();
                        RepairRequest.AR_CODE.add(Arcode);
                        Arcode.AS_REPORTED_CODE = str;
                        Arcode.AS_REPORTED_DESCRIPTION = '';
                
                        Arcode.RECORD_INSERTED_DATE = NFMUtil.formatDate2Str(Date.today());
                
                    }                
                }

                //ANA_CAU_CODE
                RepairRequest.ANA_CAU_CODE = new List<AanaCaucode_element>();
                if (String.isNotBlank(rr.ASReportedCodeAC__c)) {
                    List<String> ANACODEList = rr.ASReportedCodeAC__c.split(',');
                    for (String str : ANACODEList) {
                        AanaCaucode_element AanaCaucode = new AanaCaucode_element();
                        RepairRequest.ANA_CAU_CODE.add(AanaCaucode);
                        if (statu != 'R1') {
                            AanaCaucode.AS_ANALYZED_CODE = str;
                        }

                        AanaCaucode.RECORD_INSERTED_DATE = NFMUtil.formatDate2Str(Date.today());
                     
                    }
                }

                 //ATTACHMENT
                RepairRequest.ATTACHMENT = new List<Attachment_element>();
                // List<Attachment> attList = rAMap.get(rr.id);
                // if (attList != null && attList.size() > 0) {
                //     for(Attachment rpd : attList){

                //         //2020-11-05  如果附件大小超过1.5mb 发送邮件提示
                //         if (rpd.BodyLength > 1048576) {
                //             sendemail(rpd,'修理'+rr.Name);
                //         }else{
                //             Attachment_element Attachmentdet = new Attachment_element();
                //             RepairRequest.ATTACHMENT.add(Attachmentdet);
                //             Attachmentdet.FILE_DATA = EncodingUtil.base64Encode(rpd.body);
                //             Attachmentdet.ATTACHMENT_FIELD_NAME = 'COMPLAINTS_DOCUMENT_ATTACHMENT';
                //             Attachmentdet.FILE_NAME = rpd.Name;
                //             Attachmentdet.GLOBAL_ATTRIBUTE1 = 'COMPLAINT';
                //             Attachmentdet.RECORD_INSERTED_DATE = NFMUtil.formatDate2Str(Date.today());
                //         }
                //     }
                // }
                List<ContentDocumentLink> attList = rAMap.get(rr.id);
                if (attList != null && attList.size() > 0) {
                    for(ContentDocumentLink rpd : attList){
                        ContentVersion version = [
                            select
                            Title,
                            VersionData,
                            ContentSize
                            from ContentVersion where ContentDocumentId =: rpd.ContentDocumentId
                        ];
                        //2020-11-05  如果附件大小超过1.5mb 发送邮件提示
                        if (version.ContentSize > 1048576) {
                            sendemail(version,'修理'+rr.Name);
                        }else{
                            Attachment_element Attachmentdet = new Attachment_element();
                            RepairRequest.ATTACHMENT.add(Attachmentdet);
                            Attachmentdet.FILE_DATA = EncodingUtil.base64Encode(version.VersionData);
                            Attachmentdet.ATTACHMENT_FIELD_NAME = 'COMPLAINTS_DOCUMENT_ATTACHMENT';
                            Attachmentdet.FILE_NAME = version.Title;
                            Attachmentdet.GLOBAL_ATTRIBUTE1 = 'COMPLAINT';
                            Attachmentdet.RECORD_INSERTED_DATE = NFMUtil.formatDate2Str(Date.today());
                        }
                    }
                }
            }

            // QIS 遍历生成数据
            for (QIS_Report__c qr : qISList) {

                RepairRequest_element RepairRequest = new RepairRequest_element();
                RepairRequest_elementdummy RepairRequestdummy = new RepairRequest_elementdummy();
                quotations.GeneralData.add(RepairRequest);
                quotationsdummy.GeneralDatadummy.add(RepairRequestdummy);
                RepairRequestdummy.SYSTEM_SOURCEID = qr.Id;
                RepairRequestdummy.SYSTEM_STATUS = statu;
                RepairRequest.SYSTEM_SOURCE_REF_NO = qr.Name ;
                RepairRequest.SYSTEM_SOURCE = 'OCSM-SFDC' ;

                RepairRequest.ORDER_TYPE = qr.demo_or_asset__c == '购买' ? 'Purchased' : qr.demo_or_asset__c == '备品' ? 'Loaner' : null ;

                RepairRequest.CUSTOMER_RESPONSE_REQUESTED = 'N';
                RepairRequest.NAME_OF_COMPETENT_AUTH = qr.Report_For_Goz__c == '有' ? 'Local PMPA' : null;
                RepairRequest.NEED_TO_RTRN_ITEM_TO_CUST = 'N';
                RepairRequest.GLOBAL_ATTRIBUTE3 = String.valueOf(qr.Trable_occur_daY_collect__c);

                RepairRequest.INITIATOR = username;
                RepairRequest.EVENT_DATE = NFMUtil.formatDate2Str(qr.Faliour_date__c) ;
                RepairRequest.REPORTING_PERSON = null;
                RepairRequest.IS_THIS_COMPLAINT = 'Y' ;

                RepairRequest.EVENT_FOUND_AT = NFMUtil.getMapValue(transferMap,
                            'failuer_situation__c', qr.failuer_situation__c, iflog);
                RepairRequest.DEC_TREE_ANS_1 = NFMUtil.getMapValue(transferMap,
                            'ifDeadHurt__c', qr.Damage_For_Doc_Or_Pat__c, iflog);

                if (qr.Damage_For_Doc_Or_Pat__c == '无') {
                    if (qr.PAE_DetermineResultsText__c == 'PAE') {
                        RepairRequest.DEC_TREE_ANS_2 = 'Y' ;
                    }
                    if (qr.PAE_DetermineResultsText__c == 'nonPAE') {
                        RepairRequest.DEC_TREE_ANS_2 = 'N' ;
                    }
                    if (qr.PAE_DetermineResultsText__c == 'Unknown') {
                        RepairRequest.DEC_TREE_ANS_2 = 'U' ;
                    }
                }
                RepairRequest.CUSTOMER_TELEPHONE = String.isBlank(qr.HospitalPhone__c)  ? '/' : qr.HospitalPhone__c;
                RepairRequest.COUNTRY = 'China' ;
                RepairRequest.ITEM_CODE = qr.ProductCode_Ext__c ;
                if (qr.nonyushohin__c != null) {
                    // add by lc WLIG-CGRBFX 2022-08-03 start
                    //RepairRequest.LOT_SERIAL = qr.nonyushohin__r.unknow_serial_NO_product__c == true ? 'Unknown' : qr.lot_or_serial__c.indexOf('(') > 0 ? qr.lot_or_serial__c.subString(0,qr.lot_or_serial__c.indexOf('(')) : qr.lot_or_serial__c ;
                    RepairRequest.LOT_SERIAL = qr.nonyushohin__r.unknow_serial_NO_product__c || qr.nonyushohin__r.Serinumber_Asset_number_Equ__c ? 'Unknown' : qr.lot_or_serial__c.indexOf('(') > 0 ? qr.lot_or_serial__c.subString(0,qr.lot_or_serial__c.indexOf('(')) : qr.lot_or_serial__c ;
                    // add by lc WLIG-CGRBFX 2022-08-03 end
                }

                RepairRequest.EVALUATION_RESULT = null ;

                RepairRequest.SYSTEM_SOURCE_2 = 'OSH-QIS';
                RepairRequest.INITIATOR_LOCATION = 'Olympus Medical : OSH';
                RepairRequest.ACTIONS_TAKEN_AT_CUST_SITE = NFMUtil.getMapValue(transferMap,'qis_next_action__c', qr.next_action__c, iflog) ;

                RepairRequest.INITIATION_DATE = NFMUtil.formatDate2Str(qr.QIS_Submit_day__c) ;
                // 精琢科技  2022-03-03 LJPH-CC5ANQ 【委托】之前会传一个1900-1-1的日期，导致报错，现在改为传null zxk  start
                // gzw fix 20220427 1900-1-1 send null
                if(qr.QISInstallDate__c != null && qr.QISInstallDate__c.year() != 1900){
                    RepairRequest.INSTALLATION_DATE = NFMUtil.formatDate2Str(qr.QISInstallDate__c) ;
                }else{
                    RepairRequest.INSTALLATION_DATE =  null;
                }
                // 精琢科技  2022-03-03 LJPH-CC5ANQ 【委托】之前会传一个1900-1-1的日期，导致报错，现在改为传null zxk  end

                RepairRequest.EVENTDATE_UNKNOWN = null ;

                RepairRequest.SEND_REPORT_COMPETENT_AUTH = NFMUtil.getMapValue(transferMap,'ReportAdverseEvents__c', qr.Report_For_Goz__c, iflog) ;

                RepairRequest.PROC_THERAPEUTIC_DIAGNOSTIC = NFMUtil.getMapValue(transferMap,'WhatProject__c', qr.Which_Project__c, iflog) ;

                RepairRequest.GLOBAL_ATTRIBUTE1 = 'Olympus Medical : OSH' ;

                RepairRequest.GLOBAL_ATTRIBUTE4 = String.valueOf(qr.QIS_submit_date_for_MBC__c);
                RepairRequest.SYSTEM_SOURCE_REF_NO_2 = qr.QIS_no_for_m_BC__c;

                if ((qr.OCSM_Nogoods__c == false && qr.OCM_RC_RecievedDate__c != null) ||
                    (qr.OSH_Nogoods__c == false && qr.OSHRecievedDate__c != null)) {
                    RepairRequest.PRODUCT_TO_BE_RETURNED = 'Y';
                }else{
                    RepairRequest.PRODUCT_TO_BE_RETURNED = 'N';
                }

                if (RepairRequest.PRODUCT_TO_BE_RETURNED == 'Y') {
                    RepairRequest.PRODUCT_RECEIPT_DATE = NFMUtil.formatDate2Str(qr.Product_Receipt_Date__c);
                }else{
                    RepairRequest.PRODUCT_RECEIPT_DATE = null;
                }

                RepairRequest.RECORD_INSERTED_DATE = NFMUtil.formatDate2Str(Date.today());


                // Locals
                RepairRequest.Local = new List<Local_element>();
                Local_element Localdet = new Local_element();
                RepairRequest.Local.add(Localdet);
                Localdet.EVENT_DESCRIPTION_LOCAL_LANG = qr.problem_detail__c;
                Localdet.CUSTOMER_CITY_LOCAL_LANG = qr.City_Master__c;
                Localdet.DEPARTMENT_LOCAL_LANG = qr.Department_Name__c;
                Localdet.STATE_LOCAL_LANG = 'Refer Address' ;
                Localdet.OTHR_PROD_INVOLVED_LOCAL_LANG = qr.accsessary_detail__c ;
                Localdet.RECORD_INSERTED_DATE = NFMUtil.formatDate2Str(Date.today());
                Localdet.CDS_METHOD_LOCAL_LANG = '【使用频率】\n'+qr.usage_frequence__c+'\n【清洗消毒方法(方法,化学消毒剂)】\n'+qr.cds_methods__c+'\n【清洗】\n'+qr.cleanning__c+'\n【消毒】\n'+qr.disinfection__c+'\n【杀菌(温度及杀菌时间)】\n'+qr.sterlization__c;
                Localdet.REPORTING_PERSON_LOCAL_LANG = qr.Owner.Name;
                Localdet.CUSTOMER_NAME_LOCAL_LANG = qr.Hospital__r.Name;
                Localdet.CUSTOMER_ADDRESS_LOCAL_LANG = qr.Hospital__r.Address__c;

                // AR_CODE
                RepairRequest.AR_CODE = new List<Arcode_element>();
                if (String.isNotBlank(qr.ASReportedCode__c)) {
                    List<String> arcodeList = qr.ASReportedCode__c.split(',');
                    for(String str : arcodeList){
                        Arcode_element Arcode = new Arcode_element();
                        RepairRequest.AR_CODE.add(Arcode);
                        Arcode.AS_REPORTED_CODE = str;
                        Arcode.AS_REPORTED_DESCRIPTION = '';
                        Arcode.RECORD_INSERTED_DATE = NFMUtil.formatDate2Str(Date.today());
                    }
                }
                
                // ANA_CAU_CODE
                RepairRequest.ANA_CAU_CODE = new List<AanaCaucode_element>();
                if (String.isNotBlank(qr.ASReportedCodeAC__c)) {
                    List<String> ANACODEList = qr.ASReportedCodeAC__c.split(',');
                    for (String str : ANACODEList) {
                        AanaCaucode_element AanaCaucode = new AanaCaucode_element();
                        RepairRequest.ANA_CAU_CODE.add(AanaCaucode);
                        if (statu != 'Q1') {
                            AanaCaucode.AS_ANALYZED_CODE = str;
                        }

                        AanaCaucode.RECORD_INSERTED_DATE = NFMUtil.formatDate2Str(Date.today());
                    }
                }

                // ATTACHMENT
                RepairRequest.ATTACHMENT = new List<Attachment_element>();
                // List<Attachment> attList = rAMap.get(qr.id);
                // if (attList != null && attList.size() > 0) {
                //     for(Attachment rpd :attList){
                //          //2020-11-05  如果附件大小超过1.5mb 发送邮件提示
                //         if (rpd.BodyLength > 1048576) {
                //             sendemail(rpd,'QIS '+qr.Name);
                //         }else{
                //             Attachment_element Attachmentdet = new Attachment_element();
                //             RepairRequest.ATTACHMENT.add(Attachmentdet);
                //             Attachmentdet.FILE_DATA = EncodingUtil.base64Encode(rpd.body);
                //             Attachmentdet.ATTACHMENT_FIELD_NAME = 'COMPLAINTS_DOCUMENT_ATTACHMENT';
                //             Attachmentdet.FILE_NAME = rpd.Name;
                //             Attachmentdet.GLOBAL_ATTRIBUTE1 = 'COMPLAINT';
                //             Attachmentdet.RECORD_INSERTED_DATE = NFMUtil.formatDate2Str(Date.today());
                //         }
                //     }
                // }
                List<ContentDocumentLink> attList = rAMap.get(qr.id);
                if (attList != null && attList.size() > 0) {
                    for(ContentDocumentLink rpd :attList){
                         //2020-11-05  如果附件大小超过1.5mb 发送邮件提示
                        ContentVersion version = [
                            select
                            VersionData,
                            Title,
                            ContentSize
                            from ContentVersion where ContentDocumentId =: rpd.ContentDocumentId
                        ];
                        if (version.ContentSize > 1048576) {
                            sendemail(version,'QIS '+qr.Name);
                        }else{
                            Attachment_element Attachmentdet = new Attachment_element();
                            RepairRequest.ATTACHMENT.add(Attachmentdet);
                            Attachmentdet.FILE_DATA = EncodingUtil.base64Encode(version.VersionData);
                            Attachmentdet.ATTACHMENT_FIELD_NAME = 'COMPLAINTS_DOCUMENT_ATTACHMENT';
                            Attachmentdet.FILE_NAME = version.Title;
                            Attachmentdet.GLOBAL_ATTRIBUTE1 = 'COMPLAINT';
                            Attachmentdet.RECORD_INSERTED_DATE = NFMUtil.formatDate2Str(Date.today());
                        }
                    }
                }

                if (statu == 'Q1') {
                    RepairRequest.AWARE_DATE = NFMUtil.formatDate2Str(qr.Trable_occur_daY_collect__c);
                    // 20220507 ljh WLIG-CF56M9 start
                    // RepairRequest.ASSIGN_TO = 'OSH RA' ;
                    RepairRequest.ASSIGN_TO = 'OSH G-QARA' ;
                    // 20220507 ljh WLIG-CF56M9 end
                        
                }else if (statu == 'Q2') {

                    if (qr.AE_DetermineResult__c == 'nonAE' && qr.PAE_Determine__c == 'nonPAE' && qr.PAE_DetermineAC__c == 'nonPAE') {
                        RepairRequest.ASSIGN_TO = 'OSH QA';
                    }else{
                        // 20220507 ljh WLIG-CF56M9 start
                        // RepairRequest.ASSIGN_TO = 'OSH RA';
                        RepairRequest.ASSIGN_TO = 'OSH G-QARA';
                        // 20220507 ljh WLIG-CF56M9 end
                    }
                    RepairRequest.EVALUATION_INITIATION_DATE = NFMUtil.formatDate2Str(Date.today()) ;

                    RepairRequest.EVALUATION_INITIATION_BY = username;
                    RepairRequest.AWARE_DATE = NFMUtil.formatDate2Str(qr.RC_inspection_date__c);
                    Localdet.EVALUATION_RESULT_LOCAL_LANG = '【检测/分析情况详述】\n' +  qr.QIS_Reply_Comment__c + '\n【第一次检测/分析情况详述】\n' +  qr.QIS_Reply_Comment1__c  + '\n【检测/分析情况详述(OSH)】\n' +  qr.OSH_judgement__c ;

                }else if (statu == 'Q3') {
                    RepairRequest.EVALUATION_INITIATION_DATE = NFMUtil.formatDate2Str(Date.today()) ;

                    RepairRequest.EVALUATION_INITIATION_BY = username;
                    RepairRequest.EVALUATION_COMPLETION_DATE = NFMUtil.formatDate2Str(Date.today()) ;

                    RepairRequest.EVALUATION_COMPLETED_BY = username;
                    RepairRequest.INVESTIGATION_DECISION_BY = username;
                    RepairRequest.INVESTIGATION_DECISION_DATE = NFMUtil.formatDate2Str(Date.today()) ;
                    RepairRequest.COMPLAINT_CLOSURE_DATE = NFMUtil.formatDate2Str(Date.today());
                    Localdet.EVALUATION_RESULT_LOCAL_LANG = '【检测/分析情况详述】\n' +  qr.QIS_Reply_Comment__c + '\n【第一次检测/分析情况详述】\n' +  qr.QIS_Reply_Comment1__c  + '\n【检测/分析情况详述(OSH)】\n' +  qr.OSH_judgement__c ;

                    if (qr.next_action__c == '无偿更换') {
                        RepairRequest.CORRECTION_ACTION = 'Exchange goods; no corrective action required.';
                    }else if (qr.next_action__c == '送回') {
                        RepairRequest.CORRECTION_ACTION = 'Return goods; no corrective action required.';
                    }else {
                        RepairRequest.CORRECTION_ACTION = 'Routine repair; no corrective action required.';
                    }
                    RepairRequest.INVESTIGATION_REQUIRED = 'N';
                    RepairRequest.NO_INVESTIGATION_RATIONALE = 'This is a non-reportable malfunction complaint according to the RSS.  This failure mode will be monitored through on-going trending.';
                    RepairRequest.COMPLAINT_CLOSED_BY = username;

                    if (qr.next_action__c == '无偿更换') {
                        Localdet.CORRECTION_DESC_LOCAL_LANG = 'Exchange goods; no correction required.';
                    }else if (qr.next_action__c == '送回') {
                        Localdet.CORRECTION_DESC_LOCAL_LANG = 'Return goods; no correction required.';
                    }else {
                        Localdet.CORRECTION_DESC_LOCAL_LANG = 'Routine repair; refer to Final Universal Code.';
                    }

                    RepairRequest.ASSIGN_TO = null;
                }
            }



            String strrowDatatoETQ = JSON.serialize(quotations);
            System.debug('strrowDatatoETQ::'+strrowDatatoETQ);
            if (rowDataSFDC == null) {

                rowDataSFDC = NFMUtil.makeRowData(iflog, 'NFM401', quotationsdummy);
            }
            execute(strrowDatatoETQ,rowDataSFDC, iflog);
            logstr += 'Status:' + status;
            logstr += '\nend';


        } catch (Exception ex) {
            // エラーが発生した場合
            System.debug(Logginglevel.ERROR, 'NFM401_' + iflog.Name + ':' + ex.getMessage());
            System.debug(Logginglevel.ERROR, 'NFM401_' + iflog.Name + ':' + ex.getStackTraceString());
            logstr += ex.getMessage();
            iflog.ErrorLog__c += ex.getMessage() + '\n';
            iflog.ErrorLog__c += ex.getStackTraceString() + '\n';
        }

        if (rowDataSFDC != null) {
            upsert rowDataSFDC;
        }
        iflog.Log__c = logstr;
        upsert iflog;






    }

    //add by rentx 2020-11-05 start
    //发送提示附件过大电子邮件
    public static void SendEmail(ContentVersion rpd,String str){
        
        // sendMails = new List<Messaging.SingleEmailMessage>();
        //发送邮件
        String title = '';
        String body = '';
        title = str+' 中的文件 '+rpd.Title+' 发送etq失败提醒';
        body =  '您好,';
        body += '<br/>';
        body += str+'中的文件'+rpd.Title+'过大,导致发送ETQ失败!';
        body += '<br/>';
        body += '请手动上传到ETQ';
        List<String> toMailList = new List<String>();
        List<String> toccList = new List<String>();
        // emas.add(UserInfo.getUserEmail());
        //收信人
        toMailList.add(UserInfo.getUserEmail());
        // 20220607 ljh add start
        String uid = UserInfo.getUserId();
        String BUid = System.Label.Batch_User_Id;
        if (uid.substring(0,15) == BUid.substring(0,15)) {
            for(String email : System.Label.RepairETQ.split(',')){
                toMailList.add(email);
            }
        }
        // 20220607 ljh add start
        //抄送人
        toccList.add('Hiroki_Hotta@olympus.com.cn');
        toccList.add('olympus@prec-tech.com');
        Messaging.SingleEmailMessage messageNEW = new Messaging.SingleEmailMessage();
        messageNEW.subject = title;
        //messageNEW.plainTextBody = body;
        messageNEW.htmlBody = body;
        messageNEW.setCharset('UTF-8');
        messageNEW.toAddresses = toMailList;
        messageNEW.ccAddresses = toccList;
        sendMails.add(messageNEW);

    }

    //add by rentx 2020-11-05 end

    // 手工调用处理方法
    public static void executeAuto(BatchIF_Log__c rowData) {
        String rowDataStr = NFMUtil.getRowDataStr(rowData);
        RepairRequests_elementdummy quotationsdummy = (RepairRequests_elementdummy) JSON.deserialize(rowDataStr, RepairRequests_elementdummy.class);
        List<String> repairOrQisidList = new List<String>();
        String statu = '';
        for (RepairRequest_elementdummy infoGD : quotationsdummy.GeneralDatadummy) {
            if (String.isNotBlank(infoGD.SYSTEM_SOURCEID)) {
                repairOrQisidList.add(infoGD.SYSTEM_SOURCEID);
            }
            statu = infoGD.SYSTEM_STATUS;
        }

        System.debug(Logginglevel.ERROR, 'repairOrQisidList_： ' + repairOrQisidList);
        callout(null,rowData,repairOrQisidList,statu);
    }

    public static void execute(String strrowDatatoETQ, BatchIF_Log__c rowDataSFDC, BatchIF_Log__c iflog) {

        Integer batch_retry_max_cnt = Integer.valueOf(System.Label.batch_retry_max_cnt);


        // String logstr = rowDataSFDC.MessageGroupNumber__c + ' start\n';
        logstr += rowDataSFDC.MessageGroupNumber__c + ' start\n';
        Boolean needUpdateIflog = false;
        if  (iflog == null) {
            needUpdateIflog = true;
            iflog = new BatchIF_Log__c();
            iflog.Type__c = 'NFM401';
            iflog.MessageGroupNumber__c = rowDataSFDC.MessageGroupNumber__c;
            iflog.Log__c = logstr;
            iflog.ErrorLog__c = '';

        } else {
            logstr = iflog.Log__c;
        }

        try{
            // 向SPO送信
            status =
            NFMUtil.sendToETQ(strrowDatatoETQ, NFMUtil.NFM401_ENDPOINT);
            if (status == 'OK') {
                rowDataSFDC.retry_cnt__c = 0;
            } else {
                if (rowDataSFDC.retry_cnt__c == null) rowDataSFDC.retry_cnt__c = 0;
                if (rowDataSFDC.retry_cnt__c < batch_retry_max_cnt) {
                    rowDataSFDC.retry_cnt__c++;
                    LogAutoSendSchedule.assignOneMinute();
                }
                if (rowDataSFDC.retry_cnt__c >= batch_retry_max_cnt) {
                    rowDataSFDC.ErrorLog__c = 'status:' + status +
                                          '\n错误次数已经超过自动送信设定的最大次数，请手动送信';
                }
            }
        } catch (Exception ex) {
            // TODO IOException
            // エラーが発生した場合
            System.debug(Logginglevel.ERROR, 'NFM001_' + iflog.Name + ':' + ex.getMessage());
            System.debug(Logginglevel.ERROR, 'NFM001_' + iflog.Name + ':' + ex.getStackTraceString());
            logstr += ex.getMessage();
            iflog.ErrorLog__c += ex.getMessage() + '\n';
            iflog.ErrorLog__c += ex.getStackTraceString() + '\n';

            if (rowDataSFDC.retry_cnt__c == null) rowDataSFDC.retry_cnt__c = 0;
            if (rowDataSFDC.retry_cnt__c < batch_retry_max_cnt) {
                rowDataSFDC.retry_cnt__c++;
                LogAutoSendSchedule.assignOneMinute();
            }
            if (rowDataSFDC.retry_cnt__c >= batch_retry_max_cnt) {
                rowDataSFDC.ErrorLog__c = ex.getMessage() + '\n' + ex.getStackTraceString() + '\n' + rowDataSFDC.ErrorLog__c+'错误次数已经超过自动送信设定的最大次数，请手动送信';
            }
        }

        //add by rentx 2020-11-05 start
        if (sendMails.size() > 0) {
            for(Messaging.SingleEmailMessage mc : sendMails){
                List<Messaging.SingleEmailMessage> tempsendMails = new List<Messaging.SingleEmailMessage>();
                tempsendMails.add(mc);
                Messaging.SendEmailResult[] results = messaging.sendEmail(tempsendMails);
                if (results!= null && results.size() > 0) {
                    for (Integer i = 0; i < results.size(); i++) {
                        if (results[i].success == false) {
                            logstr += '邮件 '+ mc.subject+' 发送失败';
                        }else{
                            logstr+= '邮件 '+ mc.subject+' 邮件发送成功!';
                        }
                    }
                }
            }
            // sendMails = new List<Messaging.SingleEmailMessage>();
            
        }
        //add by rentx 2020-11-05 end

        iflog.Log__c = logstr;
        System.debug('logdate2' + rowDataSFDC);

            upsert iflog;
            upsert rowDataSFDC;


    }



}