liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
global without sharing class Batch_MappingQRCodeToAsset implements Database.Batchable<sObject> {
    private String objectApiName  = null;
    private String whereStr = null;
    public Batch_MappingQRCodeToAsset(){}
    public Batch_MappingQRCodeToAsset(String objectApiName, String whereStr) {
        this.objectApiName = objectApiName;
        this.whereStr = whereStr;
    }
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = '';
        switch on this.objectApiName {
            when null, '' {
                System.debug('No availabe object api name in start phase>>>>>>');
            }
            when 'Asset' {
                query ='select id,QRId__c FROM Asset ';
            }
            when 'Rental_Apply__c' {
                query ='select id,QRId__c FROM Rental_Apply__c ';
            }
            when 'Consum_Apply__c' {
                query ='select id,QRId__c FROM Consum_Apply__c ';
            }
            when 'TransferApply__c' {
                query ='select id,QRId__c FROM TransferApply__c ';
            }
            when 'ReceivingNote__c' {
                query ='select id,QRId__c FROM ReceivingNote__c ';
            }
            when else {
                System.debug('open soon>>>>>>>>>');
            }
        }
        if(String.isNotBlank(this.whereStr)) {
            query += this.whereStr;
        }
        System.debug('query>>>>>'+query);
        return Database.getQueryLocator(query);
    }
 
    global void execute(Database.BatchableContext BC, list<Sobject> scope) {
        if(scope.size() <= 0) {
            return;
        }
        switch on this.objectApiName {
            when 'Asset' {
                processAsset(scope);
            }
            when 'Rental_Apply__c' {
                processRentalApply(scope);
            }
            when 'Consum_Apply__c' {
                processConsumApply(scope);
            }
            when 'TransferApply__c' {
                processTransferApply(scope);
            }
            when 'ReceivingNote__c' {
                processReceivingNote(scope);
            }
            when else {
                System.debug('No availabe object api name in execution phase>>>>>>');
            }
        }
    }
 
    global void processReceivingNote(list<Sobject> scope) {
        Set<String> receivingNoteIdSet = new  Set<String>();
        List<ReceivingNote__c> updateReceivingNoteList = new List<ReceivingNote__c>();
        Set<String> receivingNoteVersionIdSet = new  Set<String>();
        for(Sobject targetObj : scope)  {
            ReceivingNote__c receivingNoteObj = (ReceivingNote__c)targetObj;
            receivingNoteIdSet.add(receivingNoteObj.Id);
        }
        String queryContentDocumentList = 'select id,ContentDocument.LatestPublishedVersionId,LinkedEntityId from ContentDocumentLink where LinkedEntityId IN :receivingNoteIdSet and LinkedEntity.type = \'ReceivingNote__c\' and ContentDocument.Title like \'QRCode-%\'';
        List<ContentDocumentLink> contentDocumentLinkList = Database.query(queryContentDocumentList);
        for(ContentDocumentLink contentDocumentLink : contentDocumentLinkList) {
            if (!receivingNoteVersionIdSet.contains(contentDocumentLink.LinkedEntityId)) {
                ReceivingNote__c tempReceivingNote = new ReceivingNote__c();
                tempReceivingNote.Id = contentDocumentLink.LinkedEntityId;
                tempReceivingNote.QRId__c = contentDocumentLink.ContentDocument.LatestPublishedVersionId;
                updateReceivingNoteList.add(tempReceivingNote);
                receivingNoteVersionIdSet.add(contentDocumentLink.LinkedEntityId);
            }
        }
 
        if(updateReceivingNoteList.size() > 0) {
            List<Database.SaveResult> saveResultsContents = Database.update(updateReceivingNoteList, false);
            insertLog(saveResultsContents,receivingNoteIdSet);
        }
    }
 
    global void processTransferApply(list<Sobject> scope) {
        Set<String> transferApplyIdSet = new  Set<String>();
        List<TransferApply__c> updateTransferApplyList = new List<TransferApply__c>();
        Set<String> transferApplyVersionIdSet = new  Set<String>();
        for(Sobject targetObj : scope)  {
            TransferApply__c transferApplyObj = (TransferApply__c)targetObj;
            transferApplyIdSet.add(transferApplyObj.Id);
        }
        String queryContentDocumentList = 'select id,ContentDocument.LatestPublishedVersionId,LinkedEntityId from ContentDocumentLink where LinkedEntityId IN :transferApplyIdSet and LinkedEntity.type = \'TransferApply__c\' and ContentDocument.Title like \'QRCode-%\'';
        List<ContentDocumentLink> contentDocumentLinkList = Database.query(queryContentDocumentList);
        for(ContentDocumentLink contentDocumentLink : contentDocumentLinkList) {
            if (!transferApplyVersionIdSet.contains(contentDocumentLink.LinkedEntityId)) {
                TransferApply__c tempTransferApply = new TransferApply__c();
                tempTransferApply.Id = contentDocumentLink.LinkedEntityId;
                tempTransferApply.QRId__c = contentDocumentLink.ContentDocument.LatestPublishedVersionId;
                updateTransferApplyList.add(tempTransferApply);
                transferApplyVersionIdSet.add(contentDocumentLink.LinkedEntityId);
            }
        }
 
        if(updateTransferApplyList.size() > 0) {
            List<Database.SaveResult> saveResultsContents = Database.update(updateTransferApplyList, false);
            insertLog(saveResultsContents,transferApplyIdSet);
        }
    }
 
    global void processConsumApply(list<Sobject> scope) {
        Set<String> consumApplyIdSet = new  Set<String>();
        List<Consum_Apply__c> updateConsumApplyList = new List<Consum_Apply__c>();
        Set<String> consumApplyVersionIdSet = new  Set<String>();
        for(Sobject targetObj : scope)  {
            Consum_Apply__c consumApplyIdObj = (Consum_Apply__c)targetObj;
            consumApplyIdSet.add(consumApplyIdObj.Id);
        }
        String queryContentDocumentList = 'select id,ContentDocument.LatestPublishedVersionId,LinkedEntityId from ContentDocumentLink where LinkedEntityId IN :consumApplyIdSet and LinkedEntity.type = \'Consum_Apply__c\' and ContentDocument.Title like \'QRCode-%\'';
        List<ContentDocumentLink> contentDocumentLinkList = Database.query(queryContentDocumentList);
        for(ContentDocumentLink contentDocumentLink : contentDocumentLinkList) {
            if (!consumApplyVersionIdSet.contains(contentDocumentLink.LinkedEntityId)) {
                Consum_Apply__c tempConsumApply = new Consum_Apply__c();
                tempConsumApply.Id = contentDocumentLink.LinkedEntityId;
                tempConsumApply.QRId__c = contentDocumentLink.ContentDocument.LatestPublishedVersionId;
                updateConsumApplyList.add(tempConsumApply);
                consumApplyVersionIdSet.add(contentDocumentLink.LinkedEntityId);
            }
        }
 
        if(updateConsumApplyList.size() > 0) {
            List<Database.SaveResult> saveResultsContents = Database.update(updateConsumApplyList, false);
            insertLog(saveResultsContents,consumApplyIdSet);
        }
    }
 
    global void processRentalApply(list<Sobject> scope) {
        Set<String> rentalApplyIdSet = new  Set<String>();
        List<Rental_Apply__c> updateRentalApplyList = new List<Rental_Apply__c>();
        Set<String> rentalApplyVersionIdSet = new  Set<String>();
        for(Sobject targetObj : scope)  {
            Rental_Apply__c rentalApplyObj = (Rental_Apply__c)targetObj;
            rentalApplyIdSet.add(rentalApplyObj.Id);
        }
        String queryContentDocumentList = 'select id,ContentDocument.LatestPublishedVersionId,LinkedEntityId from ContentDocumentLink where LinkedEntityId IN :rentalApplyIdSet and LinkedEntity.type = \'Rental_Apply__c\' and ContentDocument.Title like \'QRCode-%\'';
        List<ContentDocumentLink> contentDocumentLinkList = Database.query(queryContentDocumentList);
        for(ContentDocumentLink contentDocumentLink : contentDocumentLinkList) {
            if (!rentalApplyVersionIdSet.contains(contentDocumentLink.LinkedEntityId)) {
                Rental_Apply__c tempRentalApply = new Rental_Apply__c();
                tempRentalApply.Id = contentDocumentLink.LinkedEntityId;
                tempRentalApply.QRId__c = contentDocumentLink.ContentDocument.LatestPublishedVersionId;
                updateRentalApplyList.add(tempRentalApply);
                rentalApplyVersionIdSet.add(contentDocumentLink.LinkedEntityId);
            }
           
        }
 
        if(updateRentalApplyList.size() > 0) {
            List<Database.SaveResult> saveResultsContents = Database.update(updateRentalApplyList, false);
            insertLog(saveResultsContents,rentalApplyIdSet);
        }
    }
 
    global void processAsset(list<Sobject> scope) {
        Set<String> assetIdSet = new  Set<String>();
        List<Asset> updateAssetSet = new List<Asset>();
        Set<String> assetWithVersionIdSet = new  Set<String>();
        for(Sobject targetObj : scope)  {
            Asset assetObj = (Asset)targetObj;
            assetIdSet.add(assetObj.Id);
        }
        String queryContentDocumentList = 'select id,ContentDocument.LatestPublishedVersionId,LinkedEntityId from ContentDocumentLink where LinkedEntityId IN :assetIdSet and LinkedEntity.type = \'Asset\' and ContentDocument.Title = \'QRCode\'';
        List<ContentDocumentLink> contentDocumentLinkList = Database.query(queryContentDocumentList);
        for(ContentDocumentLink contentDocumentLink : contentDocumentLinkList) {
            if (!assetWithVersionIdSet.contains(contentDocumentLink.LinkedEntityId)) {
                Asset tempAsset = new Asset();
                tempAsset.Id = contentDocumentLink.LinkedEntityId;
                tempAsset.QRId__c = contentDocumentLink.ContentDocument.LatestPublishedVersionId;
                updateAssetSet.add(tempAsset);
                assetWithVersionIdSet.add(contentDocumentLink.LinkedEntityId);
            }
        }
 
        if(updateAssetSet.size() > 0) {
            List<Database.SaveResult> saveResultsContents = Database.update(updateAssetSet, false);
            insertLog(saveResultsContents,assetIdSet);
        }
    }
 
    global void insertLog(List<Database.SaveResult> saveResults,Set<String> mainIds) {
        Transaction_Log__c traLog = new Transaction_Log__c();
        List<Map<String,String>> logMapList = new List<Map<String,String>>();
        for (Database.SaveResult result : saveResults) {
            String recordId = result.getId();
            if (!result.isSuccess()) {
                for (Database.Error error : result.getErrors()) {
                    Map<String,String> logMap = new Map<String,String>();
                    String errorMsg = error.getMessage();
                    logMap.put('recordId',recordId);
                    logMap.put('errorMsg',errorMsg);
                    logMapList.add(logMap);
                }
            }
        }
        if(logMapList.size() > 0){
            Map<String,String> mainIdMap = new Map<String,String>();
            mainIdMap.put(this.objectApiName + 'Ids',JSON.serialize(mainIds));
            logMapList.add(mainIdMap);
            traLog.Response__c = JSON.serialize(logMapList);
            traLog.Module__c = 'QR mapping Convert Transaction';
            Insert traLog;
        }
    }
    
    global void finish(Database.BatchableContext BC) {
 
    }
 
 
}