高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
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
61
62
63
64
65
66
67
68
global without sharing class RentalApplyDetailStatusUpdateBatch implements Database.Batchable<sObject>, Database.Stateful {
    global List<String> errorList = new List<String>();
    global Integer allCount = 0;
    global Integer doneCount = 0;
    private UpdateRentalApplyBatch.ScBean scB;
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
        scB = UpdateRentalApplyBatch.setSc('RentalApplyDetailStatusUpdateSchedule', 9, 23, 0, '0 30 2', null);
        if (System.Test.isRunningTest() == false) {
            for(CronTrigger ct : [SELECT Id FROM CronTrigger WHERE CronJobDetail.Name =: scB.scName]) {
                System.abortJob(ct.Id);
            }
            system.schedule(scB.scName, scB.scTime, new RentalApplyDetailStatusUpdateSchedule());
        }
        String query = 'SELECT RAESD_Status__c'
                    + ' FROM Rental_Apply_Equipment_Set_Detail__c'
                    + ' WHERE RAESD_Status_Text_Update_F__c = true';
        return Database.getQueryLocator(query);
    }
 
    global void execute(Database.BatchableContext BC, list<Rental_Apply_Equipment_Set_Detail__c> raesdList) {
        allCount += raesdList.size();
        Savepoint sp = Database.setSavepoint();
        try {
            List<Rental_Apply_Equipment_Set_Detail__c> updateList = new List<Rental_Apply_Equipment_Set_Detail__c>();
            List<Database.SaveResult> updResultList = Database.update(raesdList, false);
            for (Integer i = 0; i < updResultList.size(); i++) {
                Database.SaveResult sr = updResultList[i];
                Rental_Apply_Equipment_Set_Detail__c raesd = raesdList[i];
                if (true == sr.isSuccess()) {
                    raesd.RAESD_Status_Text__c = raesd.RAESD_Status__c;
                    updateList.add(raesd);
                } else {
                    for (Database.Error err : sr.getErrors()) {
                        errorList.add(err.getStatusCode() + ':' + err.getMessage() + err.getFields());
                    }
                }
            }
            if(updateList.size() > 0) {
                Database.update(updateList);
                doneCount += updateList.size();
            }
        }
        catch (Exception e) {
            Database.rollback(sp);
            errorList.add(e.getMessage() + '\n' + e.getStackTraceString());
            System.debug(LoggingLevel.ERROR, e.getMessage() + '\n' + e.getStackTraceString());
            throw e;
        }
    }
 
    global void finish(Database.BatchableContext BC) {
        UpdateRentalApplyBatch.removeOtherSc('RentalApplyDetailStatusUpdateSchedule', scB.scName);
        if (allCount != doneCount || errorList.size() > 0) {
            BatchEmailUtil be = new BatchEmailUtil();
            String[] toList = new String[]{};
            String title = '借出明细状态更新batch失败';
            String[] ccList = new String[]{};
            for (OrgWideEmailAddress tmpEmailObj : [SELECT Id, Address, DisplayName
                    FROM OrgWideEmailAddress
                    WHERE DisplayName like 'BatchNotify']) {
                toList.add(tmpEmailObj.Address);
            }
            be.failedMail(toList, ccList, title, String.join(this.errorList, '\n'), allCount, doneCount, errorList.size());
            be.send();
        }
    }
}