/********************************************************************** * * * @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 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 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 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 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 = ''; // 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 = ''; // 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; } }