/*****
|
* @createDate:20240305
|
* @createBy:zzm
|
* @Descript:SCM本部 保有设备所属变更 通过接口传输直接提交申请
|
*
|
* @url: /services/apexrest/rest
|
*/
|
@RestResource(urlMapping='/NFM213/*')
|
global with sharing class NFM213Rest {
|
|
global class AssetModifyBelongsRest{
|
public GeDatas GeDatas;
|
}
|
|
global class GeDatas {
|
public NFMUtil.Monitoring Monitoring;
|
public GeData[] GeData;
|
}
|
global class GeData {
|
String rawAccount;
|
String AccountField;
|
String ChangeReason;
|
String remark;
|
String ServiceRequestTrackingNumber;
|
String share;
|
String SPOLink;
|
String ModifyBelongsEmail;
|
String Owner;
|
String [] SerialNumber;
|
}
|
@HttpPost
|
global static void execute() {
|
// 取得接口传输内容
|
String strData = RestContext.request.requestBody.toString();
|
system.debug('strData--' +strData);
|
GeDatas ges = (GeDatas) JSON.deserializeStrict(strData, GeDatas.class);
|
NFMUtil.NFMResponse result = NFMUtil.getNFMResponse();
|
|
if (ges == null ) {
|
return;
|
}
|
|
NFMUtil.Monitoring Monitoring = ges.Monitoring;
|
if (Monitoring == null) {
|
return;
|
}
|
|
BatchIF_Log__c rowData = NFMUtil.saveRowData(Monitoring, 'NFM213', ges);
|
if (String.isBlank(rowData.Log__c) == false) {
|
executefuture(rowData.Id);
|
}
|
RestResponse res = RestContext.response;
|
res.addHeader('Content-Type', 'application/json');
|
res.statusCode = 200;
|
String jsonResponse = '{"status": "Success", "Message":""}';
|
|
system.debug('result---'+jsonResponse);
|
res.responseBody = blob.valueOf(jsonResponse);
|
return;
|
}
|
@future
|
global static void executefuture(String rowData_Id) {
|
main(rowData_Id);
|
}
|
|
global static void main(String rowData_Id) {
|
Integer batch_retry_max_cnt = Integer.valueOf(System.Label.batch_retry_max_cnt);
|
BatchIF_Log__c rowData = [Select Id, Name, Log__c, ErrorLog__c, Log2__c, Log3__c, Log4__c, Log5__c, Log6__c, Log7__c, Log8__c, Log9__c, Log10__c, Log11__c, Log12__c, MessageGroupNumber__c, retry_cnt__c from BatchIF_Log__c where RowDataFlg__c = true and Id = :rowData_Id];
|
String logstr = rowData.MessageGroupNumber__c + ' start\n';
|
BatchIF_Log__c iflog = new BatchIF_Log__c();
|
iflog.Type__c = 'NFM213';
|
iflog.MessageGroupNumber__c = rowData.MessageGroupNumber__c;
|
iflog.Log__c = logstr;
|
iflog.ErrorLog__c = '';
|
insert iflog;
|
|
String rowDataStr = NFMUtil.getRowDataStr(rowData);
|
List<GeData> geDataList = (List<GeData>) JSON.deserialize(rowDataStr, List<GeData>.class);
|
if (geDataList == null || geDataList.size() == 0) {
|
return;
|
}
|
Map<String, String> transferMap = new Map<String, String>();
|
List<BatchIF_Transfer__c> transferList = [select Table__c,
|
Column__c,
|
External_value__c,
|
Internal_value__c
|
from BatchIF_Transfer__c
|
where Dropped_Flag__c = false
|
and (Table__c = 'Account' OR Table__c = 'NFM213Rest')];
|
for (BatchIF_Transfer__c t : transferList) {
|
transferMap.put(t.Column__c + t.External_value__c, t.Internal_value__c);
|
}
|
|
|
Savepoint sp = Database.setSavepoint();
|
try {
|
List<GeData> nfmgd = new List<GeData>();
|
// Map<String,List<String>> complianceDataMap = new Map<String,List<String>>();
|
// List<String> rawAccountCodes = new List<String>();
|
// List<String> accountFieldCodes = new List<String>();
|
|
List<String> accountCodes = new List<String>();
|
List<String> assetcodes = new List<String>();
|
List<String> hospitalIds = new List<String>();
|
List<String> assetIds = new List<String>();
|
Set<String> assetHospitalCodes = new Set<String>();
|
|
//验证必填字段
|
for (GeData gda : geDataList) {
|
if (String.isBlank(gda.rawAccount)) {
|
iflog.ErrorLog__c += 'rawAccount is required,This data is skipped.\n';
|
update iflog;
|
return;
|
}
|
if (gda.SerialNumber==null || gda.SerialNumber.size()==0) {
|
iflog.ErrorLog__c += 'SerialNumber is required,This data is skipped.\n';
|
update iflog;
|
return;
|
}
|
if (String.isBlank(gda.AccountField)) {
|
iflog.ErrorLog__c += 'AccountField is required,This data is skipped.\n';
|
update iflog;
|
return;
|
}
|
if (String.isBlank(gda.Owner)) {
|
iflog.ErrorLog__c += 'Owner is required,This data is skipped.\n';
|
update iflog;
|
return;
|
}
|
if (String.isNotBlank(gda.rawAccount) && String.isNotBlank(gda.AccountField) && gda.SerialNumber.size()>0){
|
accountCodes.add(gda.rawAccount);
|
accountCodes.add(gda.AccountField);
|
// accountFieldCodes.add(gda.AccountField);
|
assetcodes.addAll(gda.SerialNumber);
|
// complianceDataMap.put(gda.rawAccount+':'+gda.AccountField,gda.SerialNumber);
|
// nfmgd.add(gda);
|
// for (String assetNumber : gda.SerialNumber) {
|
// assetHospitalCodes.add(gda.rawAccount+':'+assetNumber);
|
// }
|
}
|
}
|
//客户
|
List<Account> accountList = [Select ID ,AccountManageNumber__c from Account where AccountManageNumber__c in:accountCodes ];
|
Map<String,String> accountMap = new Map<String,String>();
|
for (Account account : accountList) {
|
accountMap.put(account.AccountManageNumber__c, account.Id);
|
}
|
// 保有设备
|
List<Asset> assetAll = new List<Asset>([select Id, SerialNumber,AccountId ,Account.AccountManageNumber__c from Asset where Account.AccountManageNumber__c in : accountCodes and SerialNumber in:assetcodes]);
|
//check 数据的有效性
|
Map<String,Asset> assetMap = new Map<String,Asset>();
|
for(Asset assettmp: assetAll){
|
assetMap.put(assettmp.Account.AccountManageNumber__c+':'+assettmp.SerialNumber,assettmp);
|
}
|
List<AssetModifyBelongsChange__c> insertAmbc = new List<AssetModifyBelongsChange__c>();
|
List<AssetModifyBelongsChangeDetail__c> insertAmbcd = new List<AssetModifyBelongsChangeDetail__c>();
|
for(GeData gda : geDataList) {
|
AssetModifyBelongsChange__c ambcTmp = new AssetModifyBelongsChange__c();
|
ambcTmp.Change_status__c = '已提交';
|
ambcTmp.share__c = gda.share=='true'?true:false;
|
ambcTmp.AccountField__c = accountMap.get(gda.AccountField);
|
ambcTmp.rawAccount__c = accountMap.get(gda.rawAccount);
|
|
for(String serialtmp:gda.SerialNumber) {
|
Asset assettmp = assetMap.get(gda.rawAccount+':'+serialtmp);
|
if(assettmp!=null) {
|
AssetModifyBelongsChangeDetail__c ambcdTmp = new AssetModifyBelongsChangeDetail__c();
|
ambcdTmp.Asset__c = assettmp.Id;
|
ambcdTmp.ChangeAndChangeDetail__c = ambcTmp.Id;
|
|
insertAmbcd.add(ambcdTmp);
|
|
} else {
|
System.debug('保有设备不存在');
|
return;
|
}
|
insertAmbc.add(ambcTmp);
|
}
|
}
|
if(insertAmbc.size()>0) {
|
insert insertAmbc;
|
}
|
if(insertAmbcd.size()>0) {
|
insert insertAmbcd;
|
}
|
|
} catch (Exception ex) {
|
Database.rollback(sp);
|
System.debug(Logginglevel.ERROR, 'NFM213_' + rowData.MessageGroupNumber__c + ':' + ex.getMessage());
|
System.debug(Logginglevel.ERROR, 'NFM213_' + rowData.MessageGroupNumber__c + ':' + ex.getStackTraceString());
|
logstr += '\n' + ex.getMessage();
|
|
iflog.ErrorLog__c = ex.getMessage() + '\n' + ex.getStackTraceString() + '\n' + iflog.ErrorLog__c;
|
|
if (rowData.retry_cnt__c == null) rowData.retry_cnt__c = 0;
|
if (rowData.retry_cnt__c < batch_retry_max_cnt) {
|
rowData.retry_cnt__c++;
|
if(!Test.isRunningTest()){
|
LogAutoSendSchedule.assignOneMinute();
|
}
|
}
|
if (rowData.retry_cnt__c >= batch_retry_max_cnt) {
|
rowData.ErrorLog__c = ex.getMessage() + '\n' + ex.getStackTraceString() + '\n' + rowData.ErrorLog__c + '错误次数已经超过自动收信设定的最大次数,请手动收信';
|
}
|
}
|
update rowData;
|
iflog.Log__c = logstr;
|
if (iflog.Log__c.length() > 131072) {
|
iflog.Log__c = iflog.Log__c.subString(0, 131065) + ' ...';
|
}
|
if (iflog.ErrorLog__c.length() > 32768) {
|
iflog.ErrorLog__c = iflog.ErrorLog__c.subString(0, 32760) + ' ...';
|
}
|
update iflog;
|
}
|
|
|
}
|