global without sharing class TrackConsumableSaleForecastBatch1 implements Database.Batchable<sObject>{

    public Date currentMonth;
    public Date endDate;
    public Date lastMonth;
    public String queryStr;

    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
        queryStr = 'SELECT 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';
    }

    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;
            //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;
            newTcsfList.add(newTcsf);
        }
        if(!newTcsfList.isEmpty()){
            insert newTcsfList;
        }       
    }

    global void finish(Database.BatchableContext BC) {
        Id batjobId = Database.executeBatch(new TrackConsumableSaleForecastBatch2(currentMonth,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++;
                
                
    
            }
}