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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
public without sharing class PreProductStorageHandeler extends Oly_TriggerHandler {
    private List<PreProduct_Storage__c> oldList;
    private List<PreProduct_Storage__c> newList;
    private Map<Id,PreProduct_Storage__c> oldMap;
    private Map<Id,PreProduct_Storage__c> newMap;
    // 创建省份和对应大区的映射关系
    private Map<String, String> provinceToRegionMap = new Map<String, String>();
    //本部和本部窗口邮件地址的映射关系
    private Map<String,String> DepartmentToDeparWindowMap = new Map<String,String>{
        '1.华北' => Label.DepartmentWindow1,
        '2.东北' => Label.DepartmentWindow2,
        '3.西北' => Label.DepartmentWindow3,
        '4.西南' => Label.DepartmentWindow4,
        '5.华东' => Label.DepartmentWindow5,
        '6.华南' => Label.DepartmentWindow6,
        '市场' => Label.DepartmentWindowMarket,
        '其他' => Label.DepartmentWindowElse };
    //当前时间
    private DateTime currentTime = System.now();
    private String recordEntailCompoId ;
    private String recordConsumablesId ;
 
    public PreProductStorageHandeler() {
        this.newList = (List<PreProduct_Storage__c>) Trigger.new;
        this.oldMap = (Map<Id,PreProduct_Storage__c>) Trigger.oldMap;
        this.newMap = (Map<Id,PreProduct_Storage__c>) Trigger.newMap;
        this.oldList = (List<PreProduct_Storage__c>) Trigger.old;
        provinceToRegionMap.put('北京','1.华北');
        provinceToRegionMap.put('天津','1.华北');
        provinceToRegionMap.put('河南','3.西北');
        provinceToRegionMap.put('河北','1.华北');
        provinceToRegionMap.put('山西','3.西北');
        provinceToRegionMap.put('内蒙古','1.华北');
        provinceToRegionMap.put('山东','1.华北');
        provinceToRegionMap.put('陕西','3.西北');
        provinceToRegionMap.put('青海','3.西北');
        provinceToRegionMap.put('宁夏','3.西北');
        provinceToRegionMap.put('甘肃','3.西北');
        provinceToRegionMap.put('新疆','3.西北');
        provinceToRegionMap.put('辽宁','2.东北');
        provinceToRegionMap.put('黑龙江','2.东北');
        provinceToRegionMap.put('沈阳','2.东北');
        provinceToRegionMap.put('吉林','2.东北');
        provinceToRegionMap.put('上海','5.华东');
        provinceToRegionMap.put('江苏','5.华东');
        provinceToRegionMap.put('浙江','5.华东');
        provinceToRegionMap.put('福建','5.华东');
        provinceToRegionMap.put('安徽','5.华东');
        provinceToRegionMap.put('江西','5.华东');
        provinceToRegionMap.put('广东','6.华南');
        provinceToRegionMap.put('广西','6.华南');
        provinceToRegionMap.put('重庆','4.西南');
        provinceToRegionMap.put('四川/西藏','4.西南');
        provinceToRegionMap.put('云南','4.西南');
        provinceToRegionMap.put('贵州','4.西南');
        provinceToRegionMap.put('湖北','6.华南');
        provinceToRegionMap.put('湖南','6.华南');
        provinceToRegionMap.put('深圳','6.华南');
        provinceToRegionMap.put('海南','6.华南');
        provinceToRegionMap.put('大连','2.东北');
        provinceToRegionMap.put('青岛','1.华北');
        recordEntailCompoId = [SELECT Id FROM RecordType where name ='整机' and DeveloperName ='EntailCompo'].Id;
        recordConsumablesId = [SELECT Id FROM RecordType where name ='耗材' and DeveloperName ='Consumables'].Id;
    }
 
    protected override void afterUpdate() {   
        //更改的产品预留,如果状态发生了变化,从无效变为有效时,产品主数据 停产预留 勾上
        changeIfValidCallProduct2();
        //更改的产品预留 如果状态是有效的,产品发生了变化,新产品主数据的停产预留 勾上
        changeProduct2IdCallPro2();
        //变更省份 还需要重新给分配权限,首先得删掉之前的权限,在新建
        //暂时没做
    }
    protected override void beforeUpdate() {   
        setCloum();
        //更新时判断所属本部或者所属省份是否已有相同产品的产品预留
        // checkPreProductSame();
        storageTheOneSet();
    }
 
    protected override void afterInsert() {   
        //新增有效 产品预留,将产品主数据的停产预留字段 勾上
        actviePreProductCallProduct2();
        //本部或省份变更 ,同步空更新包含此产品预留的产品预留调整
        //暂时没做()
        //给省份SPGI助理读产品预留的权限
        giveProvinceAssistPower();
        
    }
 
    protected override void beforeInsert() {   
        //剩余预留总量 触发器 在新建的时候用初始预留总量赋值
        
        //产品类型为整机,省份从 所属本部选择 自动赋值
        setCloum();
        // checkPreProductSame();
        storageTheOneSet();
    }
 
    public void giveProvinceAssistPower(){
        List<PreProduct_Storage__Share> shareList = new LIst<PreProduct_Storage__Share>();
        for(PreProduct_Storage__c storage : newList){
            PreProduct_Storage__Share  storageShare = new PreProduct_Storage__Share();
            storageShare.ParentId = storage.Id;
            storageShare.AccessLevel = 'Read';
            if(storage.ProvinceAssistance__c!=null){
                storageShare.UserOrGroupId = storage.ProvinceAssistance__c;
                shareList.add(storageShare);
            }
            if(storage.ProvinceAssistance__c != storage.ProvinceSpAssistance__c && storage.ProvinceSpAssistance__c!=null){
                PreProduct_Storage__Share  storageShare2 = new PreProduct_Storage__Share();
                storageShare2.UserOrGroupId = storage.ProvinceSpAssistance__c;
                storageShare2.ParentId = storage.Id;
                storageShare2.AccessLevel = 'Read';
                shareList.add(storageShare2);
            }
        }
        if(shareList.size() >0){
            INSERT shareList;
        }
    }
 
    public void actviePreProductCallProduct2() {
        //有效的 产品预留对象 对应的产品主数据Id set
        Set<String> actvieProduct2IdSet = new Set<String>();
        for( PreProduct_Storage__c  storage :  newList){
            if(storage.ifValid__c){
                actvieProduct2IdSet.add(storage.Field1__c);
            }
        }
        //产品主数据
        List<Product2>  acitveProduct2 = [SELECT Id,LastbuyProductFLG__c FROM Product2 WHERE Id In :actvieProduct2IdSet];
        if(acitveProduct2.size() > 0){
            for(Product2 pro2 : acitveProduct2){
                pro2.LastbuyProductFLG__c=True;
            }   
            UPDATE  acitveProduct2;
        }
    }
 
    public void changeIfValidCallProduct2(){
        //存放无效变有效的产品预留 对应的产品主数据的Id set
        Set<String> pro2IdSet = new Set<String>();
         for (PreProduct_Storage__c storage : newList) {
            PreProduct_Storage__c oldStorage = oldMap.get(storage.Id);
            if (storage.ifValid__c != oldStorage.ifValid__c && storage.ifValid__c ) {
                // 字段值发生变更的逻辑
                pro2IdSet.add(storage.Field1__c);
                System.debug('字段 FieldName__c 的值已经发生变更');
            }
        } 
        //产品主数据
        List<Product2>  acitveProduct2 = [SELECT Id,LastbuyProductFLG__c FROM Product2 WHERE Id In :pro2IdSet];
        if(acitveProduct2.size() > 0){
            for(Product2 pro2 : acitveProduct2){
                pro2.LastbuyProductFLG__c=True;
            }   
            UPDATE  acitveProduct2;
        }   
    }
    public void changeProduct2IdCallPro2(){
        //存放更改产品id产品预留 对应的产品主数据的Id set
        Set<String> pro2IdSet = new Set<String>();
         for (PreProduct_Storage__c storage : newList) {
            PreProduct_Storage__c oldStorage = oldMap.get(storage.Id);
            if(storage.ifValid__c){
                if (storage.Field1__c != oldStorage.Field1__c ) {
                    // 字段值发生变更的逻辑
                    pro2IdSet.add(storage.Field1__c);
                    System.debug('字段 FieldName__c 的值已经发生变更');
                }    
            }
            
        } 
        //产品主数据
        List<Product2>  acitveProduct2 = [SELECT Id,LastbuyProductFLG__c FROM Product2 WHERE Id In :pro2IdSet];
        if(acitveProduct2.size() > 0){
            for(Product2 pro2 : acitveProduct2){
                pro2.LastbuyProductFLG__c=True;
            }   
            UPDATE  acitveProduct2;
        }     
    }
    public void setCloum(){
        List<String> storageList = new List<String>();
        Map<String,String> storageMap = new Map<String,String>();
        Map<String,OCM_Management_Province__c> manageToPrivinceMap = new Map<String,OCM_Management_Province__c>();
        List<String> provinceList = new List<String>();
        for( PreProduct_Storage__c storage : newList){
            storageList.add(storage.Field1__c);
            provinceList.add(storage.Province__c);
        }
        List<Product2> product2List = [SELECT Id,Asset_Model_No__c FROM Product2 WHERE Id IN:storageList];
        for(Product2 pro : product2List){
            storageMap.put(pro.Id,pro.Asset_Model_No__c);
        } 
        List<OCM_Management_Province__c> manageMentProvinceList = [SELECT Id,Name,GI_assistant__c,SP_assistant__c FROM OCM_Management_Province__c WHERE Name IN:provinceList];
        for(OCM_Management_Province__c manage : manageMentProvinceList){
            manageToPrivinceMap.put(manage.Name,manage);
        }
        for(PreProduct_Storage__c storage   :newList){
            // 产品类型为耗材,本部从 所属省份选择 自动赋值
            if(storage.RecordTypeId == recordConsumablesId ){
                //因为本部有可能有值,且正确性未知
                String Department =  provinceToRegionMap.get(storage.Province__c);
                if(Department != storage.Department__c && (storage.Province__c !=null && storage.Province__c !='')){
                    storage.Department__c = Department;
                }
            }
            String sDepar = storage.Department__c;
            String province = storage.Province__c;
            if(sDepar == null){
                sDepar = '';
            }
            if(province == null){
                province = '';
            }
            //区分insert和update  
            if(oldMap!=null){
                if(storageMap.get(storage.Field1__c) !=null && (sDepar != oldMap.get(storage.Id).Department__c || province != oldMap.get(storage.Id).Province__c) ){
                    //区分记录类型  整机和耗材
                    if(storage.RecordTypeId == recordEntailCompoId){
                        //给name赋值 
                        storage.Name = storageMap.get(storage.Field1__c) +'&&' + sDepar;
                        //本部映射本部窗口
                        storage.DepartmentWindow__c =  DepartmentToDeparWindowMap.get(storage.Department__c);
                    }
                    else if (storage.RecordTypeId == recordConsumablesId){
                        storage.Name = storageMap.get(storage.Field1__c) + '&&' + sDepar + province ;
                        //本部映射本部窗口
                        storage.DepartmentWindow__c =  DepartmentToDeparWindowMap.get(storage.Department__c);
                        //省份不为空的情况下 给省份GI助理赋值对应办事处的GI助理 省份SP助理赋值对应办事处的GI助理
                        if(storage.Province__c !=null){
                            storage.ProvinceAssistance__c =  manageToPrivinceMap.get(storage.Province__c).GI_assistant__c;
                            storage.ProvinceAssistance__c =  manageToPrivinceMap.get(storage.Province__c).SP_assistant__c;
                        }
 
                    }
                }
            }
            else{
                if(storageMap.get(storage.Field1__c) !=null  ){
                    if(storage.RecordTypeId == recordEntailCompoId){
                        storage.Name = storageMap.get(storage.Field1__c) + '&&' + sDepar;
                        storage.DepartmentWindow__c =  DepartmentToDeparWindowMap.get(storage.Department__c);
                    }
                    else if (storage.RecordTypeId == recordConsumablesId){
                        storage.Name = storageMap.get(storage.Field1__c) + '&&' + sDepar + province ;
                        storage.DepartmentWindow__c =  DepartmentToDeparWindowMap.get(storage.Department__c);
                        if(storage.Province__c !=null){
                            storage.ProvinceAssistance__c =  manageToPrivinceMap.get(storage.Province__c).GI_assistant__c;
                            storage.ProvinceAssistance__c =  manageToPrivinceMap.get(storage.Province__c).SP_assistant__c;
                        }
                    }
                }    
            }
        }
    }
 
        //自己写的唯一check 先注释掉
    // public void checkPreProductSame(){
    //     List<String>   checkList1 = new List<String>();
    //     List<String>   checkList2 = new List<String>();
    //     List<PreProduct_Storage__c> checkSameStorage1 = new List<PreProduct_Storage__c>();
    //     List<PreProduct_Storage__c> checkSameStorage2 = new List<PreProduct_Storage__c>();
    //     //区分 insert update
    //     //1.先检查批量更新插入的对象的记录类型
    //     //2.整机判断本部  耗材判断省份    下是否有相同产品的产品预留
    //     // recordEntailCompoId = [SELECT Id FROM RecordType where name ='整机' and DeveloperName ='EntailCompo'].Id;
    //     // String recordConsumablesId = [SELECT Id FROM RecordType where name ='耗材' and DeveloperName ='Consumables'];
    //     if(oldMap!=null){
    //         //更新进入
    //         for( PreProduct_Storage__c storage : newList){
    //             if(storage.RecordTypeId == recordEntailCompoId){
    //                 PreProduct_Storage__c oldStorage = oldMap.get(storage.Id);
    //                 if(storage.Department__c != oldStorage.Department__c){
    //                     checkList1.add(storage.WhetherOne__c);
    //                 }
    //             }
    //             else if (storage.RecordTypeId == recordConsumablesId){
    //                 PreProduct_Storage__c oldStorage = oldMap.get(storage.Id);
    //                 if(storage.Province__c != oldStorage.Province__c || storage.Department__c != oldStorage.Department__c){
    //                     checkList2.add(storage.WhetherOne__c);
    //                 }
    //             }
    //         }
    //         if(checkList1.size() > 0){
    //             checkSameStorage1 = [SELECT id,WhetherOne__c FROM PreProduct_Storage__c WHERE ifValid__c = true AND WhetherOne__c IN :checkList1];
    //         }
    //         if(checkList2.size() > 0){
    //             checkSameStorage2 = [SELECT id,WhetherOne__c FROM PreProduct_Storage__c WHERE ifValid__c = true AND WhetherOne__c IN :checkList2];
    //         }    
    //         if(checkSameStorage1.size() > 0){
    //             throw new DmlException ('该产品已有相同本部的产品预留,请确认所属本部以及产品是否正确');
    //         }
    //         if(checkSameStorage2.size() > 0){
    //             throw new DmlException ('该产品已有相同省份的产品预留,请确认所属省份以及产品是否正确');
    //         }
 
    //     }
    //     else{
    //         //insert 进入
    //         for( PreProduct_Storage__c storage : newList){
    //             if(storage.RecordTypeId == recordEntailCompoId){
    //                 checkList1.add(storage.WhetherOne__c);
    //             }
    //             else if (storage.RecordTypeId == recordConsumablesId){
    //                 checkList2.add(storage.WhetherOne__c);
    //             }
    //         }
    //         if(checkList1.size() > 0){
    //             checkSameStorage1 = [SELECT id,WhetherOne__c FROM PreProduct_Storage__c WHERE ifValid__c = true AND WhetherOne__c IN :checkList1];
    //         }
    //         if(checkList2.size() > 0){
    //             checkSameStorage2 = [SELECT id,WhetherOne__c FROM PreProduct_Storage__c WHERE ifValid__c = true AND WhetherOne__c IN :checkList2];
    //         }    
    //         if(checkSameStorage1.size() > 0){
    //             throw new DmlException ('该产品已有相同本部的产品预留,请确认所属本部以及产品是否正确');
    //         }
    //         if(checkSameStorage2.size() > 0){
    //             throw new DmlException ('该产品已有相同省份的产品预留,请确认所属省份以及产品是否正确');
    //         }
    //     }
        
    // }
 
    public void storageTheOneSet(){
        for(PreProduct_Storage__c storage : newList){
            String province = storage.Province__c;
            String depar = storage.Department__c;
            //insert 时
            if(oldMap == null){
                if(province == null){
                    storage.StorageTheOne__c = storage.Field1__c + storage.Department__c ;
                }
                else{
                    storage.StorageTheOne__c = storage.Field1__c + storage.Department__c + storage.Province__c;
                }
            }
            //update 产品预留变为无效时  给唯一主键加上时间戳
            // 不改变无效时也要 变更唯一主键
            else{
                if(storage.RecordTypeId == recordConsumablesId){
                    if(storage.Province__c != oldMap.get(storage.Id).Province__c  ||  storage.Department__c != oldMap.get(storage.Id).Department__c ){
                        if(province == null){
                            storage.StorageTheOne__c = storage.Field1__c + storage.Department__c ;
                        }
                        else{
                            storage.StorageTheOne__c = storage.Field1__c + storage.Department__c + storage.Province__c;
                        }   
                    }
                }
                else if(storage.RecordTypeId == recordEntailCompoId){
                    if(depar != oldMap.get(storage.Id).Department__c ){
                        storage.StorageTheOne__c = storage.Field1__c + storage.Department__c + storage.Province__c;
                    }    
                }
                if(storage.ifValid__c != oldMap.get(storage.Id).ifValid__c && storage.ifValid__c ==false){
                    if(province == null){
                        storage.StorageTheOne__c = storage.Field1__c + storage.Department__c + DateTime.now();
                    }
                    else{
                        storage.StorageTheOne__c = storage.Field1__c + storage.Department__c + storage.Province__c + DateTime.now();
                    }        
                }
            }
            
        }
    }
 
}