19626
2023-07-21 e53fdfdd78538a21cddd45b9d3faa1a7e9e37a0a
force-app/main/default/classes/NFM609FiledDownloadBatch.cls
@@ -43,15 +43,15 @@
        }
        try{
            List < Attachment > newAttList = AttachmentFiledDownload(endpoint, parentId, attachmentName);
            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;
            }
            // 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;
            }
@@ -66,9 +66,22 @@
        // }
    }
    global static List < Attachment > AttachmentFiledDownload(String endpoint, String parentId, String attachmentName) {
        List < Attachment > result = new List < Attachment > ();
    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);
@@ -82,9 +95,13 @@
        String statusCode = String.valueOf(response.getStatusCode());
        system.debug('statusCode---->' + statusCode);
        Attachment atta = new Attachment();
        atta.ParentId = parentId;
        atta.Name = attachmentName;
        // 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
@@ -92,14 +109,24 @@
            System.debug('ContentLength----->' + ContentLength);
            if (ContentLength < 12582912) {
                Blob bodyAsBlob = response.getBodyAsBlob();
                atta.Body = bodyAsBlob;
                result.add(atta);
                version.VersionData = bodyAsBlob;
                // atta.Body = bodyAsBlob;
                result.add(version);
            } else {
                atta.Name = '文件大小超过12M,请在新服务系统查看';
                atta.Body = Blob.valueOf('文件大小超过12M');
                result.add(atta);
                // atta.Name = '文件大小超过12M,请在新服务系统查看';
                // atta.Body = Blob.valueOf('文件大小超过12M');
                version.Title = '文件大小超过12M,请在新服务系统查看';
                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;
        }