global without sharing class TrackConsumableSaleForecastASPBatch4 implements Database.Batchable<sObject>{
    public Date currentMonth;
    public Date endDate;
    public Date lastMonth;
    public String queryStr;
    public String recordIdET;
    public String recordIdENG;
    public Boolean hisFlag;
    public String recordIdC;
    global TrackConsumableSaleForecastASPBatch4(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);
        }

        //20240131 lwt 耗材销量分析维度修改 start
        this.hisFlag=false;
        queryStr = 'SELECT Hospital__c,HospitalName__c,ProductModel__c,ThisMonthDeliveryNumber__c,LastShipmentDate__c,Type__c,Key__c FROM TrackConsumableSalesForecast__c WHERE SnapshotMonth__c = :currentMonth ';
        //20240131 lwt 耗材销量分析维度修改 end
    }
    //20240131 lwt 耗材销量分析维度修改 end
    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){
            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);
        }
        //20240131 lwt 耗材销量分析维度修改
        List<TrackConsumableSalesForecast__c> tcsfSixMonthAgoList = [SELECT ThisMonthDelivery_Price_IMS__c,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()];
       
        for(String key : tcsfMap.keySet()){
            Decimal sumSixMonthSalesIMS = 0;//20240131 lwt 耗材销量分析维度修改
            for(TrackConsumableSalesForecast__c tcsf : tcsfSixMonthAgoList){
                if(tcsf.Key__c == key){
                    //20240131 lwt 耗材销量分析维度修改 start
                    if(tcsf.ThisMonthDelivery_Price_IMS__c!=null){
                        sumSixMonthSalesIMS += tcsf.ThisMonthDelivery_Price_IMS__c;
                    }
                    //20240131 lwt 耗材销量分析维度修改 end
                }
            }
            //20240131 lwt 耗材销量分析维度修改 start
            if(sumSixMonthSalesIMS != 0){
                tcsfMap.get(key).LastSixMonthsAverage_Price_IMS__c = sumSixMonthSalesIMS;
            }else {
                tcsfMap.get(key).LastSixMonthsAverage_Price_IMS__c = 0;
            }
            //20240131 lwt 耗材销量分析维度修改 start
        }
        update tcsfMap.values();
    }

    global void finish(Database.BatchableContext BC) {
        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 TrackConsumableSaleForecastASPBatch1(nextMonth,endDate), 200);
        }
        
        
    }
     
}