From e53fdfdd78538a21cddd45b9d3faa1a7e9e37a0a Mon Sep 17 00:00:00 2001
From: 19626 <1962676589@qq.com>
Date: 星期五, 21 七月 2023 13:36:24 +0800
Subject: [PATCH] 修改附件

---
 force-app/main/default/classes/NFM609FiledDownloadBatch.cls |   67 +++++++++++++++++++++++----------
 1 files changed, 47 insertions(+), 20 deletions(-)

diff --git a/force-app/main/default/classes/NFM609FiledDownloadBatch.cls b/force-app/main/default/classes/NFM609FiledDownloadBatch.cls
index 313a8d5..0a9062b 100644
--- a/force-app/main/default/classes/NFM609FiledDownloadBatch.cls
+++ b/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;
         }
 
 

--
Gitblit v1.9.1