//拜访次数 //获取日报的拜访次数 global class ConsumableTargetManageBatch3 implements Database.Batchable { public Date currentMonth; public Date endDate; public Date startDate; public String queryStr; public String year; public Boolean isIncrementOnly; //是否只跑增量 global ConsumableTargetManageBatch3(Date startDate,Date endDate,Boolean isIncrementOnly) { //当年 this.startDate=startDate; this.endDate=endDate; this.isIncrementOnly=isIncrementOnly; queryStr = 'SELECT id,Hospital_ID__c,etapp_third_category__c,etapp_third_category2__c,etapp_third_category3__c,etapp_forth_category__c,etapp_forth_category2__c,etapp_forth_category3__c,ActivityDate__c' +' FROM Event__c ' +' WHERE (Status__c=\'申請中\' OR Status__c=\'承認\') AND Hospital_ID__c!=null ' +' AND ((etapp_third_category__c !=null AND etapp_forth_category__c!=null) ' +' OR (etapp_third_category2__c !=null AND etapp_forth_category2__c!=null) ' +' OR (etapp_third_category3__c !=null AND etapp_forth_category3__c!=null) ) ' +' AND ActivityDate__c>=:startDate AND ActivityDate__c<=:endDate'; } global Database.QueryLocator start(Database.BatchableContext bc) { //如果跑全量数据,需要重置 //数据超过一万条会报错 if(!isIncrementOnly){ // Integer year = startDate.year(); // Integer FYear = year; // if(startDate>=Date.newInstance(year, 4, 1)){ // FYear = FYear+1; // } // String OCSMYear= 'FY'+FYear; // List ctmList= [SELECT id FROM ConsumableTargetManage__c WHERE OCM_Year__c = :OCSMYear ]; // for(ConsumableTargetManage__c ctm:ctmList){ // ctm.SpecialistVisitsNum1__c=0; // ctm.SpecialistVisitsNum2__c=0; // ctm.SpecialistVisitsNum3__c=0; // ctm.SpecialistVisitsNum4__c=0; // ctm.SpecialistVisitsNum5__c=0; // ctm.SpecialistVisitsNum6__c=0; // ctm.SpecialistVisitsNum7__c=0; // ctm.SpecialistVisitsNum8__c=0; // ctm.SpecialistVisitsNum9__c=0; // ctm.SpecialistVisitsNum10__c=0; // ctm.SpecialistVisitsNum11__c=0; // ctm.SpecialistVisitsNum12__c=0; // } // update ctmList; } return Database.getQueryLocator(queryStr); } global void execute(Database.BatchableContext BC, list scope) { System.debug('获取到报告一览:'); System.debug(scope); Integer year = startDate.year(); Integer FYear = year; if(startDate>=Date.newInstance(year, 4, 1)){ FYear = FYear+1; } String OCSMYear= 'FY'+FYear; Set ctmKeySet=new Set(); Map> eventMap = new Map>(); Map hpIdMap=new Map(); List hpIdList=new List(); for(Event__c event:scope){ hpIdList.add(event.Hospital_ID__c); } List accList=[SELECT id from Account where id IN :hpIdList]; for(Account acc:accList){ String id=acc.Id; hpIdMap.put(id.substring(0,15),id); } for(Event__c event:scope){ String key1=event.etapp_third_category__c+'-'+event.etapp_forth_category__c+'-'+event.Hospital_ID__c; String key2=event.etapp_third_category2__c+'-'+event.etapp_forth_category2__c+'-'+event.Hospital_ID__c; String key3=event.etapp_third_category3__c+'-'+event.etapp_forth_category3__c+'-'+event.Hospital_ID__c; String eventHpId=hpIdMap.get(event.Hospital_ID__c); ctmKeySet.add(eventHpId+'-'+event.etapp_third_category__c+'-'+event.etapp_forth_category__c+'-'+OCSMYear); ctmKeySet.add(eventHpId+'-'+event.etapp_third_category2__c+'-'+event.etapp_forth_category2__c+'-'+OCSMYear); ctmKeySet.add(eventHpId+'-'+event.etapp_third_category3__c+'-'+event.etapp_forth_category3__c+'-'+OCSMYear); if(eventMap.containsKey(key1)){ eventMap.get(key1).add(event); }else{ List eventList = new List(); eventList.add(event); eventMap.put(key1,eventList); } if(eventMap.containsKey(key2)){ eventMap.get(key2).add(event); }else{ List eventList = new List(); eventList.add(event); eventMap.put(key2,eventList); } if(eventMap.containsKey(key3)){ eventMap.get(key3).add(event); }else{ List eventList = new List(); eventList.add(event); eventMap.put(key3,eventList); } } List ctmList= [SELECT SpecialistVisitsNum1__c,SpecialistVisitsNum2__c,SpecialistVisitsNum3__c, SpecialistVisitsNum4__c,SpecialistVisitsNum5__c,SpecialistVisitsNum6__c,SpecialistVisitsNum7__c,SpecialistVisitsNum8__c, SpecialistVisitsNum9__c,SpecialistVisitsNum10__c,SpecialistVisitsNum11__c,SpecialistVisitsNum12__c, Hospital__c,OCM_Year__c,Category3__c,Category4__c FROM ConsumableTargetManage__c WHERE OCM_Year__c = :OCSMYear AND ConsumableTargetManageKey__c in :ctmKeySet]; for(ConsumableTargetManage__c ctm:ctmList){ String hpId=''; if(ctm.Hospital__c!=null){ hpId=ctm.Hospital__c; hpId=hpId.substring(0,15); } String key=ctm.Category3__c+'-'+ctm.Category4__c+'-'+ hpId; if(ctm.SpecialistVisitsNum1__c==null){ ctm.SpecialistVisitsNum1__c=0; } if(ctm.SpecialistVisitsNum2__c==null){ ctm.SpecialistVisitsNum2__c=0; } if(ctm.SpecialistVisitsNum3__c==null){ ctm.SpecialistVisitsNum3__c=0; } if(ctm.SpecialistVisitsNum4__c==null){ ctm.SpecialistVisitsNum4__c=0; } if(ctm.SpecialistVisitsNum5__c==null){ ctm.SpecialistVisitsNum5__c=0; } if(ctm.SpecialistVisitsNum6__c==null){ ctm.SpecialistVisitsNum6__c=0; } if(ctm.SpecialistVisitsNum7__c==null){ ctm.SpecialistVisitsNum7__c=0; } if(ctm.SpecialistVisitsNum8__c==null){ ctm.SpecialistVisitsNum8__c=0; } if(ctm.SpecialistVisitsNum9__c==null){ ctm.SpecialistVisitsNum9__c=0; } if(ctm.SpecialistVisitsNum10__c==null){ ctm.SpecialistVisitsNum10__c=0; } if(ctm.SpecialistVisitsNum11__c==null){ ctm.SpecialistVisitsNum11__c=0; } if(ctm.SpecialistVisitsNum12__c==null){ ctm.SpecialistVisitsNum12__c=0; } if(eventMap.containsKey(key)){ List eventList=eventMap.get(key); System.debug('修改前:'+ctm); System.debug('event:'+eventList); for(Event__c event:eventList){ if(event.ActivityDate__c >= Date.newInstance(FYear, 1, 1) && event.ActivityDate__c < Date.newInstance(FYear, 2, 1)){ ctm.SpecialistVisitsNum1__c++; }else if(event.ActivityDate__c >= Date.newInstance(FYear, 2, 1) && event.ActivityDate__c < Date.newInstance(FYear, 3, 1)){ ctm.SpecialistVisitsNum2__c++; }else if(event.ActivityDate__c >= Date.newInstance(FYear, 3, 1) && event.ActivityDate__c < Date.newInstance(FYear, 4, 1)){ ctm.SpecialistVisitsNum3__c++; }else if(event.ActivityDate__c >= Date.newInstance(FYear-1, 4, 1) && event.ActivityDate__c < Date.newInstance(FYear-1, 5, 1)){ ctm.SpecialistVisitsNum4__c++; }else if(event.ActivityDate__c >= Date.newInstance(FYear-1, 5, 1) && event.ActivityDate__c < Date.newInstance(FYear-1, 6, 1)){ ctm.SpecialistVisitsNum5__c++; }else if(event.ActivityDate__c >= Date.newInstance(FYear-1, 6, 1) && event.ActivityDate__c < Date.newInstance(FYear-1, 7, 1)){ ctm.SpecialistVisitsNum6__c++; }else if(event.ActivityDate__c >= Date.newInstance(FYear-1, 7, 1) && event.ActivityDate__c < Date.newInstance(FYear-1, 8, 1)){ ctm.SpecialistVisitsNum7__c++; }else if(event.ActivityDate__c >= Date.newInstance(FYear-1, 8, 1) && event.ActivityDate__c < Date.newInstance(FYear-1, 9, 1)){ ctm.SpecialistVisitsNum8__c++; }else if(event.ActivityDate__c >= Date.newInstance(FYear-1, 9, 1) && event.ActivityDate__c < Date.newInstance(FYear-1, 10, 1)){ ctm.SpecialistVisitsNum9__c++; }else if(event.ActivityDate__c >= Date.newInstance(FYear-1, 10, 1) && event.ActivityDate__c < Date.newInstance(FYear-1, 11, 1)){ ctm.SpecialistVisitsNum10__c++; }else if(event.ActivityDate__c >= Date.newInstance(FYear-1, 11, 1) && event.ActivityDate__c < Date.newInstance(FYear-1, 12, 1)){ ctm.SpecialistVisitsNum11__c++; }else if(event.ActivityDate__c >= Date.newInstance(FYear-1, 12, 1) && event.ActivityDate__c < Date.newInstance(FYear, 1, 1)){ ctm.SpecialistVisitsNum12__c++; } } System.debug('修改后:'+ctm); } } if(ctmList.size()>0){ update ctmList; } } global void finish(Database.BatchableContext BC) { Id batjobId = Database.executeBatch(new ConsumableTargetManageBatch4(startDate,endDate), 200); } }