高章伟
2022-02-24 2aa8da8af66aa8ae00f25831aed6bb0364176e7b
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
/**********************************************************************
 * 
 *
 * @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;
 
        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) {
                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;
                    }
                    if (productId != null) {
                        //String productSerialNo = rnds.Fixture_Set_Detail__r.Product2__r.Name + '&' + rnds.Fixture_Set_Detail__r.Product2__r.Asset_Model_No__c;
                        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;
 
                            //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;
                } 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;
    }
}