高章伟
2022-02-24 2aa8da8af66aa8ae00f25831aed6bb0364176e7b
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
public without sharing class CampaignCopyToOPDPlanHandler  extends Oly_TriggerHandler {
 
    private Map<Id, Campaign> newMap;
    private Map<Id, Campaign> oldMap;
    private List<Campaign> newList;
    private List<Campaign> oldList;
    public static boolean HandlerFlag = false;
    public CampaignCopyToOPDPlanHandler() {
        this.newMap = (Map<Id, Campaign>) Trigger.newMap;
        this.oldMap = (Map<Id, Campaign>) Trigger.oldMap;
        this.newList = (List<Campaign>) Trigger.new;
        this.oldList = (List<Campaign>) Trigger.old;
    }
    protected override void afterUpdate() {
        //将符合条件的学会拷贝成OPDPlan
        CampaignCopyToOPDPlan();
        //学会发生改变,对应的OPDPlan也会发生相应的改变
        listen();
        //add by rentx LZHU-BX3DU8 2021-01-13 start
        //在学会上学会的状态变成“已提交报告”的时候,查一下关联的opd计划,把状态变成完成
        changeOPDPlanStatusBeOver();
        //add by rentx LZHU-BX3DU8 2021-01-13 end
 
        // 2022-01-26 ssm 决裁信息变化时更新OPD计划 start
        updateApprovedToOPDPlan();
        // 2022-01-26 ssm 决裁信息变化时更新OPD计划 end
    }
 
    private void CampaignCopyToOPDPlan() {
        //记录类型 : 1.学会/会议 --> Society  2.社外培训 --> Training_event  3.服务培训/学会会议 -->Service_trainig  6.营业部专用会议 -->BusinessTraining
        List<Id> recordTypeIds = new List<Id>();
        recordTypeIds.add(Schema.SObjectType.Campaign.getRecordTypeInfosByDeveloperName().get('Society').getRecordTypeId()); //学会/会议
        recordTypeIds.add(Schema.SObjectType.Campaign.getRecordTypeInfosByDeveloperName().get('Training_event').getRecordTypeId()); //社外培训
        recordTypeIds.add(Schema.SObjectType.Campaign.getRecordTypeInfosByDeveloperName().get('Service_trainig').getRecordTypeId()); //服务培训/学会会议
        recordTypeIds.add(Schema.SObjectType.Campaign.getRecordTypeInfosByDeveloperName().get('BusinessTraining').getRecordTypeId()); //营业部专用会议
        //用来保存将学会转换成的OPDPlan
        List<OPDPlan__c> ResultList = new List<OPDPlan__c> ();
        //System.debug('CamMzyNewList :'+ newList.size());
        List<String> idList = new List<String>();
        for ( Campaign  c : newList) {
            OPDPlan__c  tempOPD = new OPDPlan__c();
            //RecordTypeId 摘要中
            //System.debug('CamMzyStauts :'+ c.Status);
            //System.debug('CamMzyStauts :'+ c.RecordTypeId);
            //判断是由其他状态变成 公开中 ,且需要借出备品 时插入数据
            Campaign oldCampaign = oldMap.get(c.Id);
            if (!String.isEmpty(String.valueOf(oldCampaign.Status)) && !oldCampaign.Status.equals('公开中')) {
                if ( c.Status != null && c.Status.equals('公开中') && recordTypeIds.contains(c.RecordTypeId)
                        && String.isNotBlank(c.Is_LendProduct__c)
                        && String.valueOf(c.Is_LendProduct__c).equals('是')) {
                    //  System.debug('zzz:' + c);
                    //转换 (对应字段的管理)
                    // 学会的开始日期 --> OPD计划实施日期temp
                    tempOPD.OPDPlan_ImplementDate_temp__c  =  c.StartDate ;
                    // OPD 查找 市场活动 的关系字段
                    tempOPD.Campaign__c = c.Id ;
                    // 学会生成OPD计划时, OPD计划状态为 计划中
                    tempOPD.Status__c = '计划中';
                    // 学会名称 --> OPD计划科室
                    //tempOPD.Account_Laboratory__c = c.Name2__c ;
                    // 学会所有人 --> OPD计划所有人
                    tempOPD.OwnerId = c.OwnerId;
                    // 学会会议的出借备品信息-->OPD计划出借备品信息
                    tempOPD.PlanProdDetail__c = c.LoadNum__c;
                    tempOPD.OPDType__c = '学会';//2020-10-21 zh OPD计划类型为学会 SWAG-BS6BYA
                    //20201118 you  SWAG-BVGAGD start
                    tempOPD.Meeting_Cooperate__c = c.Meeting_Cooperate__c;
                    tempOPD.Meeting_Cooperate2__c = c.Meeting_Cooperate2__c;
                    tempOPD.Meeting_Cooperate3__c = c.Meeting_Cooperate3__c;
                    tempOPD.Meeting_Cooperate4__c = c.Meeting_Cooperate4__c;
                    //20201118 you SWAG-BVGAGD end
                    //20201208 liying  SFDC-BVY95C start
                    tempOPD.Open_scope__c = c.Open_scope__c;
                    //20201208 liying SFDC-BVY95C end
                    //20211223 OBPM LY start 
                    tempOPD.IF_Approved__c = c.IF_Approved__c;//是否申请决裁
                    tempOPD.Approved_Status__c = c.Approved_Status__c;//决裁状态
                    tempOPD.Approved_No__c = c.Meeting_Approved_No__c;//决裁编码
                    //20211223 OBPM LY end
 
                    idList.add(c.Id);
                    //写入集合中
                    ResultList.add(tempOPD);
                }
            }
            //System.debug('mmmmmmm:' + ResultList);
        }
        //20201022 zh 小表同步opd SWAG-BS6BYA
        Map<Id, Id> camToOPD = new Map<Id, Id>();
        if (idList.size() > 0) {
            //查询指定OPDPlan mzy
            List<OPDPlan__c> OPDPlanList =  [select Campaign__c from OPDPlan__c where Campaign__c in :idList];
            if (OPDPlanList.size() <= 0) {
                //添加
                insert ResultList;
                for (OPDPlan__c opd : ResultList ) {
                    camToOPD.put(opd.Campaign__c, opd.Id);
                }
 
                List<Plan_Rental_Equipment__c> updateProds = new List<Plan_Rental_Equipment__c>();
                List<Plan_Rental_Equipment__c> planProds = [SELECT Id, Campaign__c, OPD_Plan__c FROM Plan_Rental_Equipment__c WHERE Campaign__c in :idList];
                if (planProds.size() > 0) {
                    for (Plan_Rental_Equipment__c plan :  planProds) {
                        plan.OPD_Plan__c = camToOPD.get(plan.Campaign__c);
                        updateProds.add(plan);
 
                    }
                }
                if (updateProds.size() > 0) {
                    update updateProds;
                }
            }
 
        }
    }
 
    // 2020-11-10   mzy  update  学会取消或者延期, 不直接更改opd计划的状态和时间,而是新建一个取消延期对象  start
    // 构建需要取消的OPD计划
    @TestVisible
    private CancelPostponePlan__c QX_OPDPlanBuild(ID OpdID) {
        CancelPostponePlan__c tempCpp = new CancelPostponePlan__c();
        tempCpp.CancelOPDPlan__c = OpdID;
        tempCpp.Status__c = '取消成功';
        tempCpp.cancelReasonCombobox__c = '学会取消';
        tempCpp.RecordTypeId = '01210000000gQyL';
        return tempCpp;
    }
    // 构建需要延期的OPD计划
    @TestVisible
    private CancelPostponePlan__c YQ_OPDPlanBuild(OPDPlan__c Opd, Date startDate) {
        CancelPostponePlan__c tempCpp = new CancelPostponePlan__c();
        tempCpp.CancelOPDPlan__c = Opd.id;
        tempCpp.Status__c = '延期成功';
        tempCpp.cancelReasonCombobox__c = '学会延期';
        tempCpp.NextPlanDate__c = startDate;
        tempCpp.RecordTypeId = '01210000000gQyM';
        // 2021-01-21 mzy add 会议的时间大于OPD计划的时间,则为延期 , 会议的时间小于OPD计划的时间,则为提前
        if(Opd.OPDPlan_ImplementDate__c < StartDate){
         tempCpp.ApplyType__c = '延期';
        }else if(Opd.OPDPlan_ImplementDate__c > StartDate){
         tempCpp.ApplyType__c = '提前';
        }
        // 2021-01-21 mzy add 会议的时间大于OPD计划的时间,则为延期 , 会议的时间小于OPD计划的时间,则为提前
        return tempCpp;
    }
    // 2020-11-10   mzy  update  学会取消或者延期, 不直接更改opd计划的状态和时间,而是新建一个取消延期对象  end
 
    private void listen() {
        // 2020-11-10   mzy  update  学会取消或者延期, 不直接更改opd计划的状态和时间,而是新建一个取消延期对象  start
        //如果学会的状态发生改变时,OPDplan的状态也将发生改变
         // 进入了一次不进入第二次
        if(CampaignCopyToOPDPlanHandler.HandlerFlag){
            return;
        }
        CampaignCopyToOPDPlanHandler.HandlerFlag  =  true;
        map<ID, Campaign> QXMap = new Map<ID, Campaign>(); //保存取消的学会
        Map<ID, Campaign> YQMap = new Map<ID, Campaign>(); //保存延期的学会
        Map<ID, Campaign> hyhzMap = new Map<ID, Campaign>();//SWAG-BVGAGD 20201119 you  会议合作本部1,2,3,4带值
        for (Campaign c : newList) {
            //目的 :学会变化 : 状态变为取消 / 开始日期发生改变
            Campaign oldCampaign = oldMap.get(c.Id);
            if (!String.isEmpty(String.valueOf(oldCampaign.Status))
                    && !oldCampaign.Status.equals('取消')) {
                // 状态从非取消变为取消
                if ( (c.Status != null && c.Status.equals('取消')) ) {
                    // 取消的学会
                    QXMap.put(c.id, c);
                }
                // 在非取消时,开始日期发生变化为延期
                else if (c.StartDate != oldCampaign.StartDate) {
                    // 延期的学会
                    YQMap.put(c.id, c);
                }
                //SWAG-BVGAGD 20201119 you start
                if(c.Meeting_Cooperate__c != oldCampaign.Meeting_Cooperate__c 
                        || c.Meeting_Cooperate2__c != oldCampaign.Meeting_Cooperate2__c 
                        || c.Meeting_Cooperate3__c != oldCampaign.Meeting_Cooperate3__c  
                        || c.Meeting_Cooperate4__c != oldCampaign.Meeting_Cooperate4__c
                        || c.Open_scope__c  != oldCampaign.Open_scope__c){
                    hyhzMap.put(c.id, c);
                } 
                //SWAG-BVGAGD 20201119 you  end
            }
        }
        //用来保存新建的取消(延期)对象,包括取消和延期两种情况
        List<CancelPostponePlan__c> QX_List =  new List<CancelPostponePlan__c>();
        //System.debug('Test : '+idList);
        //查询取消学会的OPDPlan
        List<OPDPlan__c> QX_OPDPlan = new List<OPDPlan__c>();
        //查询延期学会的OPDPlan
        List<OPDPlan__c> YQ_OPDPlan = new List<OPDPlan__c>();
        //SWAG-BVGAGD 20201119 you
        List<OPDPlan__c> oPDPlans = new List<OPDPlan__c>();
        // 需要更新的OPD计划的ID集合
        Set<ID> targetIDSet = new Set<ID>();
        targetIDSet.addAll(QXMap.keySet());
        targetIDSet.addAll(YQMap.keySet());
        //SWAG-BVGAGD 20201119 you start
        targetIDSet.addAll(hyhzMap.keySet());
        //SWAG-BVGAGD 20201119 you end
 
        if (targetIDSet.size() > 0) {
            List<OPDPlan__c> targetOPDPlanList =
                [select id , Campaign__c ,OPDPlan_ImplementDate__c
                 from OPDPlan__c
                 where Campaign__c != null
                 and status__c != '取消'
                 and Campaign__c in: targetIDSet
                ];
 
            for (OPDPlan__c tempOPD : targetOPDPlanList) {
                // 如果学会取消,那么生成取消的 取消(延期)对象
                if (QXMap.containsKey(tempOPD.Campaign__c)) {
                    QX_List.add(QX_OPDPlanBuild(tempOPD.id));
                }
                // 如果学会延期,那么生成延期的 取消(延期)对象,并且记录下来新的学会开始日期
                else if (YQMap.containsKey(tempOPD.Campaign__c)) {
                    Date startDate = YQMap.get(tempOPD.Campaign__c)?.StartDate;
                    QX_List.add(YQ_OPDPlanBuild(tempOPD, startDate));
                }
                //SWAG-BVGAGD 20201119 you start
                // add by gzw 注释掉,单独操作 start
                // for (Campaign c : newList) {
                //   if(c.id==tempOPD.Campaign__c){
                //      tempOPD.Meeting_Cooperate__c = c.Meeting_Cooperate__c;
                //      tempOPD.Meeting_Cooperate2__c = c.Meeting_Cooperate2__c;
                //      tempOPD.Meeting_Cooperate3__c = c.Meeting_Cooperate3__c;
                //      tempOPD.Meeting_Cooperate4__c = c.Meeting_Cooperate4__c;
                //      tempOPD.Open_scope__c = c.Open_scope__c;
                //      oPDPlans.add(tempOPD);
                //   }
                // }
                // add by gzw 注释掉,单独操作 end
                //SWAG-BVGAGD 20201119 you end
               
            }
        }
 
        if (QX_List.size() > 0 && QX_List != null) {
            insert QX_List;
        }
        // add by gzw OPDPlan__c 单独循环,否则OPDPlan_ImplementDate__c 会还原,报错 start
        if (targetIDSet.size() > 0) {
            List<OPDPlan__c> targetOPDPlanList =
                [select id , Campaign__c
                 from OPDPlan__c
                 where Campaign__c != null
                 and status__c != '取消'
                 and Campaign__c in: targetIDSet
                ];
 
            for (OPDPlan__c tempOPD : targetOPDPlanList) {
                //SWAG-BVGAGD 20201119 you start
                for (Campaign c : newList) {
                  if(c.id==tempOPD.Campaign__c){
                     tempOPD.Meeting_Cooperate__c = c.Meeting_Cooperate__c;
                     tempOPD.Meeting_Cooperate2__c = c.Meeting_Cooperate2__c;
                     tempOPD.Meeting_Cooperate3__c = c.Meeting_Cooperate3__c;
                     tempOPD.Meeting_Cooperate4__c = c.Meeting_Cooperate4__c;
                     tempOPD.Open_scope__c = c.Open_scope__c;
                     oPDPlans.add(tempOPD);
                  }
                }
                //SWAG-BVGAGD 20201119 you end
               
            }
        }
        // add by gzw OPDPlan__c 单独循环,否则OPDPlan_ImplementDate__c 会还原,报错 end
        //SWAG-BVGAGD 20201119 you start
        if (oPDPlans.size() > 0) {
            // System.debug('oPDPlans++++++' + oPDPlans);
            update oPDPlans;
        }
        //SWAG-BVGAGD 20201119 you end
 
        // 2020-11-10   mzy  update  学会取消或者延期, 不直接更改opd计划的状态和时间,而是新建一个取消延期对象  end
 
 
        /*    2020-11-10   mzy  update  学会取消或者延期,OPD计划相应的发生改变
 
        //创建Map保存 学会Id 和 OPDPlan的Id
        Map<String,String> QXIdsMap = new Map<String,String>();
        for (OPDPlan__c tempOPD : QX_OPDPlan){
             QXIdsMap.put(tempOPD.Campaign__c,tempOPD.id);
        }
 
        //用来保存发生变化的OPDPlan
        List<OPDPlan__c> UpdateOPDPlan = new List<OPDPlan__c>();
        for (Campaign c : newList){
             OPDPlan__c  tempOPD = new OPDPlan__c();
            // System.debug('====111: ' + IdsMap.get(c.id));
            // 判断如果id为空则不进行更新操作
             if(IdsMap.get(c.id) != null && !String.isEmpty(IdsMap.get(c.id))){
                 tempOPD.id = IdsMap.get(c.id);
                 //模拟条件
                 //if(c.Status.equals('草案中')){
                 tempOPD.Status__c = '计划中' ;
                 }else
                 // 学会会议延期 更新OPD计划的实施日期
                 tempOPD.OPDPlan_ImplementDate__c  =  c.StartDate ;
                 //学会会议取消,OPD计划取消
                 if(c.Status.equals('取消')) {
                     tempOPD.Status__c = '取消';
                 }
                 UpdateOPDPlan.add(tempOPD);
                 //else {
                 //    tempOPD.Status__c = '完毕';
                // }
                // UpdateOPDPlan.add(tempOPD);
             }
 
        }
            //更新状态
           update UpdateOPDPlan ;*/
 
 
    }
    
    //add by rentx LZHU-BX3DU8 2021-01-13 start
    private void changeOPDPlanStatusBeOver(){
        //获取状态变为 '完毕未报告' 的学会的id
        List<Id> camIds = new List<Id>();
        for ( Campaign  c : newList) {
            if (c.Status != null && c.Status == '已提交报告' && oldMap.get(c.Id).Status != null && oldMap.get(c.Id).Status != '已提交报告') {
                camIds.add(c.Id);
            }
        }
        //获取学会下的OPD计划
        if (camIds.size() > 0) {
            List<OPDPlan__c> opdlist = [select id,status__c from OPDPlan__c WHERE OPDType__c = '学会' and status__c != '完毕' and Campaign__c in: camIds];
            //将OPD计划的状态更新为 完毕
            if (opdlist != null && opdlist.size() > 0) {
                for (OPDPlan__c opdc : opdlist) {
                    opdc.Status__c= '完毕';
                }
                update opdlist;
            }
        }
        
    }
    //add by rentx LZHU-BX3DU8 2021-01-13 end
 
    // 2022-01-26 ssm 决裁信息变化时更新OPD计划 start
    private void updateApprovedToOPDPlan() {
        List<String> cam_ids = new List<String>();
        for (Campaign cam : this.newList) {
            Campaign old_cam = this.oldMap.get(cam.Id);
            if (old_cam != null && (
                old_cam.IF_Approved__c != cam.IF_Approved__c || 
                old_cam.Approved_Status__c != cam.Approved_Status__c || 
                old_cam.Meeting_Approved_No__c != cam.Meeting_Approved_No__c
            )) {
                cam_ids.add(cam.Id);
            }
        }
        if (cam_ids.size() > 0) {
            List<OPDPlan__c> plans = [select Id, Campaign__c, IF_Approved__c, Approved_Status__c, Approved_No__c from OPDPlan__c where Campaign__c in :cam_ids];
            if (plans != null && plans.size() > 0) {
                for (OPDPlan__c plan : plans) {
                    Campaign cam = this.newMap.get(plan.Campaign__c);
                    plan.IF_Approved__c = cam.IF_Approved__c;
                    plan.Approved_Status__c = cam.Approved_Status__c;
                    plan.Approved_No__c = cam.Meeting_Approved_No__c;
                }
                update plans;
            }
        }
    }
    // 2022-01-26 ssm 决裁信息变化时更新OPD计划 end
}