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 updateCaseList = new List(); List updateSubOrderList = new List(); 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 linkList = [ select ContentDocumentId from ContentDocumentLink where LinkedEntityId =: parentId ]; if (linkList.size() > 0) { List idList = new List(); for (ContentDocumentLink link : linkList) { idList.add(link.ContentDocumentId); } List conList = [select Id from ContentDocument where Id in: idList]; delete conList; } // List < Attachment > result = new List < Attachment > (); List result = new List(); 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) { } }