global class Consumable7daysBatch implements Database.Batchable<sObject>, Database.AllowsCallouts,Database.Stateful {
    //统计发货超过7天未收货
    public String query;

    Boolean IsNeedExecute = false;
    global Consumable7daysBatch() {
    }
    global Consumable7daysBatch(Boolean NeedExecute) {
        this.IsNeedExecute = NeedExecute;
    }

    // public Consumable7daysBatch() {
    //     this.query = query;//select id from Consumable_order_details2__c where Dealer_Arrive__c = false AND 	Dealer_Shipment__c = false AND 	Dealer_Saled__c = false
    // }
    global Database.QueryLocator start(Database.BatchableContext bc) {
        system.debug('start方法开始=================>');
        // query = 'Select Consumable_order_minor__c,id from Consumable_order_details2__c WHERE Consumable_order_minor__c != null AND Dealer_Shipment__c = false AND Dealer_Arrive__c = false AND Dealer_Saled__c = false AND Deliver_date__c < LAST_N_DAYS:7 ORDER BY  Consumable_order_minor__c';
        // //query = 'Select Consumable_order_minor__c,id from Consumable_order_details2__c WHERE Consumable_order_minor__c != null AND Dealer_Shipment__c = false AND Dealer_Arrive__c = false AND Dealer_Saled__c = false AND Deliver_date__c < LAST_N_DAYS:7 ORDER BY  Consumable_order_minor__c';
        // //Select Consumable_order_minor__c comId ,COUNT(id) cnt from Consumable_order_details2__c WHERE Consumable_order_minor__c != null AND Dealer_Shipment__c = false AND Dealer_Arrive__c = false AND Dealer_Saled__c = false AND Deliver_date__c < LAST_N_DAYS:7 GROUP BY  Consumable_order_minor__c
        // return Database.getQueryLocator(query);
        return Database.getQueryLocator([select Id from Consumable_order__c where Delivery_detail_count__c > 0 AND showFalseNotshowTrue__c = false]);
                                    
    }

    global void execute(Database.BatchableContext BC, list<Consumable_order__c> scope){
        system.debug('execute方法开始=================>');
        List<Id> COList = new List<Id>();
        List<Consumable_order__c> updateList = new List<Consumable_order__c>();
        for(Consumable_order__c temp : scope){
            COList.add(temp.Id);
        } 

        List<AggregateResult> resultsET =[Select Consumable_order_minor__c comid , COUNT(id) cid 
                                                    from Consumable_order_details2__c 
                                                    WHERE Consumable_order_minor__c != null 
                                                    AND Dealer_Shipment__c = false 
                                                    AND Dealer_Arrive__c = false 
                                                    AND Dealer_Saled__c = false 
                                                    AND Consumable_order_minor__r.showFalseNotshowTrue__c = false 
                                                    AND Deliver_date__c < LAST_N_DAYS:7
                                                    AND Consumable_order_minor__c IN :COList
                                                    GROUP BY Consumable_order_minor__c ]; 
       for(AggregateResult temp : resultsET){
           Consumable_order__c co = new Consumable_order__c();
           co.Id = String.valueOf(temp.get('comid'));
           co.More_than_seven_days__c = Integer.valueOf(temp.get('cid'));
           updateList.add(co);
        }
        system.debug('COList=================>'+COList);
        update updateList;
    }

    global void finish(Database.BatchableContext BC) {

    }
}