global class AuthRentalApplySplitBatch implements Database.Batchable,Database.Stateful { private static final Map DEVELOPERNAMEMAP = Schema.SObjectType.Rental_Apply__c.getRecordTypeInfosByDeveloperName();//记录类型 private Boolean isFirst = true; public List raIdList; private BatchIF_Log__c iflog; private static String iflogLog3 = ''; // 借用机会可视化-根据邮箱地址去执行分单分配 2024-1-9 zyh add private String raMail; // 借用机会可视化-根据邮箱地址去执行分单分配 2024-1-9 zyh add global AuthRentalApplySplitBatch() { } // 借用机会可视化-根据邮箱地址去执行分单分配 2024-1-9 zyh start global AuthRentalApplySplitBatch(String mailAddress) { if (mailAddress == 'BJ') { mailAddress = System.Label.BeijingEmail; } if (mailAddress == 'SH') { mailAddress = System.Label.ShanghaiEmai; } if (mailAddress == 'GZ') { mailAddress = System.Label.GuangzhouEmail; } this.raMail = mailAddress; } // 借用机会可视化-根据邮箱地址去执行分单分配 2024-1-9 zyh end global AuthRentalApplySplitBatch(List raIdList) { System.debug('zyhtest====Batch'); this.raIdList = raIdList; } global Database.QueryLocator start(Database.BatchableContext bc) { iflog = new BatchIF_Log__c(); iflog.Type__c = 'AuthRentalApplySplitBatch'; iflog.Log__c = 'AuthRentalApplySplitBatch start\n'; iflog.Log2__c = '申请单Id start:\n'; iflog.Log3__c = '申请单分配Id start:\n'; iflog.ErrorLog__c = ''; insert iflog; String query = 'SELECT Id, Status__c, Request_shipping_day__c, Person_In_Charge__r.Email, Rental_Assistant__r.Email, Demo_purpose1__c,RecordType.DeveloperName,Loaner_centre_mail_address__c from Rental_Apply__c'; query += ' WHERE Status__c != \'完了\' AND Status__c != \'取消\' AND Status__c != \'删除\' AND Rental_Status__c = \'已批准\' AND RecordType.DeveloperName = \'StandardRequest\' AND Request_shipping_day__c <= NEXT_N_DAYS:7 AND Demo_purpose1__c != \'其他\' AND Request_approval_time__c != NULL AND Cross_Region_Assign__c = null'; // 借用机会可视化-根据邮箱地址去执行分单分配 2024-1-9 zyh start if (String.isNotBlank(this.raMail)) { query += ' AND Loaner_centre_mail_address__c =: raMail'; } // 借用机会可视化-根据邮箱地址去执行分单分配 2024-1-9 zyh end if (this.raIdList != null && this.raIdList.size() > 0) { query += ' AND Id IN :raIdList ORDER BY OPD_OrderNum__c NULLS LAST,OPD_OrderNum__c,Request_approval_time__c'; // 20231213-zyh-借用机会可视化==按照OPD排序推送 System.debug('testzyh===='+query); } else{ query += ' ORDER BY OPD_OrderNum__c NULLS LAST,OPD_OrderNum__c,Request_approval_time__c'; // 添加OPD排队Num&维修代用等 } System.debug('zyhsql===='+query); return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, list scope) { List raIdList = new List(); for (Rental_Apply__c ra : scope) { raIdList.add(ra.Id); iflog.Log2__c += raIdList + ',\n'; System.debug('1213===raIdList'+ra.Id); } System.debug('zyhtest=====fendan:'+raIdList); if (raIdList.size() > 0) { if (String.isNotBlank(scope[0].Loaner_centre_mail_address__c) && scope[0].Loaner_centre_mail_address__c == System.Label.ShanghaiEmai) { iflog.Log3__c += raIdList + ',\n'; if (isFirst) { toFp(raIdList,isFirst); // System.enqueueJob(new RentalFixtureSetAssignAndQueueWebService(raIdList)); isFirst = false; } else { toFp(raIdList,isFirst); } } else { if (isFirst) { toSplitRentalApply(raIdList,isFirst); isFirst = false; } else { toSplitRentalApply(raIdList,isFirst); } } // toSplitRentalApply(raIdList); } iflog.Log3__c += AuthRentalApplySplitBatch.iflogLog3; System.debug('AuthRentalApplySplitBatch.iflogLog3=====3' + AuthRentalApplySplitBatch.iflogLog3); } global void finish(Database.BatchableContext BC) { iflog.Log__c += '\nAuthRentalApplySplitBatch end'; String tmp = iflog.ErrorLog__c; if (tmp.length() > 65000) { tmp = tmp.substring(0, 65000); tmp += ' ...have more lines...'; iflog.ErrorLog__c = tmp; } update iflog; } public static void toSplitRentalApply(List raIdList,Boolean isFirst) { System.debug('AuthRentalApplySplitBatch.iflogLog3=====1' + AuthRentalApplySplitBatch.iflogLog3); Map> raIdMap = new Map>(); // 用于存放需要分单的一览 Map raIdMap1 = new Map(); // 用于存放需要分单的申请单=>用于判断是否是整单 Map raSpIdMap = new Map(); List raesFdObjList = new List(); // 存放分单之后再分配的一览 List raesFdToFpObjList = new List(); // 存放分单之后再分配的一览 List raesFdToFpList = new List(); // 存放分单之后再分配的一览Id List raesNoFdToFpList = new List(); // 存放可以直接分配的一览Id List raNoFdToFpList = new List(); // 存放可以直接分配的申请单(未去重) List raToFpList = new List(); // 存放可以直接分配的申请单(去重) List opdSpIdList = new List(); // 用于存放需要分单的OPD的Id List raesIdList = new List(); //用于存放需要部分分单的一览Id List raSpIdList = new List(); //用于存放需要克隆的申请单Id List raUpList = new List(); //用于全部分单更新记录类型等 List raZdIdList = new List(); // 存放整单分单的申请单ID List assetList = new List(); // 用于存放查询出来的保有设备 Map assetKeyMap = new Map(); // 用于存放保有设备的一对多数量 List assetModList = new List(); // 用于存放设备型号 List assetAddList = new List(); // 用于存放备品存放地 List raes_dIdList = new List(); // 用于存放没有分配的申请单Id Map raFpMap = new Map(); // 用于存放To自动分配的 Map assetModelMap = new Map(); Map assetAddMap = new Map(); List allFpList = new List(); Map noFpMap = new Map(); Map okFpMap = new Map(); // List raSpList = // 查找需要推送的一览 Rental_Apply_Equipment_Set_Cnt__c => 申请单的一览数 List raesSpList = new List(); List raesOkList = new List(); // Id List raesList = [SELECT Id,Name,Rental_Apply__c,Fixture_Set__c,Fixture_Set__r.Loaner_categoryII__c,Rental_Apply__r.WorkPlace__c,Rental_Apply__r.OPD_OrderNum__c FROM Rental_Apply_Equipment_Set__c WHERE (RAES_Status__c = '待分配' OR RAES_Status__c = '排队中' OR RAES_Status__c = '暂定分配' OR RAES_Status__c = '已分配' OR RAES_Status__c = '已出库指示' OR RAES_Status__c = '已下架' OR RAES_Status__c = '出库前已检测') AND Rental_Apply__c IN :raIdList ORDER BY Rental_Apply__r.OPD_OrderNum__c]; System.debug('zyhtest=====raesList.size()' + raesList.size()); if(raesList.size() > 0){ for (Rental_Apply_Equipment_Set__c tempObj : raesList) { raes_dIdList.add(tempObj.Id); } // 判断有没有分配 if (raes_dIdList.size() > 0) { allFpList = [SELECT Id,Asset__c,Rental_Apply_Equipment_Set__c,EquipmentSet_Detail_Status_Status__c FROM Rental_Apply_Equipment_Set_Detail__c WHERE Rental_Apply_Equipment_Set__c IN :raes_dIdList]; if (allFpList.size() > 0) { for (Rental_Apply_Equipment_Set_Detail__c mx : allFpList) { // 如果明细分配了,就跳过,没分配的再去分单 if (String.isNotBlank(mx.Asset__c) && mx.EquipmentSet_Detail_Status_Status__c != '待废弃') { // 如果已分配的map里包含了,那么就跳过 if (okFpMap.containsKey(mx.Rental_Apply_Equipment_Set__c)) { continue; } else { // 如果一览里,既有没分配的,也有分配了的,那么把没分配的map里的数据删了,加到已分配的map里 if (noFpMap.containsKey(mx.Rental_Apply_Equipment_Set__c)){ noFpMap.remove(mx.Rental_Apply_Equipment_Set__c); // 删除没分配map里的数据 okFpMap.put(mx.Rental_Apply_Equipment_Set__c,mx); // 新增已分配map里的数据 } else { okFpMap.put(mx.Rental_Apply_Equipment_Set__c,mx); } } } else { // 如果未分配的map里包含了,那么就跳过 if (noFpMap.containsKey(mx.Rental_Apply_Equipment_Set__c)) { continue; } // 如果已分配的map里没包含,就加到未分配的map里 if (!okFpMap.containsKey(mx.Rental_Apply_Equipment_Set__c)) { noFpMap.put(mx.Rental_Apply_Equipment_Set__c,mx); } } } // 未分配的往下走自动分单/分配,已分配的不管 for (String raesId : noFpMap.keySet()) { raesOkList.add(raesId); // 存放未分配的(可不可以分单的都有) } } } if(raesOkList.size() > 0){ raesFdObjList = [SELECT Id,Name,Rental_Apply__c,Fixture_Set__c,Fixture_Set__r.Loaner_categoryII__c,Loaner_code_F__c,Rental_Apply__r.WorkPlace__c,Rental_Apply__r.Salesdept__c,Rental_Apply__r.OPD_OrderNum__c,Rental_Num__c,First_RAESD_Is_Main__c FROM Rental_Apply_Equipment_Set__c WHERE (RAES_Status__c = '待分配' OR RAES_Status__c = '排队中' OR RAES_Status__c = '暂定分配' OR RAES_Status__c = '已分配' OR RAES_Status__c = '已出库指示' OR RAES_Status__c = '已下架' OR RAES_Status__c = '出库前已检测') AND Id IN :raesOkList ORDER BY Rental_Apply__r.OPD_OrderNum__c]; } // for (Rental_Apply_Equipment_Set__c raesNum : raesList) { // 2023-10-28 zyh 更换遍历的list(优化) for (Rental_Apply_Equipment_Set__c raesNum : raesFdObjList) { // 将不是北上广深的台车、监视器并且有主体的一览组成List进行自动分单 if (raesNum.First_RAESD_Is_Main__c && !raesNum.Rental_Apply__r.WorkPlace__c.contains('北京') && !raesNum.Rental_Apply__r.WorkPlace__c.contains('上海') && !raesNum.Rental_Apply__r.WorkPlace__c.contains('广州') && !raesNum.Rental_Apply__r.WorkPlace__c.contains('深圳') && !raesNum.Rental_Apply__r.WorkPlace__c.contains('安徽') && !raesNum.Rental_Apply__r.WorkPlace__c.contains('福建') && !raesNum.Rental_Apply__r.WorkPlace__c.contains('江苏') && !raesNum.Rental_Apply__r.WorkPlace__c.contains('江西') && !raesNum.Rental_Apply__r.WorkPlace__c.contains('浙江') && !raesNum.Rental_Apply__r.Salesdept__c.contains('华东') && (raesNum.Fixture_Set__r.Loaner_categoryII__c == '台车' || raesNum.Fixture_Set__r.Loaner_categoryII__c == '监视器')){ assetModList.add(raesNum.Loaner_code_F__c); assetAddList.add(raesNum.Rental_Apply__r.WorkPlace__c); assetAddMap.put(raesNum.Id,raesNum.Rental_Apply__r.WorkPlace__c); assetModelMap.put(raesNum.Id,raesNum.Loaner_code_F__c); raesFdToFpObjList.add(raesNum); System.debug('zyhtest===raesNoFdToFpList:jinlaifendan'); System.debug('zyhtest===assetModList:jinlaifendan'+assetModList); System.debug('zyhtest===assetModList:jinlaifendan'+assetAddList); } else { raesNoFdToFpList.add(raesNum.Id); // 不符合分单条件的未分配的直接走自动分配 } System.debug('zyhtest===raesNoFdToFpList:'+raesNoFdToFpList); } if (assetModList.size() > 0) { // 查询对应的保有设备 assetList = [SELECT Id,Name,Internal_asset_location__c,Fixture_Model_No_F__c,You_Xiao_Ku_Cun__c FROM Asset WHERE AssetManageConfirm__c = true AND Internal_asset_location__c IN :assetAddList AND Fixture_Model_No_F__c IN :assetModList]; System.debug('zyhtest===assetList'+assetList); System.debug('zyhtest===assetList'+assetList.size()); if (assetList.size() > 0) { for (Asset asset : assetList) { // 查询保有设备的总有效库存 if (assetKeyMap.containsKey(asset.Internal_asset_location__c + asset.Fixture_Model_No_F__c)){ Decimal num = assetKeyMap.get(asset.Internal_asset_location__c + asset.Fixture_Model_No_F__c) + asset.You_Xiao_Ku_Cun__c; assetKeyMap.put(asset.Internal_asset_location__c + asset.Fixture_Model_No_F__c , num); } else { assetKeyMap.put(asset.Internal_asset_location__c + asset.Fixture_Model_No_F__c , asset.You_Xiao_Ku_Cun__c); } } System.debug('zyhtest===assetKeyMap'+assetKeyMap); } } // for(Rental_Apply_Equipment_Set__c raes : raesList){ // 2023-10-28 zyh 更换遍历的list(优化) for(Rental_Apply_Equipment_Set__c raes : raesFdToFpObjList){ // 遍历走自动分单的List // 判断是否是北、上、广、深的单子,不是的话自动分单,是的话自动分配 if (!raes.Rental_Apply__r.WorkPlace__c.contains('北京') && !raes.Rental_Apply__r.WorkPlace__c.contains('上海') && !raes.Rental_Apply__r.WorkPlace__c.contains('广州') && !raes.Rental_Apply__r.WorkPlace__c.contains('深圳') && !raes.Rental_Apply__r.WorkPlace__c.contains('安徽') && !raes.Rental_Apply__r.WorkPlace__c.contains('福建') && !raes.Rental_Apply__r.WorkPlace__c.contains('江苏') && !raes.Rental_Apply__r.WorkPlace__c.contains('江西') && !raes.Rental_Apply__r.WorkPlace__c.contains('浙江') && !raes.Rental_Apply__r.Salesdept__c.contains('华东')){ System.debug('zyhtest===raes.Rental_Num__c'+raes.Rental_Num__c); System.debug('zyhtest===assetKeyMap.get(raes.Rental_Apply__r.WorkPlace__c + raes.Loaner_code_F__c)'+assetKeyMap.get(raes.Rental_Apply__r.WorkPlace__c + raes.Loaner_code_F__c)); // 判断是否有库存,有的话自动分单,没有的话自动分配 // 把需要的数量换成1,每个一览只有一个配套 20231210 zyh start // if (raes.Rental_Num__c <= assetKeyMap.get(raes.Rental_Apply__r.WorkPlace__c + raes.Loaner_code_F__c)) { if (1 <= assetKeyMap.get(raes.Rental_Apply__r.WorkPlace__c + raes.Loaner_code_F__c)) { // 确认分单后减去一分单的数量 // assetKeyMap.put(raes.Rental_Apply__r.WorkPlace__c + raes.Loaner_code_F__c , assetKeyMap.get(raes.Rental_Apply__r.WorkPlace__c + raes.Loaner_code_F__c) - raes.Rental_Num__c); assetKeyMap.put(raes.Rental_Apply__r.WorkPlace__c + raes.Loaner_code_F__c , assetKeyMap.get(raes.Rental_Apply__r.WorkPlace__c + raes.Loaner_code_F__c) - 1); // 把需要的数量换成1,每个一览只有一个配套 20231210 zyh end // clone申请单 // 判定需要分单的配套,台车/监视器才可以分单 System.debug('zyhtest=====raes.Fixture_Set__r.Loaner_categoryII__c' + raes.Fixture_Set__r.Loaner_categoryII__c + raes.Id); if (raes.Fixture_Set__r.Loaner_categoryII__c == '台车' || raes.Fixture_Set__r.Loaner_categoryII__c == '监视器') { if(raIdMap.containsKey(raes.Rental_Apply__c)) { raIdMap.get(raes.Rental_Apply__c).add(raes); System.debug('zyhtest====raIdMap1'+raIdMap); } else{ List raesUpList = new List(); raesUpList.add(raes); raIdMap.put(raes.Rental_Apply__c,raesUpList); System.debug('zyhtest====raIdMap2'+raIdMap); System.debug('zyhtest====raIdMap2size'+raIdMap.size()); } raSpIdList.add(raes.Rental_Apply__c); raesSpList.add(raes); System.debug('zyhtest====raSpIdList'+raSpIdList.size()); System.debug('zyhtest====raSpIdList'+raSpIdList); System.debug('zyhtest====raIdMap.values()'+raIdMap.values()); System.debug('zyhtest====raIdMap.values()'+raIdMap); raes_dIdList.add(raes.Id); } } else { raesNoFdToFpList.add(raes.Id); // 存放有效库存不足而不能分单的一览 } } // raesIdList.add(ra.Id); } if(raSpIdList.size() > 0){ List raSpList = getRa(raSpIdList).clone(); // 用于存放需要克隆的申请单 List raSpZcList = new List(); // 用于存放要新建的申请单 List raSpOpdList = new List(); // 存放OPD来源的申请单 List raRootIdList = new List(); // 用于存放源申请单ID Map> raRootMap = new Map>(); // 用于一对多存放要分单的申请单/数量 Map raOPDMap = new Map(); // 存放一对一的OPDID和申请单ID Integer num = 0; // 用于命名_num // 需要分单的申请单创建 if (raSpList.size() > 0) { // 遍历要克隆的申请单 for (Rental_Apply__c spRa : raSpList) { // 判断是否有源申请单,没有的话赋Id if (String.isNotBlank(spRa.Root_Rental_Apply__c)) { raRootIdList.add(spRa.Root_Rental_Apply__c); } else { raRootIdList.add(spRa.Id); } raIdMap1.put(spRa.Id,spRa); System.debug('zyhtest====raIdMap1====' + raIdMap1); System.debug('zyhtest====raIdMap1.size()====' + raIdMap1.size()); } if(raRootIdList.size() > 0){ System.debug('zyhtest====raRootIdList:' + raRootIdList.size()); System.debug('zyhtest====raRootIdList:' + raRootIdList); List raRootList = [SELECT Id,Name,Root_Rental_Apply__c FROM Rental_Apply__c WHERE Root_Rental_Apply__c IN :raRootIdList]; System.debug('zyhtest====raRootList:' + raRootList); System.debug('zyhtest====raRootList:' + raRootList.size()); // 有源申请单的批量做成一对多的Map if(raRootList.size() > 0){ for (Rental_Apply__c rootRa : raRootList) { System.debug('zyhtest====rootRa.Root_Rental_Apply__c'+rootRa.Root_Rental_Apply__c); // 源申请单有值的话判断Map里是否含有,没有的话value定义一个List if(String.isNotBlank(rootRa.Root_Rental_Apply__c)){ if(raRootMap.containsKey(rootRa.Root_Rental_Apply__c)){ raRootMap.get(rootRa.Root_Rental_Apply__c).add(rootRa.Id); }else{ List rootList = new List(); rootList.add(rootRa.Id); raRootMap.put(rootRa.Root_Rental_Apply__c,rootList); } }else{ raRootMap.put(rootRa.Id,null); } } } } System.debug('zyhtest=====raRootMap:'+raRootMap); for (Rental_Apply__c raObj : raSpList) { raObj.Split_Apply_Reason__c = '现地管理分单'; raObj.RecordTypeId = DEVELOPERNAMEMAP.get('AgencyRequest').getRecordTypeId(); // 记录类型 raObj.SalesdeptSelect__c = raObj.Salesdept__c; // 申请者销售本部 raObj.ToAgency__c = getOcmMgtProvMap().get(raObj.WorkPlace__c); // 分配办事处 // 判断有没有源申请单 if(String.isNotBlank(raObj.Root_Rental_Apply__c)){ raObj.Root_Rental_Apply__c = raObj.Root_Rental_Apply__c; // 源申请单 System.debug('zyhtest=====Root_Rental_Apply__c:'+raObj.Root_Rental_Apply__c); }else { raObj.Root_Rental_Apply__c = raObj.Id; // 源申请单 System.debug('zyhtest=====Root_Rental_Apply__c1:'+raObj.Root_Rental_Apply__c); } // 判断是整单分单还是部分分单 System.debug('zyhtest=====raIdMap.get(raObj.Id).size()='+raIdMap.get(raObj.Id).size()); System.debug('zyhtest=====raIdMap1.get(raObj.Id).Rental_Apply_Equipment_Set_Cnt__c='+raIdMap1.get(raObj.Id).Rental_Apply_Equipment_Set_Cnt__c); if(raIdMap.get(raObj.Id).size() == raIdMap1.get(raObj.Id).Rental_Apply_Equipment_Set_Cnt__c){ // 如果是整单,就更新记录类型等 System.debug('zyhtest=====Update'); raObj.Id = raObj.Id; raObj.Root_Rental_Apply__c = ''; // 整单不需要有源申请单 raZdIdList.add(raObj.Id); // 整单分单的申请单的ID raUpList.add(raObj); }else{ // 如果是部分分单,就新建申请单 System.debug('zyhtest=====Insert'); // 借用机会可视化-判断是不是分单后的数据 20240113 zyh start // if(raRootMap.get(raObj.Id) == null){ if(raRootMap.get(raObj.Old_Rental_Apply__c) == null){ // 不是分单的没有旧申请单 // 可以根据当前申请单Id去做分单No后缀判断 if(raRootMap.get(raObj.Id) == null){ num = 1; } else { num = raRootMap.get(raObj.Id).size() + 1; } raObj.Name = raObj.Name + '_' + num; }else{ // num = raRootMap.get(raObj.Id).size() + 1; // 是分单数据的话根据原申请单去判断分单No后缀 num = raRootMap.get(raObj.Root_Rental_Apply__c).size() + 1; raObj.Name = raObj.Old_Rental_Apply__r.Name + '_' + num; } // raObj.Name = raObj.Name + '_' + num; // raObj.Name = raObj.Old_Rental_Apply__r.Name + '_' + num; // 借用机会可视化-判断是不是分单后的数据 20240113 zyh end raObj.Old_Rental_Apply__c = raObj.Id; raObj.autoSplitContinue__c = true; // if (String.isNotBlank(raObj.AWS_Data_Id__c)) { // raObj.AWS_Data_Id__c = raObj.AWS_Data_Id__c + 'X' + num; // } else { // raObj.AWS_Data_Id__c = raObj.Name.subString(17,22) + 'X' + num; // } // raObj.AWS_Data_Id__c = raObj.AWS_Data_Id__c + 'X' + num; raObj.Id = null; if (raObj.OPDPlan__c != null) { // 存放OPD来源的OPDId opdSpIdList.add(raObj.OPDPlan__c); // 存放OPD来源的申请单 raSpOpdList.add(raObj); }else { // 存放其他来源的申请单 raSpZcList.add(raObj); } } } System.debug('zyhtest=====Insert_opdSpIdList' + opdSpIdList); System.debug('zyhtest=====Insert_raSpOpdList' + raSpOpdList.size()); if (opdSpIdList.size() > 0) { List opdCloneList = getOpd(opdSpIdList).clone(); List opdCloneList1 = new List(); Map opdSpMap = new Map(); for (OPDPlan__c opdObj : opdCloneList) { opdObj.OriginalOpdPlanRental__c = opdObj.Id; opdObj.Status__c = '计划中'; opdObj.Id = null; opdCloneList1.add(opdObj); } if (opdCloneList1.size() > 0){ System.debug('zyhtest=====opdCloneList1'+opdCloneList1); FixtureUtil.withoutInsert(opdCloneList1); for (OPDPlan__c opdObj : opdCloneList1) { raOPDMap.put(opdObj.OriginalOpdPlanRental__c, opdObj.Id); } if (raOPDMap.size() > 0) { for (Rental_Apply__c raopd : raSpOpdList) { raopd.OPDPlan__c = raOPDMap.get(raopd.OPDPlan__c); } } System.debug('zyhtest=====opdCloneList1==1'+opdCloneList1); } } // 整单分单更新 if (raUpList.size() > 0) { FixtureUtil.withoutUpdate(raUpList); List raesdZdList = [SELECT Id, Rental_Apply_Equipment_Set__c,Asset__c,Rental_Apply__c FROM Rental_Apply_Equipment_Set_Detail__c WHERE Rental_Apply__c in :raZdIdList]; if (raesdZdList.size() > 0) { for (Rental_Apply_Equipment_Set_Detail__c raesdObj : raesdZdList) { // 清空排队信息 zyh start raesdObj.Queue_Conment__c = null; raesdObj.Queue_Day__c = null; raesdObj.Queue_Time__c = null; raesdObj.Queue_User__c = null; // raesdObj.Queue_Day_Text__c = null;// 20210903 you SFDC-C6E3WQ raesdObj.Queue_Number__c = null; // 清空排队信息 zyh end } FixtureUtil.withoutUpdate(raesdZdList); } } // 部分分单新建 if (raSpZcList.size() > 0 || raSpOpdList.size() > 0) { System.debug('zyhtest=====fendan2-1:'+raSpZcList); raSpZcList.addAll(raSpOpdList); System.debug('zyhtest=====fendan2-2:'+raSpZcList); FixtureUtil.withoutInsert(raSpZcList); // 创建分单后的单子 System.debug('zyhtest=====fendan3:'+raSpZcList); // Map> raseSpUpMap = new Map>(); // 多单情况 Map raseSpUpMap = new Map(); // 一对一 for (Rental_Apply__c spRa : raSpZcList) { // 多单情况start // if (raseSpUpMap.containsKey(spRa.Old_Rental_Apply__c)){ // raseSpUpMap.get(spRa.Old_Rental_Apply__c).add(spRa.Id); // } else { // List tempList = new List(); // tempList.add(spRa.Id); // raseSpUpMap.put(spRa.Old_Rental_Apply__c,tempList); // } // 多单情况end raseSpUpMap.put(spRa.Old_Rental_Apply__c,spRa.Id); // 一对一 raNoFdToFpList.add(spRa.Old_Rental_Apply__c); System.debug('zyhtest=====raseSpUpMap'+raseSpUpMap); } if (raseSpUpMap.size() > 0){ List tempUpRaesList = new List(); // List raesIdList = new List(); for (Rental_Apply_Equipment_Set__c tempUpRaes : raesSpList){ System.debug('zyhtest=====raesSpList'+raesSpList.size()); if (raseSpUpMap.containsKey(tempUpRaes.Rental_Apply__c)){ tempUpRaes.Old_Rental_Apply__c = tempUpRaes.Rental_Apply__c; tempUpRaes.Rental_Apply__c = raseSpUpMap.get(tempUpRaes.Rental_Apply__c); tempUpRaesList.add(tempUpRaes); raesIdList.add(tempUpRaes.Id); System.debug('zyhtest=====tempUpRaesList'+tempUpRaesList); System.debug('zyhtest=====tempUpRaesList'+tempUpRaesList.size()); } } if(raesIdList.size() > 0){ List raesdList = [SELECT Id, Rental_Apply_Equipment_Set__c,Asset__c,Rental_Apply__c FROM Rental_Apply_Equipment_Set_Detail__c WHERE Rental_Apply_Equipment_Set__c in :raesIdList]; if(raesdList.size() > 0){ for(Rental_Apply_Equipment_Set_Detail__c raesdObj : raesdList){ if (String.isNotBlank(raesdObj.Rental_Apply__c)) { raesdObj.Rental_Apply__c = raseSpUpMap.get(raesdObj.Rental_Apply__c); // 清空排队信息 zyh start raesdObj.Queue_Conment__c = null; raesdObj.Queue_Day__c = null; raesdObj.Queue_Time__c = null; raesdObj.Queue_User__c = null; // raesdObj.Queue_Day_Text__c = null;// 20210903 you SFDC-C6E3WQ raesdObj.Queue_Number__c = null; // 清空排队信息 zyh end } } } FixtureUtil.withoutUpdate(raesdList); // 更新明细 } if(tempUpRaesList.size() > 0){ FixtureUtil.withoutUpdate(tempUpRaesList); // 更新一览 } } } } } if (raesNoFdToFpList.size() > 0) { List raFpList = [SELECT Id,Name,Rental_Apply__c FROM Rental_Apply_Equipment_Set__c WHERE Id IN :raesNoFdToFpList]; for (Rental_Apply_Equipment_Set__c fpRa : raFpList) { raNoFdToFpList.add(fpRa.Rental_Apply__c); // 最终自动分配的申请单的Id(未去重) AuthRentalApplySplitBatch.iflogLog3 += fpRa.Rental_Apply__c + ',\n'; } // 借用机会可视化优化-2024-1-12 zyh start // for (String str : raNoFdToFpList) { // if (raFpMap.containsKey(str)) { // continue; // } else { // raFpMap.put(str , str); // } // } // 借用机会可视化优化-2024-1-12 zyh end } // for (String str : raFpMap.keySet()) { // raToFpList.add(str); // } // System.debug('zyhtest=====fendan2:'+raIdMap); } system.debug('zyhtest=====fenpeiold:' + raNoFdToFpList); // if (raToFpList.size() > 0) { if (raNoFdToFpList.size() > 0) { system.debug('zyhtest=====fenpei:' + raNoFdToFpList); // 借用机会可视化 - 单独分出调用分配的方法 2024-01-10 zyh start toFp(raNoFdToFpList,isFirst); // 借用机会可视化 - 单独分出调用分配的方法 2024-01-10 zyh end //自动分配raToFpList // 借用机会可视化 触发器走异步 zyh 20231225 start // if (!System.isBatch()) { // // 不是Batch调用,同步进行分配 20231209 zyh add // RentalFixtureSetAssignAndQueueWebService.setAssignAndQueue(raToFpList); // } else { // 借用机会可视化 触发器走异步 zyh 20231225 end // 借用机会可视化 - 单独分出调用分配的方法 2024-01-10 zyh start // 借用机会可视化--延迟推送分配/排队--zyh--start // DateTime startHold = Datetime.now(); // DateTime endHold = startHold.addSeconds(60); // System.debug('20231212-isFirst-end'+isFirst); // // System.debug('20231212-whileOut-start'+startHold); // // System.debug('20231214-isFirst-start'+Datetime.now()+AuthRentalApplySplitBatch.isFirst); // if (isFirst) { // System.debug('** isFirst'); // if (!Test.isRunningTest()) { // System.enqueueJob(new RentalFixtureSetAssignAndQueueWebService(raToFpList)); // } // // System.enqueueJob(new RentalFixtureSetAssignAndQueueWebService(raToFpList)); // } else { // System.debug('**Not isFirst'); // while (Datetime.now() < endHold) { // // System.debug('20231212-while'+Datetime.now()); // } // if (!Test.isRunningTest()) { // System.enqueueJob(new RentalFixtureSetAssignAndQueueWebService(raToFpList)); // } // } // System.debug('20231212-whileOut-nowD'+Datetime.now()); // Batch调用,异步进行分配 20231209 zyh add // 借用机会可视化 - 单独分出调用分配的方法 2024-01-10 zyh end // 借用机会可视化--延迟推送分配/排队--zyh--start // AuthRentalApplySplitBatch.isFirst = false; // System.debug('20231214-isFirst-end'+Datetime.now()+AuthRentalApplySplitBatch.isFirst); // 借用机会可视化 触发器走异步 zyh 20231225 start // } // 借用机会可视化 触发器走异步 zyh 20231225 end system.debug('zyhtest=====raToFpList:' + raNoFdToFpList.size()); // RentalFixtureSetAssignAndQueueWebService rfsAndQ = new RentalFixtureSetAssignAndQueueWebService(); // rfsAndQ.setAssignAndQueue(raToFpList); } } /** * @description 获取新建申请单时需要复制的字段 **/ private static List getRa(List idList) { return [SELECT Id, OwnerId, Status__c, Cancel_Reason__c, Old_Rental_Apply__c, Old_Rental_Apply__r.Name, Split_Apply_Reason__c, // ------ 备品借出申请の詳細 ------ Name, // 申请No. ToAgency__c, // 分配办事处 20201117 ljh add SalesdeptSelect__c, // 申请者销售本部 20201117 ljh add Salesdept__c, // 申请者销售本部 WorkPlace__c, // 申请者办事处 ApplyPerson_Phone__c, // 申请人电话 Loaner_centre_mail_address__c, // 备品中心的邮箱地址 Select_Status__c, // 分配状态 Queue_Status__c, // 排队状态 Rental_Assistant__c, // 备品助理 RA_Status__c, // 备品借出状态 // OLY_OCM-504 不拷贝追加备品审批状态 //Add_Approval_Status__c, // 追加备品审批状态 Person_In_Charge__c, // 备品出借担当 applyUser__c, // 操作者 Foul_Points__c, // 备品出借担当累计犯规点数 // First_Satisfied__c, // 首满 loaner_Status__c, // 出库状态 Cross_Region_Assign__c, // 跨区域分配 // ------ 备品借用方 ------ Hospital__c, // 医院 Strategic_dept__c, // 战略科室 OCM_dept_category__c, // 战略科室分类 Account__c, // 科室 OCM_segmentation__c, // OCM分类 Loaner_medical_Staff__c, // 科室负责人 Phone_number__c, // 联系电话 Phone_Number_Encrypt__c, // 20220401 PI COPY by Bright // ------ 借用备品的目的 ------ Demo_purpose1__c, // 使用目的1 demo_purpose2__c, // 使用目的2 // OLY_OCM-669 Start Hope_Lonaer_date_Num__c, // 希望借用天数 // OLY_OCM-669 End Product_category__c, // 产品分类(GI/SP) Campaign__c, // 学会 //OPD__c, // OPD报告书 2021-12-28 mzy OPD计划状态改善 OPD__c, // 20231030 备品智能化 zyh QIS_number__c, // QIS QIS_ID_Line__c, // QIS的ID值 QISRepair__c, // QIS修理 Demo_purpose_text__c, // 申请理由 Repair__c, // 修理 Follow_UP_Opp__c, // 跟进询价1 Follow_pcl_status2__c, // 跟进询价状态 Follow_pcl_status2_Text__c, // 跟进询价状态(申请时) Follow_Inquiry_Level_Text__c, // 询价等级(申请时) // 20211008 ljh SWAG-C7LCH2 Follow_Inquiry_Stage_Text__c, // 询价阶段(申请时) // 20211008 ljh SWAG-C7LCH2 Statu_Achievements__c, // 注残 Shipping_Finished_Day_Func__c, // 最终发货日(已购待货) CrinicalTrialName__c, // 临床研究项目名称 DB_loaner_request__c, // MA本部审核合同编号 // ------ 备品希望借出期限 ------ Request_shipping_day__c, // 希望到货日 Return_dadeline_final__c, // 最新预定归还日 Request_return_day__c, // 预定归还日 Lonaer_date_not_working_date__c, // 希望借用天数 // ------ 发送信息 ------ Shipment_address__c, // 办事处地址 Dealer__c, // 经销商 Loaner_received_staff__c, // 收件者姓名 (销售或FSE) Shippment_adress_detail__c, // 发送办事处地址详细 direct_shippment_address__c, // 直送发送地址 Direct_Shippment_Address_Encrypt__c, // 20220401 PI COPY by Bright direct_send__c, // 发送方 pickup_time__c, // 自提时间 Loaner_received_staff_phone__c, // 收件者电话(销售或FSE) Post_Code__c, // 邮编 // ApprovedNo_Delivery__c, // 决裁编号(发货时) 20220224 ljh add // Approved_State_Delivery__c, // 决裁状态(发货时)20220224 ljh add ApprovedNo_Create__c, // 决裁编号(创建时) 20220301 ljh add Approved_State_Create__c, // 决裁状态(创建时) 20220301 ljh add Response__c, // 应答沟通 Request_demo_time__c, // 申请时间 Request_approval_time__c, // 批准时间(申请提交时间) Request_answer_time__c, // 备品首次分配时间 Application_accept_time__c, // 应答沟通申请受理时间(回答时间) OPDPlan__c, // OPD计划 RecordTypeId, // 记录类型 1732 ljh add 20210528 Root_Rental_Apply__c, // 源申请单(root) 1732 ljh add 20210609 First_level_actual_approver__c, //一级实际审批人 20210908 ljh SFDC-C6A97F add Second_level_actual_approver__c, //二级实际审批人 20210908 ljh SFDC-C6A97F add OPDManager__c, // 20210926 ljh add SFDC-C6A97F add OPDBuchang__c, // 20220722 zyh add SWAG-CGG7X9 add Rental_Apply_Equipment_Set_Cnt__c, // 20231025 zyh add NoDirectReason__c, // 20231110 ljh DB202310484652 add NoDirectNoteReason__c, // 20231110 ljh DB202310484652 add Medical_Institution_Address__c, // 20231227 ljh DB202312530994 add Medical_Institution_Address_sup__c,// 20231227 ljh DB202312530994 add // AWS_Data_Id__c, // OLY_OCM-583 Start 分单时增加拷贝字段GI本部区分 GI_Diff__c // GI本部区分 // OLY_OCM-583 End FROM Rental_Apply__c WHERE Id IN :idList]; } private static List getOpd(List idList) { return [SELECT Id, OwnerId, //所有人 //Status__c, //状态 OPDPlan_ImplementDate__c, //OPD计划实施日期 OPDPlan_ImplementDate_temp__c, // OPD计划实施日期(Temp) Activity_Type2__c, //拜访区分 Account_Laboratory__c, //科室 //OPDPlan_Report__c, //报告书 //2021-12-28 mzy OPD计划改善 Campaign__c, //学会 Related_Opportunity1_ID__c, //跟进询价ID1 Related_Opportunity2_ID__c, //跟进询价ID2 NoOpp_Reason__c,//无询价申请理由 OPDType__c, //OPD计划来源 SalesManager__c, //推广经理 SalesManager_Txt__c, //推广经理(文本) PlanProdDetail__c, //计划出借备品信息 DelayCancel_Reason__c, //延期取消理由 RentalReson__c, //出借目的 noReportStatus__c, //无报告状态 ModelLendingProduct__c, //模型出借产品 ModelLending__c, //模型出借产品型号 CorrespondingRepairNo__c, //对应修理维修NO AttachmentCertificate__c, //附件证明 Meeting_Cooperate__c, //会议合作本部1 Meeting_Cooperate2__c, //会议合作本部2 Meeting_Cooperate3__c, //会议合作本部3 Meeting_Cooperate4__c, //会议合作本部4 IF_Approved__c, // 是否申请决裁 20220218 ljh add Approved_No__c, // 决裁编码 20220218 ljh add Approved_Status__c,// 决裁状态 20220218 ljh add EquipmentFromOpp__c, // 报价外产品 20240110 zyh add EquipmentNotFromOpp__c, // 报价内产品 20240110 zyh add PlanProdDetailSys__c, // 计划出借战略产品 20240110 zyh add PlanProdDetailSysT__c, // 计划出借战略产品 20240110 zyh add AdditionalSupport__c, //原OPD计划(追加配套用) EquipmentFromOppNum__c, //报价内产品数量 20240204 sx 报价外产品数量/报价内产品数量 赋值 EquipmentNotFromOppNum__c //报价外产品数量 20240204 sx 报价外产品数量/报价内产品数量 赋值 FROM OPDPlan__c WHERE Id IN :idList]; } /** * 从 Label.OCM_Management_Province_Mapping * 取出 存放地 → 省 的 Map */ private static Map getOcmMgtProvMap() { Map ocm_Map = new Map(); String OCM_Management_Province_Mapping = System.Label.OCM_Management_Province_Mapping; for (String str : OCM_Management_Province_Mapping.split(',')) { List strList = str.split(':'); ocm_Map.put(strList[1].trim(), strList[0].trim()); } return ocm_Map; } /** * 单独拎出分配方法 * 2024-01-10 zyh */ private static void toFp(List raIdFpList,Boolean isFirst) { // 借用机会可视化--延迟推送分配/排队--zyh--start DateTime startHold = Datetime.now(); // 借用机会可视化_延迟推送改为自定义标签 20240113 zyh start // DateTime endHold = startHold.addSeconds(60); Integer num = 60; if (String.isNotBlank(System.Label.SplitToFp)) { System.debug('System.Label.SplitToFp===' + System.Label.SplitToFp); num = Integer.valueOf(System.Label.SplitToFp); } DateTime endHold = startHold.addSeconds(num); // 借用机会可视化_延迟推送改为自定义标签 20240113 zyh end if (isFirst) { System.debug('** isFirst'); if (!Test.isRunningTest()) { System.enqueueJob(new RentalFixtureSetAssignAndQueueWebService(raIdFpList)); } } else { System.debug('**Not isFirst'); while (Datetime.now() < endHold) { } if (!Test.isRunningTest()) { System.enqueueJob(new RentalFixtureSetAssignAndQueueWebService(raIdFpList)); } } } }