public without sharing class QuoteTriggerHandler {

	public static void setDealerGroup(List<Quote> newList, Map<Id, Quote> newMap, List<Quote> oldList, Map<Id, Quote> oldMap) {
        List<String> oppIdList = new List<String>();
        for (Quote newQuote : newList) {
            oppIdList.add(newQuote.OpportunityId);
        }

        if (oppIdList.size() > 0) {
            List<OpportunityTeamMember> otmList = [select Id, UserId, User.ProfileId from OpportunityTeamMember where opportunityId = :oppIdList];
            List<OpportunityTeamMember> delList = new List<OpportunityTeamMember>();
            for (OpportunityTeamMember otm : otmList) {
                String profileId = otm.User.ProfileId;
                if (profileId.substring(0, 15) == System.Label.RT_BS_No_Price ||
                    profileId.substring(0, 15) == System.Label.RT_BS_No_Price_Email) {
                    delList.add(otm);
                }
            }

            if (delList.size() > 0) delete delList;
    }

       
	}

    public static void setPaymetTerms(List<Quote> newList, Map<Id, Quote> newMap, List<Quote> oldList, Map<Id, Quote> oldMap){
        System.debug('1111');
        Map<String,Quote> oppMap = new Map<String,Quote>();
        //String quoId = '';
        for (Quote newQuote : newList) {
            Quote old = oldMap.get(newQuote.Id);
            System.debug(newQuote.PaymentTerms__c);
            System.debug(old.PaymentTerms__c);
            if(newQuote.PaymentTerms__c != old.PaymentTerms__c){
                oppMap.put(newQuote.OpportunityId, newQuote);
            }
           /* if(newQuote.ProductSegment__c == 'BS' && newQuote.Is_Decided__c != oldMap.get(newQuote.iD).Is_Decided__c){
                quoId = newQuote.Id;
            }*/
        }
        System.debug(oppMap.keySet().size());
        if(oppMap.keySet().size()>0){
            System.debug(oppMap.keySet());
            List<Order> orderList = [select Id,OpportunityId from Order where Status__c = 'Active' and OpportunityId = :oppMap.keySet()];
            System.debug(orderList);
            if(orderList.size() > 0){
                System.debug('22222222222');
                for(Order odr : orderList){
                    odr.PaymentTerms__c = oppMap.get(odr.OpportunityId).PaymentTerms__c;
                }
                System.debug(orderList);
                update orderList;
            }
        }

        /*if(quoId != '' && quoId != null){
            List<QuoteLineItem> qliList = [select id,DontSingleProduct__c from QuoteLineItem where QuoteId = :quoId];
            String temp = '';
            for(QuoteLineItem qli : qliList){
                if(qli.DontSingleProduct__c != '1'){
                    temp+=(' '+qli.DontSingleProduct__c);
                }
            }
            Quote quo = new Quote();
            quo.id = quoId;
            quo.DontSingleProductCode__c = temp;

            if(temp.length() >0) update quo;
        }*/
    }

    public static void setDontSingleProductCode(List<Quote> newList, Map<Id, Quote> newMap, List<Quote> oldList, Map<Id, Quote> oldMap){ 
        //String quoId = '';

        Quote quo = newList[0];
        // Opportunity opp = [select Id,Dealer__c from Opportunity where Id = :quo.OpportunityId];
        // if(opp.Dealer__c != '0012800001HoORb' && opp.Dealer__c != '0012800001HoPY4' && opp.Dealer__c != '0012800001HoPaz'){
        // WLIG-BVP4L2 触发101 新建一个公式字段OppDealer__c，去掉上面的检索
        if(quo.OppDealer__c != '0012800001HoORb' && quo.OppDealer__c != '0012800001HoPY4' && quo.OppDealer__c != '0012800001HoPaz'){

            //if(quo.ProductSegment__c == 'BS' && quo.Is_Decided__c != oldMap.get(quo.Id).Is_Decided__c && quo.Is_Decided__c == true){
                List<QuoteLineItem> qliList = [select id,DontSingleProduct__c from QuoteLineItem where QuoteId = :quo.Id];
                String temp = '';
                for(QuoteLineItem qli : qliList){
                    if(qli.DontSingleProduct__c != '1'){
                        temp+=(' '+qli.DontSingleProduct__c);
                    }
                }
                quo.DontSingleProductCode__c = temp;
           // }
        }

    }

    public static void check(List<Quote> newList, Map<Id, Quote> newMap, List<Quote> oldList, Map<Id, Quote> oldMap){
        //if(newList[0].Is_Decided__c == true){
            String quoid =  newList[0].id;
            List<QuoteLineItem> qliList= [select id,Product2.NMPAStatus__c,Product2.ProductCode,Product2.ProductStatusFormula__c,QuoLiOrderIsChange__c,quoLiVerifyProductValid__c from QuoteLineItem where QuoteId = :quoid];
            String str = '';
            Boolean isok = true;
            for(QuoteLineItem qli : qliList){
                // WLIG-BTA8C2 XHL 20200925 -Start
                // 合同变更 并且 报价产品的 验证产品有效性 为 false 时跳过验证
                if (qli.QuoLiOrderIsChange__c && qli.quoLiVerifyProductValid__c == false) {
                // WLIG-BTA8C2 XHL 20200925 -End
                } else {
                    if(qli.Product2.ProductStatusFormula__c == '0' ){
                        if(newList[0].Is_Decided__c == true && oldList[0].Is_Decided__c == false){
                            str += (' '+qli.Product2.ProductCode);
                            isok = false;
                        }else{
                            
                        }
                    }   
                }
                
            }
            if(isok == false){
                newList[0].addError(str + '产品状态不正确。');
            }
        //}

   } 

}