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;
    }    
}