高章伟
2022-02-24 2aa8da8af66aa8ae00f25831aed6bb0364176e7b
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
global class NFM609FiledDownloadBatch implements Database.Batchable < sObject > , Database.AllowsCallouts {
    public String query;
    public string objectType;
    global NFM609FiledDownloadBatch() {
        this.query = query;
    }
 
    global NFM609FiledDownloadBatch(String objectType) {
        this.objectType = objectType;
    }
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
        String sql = 'SELECT Id, AttachmentDownload__c, AttachmentLink__c, AttachmentName__c FROM '+objectType+' where AttachmentDownload__c = true AND AttachmentLink__c != \'\' AND AttachmentName__c != \'\'';
        System.debug('sql--->'+sql);
        return Database.getQueryLocator(sql);
    }
 
    global void execute(Database.BatchableContext BC, List < sObject > sObjectList) {
        String endpoint = '';
        String parentId = '';
        String attachmentName = '';
        List<Case> updateCaseList = new List<Case>();
        List<RepairSubOrder__c> updateSubOrderList = new List<RepairSubOrder__c>();
        for (sObject obj: sObjectList) {
            if ('Case'.equals(objectType)) {
                Case cas = (Case)obj;
                cas.AttachmentDownload__c = false;
                endpoint = cas.AttachmentLink__c;
                parentId = cas.Id;
                attachmentName = cas.AttachmentName__c;
                updateCaseList.add(cas);    
            }
 
            if ('RepairSubOrder__c'.equals(objectType)) {
                RepairSubOrder__c subOrder = (RepairSubOrder__c)obj;
                subOrder.AttachmentDownload__c = false;
                endpoint = subOrder.AttachmentLink__c;
                parentId = subOrder.Id;
                attachmentName = subOrder.AttachmentName__c;
                updateSubOrderList.add(subOrder);    
            }
            
        }
 
        try{
            List < Attachment > newAttList = AttachmentFiledDownload(endpoint, parentId, attachmentName);
 
            if (newAttList.size() > 0) {
                List < Attachment > delAttaList = [select id from Attachment where ParentId =: parentId];
                if (delAttaList.size() > 0) {
                    delete delAttaList;
                }
                insert newAttList;
            } 
            if (updateCaseList.size() > 0) {
                update updateCaseList;
            }
 
            if (updateSubOrderList.size() > 0) {
                update updateSubOrderList;
            }
            // update sObjectList;
        } catch (Exception ex) {
 
        }
        // }
    }
 
    global static List < Attachment > AttachmentFiledDownload(String endpoint, String parentId, String attachmentName) {
 
        List < Attachment > result = new List < Attachment > ();
        Http http = new Http();
        HttpRequest req = new HttpRequest();
        req.setTimeout(120000);
        req.setEndpoint(endpoint);
        req.setMethod('GET');
 
        HTTPResponse response = http.send(req);
 
        String status = response.getStatus();
        system.debug('status---->' + status);
 
        String statusCode = String.valueOf(response.getStatusCode());
        system.debug('statusCode---->' + statusCode);
        Attachment atta = new Attachment();
        atta.ParentId = parentId;
        atta.Name = attachmentName;
        if ('200'.equals(statusCode)) {
            // 附件大小
            // 12,582,912
            Integer ContentLength = Integer.valueOf(response.getHeader('Content-Length'));
            System.debug('ContentLength----->' + ContentLength);
            if (ContentLength < 12582912) {
                Blob bodyAsBlob = response.getBodyAsBlob();
                atta.Body = bodyAsBlob;
                result.add(atta);
            } else {
                atta.Name = '文件大小超过12M,请在新服务系统查看';
                atta.Body = Blob.valueOf('文件大小超过12M');
                result.add(atta);
            }
 
        }
 
 
 
        // system.debug('responseBody503--->'+bodyAsBlob);
        return result;
    }
 
    global void finish(Database.BatchableContext BC) {
 
    }
}