global class NFM116Batch implements Database.Batchable<sObject>, Database.AllowsCallouts {
    // 设定发送指定签收单的ID
    public String setId;
    // 设定执行日期
    public String setDate;
    public String endDate;
    // 是否发送指定的签收单
    public Boolean isAppoint = false;
    // 是否发送指定日期的签收单
    public Boolean isSpecifyDate = false;
    // 设定发送的签收单的IdList
    public List<String> setIdList;

    // Default 发送当日符合条件的签收单
    global NFM116Batch() {
        // String yesterday = Date.today().addDays(-1).format().replace('/', '-');
        // String today = Date.today().format().replace('/', '-');
        // this.setDate = yesterday + 'T00:00:00.000+0000';
        // this.endDate = today + 'T00:00:00.000+0000';
        this.setDate = 'and LastModifiedDate = LAST_N_DAYS:1 and LastModifiedDate != LAST_N_DAYS:0 ';
    }
    // 发送指定日期符合条件的签收单, eg: testDate = Date.newInstance(2022, 05, 02),发送20220501完成签收的签收单
    global NFM116Batch(Date testDate) {
        isAppoint = true;
        isSpecifyDate = true;
        String yesterday = testDate.addDays(-1).format().replace('/', '-');
        String pointday = testDate.format().replace('/', '-');
        this.setDate = yesterday + 'T00:00:00.000+0000';
        this.endDate = pointday + 'T00:00:00.000+0000';
    }
    // 发送在指定日期30天之前的所有符合条件的签收单(isAfterDate为true的时候)
    global NFM116Batch(Date testDate, Boolean isAfterDate) {
        if(isAfterDate){
            isAppoint = true;
            isSpecifyDate = true;
            String beforepointday = testDate.addDays(-30).format().replace('/', '-');
            String pointday = testDate.format().replace('/', '-');
            this.setDate = beforepointday + 'T00:00:00.000+0000';
            this.endDate = pointday + 'T00:00:00.000+0000';
        }
    }
    // 发送指定Id的签收单
    global NFM116Batch(String setId) {
        this.setId = setId;
        this.isAppoint = true;
    }
    // 发送指定IdList的签收单
    global NFM116Batch(List<String> setIdList) {
        this.setIdList = setIdList;
        this.isAppoint = true;
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {
        String query = 'select Id from eSignForm__c where (agencyAcceptSAPResult__c != null or HPAcceptSAPResult__c != null) and SAPReportDate__c != null ';
        if(String.isNotBlank(setId)){
            query += ' and Id =: setId ';
        } else if (setIdList != null && setIdList.size() > 0) {
            query += ' and Id IN: setIdList ';
        } else if(!isAppoint){
            query += setDate;
        } else if(isSpecifyDate){
            query += ' and LastModifiedDate > ' + setDate;
            query += ' and LastModifiedDate < ' + endDate;
        }
        System.debug('thhquery:' + query);
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<eSignForm__c> eSignFormList) {
        List<String> eSignFormIdList = new List<String>();
        if(eSignFormList.size() > 0){
            for(eSignForm__c es : eSignFormList){
                eSignFormIdList.add(es.Id);
            }
        }
        System.debug('eSignFormIdList1:' + eSignFormIdList);
        if(eSignFormIdList.size() > 0){
            NFM116Controller.executeNotFuture(null, eSignFormIdList);
        }
    }

    global void finish(Database.BatchableContext BC) {
        
    }
}