public without sharing class DiscountProductApplicationApproveHandler extends Oly_TriggerHandler {
    
	//private Map<Id, DiscountProductApplication__c> newMap;
    private Map<Id, DiscountProductApplication__c> oldMap;
    private List<DiscountProductApplication__c> newList;
    //private List<DiscountProductApplication__c> oldList;

	public DiscountProductApplicationApproveHandler(){
        this.newList = (List<DiscountProductApplication__c>) Trigger.new;
		//this.oldList = (List<DiscountProductApplication__c>) Trigger.old;
        //this.newMap = (Map<Id, DiscountProductApplication__c>) Trigger.newMap;
        this.oldMap = (Map<Id, DiscountProductApplication__c>) Trigger.oldMap;
	}

	protected override void afterUpdate(){
        insertHospitalPrice();
        insertOrDelPrice();
    }
    public void insertHospitalPrice() {
        List<String> dpaIdList = new List<String>();
        //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<DiscountProductApplicationDetail__c> dpadList = new List<DiscountProductApplicationDetail__c>();
	   	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<String> hpaphList = new List<String>();
        //待删除的经销商医院特价产品
	   	List<String> hpaphDelList = new List<String>();
	   	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<hospitalprice__c> hpDelList = [select id,aph__c from hospitalprice__c where aph__c in :hpaphDelList];
        //add by rentx  2021-2-23 end

	   	//已生成的医院特价关系表
	   	List<hospitalprice__c> hpOldList = [select id,name,hospital__c,product__c,account__c,mPrice__c,aph__c from hospitalprice__c where aph__c in: hpaphList];

	    //医院特价关系表赋值
	   	List<hospitalprice__c> hpList= new List<hospitalprice__c>();
	   	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<Id> ids = new List<Id>();
    	List<Dealer_Product__c> dealerPDel = new List<Dealer_Product__c>();
    	List<Dealer_Product__c> dealerPIns = new List<Dealer_Product__c>();
        List<DiscountProductApplication__c> applist = new List<DiscountProductApplication__c>();

    	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<Id,List<DiscountProductApplicationDetail__c>> dismap = new Map<Id,List<DiscountProductApplicationDetail__c>>();
            List<DiscountProductApplicationDetail__c> 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<DiscountProductApplicationDetail__c>());
                }
                dismap.get(dis.DiscountProductApplication__c).add(dis);
            }


            for (DiscountProductApplication__c dpac : applist) {
                //获取需要新建的经销商产品
                List<DiscountProductApplicationDetail__c> disList = new List<DiscountProductApplicationDetail__c>();
                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

}