游畅
2022-12-08 54e9fce17758136f2f5223038d30eeb32439c603
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/*
 * HWAG-BG682M accountDailyUpdateBatch 更新询价和客户人员主担当的内容拆出来新batch
 */
 
global class OpportunityAndContactDailyUpdateBatch implements Database.Batchable<sObject>, Database.Stateful {
 
    private String query;
    private String salesdepartment;
    private BatchIF_Log__c iflog;
 
    private Integer runLimit;
    private List<String> runTarget;
    private String AccountList;
 
    global OpportunityAndContactDailyUpdateBatch() {
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'PushNotification';
        iflog.Is_Error__c = 0;
        iflog.Log__c = 'OpportunityAndContactDailyUpdateBatch start1\n';
        iflog.ErrorLog__c = '';
        insert iflog;
     }
    global OpportunityAndContactDailyUpdateBatch(String dePartment) {
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'PushNotification';
        iflog.Is_Error__c = 0;
        iflog.Log__c = 'OpportunityAndContactDailyUpdateBatch start2\n';
        iflog.ErrorLog__c = '';
        insert iflog;
 
        salesdepartment = dePartment;
     }
     global OpportunityAndContactDailyUpdateBatch(Integer limitNum, String targetId1, String targetId2, String targetId3, String targetId4, String targetId5) {
        //执行五条数据
        iflog = new BatchIF_Log__c();
        iflog.Type__c = 'PushNotification';
        iflog.Is_Error__c = 0;
        iflog.Log__c = 'OpportunityAndContactDailyUpdateBatch start4\n';
        iflog.ErrorLog__c = '';
        insert iflog;
 
        runLimit = limitNum;
        runTarget = new List<String>();
        if (targetId1 != null && targetId1 != '') {
            runTarget.add(targetId1);
        }
        if (targetId2 != null && targetId2 != '') {
            runTarget.add(targetId2);
        }
        if (targetId3 != null && targetId3 != '') {
            runTarget.add(targetId3);
        }
        if (targetId4 != null && targetId4 != '') {
            runTarget.add(targetId4);
        }
        if (targetId5 != null && targetId5 != '') {
            runTarget.add(targetId5);
        }
        system.debug(runTarget);
    }
 
    global Database.QueryLocator start(Database.BatchableContext BC) {
        if (salesdepartment == null || salesdepartment == '') {
            query = ' select Id,FSE_Main__c,GI_Main__c,SP_Main__c,GI_Product_Leader__c,GS_Product_Leader__c,GYN_Product_Leader__c,BF_owner__c, ';
            query += 'ET_Product_Leader__c,BF_Product_Leader__c,ENT_Product_Leader__c,FSE_GI_Vice_Leader__c,FSE_SP_Vice_Leader__c,GYN_owner__c,';
            /* SWAG-B7LADC 2018/12/19 FSE ENG   start*/
            query += 'FSE_ENG_Vice_Leader__c, FSE_ENG_Main_Leader__c, ';
            /* SWAG-B7LADC 2018/12/19 FSE ENG   end*/
            query += 'ENT_owner_ID__c,Original_Date_Flag__c,ET_owner__c,URO_owner_ID__c ,URO_Produc_Leader__c,FSE_SP_Main_Leader__c, FSE_GI_Main_Leader__c, ParentId, OwnerId,Energy_LeaderStr__c,FSE_Extend_viceLeaderStr__c,Extend_Leader_Str__c,';
            query += 'FSE_Main__r.IsActive ,GI_Main__r.IsActive ,URO_owner_ID__r.IsActive ,ET_owner__r.IsActive ,';
            query += 'ENT_owner_ID__r.IsActive ,BF_owner__r.IsActive ,SP_Main__r.IsActive ,GYN_owner__r.IsActive ';
            query += ' from Account where Is_Active__c = \'有効\' and RecordType.DeveloperName = \'HP\' and Id=\'0011000000V9UKl\'';
            if (runTarget != null && runTarget.size() > 0) {
                query += ' and id = :runTarget ';
            }
            if (runLimit != null && runLimit > 0) {
                query += ' limit ' + runLimit;
            }
        } else {
            query = ' select Id,FSE_Main__c,GI_Main__c,SP_Main__c,GI_Product_Leader__c,GS_Product_Leader__c,GYN_Product_Leader__c,BF_owner__c, ';
            query += 'ET_Product_Leader__c,BF_Product_Leader__c,ENT_Product_Leader__c,FSE_GI_Vice_Leader__c,FSE_SP_Vice_Leader__c,GYN_owner__c,';
            query += 'ENT_owner_ID__c,Original_Date_Flag__c,ET_owner__c,URO_owner_ID__c ,URO_Produc_Leader__c,FSE_SP_Main_Leader__c, FSE_GI_Main_Leader__c, ParentId, OwnerId,Energy_LeaderStr__c,FSE_Extend_viceLeaderStr__c,Extend_Leader_Str__c,';
            query += 'FSE_Main__r.IsActive ,GI_Main__r.IsActive ,URO_owner_ID__r.IsActive ,ET_owner__r.IsActive ,';
            /* SWAG-B7LADC 2018/12/19 FSE ENG   start*/
            query += 'FSE_ENG_Vice_Leader__c, FSE_ENG_Main_Leader__c, ';
            /* SWAG-B7LADC 2018/12/19 FSE ENG   end*/
            query += 'ENT_owner_ID__r.IsActive ,BF_owner__r.IsActive ,SP_Main__r.IsActive ,GYN_owner__r.IsActive ';
            query += ' from Account where Is_Active__c = \'有効\' and RecordType.DeveloperName = \'HP\' and '+
            ' Salesdepartment_HP__c = \'' + salesdepartment + '\'';
        }
        system.debug('=====query:' + query);
        //Id=\'0011000000V9OGP\' and  测试用 给一条客户id
        return Database.getQueryLocator(query);
    
    }
    
    global void execute(Database.BatchableContext BC, List<SObject> hpList){
         List<Account> deptClassUpdateList = [select Id, RecordType.DeveloperName, Parent.Original_Date_Flag__c, ParentId, Parent.ownerId, Parent.owner.Alias__c, OwnerId from Account where Parent.Is_Active__c = '有効' and Parent.RecordType.DeveloperName = 'HP' and Hospital_Department_Class__c = :hpList order by Hospital_Department_Class__c, Account_Sort__c];
 
         List<Account> deptUpdateList = [select Id, RecordType.DeveloperName, Parent.Parent.Original_Date_Flag__c, Parent.owner.Alias__c, Parent.ParentId, Parent.Parent.ownerId, OwnerId from Account where Parent.Parent.Is_Active__c = '有効' and Parent.Parent.RecordType.DeveloperName = 'HP' and Hospital__c = :hpList order by Hospital__c, Account_Sort__c];
         
         AccountDailyUpdateBatch.updateFromHosToContact(deptUpdateList, iflog);
         updateFromHosToContact(deptUpdateList, iflog);
         //20220415 you SWAG-CBUB2W start 
         updateFromHosToInquiryForm(deptUpdateList, iflog);
         //20220415 you SWAG-CBUB2W end
 
         //20220509 you SWAG-CBUB2W start 
         updateFromHosToInquiryFormFSEMain(hpList, iflog);
         //20220509 you SWAG-CBUB2W end
    }
     //同步所属的询价(业务机会)的所有人
    public static void updateFromHosToContact(List<Account> deptUpdateList, BatchIF_Log__c iflog) {
         List<Opportunity> updateOpportunityList =
            [select id, Ownerid, Account.ownerId from Opportunity
        where Accountid in:deptUpdateList
        and isOwnerDiffWithAccount__c = true
            AND owner_not_automatically_update__c = FALSE ];
        //20220928 you SWAG-CJP7TL start
        system.debug('deptUpdateList==='+deptUpdateList);
        Map<Id,String> mapoppid = new Map<Id,String>();
        for (Opportunity opp : updateOpportunityList) {
            if (opp.Ownerid != opp.Account.ownerId) {
                opp.Ownerid = opp.Account.ownerId;
                mapoppid.put(opp.id,opp.Account.ownerId);
            }
        }
        List<Task__c> taskList = [
                SELECT Id,OpportunityId__c,taskStatus__c 
                FROM task__c 
                WHERE OpportunityId__c in :mapoppid.keySet()
                AND (RecordType.Name = '中标结果确认' OR RecordType.Name = '失单报告任务')
                AND taskStatus__c = '02 接受'];
        system.debug(mapoppid.keySet()+'test1-----'+taskList);
        for(Task__c t: taskList){
            if(mapoppid.containsKey(t.OpportunityId__c)){
               t.Ownerid = mapoppid.get(t.OpportunityId__c);
               t.assignee__c = mapoppid.get(t.OpportunityId__c);
            }
                       
        }    
 
        Integer indexCon = 0;
        Database.SaveResult[] lsrUpdateCon = Database.update(updateOpportunityList, false);
        Database.SaveResult[] lsrUpdatetask = Database.update(taskList, false);
        //20220928 you SWAG-CJP7TL end
        for (Database.SaveResult lsrChild : lsrUpdateCon) {
            if (!lsrChild.isSuccess()) {
                iflog.Is_Error__c = 3;
                Database.Error emsg = lsrChild.getErrors()[0];
                iflog.ErrorLog__c += 'ownerId: ' + updateOpportunityList.get(indexCon).ownerId + ' \n'
                                     + 'OpportunityID: ' + updateOpportunityList.get(indexCon).Id + '\n ConLog:' + emsg.getMessage() + '\n';
 
            }
            indexCon ++ ;
        }
 
    }
     //20220415 you SWAG-CBUB2W start 
     //同步所属的产品咨询单的所有人
    public static void updateFromHosToInquiryForm(List<Account> deptUpdateList, BatchIF_Log__c iflog) {
         List<Inquiry_form__c> updateInquiryFormList =
            [select id, Ownerid,Depart_Owner__c,Product_Owner__c, Hospital_Name__r.ownerId from Inquiry_form__c
        where Hospital_Name__c in:deptUpdateList
        and isOwnerDiffWithAccount__c = true ];
        for (Inquiry_form__c ifo : updateInquiryFormList) {
            if (ifo.Ownerid != ifo.Hospital_Name__r.ownerId) {
                ifo.Depart_Owner__c = ifo.Hospital_Name__r.ownerId;
                //20221128 you DB202211530484  当勾选 转产品担当时,所有人可以手动更改,不需要同步跟科室所有人一致
                if(ifo.Product_Owner__c == false){
                    ifo.Ownerid = ifo.Hospital_Name__r.ownerId;
                }
                
            }
        }
        Integer indexCon = 0;
        Database.SaveResult[] lsrUpdateInq = Database.update(updateInquiryFormList, false);
        for (Database.SaveResult lsrChild : lsrUpdateInq) {
            if (!lsrChild.isSuccess()) {
                iflog.Is_Error__c = 3;
                Database.Error emsg = lsrChild.getErrors()[0];
                iflog.ErrorLog__c += 'ownerId: ' + updateInquiryFormList.get(indexCon).ownerId + ' \n'
                                     + 'InquiryFormID: ' + updateInquiryFormList.get(indexCon).Id + '\n ConLog:' + emsg.getMessage() + '\n';
 
            }
            indexCon ++ ;
        }
 
    }
 
    //20220415 you SWAG-CBUB2W end 
    //20220509 you SWAG-CBUB2W start 
    //FSE主担当变化
     public static void updateFromHosToInquiryFormFSEMain(List<SObject> hpList, BatchIF_Log__c iflog) {
         List<Inquiry_form__c> updateInquiryFormFSEList =
            [select id, Ownerid,Hospital_Name__c,Hospital_Name__r.Name, Hospital_Name__r.ownerId,FSE_Owner__c,
            Hospital_Name__r.Parent.Parent.FSE_GI_Main_Leader__c,Hospital_Name__r.Parent.Parent.FSE_SP_Main_Leader__c from Inquiry_form__c
        where Hospital__c in:hpList
        and isFSEDiffWithAccount__c = true ];
        for (Inquiry_form__c ifo : updateInquiryFormFSEList) {
            if (String.isNotBlank(ifo.Hospital_Name__r.Name) && (ifo.Hospital_Name__r.Name.contains('消化') || ifo.Hospital_Name__r.Name.contains('呼吸') || ifo.Hospital_Name__r.Name.contains('ET')) && ifo.FSE_Owner__c != ifo.Hospital_Name__r.Parent.Parent.FSE_GI_Main_Leader__c) {
                ifo.FSE_Owner__c = ifo.Hospital_Name__r.Parent.Parent.FSE_GI_Main_Leader__c;
                
            }
            if(String.isNotBlank(ifo.Hospital_Name__r.Name) && !ifo.Hospital_Name__r.Name.contains('消化')  && !ifo.Hospital_Name__r.Name.contains('呼吸') && !ifo.Hospital_Name__r.Name.contains('ET') &&  ifo.FSE_Owner__c != ifo.Hospital_Name__r.Parent.Parent.FSE_SP_Main_Leader__c){
               ifo.FSE_Owner__c = ifo.Hospital_Name__r.Parent.Parent.FSE_SP_Main_Leader__c;
            }
        }
        Integer indexCon = 0;
        Database.SaveResult[] lsrUpdateInq = Database.update(updateInquiryFormFSEList, false);
        for (Database.SaveResult lsrChild : lsrUpdateInq) {
            if (!lsrChild.isSuccess()) {
                iflog.Is_Error__c = 3;
                Database.Error emsg = lsrChild.getErrors()[0];
                iflog.ErrorLog__c += 'ownerId: ' + updateInquiryFormFSEList.get(indexCon).ownerId + ' \n'
                                     + 'InquiryFormID: ' + updateInquiryFormFSEList.get(indexCon).Id + '\n ConLog:' + emsg.getMessage() + '\n';
 
            }
            indexCon ++ ;
        }
 
    }
    //20220509 you SWAG-CBUB2W end
    global void finish(Database.BatchableContext BC) {
        system.debug('=====iflog:' + iflog.id);
        iflog.Log__c += 'OpportunityAndContactDailyUpdateBatch finish()\n';
        iflog.Log__c += '\nOpportunityAndContactDailyUpdateBatch end';
 
        String tmp = iflog.ErrorLog__c;
        if (tmp.length() > 65000) {
            tmp = tmp.substring(0, 65000);
            tmp += ' ...have more lines...';
            iflog.ErrorLog__c = tmp;
        }
        String tmp2 = iflog.Log__c;
        if (tmp2.length() > 65000) {
            tmp2 = tmp2.substring(0, 65000);
            tmp2 += ' ...have more lines...';
            iflog.Log__c = tmp2;
        }
        if (System.Label.Log_IO_Flag == 'Keep') {
            update iflog;
        } else if (System.Label.Log_IO_Flag == 'Auto') {
            if (iflog.Is_Error__c > 0) {
                update iflog;
            }
        }
        //add by rentx 2020-12-29
        if (String.isNotBlank(salesdepartment) && salesdepartment.equals('1.华北')) {
            Database.executeBatch(new ConsumablesApplyOrLendBatch(), 50);
        }
        //add by rentx 2020-12-29
 
        //20200819 ljh add start
        //Database.executeBatch(new ConsumablesApplyOrLendBatch(), 50);
        //20200819 ljh add end
    }
    
     @TestVisible private static void test() {
        Integer i = 0;
        
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;//可以多写点
    }
}