buli
2023-05-23 07390e2fcb4adf27c928335bf27ae7939c5a80ad
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
57
58
59
60
global class RentalQueueShippmentDateSumBatch implements Database.Batchable<sObject> {
    public String query;
 
    global RentalQueueShippmentDateSumBatch() {
        this.query = query;
    }
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
        query ='select Id FROM Rental_Apply__c ';
        query += 'where Queue_Count__c > 0';
        return Database.getQueryLocator(query);
    }
 
    global void execute(Database.BatchableContext BC, List<Rental_Apply__c> raList) {
        List<String> raIdList=new List<String>();
        for (Rental_Apply__c ra : raList) {
            raIdList.add(ra.Id);
        }
        // List<AggregateResult> raesList=[select Rental_Apply__c,Max(QueueShippmentDate__c) qsd FROM Rental_Apply_Equipment_Set__c WHERE Rental_Apply__c in :raIdList AND QueueShippmentDate__c !=NULL AND First_RAESD_Is_Main__c = true AND First_RAESD__r.RAESD_Status__c = '排队中'  group by Rental_Apply__c];
        List<Rental_Apply_Equipment_Set__c> raesList=[select Rental_Apply__c,Rental_Apply__r.QueueShippmentDate__c,QueueShippmentDate__c  FROM Rental_Apply_Equipment_Set__c WHERE Rental_Apply__c in :raIdList  AND First_RAESD_Is_Main__c = true AND First_RAESD__r.RAESD_Status__c = '排队中'  order by Rental_Apply__c, QueueShippmentDate__c DESC nulls first];
        List<Rental_Apply__c> uraList=new List<Rental_Apply__c>();
        Map<String,String> RaIdMap = new Map<String,String>();
        for (Rental_Apply_Equipment_Set__c raes : raesList) {
            Rental_Apply__c ra = new Rental_Apply__c();
            // ra.Id=(Id)raes.get('Rental_Apply__c');
            // ra.QueueShippmentDate__c = (Date)raes.get('qsd');
            // 不相等的时候在更新
            // raes.Rental_Apply__r.QueueShippmentDate__c 空 raes.QueueShippmentDate__c 不空
            // raes.Rental_Apply__r.QueueShippmentDate__c 不空 raes.QueueShippmentDate__c 空
            // raes.Rental_Apply__r.QueueShippmentDate__c 不空 raes.QueueShippmentDate__c 不空 && 这两这个不相等
            
            if(
                !RaIdMap.containsKey(raes.Rental_Apply__c)
                &&((raes.Rental_Apply__r.QueueShippmentDate__c == null && raes.QueueShippmentDate__c != null)
                || (raes.Rental_Apply__r.QueueShippmentDate__c != null && raes.QueueShippmentDate__c == null)
                || (raes.Rental_Apply__r.QueueShippmentDate__c != null && raes.QueueShippmentDate__c != null && raes.Rental_Apply__r.QueueShippmentDate__c != raes.QueueShippmentDate__c)
             )){
                ra.Id=raes.Rental_Apply__c;
                ra.QueueShippmentDate__c = raes.QueueShippmentDate__c;
                uraList.add(ra);
            }
            if(RaIdMap.containsKey(raes.Rental_Apply__c))continue;
            RaIdMap.put(raes.Rental_Apply__c, raes.Rental_Apply__c);
        }
        if(uraList.size() > 0){
            update uraList;
        }
    }
 
    global void finish(Database.BatchableContext BC) {
        if(System.Test.isRunningTest()){
           test(); 
        }
 
    }
    @TestVisible private static void test() {
        Integer i = 0;
        
    }
}