global without sharing class TrackConsumableSaleForecastBatch3 implements Database.Batchable<sObject>{
|
public Date currentMonth;
|
public Date endDate;
|
public Date lastMonth;
|
public String queryStr;
|
|
global TrackConsumableSaleForecastBatch3(Date currentMonth,Date endDate) {
|
this.currentMonth = currentMonth;
|
this.endDate = endDate;
|
Integer year = currentMonth.year();
|
Integer month = currentMonth.month();
|
this.currentMonth = Date.newInstance(year, month, 1);
|
if(month == 1){
|
lastMonth = Date.newInstance(year-1, 12, 1);
|
}else {
|
lastMonth = Date.newInstance(year, month-1, 1);
|
}
|
queryStr = 'SELECT Hospital__c,HospitalName__c,ProductModel__c,ThisMonthDeliveryNumber__c,LastShipmentDate__c,Type__c,Key__c FROM TrackConsumableSalesForecast__c WHERE SnapshotMonth__c = :currentMonth';
|
}
|
|
global Database.QueryLocator start(Database.BatchableContext BC) {
|
return Database.getQueryLocator(queryStr);
|
}
|
|
global void execute(Database.BatchableContext BC, List<TrackConsumableSalesForecast__c> scope){
|
Map<String,TrackConsumableSalesForecast__c> tcsfMap = new Map<String,TrackConsumableSalesForecast__c>();
|
for(TrackConsumableSalesForecast__c tcsf : scope){
|
tcsf.Status__c = '完成';
|
tcsfMap.put(tcsf.Key__c,tcsf);
|
}
|
//更新过去6月的平均销量及失单预警
|
Date sixMonthAgo;
|
Date threeMonthAgo;
|
Integer year = currentMonth.year();
|
Integer month = currentMonth.month();
|
if(month <= 2){
|
threeMonthAgo = Date.newInstance(year-1, 10+month, 1);
|
}else {
|
threeMonthAgo = Date.newInstance(year, month-2, 1);
|
}
|
if(month <= 6){
|
sixMonthAgo = Date.newInstance(year-1, 6+month, 1);
|
}else {
|
sixMonthAgo = Date.newInstance(year, month-6, 1);
|
}
|
List<TrackConsumableSalesForecast__c> tcsfSixMonthAgoList = [SELECT Hospital__c,HospitalName__c,ProductModel__c,ThisMonthDeliveryNumber__c,LastShipmentDate__c,SnapshotMonth__c,Key__c FROM TrackConsumableSalesForecast__c WHERE SnapshotMonth__c >= :sixMonthAgo AND SnapshotMonth__c < :currentMonth AND Key__c IN :tcsfMap.keySet()];
|
System.debug('tcsfSixMonthAgoList:'+tcsfSixMonthAgoList);
|
for(String key : tcsfMap.keySet()){
|
System.debug('key:'+key);
|
Decimal sumThreeMonthSales = 0;
|
Decimal sumSixMonthSales = 0;
|
for(TrackConsumableSalesForecast__c tcsf : tcsfSixMonthAgoList){
|
if(tcsf.Key__c == key){
|
if(tcsf.SnapshotMonth__c >= threeMonthAgo){
|
sumThreeMonthSales += tcsf.ThisMonthDeliveryNumber__c;
|
}
|
sumSixMonthSales += tcsf.ThisMonthDeliveryNumber__c;
|
}
|
}
|
if(sumThreeMonthSales == 0 && tcsfMap.get(key).ThisMonthDeliveryNumber__c == 0){
|
tcsfMap.get(key).BillLostWarning__c = 1;
|
}
|
if(sumSixMonthSales != 0){
|
tcsfMap.get(key).LastSixMonthsAverage__c = sumSixMonthSales/6;
|
}else {
|
tcsfMap.get(key).LastSixMonthsAverage__c = 0;
|
}
|
}
|
update tcsfMap.values();
|
}
|
|
global void finish(Database.BatchableContext BC) {
|
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);
|
}
|
}
|
//add by allen 拆分部署ali生产
|
public void testMock(){
|
Integer i = 0;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
i++;
|
|
|
|
|
}
|
}
|