public without sharing class DiscountProductApplicationApproveHandler extends Oly_TriggerHandler { //private Map newMap; private Map oldMap; private List newList; //private List oldList; public DiscountProductApplicationApproveHandler(){ this.newList = (List) Trigger.new; //this.oldList = (List) Trigger.old; //this.newMap = (Map) Trigger.newMap; this.oldMap = (Map) Trigger.oldMap; } protected override void afterUpdate(){ insertHospitalPrice(); insertOrDelPrice(); } public void insertHospitalPrice() { List dpaIdList = new List(); //add by rentx Id HosTypeId = [select Id from RecordType where IsActive = true and DeveloperName = 'HospitalSpecials'].Id; //比对是否为变更后为批准 for(DiscountProductApplication__c dpa :newList){ if(oldMap.get(dpa.Id).get('ApplicationStatus__c') != dpa.ApplicationStatus__c && dpa.ApplicationStatus__c == '批准'){ //update by rentx 2020-12-23 // dpaIdList.add(dpa.Id); if (dpa.RecordTypeId == HosTypeId) { dpaIdList.add(dpa.Id); } //update by rentx 2020-12-23 } } //特价产品明细 List dpadList = new List(); if(dpaIdList.size()>0){ dpadList = [select id,name,Product2__c,ProductDiscount__c,DiscountProductApplication__c,DiscountProductApplication__r.RawAccount__c,DiscountProductApplication__r.AimsAccount__c from DiscountProductApplicationDetail__c where DiscountProductApplication__c in: dpaIdList]; } //本次特价产品明细生成的经销商医院产品 List hpaphList = new List(); //待删除的经销商医院特价产品 List hpaphDelList = new List(); if(dpadList.size()>0){ String hpaph; for(DiscountProductApplicationDetail__c dpad : dpadList){ String a = dpad.DiscountProductApplication__r.AimsAccount__c; String p = dpad.DiscountProductApplication__r.RawAccount__c; String h = dpad.Product2__c; hpaph = a + p + h; //update by rentx 2021-22-23 start // hpaphList.add(hpaph); if (dpad.ProductDiscount__c == null) { hpaphDelList.add(hpaph); }else{ hpaphList.add(hpaph); } //update by rentx 2021-22-23 end } } //add by rentx 2021-2-23 start //待删除的医院特价产品 List hpDelList = [select id,aph__c from hospitalprice__c where aph__c in :hpaphDelList]; //add by rentx 2021-2-23 end //已生成的医院特价关系表 List hpOldList = [select id,name,hospital__c,product__c,account__c,mPrice__c,aph__c from hospitalprice__c where aph__c in: hpaphList]; //医院特价关系表赋值 List hpList= new List(); if(dpadList.size()>0){ for(DiscountProductApplicationDetail__c dpad : dpadList){ if (dpad.ProductDiscount__c != null) { hospitalprice__c hp = new hospitalprice__c(); String a = dpad.DiscountProductApplication__r.AimsAccount__c; String p = dpad.DiscountProductApplication__r.RawAccount__c; String h = dpad.Product2__c; //hp.aph__c = dpad.DiscountProductApplication__r.AimsAccount__c + dpad.DiscountProductApplication__r.RawAccount__c + dpad.Product2__c + ''; hp.aph__c = a + p + h; for(hospitalprice__c hpold:hpOldList){ if(hp.aph__c == hpold.aph__c){ hp.aph__c = hpold.aph__c; hp.id = hpold.id; } } hp.hospital__c = dpad.DiscountProductApplication__r.AimsAccount__c; hp.mPrice__c = dpad.ProductDiscount__c; hp.product__c = dpad.Product2__c; hp.account__c = dpad.DiscountProductApplication__r.RawAccount__c; hpList.add(hp); } } } if(hpList.size()>0){ upsert hpList; } if (hpDelList.size() > 0) { delete hpDelList; } } //add by rentx 2020-12-23 public void insertOrDelPrice(){ Id ProductTypeId = [select Id from RecordType where IsActive = true and DeveloperName = 'ProductDiscount'].Id; List ids = new List(); List dealerPDel = new List(); List dealerPIns = new List(); List applist = new List(); for (DiscountProductApplication__c dpac : newList) { if (dpac.RecordTypeId == ProductTypeId) { if (dpac.ApplicationStatus__c == '批准' && oldMap.get(dpac.Id).ApplicationStatus__c != '批准') { ids.add(dpac.Id); applist.add(dpac); } } } //获取对应的明细 if (ids.size() > 0) { Map> dismap = new Map>(); List dpalist = [select id,Product2__c,ProductDiscount__c,DiscountProductApplication__c,Special_Discount__c,DealerProductId__c from DiscountProductApplicationDetail__c where DiscountProductApplication__c in: ids]; for (DiscountProductApplicationDetail__c dis : dpalist) { if (!dismap.containsKey(dis.DiscountProductApplication__c)) { dismap.put(dis.DiscountProductApplication__c, new List()); } dismap.get(dis.DiscountProductApplication__c).add(dis); } for (DiscountProductApplication__c dpac : applist) { //获取需要新建的经销商产品 List disList = new List(); disList = dismap.get(dpac.Id); if (disList!= null && disList.size() > 0) { for (DiscountProductApplicationDetail__c dis : disList) { if ((dis.Special_Discount__c == null) && (dis.ProductDiscount__c == null)) { Dealer_Product__c dpc = new Dealer_Product__c(); dpc.Id = dis.DealerProductId__c; dealerPDel.add(dpc); }else{ Dealer_Product__c dpc = new Dealer_Product__c(); dpc.Id = dis.DealerProductId__c; dpc.Dealer_Product2__c = dis.Product2__c; dpc.Dealer_Contact__c = dpac.Dealer_Contact__c; dpc.Special_Discount__c = dis.Special_Discount__c; dpc.Special_Campaign_Price__c = dis.ProductDiscount__c; dealerPIns.add(dpc); } } } } } //1.删除经销商产品 if (dealerPDel.size() > 0 ) { delete dealerPDel; } //2.添加/修改经销商产品 if (dealerPIns.size() > 0) { upsert dealerPIns; } } //add by rentx 2020-12-23 }