global class CampaignUpsertContactToSapBatch implements Database.Batchable<sObject>, Database.AllowsCallouts {
    public String query;
    // 市场活动批量导入联系人，每晚发送给SAP
    public List<String> contactIdList;
    global CampaignUpsertContactToSapBatch() {
        this.query = query;
    }

    global CampaignUpsertContactToSapBatch(List<String> contactIdList) {
        this.contactIdList = contactIdList;
    }
    

    global Database.QueryLocator start(Database.BatchableContext bc) {

        if (contactIdList != null && contactIdList.size() > 0) {
            return Database.getQueryLocator([SELECT Id,Name,isBatch__c,CampaignUpsertContact__c FROM Contact WHERE Id IN : contactIdList]);
        } else {
            return Database.getQueryLocator([SELECT Id,Name,isBatch__c,CampaignUpsertContact__c FROM Contact WHERE CampaignUpsertContact__c = true ]);
        }

        
    }

    global void execute(Database.BatchableContext BC, List<Contact> contactList) {
        
        if (contactList != null && contactList.size() > 0){
            List<String> contactIdListToSAP = new List<String>();
            for (Contact con:contactList) {
                con.CampaignUpsertContact__c = false;
                contactIdListToSAP.add(con.Id);
            }
            SBG001TriggerHandler.calloutContactNotFuture(null,contactIdListToSAP);
            update contactList;

        }
    }

    global void finish(Database.BatchableContext BC) {

    }
}