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
public without sharing class BeforeUpsertReportHandler extends Oly_TriggerHandler {
    /**
    SWAG-BVW8CW 20201207 you create
    **/
    private Map<Id, Report__c> newMap;
    private Map<Id, Report__c> oldMap;
    private List<Report__c> newList;
    private List<Report__c> oldList;
    public BeforeUpsertReportHandler() {
        this.newMap = (Map<Id, Report__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Report__c>) Trigger.oldMap;
        this.newList = (List<Report__c>) Trigger.new;
        this.oldList = (List<Report__c>) Trigger.old;
    }
 
    protected override void beforeInsert() {
        InsertReport();
        InsertReportConsume();
    }
 
    protected override void beforeUpdate() {
        InsertReport();
        InsertReportConsume();
 
    }
    private void InsertReport() {
        Integer buffer = Integer.valueOf(System.Label.report_oldDate_update);//为了刷新prod上的老数据
        Set<Id> rentalIdSet = new Set<Id>();//备品借出申请id 做询价用
        Set<Id> opdplanIdSet = new Set<Id>();//OPD计划id
        Set<Id> rentalproIdSet = new Set<Id>();//备品借出申请id 做产品1,2,3,4,5赋值用
        for (Report__c re : newList) {
            /** 从opd报告书得按钮带值
            //有备品从备品带,无备品从opd计划上带 询价
            if (re.Loaner_request_no1__c != null && (Trigger.isInsert || (Trigger.isUpdate && (oldMap.get(re.id).Loaner_request_no1__c != re.Loaner_request_no1__c))) ) {
                rentalIdSet.add(re.Loaner_request_no1__c);
            } else if (re.Loaner_request_no1__c == null && (Trigger.isInsert || (Trigger.isUpdate && (oldMap.get(re.id).Report_OPDPlan__c != re.Report_OPDPlan__c))) && re.Report_OPDPlan__c != null) {
                opdplanIdSet.add(re.Report_OPDPlan__c);
            }
            **/
            if( re.Purpose1__c=='OPD' && re.Loaner_request_no1__c != null && (Trigger.isInsert || (Trigger.isUpdate && (oldMap.get(re.id).Loaner_request_no1__c != re.Loaner_request_no1__c || oldMap.get(re.id).Purpose1__c != re.Purpose1__c || buffer==1)))){
                rentalproIdSet.add(re.Loaner_request_no1__c);
            }
            //20210106 BWZ6M8 you start
             if(Trigger.isUpdate && oldMap.get(re.id).Status__c != re.Status__c && re.Status__c=='申請中' && oldMap.get(re.id).Status__c=='草案中' && re.Purpose1__c=='OPD'&& re.RecordTypeId == System.Label.OPD_SIS && re.Loaner_request_no1__c == null){//niwu- 01210000000RLTiAAO 改为 012C600000003KJIAY    
                re.Loaner_request_no1__c.addError('当目的1为OPD时:借出备品申请单号必须填写。');
            }
            //20210106 BWZ6M8 you end
        }
        /**
        //备品带询价
        if (rentalIdSet.size() > 0) {
            Map<string, string> rentalapplyMap = new Map<string, string>();
            List<Rental_Apply__c> rList = [select id, Follow_UP_Opp__c from Rental_Apply__c where id in:rentalIdSet and demo_purpose2__c = '试用(有询价)' and Follow_UP_Opp__c <> null];
            if (null != rList && rList.size() > 0) {
                for (Rental_Apply__c ra : rList) {
                    rentalapplyMap.put(ra.id, ra.Follow_UP_Opp__c);
                }
            }
            system.debug('==rentalapplyMap==' + rentalapplyMap);
            
            for (Report__c re : newList) {
                re.Opportunity1__c = null;
                re.Opportunity_Situation__c = '引合無';
                
                if (rentalapplyMap.containsKey(re.Loaner_request_no1__c)) {
                    re.Opportunity1__c = rentalapplyMap.get(re.Loaner_request_no1__c);
                    re.Opportunity_Situation__c = '跟进询价'; 
                }
             
            }
        }
        **/
        //借出备品配套一览  根据:备品配套明细型号 到产品:备品配套明细型号 比对,随机拿产品名字放过来
        //SWAG-C4U43T 20210713 you 改成 otcode去比对
        //1.重点产品主机/镜子最优先,2.主机/镜子(不要台车),3.其他产品
        System.debug('rentalproIdSet'+rentalproIdSet);
    
        if (rentalproIdSet.size() > 0) {
            Set<String> raesdmodelSet = new Set<String>();//备品配套明细型号
            Map<String, String>  raesdmodelMap = new Map<String, String>();
            List<Rental_Apply_Equipment_Set__c> raesList = [select id, OtCode__c, Rental_Apply__c from Rental_Apply_Equipment_Set__c where Rental_Apply__c in:rentalproIdSet and RAES_Status__c !='取消'];
            if (null != raesList && raesList.size() > 0) {
                for (Rental_Apply_Equipment_Set__c raes : raesList) {
                    raesdmodelSet.add(raes.OtCode__c);
                    String key =  raes.Rental_Apply__c + '|' + raes.OtCode__c;
                    raesdmodelMap.put(key, raes.OtCode__c);
                }
 
            }
            system.debug('==raesdmodelMap==' + raesdmodelMap);
            String Key_ProductLabel = System.Label.Key_ProductLabel;//重点产品
            List<String> zhlistAll = new List<String>();
            if (raesdmodelMap.size() > 0) {
                List<Product2> prodList = [select id, ProductCode,Key_product_147P__c,Category3__c from Product2 where ProductCode != '' And ProductCode in:raesdmodelSet and IsActive =true]; //产品
                if (null != prodList && prodList.size() > 0) {
                    List<String> zdidList = new List<String>();//重点产品
                    List<String> fzdidList = new List<String>();//非重点产品
                    List<String> qtidList = new List<String>();//非重点产品
                    for (Product2 pro : prodList) {
                        String mapkey = '';
                        if(String.isNotBlank(pro.Key_product_147P__c) && Key_ProductLabel.contains(pro.Key_product_147P__c)){
                            mapkey = pro.ProductCode +'_'+ pro.id;
                            zdidList.add(mapkey);  
                        } else if(String.isNotBlank(pro.Category3__c) && (pro.Category3__c.contains('主机') || pro.Category3__c.contains('镜'))){
                            mapkey = pro.ProductCode +'_'+ pro.id;
                            fzdidList.add(mapkey);
                        } else{
                            mapkey = pro.ProductCode +'_'+ pro.id;
                            qtidList.add(mapkey);
                        }
                        
                    }
                    if(null!=zdidList && zdidList.size()>0){
                       zhlistAll.addAll(zdidList);
                    }
                    if(null!=fzdidList && fzdidList.size()>0){
                       zhlistAll.addAll(fzdidList);
                    }
                    if(null!=qtidList && qtidList.size()>0){
                       zhlistAll.addAll(qtidList);
                    }
                }
            }
            system.debug('zheli'+zhlistAll);
             for (Report__c re : newList) {
                
                re.Product1__c = null;
                re.Product2__c = null;
                re.Product3__c = null;
                re.Product4__c = null;
                re.Product5__c = null;
                
                if (zhlistAll != null && zhlistAll.size() > 0) {
                    Integer num = 1;
                    for (String modelNo : zhlistAll) {
                        String[] pdcode = modelNo.split('_');
                        String key = re.Loaner_request_no1__c + '|'+ pdcode[0];
 
                        String stringid = pdcode[1];
                        system.debug('==外key==' + key);
                        if (raesdmodelMap.containsKey(key)) {
                            system.debug('==内key==' + key);
                            if (num == 1) {
                                re.Product1__c = stringid;
                            } else if (num == 2) {
                                re.Product2__c = stringid;
                            } else if (num == 3) {
                                re.Product3__c = stringid;
                            } else if (num == 4) {
                                re.Product4__c = stringid;
                            } else if (num == 5) {
                                re.Product5__c = stringid;
                            }
                            num += 1;
                        }
 
                    }
                }
            }
        }   
        /**
        //opd计划带询价
        if (opdplanIdSet.size() > 0) {
            Map<string, string> opdplanMap = new Map<string, string>();
            List<OPDPlan__c> opdplanist = [select id, Related_Opportunity1_ID__c from OPDPlan__c where id in:opdplanIdSet and Related_Opportunity1_ID__c <> null];
            if (opdplanist.size() > 0) {
                for (OPDPlan__c op : opdplanist) {
                    opdplanMap.put(op.id, op.Related_Opportunity1_ID__c);
                }
            }
            for (Report__c re : newList) {
                re.Opportunity1__c = null;
                re.Opportunity_Situation__c = '引合無';
                if (opdplanMap.containsKey(re.Report_OPDPlan__c)) {
                    re.Opportunity1__c = opdplanMap.get(re.Report_OPDPlan__c);
                    re.Opportunity_Situation__c = '跟进询价';
                }
 
            }
        }
        **/
    }
 
 
//2021/01/28 liying start 耗材申请借出
 
 private void InsertReportConsume() {
        Set<Id> consumeIdSet = new Set<Id>();//耗材借出申请id 做询价用
        Set<Id> consumeproIdSet = new Set<Id>();//耗材借出申请id 做产品1,2,3,4,5赋值用
        for (Report__c re : newList) {
           
            if( (re.Purpose1__c=='SIS-ET' || re.Purpose1__c=='SIS-ENG耗材' )&& re.Consum_Apply__c != null && (Trigger.isInsert || (Trigger.isUpdate && (oldMap.get(re.id).Consum_Apply__c != re.Consum_Apply__c || oldMap.get(re.id).Purpose1__c != re.Purpose1__c)))){
                consumeproIdSet.add(re.Consum_Apply__c);
            } 
        }
        //耗材备品配套一览  根据:备品配套明细型号 到产品:备品配套明细型号 比对,随机拿产品名字放过来
        if (consumeproIdSet.size() > 0) {
            Set<String> raesdmodelSet = new Set<String>();//备品配套明细型号
            Map<String, String>  raesdmodelMap = new Map<String, String>();
            List<Consum_Apply_Equipment_Set__c> raesList = [select id, Model_No__c, Consum_Apply__c from Consum_Apply_Equipment_Set__c where Consum_Apply__c in:consumeproIdSet];
            if (null != raesList && raesList.size() > 0) {
                for (Consum_Apply_Equipment_Set__c raes : raesList) {
                    raesdmodelSet.add(raes.Model_No__c);
                    String key =  raes.Consum_Apply__c + '|' + raes.Model_No__c;
                    raesdmodelMap.put(key, raes.Model_No__c);
                }
            }
            system.debug('==raesdmodelMap==' + raesdmodelMap);
            Map<String, String> prodIdMap = new Map<String, String>();
            if (raesdmodelMap.size() > 0) {
                
                List<Product2> prodList = [select id, Fixture_Model_No_F__c from Product2 where Fixture_Model_No_F__c != '' And Fixture_Model_No_F__c in:raesdmodelSet and IsActive =true]; //产品
                if (null != prodList && prodList.size() > 0) {
                    for (Product2 pro : prodList) {
                        prodIdMap.put(pro.Fixture_Model_No_F__c, pro.id );
                    }
                }
            }
             for (Report__c re : newList) {
                
                re.Product1__c = null;
                re.Product2__c = null;
                re.Product3__c = null;
                re.Product4__c = null;
                re.Product5__c = null;
                
                system.debug('==prodIdMap==' + prodIdMap);
                if (prodIdMap != null && prodIdMap.size() > 0) {
                        Integer num = 1;
                        for (String modelNo : prodIdMap.keySet()) {
 
                            String key = re.Consum_Apply__c + '|'+ modelNo;
                            String stringid = prodIdMap.get(modelNo);
                            system.debug('==外key==' + key);
                            if (raesdmodelMap.containsKey(key)) {
                                system.debug('==内key==' + key);
                                if (num == 1) {
                                    re.Product1__c = stringid;
                                } else if (num == 2) {
                                    re.Product2__c = stringid;
                                } else if (num == 3) {
                                    re.Product3__c = stringid;
                                } else if (num == 4) {
                                    re.Product4__c = stringid;
                                } else if (num == 5) {
                                    re.Product5__c = stringid;
                                }
                                num += 1;
                            }
 
                        }
                    }
                }
                }      
            }
//2021/01/28 liying end
}