public with sharing class QLMAttachmentPreviewController {
    // //附件的parentId
    public String parentId { get; private set; }
//    //附件的Id
    public String attachId { get; private set; }
    //所有附件
    public list<ContentVersion> attachMentList { get; private set; }

    //所有超过12Murl
    public list<Over12MInfo> Over12MInfoList { get; set; }


    //构造bean类
    public AttachmentInfo attachmentInfo  {get; set;}

    public String gethtmlvalue() {
        this.parentId = ApexPages.currentPage().getParameters().get('parentId');
        this.attachId = ApexPages.currentPage().getParameters().get('Id');
        System.debug('123456--=--=--=-=--=' + attachId);
        try {
            // return [SELECT Id, Body, ParentId FROM Attachment
            //        where Id = :parentId ].body.tostring();
            // String Att = [SELECT Id, Body, ParentId FROM Attachment
            //               where Id = :attachId ].body.tostring();
            // lwc改造
            String Att = [SELECT Id,Title,VersionData, ContentDocumentId
                                         FROM ContentVersion
                                         WHERE id = :attachId].VersionData.toString();
            System.debug('------123456----' + Att);
            return Att;
        } catch (exception e) {
            return '';
        }
    }

    public void init() {
        //获取页面参数
        this.parentId = ApexPages.currentPage().getParameters().get('parentId');
        this.attachId = ApexPages.currentPage().getParameters().get('Id');
        Over12MInfoList = new List<Over12MInfo>();
        try {
            //判断url是否有id
            if (String.isNotBlank(this.parentId)) {
                // this.attachMentList = [SELECT id, parentId, Name, Body, ContentType
                //                        from Attachment where parentId = :parentId];
                // lwc 修改
                // 查询 ContentDocumentLink 对象
                List<ContentDocumentLink> cdlList = [SELECT ContentDocumentId
                                                     FROM ContentDocumentLink
                                                     WHERE LinkedEntityId = :parentId];

                // 遍历 ContentDocumentLink 对象列表
                for (ContentDocumentLink cdl : cdlList) {
                    // 查询 ContentVersion 对象
                    this.attachMentList = [SELECT Id,Title,VersionData, ContentDocumentId
                                         FROM ContentVersion
                                         WHERE ContentDocumentId = :cdl.ContentDocumentId];
                }
                attachmentInfo = new AttachmentInfo(attachMentList);
                List<Tender_information__c> QLMattachMentList = [SELECT id, ProjectId__c, InfoType__c,
                                            Overstep_12M_infofile__c,
                                            File_Surpass_12M__c from Tender_information__c
                                            where Id = :parentId];
                if (QLMattachMentList.size() > 0) {
                    System.debug('QLMattachMentList:' + QLMattachMentList);
                    Integer i = 1;
                    // for (Tender_information__c Over12M : QLMattachMentList) {
                    for (String urlStr : QLMattachMentList[0].Overstep_12M_infofile__c.split(',')) {
                        // this.url = Over12M.Overstep_12M_infofile__c;
                        System.debug('urlStr:' + urlStr);
                        urlStr = urlStr.replaceAll('http://', 'https://');
                        over12MInfo over12MFile = new Over12MInfo('文件' + i, urlStr);
                        Over12MInfoList.add(over12MFile);
                        System.debug('12345678Over12MInfoList:' + Over12MInfoList);
                        i++;
                    }
                }
            }
        } catch (Exception ex) {
            ApexPages.addMessages(ex);
        }
    }

    public class AttachmentInfo {
        public list<ContentVersion> attachMentList { get; set; }

        public AttachmentInfo(list<ContentVersion> attachMentList) {
            this.attachMentList = attachMentList;
            for (ContentVersion tempatt : attachMentList) {
               tempatt.Title =  String.escapeSingleQuotes(tempatt.Title);
            }
        }
    }
    public class Over12MInfo {
        public string name { get; set; }
        public string url { get; set; }
        public Over12MInfo(string name, string url) {
            this.name = name;
            this.url = url;
        }
    }
}