global without sharing class TrackConsumableSaleForecastBatch1 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 Integer fyear;
    global TrackConsumableSaleForecastBatch1(Date currentMonth,Date endDate) {
        this.currentMonth = currentMonth;
        this.endDate = endDate;
        //确保开始日期和结束日期为每月的1号
        this.endDate = Date.newInstance(endDate.year(), endDate.month(), 1);
        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);
        }
        //20231225  DB202312609378【IMS/TMS】ASP价格上传--平均价格，平均月销量 lwt
        //20240222 耗材销量分析销售状态修改 start
        this.hisFLag=false;
        this.onlyETENG=false;
        this.fyear=year;
        if(currentMonth.month()>=4){
            this.fyear=year+1;
        }
        queryStr = 'SELECT ';
        for(Integer i=2021;i<=this.fyear;i++){
            queryStr += 'FY_SaleStatus_Batch_'+i+'__c,';
        }
        //20240222 耗材销量分析销售状态修改 end
        queryStr += ' IMS_Flag__c,ThisMonthDelivery_Price_IMS__c,LastMonthDelivery_Price_IMS__c,Sale_OrderName_Set__c,Least_Unit__c,Lastyear_SaleStatus_text__c,Thisyear_SaleStatus_text__c,RecordTypeId,ThisYearDeliveryNumber__c,Consumable_Category3__c,ASP_Price__c,Hospital__c,HospitalName__c,ProductModel__c,ThisMonthDeliveryNumber__c,LastShipmentDate__c,Type__c,Consumable_Category1__c,Consumable_Category2__c,OwnerId FROM TrackConsumableSalesForecast__c WHERE SnapshotMonth__c = :lastMonth';
    }

    //20240131 lwt 耗材销量分析维度修改 start
    global TrackConsumableSaleForecastBatch1(Date currentMonth,Date endDate,Boolean hisFLag) {
        this.currentMonth = currentMonth;
        this.endDate = endDate;
        //确保开始日期和结束日期为每月的1号
        this.endDate = Date.newInstance(endDate.year(), endDate.month(), 1);
        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);
        }
        //20231225  DB202312609378【IMS/TMS】ASP价格上传--平均价格，平均月销量 lwt
        //20240222 耗材销量分析销售状态修改 start
        this.hisFLag=hisFLag;
        //20240308 lwt 出库价，销售状态修改 start
        this.onlyETENG=false;
        this.fyear=year;
        if(currentMonth.month()>=4){
            this.fyear=year+1;
        }
        queryStr = 'SELECT ';
        for(Integer i=2021;i<=this.fyear;i++){
            queryStr += 'FY_SaleStatus_Batch_'+i+'__c,';
        }
        
         //20240308 lwt 出库价，销售状态修改 end
        //20240222 耗材销量分析销售状态修改 end
        queryStr += ' IMS_Flag__c,ThisMonthDelivery_Price_IMS__c,LastMonthDelivery_Price_IMS__c,Sale_OrderName_Set__c,Least_Unit__c,Lastyear_SaleStatus_text__c,Thisyear_SaleStatus_text__c,RecordTypeId,ThisYearDeliveryNumber__c,Consumable_Category3__c,ASP_Price__c,Hospital__c,HospitalName__c,ProductModel__c,ThisMonthDeliveryNumber__c,LastShipmentDate__c,Type__c,Consumable_Category1__c,Consumable_Category2__c,OwnerId FROM TrackConsumableSalesForecast__c WHERE SnapshotMonth__c = :lastMonth';
        if(hisFLag){
            String recordIdET = Schema.SObjectType.TrackConsumableSalesForecast__c.getRecordTypeInfosByDeveloperName().get('ET').getRecordTypeId();
            String recordIdENG = Schema.SObjectType.TrackConsumableSalesForecast__c.getRecordTypeInfosByDeveloperName().get('ENG').getRecordTypeId();
            this.recordIdENG=recordIdENG;
            this.recordIdET=recordIdET;
            queryStr += '  AND (RecordTypeId != :recordIdET AND RecordTypeId != :recordIdENG)';
        }
    }
    //20240131 lwt 耗材销量分析维度修改 end

    
    //20240308 lwt 出库价，销售状态修改 start
    global TrackConsumableSaleForecastBatch1(Date currentMonth,Date endDate,Boolean hisFLag,Boolean onlyETENG) {
        this.currentMonth = currentMonth;
        this.endDate = endDate;
        //确保开始日期和结束日期为每月的1号
        this.endDate = Date.newInstance(endDate.year(), endDate.month(), 1);
        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);
        }
        //20231225  DB202312609378【IMS/TMS】ASP价格上传--平均价格，平均月销量 lwt
        //20240222 耗材销量分析销售状态修改 start
        this.hisFLag=hisFLag;
        this.onlyETENG=onlyETENG;
        this.fyear=year;
        if(currentMonth.month()>=4){
            this.fyear=year+1;
        }
        queryStr = 'SELECT ';
        for(Integer i=2021;i<=this.fyear;i++){
            queryStr += 'FY_SaleStatus_Batch_'+i+'__c,';
        }
        //20240222 耗材销量分析销售状态修改 end
        queryStr += ' IMS_Flag__c,ThisMonthDelivery_Price_IMS__c,LastMonthDelivery_Price_IMS__c,Sale_OrderName_Set__c,Least_Unit__c,Lastyear_SaleStatus_text__c,Thisyear_SaleStatus_text__c,RecordTypeId,ThisYearDeliveryNumber__c,Consumable_Category3__c,ASP_Price__c,Hospital__c,HospitalName__c,ProductModel__c,ThisMonthDeliveryNumber__c,LastShipmentDate__c,Type__c,Consumable_Category1__c,Consumable_Category2__c,OwnerId FROM TrackConsumableSalesForecast__c WHERE SnapshotMonth__c = :lastMonth';
        if(hisFLag&&onlyETENG){
            String recordIdET = Schema.SObjectType.TrackConsumableSalesForecast__c.getRecordTypeInfosByDeveloperName().get('ET').getRecordTypeId();
            String recordIdENG = Schema.SObjectType.TrackConsumableSalesForecast__c.getRecordTypeInfosByDeveloperName().get('ENG').getRecordTypeId();
            this.recordIdENG=recordIdENG;
            this.recordIdET=recordIdET;
            queryStr += '  AND (RecordTypeId = :recordIdET OR RecordTypeId = :recordIdENG)';
        }
    }
    //20240308 lwt 出库价，销售状态修改 end

    global Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator(queryStr);
    }

    global void execute(Database.BatchableContext BC, List<TrackConsumableSalesForecast__c> scope){
        List<TrackConsumableSalesForecast__c> newTcsfList = new List<TrackConsumableSalesForecast__c>();
        for(TrackConsumableSalesForecast__c tcsf : scope){
            TrackConsumableSalesForecast__c newTcsf = new TrackConsumableSalesForecast__c();
            newTcsf.Hospital__c = tcsf.Hospital__c;
            newTcsf.HospitalName__c = tcsf.HospitalName__c;
            newTcsf.ProductModel__c = tcsf.ProductModel__c;
            newTcsf.LastMonthDeliveryNumber__c = tcsf.ThisMonthDeliveryNumber__c != null ? tcsf.ThisMonthDeliveryNumber__c : 0;
            newTcsf.ThisMonthDeliveryNumber__c = 0;
            newTcsf.LastShipmentDate__c = tcsf.LastShipmentDate__c;
            newTcsf.Type__c = tcsf.Type__c;
            // 20231211 hql 课题  耗材【销售状态】查看权限 DB202311574810 start
            newTcsf.OwnerId = tcsf.OwnerId;
            // 20231211 hql 课题  耗材【销售状态】查看权限 DB202311574810 end
            //20231225  DB202312609378【IMS/TMS】ASP价格上传--平均价格，平均月销量 lwt start
            newTcsf.ASP_Price__c = tcsf.ASP_Price__c;
            newTcsf.Consumable_Category3__c = tcsf.Consumable_Category3__c;
            //20231225  DB202312609378【IMS/TMS】ASP价格上传--平均价格，平均月销量 lwt end
            newTcsf.SnapshotMonth__c = currentMonth;
            newTcsf.Status__c = '复制';
            newTcsf.Consumable_Category1__c = tcsf.Consumable_Category1__c;
            newTcsf.Consumable_Category2__c = tcsf.Consumable_Category2__c;
            //20240131 lwt 耗材销量分析维度修改 start
            newTcsf.RecordTypeId = tcsf.RecordTypeId;
            newTcsf.LastMonthDelivery_Price_IMS__c = tcsf.ThisMonthDelivery_Price_IMS__c != null ? tcsf.ThisMonthDelivery_Price_IMS__c : 0;
            newTcsf.ThisMonthDelivery_Price_IMS__c = 0;




            //20240222 耗材销量分析销售状态修改 start

            for(Integer i=2021;i<=this.fyear;i++){
                newTcsf.put('FY_SaleStatus_Batch_'+i+'__c',tcsf.get('FY_SaleStatus_Batch_'+i+'__c'));
            }
            //20240222 耗材销量分析销售状态修改 end

            if(currentMonth.month()==5){
                newTcsf.ThisYearDeliveryNumber__c = 0;
                newTcsf.Least_Unit__c = 0;
                newTcsf.Sale_OrderName_Set__c = '';
                newTcsf.IMS_Flag__c=false;
            }else{
                newTcsf.ThisYearDeliveryNumber__c = tcsf.ThisYearDeliveryNumber__c;
                newTcsf.Least_Unit__c = tcsf.Least_Unit__c;
                newTcsf.Sale_OrderName_Set__c = tcsf.Sale_OrderName_Set__c;
                newTcsf.IMS_Flag__c=tcsf.IMS_Flag__c;
            }
            //20240131 lwt 耗材销量分析维度修改 end

            newTcsfList.add(newTcsf);
        }
        if(!newTcsfList.isEmpty()){
            insert newTcsfList;
        }       
    }

    global void finish(Database.BatchableContext BC) {
        
        //20240308 lwt 出库价，销售状态修改 start
        //20240131 lwt 耗材销量分析维度修改 start
        // Id batjobId = Database.executeBatch(new TrackConsumableSaleForecastBatch2(currentMonth,endDate), 200);
        if(this.onlyETENG){
            Id batjobId = Database.executeBatch(new TrackConsumableSaleForecastBatch2(currentMonth,endDate,this.onlyETENG), 200);
        }else if(this.hisFLag){
            Id batjobId = Database.executeBatch(new TrackConsumableSaleForecastBatch2C(currentMonth,endDate,this.hisFLag), 200);
        }else{
            Id batjobId = Database.executeBatch(new TrackConsumableSaleForecastBatch2(currentMonth,endDate), 200);
        }
         //20240131 lwt 耗材销量分析维度修改 end 
        //20240308 lwt 出库价，销售状态修改 end  
    }
}