global class NFM623RestBatch implements Database.Batchable<sObject> {
    public String query;

    global NFM623RestBatch() {
        //this.query = query;
    }

    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator([select Id,Contact_ID__c,Campaign__c,Campaign__r.Num__c,ViewContactId__c from CampaignMember__c where ViewContactId__c != null and Contact_ID__c = null]);
    }

    global void execute(Database.BatchableContext BC, list<Sobject> scope) {
        List<CampaignMember__c> campaignMemberList = new List<CampaignMember__c>();
        List <String> sobjectList = new List <String> ();
        for (Sobject sobj: scope) {
            sobjectList.add(sobj.Id);
        }
        List<CampaignMember__c> capMemList = [select Id,Contact_ID__c,Campaign__c,Campaign__r.Num__c,ViewContactId__c from CampaignMember__c where ID in:sobjectList];
        Map<String, String> capMemMap = new Map<String, String>(); 
        //统一用户ID存入List,参会人员记录ID存入MAP
        List<String> contactIdList = new List<String>();
        for (CampaignMember__c capMemInfo : capMemList) {
            contactIdList.add(capMemInfo.ViewContactId__c);
            capMemMap.put(capMemInfo.ViewContactId__c,capMemInfo.Id);
        }
        List<Contact> contactList = [select Id,Name,UnifiedI_Contact_ID__c,AccountId,Account.Name from Contact where UnifiedI_Contact_ID__c in :contactIdList];
        if (contactList.size() > 0) {
            for (Contact contactInfo : contactList) {
                CampaignMember__c camMemberInfo = new CampaignMember__c();
                camMemberInfo.Id = capMemMap.get(contactInfo.UnifiedI_Contact_ID__c);
                camMemberInfo.Contact_ID__c = contactInfo.ID;                   //参会人员ID
                camMemberInfo.Department_ID__c = contactInfo.AccountId;         //医院科室
                camMemberInfo.Department__c = contactInfo.Account.Name;         //医院科室（文本）
                camMemberInfo.Contact__c = contactInfo.Name;                    //客户人员姓名
                campaignMemberList.add(camMemberInfo);
            }
            
        }
        if (campaignMemberList.size() > 0) {
            update campaignMemberList;
        }
    }

    global void finish(Database.BatchableContext BC) {

    }
}