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
public with sharing class OFSRepairConsignPDFOuterController {
    public Repair__c rp { get; private set; }
    
    public string signStr { get; set; }
    
    public void init() {
        String id = ApexPages.currentPage().getParameters().get('id');
        rp = this.getReportData(id);
    }
    
    public void saveSign() {
        // 20231103 chenjingwu  Lightning文件修改 Start
        // Sign画像は一つでいいじゃない?Delete⇒Insertにする
        // 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';
        // 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 {
            
            rp.Sign__c = '<img src="/sfc/servlet.shepherd/version/download/'+ contVerFile.Id + '"/>';
            rp.SignUrl__c = '/sfc/servlet.shepherd/version/download/'+ contVerFile.Id;
            rp.SignDate__c = Date.today();
            update rp;
        } catch (Exception ex) {
            ApexPages.addMessages(ex);
            //return;
        }
// 20231103 chenjingwu  Lightning文件修改 End
        
    }
    
    public void savePDF() {
        // 20231103 chenjingwu  Lightning文件修改 Start
        // String pdfPageURL = '/apex/OFSRepairConsignPDF?id=' + rp.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 = 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);
 
        ContentVersion contVerFile = new ContentVersion();
        // TODO TestMethodはgetContentをサポートしない
        if (!Test.isRunningTest()) {
            contVerFile.VersionData = pageRef.getContent();
        } else {
            contVerFile.VersionData = EncodingUtil.base64Decode('test');
        }
        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 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;
        }
 
// 20231103 chenjingwu  Lightning文件修改 End
    }
    
    private Repair__c getReportData(String id) {
        Schema.DescribeSobjectResult d = Repair__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 Repair__c where Id = \'' + id + '\'';
        
        return Database.query(soql);
    }
}