public without sharing class Product2TriggerHandler { public static void upsertProductSearch(List newList, Map newMap, List oldList, Map oldMap) { if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate)) { List p2ids = new List(); for(Product2 p2 : newList) { p2ids.add(p2.id); } List ps = [select Id,Name,CurrencyIsoCode,Product__c From Product_Search__c Where Product__c in: p2ids]; Map psmap = new Map(); List pss = new List(); for(Product_Search__c p : ps) { psmap.put(p.Product__c,p); } for(Product2 p2 : newList) { Product_Search__c p2s; if(psmap.containsKey(p2.id)) { p2s = psmap.get(p2.id); } else { p2s = new Product_Search__c(); } p2s.Name = p2.Name; p2s.Product__c = p2.id; p2s.CurrencyIsoCode = p2.CurrencyIsoCode; pss.add(p2s); } System.debug('psssize' + pss.size()); upsert(pss); }else if(Trigger.isDelete) { for(Product2 en : newList) { en.addError('此数据不能删除'); } } } //用于降类 public static void checkProduct2Level(List newList, Map newMap, List oldList, Map oldMap) { //取产品id List p2ids = new List(); for(Product2 p2 : newList) { p2ids.add(p2.id); } //存放查找的soql文,关系表里的产品和注册证相关字段 List pro2List = new List(); pro2List = [select Product2__r.id,Product2__r.demoteer_Sap__c,Product2__r.Diedatvanink__c,Product_Register__r.Stelsedag__c,Product_Register__r.MedPrdClass__c,Product_Register__r.ValidTo__c,Product_Register__r.ValidFrom__c,Product_Register__c,Product2__c from Product_Register_Link__c where Product2__r.Id in : p2ids]; System.debug('pro2List='+pro2List); //存放产品的id和注册证医疗器械分类的值的集合 Map> pro2Map = new Map>(); //存放注册证上最前的维护日,一类证优先,否则二类证。 Map prSteMap = new Map();//存一类维护日 //Map prSte1Map = new Map();//存二类维护日 if (pro2List.size()>0) { for (Product_Register_Link__c prl: pro2List) { //有医疗器械分类就增加,没有就放进去 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()); pro2Map.get(prl.Product2__r.Id).add(prl.Product_Register__r.MedPrdClass__c); } //一类证.维护日不为空.产品生产日期在注册证有效期之内,下面的是二类 if (prl.Product_Register__r.MedPrdClass__c == '1' && prl.Product_Register__r.Stelsedag__c <> null && (newMap.get(prl.Product2__r.Id).demoteer_Sap__c <= prl.Product_Register__r.ValidTo__c && newMap.get(prl.Product2__r.Id).demoteer_Sap__c >= prl.Product_Register__r.ValidFrom__c)) { if (prSteMap.containsKey(prl.Product2__r.Id)) { //这一步,map可能有id,但是值是null,所以是null时也给他赋值 if (prSteMap.get(prl.Product2__r.Id) > prl.Product_Register__r.Stelsedag__c || prSteMap.get(prl.Product2__r.Id) == null) { prSteMap.put(prl.Product2__r.Id,prl.Product_Register__r.Stelsedag__c); } }else { prSteMap.put(prl.Product2__r.Id,prl.Product_Register__r.Stelsedag__c); } }else { //不符合就赋空,和上面的判断条件有关联*** if (!prSteMap.containsKey(prl.Product2__r.Id) || prSteMap.get(prl.Product2__r.Id) == null) { prSteMap.put(prl.Product2__r.Id,null); } } /*//和上面一样 if ((prl.Product_Register__r.MedPrdClass__c == '2' || prl.Product_Register__r.MedPrdClass__c == '3') && prl.Product_Register__r.Stelsedag__c <> null && (newMap.get(prl.Product2__r.Id).demoteer_Sap__c <= prl.Product_Register__r.ValidTo__c && newMap.get(prl.Product2__r.Id).demoteer_Sap__c >= prl.Product_Register__r.ValidFrom__c)) { System.debug(prSte1Map.containsKey(prl.Product2__r.Id)); System.debug(prSte1Map); if (prSte1Map.containsKey(prl.Product2__r.Id)) { System.debug(prSte1Map.get(prl.Product2__r.Id) > prl.Product_Register__r.Stelsedag__c); if (prSte1Map.get(prl.Product2__r.Id) > prl.Product_Register__r.Stelsedag__c || prSte1Map.get(prl.Product2__r.Id) == null) { prSte1Map.put(prl.Product2__r.Id,prl.Product_Register__r.Stelsedag__c); } }else { prSte1Map.put(prl.Product2__r.Id,prl.Product_Register__r.Stelsedag__c); } }else { if (!prSte1Map.containsKey(prl.Product2__r.Id) || prSte1Map.get(prl.Product2__r.Id) == null) { prSte1Map.put(prl.Product2__r.Id,null); } }*/ } } //存放产品id和字符串值 Map prlMap = new Map(); Map prl1Map = new Map(); Map prl2Map = new Map(); Map prl3Map = new Map(); System.debug('pro2Map='+pro2Map); System.debug('prSteMap='+prSteMap); /*System.debug('prSte1Map='+prSte1Map);*/ Date stedate; //通过上面的循环,把关系表过获取的数据读出来,传给map,一共分为3个map,要注意 if (pro2Map.size()>0) { for (Product_Register_Link__c pr: pro2List) { //保证id在上面的map里存在,但是这一步有没有好像没啥区别,又没错误,索性不删掉 //&& (prSteMap.containsKey(pr.Product2__r.Id)) || prSte1Map.containsKey(pr.Product2__r.Id) if (pro2Map.containsKey(pr.Product2__r.Id)) { //取得是产品的生产日期在注册证的有效期内的注册证上面的维护日,一类优先,其次二类 /*stedate = prSteMap.get(pr.Product2__r.Id)==null?(prSte1Map.get(pr.Product2__r.Id)==null?null:prSte1Map.get(pr.Product2__r.Id)):prSteMap.get(pr.Product2__r.Id);*/ stedate = prSteMap.get(pr.Product2__r.Id); //保证产品上的生产日有值 if (newMap.get(pr.Product2__r.Id).demoteer_Sap__c <> null) { //注册证维护日有值 if (stedate <> null) { //入库日小于等于维护日 if (newMap.get(pr.Product2__r.Id).Diedatvanink__c == null || newMap.get(pr.Product2__r.Id).Diedatvanink__c <= stedate) { if (newMap.get(pr.Product2__r.Id).demoteer_Sap__c <= pr.Product_Register__r.ValidTo__c && newMap.get(pr.Product2__r.Id).demoteer_Sap__c >= pr.Product_Register__r.ValidFrom__c && (pr.Product_Register__r.MedPrdClass__c == '2' || pr.Product_Register__r.MedPrdClass__c == '3')) { System.debug('2'); if (prlMap.get(pr.Product2__r.Id) == null || !prlMap.containsKey(pr.Product2__r.Id)) { prlMap.put(pr.Product2__r.Id, '二类'); }else { String Lins = prlMap.get(pr.Product2__r.Id); Lins += '二类'; prlMap.put(pr.Product2__r.Id, Lins); } }else if (newMap.get(pr.Product2__r.Id).demoteer_Sap__c <= pr.Product_Register__r.ValidTo__c && newMap.get(pr.Product2__r.Id).demoteer_Sap__c >= pr.Product_Register__r.ValidFrom__c && pr.Product_Register__r.MedPrdClass__c == '1') { System.debug('1'); if (prlMap.get(pr.Product2__r.Id) == null || !prlMap.containsKey(pr.Product2__r.Id)) { prlMap.put(pr.Product2__r.Id, '一类'); }else { String Lins = prlMap.get(pr.Product2__r.Id); Lins += '一类'; prlMap.put(pr.Product2__r.Id, Lins); } } //入库日大于维护日 }else if (newMap.get(pr.Product2__r.Id).Diedatvanink__c <> null && newMap.get(pr.Product2__r.Id).Diedatvanink__c > stedate) { if (newMap.get(pr.Product2__r.Id).demoteer_Sap__c <= pr.Product_Register__r.ValidTo__c && newMap.get(pr.Product2__r.Id).demoteer_Sap__c >= pr.Product_Register__r.ValidFrom__c && pr.Product_Register__r.MedPrdClass__c == '1') { if (prl1Map.get(pr.Product2__r.Id) == null || !prl1Map.containsKey(pr.Product2__r.Id)) { prl1Map.put(pr.Product2__r.Id, '一类'); }else { String Lins = prl1Map.get(pr.Product2__r.Id); Lins += '一类'; prl1Map.put(pr.Product2__r.Id, Lins); } }else if (newMap.get(pr.Product2__r.Id).demoteer_Sap__c <= pr.Product_Register__r.ValidTo__c && newMap.get(pr.Product2__r.Id).demoteer_Sap__c >= pr.Product_Register__r.ValidFrom__c && pr.Product_Register__r.MedPrdClass__c == '2' || pr.Product_Register__r.MedPrdClass__c == '3') { if (prl1Map.get(pr.Product2__r.Id) == null || !prl1Map.containsKey(pr.Product2__r.Id)) { prl1Map.put(pr.Product2__r.Id, '二类'); }else { String Lins = prl1Map.get(pr.Product2__r.Id); Lins += '二类'; prl1Map.put(pr.Product2__r.Id, Lins); } } } }else if (stedate == null && newMap.get(pr.Product2__r.Id).demoteer_Sap__c <= pr.Product_Register__r.ValidTo__c && newMap.get(pr.Product2__r.Id).demoteer_Sap__c >= pr.Product_Register__r.ValidFrom__c && (pr.Product_Register__r.MedPrdClass__c == '2' || pr.Product_Register__r.MedPrdClass__c == '3')) { prl3Map.put(pr.Product2__r.Id, '二类'); }else { //维护日空,也没有二类证,这个map置空 prl2Map.put(pr.Product2__r.Id, '空'); } } } } } System.debug('prlMap='+prlMap); System.debug('prl1Map='+prl1Map); System.debug('prl2Map='+prl2Map); //给产品上的等级类别字段赋值 for (Product2 pr2: newList) { if (prlMap.containsKey(pr2.Id)) { if ((prlMap.get(pr2.Id).contains('二类') && prlMap.get(pr2.Id).contains('一类')) || (!prlMap.get(pr2.Id).contains('一类') && prlMap.get(pr2.Id).contains('二类'))){ pr2.Level_Category__c = '二类'; }else if (prlMap.get(pr2.Id).contains('一类') && !prlMap.get(pr2.Id).contains('二类')) { pr2.Level_Category__c = '一类'; } }else if (prl1Map.containsKey(pr2.Id)) { if (prl1Map.get(pr2.Id).contains('一类')) { pr2.Level_Category__c = '一类'; }else if (prl1Map.get(pr2.Id).contains('二类') && !prl1Map.get(pr2.Id).contains('一类')) { pr2.Level_Category__c = '二类'; } }else if (prl3Map.containsKey(pr2.Id)) { pr2.Level_Category__c = '二类'; }else if (prl2Map.containsKey(pr2.Id)) { //为了避免麻烦与后续修改,统一非监管了。 pr2.Level_Category__c = '非监管'; } //生产日无值,等级类别置空 if (pr2.demoteer_Sap__c == null) { pr2.Level_Category__c = null; } System.debug('pr2.Level_Category__c='+pr2.Level_Category__c); } } }