public without sharing class ProRegisterLinkHandler {
	/* 更新产品内最新产品在注册证
	   1.根据产品-注册证关系找出所有对应的产品
	   2.根据产品找出其所需最新产品注册证
	  */
	public static void updateProduct(List<Product_Register_Link__c> newList, Map<Id, Product_Register_Link__c> newMap, List<Product_Register_Link__c> oldList, Map<Id, Product_Register_Link__c> oldMap, string HandleType) {
		// 产品-注册证关系 中所有 产品ID
		list<ID> Product2_ID_list = new list<ID>();
		// 现有产品 map
		map<ID, Product2> Exist_ProductMap = new map<ID, Product2>();
		// 需要更新产品 list
		List<Product2> UpdateProductlist = new list<Product2>();

		List<Product_Register_Link__c> currentList = new List<Product_Register_Link__c>();

		if ('1'.equals(HandleType)) {
			currentList = newList;
		} else {
			currentList = oldList;
		}

		for (Product_Register_Link__c PRL : currentList) {
			Product2_ID_list.add(PRL.Product2__c);
		}

		Exist_ProductMap = new map<ID, Product2>([select ID, registrationCode__c, clinical_product_code__c from Product2 where id in: Product2_ID_list]);
		List<Product_Register_Link__c> allPRLList = new List<Product_Register_Link__c>();

		allPRLList = [select  Product2__c,
		              Product_Register__c,
		              Product_Register__r.PrdCompanyAddr__c,
		              Product_Register__r.ValidFrom__c,
		              Product_Register__r.ValidTo__c,
		              Product_Register__r.name,
		              Product_Register__r.ValidProductRegister__c,
		              Product_Register__r.ClinicalProductCode__c
		              from Product_Register_Link__c
		              where Product2__c  in : Product2_ID_list
		              order by Product2__c,
		              LastModifiedDate desc
		             ];

		String tmpProductID = '';
		Map<ID, Product2> Upd_ProductMap = new Map<ID, Product2>();
		Product2 temProduct = new Product2();

		for (String ProId : Product2_ID_list) {
			temProduct = Exist_ProductMap.get(ProId);
			Upd_ProductMap.put(ProId, temProduct);
		}

		for (Product_Register_Link__c temPRL : allPRLList) {
			if (!tmpProductID.equals(temPRL.Product2__c)) {
				tmpProductID = temPRL.Product2__c;
				temProduct = Upd_ProductMap.get(tmpProductID);
				temProduct.Register_Latest__c = temPRL.Product_Register__c;                    // 最新注册证ID
				temProduct.registrationCode__c = null;
				temProduct.clinical_product_code__c = null;
			}
			//// 如果注册证状态有效,记录有效注册证号
			if (temPRL.Product_Register__r.ValidProductRegister__c) {
				if (String.isBlank(temProduct.registrationCode__c)) {
					temProduct.registrationCode__c = temPRL.Product_Register__r.Name + '\n';
				} else {
					if (String.isNotBlank(temPRL.Product_Register__r.Name)) {
						if (temProduct.registrationCode__c.indexOf(temPRL.Product_Register__r.Name) == -1) {
							temProduct.registrationCode__c += temPRL.Product_Register__r.Name + '\n';
						}
					}
				}
				String clinicalCode = temPRL.Product_Register__r.ClinicalProductCode__c;
				if (String.isBlank(temProduct.clinical_product_code__c)) {
					temProduct.clinical_product_code__c = clinicalCode;//temPRL.Product_Register__r.ClinicalProductCode__c;
				} else {

					if (String.isNotBlank(clinicalCode)) {

						Map<String, String> codeMap = new Map<String, String>();
						for (String code : temProduct.clinical_product_code__c.split('/')) {
							codeMap.put(code, code);
						}
						codeMap.put(clinicalCode, clinicalCode);
						temProduct.clinical_product_code__c = '';
						for (String code : codeMap.values()) {
							if (String.isBlank(temProduct.clinical_product_code__c)) {
								temProduct.clinical_product_code__c = code;
							} else {
								temProduct.clinical_product_code__c += '/' + code;
							}
						}
						
					}

				}
			}


			Upd_ProductMap.put(tmpProductID, temProduct);
		}

		if (Upd_ProductMap.keySet().size() > 0)  {
			update Upd_ProductMap.values();
		}

	}

}