global class ConsumableTargetManageBatch6 implements Database.Batchable<sObject> {
public Date currentMonth;
    public Date endDate;
    public Date startDate;
    public String queryStr;
    public String year;
    public Boolean isIncrementOnly; //是否只跑增量
    public String OCSMYear;
    global ConsumableTargetManageBatch6(Date startDate,Date endDate) {
        this.startDate=startDate;
        this.endDate=endDate;
        Integer year = startDate.year();
        Integer FYear = year;
        if(startDate>=Date.newInstance(year, 4, 1)){
            FYear = FYear+1;
        }
        String  OCSMYear= 'FY'+FYear;
        this.OCSMYear=OCSMYear;
        queryStr = 'SELECT  nowDosageNumTotal__c,nowDosageAmountTotal__c,lastDosageNumTotal__c,lastDosageAmountTotal__c,Hospital__c,OCM_Year__c,Category3__c,Category4__c FROM ConsumableTargetManage__c WHERE OCM_Year__c = :OCSMYear';
    }

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

    global void execute(Database.BatchableContext BC, list<ConsumableTargetManage__c> scope) {
        Integer year = startDate.year();
        Integer FYear = year;
        if(startDate>=Date.newInstance(year, 4, 1)){
            FYear = FYear+1;
        }

        String  OCSMYear= 'FY'+FYear;        
        Integer lastFYear = FYear-1;
        String  lastOCSMYear= 'FY'+lastFYear;

        Set<String> ctmKeySet=new Set<String>();
        for(ConsumableTargetManage__c ctm:scope){
            ctmKeySet.add(ctm.Hospital__c+'-'+ctm.Category3__c+'-'+ctm.Category4__c+'-'+lastOCSMYear);
        }

 
        List<ConsumableTargetManage__c> lastCtmList= [SELECT  nowDosageNumTotal__c,nowDosageAmountTotal__c,lastDosageNumTotal__c,lastDosageAmountTotal__c,Hospital__c,OCM_Year__c,Category3__c,Category4__c FROM ConsumableTargetManage__c WHERE  ConsumableTargetManageKey__c in :ctmKeySet];
        Map<String,ConsumableTargetManage__c> lastCtmMap=new Map<String,ConsumableTargetManage__c>();
        
        for(ConsumableTargetManage__c ctm:lastCtmList){
            String key=ctm.Category3__c+'-'+ctm.Category4__c+'-'+ ctm.Hospital__c;
            lastCtmMap.put(key, ctm);
        }
        for(ConsumableTargetManage__c ctm:scope){
            String key=ctm.Category3__c+'-'+ctm.Category4__c+'-'+ ctm.Hospital__c;
            if(lastCtmMap.containsKey(key)){
                if(lastCtmMap.get(key).nowDosageNumTotal__c!=null){
                    ctm.lastDosageNumTotal__c=lastCtmMap.get(key).nowDosageNumTotal__c;
                }
                
                if(lastCtmMap.get(key).nowDosageAmountTotal__c!=null){
                    ctm.lastDosageAmountTotal__c=lastCtmMap.get(key).nowDosageAmountTotal__c;
                }
                
            }else{
                ctm.lastDosageNumTotal__c=0;
                ctm.lastDosageAmountTotal__c=0;
            }
        }

        update scope;
        
    }

    global void finish(Database.BatchableContext BC) {

    }
}