GWY
2022-05-21 a3460549533111815e7f73d6cef601a58031525d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
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++;
}
 
}