public with sharing class RepairAgainAnHandler extends Oly_TriggerHandler{ private Map newMap; private Map oldMap; private List newList; private List oldList; public RepairAgainAnHandler() { this.newMap = (Map) Trigger.newMap; this.oldMap = (Map) Trigger.oldMap; this.newList = (List) Trigger.new; this.oldList = (List) Trigger.old; } protected override void beforeInsert() { Set repairSet = new Set(); for(RepairAgainAn__c ra : newList){ repairSet.add(ra.Repair__c); } Map repairMap = new Map([SELECT Id,Name,SAP_Service_Repair_No__c,(select Id from reparirAgain01__r where RepairAgain_Status__c != '取消') from Repair__c where id in :repairSet]); //Map repairConplateMap = new Map([SELECT Id,Name,SAP_Service_Repair_No__c,(select Id from reparirAgain01__r) from Repair__c where id in :repairSet]); for(RepairAgainAn__c ra : newList){ //修理没上传SAP之前,不能新建 再修理分析 System.debug('=======repairMap' + repairMap); System.debug('=======newList' + newList); //已存在再修理分析的修理不能新建 存在取消的也可以新建再修理 if( repairMap.containsKey(ra.Repair__c) && repairMap.get(ra.Repair__c).reparirAgain01__r.size() >= 1 ){ ra.addError('该修理已存在再修理返品分析,不能再新建再修理返品分析'); }else if(repairMap.containsKey(ra.Repair__c) && repairMap.get(ra.Repair__c).SAP_Service_Repair_No__c == null){ ra.addError('该修理还没有上传SAP,不能够新建再修理分析'); }else{ setPhotoImgText(); setRepairAgainNameAndReport(); } } } protected override void beforeUpdate() { setPhotoImgText(); setStatusChange(); //提交审批之后更新字段 updateExaminePeople(); //附件更新 //updateAttachment(); } protected override void afterUpdate(){ submitRepair(); //发送邮件 sendIssueEmail(); } public static Set RA_Ids = new Set(); //再注册分析 需求 新增字段 20220811 sx start public static List openRepairAgainRepairIds = new List(); public static List oSHRepairAgainRepairIds = new List(); public static List oSHWrittenRepairAgainRepairIds = new List(); public static List finalRepairAgainRepairIds = new List(); public static List cleanRepairIds = new List(); //再注册分析 需求 新增字段 20220811 sx end private void sendIssueEmail(){ List repairAgainIds = new List(); List repairAgainIdsSH = new List(); List repairAgainIdsGZ = new List(); for(RepairAgainAn__c ra : newList){ //一次二次通过都发送邮件通知对应的OCSM // TODO 发行投诉邮件在workflow,审批通过邮件在审批流,后续业务确认后再统一整合 if(ra.RepairAgainAn_Approve_status__c != oldMap.get(ra.Id).RepairAgainAn_Approve_status__c && ra.RepairAgainAn_Approve_status__c == '批准'){ repairAgainIds.add(ra.Id); }else if(ra.RepairAgainAn_Approve_status2__c != oldMap.get(ra.Id).RepairAgainAn_Approve_status2__c && ra.RepairAgainAn_Approve_status2__c == '批准'){ repairAgainIds.add(ra.Id); } //工厂/RC发起再注册申请时,给对应的用户发送邮件提醒 if(ra.RepairAgain_Status__c == 'RC填写完毕' && oldMap.get(ra.Id).RepairAgain_Status__c != 'RC填写完毕') { if (ra.SORC__c == 'OSH-SH') { repairAgainIdsSH.add(ra.Id); } else if (ra.SORC__c == 'OSH-GZ') { repairAgainIdsGZ.add(ra.Id); } } } /*if (!repairAgainIds.isEmpty()) { email(repairAgainIds, '再修理分析审批通过', System.Label.RepairAgainAn_Issue_Sendmail); }*/ if (!repairAgainIdsSH.isEmpty()) { email(repairAgainIdsSH, '工厂/RC发起再注册申请通知', System.Label.SendRepairAgainAn_OSH_SH); } if (!repairAgainIdsGZ.isEmpty()) { email(repairAgainIdsGZ, '工厂/RC发起再注册申请通知', System.Label.SendRepairAgainAn_OSH_GZ); } } private void email(List neadUpdateList, String emailTemplateName, String toEmailAddress){ EmailTemplate et=[Select id from EmailTemplate where name =: emailTemplateName limit 1]; String[] toAddresses = toEmailAddress.split(';'); List emails = new List(); for(Id repairAgainId: neadUpdateList){ Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail = Messaging.renderStoredEmailTemplate(et.Id, null , repairAgainId); mail.setSaveAsActivity(false); mail.setToAddresses(toAddresses); emails.add(mail); } Messaging.sendEmail(emails); } private void setRepairAgainNameAndReport() { Date todayTemp = Date.today(); Set repairSet = new Set(); for(RepairAgainAn__c ra : newList){ repairSet.add(ra.Repair__c); // 报告者、报告日自动设值,创建者自动设置 ra.Reporter__c = UserInfo.getUserId(); ra.Report_Date__c = todayTemp; } Map repairMap = new Map([SELECT Id,Name,Repair_Detail__c,InspectionFailureCause__c,SerialNumber__c,Delivered_Product__c,Incharge_Staff__c,(select Id from reparirAgain01__r) from Repair__c where id in :repairSet]); System.debug('repairMap========='+repairMap); for(RepairAgainAn__c ra : newList){ if(repairMap.containsKey(ra.Repair__c)){ Integer repairSize = repairMap.get(ra.Repair__c).reparirAgain01__r.size() + 1; String repairSizeString = '000' + repairSize; String cutString = repairSizeString.substring(repairSizeString.length()-3, repairSizeString.length()); if(repairSize == 0) { ra.Name = repairMap.get(ra.Repair__c).Name + '_001'; }else{ ra.Name = repairMap.get(ra.Repair__c).Name + '_' + cutString; } //修理上字段带过来 客户确认 = 修理.问题内容描述 RC确认 = 修理.检测故障内容 机身号码 ra.RC_Describe__c = repairMap.get(ra.Repair__c).InspectionFailureCause__c; ra.Customer_Describe__c = repairMap.get(ra.Repair__c).Repair_Detail__c; ra.SerialNumber1__c = repairMap.get(ra.Repair__c).SerialNumber__c; ra.Delivered_Product__c = repairMap.get(ra.Repair__c).Delivered_Product__c; ra.Incharge_Staff__c = repairMap.get(ra.Repair__c).Incharge_Staff__c; } } } //通过审批流判断确认者和审核人员的值 private void updateExaminePeople(){ for(RepairAgainAn__c ram : newList){ RepairAgainAn__c ramOld = oldMap.get(ram.Id); if(ram.OSH_Confirmation_Date__c != ramOld.OSH_Confirmation_Date__c && ram.OSH_Confirmation_Date__c != null){ //审批提交日修改 OSH确认者 = 操作人员 ram.OSH_Confirmer__c = UserInfo.getUserId(); }else if(ram.RepairAgainAn_Approve_Confirm__c != ramOld.RepairAgainAn_Approve_Confirm__c && ram.RepairAgainAn_Approve_Confirm__c != null){ //OSH审批完了日修改 OSH审核人员 = 操作人员 ram.OSH_Auditor__c = UserInfo.getUserId(); }else if(ram.OSH_Confirmation_Date2__c != ramOld.OSH_Confirmation_Date2__c && ram.OSH_Confirmation_Date2__c != null){ //审批提交日(二次)修改 OSH确认者(二次) = 操作人员 ram.OSH_Confirmer2__c = UserInfo.getUserId(); }else if(ram.RepairAgainAn_Approve_Confirm2__c != ramOld.RepairAgainAn_Approve_Confirm2__c && ram.RepairAgainAn_Approve_Confirm2__c != null){ //OSH审批完了日(二次)修改 OSH审核人员(二次) = 操作人员 ram.OSH_Auditor2__c = UserInfo.getUserId(); } // OCSM确认者 if (ram.OCSM_Approve_Status__c == '批准' && ram.OCSM_Approve_Status__c != ramOld.OCSM_Approve_Status__c) { ram.OCM_Confirmer__c = UserInfo.getUserId(); } } } private void setPhotoImgText() { for (RepairAgainAn__c ra : newList) { System.debug('ra.Dectect_Picture1__c2:' + ra.Dectect_Picture1__c); // 禁止重复执行 if (RA_Ids.contains(ra.Id)) continue; // 本次报价检测图片1 if (Trigger.isAfter && Trigger.isInsert && String.isBlank(ra.Dectect_Picture1__c) == false || (Trigger.isUpdate && ((RepairAgainAn__c) oldMap.get(ra.Id)).Dectect_Picture1__c != ra.Dectect_Picture1__c) || (Trigger.isUpdate && String.isBlank(ra.Dectect_Picture1__c) == false && String.isBlank(ra.Dectect_Picture1_URL__c)) ) { String phtTxt = ''; if (String.isBlank(ra.Dectect_Picture1__c) == false) { Integer pStt = ra.Dectect_Picture1__c.indexOf('= 0) { pStt = ra.Dectect_Picture1__c.indexOf('src="'); if (pStt >= 0) { Integer pEnd = ra.Dectect_Picture1__c.indexOf('"', pStt + 5); if (pEnd >= 0) { phtTxt = ra.Dectect_Picture1__c.substring(pStt + 5, pEnd); System.debug('ra.Dectect_Picture1__c1:' + ra.Dectect_Picture1__c); System.debug('phtTxt1:' + phtTxt); //富文本反转义 phtTxt = phtTxt.unescapeHtml4(); System.debug('phtTxt2:' + phtTxt); } } } } ra.Dectect_Picture1_URL__c = phtTxt; RA_Ids.add(ra.Id); } // 本次报价检测图片2 if (Trigger.isAfter && Trigger.isInsert && String.isBlank(ra.Dectect_Picture2__c) == false || (Trigger.isUpdate && ((RepairAgainAn__c) oldMap.get(ra.Id)).Dectect_Picture2__c != ra.Dectect_Picture2__c) || (Trigger.isUpdate && String.isBlank(ra.Dectect_Picture2__c) == false && String.isBlank(ra.Dectect_Picture2_URL__c)) ) { String phtTxt = ''; if (String.isBlank(ra.Dectect_Picture2__c) == false) { Integer pStt = ra.Dectect_Picture2__c.indexOf('= 0) { pStt = ra.Dectect_Picture2__c.indexOf('src="'); if (pStt >= 0) { Integer pEnd = ra.Dectect_Picture2__c.indexOf('"', pStt + 5); if (pEnd >= 0) { phtTxt = ra.Dectect_Picture2__c.substring(pStt + 5, pEnd); //富文本反转义 phtTxt = phtTxt.unescapeHtml4(); } } } } ra.Dectect_Picture2_URL__c = phtTxt; RA_Ids.add(ra.Id); } // 本次报价检测图片3 if(Trigger.isAfter && Trigger.isInsert && String.isBlank(ra.Dectect_Picture3__c) == false || (Trigger.isUpdate && ((RepairAgainAn__c) oldMap.get(ra.Id)).Dectect_Picture3__c != ra.Dectect_Picture3__c) || (Trigger.isUpdate && String.isBlank(ra.Dectect_Picture3__c) == false && String.isBlank(ra.Dectect_Picture3_URL__c))){ ra.Dectect_Picture3_URL__c = setPictureUrl(ra.Dectect_Picture3__c); RA_Ids.add(ra.Id); } // 本次报价检测图片4 if(Trigger.isAfter && Trigger.isInsert && String.isBlank(ra.Dectect_Picture4__c) == false || (Trigger.isUpdate && ((RepairAgainAn__c) oldMap.get(ra.Id)).Dectect_Picture4__c != ra.Dectect_Picture4__c) || (Trigger.isUpdate && String.isBlank(ra.Dectect_Picture4__c) == false && String.isBlank(ra.Dectect_Picture4_URL__c))){ ra.Dectect_Picture4_URL__c = setPictureUrl(ra.Dectect_Picture4__c); RA_Ids.add(ra.Id); } // OSH技术检测图片1 if (Trigger.isInsert && String.isBlank(ra.OSH_Dectect_Picture1__c) == false || (Trigger.isUpdate && ((RepairAgainAn__c) oldMap.get(ra.Id)).OSH_Dectect_Picture1__c != ra.OSH_Dectect_Picture1__c) || (Trigger.isUpdate && String.isBlank(ra.OSH_Dectect_Picture1__c) == false && String.isBlank(ra.OSH_Dectect_Picture1_URL__c)) ) { String phtTxt = ''; if (String.isBlank(ra.OSH_Dectect_Picture1__c) == false) { Integer pStt = ra.OSH_Dectect_Picture1__c.indexOf('= 0) { pStt = ra.OSH_Dectect_Picture1__c.indexOf('src="'); if (pStt >= 0) { Integer pEnd = ra.OSH_Dectect_Picture1__c.indexOf('"', pStt + 5); if (pEnd >= 0) { phtTxt = ra.OSH_Dectect_Picture1__c.substring(pStt + 5, pEnd); System.debug('ra.OSH_Dectect_Picture1__c1:' + ra.OSH_Dectect_Picture1__c); System.debug('phtTxt3:' + phtTxt); //富文本反转义 phtTxt = phtTxt.unescapeHtml4(); System.debug('phtTxt4:' + phtTxt); } } } } ra.OSH_Dectect_Picture1_URL__c = phtTxt; RA_Ids.add(ra.Id); } // OSH技术检测图片2 if (Trigger.isInsert && String.isBlank(ra.OSH_Dectect_Picture2__c) == false || (Trigger.isUpdate && ((RepairAgainAn__c) oldMap.get(ra.Id)).OSH_Dectect_Picture2__c != ra.OSH_Dectect_Picture2__c) || (Trigger.isUpdate && String.isBlank(ra.OSH_Dectect_Picture2__c) == false && String.isBlank(ra.OSH_Dectect_Picture2_URL__c)) ) { String phtTxt = ''; if (String.isBlank(ra.OSH_Dectect_Picture2__c) == false) { Integer pStt = ra.OSH_Dectect_Picture2__c.indexOf('= 0) { pStt = ra.OSH_Dectect_Picture2__c.indexOf('src="'); if (pStt >= 0) { Integer pEnd = ra.OSH_Dectect_Picture2__c.indexOf('"', pStt + 5); if (pEnd >= 0) { phtTxt = ra.OSH_Dectect_Picture2__c.substring(pStt + 5, pEnd); //富文本反转义 phtTxt = phtTxt.unescapeHtml4(); } } } } ra.OSH_Dectect_Picture2_URL__c = phtTxt; RA_Ids.add(ra.Id); } // OSH技术检测图片3 if (Trigger.isInsert && String.isBlank(ra.OSH_Dectect_Picture3__c) == false || (Trigger.isUpdate && ((RepairAgainAn__c) oldMap.get(ra.Id)).OSH_Dectect_Picture3__c != ra.OSH_Dectect_Picture3__c) || (Trigger.isUpdate && String.isBlank(ra.OSH_Dectect_Picture3__c) == false && String.isBlank(ra.OSH_Dectect_Picture3_URL__c)) ) { String phtTxt = ''; if (String.isBlank(ra.OSH_Dectect_Picture3__c) == false) { Integer pStt = ra.OSH_Dectect_Picture3__c.indexOf('= 0) { pStt = ra.OSH_Dectect_Picture3__c.indexOf('src="'); if (pStt >= 0) { Integer pEnd = ra.OSH_Dectect_Picture3__c.indexOf('"', pStt + 5); if (pEnd >= 0) { phtTxt = ra.OSH_Dectect_Picture3__c.substring(pStt + 5, pEnd); //富文本反转义 phtTxt = phtTxt.unescapeHtml4(); } } } } ra.OSH_Dectect_Picture3_URL__c = phtTxt; RA_Ids.add(ra.Id); } // OSH技术检测图片4 if (Trigger.isInsert && String.isBlank(ra.OSH_Dectect_Picture4__c) == false || (Trigger.isUpdate && ((RepairAgainAn__c) oldMap.get(ra.Id)).OSH_Dectect_Picture4__c != ra.OSH_Dectect_Picture4__c) || (Trigger.isUpdate && String.isBlank(ra.OSH_Dectect_Picture4__c) == false && String.isBlank(ra.OSH_Dectect_Picture4_URL__c)) ) { String phtTxt = ''; if (String.isBlank(ra.OSH_Dectect_Picture4__c) == false) { Integer pStt = ra.OSH_Dectect_Picture4__c.indexOf('= 0) { pStt = ra.OSH_Dectect_Picture4__c.indexOf('src="'); if (pStt >= 0) { Integer pEnd = ra.OSH_Dectect_Picture4__c.indexOf('"', pStt + 5); if (pEnd >= 0) { phtTxt = ra.OSH_Dectect_Picture4__c.substring(pStt + 5, pEnd); //富文本反转义 phtTxt = phtTxt.unescapeHtml4(); } } } } ra.OSH_Dectect_Picture4_URL__c = phtTxt; RA_Ids.add(ra.Id); } // OSH技术检测图片5 if(Trigger.isInsert && String.isBlank(ra.OSH_Dectect_Picture5__c) == false || (Trigger.isUpdate && ((RepairAgainAn__c) oldMap.get(ra.Id)).OSH_Dectect_Picture5__c != ra.OSH_Dectect_Picture5__c) || (Trigger.isUpdate && String.isBlank(ra.OSH_Dectect_Picture5__c) == false && String.isBlank(ra.OSH_Dectect_Picture5_URL__c)) ){ ra.OSH_Dectect_Picture5_URL__c = setPictureUrl(ra.OSH_Dectect_Picture5__c); RA_Ids.add(ra.Id); } // OSH技术检测图片6 if(Trigger.isInsert && String.isBlank(ra.OSH_Dectect_Picture6__c) == false || (Trigger.isUpdate && ((RepairAgainAn__c) oldMap.get(ra.Id)).OSH_Dectect_Picture6__c != ra.OSH_Dectect_Picture6__c) || (Trigger.isUpdate && String.isBlank(ra.OSH_Dectect_Picture6__c) == false && String.isBlank(ra.OSH_Dectect_Picture6_URL__c)) ){ ra.OSH_Dectect_Picture6_URL__c = setPictureUrl(ra.OSH_Dectect_Picture6__c); RA_Ids.add(ra.Id); } // OSH技术检测图片7 if(Trigger.isInsert && String.isBlank(ra.OSH_Dectect_Picture7__c) == false || (Trigger.isUpdate && ((RepairAgainAn__c) oldMap.get(ra.Id)).OSH_Dectect_Picture7__c != ra.OSH_Dectect_Picture7__c) || (Trigger.isUpdate && String.isBlank(ra.OSH_Dectect_Picture7__c) == false && String.isBlank(ra.OSH_Dectect_Picture7_URL__c)) ){ ra.OSH_Dectect_Picture7_URL__c = setPictureUrl(ra.OSH_Dectect_Picture7__c); RA_Ids.add(ra.Id); } // OSH技术检测图片8 if(Trigger.isInsert && String.isBlank(ra.OSH_Dectect_Picture8__c) == false || (Trigger.isUpdate && ((RepairAgainAn__c) oldMap.get(ra.Id)).OSH_Dectect_Picture8__c != ra.OSH_Dectect_Picture8__c) || (Trigger.isUpdate && String.isBlank(ra.OSH_Dectect_Picture8__c) == false && String.isBlank(ra.OSH_Dectect_Picture8_URL__c)) ){ ra.OSH_Dectect_Picture8_URL__c = setPictureUrl(ra.OSH_Dectect_Picture8__c); RA_Ids.add(ra.Id); } } } public String setPictureUrl(String pic){ String phtTxt = ''; if(String.isBlank(pic) == false) { Integer pStt = pic.indexOf('= 0){ pStt = pic.indexOf('src="'); if(pStt >= 0){ Integer pEnd = pic.indexOf('"', pStt + 5); if(pEnd >= 0){ phtTxt = pic.substring(pStt + 5, pEnd); //富文本反转义 phtTxt = phtTxt.unescapeHtml4(); } } } } return phtTxt; } public void setStatusChange() { Map rtmaps = new Map(); List rtIds = [SELECT Id,Name FROM RecordType where SobjectType='RepairAgainAn__c']; Date todayTemp = Date.today(); for (RecordType rt : rtIds) { rtmaps.put(rt.Name, rt.Id); } for (RepairAgainAn__c raa : newList) { RepairAgainAn__c raaOld = oldMap.get(raa.id); //状态变更为草案中 if (String.isBlank(raa.RepairAgain_Status__c)) { raa.RepairAgain_Status__c = '草案中'; } //当RC的内容填写完毕 if(raa.RepairAgain_Status__c == 'RC填写完毕' && raaOld.RepairAgain_Status__c != 'RC填写完毕') { raa.RecordTypeId = rtmaps.get('2.OSH'); // 发起再注册分析时间 raa.Start_RepairAgainAn_Day__c = todayTemp; // 和SAP交互 OPEN openRepairAgainRepairIds.add(raa.Repair__c); } //当OSH受理 if(raa.RepairAgain_Status__c == 'OSH受理' && raaOld.RepairAgain_Status__c != 'OSH受理') { // OSH受理日 raa.OSH_Accept_Day__c = todayTemp; // 和SAP交互 oSHRepairAgainRepairIds.add(raa.Repair__c); } //当OSH内容填写完毕 if(raa.RepairAgain_Status__c == 'OSH填写完毕' && raaOld.RepairAgain_Status__c != 'OSH填写完毕') { raa.RecordTypeId = rtmaps.get('3.OCSM'); // 和SAP交互 oSHWrittenRepairAgainRepairIds.add(raa.Repair__c); } //当OCSM跟进完毕 if(raa.RepairAgain_Status__c == '完毕' && raaOld.RepairAgain_Status__c != '完毕') { raa.RecordTypeId = rtmaps.get('4.Final完毕'); // 和SAP交互 finalRepairAgainRepairIds.add(raa.Repair__c); } //当再修理状态变成取消时 if(raa.RepairAgain_Status__c == '取消' && raaOld.RepairAgain_Status__c != '取消'){ cleanRepairIds.add(raa.Repair__c); } } } public void submitRepair(){ List repairUpdateList = new List(); if(openRepairAgainRepairIds.size() > 0 || oSHRepairAgainRepairIds.size() > 0 || oSHWrittenRepairAgainRepairIds.size() > 0 || finalRepairAgainRepairIds.size() > 0 || cleanRepairIds.size() > 0){ BatchIF_Log__c iflog = new BatchIF_Log__c(); iflog.Type__c = 'NFM103'; iflog.Log__c = 'callout start\n'; insert iflog; if(openRepairAgainRepairIds.size() > 0) { setRepairList(openRepairAgainRepairIds, repairUpdateList, 'OCSM已发起'); //关联一个open的再修理分析,传X 2023/3/30 NFM103Controller.callout 参数方法有误暂注释 NFM103Controller.callout(iflog.Id, openRepairAgainRepairIds,'OCSM已发起'); } if(oSHRepairAgainRepairIds.size() > 0 ){ setRepairList(oSHRepairAgainRepairIds, repairUpdateList, '品保已受理'); NFM103Controller.callout(iflog.Id, oSHRepairAgainRepairIds,'品保已受理'); } if(oSHWrittenRepairAgainRepairIds.size() > 0 ){ setRepairList(oSHWrittenRepairAgainRepairIds, repairUpdateList, '品保出结论'); NFM103Controller.callout(iflog.Id, oSHWrittenRepairAgainRepairIds,'品保出结论'); } if(finalRepairAgainRepairIds.size() > 0) { setRepairList(finalRepairAgainRepairIds, repairUpdateList, 'OCSM出结论'); //关联再修理分析完毕,传F NFM103Controller.callout(iflog.Id, finalRepairAgainRepairIds,'OCSM出结论'); } if(cleanRepairIds.size() > 0 ){ setRepairList(cleanRepairIds, repairUpdateList, ''); } if(repairUpdateList.size() > 0 ){ Update repairUpdateList; } } } public void setRepairList(List repairIdList, list repairList, String status){ for(Id repairId : repairIdList){ Repair__c tempRepair = new Repair__c(); tempRepair.Id = repairId; tempRepair.RepairAgainAn_Status__c = status; repairList.add(tempRepair); } } // public void updateAttachment(){ // for(RepairAgainAn__c ram : newList){ // String pid = ram.Id; // String sql = 'SELECT Name,Body,ContentType from Attachment where ParentId =\'' + pid + '\''; // Attachment att = Database.query(sql); // System.debug('bName====='+att.Body); // } // } }