/** * @url:/services/apexrest/NFM704/execute * */ @RestResource(urlMapping='/NFM704/*') global with sharing class NFM704Rest { global class GeDatas { public NFMUtil.Monitoring Monitoring; public GeData[] GeData; } public static final String LOG_TYPE = 'NFM704'; global class GeData { public String StaffMCode; //人员管理编码OBPM public String HospitalMCode; //医院管理编码 public String OfficeMCode; //OBPM科室 public String HcpNo; //HPC编码 public String HCPLevel; //HCP等级 public String Name; //姓名 public String Mobile; //电话 public String State; //省 public String City; //市 public String WorkingSeniority; //临床工作年限 public String TechnicalTitle; //技术职称 public String Speciality; //擅长的手术 public String ProfessionalField; //专业领域 public String Society1; //学会名称 public String EventStatus; //学会职称 } @HttpPost global static void doPost() { //取得接口传输内容 String strData = RestContext.request.requestBody.toString(); GeDatas ges = (GeDatas) JSON.deserializeStrict(strData, GeDatas.class); if(ges == null) { return; } NFMUtil.Monitoring Monitoring = ges.Monitoring; if(Monitoring == null ) { return; } BatchIF_Log__c rowData = NFMUtil.saveRowData(Monitoring, LOG_TYPE, ges.GeData); system.debug('String.isBlank(rowData.Log__c) == false?'); if(String.isBlank(rowData.Log__c) == false){ System.debug('进入if方法,成功调用executefuture'); executefuture(rowData.Id); } // JSONを戻す RestResponse res = RestContext.response; res.addHeader('Content-Type', 'application/json'); res.statusCode = 200; String jsonResponse = '{"Result":{"Result": "00", "Message":""}}'; res.responseBody = blob.valueOf(jsonResponse); return; } @future(callout = true) global static void executefuture(String rowData_Id){ main(rowData_Id); } global static void main(String rowData_Id){ String logstr = 'start\n'; BatchIF_Log__c iflog = new BatchIF_Log__c(); iflog.Type__c = LOG_TYPE; iflog.ErrorLog__c = ''; iflog.Log__c = logstr; insert iflog; GeDatas gds = new GeDatas(); gds.GeData = new List< GeData >(); 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 rowDataStr = NFMUtil.getRowDataStr(rowData); List < GeData > itemMasterList = (List < GeData > ) JSON.deserialize(rowDataStr, List < GeData > .class); logstr += '数据总数为:' + itemMasterList.size() + '\n'; if (itemMasterList == null || itemMasterList.size() == 0) { return; } Savepoint sp = Database.setSavepoint(); try{ //管理编码List List DoctorNoList = new List(); //待更新的客户人员List List upsertList = new List(); //医院编码List List HospitalMCodeList = new List(); for(GeData ged : itemMasterList){ String dataComplete = verify(ged); if (!String.isBlank(dataComplete)) { logstr += dataComplete; continue; } DoctorNoList.add(ged.StaffMCode); //officeMCodeList.add(ged.OfficeMCode); HospitalMCodeList.add(ged.HospitalMCode); } system.debug('HospitalMCodeList=============>'+HospitalMCodeList); system.debug('DoctorNoList=============>'+DoctorNoList); //Map> hospitalMap = new Map>(); Map conListMap = new Map(); List conList = [select Id, Management_Code__c, //医院管理编码 CManageCode__c //人员管理编码 from Contact WHERE CManageCode__c IN:DoctorNoList]; system.debug('conList============>'+conList); for(Contact con : conList ){ conListMap.put(con.CManageCode__c, con); //hospitalMap.put(con.Management_Code__c,conListMap); } List accList = [select Id, Name,GI_Main__c,Management_Code__c from Account where Management_Code__c in :HospitalMCodeList]; system.debug('accList=============>'+accList); Map accMap = new Map(); for(Account acc : accList){ accMap.put(acc.Management_Code__c, acc); } for( GeData ged : itemMasterList ){ //Map tempMap = new Map(); if(accMap.containsKey(ged.HospitalMCode)){//医院编码是否存在 是 Contact con = new Contact(); if(conListMap.containsKey(ged.StaffMCode)){ //存在则更新 system.debug('更新联系人'); logstr += conListMap.get(ged.StaffMCode).Id + '\n'; logstr += '已存在客户人员\n'; con.Id = conListMap.get(ged.StaffMCode).Id; system.debug('request'+conListMap.get(ged.StaffMCode).Management_Code__c.equals(ged.HospitalMCode)); if(conListMap.get(ged.StaffMCode).Management_Code__c.equals(ged.HospitalMCode)){//判断联系人上的管理编码和接口传输过来的管理编码是否一致 system.debug('request'+conListMap.get(ged.StaffMCode).Management_Code__c.equals(ged.HospitalMCode)); }else{ con.OBPMHospital__c = accMap.get(ged.HospitalMCode).Id;//接收的管理编码对应的医院写到OBPM医院上 } con = assignment(ged,con); }else{ //不存在则新建 system.debug('新建联系人'); //logstr += '不存在的客户人员\n'; con.OwnerId = accMap.get(ged.HospitalMCode).GI_Main__c;//所有人取医院上的消化科担当 con.AccountId = accMap.get(ged.HospitalMCode).Id;//新建联系人放到医院下 con.LastName = ged.Name; con.FirstName = ''; con = assignment(ged,con); } upsertList.add(con); }else{//否 continue; logstr += '不存在的医院管理编码:['+accMap.get(ged.HospitalMCode)+']/n'; } // //AccountId // if( accMap.get(ged.OfficeMCode) != null ){ // con.AccountId = accMap.get(ged.OfficeMCode).Id; // }else { // logstr += '当前科室id有误或不存在'; // } // con.HcpNo__c = ged.HcpNo; // con.HCPLevel__c = ged.HCPLevel; // con.LastName = ged.Name; // con.MobilePhone = ged.Mobile; // conListMap.put(ged.StaffMCode, con); } system.debug('upsertList==================>'+upsertList); if(upsertList.size() > 0 ){ // Update updateList; Oly_TriggerHandler.bypass('ConsumableOrderTrigger'); UpSert upsertList; } rowData.retry_cnt__c = 0; logstr += '\nend'; }catch (Exception ex) { Integer batch_retry_max_cnt = Integer.valueOf(System.Label.batch_retry_max_cnt); // エラーが発生した場合 Database.rollback(sp); logstr += '\n' + ex.getMessage(); iflog.ErrorLog__c = ex.getMessage() + '\n' + ex.getStackTraceString() + '\n' + iflog.ErrorLog__c; // 异常重发 rowData = NFMUtil.LogAutoSend(rowData, ex, null); } iflog.Log__c = logstr; upsert iflog; } public static String verify(GeData ged){ String result = ''; if (ged.StaffMCode == null) { result += 'DataError: SFDC人员管理编码 [ StaffMCode ] is null!\n'; } if (ged.HospitalMCode == null) { result += 'DataError: 医院管理编码 [ HospitalMCode ] is null!\n'; } // if (ged.OfficeMCode == null) { // result += 'DataError: 科室管理编码 [ OfficeMCode ] is null!\n'; // } if (ged.HcpNo == null) { result += 'DataError: HCP编码 [ HcpNo ] is null!\n'; } if (ged.HCPLevel == null) { result += 'DataError: HCP等级 [ HCPLevel ] is null!\n'; } if (ged.Name == null) { result += 'DataError: 姓名 [ Name ] is null!\n'; } if (ged.Mobile == null) { result += 'DataError: 电话 [ Mobile ] is null!\n'; } return result; } public static Contact assignment(GeData ged,Contact con){//联系人数据赋值方法 List societyList = Breakup(ged.Society1); List eventStatusList = Breakup(ged.EventStatus); con.NameOBPM__c = ged.Name;//姓名 con.MobileOBPM__c = ged.Mobile;//电话 con.stateOBPM__c = ged.State;//省OBPM con.CityOBPM__c = ged.City;//市OBPM con.HcpNo__c = ged.HcpNo;//HPC编码 con.HCPLevel__c = ged.HCPLevel;//HCP等级 con.OBPMDepartment__c = ged.OfficeMCode;//OBPM科室 con.WorkingSeniorityOBPM__c = ged.WorkingSeniority; //临床工作年限 con.TechnicalTitleOBPM__c = ged.TechnicalTitle;//技术职称 con.SpecialityOBPM__c = ged.Speciality;//擅长的手术 con.ProfessionalField__c = ged.ProfessionalField;//专业领域 if(societyList.size()>0){ con.Learn_name1__c = societyList[0];//学会名称1 } if(societyList.size()>1){ con.Learn_name2__c = societyList[1];//学会名称2 } if(societyList.size()>2){ con.Learn_name3__c = societyList[2];//学会名称3 } if(eventStatusList.size()>0){ con.Learn_serve1__c = eventStatusList[0];//学会职称1 } if(eventStatusList.size()>1){ con.Learn_serve2__c = eventStatusList[1];//学会职称2 } if(eventStatusList.size()>2){ con.Learn_serve3__c = eventStatusList[2];//学会职称3 } return con; } public static List Breakup (String str){ // List over = new List(); // over = str.split('↑'); List over = str.split('↑'); return over; } }