public without sharing class OpportunityLineItemTriggerHandler {

    public static void updateForDealer(List<OpportunityLineItem> newList, Map<Id, OpportunityLineItem> newMap, List<OpportunityLineItem> oldList, Map<Id, OpportunityLineItem> oldMap) {
        // if(StaticParameter.StageProgressBarUpdate){
        //     return;
        // }
       OpportunityLineItemTriggerHandler.test();
        List<String> pIdList = new List<String>();
        List<String> oIdList = new List<String>();
        for (OpportunityLineItem newOli : newList) {
            OpportunityLineItem oldOli = oldMap == null ? new OpportunityLineItem() : oldMap.get(newOli.Id);
            if (newOli.Product2Id != oldOli.Product2Id) {
                pIdList.add(newOli.Product2Id);
                oIdList.add(newOli.OpportunityId);
            }
        }
        system.debug('=====pIdList:' + pIdList);
        system.debug('=====oIdList:' + oIdList);

        Map<String, String> psMap = new Map<String, String>();
        if (pIdList.size() > 0) {
            List<Product_Search__c> psList = [select id, Product__c from Product_Search__c where Product__c = :pIdList];
            for (Product_Search__c ps : psList) {
                psMap.put(ps.Product__c, ps.Id);
            }    
        }        
        system.debug('=====psMap:' + psMap);

        Map<String, String> oppMap = new Map<String, String>();
        if (oIdList.size() > 0) {
            List<Opportunity> oppList = [select id, SyncedQuoteId from Opportunity where id = :oIdList];
            for (Opportunity opp : oppList) {
                oppMap.put(opp.Id, opp.SyncedQuoteId);
            }    
        }
        
        system.debug('=====oppMap:' + oppMap);
        for (OpportunityLineItem newOli : newList) {
            system.debug('20190507____' + newOli);
            OpportunityLineItem oldOli = oldMap == null ? new OpportunityLineItem() : oldMap.get(newOli.Id);
            if (UserInfo.getUserType() != 'PowerPartner' || oppMap.get(newOli.OpportunityId) != null) {
                if (newOli.Quantity != oldOli.Quantity) {
                    newOli.QuantityD__c = newOli.Quantity;
                }
                if (newOli.UnitPrice != oldOli.UnitPrice) {
                    newOli.UnitPriceD__c = newOli.UnitPrice;
                }
                if (newOli.Description != oldOli.Description) {
                    newOli.DescriptionD__c = newOli.Description;
                }
                if (newOli.Product2Id != oldOli.Product2Id) {
                    system.debug('=====Product2Id:' + newOli.Product2Id);
                    system.debug('=====OpportunityId;:' + newOli.OpportunityId);
                    newOli.Product_Search__c = psMap.get(newOli.Product2Id);
                }

            }
        }
    }

    public static void setTextField(List<OpportunityLineItem> newList, Map<Id, OpportunityLineItem> newMap, List<OpportunityLineItem> oldList, Map<Id, OpportunityLineItem> oldMap) {
        // if(StaticParameter.StageProgressBarUpdate){
        //     return;
        // }
        OpportunityLineItemTriggerHandler.test();
        for (OpportunityLineItem newOli : newList) {
             system.debug('!!!!!!!!!'+newOli.product_Description__c);
            OpportunityLineItem oldOli = oldMap == null ? new OpportunityLineItem() : oldMap.get(newOli.Id);
            if (newOli.Product2Id != oldOli.Product2Id) {
                newoli.Is_product_model_text__c = newoli.Is_product_model__c;
            }
            if(newOli.DescriptionD__c == null || newOli.DescriptionD__c == ''){

                newOli.DescriptionD__c = newoli.product_Description__c;
            }
            if(newOli.Description == null || newOli.Description == ''){
                newOli.Description = newoli.product_Description__c;
            }

        }
    }

    public static void SetProduct2Mark(List<OpportunityLineItem> newList, Map<Id, OpportunityLineItem> newMap, List<OpportunityLineItem> oldList, Map<Id, OpportunityLineItem> oldMap) {
        // if(StaticParameter.StageProgressBarUpdate){
        //     return;
        // }
        //XHL-20190510-AddStart
        OpportunityLineItemTriggerHandler.test();
        //获取询价的Id
        List<String> oIdList = new List<String>();
        for (OpportunityLineItem newOli : newList) { 
            oIdList.add(newOli.OpportunityId);
        } 
        //根据询价的Id，查找该询价中同步的报价
        Map<String, String> oppMap = new Map<String, String>();
        List<String> syncedQuoteIdList = new List<String>();
        List<Opportunity> oppList = new List<Opportunity>();
        if (oIdList.size() >0) {
            oppList = [select id, SyncedQuoteId,ProductSegment__c from Opportunity where id = :oIdList];
            for (Opportunity opp : oppList) {
                
                if ( String.isNotBlank(opp.SyncedQuoteId)) {
                    oppMap.put(opp.Id, opp.SyncedQuoteId);
                    syncedQuoteIdList.add(opp.SyncedQuoteId);
                }
                
            }    
        }
        

        List<OpportunityLineItem> opportunityLineItemList = new List<OpportunityLineItem>();
        
        if (syncedQuoteIdList.size() > 0) {
            //根据同步的报价Id，获取询价行项目的Id，以及报价产品的分类
            Map<String,QuoteLineItem> oliIdAndSingleProductMap = new Map<String,QuoteLineItem>();
            List<QuoteLineItem> quoteLineItemList = [
                    select Id,OpportunityLineItemId__c,SingleProduct__c,Set__c,SetName__c
                    from QuoteLineItem 
                    where QuoteId In :syncedQuoteIdList];
            for (QuoteLineItem qli : quoteLineItemList) {
                
                oliIdAndSingleProductMap.put(qli.OpportunityLineItemId__c, qli);
            }
            
            
            for (OpportunityLineItem newOli : newList) {

                    //因报价行项目中"询价行项目"的Id比报价行项目本身的Id短三位，因此作出如下处理
                    String newOliId = newOli.Id;
                    String newOliIdSub = newOliId.substring(0,newOliId.length()-3);
                    if (oliIdAndSingleProductMap.containsKey(newOliIdSub)) {
                    
                        OpportunityLineItem oli = new OpportunityLineItem();
                        oli.OLISingleProduct__c = oliIdAndSingleProductMap.get(newOliIdSub).SingleProduct__c;
                        oli.Set__c =  oliIdAndSingleProductMap.get(newOliIdSub).SetName__c;
                        oli.Id = newOli.Id;
                        opportunityLineItemList.add(oli);
                    }
                
            }
        } else {
            if (oppList.size() > 0 && oppList[0].ProductSegment__c == 'BS'){
                for (OpportunityLineItem newOli : newList) {
                        
                    OpportunityLineItem oli = new OpportunityLineItem();
                    oli.OLISingleProduct__c = true;
                    oli.Set__c =  '单品';
                    oli.Id = newOli.Id;
                    opportunityLineItemList.add(oli);
                        
                }
            }
            
        }
        

        if (opportunityLineItemList.size() > 0) {

            update opportunityLineItemList;
        }

        //XHL-20190510-AddEnd
    }





public static void test(){
    Integer i = 1;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
    i++;
}

}