/************************************************************************************************** @Author: 黄千龙 @Name: CoverageTargetHPBatch @CreateDate: 13/04/2022 @Description: 覆盖率目标医院 @Version 1.0 *****************************************************************************************************/ global class CoverageTargetHPBatch implements Database.Batchable,Database.Stateful { public String query; private BatchIF_Log__c iflog; public List < String > accountIdList; global CoverageTargetHPBatch() { this.query = query; } global CoverageTargetHPBatch(List < String > accountIdList) { this.query = query; this.accountIdList = accountIdList; } global Database.QueryLocator start(Database.BatchableContext bc) { system.debug('执行start'); iflog = new BatchIF_Log__c(); iflog.Type__c = 'PushNotification'; iflog.Log__c = 'CoverageTargetHPBatch start\n'; iflog.ErrorLog__c = ''; insert iflog; String query = 'SELECT Id,Name,IF_Coverage_Target_HP__c FROM Account WHERE Is_Active__c =\'有効\' AND RecordType.DeveloperName =\'HP\' AND ( NOT Name LIKE \'%olympus%\') AND ( NOT Name LIKE \'%TEST%\')'; if (accountIdList != null && accountIdList.size() > 0) { query += ' AND Id IN :accountIdList'; } return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, list accList) { List accIds = new List(); for(Account acc:accList){ accIds.add(acc.Id); } List updateAccList = new List(); for(AggregateResult result:[SELECT count(Id),Hospital__c FROM Asset WHERE Product2.ServiceCategory__c ='软性镜' AND Hospital__c IN: accIds AND (Status = '使用中' or Status = '未使用' or Status = '不明') Group by Hospital__c having count(Id)>=5]){ Account acc = new Account(); acc.Id = String.valueOf(result.get('Hospital__c')); acc.IF_Coverage_Target_HP__c= '1'; updateAccList.add(acc); } Oly_TriggerHandler.bypass('NFM701ControllerHandler'); Oly_TriggerHandler.bypass('UpdateContractAimAmountHandler'); update updateAccList; } global void finish(Database.BatchableContext BC) { iflog.Log__c += '\nCoverageTargetHPBatch 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; if (accountIdList != null && accountIdList.size() > 0) { database.executeBatch(new EquipmentCoverageTargetBatch(accountIdList),200); }else{ database.executeBatch(new EquipmentCoverageTargetBatch(),200); } } }