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

    // Default 发送当日符合条件的签收单
    global NFM116Batch() {
        this.setDate = Date.today();
    }
    // 发送指定日期符合条件的签收单, eg: testDate = Date.newInstance(2022, 05, 02),发送20220501完成签收的签收单
    global NFM116Batch(Date testDate) {
        this.setDate = testDate;
    }
    // 发送在指定日期30天之后的所有符合条件的签收单(isAfterDate为true的时候)
    global NFM116Batch(Date testDate, Boolean isAfterDate) {
        this.setDate = testDate;
        this.isAppoint = true;
        this.isAfterDate = isAfterDate;
    }
    // 发送指定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 and HPAcceptSAPResult__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 += ' and SAPReportDate__c = ' + setDate.addDays(-1).format().replace('/', '-') ;
        } else if(isAfterDate){
            query += ' and SAPReportDate__c > ' + setDate.addDays(-30).format().replace('/', '-') ;
        }
        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) {
        
    }
}