public without sharing class AssetModifyBelongsChangeHandler extends Oly_TriggerHandler { //CHAN-B7J4SE private Map newMap; private Map oldMap; private List newList; private List oldList; private List idList; private List assetList; public AssetModifyBelongsChangeHandler(){ this.newMap = (Map) Trigger.newMap; this.oldMap = (Map) Trigger.oldMap; this.newList = (List) Trigger.new; this.oldList = (List) Trigger.old; this.idList = new List(); this.assetList = new List(); } protected override void beforeUpdate(){ updateambChange_Account(); } private void updateambChange_Account() { for(AssetModifyBelongsChange__c ambc :newList){ if(oldMap.get(ambc.Id).get('Change_status__c') != ambc.Change_status__c && ambc.Change_status__c == '批准'){ idList.add(ambc.Id); } } // 虚拟维修合同 Map v_MCMap = new map (); // 虚拟维修合同对应询价名称 Map v_MCOppMap = new map (); // 需要插入的虚拟维修合同保有设备 List v_MCdetinList = new List(); // 需要更新的虚拟维修合同保有设备 List v_MCdetUpList = new List(); // 新的变更合同对应的新保有设备 中间匹配用 Map > v_MCdetnewMap = new map >(); //需要作成两个map add by rentx LJPH-C493WW // 合同询价编码 维修合同/保有设备集合 Map> OppAssMap = new Map>(); // 目标客户 合同询价编码set Map > vMOppMap = new map >(); // 总院分院间调用的保有设备,变更前发送的修理要计入到变更后的合同里 thh 20220418 start Map> rpMcaNameMap = new Map>(); Map ChangeRepAssMap = new Map(); // 总院分院间调用的保有设备,变更前发送的修理要计入到变更后的合同里 thh 20220418 end //add by rentx LJPH-C493WW //LJPH-C493WW fy 【委托】 【多年保合同】多年保设备转移造成的合同问题 start Map MainCMap=new Map(); //LJPH-C493WW fy 【委托】 【多年保合同】多年保设备转移造成的合同问题 end if(idList != null && idList.size() > 0){ List ambcDetailList ; ambcDetailList = [select Id,Asset__c, ChangeAndChangeDetail__r.AccountField__c, ChangeAndChangeDetail__r.rawAccount__c, ChangeAndChangeDetail__r.Name, ChangeAndChangeDetail__r.ChangeReason__c from AssetModifyBelongsChangeDetail__c where ChangeAndChangeDetail__c In :idList]; // SWAG-BCL2YW ---XHL---20190528---UpdateStart List accountFieldList = new List(); // 保有设备IDlist,变更时,新建维修合同用 Set assetidList = new Set(); //获取目标客户的Id for (AssetModifyBelongsChangeDetail__c ambcDetail :ambcDetailList){ accountFieldList.add(ambcDetail.ChangeAndChangeDetail__r.AccountField__c); // 获取变更的保有设备ID assetidList.add(ambcDetail.Asset__c); } Map maAssetMap = new Map(); // Schema.DescribeSobjectResult d = Maintenance_Contract_Asset__c.sObjectType.getDescribe(); // Map fieldMap = d.fields.getMap(); if(assetidList.size() > 0){ for (Maintenance_Contract_Asset__c Mca :[select id,startDateGurantee__c, startDateGurantee_Text__c,endDateGurantee__c,Asset__c, endDateGurantee_Text__c,Return_Flag__c,ChangeAcc_flag__c, Asset__r.guarantee_period__c,Asset__r.Extend_Gurantee_DateTo__c, DeliveryDate__c,No_discount__c,V_MC_Name__c,Asset_SerialNumber__c, //LJPH-C493WW fy 【委托】 【多年保合同】多年保设备转移造成的合同问题 start Maintenance_Contract__c, Maintenance_Contract__r.Add_Number__c, //LJPH-C493WW fy 【委托】 【多年保合同】多年保设备转移造成的合同问题 end Asset__r.Proviston_period__c,Maintenance_Contract__r.Management_Code__c from Maintenance_Contract_Asset__c where Asset__c in : assetidList and Asset_Status__c = true and Maintenance_Contract__r.RecordType.DeveloperName = 'VM_Contract']) { maAssetMap.put(Mca.Asset__c,Mca); //add by rentx LJPH-C493WW 20210715 if (!OppAssMap.containsKey(Mca.Maintenance_Contract__r.Management_Code__c)) { OppAssMap.put(mca.Maintenance_Contract__r.Management_Code__c,new List()); } OppAssMap.get(Mca.Maintenance_Contract__r.Management_Code__c).add(Mca); // Oppvmidmap.put(mca.Maintenance_Contract__r.Management_Code__c, mac.Maintenance_Contract__c); //add by rentx LJPH-C493WW 20210715 } } System.debug('97::: OppAssMap '+OppAssMap); if (accountFieldList.size() > 0){ Map accMap = new Map(); List accs = ControllerUtil.selectAccountForTrigger(accountFieldList); for(Account acc : accs) { accMap.put(acc.Id, acc); } // 所有客户对应的保有设备 Map> changeAccMap = new Map>(); for (AssetModifyBelongsChangeDetail__c ambcDetail :ambcDetailList){ String accountIdStr = ambcDetail.ChangeAndChangeDetail__r.AccountField__c; System.debug('accountIdStr:'+ ambcDetail.ChangeAndChangeDetail__r.AccountField__c); Account acc = accMap.get(accountIdStr); Asset asset = new Asset(); //判断目标客户的类型是否是"战略科室ET" if (acc != null && acc.RecordTypeId == System.Label.ET) { asset.Department_Class__c = accountIdStr; asset.Hospital__c = acc.ParentId; } else { asset.Department_Class__c = acc.ParentId; asset.Hospital__c = acc.Parent.ParentId; } asset.Id = ambcDetail.Asset__c; asset.AccountId = accountIdStr; asset.rawAccounts__c = ambcDetail.ChangeAndChangeDetail__r.rawAccount__c; asset.Nameno__c = ambcDetail.ChangeAndChangeDetail__c; asset.ChangeReasons__c = ambcDetail.ChangeAndChangeDetail__r.ChangeReason__c; assetList.add(asset); // 变更虚拟维修合同信息整理 start if (maAssetMap.containsKey(asset.Id)) { // if (changeAccMap.containsKey(accountIdStr)) { // changeAccMap.get(accountIdStr).add(maAssetMap.get(asset.Id)); // }else{ // List mcatempList = new List(); // mcatempList.add(maAssetMap.get(asset.Id)); // changeAccMap.put(accountIdStr,mcatempList); // } // if(!v_MCOppMap.containsKey(accountIdStr)){ // v_MCOppMap.put(accountIdStr, maAssetMap.get(asset.Id).Maintenance_Contract__r.Management_Code__c); // } //update by rentx LJPH-C493WW start //LJPH-C493WW fy 【委托】 【多年保合同】多年保设备转移造成的合同问题 start if (!vMOppMap.containsKey(accountIdStr)) { vMOppMap.put(accountIdStr, new Set()); // vMOppMap.put(accountIdStr, new Set()); } Maintenance_Contract__c maintenancecontc=new Maintenance_Contract__c(); maintenancecontc.Id = maAssetMap.get(asset.Id).Maintenance_Contract__c; maintenancecontc.Add_Number__c = maAssetMap.get(asset.Id).Maintenance_Contract__r.Add_Number__c; maintenancecontc.Management_Code__c = maAssetMap.get(asset.Id).Maintenance_Contract__r.Management_Code__c; vMOppMap.get(accountIdStr).add(maintenancecontc); // vMOppMap.get(accountIdStr).add(maAssetMap.get(asset.Id)); //LJPH-C493WW fy 【委托】 【多年保合同】多年保设备转移造成的合同问题 end //update by rentx LJPH-C493WW end System.debug('maAssetMap.get(asset.Id)=====:'+maAssetMap.get(asset.Id)); // 总院分院间调用的保有设备,变更前发送的修理要计入到变更后的合同里 thh 20220418 start if(ambcDetail.ChangeAndChangeDetail__r.ChangeReason__c == '总院分院间调用'){ ChangeRepAssMap.put(maAssetMap.get(asset.Id).Maintenance_Contract__r.Management_Code__c, ambcDetail.ChangeAndChangeDetail__r.ChangeReason__c); } // 总院分院间调用的保有设备,变更前发送的修理要计入到变更后的合同里 thh 20220418 end } } System.debug('154::: vMOppMap '+vMOppMap); vMOppMap.remove(null); System.debug('156::: vMOppMap '+vMOppMap); // if (vMOppMap.size() > 0) { // 总院分院间调用的保有设备,变更前发送的修理要计入到变更后的合同里 thh 20220418 start List mcaIdList = new List(); Map> RepairIdMap = new Map>(); for (String accountId : vMOppMap.keySet()) { System.debug('accountId1:' + accountId); Set mcSet = vMOppMap.get(accountId); for(Maintenance_Contract__c mc : mcSet){ System.debug('ChangeRepAssMap1:' + ChangeRepAssMap); System.debug('mc.Management_Code__c1:' + mc.Management_Code__c); if(ChangeRepAssMap.containsKey(mc.Management_Code__c)){ mcaIdList.add(mc.Id); } } } System.debug('mcaIdList1:' + mcaIdList); if(mcaIdList.size() > 0){ List RepairList = [select Id, VM_Maintenance_Contract__c from Repair__c where VM_Maintenance_Contract__c IN :mcaIdList]; if(RepairList.size() > 0){ for(Repair__c rp : RepairList){ List newRepairIdList = new List(); if(RepairIdMap.containsKey(rp.VM_Maintenance_Contract__c)){ newRepairIdList = RepairIdMap.get(rp.VM_Maintenance_Contract__c); newRepairIdList.add(rp.Id); RepairIdMap.put(rp.VM_Maintenance_Contract__c, newRepairIdList); } else{ newRepairIdList.add(rp.Id); RepairIdMap.put(rp.VM_Maintenance_Contract__c, newRepairIdList); } } } } // 总院分院间调用的保有设备,变更前发送的修理要计入到变更后的合同里 thh 20220418 end for (String str : vMOppMap.keySet()) { //update by rentx LJPH-C493WW start Set vset = vMOppMap.get(str); //Set vset = vMOppMap.get(str); //LJPH-C493WW fy 【委托】 【多年保合同】多年保设备转移造成的合同问题 start for (Maintenance_Contract__c mcodes : vset) { //for (Maintenance_Contract_Asset__c mcodes : vset) { Maintenance_Contract__c vmc = new Maintenance_Contract__c(); vmc.Name = accMap.get(str).Parent.Parent.Name + '多年保修'; vmc.Status__c = '契約'; system.debug('mcodes==='+mcodes); // List mcodestrList= mcodes.split('@'); // String mcode =mcodes.Maintenance_Contract__r.Management_Code__c; // String mcid=(String)mcodes.Maintenance_Contract__c; String mcode =mcodes.Management_Code__c; String mcid=(String)mcodes.Id; // String addNumber =mcodestrList[1]; Map addnumMap = new Map(); if (String.isNotBlank(mcode) && mcode.substring(mcode.length()-3,mcode.length()-2) == '-' ) { String startstr = mcode.substring(0,mcode.length()-2); Integer count = Integer.valueOf(mcode.substring(mcode.length()-2,mcode.length())); vmc.Management_CodeVM__c = startstr + String.valueOf(count +1).leftPad(2,'0'); // mcodes.Maintenance_Contract__r.Add_Number__c=count +1; // addnumMap.put(mcid,mcodes.Maintenance_Contract__r.Add_Number__c); mcodes.Add_Number__c=count +1; addnumMap.put(mcid,mcodes.Add_Number__c); }else{ system.debug('mcodes.Maintenance_Contract__r.Add_Number__c'+mcodes.Add_Number__c); if(mcodes.Add_Number__c==null||mcodes.Add_Number__c==0){ // if(mcodes.Maintenance_Contract__r.Add_Number__c==null||mcodes.Maintenance_Contract__r.Add_Number__c==0){ vmc.Management_CodeVM__c = mcode + '-01'; // mcodes.Maintenance_Contract__r.Add_Number__c=1; // addnumMap.put(mcid,mcodes.Maintenance_Contract__r.Add_Number__c); mcodes.Add_Number__c=1; addnumMap.put(mcid,mcodes.Add_Number__c); }else{ // Decimal addnum = mcodes.Maintenance_Contract__r.Add_Number__c+1; // if(mcodes.Maintenance_Contract__r.Add_Number__c<=9){ Decimal addnum = mcodes.Add_Number__c+1; if(mcodes.Add_Number__c<=9){ vmc.Management_CodeVM__c = mcode + '-0'+addnum; }else{ vmc.Management_CodeVM__c = mcode + '-'+addnum; } addnumMap.put(mcid,addnum); } system.debug('vmc.Management_CodeVM__c ======'+vmc.Management_CodeVM__c); system.debug('addnumMap ======'+addnumMap); } // 总院分院间调用的保有设备,变更前发送的修理要计入到变更后的合同里 thh 20220418 start if(ChangeRepAssMap.containsKey(mcodes.Management_Code__c) && RepairIdMap.containsKey(mcodes.Id)){ rpMcaNameMap.put(vmc.Management_CodeVM__c, RepairIdMap.get(mcodes.Id)); } System.debug('rpMcaNameMap====' + rpMcaNameMap); // 总院分院间调用的保有设备,变更前发送的修理要计入到变更后的合同里 thh 20220418 end //LJPH-C493WW fy 【委托】 【多年保合同】多年保设备转移造成的合同问题 end vmc.Department__c = accMap.get(str).Id; vmc.Department_Class__c = accMap.get(str).ParentId; vmc.Hospital__c = accMap.get(str).Parent.ParentId; vmc.RecordTypeId = System.Label.VM_ContractType; v_MCMap.put(mcode,vmc); List mcatempList = new List(); for (Maintenance_Contract_Asset__c mac : OppAssMap.get(mcode)) { Maintenance_Contract_Asset__c mactemp = mac.clone(); //mactemp.V_MC_Name__c = null; Maintenance_Contract__c mctrobject= new Maintenance_Contract__c(); mactemp.Maintenance_Contract__c = null; mactemp.Extend_Gurantee_DateTo__c = mac.Asset__r.Extend_Gurantee_DateTo__c; mactemp.guarantee_period__c = mac.Asset__r.guarantee_period__c; mactemp.Proviston_period__c = mac.Asset__r.Proviston_period__c; mcatempList.add(mactemp); mac.ChangeAcc_flag__c = true; mac.endDateGurantee_changeAcc__c = Date.today(); mac.Extend_Gurantee_DateTo__c = mac.Asset__r.Extend_Gurantee_DateTo__c; mac.guarantee_period__c = mac.Asset__r.guarantee_period__c; mac.Proviston_period__c = mac.Asset__r.Proviston_period__c; //LJPH-C493WW fy 【委托】 【多年保合同】多年保设备转移造成的合同问题 start mctrobject.Id=mac.Maintenance_Contract__c; mctrobject.Add_Number__c=addnumMap.get(mac.Maintenance_Contract__c); MainCMap.put(mctrobject.Id,mctrobject); system.debug('mctrobject.Id===='+mctrobject.Id); system.debug('mctrobject.Add_Number__c======='+mctrobject.Add_Number__c); //LJPH-C493WW fy 【委托】 【多年保合同】多年保设备转移造成的合同问题 end // 给总院分院间调用的维修合同保有设备的总院分院间变更标识赋true thh 20220426 start if(ChangeRepAssMap.containsKey(mac.Maintenance_Contract__r.Management_Code__c)){ mac.General_Branch_Changeflag__c = true; } // 给总院分院间调用的维修合同保有设备的总院分院间变更标识赋true thh 20220426 end v_MCdetUpList.add(mac); } v_MCdetnewMap.put(mcode, mcatempList); } /* Maintenance_Contract__c vmc = new Maintenance_Contract__c(); vmc.Name = accMap.get(str).Parent.Parent.Name + '多年保修'; vmc.Status__c = '契約'; String oppNostr = v_MCOppMap.get(str); if (String.isNotBlank(oppNostr) && oppNostr.substring(oppNostr.length()-3,oppNostr.length()-2) == '-' ) { String startstr = oppNostr.substring(0,oppNostr.length()-2); Integer count = Integer.valueOf(oppNostr.substring(oppNostr.length()-2,oppNostr.length())); vmc.Management_CodeVM__c = startstr + String.valueOf(count +1).leftPad(2,'0'); }else{ vmc.Management_CodeVM__c = oppNostr + '-01'; } //vmc.Management_CodeVM__c = 'DN-' + v_MCOppMap.get(str) + '-01'; vmc.Department__c = accMap.get(str).Id; vmc.Department_Class__c = accMap.get(str).ParentId; vmc.Hospital__c = accMap.get(str).Parent.ParentId; //vmc.Opportunity__c = oppMap.get(InquiryNoStr).Id; vmc.RecordTypeId = System.Label.VM_ContractType; v_MCMap.put(str,vmc); // 更新现有维修合同保有设备 // 新规新的维修合同保有设备 List mcatempList = new List(); for (Maintenance_Contract_Asset__c mac : changeAccMap.get(str)) { Maintenance_Contract_Asset__c mactemp = mac.clone(); //mactemp.V_MC_Name__c = null; mactemp.Maintenance_Contract__c = null; mactemp.Extend_Gurantee_DateTo__c = mac.Asset__r.Extend_Gurantee_DateTo__c; mactemp.guarantee_period__c = mac.Asset__r.guarantee_period__c; mactemp.Proviston_period__c = mac.Asset__r.Proviston_period__c; mcatempList.add(mactemp); mac.ChangeAcc_flag__c = true; mac.endDateGurantee_changeAcc__c = Date.today(); mac.Extend_Gurantee_DateTo__c = mac.Asset__r.Extend_Gurantee_DateTo__c; mac.guarantee_period__c = mac.Asset__r.guarantee_period__c; mac.Proviston_period__c = mac.Asset__r.Proviston_period__c; v_MCdetUpList.add(mac); } v_MCdetnewMap.put(str, mcatempList); */ //update by rentx LJPH-C493WW end } } // 变更虚拟维修合同信息整理 end } // SWAG-BCL2YW ---XHL---20190528---UpdateEnd } System.debug('256::: v_MCMap '+v_MCMap); if (v_MCMap.size() > 0) { insert v_MCMap.values(); for (String str : v_MCdetnewMap.keySet()) { for (Maintenance_Contract_Asset__c mca : v_MCdetnewMap.get(str)) { //mca.V_MC_Name__c = v_MCMap.get(str).Name; mca.Maintenance_Contract__c = v_MCMap.get(str).Id; mca.startDateGurantee_changgeAcc__c = Date.today(); mca.ChangeAcc_flag__c = true; // 给总院分院间调用的维修合同保有设备的总院分院间变更标识赋true thh 20220426 start if(ChangeRepAssMap.containsKey(mca.Maintenance_Contract__r.Management_Code__c)){ mca.General_Branch_Changeflag__c = true; } // 给总院分院间调用的维修合同保有设备的总院分院间变更标识赋true thh 20220426 end v_MCdetinList.add(mca); } } // 总院分院间调用的保有设备,变更前发送的修理要计入到变更后的合同里 thh 20220418 Start List updateRpList = new List(); Map rpMap = new Map(); List rpList = new List(); List rpIdList = new List(); for(String str : v_MCMap.keySet()){ Maintenance_Contract__c mc = v_MCMap.get(str); rpIdList = rpMcaNameMap.get(mc.Management_CodeVM__c); } if(rpIdList != null && rpIdList.size() > 0){ rpList = [select Id, VM_Maintenance_Contract__c from Repair__c where Id IN : rpIdList]; } if(rpList.size() > 0){ for(Repair__c rp : rpList){ rpMap.put(rp.Id, rp); } } for(String str : v_MCMap.keySet()){ Maintenance_Contract__c mc = v_MCMap.get(str); if(rpMcaNameMap.containsKey(mc.Management_CodeVM__c)){ List newrpIdList = rpMcaNameMap.get(mc.Management_CodeVM__c); if(newrpIdList != null && newrpIdList.size() > 0){ for(Id rpId : newrpIdList){ Repair__c rp = rpMap.get(rpId); rp.VM_Maintenance_Contract__c = mc.Id; updateRpList.add(rp); } } } } if(updateRpList.size() > 0){ update updateRpList; } // 总院分院间调用的保有设备,变更前发送的修理要计入到变更后的合同里 thh 20220418 end // 插入变更新合同的维修合同保有设备 insert v_MCdetinList; // 更新现有合同的维修合同保有设备 update v_MCdetUpList; } if(assetList != null && assetList.size() > 0 ){ update assetList; } //LJPH-C493WW fy 【委托】 【多年保合同】多年保设备转移造成的合同问题 start if(MainCMap != null && MainCMap.size() > 0){ List mainList=MainCMap.values(); update mainList; } //LJPH-C493WW fy 【委托】 【多年保合同】多年保设备转移造成的合同问题 end } //zzm 20240222 DB202401391766 【保有设备变更】新增cl6 cl5 cl4 质量负责人信息start protected override void afterInsert(){ updateProgressManager(); } private void updateProgressManager() { List ambsIds = new List(); // List users = new List(); // Map ambcMaps = new Map(); Map userAmbcMap = new Map(); for(AssetModifyBelongsChange__c ambc :newList){ ambsIds.add(ambc.Id); // if(newMap.get(ambc.Id).get('OwnerId') != null){ // User tmpUser = (User)oldMap.get(ambc.Id).get('OwnerId'); // userAmbcMap.put(ambc.Id,tmpUser); // } } List ambsList = new List([ select id,CreatedById,SalesdepartmentNew__c,SalesdepartmentOld__c, CreatedBy.SalesManager__c, SubmitOwner__r.Salesdepartment__c, CreatedBy.BuchangApprovalManagerSales__c, CreatedBy.ZongjianApprovalManager__c from AssetModifyBelongsChange__c where id in :ambsIds ]); for(AssetModifyBelongsChange__c ambc :ambsList) { ambc.CL6__c = ambc.CreatedBy.SalesManager__c; ambc.CL5__c = ambc.CreatedBy.BuchangApprovalManagerSales__c; ambc.CL4__c = ambc.CreatedBy.ZongjianApprovalManager__c; if(ambc.SubmitOwner__r.Salesdepartment__c =='1.华北' || ambc.SubmitOwner__r.Salesdepartment__c =='2.东北' || ambc.SubmitOwner__r.Salesdepartment__c =='3.西北') { ambc.quality__c = System.Label.qualityBF; } else if(ambc.SubmitOwner__r.Salesdepartment__c =='4.西南' || ambc.SubmitOwner__r.Salesdepartment__c =='6.华南') { ambc.quality__c = System.Label.qualityHN; } else if(ambc.SubmitOwner__r.Salesdepartment__c =='5.华东') { ambc.quality__c = System.Label.qualityHD; } } update ambsList; } //zzm 20240222 DB202401391766 【保有设备变更】新增cl6 cl5 cl4 cl3 质量负责人信息 end }