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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
@RestResource(urlMapping='/OFSUploadImage/*')
global with sharing class OFSUploadImageRest {
    // 20231103 chenjingwu  Lightning文件修改 Start
    @HttpPost
    global static void doPost(String repairId, String ccImg, String acImg, String rqId) {
        system.debug('OFSUploadImageRest.start');
        RestResponse res = RestContext.response;
        res.addHeader('Content-Type', 'application/json');
        
        String jsonResponse;
        String ccId, acId;
        
        if (String.isNotBlank(repairId)) {
            Repair__c repair = null;
            try {
                repair = [Select Id, contract_consent_id__c, acceptance_id__c From Repair__c Where Id= :repairId];
            } catch ( Exception ex ) {
                returnMessage('{"status": "Failure", "Message": "no repair found"}',200);
                // res.statusCode = 200;
                // jsonResponse = '{"status": "Failure", "Message": "no repair found"}';
                // res.responseBody = blob.valueOf(jsonResponse);
                // return;
            }
            
            if (String.isNotBlank(ccImg)) {
                Repair_Quotation__c repairQ = null;
                try {
                    repairQ = [Select Id, contract_consent_id__c From Repair_Quotation__c Where Id= :rqId];
                } catch ( Exception ex ) {
                    returnMessage('{"status": "Failure", "Message": "no RepairQuotation found"}',200);
                    // res.statusCode = 200;
                    // jsonResponse = '{"status": "Failure", "Message": "no RepairQuotation found"}';
                    // res.responseBody = blob.valueOf(jsonResponse);
                    // return;
                }
                // 20231103 chenjingwu  Lightning文件修改 Start
                // Attachment att;
                ContentVersion version;
                if (String.isNotBlank(repairQ.contract_consent_id__c)) {
                    // att = [Select Id, Name, Body, ParentId From Attachment Where Id= :repairQ.contract_consent_id__c];
                    version = [select Id,VersionData,Title from ContentVersion where Id =: repairQ.contract_consent_id__c];
                } else {
                    version = new ContentVersion();
                    version.Title = 'ContractConsent.jpg';
                    version.ContentLocation = 's';
                    version.PathOnClient = 'ContractConsent.jpg';
                    // att = new Attachment();
                    // att.Name = 'ContractConsent.jpg';
                    // att.ParentId = rqId;
                }
                // att.Body = EncodingUtil.base64Decode(ccImg);
                version.VersionData = EncodingUtil.base64Decode(ccImg);
                try {
                    if (String.isNotBlank(repairQ.contract_consent_id__c)) {
                        update version; 
                    }
                    else { 
                        insert version; 
                        version = [select Id,ContentDocumentId from ContentVersion where Id =: version.Id];
                        ContentDocumentLink link = new ContentDocumentLink();
                        link.LinkedEntityId = rqId;
                        link.ContentDocumentId = version.ContentDocumentId;
                        link.ShareType = 'I';
                        link.Visibility = 'AllUsers';
                        insert link;
                    }
                    ccId = version.Id;
                    repairQ.contract_consent_id__c = ccId;
                } catch ( Exception ex ) {
                    //TODO:
                    //error message:cannot update exception
                    returnMessage('{"status": "Failure", "Message": "error when try to upsert ContractConsent Attachment. '+ ex +'"}',200);
                    // res.statusCode = 200;
                    // jsonResponse = '{"status": "Failure", "Message": "error when try to upsert ContractConsent Attachment. '+ ex +'"}';
                    // res.responseBody = blob.valueOf(jsonResponse);
                    // return;
                }
                // 20231103 chenjingwu  Lightning文件修改 End
                
                try {
                    update repairQ;
                } catch ( Exception ex ) {
                    //TODO:
                    //error message:cannot update exception
                    returnMessage('{"status": "Failure", "Message": "error when try to update RepairQuotation. '+ ex +'"}',200);
                    // res.statusCode = 200;
                    // jsonResponse = '{"status": "Failure", "Message": "error when try to update RepairQuotation. '+ ex +'"}';
                    // res.responseBody = blob.valueOf(jsonResponse);
                    // return;
                }
            }
            if (String.isNotBlank(acImg)) {
                // 20231103 chenjingwu  Lightning文件修改 Start
                // Attachment att;
                ContentVersion version;
                if (String.isNotBlank(repair.acceptance_id__c)) {
                    // att = [Select Id, Name, Body, ParentId From Attachment Where Id= :repair.acceptance_id__c];
                    version = [select Id,Title,VersionData from ContentVersion where Id =: repair.acceptance_id__c];
                } else {
                    version = new ContentVersion();
                    version.Title = 'Acceptance.jpg';
                    version.ContentLocation = 's';
                    version.PathOnClient = 'Acceptance.jpg'; 
                    // att = new Attachment();
                    // att.Name = 'Acceptance.jpg';
                    // att.ParentId = repairId;
                }
                // att.Body = EncodingUtil.base64Decode(acImg);
                version.VersionData = EncodingUtil.base64Decode(acImg);
                try {
                    if (String.isNotBlank(repair.acceptance_id__c)) {
                        update version; 
                    }
                    else {
                        insert version; 
                        version = [select Id,ContentDocumentId from ContentVersion where Id =: version.Id];
                        ContentDocumentLink link = new ContentDocumentLink();
                        link.ShareType = 'I';
                        link.Visibility = 'AllUsers';
                        link.ContentDocumentId = version.ContentDocumentId;
                        link.LinkedEntityId = repairId;
                        insert link;
                    }
                    acId = version.Id;
                } catch ( Exception ex ) {
                    //TODO:
                    //error message:cannot update exception
                    returnMessage('{"status": "Failure", "Message": "error when try to upsert Acceptance Attachment. '+ ex +'"}',200);
                    // res.statusCode = 200;
                    // jsonResponse = '{"status": "Failure", "Message": "error when try to upsert Acceptance Attachment. '+ ex +'"}';
                    // res.responseBody = blob.valueOf(jsonResponse);
                    // return;
                }
                // 20231103 chenjingwu  Lightning文件修改 End
            }
            
            if (String.isNotBlank(ccId)) repair.Repair_Quotation_Id__c = rqId;
            if (String.isNotBlank(acId)) repair.acceptance_id__c = acId;
            try {
                update repair;
                returnMessage('{"status": "Success", "Message": "upload Success"}',200);
                // res.statusCode = 200;
                // jsonResponse = '{"status": "Success", "Message": "upload Success"}';
                // res.responseBody = blob.valueOf(jsonResponse);
                // return;
            } catch ( Exception ex ) {
                //TODO:
                //error message:cannot update exception
                returnMessage('{"status": "Failure", "Message": "error when try to update repair data. '+ ex +'"}',200);
                // res.statusCode = 200;
                // jsonResponse = '{"status": "Failure", "Message": "error when try to update repair data. '+ ex +'"}';
                // res.responseBody = blob.valueOf(jsonResponse);
                // return;
            }
        } else {
            returnMessage('{"status": "Failure", "Message": "no repair Id"}',200);
            // res.statusCode = 200;
            // jsonResponse = '{"status": "Failure", "Message": "no repair Id"}';
            // res.responseBody = blob.valueOf(jsonResponse);
            // return;
        }
    }
 
    public static void returnMessage(String response,Integer code){
        RestResponse res = RestContext.response;
        res.addHeader('Content-Type', 'application/json');
        String jsonResponse;
        res.statusCode = code;
        jsonResponse = response;
        res.responseBody = blob.valueOf(jsonResponse);
        return;
    }
    // 20231103 chenjingwu  Lightning文件修改 End
}