public without sharing class ProRegisterHandler {
|
|
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) {
|
|
// 产品-注册证关系 中所有 产品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, registrationCode__c, clinical_product_code__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.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) {
|
if (Trigger.isAfter && Trigger.isUpdate) {
|
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,Product_Register__r.MedPrdClass__c,Product_Register__r.ValidTo__c,Product_Register__r.ValidFrom__c
|
from Product_Register_Link__c
|
where Product_Register__r.Id in : prcList];
|
|
//存放产品的id和等级类别字段的值的集合
|
Map<Id,List<String>> pro2Map = new Map<Id,List<String>>();
|
if (prlList.size()>0) {
|
for (Product_Register_Link__c prl: prlList) {
|
if (pro2Map.containsKey(prl.Product2__r.Id)) {
|
pro2Map.get(prl.Product2__r.Id).add(prl.Product_Register__r.MedPrdClass__c);
|
}else {
|
pro2Map.put(prl.Product2__r.Id, new List<String>());
|
pro2Map.get(prl.Product2__r.Id).add(prl.Product_Register__r.MedPrdClass__c);
|
}
|
}
|
}
|
|
//存放产品id和字符串值
|
Map<Id,String> prlMap = new Map<Id,String>();
|
System.debug('pro2Map='+pro2Map);
|
|
List<Id> pidList = new List<Id>();
|
//通过上面的循环,把关系表过获取的数据读出来,传给map
|
if (pro2Map.size()>0) {
|
for (Product_Register_Link__c pr: prlList) {
|
// && (oldMap.get(pr.Product_Register__r.Id).ValidTo__c != newMap.get(pr.Product_Register__r.Id).ValidTo__c || oldMap.get(pr.Product_Register__r.Id).ValidFrom__c != newMap.get(pr.Product_Register__r.Id).ValidFrom__c)
|
if (pro2Map.containsKey(pr.Product2__r.Id)) {
|
pidList.add(pr.Product2__r.Id);
|
//生产日期在注册证有效期内的是二类,生产日期在备案证有效期的是一类,前提为有效期
|
// System.debug('pr.Product_Register__r.ValidTo__c='+ pr.Product_Register__r.ValidTo__c);
|
// System.debug('pr.Product_Register__r.Level_Category__c='+pr.Product_Register__r.MedPrdClass__c);
|
// System.debug('pr.Product2__r.demoteer_Sap__c='+ pr.Product2__r.demoteer_Sap__c);
|
if (pr.Product2__r.demoteer_Sap__c <= newMap.get(pr.Product_Register__r.Id).ValidTo__c && pr.Product2__r.demoteer_Sap__c >= newMap.get(pr.Product_Register__r.Id).ValidFrom__c
|
&& (pr.Product_Register__r.MedPrdClass__c == '1')) {
|
prlMap.put(pr.Product2__r.Id, '一类');
|
}
|
else if (pr.Product2__r.demoteer_Sap__c <= newMap.get(pr.Product_Register__r.Id).ValidTo__c && pr.Product2__r.demoteer_Sap__c >= newMap.get(pr.Product_Register__r.Id).ValidFrom__c
|
&& pr.Product_Register__r.MedPrdClass__c == '2' || pr.Product_Register__r.MedPrdClass__c == '3') {
|
if (!'一类'.equals(prlMap.get(pr.Product2__r.Id))){
|
prlMap.put(pr.Product2__r.Id, '二类');
|
}
|
}
|
}
|
//System.debug('prlMap='+prlMap);
|
}
|
}
|
|
List<Product2> por2Lsit = new List<Product2>();
|
//查出产品的id和等级类别的字段
|
por2Lsit = [select Id,Level_Category__c from Product2 where Id in : pidList];
|
|
//List<Product2> ppr2List = new List<Product2>();
|
//给产品上的等级类别字段赋值
|
for (Product2 pr2: por2Lsit) {
|
//Product2 ppr2;
|
if (!pro2Map.containsKey(pr2.Id)) {
|
pr2.Level_Category__c = '非监管';
|
}else if (prlMap.containsKey(pr2.Id)) {
|
pr2.Level_Category__c = prlMap.get(pr2.Id);
|
}else {
|
pr2.Level_Category__c = null;
|
}
|
if (pr2.Level_Category__c != null) {
|
prt2Map.put(pr2.Id, pr2);
|
}
|
//prt2.Id = pr2.Id;
|
//prt2.Level_Category__c = pr2.Level_Category__c;
|
//ppr2List.add(ppr2);
|
//System.debug('ppr2List='+ppr2List);
|
//System.debug('pr2.Level_Category__c='+pr2.Level_Category__c);
|
}
|
update prt2Map.values();
|
}
|
}
|
|
}
|