global class ConsumableTargetManageBatch6 implements Database.Batchable { public Date currentMonth; public Date endDate; public Date startDate; public String queryStr; public String year; public Boolean isIncrementOnly; //是否只跑增量 public String OCSMYear; global ConsumableTargetManageBatch6(Date startDate,Date endDate) { this.startDate=startDate; this.endDate=endDate; Integer year = startDate.year(); Integer FYear = year; if(startDate>=Date.newInstance(year, 4, 1)){ FYear = FYear+1; } String OCSMYear= 'FY'+FYear; this.OCSMYear=OCSMYear; queryStr = 'SELECT nowDosageNumTotal__c,nowDosageAmountTotal__c,lastDosageNumTotal__c,lastDosageAmountTotal__c,Hospital__c,OCM_Year__c,Category3__c,Category4__c FROM ConsumableTargetManage__c WHERE OCM_Year__c = :OCSMYear'; } global Database.QueryLocator start(Database.BatchableContext bc) { return Database.getQueryLocator(queryStr); } global void execute(Database.BatchableContext BC, list scope) { Integer year = startDate.year(); Integer FYear = year; if(startDate>=Date.newInstance(year, 4, 1)){ FYear = FYear+1; } String OCSMYear= 'FY'+FYear; Integer lastFYear = FYear-1; String lastOCSMYear= 'FY'+lastFYear; Set ctmKeySet=new Set(); for(ConsumableTargetManage__c ctm:scope){ ctmKeySet.add(ctm.Hospital__c+'-'+ctm.Category3__c+'-'+ctm.Category4__c+'-'+lastOCSMYear); } List lastCtmList= [SELECT nowDosageNumTotal__c,nowDosageAmountTotal__c,lastDosageNumTotal__c,lastDosageAmountTotal__c,Hospital__c,OCM_Year__c,Category3__c,Category4__c FROM ConsumableTargetManage__c WHERE ConsumableTargetManageKey__c in :ctmKeySet]; Map lastCtmMap=new Map(); for(ConsumableTargetManage__c ctm:lastCtmList){ String key=ctm.Category3__c+'-'+ctm.Category4__c+'-'+ ctm.Hospital__c; lastCtmMap.put(key, ctm); } for(ConsumableTargetManage__c ctm:scope){ String key=ctm.Category3__c+'-'+ctm.Category4__c+'-'+ ctm.Hospital__c; if(lastCtmMap.containsKey(key)){ if(lastCtmMap.get(key).nowDosageNumTotal__c!=null){ ctm.lastDosageNumTotal__c=lastCtmMap.get(key).nowDosageNumTotal__c; } if(lastCtmMap.get(key).nowDosageAmountTotal__c!=null){ ctm.lastDosageAmountTotal__c=lastCtmMap.get(key).nowDosageAmountTotal__c; } }else{ ctm.lastDosageNumTotal__c=0; ctm.lastDosageAmountTotal__c=0; } } update scope; } global void finish(Database.BatchableContext BC) { } }