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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**********************************************************************
 * 
 *
 * @url: /services/apexrest/UpdateReceivingAssetImageRest
 * @data:
 *  {
        
    }
*************************************************************************/
@RestResource(urlMapping='/UpdateReceivingAssetImageRest')
global without sharing class UpdateReceivingAssetImageRest {
    @HttpPost
    global static void doPost(String imageBase64, String imageType, String rndsId) {
        RestResponse res = RestContext.response;
        res.addHeader('Content-Type', 'application/json');
 
        String imageAssetId, imageSerialId, imageProductId;
        String jsonResponse;
 
        System.debug('收货清单配套一栏明细传入image-------->>>>'+imageBase64);
        if (String.isNotBlank(imageBase64)) {
            ReceivingNoteDetail__c rnds = [select id, 
                                                  ImageSerialBase64__c,
                                                  ImageAssetBase64__c,
                                                  RNDAssert__c,
                                                  Fixture_Set_Detail__r.Product2__c,
                                                  Fixture_Set_Detail__r.Product2__r.Asset_Model_No__c,
                                                  Fixture_Set_Detail__r.Product2__r.Name,
                                                  Fixture_Arrival_Process__c,
                                                  Fixture_Arrival_Product__r.Asset_Model_No__c,
                                                  Fixture_Arrival_Product__r.Name,
                                                  RNDAssert__r.Product2Id,
                                                  RNDAssert__r.Product2.Image__c,
                                                  RNDAssert__r.Product2.Asset_Model_No__c,
                                                  RNDAssert__r.Product2.Name,
                                                  RNDAssert__r.Product_Serial_No__c
                                            from ReceivingNoteDetail__c 
                                            where id = :rndsId];
                                            
            //if (rnds.RNDAssert__c == null) {
                System.debug('imageType数值--------->>>>'+imageType);
                if (imageType == '3' || imageType == '5') {
                    rnds.ImageAssetBase64__c = imageBase64;
                } else {
                    rnds.ImageSerialBase64__c = imageBase64;
                }
 
                // 上传产品照片
                if (imageType == '5') {
                    String productSerialNo = null;
                    String productId = null;
                    if (rnds.Fixture_Arrival_Process__c == '变体') {
                        if (rnds.Fixture_Arrival_Product__c != null) {
                            productSerialNo = rnds.Fixture_Arrival_Product__r.Name + '&' + rnds.Fixture_Arrival_Product__r.Asset_Model_No__c;
                            productId = rnds.Fixture_Arrival_Product__c;
                        }
                    }
                    else if (rnds.Fixture_Set_Detail__r.Product2__c != null) {
                        productSerialNo = rnds.Fixture_Set_Detail__r.Product2__r.Name + '&' + rnds.Fixture_Set_Detail__r.Product2__r.Asset_Model_No__c;
                        productId = rnds.Fixture_Set_Detail__r.Product2__c;
                    }
                    else if (rnds.RNDAssert__r.Product2Id != null) {
                        productSerialNo = rnds.RNDAssert__r.Product2.Name + '&' + rnds.RNDAssert__r.Product2.Asset_Model_No__c;
                        productId = rnds.RNDAssert__r.Product2Id;
                    }
                    System.debug('是否进入判断--------->>>>'+productId);
                    if (productId != null) {
                        //20231017 ymh 修改图片获取储存 start
                        // String productSerialNo = rnds.Fixture_Set_Detail__r.Product2__r.Name + '&' + rnds.Fixture_Set_Detail__r.Product2__r.Asset_Model_No__c;
                        List<ContentVersion> insertVerList = new List<ContentVersion>();
                        //20231103  ymh 优化sql查询  start
                        List<ContentVersion> versionList =[SELECT Id,VersionData,Title,ContentDocumentId From ContentVersion WHERE Title =: productSerialNo and FirstPublishLocationId =: System.Label.ProductImageFolder_New];
                        // List<ContentVersion> versionList =[SELECT Id,VersionData,Title,ContentDocumentId From ContentVersion WHERE Title =: productSerialNo];
                        ContentVersion version = new ContentVersion();
                        if(versionList.size() > 0){
                            //20231105 ymh 修改文件图片修改  start
                            List<Id> docIdList = new List<Id>();
                            for (ContentVersion versionP : versionList) {
                                docIdList.add(versionP.ContentDocumentId);
                            }
                            List<Document> docList =[SELECT Id FROM Document WHERE Id in: docIdList ];
                            if(docList.size() > 0){
                                delete docList;
                            }
                        }
 
                        // if(versionList.size() > 0){
                        //     List<Id> versionIdList = new List<Id>();
                        //     for (ContentVersion versionP : versionList) {
                        //         versionIdList.add(versionP.ContentDocumentId);
                        //     }
                        //     List<ContentDocumentLink> linkList =[SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId =: System.Label.ProductImageFolder_New and ContentDocumentId in:versionIdList];
                        //     if(linkList.size() > 0){
                        //         version = versionList[0];
                        //     }
                        // }
                        //20231103  ymh 优化sql查询  end
                        // EncodingUtil.base64Decode(imageBase64)   Blob.valueOf(imageBase64);
                        try {
                            System.debug('versionData是否为空--------->>>'+Blob.valueOf(imageBase64));
                            version.VersionData =EncodingUtil.base64Decode(imageBase64);
                            version.Title = productSerialNo;
                            version.PathOnClient = productSerialNo+'.jpg';
 
                        // List<Document > docList = [Select Id, Body, name, folderid, Type From Document Where name =:productSerialNo And folderid =:System.Label.ProductImageFolder];
                        // Document dc;]
                        // if (docList.size() > 0) {
                        //     dc = docList[0];
                        // } else {
                        //     dc = new Document();
                        // }
                        // dc.Body = EncodingUtil.base64Decode(imageBase64);
                        // dc.name = productSerialNo;
                        // dc.folderid = System.Label.ProductImageFolder;
                        // dc.Type = 'jpg';
                        // try {
                            // if (docList.size() > 0) { update dc; }
                            // else { insert dc; }
                            // imageProductId = dc.Id;
                            System.debug('传入versionList--------->>>>:'+versionList);
                            System.debug('传入version--------->>>>:'+version);
                            // if( versionList.size() > 0){
                            //     update version;
                            // }else{
                            insert version;
                            version = [SELECT Id,VersionData,Title,ContentDocumentId From ContentVersion WHERE Id =: version.Id];
                            ContentDocumentLink link = new ContentDocumentLink();
                            link.ContentDocumentId = version.ContentDocumentId;
                            //文档库产品图---
                            link.LinkedEntityId = System.Label.ProductImageFolder_New;
                            link.ShareType = 'I';
                            link.Visibility = 'AllUsers';
                            StaticParameter.ContentDocumentLink = false;
                            insert link;
                            // }
                            //20231105 ymh 修改文件图片修改  end
                            //20231104  ymh  修改imageProductId start
                            imageProductId = version.Id;
                            //20231104  ymh  修改imageProductId end
                            //20231025  ymh 修改附件存储  end
 
                            //String productId = rnds.Fixture_Set_Detail__r.Product2__c;
                            Product2 prd = new Product2(id = productId, Image_DocumentID__c = imageProductId, Image_Width__c = 320, Image_Height__c = 320);
                            update prd;
                        } catch ( Exception ex ) {
                            //TODO:
                            //error message:cannot update exception
                            res.statusCode = 200;
                            jsonResponse = '{"status": "Failure", "message": "error when try to upsert Product Image. '+ ex +'"}';
                            res.responseBody = blob.valueOf(jsonResponse);
                            return;
                        }
                    }
                }
                 //else {
                try {
                    update rnds;
                    if(System.Test.isRunningTest()){
                        throw new ControllerUtil.myException('测试错误!');
                    }
                } catch ( Exception ex ) {
                    //TODO:
                    //error message:cannot update exception
                    res.statusCode = 200;
                    jsonResponse = '{"status": "Failure", "message": "error when try to upsert ReceivingNoteDetail. '+ ex +'"}';
                    res.responseBody = blob.valueOf(jsonResponse);
                    return;
                }
                //}
            //} 
            //else {
            //    String productSerialNo = rnds.RNDAssert__r.Product_Serial_No__c;
            //    if (imageType == '3' || imageType == '5') {
            //        List<Document > docList = [Select Id, Body, name, folderid, Type From Document Where name =:(productSerialNo + '_Asset') And folderid =:System.Label.AssetImageFolder];
            //        Document dc;
            //        Document prdc;
            //        if (docList.size() > 0) {
            //            dc = docList[0];
            //        } else {
            //            dc = new Document();
            //        }
            //        dc.Body = EncodingUtil.base64Decode(imageBase64);
            //        dc.name = productSerialNo + '_Asset';
            //        dc.folderid = System.Label.AssetImageFolder;
            //        dc.Type = 'jpg';
 
            //        if (rnds.RNDAssert__r.Product2.Image__c == null) {
            //            prdc = dc.clone();
            //            String productDcName;
            //            if (rnds.RNDAssert__r.Product2Id != null) {
            //                productDcName = rnds.RNDAssert__r.Product2.Name + '&' + rnds.RNDAssert__r.Product2.Asset_Model_No__c;
            //            } else {
            //                productDcName = rnds.Fixture_Set_Detail__r.Product2__r.Name + '&' + rnds.Fixture_Set_Detail__r.Product2__r.Asset_Model_No__c;
            //            }
            //            prdc.name = productDcName;
            //            prdc.folderid = System.Label.ProductImageFolder;
            //        }
            //        try {
            //            if (docList.size() > 0) { update dc; }
            //            else { insert dc; }
            //            imageAssetId = dc.Id;
 
            //            if (prdc != null) { 
            //                insert prdc; 
            //                imageProductId = prdc.Id;
            //            }
            //        } catch ( Exception ex ) {
            //            //TODO:
            //            //error message:cannot update exception
            //            res.statusCode = 200;
            //            jsonResponse = '{"status": "Failure", "message": "error when try to upsert Asset Doc. '+ ex +'"}';
            //            res.responseBody = blob.valueOf(jsonResponse);
            //            return;
            //        }
            //    } else {
            //        List<Document > docList = [Select Id, Body, name, folderid, Type From Document Where name =:(productSerialNo + '_Serial') And folderid =:System.Label.AssetImageFolder];
            //        Document dc;
            //        if (docList.size() > 0) {
            //            dc = docList[0];
            //        } else {
            //            dc = new Document();
            //        }
               
            //        dc.Body = EncodingUtil.base64Decode(imageBase64);
            //        dc.name = productSerialNo + '_Serial';
            //        dc.folderid = System.Label.AssetImageFolder;
            //        dc.Type = 'jpg';
            //        try {
            //            if (docList.size() > 0) { update dc; }
            //            else { insert dc; }
            //            imageSerialId = dc.Id;
            //        } catch ( Exception ex ) {
            //            //TODO:
            //            //error message:cannot update exception
            //            res.statusCode = 200;
            //            jsonResponse = '{"status": "Failure", "message": "error when try to upsert Serial Doc. '+ ex +'"}';
            //            res.responseBody = blob.valueOf(jsonResponse);
            //            return;
            //        }
            //    }
 
            //    List<Asset> assetList = [Select Id, ImageAsset__c, ImageSerial__c From Asset Where id = :rnds.RNDAssert__c];
            //    if (assetList.size() == 0) {
            //        res.statusCode = 200;
            //        jsonResponse = '{"status": "Failure", "message": "no asset found"}';
            //        res.responseBody = blob.valueOf(jsonResponse);
            //        return;
            //    } else {
            //        Product2 prd = null;
            //        Asset asset = assetList[0];
            //        if (String.isNotBlank(imageAssetId)) {
            //            asset.ImageAsset__c = '<img style="width:320px" src="/servlet/servlet.FileDownload?file=' + imageAssetId + '"/>';
            //            asset.ImageAssetUploadedBy__c = UserInfo.getUserId();
            //            asset.ImageAssetUploadedTime__c = Datetime.now();
 
            //            if (String.isNotBlank(imageProductId)) {
            //                String productId = rnds.RNDAssert__r.Product2Id;
            //                prd = new Product2(id = productId, Image_DocumentID__c = imageProductId, Image_Width__c = 320, Image_Height__c = 320);
            //            }
            //        }
            //        if (String.isNotBlank(imageSerialId)) {
            //            asset.ImageSerial__c = '<img style="width:320px" src="/servlet/servlet.FileDownload?file=' + imageSerialId + '"/>';
            //            asset.ImageSerialUploadedBy__c = UserInfo.getUserId();
            //            asset.ImageSerialUploadedTime__c = Datetime.now();
            //        }
            //        try {
            //            update asset;
            //            if (prd != null) update prd;
            //        } catch ( Exception ex ) {
            //            //TODO:
            //            //error message:cannot update exception
            //            res.statusCode = 200;
            //            jsonResponse = '{"status": "Failure", "message": "error when try to update asset data. '+ ex +'"}';
            //            res.responseBody = blob.valueOf(jsonResponse);
            //            return;
            //        }
            //    }
            //}
        }
        res.statusCode = 200;
        jsonResponse = '{"status": "Success", "message": "updated!"}';
        res.responseBody = blob.valueOf(jsonResponse);
        return;
    }
}