高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
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
328
329
330
331
332
333
334
335
336
337
public without sharing class Product2Handler extends Oly_TriggerHandler {
    private Map<Id, Product2> newMap;
    private Map<Id, Product2> oldMap;
    private List<Product2> newList;
    private List<Product2> oldList;
 
    public Product2Handler() {
        this.newMap = (Map<Id, Product2>) Trigger.newMap;
        this.oldMap = (Map<Id, Product2>) Trigger.oldMap;
        this.newList = (List<Product2>) Trigger.new;
        this.oldList = (List<Product2>) Trigger.old;
    }
 
    protected override void beforeInsert() {
        beforeSetValue();
        // SWAG-BHE9GK LHJ 竞争对手产品自动编码 Start
        SetCompetitorCode();
        // SWAG-BHE9GK LHJ 竞争对手产品自动编码 End
    }
    protected override void beforeUpdate() {
        beforeSetValue();
        //SFDC停止预警  lt  20211015 add start
        // FlgStopManufacure(this.newList, this.oldMap);
        //SFDC停止预警  lt  20211015 add end
        FlgStopManufacure();
 
    }
 
    //SFDC停止预警  lt  20210922 add start
    protected override void afterUpdate(){
        Calculate_AverageMonthSales(this.newList, this.oldMap);
    }
    //SFDC停止预警  lt  20210922 add end
 
    // SWAG-BHE9GK LHJ 竞争对手产品自动编码 Start
    private void SetCompetitorCode() {
        Boolean SelctFlg = False;
        list<AggregateResult> proList;
        String strPC = '';
        Integer i = 1;
 
        for (Product2 nObj : newList) {
            if (nObj.Competitor_product_dummy__c) {
                SelctFlg = True;
            }
        }
        if (SelctFlg) {
            proList = [Select Max(ProductCode) PC From Product2 Where ProductCode Like 'SD%'];
            if (proList != null && proList.size() > 0) {
                strPC = String.valueof(proList[0].get('PC'));
            }
        }
        for (Product2 nObj : newList) {
            if (nObj.Competitor_product_dummy__c && strPC.length() == 8
                    && proList != null && proList.size() > 0 ) {
                Integer num = Integer.valueOf(strPC.SubString(2,strPC.length())) + i;
                nObj.ProductCode  = 'SD' + String.valueOf(num).leftpad(6,'0');
                i++;
            }
        }
    }
    // SWAG-BHE9GK LHJ 竞争对手产品自动编码 End
 
    private void beforeSetValue() {
 
        for (Product2 nObj : newList) {
            //CHAN-B8GCBB
            if (nObj.WhiteSpace__c == true) {
                nObj.StorageStatus__c = String.valueOf(nObj.StorageStatusNo__c);
            } else {
                nObj.StorageStatus__c = '';
            }
            //CHAN-B8GCBB
 
            nObj.Fixture_Model_No_T__c = nObj.Fixture_Model_No_F__c;
            // CBPR LHJ
            if (Trigger.isInsert || 
                    (Trigger.isUpdate && nObj.SFDA_Status_New__c != oldMap.get(nObj.id).SFDA_Status_New__c)) {
                nObj.SFDA_Status__c = nobj.SFDA_Status_New__c;
                if (nobj.SFDA_Status_New__c == '新製品申請中' || nobj.SFDA_Status_New__c == '準備中') {
                    nObj.SFDA_Status__c = '停止';
                }
            }
            // CBPR LHJ
            
            // SWAG-BHE9GK LHJ 竞争对手产品自动编码 Start
            if (Trigger.isInsert) {
 
            }
            // SWAG-BHE9GK LHJ 竞争对手产品自动编码 End
        }
    }
 
    //SFDC停止预警  lt  20210914 add start
    private static void Calculate_AverageMonthSales(List<Product2> newList,Map<Id, Product2> oldMap){
 
        //符合条件的 旧 产品List
        List<Id> oproList = new List<Id>();
        //符合条件的 新旧 产品Id List  作为条件
        List<Id> conList = new List<Id>();
        //Map<旧的,新的>对应
        Map<String,String> matMap = new Map<String,String>();
        
        //Map<旧Id,预计停止日>
        Map<String,Date> adateMap = new Map<String,Date>();
 
        //Map<旧Id, x(平均月销量)>
        Map<String,Integer> xMap = new Map<String,Integer>();
        //Map<旧Id, z(天数)>
        Map<String,Integer> zMap = new Map<String,Integer>();
 
        for(Product2 term : newList){
            System.debug('标识'+term.CalMonthSalesFLG__c);
            System.debug('新库存'+term.StorageStatusNo__c);
            System.debug('旧库存'+oldMap.get(term.Id).StorageStatusNo__c);
            if(term.M_BC_Stop_manufacure__c != null && term.SFDA_Status__c != '停止' && (term.StorageStatusNo__c != oldMap.get(term.Id).StorageStatusNo__c || term.CalMonthSalesFLG__c == true)){
                if(term.Correspond_Code__c != null){
                    matMap.put((String)term.Id,(String)term.Correspond_Code__c);
                    conList.add(term.Correspond_Code__c);
                }
                conList.add(term.Id);
                oproList.add(term.Id);
 
                adateMap.put(term.Id, term.M_BC_Stop_manufacure__c);
            }
        }
 
        //做 一对多 的 Map<String,List<月销量>>
        Map<String,List<ProductMonthlySales__c>> msMap = new Map<String,List<ProductMonthlySales__c>>();
 
        if(conList.size()>0){
            List<ProductMonthlySales__c> msList = [Select Id, MonthSales__c, CurrentMonth__c, CurrentDate__c, 
                                                          CurrentYear__c, ProductM__c, Product2Model__c, 
                                                          ProductM__r.StorageStatusNo__c,
                                                          ProductM__r.M_BC_Stop_manufacure__c
                                                   From ProductMonthlySales__c
                                                   Where ProductM__c in: conList
                                                   Order by CurrentDate__c DESC];
            //双层循环  产品==>月销量
            for(Id pro : conList){
                for(ProductMonthlySales__c mms : msList){
                    if(pro == mms.ProductM__c){
                        if(msMap.containsKey(pro)){
                            msMap.get(pro).add(mms);
                        }else{
                            List<ProductMonthlySales__c> pmsaleList = new List<ProductMonthlySales__c>();
                            pmsaleList.add(mms);
                            msMap.put(pro, pmsaleList);
                        }
                    }
 
                }
            }
 
            System.debug('条件List'+conList);
            System.debug('sql查到的List'+msList);
            System.debug('产品月销量msMap为'+msMap);
            System.debug('旧的产品List'+oproList);
 
            if(msMap.size() > 0){
                System.debug('产品月销量msMap为'+msMap);
                for(Id res : oproList){
 
                    //a旧 月销量和  ; b新 月销量和 ; c商(平均月销量) ; x商四舍五入 ;
                    //y 库存/平均月销量天数 ; z 天数 四舍五入
                    Decimal c = 0, y = 0;
                    Integer x = 0, z = 0;
 
                    if(msMap.get(res) != null){
                        //月销量大于12个月
                        if(msMap.get(res).size() >= 12){
                            Decimal a = 0 , b = 0;
                            Integer i;
                            for(i = 0 ; i < 12 ; i++){
                                a += msMap.get(res)[i].MonthSales__c;
                            }
                            if(matMap.get(res) != null){
                                if(msMap.get(matMap.get(res)) != null){
                                    if(msMap.get(matMap.get(res)).size() >= 12){
                                        for(i = 0 ; i < 12 ; i++){
                                            b += msMap.get(matMap.get(res))[i].MonthSales__c;
                                            System.debug('新的大于十二'+b);
                                        }
                                    }
                                    else{
                                        for(ProductMonthlySales__c nms : msMap.get(matMap.get(res))){
                                            b+=nms.MonthSales__c;
                                            System.debug('新的小于十二遍历全部'+b);
                                        }
        
                                    }
                                }
                            }
                            
                            //四舍五入  
                            c = (a + b) / 12 ;
    
                            if(c > 1){
                                x = Integer.valueOf(c.round(System.RoundingMode.HALF_UP));
                                xMap.put(res,x);
                            }else if(c > 0){
                                x = 1;
                                xMap.put(res,x);
                            }
                            //x = Integer.valueOf(c.round(System.RoundingMode.Up));
                            //x = Integer.valueOf(c.round(System.RoundingMode.HALF_UP));
    
                            System.debug('新'+b);
                            System.debug('旧'+a);
                            System.debug('除法足c'+c);
                            System.debug('四舍五入x'+x);
    
                        }
                        //月销量不足12个月
                        else{
                            Decimal a = 0 , b = 0 ;
                            for(ProductMonthlySales__c oms : msMap.get(res)){
                                a+=oms.MonthSales__c;
                            }
                            if(msMap.get(matMap.get(res)) != null){
                                for(ProductMonthlySales__c nms : msMap.get(matMap.get(res))){
                                    b+=nms.MonthSales__c;
                                }
                            }
                            c = (a + b) / msMap.get(res).size();
                            //x = Integer.valueOf(c.round(System.RoundingMode.UP));
                            //x = Integer.valueOf(c.round(System.RoundingMode.HALF_UP));
                            if(c > 1){
                                x = Integer.valueOf(c.round(System.RoundingMode.HALF_UP));
                                xMap.put(res,x);
                            }else if(c > 0){
                                x = 1;
                                xMap.put(res,x);
                            }
    
                            System.debug('除法不足12c'+c);
                            System.debug('四舍五入x'+x);
                        }
 
                        if(x != 0){
                            for(ProductMonthlySales__c dat : msMap.get(res)){
                                Decimal m = 0;
                                if(dat.ProductM__r.StorageStatusNo__c != null){
                                    m = dat.ProductM__r.StorageStatusNo__c;
                                } 
                                y = m / x;
                                y = y * 30 ;
                            }
                        }
                        //z = Integer.valueOf(y.round(System.RoundingMode.UP));
                        z = Integer.valueOf(y.round(System.RoundingMode.HALF_UP));
                        zMap.put(res,z);
                        System.debug('天数y'+y);
                        System.debug('四舍五入z'+z);
 
                    }
                }
            }
 
            List<Product2> finList = new List<Product2>();
            for(Id fin : oproList){
                Product2 fip = new Product2();
                fip.Id = fin;
                fip.CalMonthSalesFLG__c = false;
 
                if(xMap.get(fin) != null){
                    fip.Average_MonthSales__c = xMap.get(fin);
                }
 
                //20211020
                if(zMap.get(fin) != null){
                    if(zMap.get(fin) != 0){
                        fip.Estimated_ConsumptionDueDate__c = Date.today().addDays(zMap.get(fin));
                    }else{
                        if(adateMap != null){
                            Date adate = adateMap.get(fin);
                            fip.Estimated_ConsumptionDueDate__c = adate;
                        }
                    }
                }else{
                    if(adateMap != null){
                        Date adate = adateMap.get(fin);
                        fip.Estimated_ConsumptionDueDate__c = adate;
                    }
                }
                //20211020
                
                finList.add(fip);
            }
            update finList;
        } 
 
    }
    //SFDC停止预警  lt  20210914 add end
 
    //SFDC停止预警  lt  20211015 add start
    //ProductMonthlySalesFlag__c  12月销量标识  预计停止日期(M_BC_Stop_manufacure__c)变化时
    // private static void FlgStopManufacure(List<Product2> newList,Map<Id, Product2> oldMap){
    //     if(newList != null){
    //         for(Product2 pts : newList){
    //             if(oldMap != null){
    //                 if(pts.M_BC_Stop_manufacure__c != null && oldMap.get(pts.Id).M_BC_Stop_manufacure__c == null){
    //                     pts.ProductMonthlySalesFlag__c = true;
    //                 }
    //                 if(pts.M_BC_Stop_manufacure__c != null && oldMap.get(pts.Id).M_BC_Stop_manufacure__c != null && pts.M_BC_Stop_manufacure__c != oldMap.get(pts.Id).M_BC_Stop_manufacure__c){
    //                     pts.ProductMonthlySalesFlag__c = true;
    //                 }
    //                 if(pts.M_BC_Stop_manufacure__c == null && oldMap.get(pts.Id).M_BC_Stop_manufacure__c != null){
    //                     pts.ProductMonthlySalesFlag__c = false;
    //                     pts.Estimated_ConsumptionDueDate__c = null;
    //                 }
    //             }
    //         }
    //     }
        
    // }
    //SFDC停止预警  lt  20211015 add end
    private  void FlgStopManufacure(){
        for(Product2 pts : newList){
 
            Product2 oldPts = oldMap.get(pts.Id);
 
            if(pts.M_BC_Stop_manufacure__c  != oldPts.M_BC_Stop_manufacure__c){
                pts.ProductMonthlySalesFlag__c = true;
                if (pts.M_BC_Stop_manufacure__c == null) {
                    pts.ProductMonthlySalesFlag__c = false;
                    pts.Estimated_ConsumptionDueDate__c = null;    
                }
            }
 
            if(pts.SFDA_Status__c == '停止' && oldPts.SFDA_Status__c != '停止'){
                pts.Estimated_ConsumptionDueDate__c = null;
            }
        }
    }
 
}