global without sharing class TrackConsumableSaleForecastBatch3 implements Database.Batchable<sObject>{
    public Date currentMonth;
    public Date endDate;
    public Date lastMonth;
    public String queryStr;
    public String recordIdET;
    public String recordIdENG;
    //20240308 lwt 出库价，销售状态修改 start
    public Boolean hisFlag=false;
    public Boolean onlyETENG=false;
    //20240308 lwt 出库价，销售状态修改 end
    public String recordIdC;
    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);
        }

        //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 耗材销量分析维度修改 start
    global TrackConsumableSaleForecastBatch3(Date currentMonth,Date endDate,Boolean hisFlag) {
        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);
        }
        this.hisFlag=hisFlag;

        queryStr = 'SELECT ThisMonthDelivery_Price_IMS__c,Hospital__c,HospitalName__c,ProductModel__c,ThisMonthDeliveryNumber__c,LastShipmentDate__c,Type__c,Key__c FROM TrackConsumableSalesForecast__c WHERE SnapshotMonth__c = :currentMonth ';
        if(this.hisFlag){
            String recordIdC = Schema.SObjectType.TrackConsumableSalesForecast__c.getRecordTypeInfosByDeveloperName().get('ConsumableCategory3').getRecordTypeId();
            this.recordIdC=recordIdC;
            queryStr+= ' AND RecordTypeId=:recordIdC';
        }
    }
    //20240131 lwt 耗材销量分析维度修改 end
    //20240308 lwt 出库价，销售状态修改 start
    global TrackConsumableSaleForecastBatch3(Date currentMonth,Date endDate,Boolean hisFlag,Boolean onlyETENG) {
        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);
        }
        this.hisFlag=hisFlag;
		this.onlyETENG=onlyETENG;
        queryStr = 'SELECT ThisMonthDelivery_Price_IMS__c,Hospital__c,HospitalName__c,ProductModel__c,ThisMonthDeliveryNumber__c,LastShipmentDate__c,Type__c,Key__c FROM TrackConsumableSalesForecast__c WHERE SnapshotMonth__c = :currentMonth ';
        if(this.hisFlag&&this.onlyETENG){
            String recordIdC = Schema.SObjectType.TrackConsumableSalesForecast__c.getRecordTypeInfosByDeveloperName().get('ConsumableCategory3').getRecordTypeId();
            this.recordIdC=recordIdC;
            queryStr+= ' AND RecordTypeId!=:recordIdC';
        }
    }
    //20240308 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){
            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);
        }
        //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()];
        System.debug('tcsfSixMonthAgoList:'+tcsfSixMonthAgoList);
        for(String key : tcsfMap.keySet()){
            System.debug('key:'+key);
            Decimal sumThreeMonthSales = 0;
            Decimal sumSixMonthSales = 0;
            Decimal sumSixMonthSalesIMS = 0;//20240131 lwt 耗材销量分析维度修改
            for(TrackConsumableSalesForecast__c tcsf : tcsfSixMonthAgoList){
                if(tcsf.Key__c == key){
                    if(tcsf.SnapshotMonth__c >= threeMonthAgo){
                        sumThreeMonthSales += tcsf.ThisMonthDeliveryNumber__c;
                    }
                    sumSixMonthSales += tcsf.ThisMonthDeliveryNumber__c;
                    //20240131 lwt 耗材销量分析维度修改 start
                    if(tcsf.ThisMonthDelivery_Price_IMS__c!=null){
                        sumSixMonthSalesIMS += tcsf.ThisMonthDelivery_Price_IMS__c;
                    }
                    //20240131 lwt 耗材销量分析维度修改 end
                }
            }
            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;
            }
            //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) {
        //20240308 lwt 出库价，销售状态修改 start
        ////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);
        //    }
        //}
        
        if(this.onlyETENG){
            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,this.onlyETENG), 200);
            }
       
        }else if(this.hisFLag){
            Id batjobId = Database.executeBatch(new ConsumableTargetManageBatch1(currentMonth,endDate,this.hisFLag), 200);
        }else{
            Id batjobId = Database.executeBatch(new ConsumableTargetManageBatch1(currentMonth,endDate,false), 200);
        }
        //20240308 lwt 出库价，销售状态修改 end
        
    }
     
}