liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
public with sharing class OpportunityLineItemTrigger {
    public static void setOpportunityAfter(List<OpportunityLineItem> newList,
        List<OpportunityLineItem> oldList, Map<Id, OpportunityLineItem> newMap, Map<Id, OpportunityLineItem> oldMap) {
        List<String> selIds = new List<String>();
        Map<String, OpportunityLineItem> deleteOpportunityLineItemMap = new Map<String, OpportunityLineItem>();
        if (Trigger.isInsert || Trigger.isUpdate) {
            // OpportunityLineItemのOpportunityIdを集める
            for (OpportunityLineItem oppli : newList) {
                if (oppli.OpportunityId != null) {
                    selIds.add(oppli.OpportunityId);
                }
            }
        } else if (Trigger.isDelete) {
            // OpportunityLineItemのOpportunityIdを集める
            for (OpportunityLineItem oppli : oldList) {
                if (oppli.OpportunityId != null) {
                    selIds.add(oppli.OpportunityId);
                    deleteOpportunityLineItemMap.put(oppli.Id, oppli);
                }
            }
        }
 
        List<OpportunityLineItem> retList =
            [Select Id, OpportunityId, Category3__c, Product2.MDM_Model_No__c, Opportunity.STMS_Package_Sale__c
             , Opportunity.State_Owner__c, Product2.Family, Opportunity.EnergyHost__c, Product2.Asset_Model_No__c
             from OpportunityLineItem
             where OpportunityId in: selIds
             order by OpportunityId];
 
system.debug('===retList==='+retList);
        if (retList.size() > 0) {
 
            Map<String, Map<String, OpportunityLineItem>> opportunityMap = new Map<String, Map<String, OpportunityLineItem>>();
 
            for (OpportunityLineItem oli : retList) {
                // 排除待删除的数据 防止对现有数据的干扰
                if (deleteOpportunityLineItemMap.size() > 0 && deleteOpportunityLineItemMap.containsKey(oli.Id)) {
                    continue;
                }
 
                Map<String, OpportunityLineItem> familyMap = new Map<String, OpportunityLineItem>();
                // 产品的第3分类 是 主机 或 能量主机
                if (String.isNotBlank(oli.Category3__c) && oli.Category3__c.contains('主机')) {
                    string mapkey = oli.Product2.Family + '_' + oli.Category3__c;
                    if (opportunityMap.containsKey(oli.OpportunityId)) {
                        familyMap = opportunityMap.get(oli.OpportunityId);
                    }
                    familyMap.put(mapkey, oli);
system.debug('==familyMap==='+familyMap);
                    opportunityMap.put(oli.OpportunityId, familyMap);
                }
            }
            Map<String, Opportunity> updateOpportunityMap = new Map<String, Opportunity>();
            Boolean flg_usg =false;
            Boolean flg_usg1 =false;
            for (OpportunityLineItem oli1 : retList) {
            system.debug('==oli1==='+oli1.id+'======='+oli1.Product2.Asset_Model_No__c);
                if(oli1.Product2.Asset_Model_No__c != null && oli1.Product2.Asset_Model_No__c.contains('USG-400')){
                    system.debug('==jinlai==='+oli1.id);
                    flg_usg1 = true;
                    break;
                }
            }    
            for (OpportunityLineItem oli : retList) {
                // 获取原字段值
                String oppId = oli.OpportunityId;
                //能量打包
                Boolean getOldStmsPackageSale  = oli.Opportunity.STMS_Package_Sale__c;
                //能量主机
                Boolean getOldEnergyHost  = oli.Opportunity.EnergyHost__c;
                Opportunity updateOpportunity = new Opportunity();
                updateOpportunity.Id = oppId;
                // 判断 询价产品中是否含有主机产品
                if (!updateOpportunityMap.containsKey(oppId)) {
                    if (opportunityMap.containsKey(oppId) ) {
                        Map<String, OpportunityLineItem> familyMap =   opportunityMap.get(oppId);
                        // 判断 询价产品是否含有 ENG 主机 产品
                        string mapkey1 = 'ENG_主机';
                        string mapkey2 = 'ENG_能量主机';
                        //if (oli.Product2.Asset_Model_No__c != null && oli.Product2.Asset_Model_No__c.contains('USG-400')) {
                        //     flg_usg = true;
                        // }
                        system.debug('==jinlai==familyMap==='+familyMap);
                        if (familyMap.containsKey(mapkey1) || familyMap.containsKey(mapkey2)) {
                            OpportunityLineItem FamMap1 = familyMap.get(mapkey1);
                            OpportunityLineItem FamMap2 = familyMap.get(mapkey2);
                            if((FamMap1 !=null && FamMap1.Product2.Asset_Model_No__c != null && FamMap1.Product2.Asset_Model_No__c.contains('USG-400')) || (FamMap2 !=null &&FamMap2.Product2.Asset_Model_No__c != null && FamMap2.Product2.Asset_Model_No__c.contains('USG-400'))){
                               flg_usg = true;
                            }  
                            system.debug('flg_usg=='+flg_usg+'========='+familyMap.size());      
                            // 只含有 ENG 主机产品
                            if (familyMap.size() == 1 ) {
                                //能量打包 False
                                //能量主机 True
                                //if (getOldStmsPackageSale || !getOldEnergyHost) {
                                    updateOpportunity.STMS_Package_Sale__c = false;
                                    //updateOpportunity.EnergyHost__c =  true;
                                    if(flg_usg || flg_usg1){
                                        updateOpportunity.EnergyHost__c =  true;
                                    }else{
                                        updateOpportunity.EnergyHost__c =  false;
                                    }
                                    updateOpportunityMap.put(oppId, updateOpportunity);
                                //}
                            } else {
                                //判断是否都是主机
                                Boolean principalMachine = true;
                                //判断是否都是能量主机
                                Boolean energyHost = true;
                                Boolean notContains = true;
                                Boolean principalMachinENG = false;
                                
                                for(String key :familyMap.keySet()) {
 
 
                                    OpportunityLineItem mapOli = familyMap.get(key);
                                    System.debug('mapOli--->'+mapOli);
                                    if (!'能量主机'.equals(mapOli.Category3__c)) {
                                        energyHost = false;
                                        System.debug('test--1-->');
                                        if(!'ENG'.equals(mapOli.Product2.Family)){
                                            principalMachine = false;
                                        } 
                                        else {
                                            principalMachinENG = true;
                                        }
                                    } 
                                    
                                }
                                system.debug(energyHost+'===='+notContains+'===='+principalMachinENG);
                                
                                system.debug('==='+flg_usg1);
                                if (energyHost ) {
                                    // 都是能量主机
                                    //能量打包 False
                                    //能量主机 True
                                    system.debug('能量打包=='+getOldStmsPackageSale+'==能量主机=='+getOldEnergyHost);
                                    //if (getOldStmsPackageSale || !getOldEnergyHost) {
                                        updateOpportunity.STMS_Package_Sale__c = false;
                                        //updateOpportunity.EnergyHost__c =  true;
                                        if(flg_usg1){
                                            updateOpportunity.EnergyHost__c =  true;
                                        }else{
                                            updateOpportunity.EnergyHost__c =  false;
                                        }
                                        updateOpportunityMap.put(oppId, updateOpportunity);
                                    //}     
                                   
                                   
                                } else {
                                    if (principalMachine) {
                                        // 只有 ENG 主机
                                        //能量打包 False
                                        //能量主机 True
                                        //if (getOldStmsPackageSale || !getOldEnergyHost) {
                                            updateOpportunity.STMS_Package_Sale__c = false;
                                            //updateOpportunity.EnergyHost__c =  true;
                                            if(flg_usg1){
                                                updateOpportunity.EnergyHost__c =  true;
                                            }else{
                                                updateOpportunity.EnergyHost__c =  false;
                                            }
                                            updateOpportunityMap.put(oppId, updateOpportunity);
                                        //}     
                                    } else {
                                        if (principalMachinENG ) {
                                            // 能量打包 True
                                            // 能量主机 False
                                            //if (!getOldStmsPackageSale || getOldEnergyHost) {
                                                updateOpportunity.STMS_Package_Sale__c = true;
                                                //updateOpportunity.EnergyHost__c =  false;
                                                if(flg_usg1){
                                                    updateOpportunity.EnergyHost__c =  true;
                                                }else{
                                                    updateOpportunity.EnergyHost__c =  false;
                                                }
                                                updateOpportunityMap.put(oppId, updateOpportunity);
                                            //}    
                                        } else {
                                            //if (getOldStmsPackageSale || getOldEnergyHost) {
                                                updateOpportunity.STMS_Package_Sale__c = false;
                                                //updateOpportunity.EnergyHost__c =  false;
                                                if(flg_usg1){
                                                    updateOpportunity.EnergyHost__c =  true;
                                                }else{
                                                    updateOpportunity.EnergyHost__c =  false;
                                                }
                                                updateOpportunityMap.put(oppId, updateOpportunity);
 
                                            //}
                                        }
                                    }
                                }
                                /*
                                // 含有 多种 主机 产品
                                // 能量打包 True
                                // 能量主机 False
                                if (!getOldStmsPackageSale || getOldEnergyHost) {
                                    updateOpportunity.STMS_Package_Sale__c = true;
                                    updateOpportunity.EnergyHost__c =  false;
                                    updateOpportunityMap.put(oppId, updateOpportunity);
                                }
                                */
                            }
                        } else {
                            // 不含有 ENG 主机 产品
                            // 能量打包 False
                            // 能量主机 False
                            if (getOldStmsPackageSale || getOldEnergyHost) {
                                updateOpportunity.STMS_Package_Sale__c = false;
                                updateOpportunity.EnergyHost__c =  false;
                                updateOpportunityMap.put(oppId, updateOpportunity);
                            }
                        }
                    } else {
                        // 不含有 主机 产品
                        // 能量打包 False
                        // 能量主机 False
                        if (getOldStmsPackageSale || getOldEnergyHost) {
                            updateOpportunity.STMS_Package_Sale__c = false;
                            updateOpportunity.EnergyHost__c =  false;
                            updateOpportunityMap.put(oppId, updateOpportunity);
 
                        }
 
                    }
                }
            }
 
 
            if (updateOpportunityMap.size() > 0) {
                update updateOpportunityMap.values();
            }
 
        }
 
        // String opportunityId = null;
        // // STMS打包销售
        // boolean flg_category3 = false;
        // boolean flg_mdm = false;
        // boolean stms_package_sale = false;
        // boolean oldStmsPackageSale = false;
        // //boolean state_Owner = false;
        // List<Opportunity> oppUpd = new List<Opportunity>();
        // //項目判断
        // for (OpportunityLineItem oli : retList) {
        //     if (opportunityId == null) {
        //         opportunityId = oli.OpportunityId;
        //         oldStmsPackageSale = oli.Opportunity.STMS_Package_Sale__c;
        //     }
 
        //     if (opportunityId != oli.OpportunityId) {
        //         //stms_package_sale = ((flg_category3 == true && flg_mdm == true) || state_Owner == true) ? true : false;
        //         stms_package_sale = (flg_category3 == true && flg_mdm == true) ? true : false;
        //         if (stms_package_sale != oldStmsPackageSale) {
        //             Opportunity opportunity = new Opportunity(
        //                 Id = opportunityId,
        //                 STMS_Package_Sale__c = stms_package_sale
        //             );
        //             oppUpd.add(opportunity);
        //         }
        //         opportunityId = oli.OpportunityId;
        //         oldStmsPackageSale = oli.Opportunity.STMS_Package_Sale__c;
        //         flg_category3 = false;
        //         flg_mdm = false;
        //         stms_package_sale = false;
        //     }
 
        //     if (oli.Category3__c != null && oli.Category3__c.contains('主机')) { //zh 2020/09/15
        //         flg_category3 = true;
        //     }
        //     if (oli.Product2.MDM_Model_No__c != null && oli.Product2.MDM_Model_No__c.contains('USG-400')) {
        //         flg_mdm = true;
        //     }
        //     //if (oli.Opportunity.State_Owner__c == '能量') {
        //     //  state_Owner = true;
        //     //}
        // }
 
        // //stms_package_sale = ((flg_category3 == true && flg_mdm == true) || state_Owner == true) ? true : false;
        // stms_package_sale = (flg_category3 == true && flg_mdm == true) ? true : false;
        // if (opportunityId != null && stms_package_sale != oldStmsPackageSale) {
        //     Opportunity opportunity = new Opportunity(
        //         Id = opportunityId,
        //         STMS_Package_Sale__c = stms_package_sale
        //     );
        //     oppUpd.add(opportunity);
        // }
 
        // if (oppUpd.size() > 0) {
        //     update oppUpd;
        // }
 
        //List<Opportunity> oppProductEPT1 = new List<Opportunity>();
        //CHAN-BCNCRB ---XHL----Start-----
        //Map<String,String> appMap = new Map<String,String>();
        //for (OpportunityLineItem oli : retList) {
        //  appMap.put(oli.OpportunityId,oli.OpportunityId);
        //}
        //Map<String,Opportunity> oppProductEPT1Map = new Map<String,Opportunity>();
        //for (OpportunityLineItem oli : retList) {
 
        //  Opportunity opportunity = new Opportunity();
        //  opportunity.Id = oli.OpportunityId;
        //  if (oli.Product2.MDM_Model_No__c != null && (oli.Product2.MDM_Model_No__c.contains('EP-T1(A)') ||
        //          oli.Product2.MDM_Model_No__c.contains('EP-T1(B)'))) {
        //      opportunity.If_Have_EP_T1__c = true;
        //      oppProductEPT1Map.put(oli.OpportunityId,opportunity);
        //  } else {
        //      opportunity.If_Have_EP_T1__c = false;
        //      if (!oppProductEPT1Map.containsKey(oli.OpportunityId)) {
        //          oppProductEPT1Map.put(oli.OpportunityId,opportunity);
        //      }
        //  }
        //  //oppProductEPT1Map.put(oli.OpportunityId,opportunity);
        //}
        //if (oppProductEPT1Map.size() > 0) {
        //  update oppProductEPT1Map.values();
        //}
        //if (oppProductEPT1.size() > 0) {
        //  update oppProductEPT1;
        //}
        //CHAN-BCNCRB ---XHL----End-----
 
    }
 
}