/**********************************************************************
 * 
 *
 * @url: /services/apexrest/NFM777Rest
 * @data:
 *  {
        
    }
*************************************************************************/
@RestResource(urlMapping='/NFM777/*')
global with sharing class NFM777Rest {

    global class SampleDNsRest {
        public SampleDNs sampleDNs;
    }

    global class SampleDNs {
        public NFMUtil.Monitoring Monitoring;
        public SampleDN[] SampleDN;
    }

    global class SampleDN {
        public String SampleName;
        public String Specs;
        public String Total;
    }
    
    @HttpPost
    global static void execute() {
        // 取得接口传输内容
        String strData = RestContext.request.requestBody.toString();
        SampleDNs sams = (SampleDNs) JSON.deserializeStrict(strData, SampleDNs.class);
        if (sams == null) {
            return;
        }

        NFMUtil.Monitoring Monitoring = sams.Monitoring;
        if (Monitoring == null) {
            return;
        }

        BatchIF_Log__c rowData = NFMUtil.saveRowData(Monitoring, 'NFM777', sams.SampleDN);
        if (String.isBlank(rowData.Log__c) == false) {
            executefuture(rowData.Id);
        }
        // JSONを戻す
        RestResponse res = RestContext.response;
        res.addHeader('Content-Type', 'application/json');
        res.statusCode = 200;
        String jsonResponse = '{"status": "Success", "Message":""}';
        res.responseBody = blob.valueOf(jsonResponse);
        return;
    }

    @future
    global static void executefuture(String rowData_Id) {
        main(rowData_Id);
    }
    
    global static void 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, 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,NFM624createHP__c,NFM624createcontact__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 = 'NFM777';
        iflog.MessageGroupNumber__c = rowData.MessageGroupNumber__c;
        iflog.Log__c = logstr;
        iflog.ErrorLog__c = '';
        insert iflog;

        String rowDataStr = NFMUtil.getRowDataStr(rowData);
        List<SampleDN> SampleDNList = (List<SampleDN>) JSON.deserialize(rowDataStr, List<SampleDN>.class);
        if (SampleDNList == null || SampleDNList.size() == 0) {
            return;
        }
        
        Savepoint sp = Database.setSavepoint();

        //增加判空


        try {
            Sample_order_list_DN__c Sold = new Sample_order_list_DN__c();
            List<Sample_stock_log_sheet__c> SampleLogList = new List<Sample_stock_log_sheet__c>();

            for(SampleDN sam : SampleDNList){
                Sold.Sample_name__c = sam.SampleName;
            }
        }catch(Exception ex){
            // エラーが発生した場合
            Database.rollback(sp);
            System.debug(Logginglevel.ERROR, 'NFM777_' +'_' + rowData.MessageGroupNumber__c + ':' + ex.getMessage());
            System.debug(Logginglevel.ERROR, 'NFM777_' +'_' + rowData.MessageGroupNumber__c + ':' + ex.getStackTraceString());
            logstr += '\n' + ex.getMessage();
            iflog.ErrorLog__c = ex.getMessage() + '\n' + ex.getStackTraceString() + '\n' + iflog.ErrorLog__c;
            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 + '错误次数已经超过自动收信设定的最大次数，请手动收信';
            }
        }
        update rowData;
        iflog.Log__c = logstr;
        if (iflog.Log__c.length() > 131072) {
            iflog.Log__c = iflog.Log__c.subString(0, 131065) + ' ...';
        }
        if (iflog.ErrorLog__c.length() > 32768) {
            iflog.ErrorLog__c = iflog.ErrorLog__c.subString(0, 32760) + ' ...';
        }
        update iflog;
    }

}