//SWAG-C23CKW add by rentx 2021-04-20 查询医院下无效的fse-Gi主担当 或 fse-Sp 主担当,把他们设置为null 不然出借备品做共享的时候可能会报错
global class UpdateLeaderBeEmptyBatch implements Database.Batchable<sObject>, Database.AllowsCallouts {
    public String query;
    public String userId;
    public BatchIF_Log__c iflog;
    public boolean IsNeedExecute=false; // 2021-04-23  mzy  WLIG-BYHD79  SFDC环境batch合并调查

    global UpdateLeaderBeEmptyBatch() {
        this.query = query;
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'UpdateLeaderBeEmpty';
        iflog.Log__c = 'start -- \n';

    }

    global UpdateLeaderBeEmptyBatch(String uId) {
        this.query = query;
        this.userId = uId;
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'UpdateLeaderBeEmpty';
        iflog.Log__c = 'start -- \n';

    }

    // 2021-04-23  mzy  WLIG-BYHD79  SFDC环境batch合并调查 start
    global UpdateLeaderBeEmptyBatch(Boolean NeedExecute) {        
        this.IsNeedExecute = NeedExecute;
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'UpdateLeaderBeEmpty';
        iflog.Log__c = 'start -- \n';

    }
    //2021-04-23  mzy  WLIG-BYHD79  SFDC环境batch合并调查 end


    global Database.QueryLocator start(Database.BatchableContext bc) {
        query = 'SELECT Id,Name,FSE_SP_Main_Leader__c,FSE_GI_Main_Leader__c,FSE_SP_Main_Leader__r.IsActive,FSE_GI_Main_Leader__r.IsActive from Account ';
        query += ' WHERE Is_Active_Formula__c = \'有效\' and RecordType.DeveloperName = \'HP\' ';
        query += ' and ((FSE_GI_Main_Leader__c != null and FSE_GI_Main_Leader__r.IsActive = false) or (FSE_SP_Main_Leader__c != null and  FSE_SP_Main_Leader__r.IsActive = false))';
        if (userId != null && userId != '') {
            query += ' and (FSE_GI_Main_Leader__c = :userId or FSE_SP_Main_Leader__c = :userId) ';
        }
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, list<Account> scope) {
        List<Account> accountList = new List<Account>();
        for (Account acc : scope) {
            if (!acc.FSE_SP_Main_Leader__r.IsActive) {
                acc.FSE_SP_Main_Leader__c = null;
                iflog.Log__c += '医院 :'+acc.Name +'的FSE-SP主担当 : '+acc.FSE_SP_Main_Leader__c +'已失效\n';

            }
            if (!acc.FSE_GI_Main_Leader__r.IsActive) {
                acc.FSE_GI_Main_Leader__c = null;
                iflog.Log__c += '医院 :'+acc.Name +'的FSE-GI主担当 : '+acc.FSE_GI_Main_Leader__c +'已失效\n';

            }

            if (acc.FSE_SP_Main_Leader__r.IsActive == false || acc.FSE_GI_Main_Leader__r.IsActive == false) {
                accountList.add(acc);
            }
        }
        iflog.Log__c += '更新:'+accountList+'\n';
        update accountList;


    }

    global void finish(Database.BatchableContext BC) {
        iflog.Log__c += 'end --';
        insert iflog;
        
        //update mzy WLIG-BYHD79  SFDC环境batch合并调查  start
        if(!Test.isRunningTest() && IsNeedExecute){
            Database.executeBatch(new CreateInspectupTaskBatch('All',true));
        }
        //update mzy WLIG-BYHD79  SFDC环境batch合并调查  end
    }
}