global class AgencyHospitalLinkBatch implements Database.Batchable<sObject> {
    public String query;
    private BatchIF_Log__c iflog;

    Boolean IsNeedExecute = false; // 2021-03-10  mzy  WLIG-BYHD79  SFDC环境batch合并调查  是否符合执行条件

    global AgencyHospitalLinkBatch() {
        this.query = query;
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'AgencyHospitalLinkUpdateError';
        iflog.Log__c  = 'AgencyHospitalLinkBatch start\n';
        iflog.ErrorLog__c = '';
        insert iflog;
    }

    // 2021-03-10  mzy  WLIG-BYHD79  SFDC环境batch合并调查  start
    global AgencyHospitalLinkBatch(Boolean NeedExecute) {
        this.query = query;
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'AgencyHospitalLinkUpdateError';
        iflog.Log__c  = 'AgencyHospitalLinkBatch start\n';
        iflog.ErrorLog__c = '';
        insert iflog;
        this.IsNeedExecute = NeedExecute;
    }
    // 2021-03-10  mzy  WLIG-BYHD79  SFDC环境batch合并调查  end

    global Database.QueryLocator start(Database.BatchableContext bc) {
        //sql语句
        query = 'select Id, Name,Hospital__c,Hospital_ID__c,Agency__c, Hospital_Name_readonly__c,';
        query += 'Hospital_Recordtype__c from Agency_Hospital_Link__c where isSame__c =\'0\' ';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, list<Sobject> scope) {
        List<Agency_Hospital_Link__c> agencyhlList = new List<Agency_Hospital_Link__c>();
        List<Agency_Hospital_Link__c> updAgencyhlList = new List<Agency_Hospital_Link__c>();
        agencyhlList = scope;
        for (Agency_Hospital_Link__c agencyhl : agencyhlList) {
                agencyhl.Name = agencyhl.Hospital_Name_readonly__c;
                updAgencyhlList.add(agencyhl);
        }
        /*if (updAgencyhlList.size() > 0) {
            update updAgencyhlList;
        }*/

        if(updAgencyhlList.size() > 0){
            Database.SaveResult[] lsr = Database.update(updAgencyhlList, false);
            for (Integer tIdx = 0; tIdx < lsr.size(); tIdx++) {
                Database.SaveResult sr = lsr[tIdx];
                System.debug('sr.isSuccess:' + sr.isSuccess());
                if (!sr.isSuccess()) {
                    Database.Error emsg = sr.getErrors()[0];
                    iflog.ErrorLog__c += 'ERROR ' + updAgencyhlList[tIdx].Id + ' Agency_Hospital_Link__c:' + emsg + '\n';
                }
            }
            //update updAgencyhlList;
        }
    }

    global void finish(Database.BatchableContext BC) {
        iflog.Log__c += '\nAgencyHospitalLinkBatch end';
        String tmp = iflog.ErrorLog__c;
        if (tmp.length() > 65000) {
            tmp = tmp.substring(0, 65000);
            tmp += ' ...have more lines...';
            iflog.ErrorLog__c = tmp;
        }
        update iflog;

        //2021-03-10  mzy  WLIG-BYHD79  SFDC环境batch合并调查  start
        if(!Test.isRunningTest() &&IsNeedExecute==true){
            //batch里调用下一个batch时，希望跟原有的Schedule里面传的条数保持一致
            Id execBTId = Database.executebatch(new Sfdc2SapDealersContractBatch(true),200);
        }
        //2021-03-10  mzy  WLIG-BYHD79  SFDC环境batch合并调查 end
    }
}