yumenghui
2023-08-11 d533f39b6fa798e238810d17e928dee75d5b1af7
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
public with sharing class ReAndQISNotePDFController {
 
    // DB202304087063 课题对应
    // public Note Noteinfo {get; set;}
 
    public ContentVersion Versioninfo {get; set;}
    // public String Title{get; set;}
    // public String Body{get; set;}
    // id
    public String objId {get; set;}
 
    public ReAndQISNotePDFController(){
        objId = ApexPages.currentPage().getParameters().get('id');
        // Noteinfo = [select Id,Parentid,Title,Body  from Note where id = :objId];
        Versioninfo = [SELECT Id,Title,VersionData,TextPreview FROM ContentVersion WHERE ContentDocumentId =: objId];
    }
 
    // 生成pdf添加到对应的修理中
    @Future(callout=true)
    public static void generateAttachment(List<String> reAndQisMap){
        PageReference pdfPage ;
        system.debug('reAndQisMap数据:' + reAndQisMap);
        List<ContentDocumentLink> linkList = new List<ContentDocumentLink>();
        List<ContentVersion> versionList = new List<ContentVersion>();
        // Map<Id,Repair__c> updateRpr = [select id,SerialNumber__c,Delivered_Product__r.Name
        //                              from Repair__c where id in : reAndQisMap.keySet()];
        // Map<Id,QIS_Report__c>updateQis = [select id,lot_or_serial__c,nonyushohin__r.Name 
        //                              from QIS_Report__c where id in : reAndQisMap.keySet()];
        List<ContentDocumentLink> selectLinkList = [SELECT ContentDocumentId FROM ContentDocumentLink WHERE Id in: reAndQisMap];
        List<Id> linkIdList = new List<Id>();
        for (ContentDocumentLink link : selectLinkList) {
            linkIdList.add(link.ContentDocumentId);
        }
        List<ContentDocument> ntList = [Select id,Title FROM ContentDocument where Id in : linkIdList];
 
        List<Id> docIdList =new List<Id>();
        List<Id> verIdList = new List<Id>();
        for (ContentDocument doc : ntList) {
 
            pdfPage = new PageReference('/apex/ReAndQISNotePDF?id='+doc.Id);
            Blob pdfBody;
            if(Test.isRunningTest()) { 
                pdfBody = blob.valueOf('Unit.Test');
            } else {
                pdfBody = pdfPage.getContentAsPDF();
            }
            ContentVersion version = new ContentVersion();
            version.VersionData = pdfBody;
            version.Title = doc.Title + '.pdf';
            version.PathOnClient = '/'+doc.Title + '.pdf';
            versionList.add(version);
            docIdList.add(doc.Id);
        }
        
        insert versionList;
        for (ContentVersion ver : versionList) {
            verIdList.add(ver.Id);
        }
        List<ContentVersion> versionDocId = new List<ContentVersion>();
        if (verIdList.size()>0) {
            versionDocId = [select ContentDocumentId from ContentVersion where Id IN: verIdList];
        }
        if (versionDocId.size()>0 && docIdList.size()>0) {
            List<ContentDocumentLink> linkId = [SELECT LinkedEntityId FROM ContentDocumentLink WHERE Id IN: reAndQisMap];
            system.debug('linkId长度:' + linkId.size());
            for (ContentVersion cv : versionDocId) {
                for (ContentDocumentLink link : linkId) {
                    ContentDocumentLink cdl = new ContentDocumentLink();
                    cdl.ContentDocumentId = cv.ContentDocumentId;
                    cdl.LinkedEntityId = link.LinkedEntityId;
                    cdl.ShareType = 'I';
                    cdl.Visibility = 'AllUsers';
                    linkList.add(cdl);
                }
            }
            // insert linkList;
            Database.SaveResult[] lsr = Database.Insert(linkList, false);
            for (Integer tIdx = 0; tIdx < lsr.size(); tIdx++) {
                Database.SaveResult sr = lsr[tIdx];
                if (!sr.isSuccess()) {
                    Database.Error emsg =sr.getErrors()[0];
                    system.debug('\nERROR ADDING ACCOUNT TEAM MEMBER:' + emsg);
                }
            }
        }
        
       
        
 
        // List<Note> NtList = [Select id,ParentId,Title,Body FROM Note where Id in : reAndQisMap];
        // List<Attachment> attachments = new List<Attachment>();
        // for (Note nt : NtList) {
        //     pdfPage = new PageReference('/apex/ReAndQISNotePDF?id='+nt.Id);
        //     Blob pdfBody;
        //     if(Test.isRunningTest()) { 
        //         pdfBody = blob.valueOf('Unit.Test');
        //     } else {
        //         pdfBody = pdfPage.getContentAsPDF();
        //     }
        //     Attachment attach = new Attachment();
        //     attach.Body = pdfBody;
        //     attach.Name = nt.Title + '.pdf';
        //     attach.ParentId = nt.ParentId;
        //     attachments.add(attach);
        // }
        // insert attachments;
        // for(Repair__c re : repList){
        //     String generatedFields = re.GeneratedPDFField__c;
        //     if (checkFieldisGeneratedPDF('ProblemDescription__c',re)) {
        //         continue;                
        //     }
        //     if (re.get('ProblemDescription__c') == null) {
        //         continue;
        //     }
        //     setImgSrcs(re, 'ProblemDescription__c');            
 
        //     pdfPage = new PageReference('/apex/repairandqistopdf?api=repair&id='+re.Id);
        //     Blob pdfBody;
        //     if(Test.isRunningTest()) { 
        //         pdfBody = blob.valueOf('Unit.Test');
        //     } else {
        //         pdfBody = pdfPage.getContentAsPDF();
        //     }
        //     Attachment attach = new Attachment();
        //     attach.Body = pdfBody;
        //     attach.Name = re.name + '_' + 'Problem_Discription.pdf';
        //     attach.ParentId = re.id;
        //     attachments.add(attach);
        //     re.GeneratedPDFField__c = 'ProblemDescription__c';
        //     updateRpr.add(re);
        // }
        // insert attachments;
        // update updateRpr;
        // return null;
    }    
}