binxie
2024-01-16 1b08402678deb31bba4a347bfd388eba8360cbc1
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
137
138
139
140
141
142
143
144
145
146
147
public with sharing class InsReportPDFOuterController {
    public Inspection_Report__c ir { get; private set; }
    //add by rentx 20210913 start 
    public String isVm {get;set;}
    //add by rentx 20210913 end
    
    public string signStr { get; set; }
    
    public void init() {
        String id = ApexPages.currentPage().getParameters().get('id');
        System.debug('id>>>>>' + id);
        ir = this.getReportData(id);
        System.debug('ir>>>');
        //add by rentx 20210913 start 
        isVm = ir.Contract__c == null ? 'FALSE' : 'TRUE';
        //add by rentx 20210913 end
    }
    
    public void saveSign() {
        // 20231103   Lightning文件修改 Start
        // // Sign画像は一つでいいじゃない?Delete⇒Insertにする
        // List<Attachment> atts = [select Id from Attachment where ParentId = :ir.Id and Name = :(ir.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 = ir.Name + '_Sign';
        // ac.ParentId = ir.Id;
        // ac.ContentType = 'jpg';
        // try {
        //     insert ac;
        //     //TODO status「サイン済み」にする
        //     ir.ResponsiblePerson_Sign__c = '<img src="/servlet/servlet.FileDownload?file=' + ac.Id + '"/>';
        //     ir.SignUrl__c = '/servlet/servlet.FileDownload?file=' + ac.Id;
        //     ir.Status__c = '已签字';
        //     update ir;
        // } catch (Exception ex) {
        //     ApexPages.addMessages(ex);
        //     //return;
        // }
 
        List<ContentDocumentLink> links = [select ContentDocumentId from ContentDocumentLink where LinkedEntityId =: ir.Id];
        if (links.size() > 0) {
            List<Id> idList = new List<Id>();
            for (ContentDocumentLink link : links) {
                idList.add(link.ContentDocumentId);
            }
            List<ContentDocument> cons = [select Id from ContentDocument where Id in: idList and Title =: (ir.Name + '_Sign')];
            if (cons.size() > 0) {
                delete cons;
            }
        }
        ContentVersion version = new ContentVersion();
        version.Title = ir.Name + '_Sign';
        version.VersionData = EncodingUtil.base64Decode(this.signStr.removeStart('data:image/png;base64,'));
        version.PathOnClient = ir.Name + '_Sign.jpg';
        version.ContentLocation = 's';
        try {
            insert version;
            version = [select Id,ContentDocumentId from ContentVersion where Id =: version.Id];
            ContentDocumentLink link = new ContentDocumentLink();
            link.LinkedEntityId = ir.Id;
            link.ShareType = 'I';
            link.ContentDocumentId = version.ContentDocumentId;
            link.Visibility = 'AllUsers';
            insert link;
            //     //TODO status「サイン済み」にする
            // ir.ResponsiblePerson_Sign__c = '<img src="/lightning/r/ContentDocument/'+ link.ContentDocumentId + '/view"/>';
            // ir.SignUrl__c = '/lightning/r/ContentDocument/'+ link.ContentDocumentId + '/view';
            ir.ResponsiblePerson_Sign__c = '<img src="/sfc/servlet.shepherd/version/download/'+ version.Id + '"/>';
            ir.SignUrl__c = '/sfc/servlet.shepherd/version/download/'+ version.Id;
            ir.Status__c = '已签字';
            update ir;
        } catch (Exception e) {
            System.debug('error---'+e.getmessage()+'  '+e.getlinenumber());
            ApexPages.addMessages(e);
        }
        // 20231103   Lightning文件修改 End
    }
    
    public void savePDF() {
        // 20231103   Lightning文件修改 Start
        // String pdfPageURL = '/apex/InsReportPDF?id=' + ir.Id;
        // PageReference pageRef = new PageReference(pdfPageURL);
 
        // Attachment att = new Attachment();
        // // TODO TestMethodはgetContentをサポートしない
        // if (!Test.isRunningTest()) {
        //     att.body = pageRef.getContent();
        // } else {
        //     att.body = EncodingUtil.base64Decode('test');
        // }
        // att.Name = ir.Name + '_点检报告书_' + String.valueOf(Datetime.now()) + '.pdf';
        // att.ParentId = ir.Id;
        // try {
        //     insert att;
        // } catch (Exception ex) {
        //     ApexPages.addMessages(ex);
        //     //return;
        // }
        String pdfPageURL = '/apex/InsReportPDF?id=' + ir.Id;
        PageReference pageRef = new PageReference(pdfPageURL);
        ContentVersion version = new ContentVersion();
        if (!Test.isRunningTest()) {
            version.VersionData = pageRef.getContentAsPDF();
        } else {
            version.VersionData = EncodingUtil.base64Decode('test');
        }
        // version.Title = ir.Name + '_点检报告书_' + String.valueOf(Datetime.now()) + '.pdf';
        version.Title = ir.Name + '_点检报告书_' + String.valueOf(Datetime.now());
        version.ContentLocation = 's';
        version.PathOnClient = ir.Name + '_点检报告书_' + String.valueOf(Datetime.now()) + '.pdf';
        try {
            insert version;
            version = [select ContentDocumentId from ContentVersion where Id =: version.Id];
            ContentDocumentLink link = new ContentDocumentLink();
            link.LinkedEntityId = ir.Id;
            link.ShareType = 'I';
            link.Visibility = 'AllUsers';
            link.ContentDocumentId = version.ContentDocumentId;
            insert link;
        } catch (Exception e) {
            ApexPages.addMessages(e);
            // ApexPages.addMessage(e);
        }
        // 20231103   Lightning文件修改 End
    }
    
    private Inspection_Report__c getReportData(String id) {
        Schema.DescribeSobjectResult d = Inspection_Report__c.sObjectType.getDescribe();
        Map<String, Schema.SObjectField> fieldMap = d.fields.getMap();
        
        String soql = 'select ';
        String fields = '';
        for (String field : fieldMap.keySet()) {
            if (fields.length() > 0) {
                fields += ', ';
            }
            fields += field;
        }
        soql += fields;
        soql += ' from Inspection_Report__c where Id = \'' + id + '\'';
        
        System.debug('soql = ' + soql);
        return Database.query(soql);
    }
}