| | |
| | | public with sharing class ProRegisterHandler extends Oly_TriggerHandler { |
| | | /* 2018-08-31 更新产品内最新产品在注册证 |
| | | 1.根据需要更新的产品注册证找出所有对应的产品-注册证关系 |
| | | 2.根据产品-注册证关系找出所有对应的产品 |
| | | 3.根据产品找出其所需最新产品注册证 |
| | | */ |
| | | private Map<Id, Product_Register__c> newMap; |
| | | private Map<Id, Product_Register__c> oldMap; |
| | | private List<Product_Register__c> newList; |
| | | private List<Product_Register__c> oldList; |
| | | public without sharing class ProRegisterHandler { |
| | | |
| | | public ProRegisterHandler() { |
| | | this.newMap = (Map<Id, Product_Register__c>) Trigger.newMap; |
| | | this.oldMap = (Map<Id, Product_Register__c>) Trigger.oldMap; |
| | | this.newList = (List<Product_Register__c>) Trigger.new; |
| | | this.oldList = (List<Product_Register__c>) Trigger.old; |
| | | } |
| | | public static void updateProduct(List<Product_Register__c> newList, Map<Id, Product_Register__c> newMap, List<Product_Register__c> oldList, Map<Id, Product_Register__c> oldMap) { |
| | | |
| | | protected override void beforeInsert() { |
| | | updateLink(); |
| | | } |
| | | // 产品-注册证关系 中所有 产品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>(); |
| | | // 根据变更的证ID找到关联的所有的产品 |
| | | list<Product_Register_Link__c> ExistPRLList = [select Product2__c, |
| | | Product_Register__c |
| | | from Product_Register_Link__c |
| | | where Product_Register__c in: newList |
| | | ]; |
| | | |
| | | protected override void beforeUpdate() { |
| | | updateLink(); |
| | | } |
| | | for (Product_Register_Link__c PRL : ExistPRLList) { |
| | | // 产品ID的LIST |
| | | Product2_ID_list.add(PRL.Product2__c); |
| | | } |
| | | |
| | | protected override void afterUpdate() { |
| | | updateProduct(); |
| | | } |
| | | // 通过产品ID找到该产品最新注册证ID |
| | | Exist_ProductMap = new Map<ID, Product2> ([select ID, Register_Latest__c, registrationCode__c, clinical_product_code__c |
| | | from Product2 |
| | | where id in: Product2_ID_list |
| | | ]); |
| | | |
| | | //protected override void beforeDelete() { |
| | | // updateProduct(); |
| | | //} |
| | | private void updateLink() { |
| | | for (Product_Register__c reg : newList) { |
| | | system.debug('reg.Name--->'+reg.Name); |
| | | reg.RegisterNoURL__c = EncodingUtil.urlEncode(reg.Name, 'UTF-8'); |
| | | // 通过产品ID找到所有涉及的有效关系 |
| | | 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 = ''; |
| | | Product2 temProduct = new Product2(); |
| | | Map<Id, Product2> UpdProMap = new Map<Id, Product2>(); |
| | | |
| | | for (Product_Register_Link__c temPRL : allPRLList) { |
| | | |
| | | if (!tmpProductID.equals(temPRL.Product2__c)) { |
| | | |
| | | tmpProductID = temPRL.Product2__c; |
| | | temProduct = Exist_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; |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
| | | } |
| | | UpdProMap.put(tmpProductID, temProduct); |
| | | } |
| | | if (UpdProMap.keySet().size() > 0) update UpdProMap.values(); |
| | | } |
| | | |
| | | //产品降类,只要注册证更新了,就检查一遍,重新给产品上的等级类别字段赋值。 |
| | | //可以找出需要的Id,然后调用方法。 |
| | | public static void checkRegistervervaldatum(List<Product_Register__c> newList, Map<Id, Product_Register__c> newMap, List<Product_Register__c> oldList, Map<Id, Product_Register__c> oldMap) { |
| | | List<String> prcList = new List<String>(); |
| | | for (Product_Register__c pr : newList) { |
| | | Product_Register__c oldpr = oldMap.get(pr.Id); |
| | | //有效期的始与终变了,就把这个产品注册证的id放入一个List集合里面 |
| | | //if (pr.ValidTo__c != oldpr.ValidTo__c || pr.ValidFrom__c != oldpr.ValidFrom__c) { |
| | | prcList.add(pr.id); |
| | | //} |
| | | } |
| | | //放到Map里,用于注册证更新产品 |
| | | Map<Id,Product2> prt2Map = new Map<Id,Product2>(); |
| | | //通过之前存储的Id用于关系表的查询。 |
| | | List<Product_Register_Link__c> prlList = new List<Product_Register_Link__c>(); |
| | | prlList = [select Product2__r.id,Product2__r.demoteer_Sap__c,Product2__r.Diedatvanink__c,Product_Register__r.MedPrdClass__c,Product_Register__r.Stelsedag__c,Product_Register__r.ValidTo__c,Product_Register__r.ValidFrom__c |
| | | from Product_Register_Link__c |
| | | where Product_Register__r.Id in : prcList]; |
| | | |
| | | List<ID> proIdList = new List<ID>(); |
| | | for (Product_Register_Link__c prl: prlList) { |
| | | proIdList.add(prl.Product2__r.id); |
| | | } |
| | | |
| | | } |
| | | private void updateProduct() { |
| | | |
| | | if (System.Label.UpdRegOnly == '1') return; |
| | | |
| | | // 产品-注册证关系 中所有 产品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>(); |
| | | // 根据变更的证ID找到关联的所有的产品 |
| | | list<Product_Register_Link__c> ExistPRLList = [select Product2__c, |
| | | Product_Register__c |
| | | from Product_Register_Link__c |
| | | where Product_Register__c in: newList |
| | | ]; |
| | | |
| | | for (Product_Register_Link__c PRL : ExistPRLList) { |
| | | // 产品ID的LIST |
| | | Product2_ID_list.add(PRL.Product2__c); |
| | | } |
| | | |
| | | // 通过产品ID找到该产品最新注册证ID |
| | | Exist_ProductMap = new Map<ID, Product2> ([select ID, Register_Latest__c |
| | | from Product2 |
| | | where id in: Product2_ID_list |
| | | ]); |
| | | |
| | | // 通过产品ID找到所有涉及的有效关系 |
| | | List<Product_Register_Link__c> allPRLList = [select Product2__c, |
| | | Product_Register__c, |
| | | Product_Register__r.PrdCompanyAddr__c, |
| | | Product_Register__r.PrdCompanyAddr2__c, |
| | | Product_Register__r.PrdCompanyAddr3__c, |
| | | Product_Register__r.ValidFrom__c, |
| | | Product_Register__r.ValidTo__c, |
| | | Product_Register__r.Name, |
| | | // 20190401 CHAN-BAPCE6 LHJ Start |
| | | Product_Register__r.BusinessScopeKey__c, |
| | | // 20190401 CHAN-BAPCE6 LHJ End |
| | | Product_Register__r.RegisterNoStatus__c |
| | | from Product_Register_Link__c |
| | | where Product2__c in : Product2_ID_list |
| | | // and If_Delete__c = false |
| | | order by Product2__c, |
| | | Product_Register__r.RegisterNoStatusCode__c, |
| | | LastModifiedDate desc |
| | | ]; |
| | | |
| | | String tmpProductID = ''; |
| | | String temAllProdStatus = ''; |
| | | String temAllProdStatus2 = ''; |
| | | String temAllProdStatusAll = ''; |
| | | // 20190401 CHAN-BAPCE6 LHJ Start |
| | | String temAllScopeKey = ''; |
| | | // 20190401 CHAN-BAPCE6 LHJ End |
| | | Product2 temProduct = new Product2(); |
| | | Map<Id, Product2> UpdProMap = new Map<Id, Product2>(); |
| | | |
| | | for (Product_Register_Link__c temPRL : allPRLList) { |
| | | |
| | | if (!tmpProductID.equals(temPRL.Product2__c)) { |
| | | tmpProductID = temPRL.Product2__c; |
| | | temProduct = Exist_ProductMap.get(tmpProductID); |
| | | temAllProdStatus = ''; |
| | | temAllProdStatus2 = ''; |
| | | temAllProdStatusAll = ''; |
| | | // 20190401 CHAN-BAPCE6 LHJ Start |
| | | temAllScopeKey = ''; |
| | | // 20190401 CHAN-BAPCE6 LHJ End |
| | | temProduct.Register_Latest__c = temPRL.Product_Register__c; // 最新注册证ID |
| | | temProduct.SFDA_Approbation_No__c = temPRL.Product_Register__r.Name; // CFDA注册号 |
| | | temProduct.SFDA_Approbated_Date__c = temPRL.Product_Register__r.ValidFrom__c; // CFDA注册日 |
| | | temProduct.SFDA_Expiration_Date__c = temPRL.Product_Register__r.ValidTo__c; // CFDA有效期限 |
| | | if(temPRL.Product_Register__r.Name != 'FYL') { |
| | | temProduct.ProduceCompany__c = temPRL.Product_Register__r.PrdCompanyAddr__c; // 生产企业 |
| | | //CHAN-C4X63A 【委托】NFM204字段“生产企业地址”优化 XHL 20210716 Start |
| | | temProduct.ProduceCompany2__c = temPRL.Product_Register__r.PrdCompanyAddr2__c; |
| | | temProduct.ProduceCompany3__c = temPRL.Product_Register__r.PrdCompanyAddr3__c; |
| | | //CHAN-C4X63A 【委托】NFM204字段“生产企业地址”优化 XHL 20210716 End |
| | | } |
| | | temProduct.SFDA_Approbated_Status__c = temPRL.Product_Register__r.RegisterNoStatus__c; // CFDA注册证状态 |
| | | } |
| | | |
| | | // 如果注册证状态有效,记录有效注册证号 |
| | | if (temPRL.Product_Register__r.RegisterNoStatus__c == '有效') { |
| | | // LHJ 20190809 |
| | | String productRegisterName = temPRL.Product_Register__r.Name; |
| | | if (temAllProdStatus.length() + productRegisterName.length() <= 240) { |
| | | if (!temAllProdStatusAll.contains(productRegisterName)) { |
| | | temAllProdStatus += ';' + productRegisterName; |
| | | temAllProdStatusAll += ';' + productRegisterName; |
| | | } |
| | | |
| | | } else { |
| | | if (!temAllProdStatusAll.contains(productRegisterName)) { |
| | | temAllProdStatus2 += ';' + productRegisterName; |
| | | temAllProdStatusAll += ';' + productRegisterName; |
| | | } |
| | | //temAllProdStatus2 += ';' + productRegisterName; |
| | | } |
| | | |
| | | // 20190401 CHAN-BAPCE6 LHJ Start |
| | | //if (String.isNotBlank(temPRL.Product_Register__r.BusinessScopeKey__c) && |
| | | // (!temAllScopeKey.contains(temPRL.Product_Register__r.BusinessScopeKey__c))) { |
| | | // temAllScopeKey += ';' + temPRL.Product_Register__r.BusinessScopeKey__c; |
| | | //} |
| | | String businessScopeKey = temPRL.Product_Register__r.BusinessScopeKey__c; |
| | | if (String.isNotBlank(businessScopeKey)) { |
| | | for(String scopeKey:businessScopeKey.split(';')){ |
| | | if (!temAllScopeKey.contains(scopeKey)) { |
| | | temAllScopeKey += ';' + scopeKey; |
| | | } |
| | | } |
| | | |
| | | |
| | | } |
| | | // 20190401 CHAN-BAPCE6 LHJ End |
| | | } |
| | | if (temAllProdStatus != '') { |
| | | temProduct.RegisterNo_ALL__c = temAllProdStatus.substring(1, temAllProdStatus.length()); |
| | | } else { |
| | | temProduct.RegisterNo_ALL__c = ''; |
| | | } |
| | | if (temAllProdStatus2 != '') { |
| | | temProduct.RegisterNo_ALL2__c = temAllProdStatus2.substring(1, temAllProdStatus2.length()); |
| | | } else { |
| | | temProduct.RegisterNo_ALL2__c = ''; |
| | | } |
| | | // 20190401 CHAN-BAPCE6 LHJ Start |
| | | if (temAllScopeKey != '') { |
| | | temProduct.RegScopeKeyAll__c = temAllScopeKey.substring(1, temAllScopeKey.length()); |
| | | } else { |
| | | temProduct.RegScopeKeyAll__c = ''; |
| | | } |
| | | // 20190401 CHAN-BAPCE6 LHJ End |
| | | UpdProMap.put(tmpProductID, temProduct); |
| | | } |
| | | if (UpdProMap.keySet().size() > 0) update UpdProMap.values(); |
| | | } |
| | | List<Product2> por2Lsit = new List<Product2>(); |
| | | //查出产品的id和等级类别的字段 |
| | | por2Lsit = [select Id,Level_Category__c from Product2 where Id in : proIdList]; |
| | | for (Product2 pro: por2Lsit) { |
| | | pro.Level_Category__c = null; |
| | | prt2Map.put(pro.Id, pro); |
| | | } |
| | | System.debug('prt2Map='+prt2Map); |
| | | if (prt2Map.size() > 0) { |
| | | update prt2Map.values(); |
| | | } |
| | | } |
| | | } |