public without sharing class CertificationDetailTriggerHandler {
    
    public static void updateDealerPurchaseDetail(List<CertificationDetails__c> newList, Map<Id, CertificationDetails__c> newMap, 
    											  List<CertificationDetails__c> oldList, Map<Id, CertificationDetails__c> oldMap) {
        // SBG203证照变更后，新增证照明细，给代理商购买明细中的证照明细字段赋值-ANI辐射证照已更新-新源志勤-XHL
    	Map<String,List<CertificationDetails__c>> certificationDetailMap = new Map<String,List<CertificationDetails__c>>();
    	List<CertificationDetails__c> detailList = new List<CertificationDetails__c>();
    	String dealerId = '';
    	for (CertificationDetails__c detail : newList) {

    		if (detail.ActivitieTypes__c == '销售' && detail.IsActive__c == true) {
    			dealerId = detail.CertificationDetailAccountId__c;
				if (certificationDetailMap.containsKey(dealerId)) {
					detailList =  certificationDetailMap.get(dealerId);
					detailList.add(detail);
					certificationDetailMap.put(dealerId, detailList);
				} else {
					detailList.add(detail);
					certificationDetailMap.put(dealerId, detailList);
				}
    		}
    		
    		
    	}

    	if (String.isNotBlank(dealerId) && certificationDetailMap != null && certificationDetailMap.size() > 0) {
    		Map<String, PurchaseDetails__c> dealerModelQuantityMap = RadiationUtil.GetPurchaseDetailsMap(dealerId);
			if (dealerModelQuantityMap != null && dealerModelQuantityMap.size() > 0) {
				for (CertificationDetails__c detail : certificationDetailMap.get(dealerId)) {

					if ('Delta和Vanta'.equals(detail.ProdustionType__c)) {

						List<String> childModels = new List<String> {'Delta', 'Vanta'};
						for (String childModel : childModels) {

							if (dealerModelQuantityMap.containsKey(childModel)) {
								dealerModelQuantityMap.get(childModel).PurchaseDetail__c = detail.Id;
							}
							
						}

					} else {
						if (dealerModelQuantityMap.containsKey(detail.ProdustionType__c)) {
							dealerModelQuantityMap.get(detail.ProdustionType__c).PurchaseDetail__c = detail.Id;
						}
					}

				} 

				update dealerModelQuantityMap.values();

			}   	
    	}

    }
}