From fd9df05c48c09b4d7f39a3b401882bda226dedea Mon Sep 17 00:00:00 2001
From: 19626 <1962676589@qq.com>
Date: 星期二, 31 十月 2023 20:38:18 +0800
Subject: [PATCH] 近期修改

---
 force-app/main/default/classes/OFSUploadRepairImageRest.cls |  181 +++++++++++++++++++++++++++++++--------------
 1 files changed, 124 insertions(+), 57 deletions(-)

diff --git a/force-app/main/default/classes/OFSUploadRepairImageRest.cls b/force-app/main/default/classes/OFSUploadRepairImageRest.cls
index 1e68355..cef55b6 100644
--- a/force-app/main/default/classes/OFSUploadRepairImageRest.cls
+++ b/force-app/main/default/classes/OFSUploadRepairImageRest.cls
@@ -1,5 +1,6 @@
 @RestResource(urlMapping='/OFSUploadRepairImage/*')
 global with sharing class OFSUploadRepairImageRest {
+    // 20231103 chenjingwu  Lightning鏂囦欢淇敼 Start
     @HttpPost
     global static void doPost(String repairId, String ccImg, String acImg, String rqId) {
         system.debug('OFSUploadImageRest.start');
@@ -11,43 +12,74 @@
         String acId;            // 妞滃弾鏇�
         
         if (String.isNotBlank(repairId)) {
-            Repair__c repair = [Select Id, Agreed_Date__c, Facility_Return_Receipt_Collection_Date__c, acceptance_id__c From Repair__c Where Id= :repairId];
-            if (repair == null) {
-                res.statusCode = 200;
-                jsonResponse = '{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage + '": "no repair found"}';
-                res.responseBody = blob.valueOf(jsonResponse);
-                return;
+            Repair__c repair = null;
+            try {
+                repair = [Select Id, Agreed_Date__c, Facility_Return_Receipt_Collection_Date__c, acceptance_id__c From Repair__c Where Id= :repairId];  
+            } catch ( Exception ex ) {
+                returnMessage('{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage + '": "no repair found"}',200);
             }
+            // if (repair == null) {
+            //     res.statusCode = 200;
+            //     jsonResponse = '{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage + '": "no repair found"}';
+            //     res.responseBody = blob.valueOf(jsonResponse);
+            //     return;
+            // }
             if (String.isNotBlank(ccImg)) {
-                Repair_Quotation__c repairQ = [Select Id, contract_consent_id__c From Repair_Quotation__c Where Id= :rqId];
-                if (repairQ == null) {
-                    res.statusCode = 200;
-                    jsonResponse = '{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage + '": "no RepairQuotation found"}';
-                    res.responseBody = blob.valueOf(jsonResponse);
-                    return;
-                }
-                List<Attachment> attList = [Select Id, Name, Body, ParentId From Attachment Where Id= :repairQ.contract_consent_id__c];
-                Attachment att = null;
-                if (attList.size() > 0) {
-                    att = attList[0];
-                } else {
-                    att = new Attachment();
-                    att.Name = 'ContractConsent.jpg';
-                    att.ParentId = rqId;
-                }
-                att.Body = EncodingUtil.base64Decode(ccImg);
+                Repair_Quotation__c repairQ = null;
                 try {
-                    if (attList.size() > 0) { update att; }
-                    else { insert att; }
-                    ccId = att.Id;
+                    repairQ = [Select Id, contract_consent_id__c From Repair_Quotation__c Where Id= :rqId];  
+                } catch ( Exception ex ) {
+                    returnMessage('{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage + '": "no RepairQuotation found"}',200);
+                }
+                // if (repairQ == null) {
+                //     res.statusCode = 200;
+                //     jsonResponse = '{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage + '": "no RepairQuotation found"}';
+                //     res.responseBody = blob.valueOf(jsonResponse);
+                //     return;
+                // }
+                // 20231103 chenjingwu  Lightning鏂囦欢淇敼 Start
+                // List<Attachment> attList = [Select Id, Name, Body, ParentId From Attachment Where Id= :repairQ.contract_consent_id__c];
+                // Attachment att = null;
+                List<ContentVersion> verisonList = [select Id,Title,VersionData from ContentVersion where Id =: repairQ.contract_consent_id__c];
+                ContentVersion version = null;
+                if (verisonList.size() > 0) {
+                    version = verisonList[0];
+                } else {
+                    // att = new Attachment();
+                    // att.Name = 'ContractConsent.jpg';
+                    // att.ParentId = rqId;
+                    version = new ContentVersion();
+                    version.Title = 'ContractConsent.jpg';
+                    version.ContentLocation = 's';
+                    version.PathOnClient = 'ContractConsent.jpg';
+                }
+                // att.Body = EncodingUtil.base64Decode(ccImg);
+                version.VersionData = EncodingUtil.base64Decode(ccImg);
+                try {
+                    if (verisonList.size() > 0) {
+                        update version; 
+                    }
+                    else {
+                        insert version;
+                        version = [select Id,ContentDocumentId from ContentVersion where Id =: version.Id];
+                        ContentDocumentLink link = new ContentDocumentLink();
+                        link.ContentDocumentId = version.ContentDocumentId;
+                        link.LinkedEntityId = rqId;
+                        link.ShareType = 'I';
+                        link.Visibility = 'AllUsers';
+                        insert link;
+                    }
+                    ccId = version.Id;
                     repairQ.contract_consent_id__c = ccId;
+                    // 20231103 chenjingwu  Lightning鏂囦欢淇敼 End
                 } catch ( Exception ex ) {
                     //TODO:
                     //error message:cannot update exception
-                    res.statusCode = 200;
-                    jsonResponse = '{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage + '": "error when try to upsert ContractConsent Attachment. '+ ex +'"}';
-                    res.responseBody = blob.valueOf(jsonResponse);
-                    return;
+                    returnMessage('{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage + '": "error when try to upsert ContractConsent Attachment. '+ ex +'"}',200);
+                    // res.statusCode = 200;
+                    // jsonResponse = '{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage + '": "error when try to upsert ContractConsent Attachment. '+ ex +'"}';
+                    // res.responseBody = blob.valueOf(jsonResponse);
+                    // return;
                 }
                 
                 try {
@@ -55,35 +87,57 @@
                 } catch ( Exception ex ) {
                     //TODO:
                     //error message:cannot update exception
-                    res.statusCode = 200;
-                    jsonResponse = '{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage + '": "error when try to update RepairQuotation. '+ ex +'"}';
-                    res.responseBody = blob.valueOf(jsonResponse);
-                    return;
+                    returnMessage('{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage + '": "error when try to update RepairQuotation. '+ ex +'"}',200);
+                    // res.statusCode = 200;
+                    // jsonResponse = '{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage + '": "error when try to update RepairQuotation. '+ ex +'"}';
+                    // res.responseBody = blob.valueOf(jsonResponse);
+                    // return;
                 }
             }
             if (String.isNotBlank(acImg)) {
-                List<Attachment> attList = [Select Id, Name, Body, ParentId From Attachment Where Id= :repair.acceptance_id__c];
-                Attachment att = null;
-                if (attList.size() > 0) {
-                    att = attList[0];
+                // 20231103 chenjingwu  Lightning鏂囦欢淇敼 Start
+                // List<Attachment> attList = [Select Id, Name, Body, ParentId From Attachment Where Id= :repair.acceptance_id__c];
+                List<ContentVersion> versionList = [select Id,Title,VersionData from ContentVersion where Id =: repair.acceptance_id__c];
+                // Attachment att = null;
+                ContentVersion version = null;
+                if (versionList.size() > 0) {
+                    version = versionList[0];
                 } else {
-                    att = new Attachment();
-                    att.Name = 'Acceptance.jpg';
-                    att.ParentId = repairId;
+                    // att = new Attachment();
+                    // att.Name = 'Acceptance.jpg';
+                    // att.ParentId = repairId;
+                    version = new ContentVersion();
+                    version.Title = 'Acceptance.jpg';
+                    version.ContentLocation = 's';
+                    version.PathOnClient =  'Acceptance.jpg';
                 }
-                att.Body = EncodingUtil.base64Decode(acImg);
+                // att.Body = EncodingUtil.base64Decode(acImg);
+                version.VersionData = EncodingUtil.base64Decode(acImg);
                 try {
-                    if (attList.size() > 0) { update att; }
-                    else { insert att; }
-                    acId = att.Id;
+                    if (versionList.size() > 0) {
+                        update version; 
+                    }
+                    else {
+                        insert version; 
+                        version = [select Id,ContentDocumentId from ContentVersion where Id =: version.Id];
+                        ContentDocumentLink link = new ContentDocumentLink();
+                        link.LinkedEntityId = repairId;
+                        link.ShareType = 'I';
+                        link.Visibility = 'AllUsers';
+                        link.ContentDocumentId = version.ContentDocumentId;
+                        insert link;
+                    }
+                    acId = version.Id;
                 } catch ( Exception ex ) {
                     //TODO:
                     //error message:cannot update exception
-                    res.statusCode = 200;
-                    jsonResponse = '{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage + '": "error when try to upsert Acceptance Attachment. '+ ex +'"}';
-                    res.responseBody = blob.valueOf(jsonResponse);
-                    return;
+                    returnMessage('{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage + '": "error when try to upsert Acceptance Attachment. '+ ex +'"}',200);
+                    // res.statusCode = 200;
+                    // jsonResponse = '{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage + '": "error when try to upsert Acceptance Attachment. '+ ex +'"}';
+                    // res.responseBody = blob.valueOf(jsonResponse);
+                    // return;
                 }
+                // 20231103 chenjingwu  Lightning鏂囦欢淇敼 End
             }
             
             if (String.isNotBlank(ccId)) {
@@ -111,16 +165,29 @@
             } catch ( Exception ex ) {
                 //TODO:
                 //error message:cannot update exception
-                res.statusCode = 200;
-                jsonResponse = '{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage + '": "error when try to update repair data. '+ ex +'"}';
-                res.responseBody = blob.valueOf(jsonResponse);
-                return;
+                returnMessage('{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage + '": "error when try to update repair data. '+ ex +'"}',200);
+                // res.statusCode = 200;
+                // jsonResponse = '{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage + '": "error when try to update repair data. '+ ex +'"}';
+                // res.responseBody = blob.valueOf(jsonResponse);
+                // return;
             }
         } else {
-            res.statusCode = 200;
-            jsonResponse = '{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage + '": "no repair Id"}';
-            res.responseBody = blob.valueOf(jsonResponse);
-            return;
+            returnMessage('{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage + '": "no repair Id"}',200);
+            // res.statusCode = 200;
+            // jsonResponse = '{"'+ System.Label.OFSErrorStatus +'": "'+ System.Label.OFSErrorFailure +'", "'+ System.Label.OFSErrorMessage + '": "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
 }
\ No newline at end of file

--
Gitblit v1.9.1