//耗材销售状态 //每月跑 global class ConsumableTargetManageBatch1 implements Database.Batchable { public Date currentMonth; public Date tcsEndDate; public Date tcsStartDate; public Date endDate; public Date startDate; public String queryStr; public Boolean hisFlag; global ConsumableTargetManageBatch1(Date startDate,Date endDate,Boolean hisFlag) { //当年 this.startDate=startDate; this.endDate=endDate; this.hisFlag=hisFlag; this.tcsStartDate=startDate; this.tcsEndDate=endDate; this.currentMonth=startDate; Integer year = startDate.year(); Integer FYear = year; if(startDate>=Date.newInstance(year, 4, 1)){ FYear = year+1; } queryStr = 'SELECT '; queryStr += ' FY_SaleStatus_Formula_'+FYear+'__c,'; queryStr += ' BillLostWarning__c,Thisyear_SaleStatus_formula__c,Consumable_Category1__c,Hospital__c,Consumable_Category2__c,Consumable_Category3__c,SnapshotMonth__c FROM TrackConsumableSalesForecast__c WHERE Consumable_Category3__c!= \'\' AND recordType.Name=\'目标耗材\' And SnapshotMonth__c = :tcsStartDate '; } 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, 5, 1)){ FYear = year+1; } String OCSMYear= 'FY'+FYear; Set ctmKeySet=new Set(); Map> tcsMap = new Map>(); for(TrackConsumableSalesForecast__c tcs:scope){ String key = tcs.Consumable_Category2__c+'-'+tcs.Consumable_Category3__c+'-'+tcs.Hospital__c+'-'+tcs.Consumable_Category1__c; ctmKeySet.add(tcs.Hospital__c+'-'+tcs.Consumable_Category2__c+'-'+tcs.Consumable_Category3__c+'-'+OCSMYear); if(tcsMap.containsKey(key)){ tcsMap.get(key).add(tcs); }else{ List tcsList = new List(); tcsList.add(tcs); tcsMap.put(key,tcsList); } } //List ctmList= [SELECT ConsumableTargetManageKey__c,Hospital__r.Name,productType__c,Hospital__c,OCM_Year__c,Category3__c,Category4__c FROM ConsumableTargetManage__c WHERE OCM_Year__c = :OCSMYear AND ConsumableTargetManageKey__c in :ctmKeySet]; String sql='SELECT ConsumableTargetManageKey__c,Hospital__r.Name,productType__c,Hospital__c,OCM_Year__c,Category3__c,Category4__c FROM ConsumableTargetManage__c WHERE OCM_Year__c = :OCSMYear AND ConsumableTargetManageKey__c in :ctmKeySet'; ApexPages.StandardSetController con= new ApexPages.StandardSetController(Database.getQueryLocator(sql)); List ctmList=(List) con.getRecords(); for(ConsumableTargetManage__c ctm:ctmList){ String key=ctm.Category3__c+'-'+ctm.Category4__c+'-'+ctm.Hospital__c+'-'+ctm.productType__c; if(tcsMap.containsKey(key)){ List tcsList=tcsMap.get(key); String statu=''; Date lastDate=Date.newInstance(FYear-1, 4, 1); for(TrackConsumableSalesForecast__c tcs:tcsList){ if(tcs.SnapshotMonth__c>=lastDate){ lastDate=tcs.SnapshotMonth__c; ctm.ConsumableSalesState__c=(String)tcs.get('FY_SaleStatus_Formula_'+FYear+'__c'); ctm.BillLostWarning__c=(tcs.BillLostWarning__c==1); } } } } if(ctmList.size()>0){ update ctmList; } } global void finish(Database.BatchableContext BC) { //if(this.hisFlag){ // Id batjobId = Database.executeBatch(new TrackConsumableSaleForecastBatch3(currentMonth,endDate,this.hisFlag), 200); //}else{ // Id batjobId = Database.executeBatch(new TrackConsumableSaleForecastBatch3(currentMonth,endDate), 200); //} //20240131 lwt 耗材销量分析维度修改 start if(this.hisFlag){ Database.executeBatch(new TrackConsumableSalesDailyBatch(), 100); Integer year = currentMonth.year(); Integer month = currentMonth.month(); Date nextMonth; if(month == 12){ nextMonth = Date.newInstance(year+1, 1, 1); }else { nextMonth = Date.newInstance(year, month+1, 1); } if(currentMonth < endDate){ Id batjobId = Database.executeBatch(new TrackConsumableSaleForecastBatch1(nextMonth,endDate,this.hisFlag), 200); } //20240131 lwt 耗材销量分析维度修改 end }else{ Database.executeBatch(new TrackConsumableSalesDailyBatch(), 100); Integer year = currentMonth.year(); Integer month = currentMonth.month(); Date nextMonth; if(month == 12){ nextMonth = Date.newInstance(year+1, 1, 1); }else { nextMonth = Date.newInstance(year, month+1, 1); } if(currentMonth < endDate){ Id batjobId = Database.executeBatch(new TrackConsumableSaleForecastBatch1(nextMonth,endDate), 200); } } } }