高章伟
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
82
global class AssetHandlerCheckQueuable implements Queueable  {
    String recIds;
    public AssetHandlerCheckQueuable(String recIds){
        this.recIds = recIds;
    }
 
    global void execute(QueueableContext context) {
        checkAsset2(this.recIds);
    }
    private static void checkAsset2(String ids) {
        List<Asset> newList = [SELECT Id
            , Ji_Zhong_Guan_Li_Ku_Cun__c
            , Out_of_wh__c
            , Out_of_wh_Sys__c
            , Rental_Count__c
            , Rental_Count_Sys__c
            , LastModifiedBy.Name
            , LastModifiedDate
            , Name
            FROM Asset WHERE Id IN:ids.split(',')
         ];
         System.debug(newList);
        List<String> rentalErrors = new List<String>();
        List<String> jizhongErrors = new List<String>();
 
        for (Asset nObj : newList) {
            if(intValueOf(nObj.Out_of_wh__c) != intValueOf(nObj.Out_of_wh_Sys__c)
                || intValueOf(nObj.Rental_Count_Sys__c) != intValueOf(nObj.Rental_Count__c)) {
                String s = '';
                s += '名称:'+nObj.Name + '\n';
                s += 'Id:'+nObj.Id + '\n';
                s += '借出_分配_数:'+nObj.Out_of_wh__c + '\n';
                s += '借出_分配_数(Sys):'+nObj.Out_of_wh_Sys__c + '\n';
                s += '已借出数:'+nObj.Rental_Count__c + '\n';
                s += '已借出数(Sys):'+nObj.Rental_Count_Sys__c + '\n';
                s += '修改人:'+nObj.LastModifiedBy.Name + '\n';
                s += '修改时间:'+ String.valueOf(nObj.LastModifiedDate) + '\n';
                rentalErrors.add(s);
            }
            if(nObj.Ji_Zhong_Guan_Li_Ku_Cun__c < 0) {
                String s = '';
                s += '名称:'+nObj.Name + '\n';
                s += 'Id:'+nObj.Id + '\n';
                s += '集中管理库存:'+nObj.Ji_Zhong_Guan_Li_Ku_Cun__c + '\n';
                s += '修改人:'+nObj.LastModifiedBy.Name + '\n';
                s += '修改时间:'+ String.valueOf(nObj.LastModifiedDate) + '\n';
                jizhongErrors.add(s);
            }
 
        }
        System.debug(rentalErrors);
        System.debug(jizhongErrors);
 
        if(!rentalErrors.isEmpty()) {
            sendEmail('已分配数或已借出数异常', String.join(rentalErrors,'\n'));
        }
        if(!jizhongErrors.isEmpty()) {
            sendEmail('集中管理库存异常', String.join(jizhongErrors,'\n'));
        }
 
    }
    private static Integer intValueOf(Decimal d) {
        if(d == null) {
            return 0;
        }
        return Integer.valueOf(d);
    }
    private static void sendEmail(String title , String bodyText) {
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
 
            String[] toAddresses = System.Label.AssetCountErrorMailAddress.split(',');
 
            mail.setToAddresses(toAddresses);
            mail.setReplyTo(UserInfo.getUserEmail());
            mail.setSubject(title);
            mail.setBccSender(false);
            mail.setUseSignature(false);
            mail.setPlainTextBody(bodyText);
 
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}