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
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{
            // 20231103 陈京武 Lightning文件修改 Start
            List < ContentVersion > 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;
            // } 
            // 20231103 陈京武 Lightning文件修改 End
            if (updateCaseList.size() > 0) {
                update updateCaseList;
            }
 
            if (updateSubOrderList.size() > 0) {
                update updateSubOrderList;
            }
            // update sObjectList;
        } catch (Exception ex) {
 
        }
        // }
    }
// 20231103 陈京武 Lightning文件修改 Start
    global static List < ContentVersion > AttachmentFiledDownload(String endpoint, String parentId, String attachmentName) {
        List<ContentDocumentLink> linkList = [
            select
            ContentDocumentId
            from ContentDocumentLink where LinkedEntityId =: parentId
        ];
        if (linkList.size() > 0) {
            List<Id> idList = new List<Id>();
            for (ContentDocumentLink link : linkList) {
                idList.add(link.ContentDocumentId);
            }
            List<ContentDocument> conList = [select Id from ContentDocument where Id in: idList];
            delete conList;
        }
        // List < Attachment > result = new List < Attachment > ();
        List<ContentVersion> result = new List<ContentVersion>();
        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;
        ContentVersion version = new ContentVersion();
        version.Title = attachmentName;
        version.ContentLocation = 's';
        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();
                version.VersionData = bodyAsBlob;
                version.PathOnClient = attachmentName + '.txt';
                // atta.Body = bodyAsBlob;
                result.add(version);
            } else {
                // atta.Name = '文件大小超过12M,请在新服务系统查看';
                // atta.Body = Blob.valueOf('文件大小超过12M');
                version.Title = '文件大小超过12M,请在新服务系统查看';
                version.PathOnClient = '文件大小超过12M,请在新服务系统查看.txt';
                version.VersionData = Blob.valueOf('文件大小超过12M');
                result.add(version);
            }
            insert result;
            version =  [select ContentDocumentId from ContentVersion where Id =: version.Id];
            ContentDocumentLink link = new ContentDocumentLink();
            link.LinkedEntityId = parentId;
            link.ShareType = 'I';
            link.ContentDocumentId = version.ContentDocumentId;
            link.Visibility = 'AllUsers';
            insert link;
        }
// 20231103 陈京武 Lightning文件修改 End
 
 
        // system.debug('responseBody503--->'+bodyAsBlob);
        return result;
    }
 
    global void finish(Database.BatchableContext BC) {
 
    }
}