From 254ea442ff0e0a7a97e9f481e9f127a782cba840 Mon Sep 17 00:00:00 2001
From: 19626 <1962676589@qq.com>
Date: 星期一, 17 七月 2023 11:37:35 +0800
Subject: [PATCH] 修改附件7.17

---
 force-app/main/default/classes/OFSRepairConsignPDFOuterController.cls |  100 ++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 84 insertions(+), 16 deletions(-)

diff --git a/force-app/main/default/classes/OFSRepairConsignPDFOuterController.cls b/force-app/main/default/classes/OFSRepairConsignPDFOuterController.cls
index 49701be..a089c99 100644
--- a/force-app/main/default/classes/OFSRepairConsignPDFOuterController.cls
+++ b/force-app/main/default/classes/OFSRepairConsignPDFOuterController.cls
@@ -10,45 +10,113 @@
     
     public void saveSign() {
         // Sign鐢诲儚銇竴銇ゃ仹銇勩亜銇樸們銇亜锛烡elete鈬扞nsert銇仚銈�
-        List<Attachment> atts = [select Id from Attachment where ParentId = :rp.Id and Name = :(rp.Name + '_Sign')];
-        if (atts.size() > 0) delete atts;
+        // List<Attachment> atts = [select Id from Attachment where ParentId = :rp.Id and Name = :(rp.Name + '_Sign')];
+        // if (atts.size() > 0) delete atts;
         
-        Attachment ac = new Attachment();
-        ac.Body = EncodingUtil.base64Decode(this.signStr.removeStart('data:image/png;base64,'));
-        ac.Name = rp.Name + '_Sign';
-        ac.ParentId = rp.Id;
-        ac.ContentType = 'jpg';
+        // Attachment ac = new Attachment();
+        // ac.Body = EncodingUtil.base64Decode(this.signStr.removeStart('data:image/png;base64,'));
+        // ac.Name = rp.Name + '_Sign';
+        // ac.ParentId = rp.Id;
+        // ac.ContentType = 'jpg';
+        // try {
+        //     insert ac;
+        //     rp.Sign__c = '<img src="/servlet/servlet.FileDownload?file=' + ac.Id + '"/>';
+        //     rp.SignUrl__c = '/servlet/servlet.FileDownload?file=' + ac.Id;
+        //     rp.SignDate__c = Date.today();
+        //     update rp;
+        // } catch (Exception ex) {
+        //     ApexPages.addMessages(ex);
+        //     //return;
+        // }
+
+
+
+        List<ContentDocumentLink> cdlList = [SELECT ContentDocumentId
+                                                   FROM ContentDocumentLink
+                                                   WHERE LinkedEntityId = :rp.Id];
+        List<ID> fileIDs = new List<ID>();
+        for (ContentDocumentLink docLink : cdlList) {
+            fileIDs.add(docLink.ContentDocumentId);
+        }
+        List<ContentDocument> attachmentinfo=[SELECT Title,OwnerId from ContentDocument WHERE id IN :fileIDs and Title =: (rp.Name + '_Sign')];
+        if (attachmentinfo.size() > 0) delete attachmentinfo;
+        ContentVersion contVerFile = new ContentVersion();
+        contVerFile.VersionData = EncodingUtil.base64Decode(this.signStr.removeStart('data:image/png;base64,'));
+        contVerFile.Title = rp.Name + '_Sign';
+        contVerFile.ContentLocation= 's';
+        contVerFile.PathOnClient=rp.Name + '_Sign.jpg';
+        insert contVerFile;
+        Id conDoc = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:contVerFile.Id].ContentDocumentId;
+        ContentDocumentLink cDe = new ContentDocumentLink();
+        cDe.ContentDocumentId = conDoc;
+        cDe.LinkedEntityId = rp.Id;
+        cDe.ShareType = 'I';
+        cDe.Visibility = 'AllUsers';
+        insert cDe;
         try {
-            insert ac;
-            rp.Sign__c = '<img src="/servlet/servlet.FileDownload?file=' + ac.Id + '"/>';
-            rp.SignUrl__c = '/servlet/servlet.FileDownload?file=' + ac.Id;
+            
+            rp.Sign__c = '<img src="/lightning/r/ContentDocument/' + contVerFile.ContentDocumentId + '/view"/>';
+            rp.SignUrl__c = '/lightning/r/ContentDocument/' + contVerFile.ContentDocumentId + '/view';
             rp.SignDate__c = Date.today();
             update rp;
         } catch (Exception ex) {
             ApexPages.addMessages(ex);
             //return;
         }
+
+        
     }
     
     public void savePDF() {
+        // String pdfPageURL = '/apex/OFSRepairConsignPDF?id=' + rp.Id;
+        // PageReference pageRef = new PageReference(pdfPageURL);
+
+        // Attachment att = new Attachment();
+        // // TODO TestMethod銇痝etContent銈掋偟銉濄兗銉堛仐銇亜
+        // if (!Test.isRunningTest()) {
+        //     att.body = pageRef.getContent();
+        // } else {
+        //     att.body = EncodingUtil.base64Decode('test');
+        // }
+        // att.Name = rp.Name + '_缁翠慨濮旀墭涔' + String.valueOf(Datetime.now()) + '.pdf';
+        // att.ParentId = rp.Id;
+        // try {
+        //     insert att;
+        // } catch (Exception ex) {
+        //     ApexPages.addMessages(ex);
+        //     //return;
+        // }
+
+
         String pdfPageURL = '/apex/OFSRepairConsignPDF?id=' + rp.Id;
         PageReference pageRef = new PageReference(pdfPageURL);
 
-        Attachment att = new Attachment();
+        ContentVersion contVerFile = new ContentVersion();
         // TODO TestMethod銇痝etContent銈掋偟銉濄兗銉堛仐銇亜
         if (!Test.isRunningTest()) {
-            att.body = pageRef.getContent();
+            contVerFile.VersionData = pageRef.getContent();
         } else {
-            att.body = EncodingUtil.base64Decode('test');
+            contVerFile.VersionData = EncodingUtil.base64Decode('test');
         }
-        att.Name = rp.Name + '_缁翠慨濮旀墭涔' + String.valueOf(Datetime.now()) + '.pdf';
-        att.ParentId = rp.Id;
+        contVerFile.Title = rp.Name + '_缁翠慨濮旀墭涔' + String.valueOf(Datetime.now()) + '.pdf';
+        contVerFile.ContentLocation= 's';
+        contVerFile.PathOnClient=rp.Name + '_缁翠慨濮旀墭涔' + String.valueOf(Datetime.now()) + '.pdf';
+        // att.ParentId = rp.Id;
         try {
-            insert att;
+            insert contVerFile;
+            Id conDoc = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:contVerFile.Id].ContentDocumentId;
+            ContentDocumentLink cDe = new ContentDocumentLink();
+            cDe.ContentDocumentId = conDoc;
+            cDe.LinkedEntityId = rp.Id;
+            cDe.ShareType = 'I';
+            cDe.Visibility = 'AllUsers';
+            insert cDe;
         } catch (Exception ex) {
             ApexPages.addMessages(ex);
             //return;
         }
+
+
     }
     
     private Repair__c getReportData(String id) {

--
Gitblit v1.9.1