高章伟
2022-02-24 2aa8da8af66aa8ae00f25831aed6bb0364176e7b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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) {
 
    }
}