高章伟
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
69
70
71
72
73
74
75
76
77
78
79
80
81
global class InventoryAutoGiveupBatch implements Database.Batchable<sObject>, Database.Stateful {
 
    global List<String> emailMessages = new List<String>();
    global Integer totalCount = 0; // 总件数
    global Integer failedCount = 0;
    global Date tdy = Date.today();
 
    /**
     * コンスタント
     */
    global InventoryAutoGiveupBatch() {
    }
 
    /**
     * startには、queryを実行、备品Set明细を検索
     */
    global Database.QueryLocator start(Database.BatchableContext BC) {
        bp3_Setting__c conf = bp3_Setting__c.getOrgDefaults();
        Date autoGiveupDate30 = Date.today().addDays(-(conf.Auto_GiveUp_Day__c == null ? 30 : Integer.valueOf(conf.Auto_GiveUp_Day__c)));
        Date autoGiveupStartDate = conf.Auto_Giveup_StartDate__c == null ? Date.valueOf('1970-01-01') : conf.Auto_Giveup_StartDate__c;
        return Database.getQueryLocator(
           [SELECT Id,
                   LostReport_Detail__c,
                   LostReport_Detail__r.LostReport_Status_F__c,
                   LostReport_Detail__r.LostReport__c
              FROM Inventory_Detail__c
             WHERE Auto_Lost_item_giveup__c = false
               AND Inventory_Deviation__c < 0
               AND Internal_Asset_Flg__c = true
               AND Inventory_Time__c != null
               AND Auto_Loaner_Giveup_StartCalculating_Date__c >= :autoGiveupStartDate
               AND Auto_Loaner_Giveup_StartCalculating_Date__c != null
               AND Auto_Loaner_Giveup_StartCalculating_Date__c <= :autoGiveupDate30
            ORDER BY Id]
        );
    }
 
    global void execute(Database.BatchableContext BC, List<SObject> ldList) {
        Savepoint sp = Database.setSavepoint();
        try{
            totalCount += ldList.size();
            List<LostReport_Detail__c> lsList = new List<LostReport_Detail__c>();
            for (SObject sa : ldList) {
                Inventory_Detail__c ld = (Inventory_Detail__c)sa;
                if (String.isNotBlank(ld.LostReport_Detail__c) && ld.LostReport_Detail__r.LostReport_Status_F__c != '已批准') {
                    lsList.add(ld.LostReport_Detail__r);
                }
            }
            System.debug(totalCount);
            //update by rentx 2021-05-14 start 1635
            // LostReportHandler.Loaner_AutoGiveup(null, lsList, ldList);
            LostReportHandler.Loaner_AutoGiveup(null, lsList, ldList, null);
            //update by rentx 2021-05-14 end 1635
        }
        catch (Exception e) {
            emailMessages.add(e.getMessage());
            failedCount += ldList.size();
            Database.rollback(sp);
        }
    }
    global void finish(Database.BatchableContext BC) {
        BatchEmailUtil be = new BatchEmailUtil();
        String[] toList = new String[]{UserInfo.getUserEmail()};
        String title = '盘亏报告自动断念';
        String[] ccList = new String[] {};
        if(this.emailMessages.size() == 0){
            be.successMail(toList, ccList, title, totalCount);
        }else{
            String emailLabel = 'BatchNotify';
            for (OrgWideEmailAddress tmpEmailObj : [SELECT Id, Address, DisplayName 
                    FROM OrgWideEmailAddress
                    WHERE DisplayName like :emailLabel]) {
                ccList.add(tmpEmailObj.Address);
            }
            be.failedMail(toList, ccList, title,
                String.join(this.emailMessages, '\n'),
                totalCount, totalCount - failedCount, failedCount);
        }
        be.send();
    }
}