public without sharing class Product2TriggerHandler {

    public static void upsertProductSearch(List<Product2> newList, Map<Id, Product2> newMap, List<Product2> oldList, Map<Id, Product2> oldMap) 
    {
        if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate))
        {
            List<String> p2ids = new List<String>();
            for(Product2 p2 : newList)
            {
                p2ids.add(p2.id);
            }
            
            List<Product_Search__c> ps = [select Id,Name,CurrencyIsoCode,Product__c From Product_Search__c Where Product__c in: p2ids];
            Map <String,Product_Search__c> psmap = new Map<String,Product_Search__c>();
            List<Product_Search__c> pss = new List<Product_Search__c>();
            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<Product2> newList, Map<Id, Product2> newMap, List<Product2> oldList, Map<Id, Product2> oldMap) {
        if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate)){
            List<String> p2ids = new List<String>();
            for(Product2 p2 : newList)
            {
                p2ids.add(p2.id);
            }

            //存放查找的soql文
            List<Product_Register_Link__c> pro2List = new List<Product_Register_Link__c>();
            pro2List = [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 Product2__r.Id in : p2ids];
            System.debug('pro2List='+pro2List);
            //存放产品的id和等级类别字段的值的集合
            Map<Id,List<String>> pro2Map = new Map<Id,List<String>>();
            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<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);

            //通过上面的循环，把关系表过获取的数据读出来，传给map
            if (pro2Map.size()>0) {
                for (Product_Register_Link__c pr: pro2List) {
                    if (pro2Map.containsKey(pr.Product2__r.Id)) {
                        if (newMap.get(pr.Product2__r.Id).demoteer_Sap__c <> null) {
                            //生产日期在注册证有效期内的是二类，生产日期在备案证有效期的是一类，前提为有效期
                            //System.debug('pr.Product_Register__r.ValidTo__c='+ pr.Product_Register__r.ValidTo__c);
                            //System.debug('pr.Product_Register__r.MedPrdClass__c='+pr.Product_Register__r.MedPrdClass__c);
                            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')) {
                                prlMap.put(pr.Product2__r.Id, '一类');
                            }
                            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 (!'一类'.equals(prlMap.get(pr.Product2__r.Id))){
                                    prlMap.put(pr.Product2__r.Id, '二类');
                                }
                            }
                        }
                    }
                    System.debug('prlMap='+prlMap);
                }
            }

            //给产品上的等级类别字段赋值
            for (Product2 pr2: newList) {
                 System.debug('pr2.demoteer_Sap__c='+pr2.demoteer_Sap__c);
                if (!pro2Map.containsKey(pr2.Id)) {
                    //避免从sap传来的时间取消了，等级类别字段上还有值这一说
                    if (pr2.demoteer_Sap__c == null) {
                        pr2.Level_Category__c = null;
                    }else{
                        pr2.Level_Category__c = '非监管';
                    }
                }else{
                //else if (prlMap.containsKey(pr2.Id)) {
                    pr2.Level_Category__c = prlMap.get(pr2.Id);
                }
                System.debug('pr2.Level_Category__c='+pr2.Level_Category__c);
            }
        }
    }     
}