高章伟
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
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
@isTest
public class MergeAgencyActivityBatchTest {
    public static Account hospital;
    public static Account agency1;
    public static Account agency2;
    public static Account agency3;
    public static String oldAgencyCode;
    public static String newAgencyCode;
    public static String oldAgencyId15;
    public static String newAgencyId15;
 
    public static Contact contact1;
    public static Contact contact2;
    public static User user1;
    public static User user2;
    public static User user3;
 
    public static Agency_Hospital_Link__c a1HPLink;
    public static Agency_Hospital_Link__c a2HPLink;
    public static Agency_Hospital_Link__c a3HPLink;
 
    public static Date monday;
    public static Id Contact_Agency_RT = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
 
    // Normal Data
    public static void setupNormalData() {
        makeNormalData(true);
    }
    public static void makeNormalData(boolean withAgencyDetail) {
        System.runAs(new User(Id = UserInfo.getUserId())) {
            // create 病院
            hospital = new Account();
            hospital.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId();
            hospital.Name = 'test hospital_1';
            // create 经销商
            agency1 = new Account();
            agency1.Name = 'test1经销商';
            agency1.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            agency2 = new Account();
            agency2.Name = 'test2经销商';
            agency2.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            agency3 = new Account();
            agency3.Name = 'test3经销商';
            agency3.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            insert new List<Account> {hospital, agency1, agency2,agency3};
            List<Account> agencyList = [SELECT Name, Management_Code__c
                    FROM Account WHERE (Id = :agency1.Id OR Id = :agency2.Id)
                    ORDER BY ID];
            oldAgencyCode = agencyList[0].Management_Code__c;
            oldAgencyId15 = ('' + agencyList[0].Id).mid(0, 15);
            newAgencyCode = agencyList[1].Management_Code__c;
            newAgencyId15 = ('' + agencyList[1].Id).mid(0, 15);
 
            // 取引先責任者
            contact1 = new Contact();
            contact1.AccountId = agency1.Id;
            contact1.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            contact1.FirstName = '責任者';
            contact1.LastName = 'test1经销商';
            contact1.Agency_User__c = true;
            Contact contact12 = new Contact();
            contact12.AccountId = agency1.Id;
            contact12.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            contact12.FirstName = '社員2';
            contact12.LastName = 'test1经销商';
            contact12.Agency_User__c = true;
            contact2 = new Contact();
            contact2.AccountId = agency2.Id;
            contact2.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            contact2.FirstName = '責任者';
            contact2.LastName = 'test2经销商';
            contact2.Agency_User__c = true;
            Contact contact22 = new Contact();
            contact22.AccountId = agency2.Id;
            contact22.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            contact22.FirstName = '社員2';
            contact22.LastName = 'test1经销商';
            contact22.Agency_User__c = true;
            Contact contact23 = new Contact();
            contact23.AccountId = agency2.Id;
            contact23.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            contact23.FirstName = '社員3';
            contact23.LastName = 'test2经销商';
            contact23.Agency_User__c = true;
 
            Contact contact3 = new Contact();
            contact3.AccountId = agency3.Id;
            contact3.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            contact3.FirstName = '社員3';
            contact3.LastName = 'test2经销商';
            contact3.Agency_User__c = true;
            insert new List<Contact> {contact1, contact12, contact2, contact22, contact23,contact3};
            // User (link to ContactId)
            Profile p = [SELECT Id FROM Profile WHERE Name = '901_经销商活动系统'];
            user1 = new User();
            user1.ProfileId = p.Id;
            user1.ContactId = contact1.Id;
            user1.FirstName = 'ユーザー';
            user1.LastName = 'テスト';
            user1.Email = 'test_user1@example.com';
            user1.Username = 'test_user1@sb1.example.com' + System.now().millisecond();
            user1.Alias = 'テユa1';
            user1.CommunityNickname = 'tu1' + System.now().millisecond();
            user1.PortalRole = 'Worker';
            user1.emailencodingkey='UTF-8';
            user1.languagelocalekey='zh_CN';
            user1.localesidkey='ja_JP';
            user1.timezonesidkey='Asia/Shanghai';
            user2 = new User();
            user2.ProfileId = p.Id;
            user2.ContactId = contact2.Id;
            user2.FirstName = 'ユーザー';
            user2.LastName = 'テスト';
            user2.Email = 'test_user2@example.com';
            user2.Username = 'test_user2@sb1.example.com' + System.now().millisecond();
            user2.Alias = 'テユa2';
            user2.CommunityNickname = 'tu2' + System.now().millisecond();
            user2.PortalRole = 'Worker';
            user2.emailencodingkey='UTF-8';
            user2.languagelocalekey='zh_CN';
            user2.localesidkey='ja_JP';
            user2.timezonesidkey='Asia/Shanghai';
 
            user3 = new User();
            user3.ProfileId = p.Id;
            user3.ContactId = contact3.Id;
            user3.FirstName = 'ユーザー';
            user3.LastName = 'テスト';
            user3.Email = 'test_user3@example.com';
            user3.Username = 'test_user3@sb1.example.com' + System.now().millisecond();
            user3.Alias = 'テユa3';
            user3.CommunityNickname = 'tu3' + System.now().millisecond();
            user3.PortalRole = 'Worker';
            user3.emailencodingkey='UTF-8';
            user3.languagelocalekey='zh_CN';
            user3.localesidkey='ja_JP';
            user3.timezonesidkey='Asia/Shanghai';
            insert new List<User> {user1, user2,user3};
            user2 = [SELECT FirstName, LastName, ContactId, Contact.AccountId, UserRoleId FROM User WHERE Id = :user2.Id];
            System.assertEquals(agency2.Id, user2.Contact.AccountId);
            UserRole rolesAcc = [SELECT Id, Name, PortalAccountId, PortalRole
                    FROM UserRole
                    WHERE PortalAccountId = :agency2.Id
                    AND PortalRole = 'Worker'];
            UserRole rolesUser = [SELECT Id, Name, PortalAccountId, PortalRole
                    FROM UserRole
                    WHERE Id = :user2.UserRoleId];
            System.assertEquals(rolesAcc.PortalAccountId, rolesUser.PortalAccountId);
            System.assertEquals(rolesAcc.PortalRole, rolesUser.PortalRole);
            System.assertEquals(rolesAcc.Name, rolesUser.Name);
            System.assertEquals(rolesAcc.Id, rolesUser.Id);
        }
        System.runAs(new User(Id = UserInfo.getUserId())) {
            // UserにPermissionSetを設定
            PermissionSet ps = [SELECT ID FROM PermissionSet WHERE Name = 'P002_Partner'];
            insert new List<PermissionSetAssignment> {
                new PermissionSetAssignment(AssigneeId = user1.id, PermissionSetId = ps.Id),
                new PermissionSetAssignment(AssigneeId = user2.id, PermissionSetId = ps.Id)
            };
        }
 
        System.runAs(new User(Id = UserInfo.getUserId())) {
            // 营销关系
            a1HPLink = new Agency_Hospital_Link__c();
            a1HPLink.Name = 'test1经销商医院';
            a1HPLink.Hospital__c = hospital.Id;
            a1HPLink.Agency__c = agency1.Id;
            a1HPLink.Agency_Campaign_Obj__c = true;
            insert a1HPLink;
            a2HPLink = new Agency_Hospital_Link__c();
            a2HPLink.Name = 'test2经销商医院';
            a2HPLink.Hospital__c = hospital.Id;
            a2HPLink.Agency__c = agency2.Id;
            a2HPLink.Agency_Campaign_Obj__c = true;
            insert a2HPLink;
 
            a3HPLink = new Agency_Hospital_Link__c();
            a3HPLink.Name = 'test2经销商医院01';
            a3HPLink.Hospital__c = hospital.Id;
            a3HPLink.Agency__c = agency3.Id;
            a3HPLink.Agency_Campaign_Obj__c = true;
            insert a3HPLink;
 
            a3HPLink.Agency_Campaign_Obj__c = false;
            update a3HPLink; 
 
            if (withAgencyDetail) {
                //.客户人员 (agency1)
                Agency_Contact__c aContact1 = new Agency_Contact__c();
                aContact1.Agency_Hospital__c = a1HPLink.Id;
                aContact1.Type__c = '医生';
                aContact1.Name = 'testAgency医生';
                aContact1.OwnerId = user1.Id;
                Agency_Contact__c aContact2 = new Agency_Contact__c();
                aContact2.Agency_Hospital__c = a1HPLink.Id;
                aContact2.Type__c = '护士';
                aContact2.Name = 'testAgency护士';
                aContact2.OwnerId = user1.Id;
                insert new List<Agency_Contact__c> {aContact1, aContact2};
 
                // 经销商询价 (agency1)
                Agency_Opportunity__c agency_opportunity1 = new Agency_Opportunity__c();
                agency_opportunity1.RecordTypeId = Schema.SObjectType.Agency_Opportunity__c.getRecordTypeInfosByDeveloperName().get('Opportunity').getRecordTypeId();
                agency_opportunity1.Name = '引合1';
                agency_opportunity1.Hospital_Target__c = hospital.Id;
                agency_opportunity1.Agency__c = agency1.Id;
                agency_opportunity1.Agency_Hospital__c = a1HPLink.Id;
                agency_opportunity1.StageName__c = '还没申请预算';
                agency_opportunity1.NewOpportunity_Agency_Apply_Status__c = '批准';
                agency_opportunity1.OwnerId = user1.Id;
                Agency_Opportunity__c agency_opportunity2 = new Agency_Opportunity__c();
                agency_opportunity2.RecordTypeId = Schema.SObjectType.Agency_Opportunity__c.getRecordTypeInfosByDeveloperName().get('Opportunity').getRecordTypeId();
                agency_opportunity2.Name = '引合2';
                agency_opportunity2.Hospital_Target__c = hospital.Id;
                agency_opportunity2.Agency__c = agency1.Id;
                agency_opportunity2.Agency_Hospital__c = a1HPLink.Id;
                agency_opportunity2.StageName__c = '还没申请预算';
                agency_opportunity2.NewOpportunity_Agency_Apply_Status__c = '批准';
                agency_opportunity2.OwnerId = user1.Id;
                insert new List<Agency_Opportunity__c> {agency_opportunity1, agency_opportunity2};
 
                // .周报一览 (agency1)
                monday = System.today().toStartofWeek().addDays(1);
                if (Test.isRunningTest() == true) {
                    OlympusCalendar__c cal1 = new OlympusCalendar__c(Date__c = monday, ChangeToHoliday__c=false, ChangeToWorkday__c=true);
                    OlympusCalendar__c cal2 = new OlympusCalendar__c(Date__c = monday.addDays(-7), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
                    insert new List<OlympusCalendar__c> {cal1, cal2};
                }
 
                String dtGTM = String.valueOf(monday);
                String lastWkGTM = String.valueOf(monday.addDays(-7));
                Agency_Report_Header__c agency_report_header1 = WeeklyReportCmp.makeReportHeader(user1.LastName
                        , dtGTM, contact1.Id, contact1.Id + ':' + dtGTM.replaceAll('-', ''));
                agency_report_header1.OwnerId = user1.Id;
                Agency_Report_Header__c agency_report_header2 = WeeklyReportCmp.makeReportHeader(user1.LastName
                        , lastWkGTM, contact1.Id, contact1.Id + ':' + lastWkGTM.replaceAll('-', ''));
                agency_report_header2.OwnerId = user1.Id;
                insert new List<Agency_Report_Header__c> {agency_report_header1, agency_report_header2};
                agency_report_header1 = [SELECT Id, Agency_ID__c FROM Agency_Report_Header__c WHERE Id = :agency_report_header1.Id];
                System.assertEquals(agency1.Id, agency_report_header1.Agency_ID__c);
 
                // .周报m明细 (agency1)
                ProductTypes__c proType;
                if (Test.isRunningTest() == true) {
                    proType = new ProductTypes__c(Department_Cateogy__c = 'GI;BF;GS;OTH', Name = '260单镜');
                    insert proType;
                } else {
                    proType = [SELECT Id, Department_Cateogy__c, Name FROM ProductTypes__c WHERE Department_Cateogy__c = 'GI;BF;GS;OTH' AND Name = '260单镜'];
                }
                Agency_Report__c agency_report1 = WeeklyReportCmp.makeAgencyReport('GI', '信息收集-日常拜访', agency_report_header1.Id
                        , a1HPLink.Id, contact1.Id, '', dtGTM
                        , proType.Id, '', ''
                        , 'Result', '', '', '', '', '', dtGTM);
                Agency_Report__c agency_report2 = WeeklyReportCmp.makeAgencyReport('GI', '信息收集-日常拜访', agency_report_header2.Id
                        , a1HPLink.Id, contact1.Id, '', lastWkGTM
                        , proType.Id, '', ''
                        , 'Result', '', '', '', '', '', lastWkGTM);
                insert new List<Agency_Report__c> {agency_report1, agency_report2};
            }
        }
    }
 
    /**
     * @description 
     * 没有AgencyReport时不会报错
     */
    @isTest static void test_emptyAgencyReport() {
        makeNormalData(false);
 
        // テストの開始
        Test.startTest();
        MergeAgencyActivityBatch mBatch = new MergeAgencyActivityBatch(oldAgencyCode, newAgencyCode, false);
        Database.executeBatch(mBatch);
        Test.stopTest();
    }
 
    /**
     * @description 
     * OCSM内部人员所有经销商询价不会修改所有人
     */
    @isTest static void test_OCSMAgencyOpportunity() {
        setupNormalData();
 
        Agency_Opportunity__c aOpp = new Agency_Opportunity__c();
        aOpp.RecordTypeId = Schema.SObjectType.Agency_Opportunity__c.getRecordTypeInfosByDeveloperName().get('Opportunity').getRecordTypeId();
        aOpp.Name = '引合3';
        aOpp.Hospital_Target__c = hospital.Id;
        aOpp.Agency__c = agency1.Id;
        aOpp.Agency_Hospital__c = a1HPLink.Id;
        aOpp.StageName__c = '还没申请预算';
        aOpp.NewOpportunity_Agency_Apply_Status__c = '批准';
        aOpp.OwnerId = UserInfo.getUserId();
        insert aOpp;
 
        // テストの開始
        Test.startTest();
        MergeAgencyActivityBatch mBatch = new MergeAgencyActivityBatch(oldAgencyCode, newAgencyCode, false);
        Database.executeBatch(mBatch);
        Test.stopTest();
 
        List<Agency_Opportunity__c> aoppList = [select Id, OwnerId from Agency_Opportunity__c where Name = '::引合3'];
        System.assertEquals(UserInfo.getUserId(), aoppList[0].OwnerId);
    }
 
    /**
     * @description start error test, forceFlg = false
     * 没有找到 或 有多条 旧经销商 [' + this.oldAgencyCode + '] 的数据
     */
    @isTest static void test_oldAgencyCode() {
        setupNormalData();
        //报错的数据
        Account agency1 = new Account(AgentCode_Ext__c = oldAgencyCode);
        agency1.Name = 'test1经销商';
        agency1.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
        insert agency1;
 
        // テストの開始
        Test.startTest();
        MergeAgencyActivityBatch mBatch = new MergeAgencyActivityBatch(oldAgencyCode, newAgencyCode, false);
        Database.executeBatch(mBatch);
        Test.stopTest();
 
        System.assertEquals(1, MergeAgencyActivityBatch.messagesForTest.size(), MergeAgencyActivityBatch.messagesForTest);
        System.assertEquals('没有找到 或 有多条 旧经销商 [' + oldAgencyCode + '] 的数据, size=2', MergeAgencyActivityBatch.messagesForTest[0]);
    }
 
    /**
     * @description start error test, forceFlg = false
     * 没有找到 或 有多条 新经销商 [' + this.newAgencyCode + '] 的数据
     */
    @isTest static void test_newAgencyCode() {
        setupNormalData();
        //报错的数据
        Account agency2 = new Account(AgentCode_Ext__c = newAgencyCode);
        agency2.Name = 'test1经销商';
        agency2.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
        insert agency2;
 
        // テストの開始
        Test.startTest();
        MergeAgencyActivityBatch mBatch = new MergeAgencyActivityBatch(oldAgencyCode, newAgencyCode, false);
        Database.executeBatch(mBatch);
        Test.stopTest();
 
        System.assertEquals(1, MergeAgencyActivityBatch.messagesForTest.size(), MergeAgencyActivityBatch.messagesForTest);
        System.assertEquals('没有找到 或 有多条 新经销商 [' + newAgencyCode + '] 的数据, size=2', MergeAgencyActivityBatch.messagesForTest[0]);
    }
 
    /**
     * @description start error test, forceFlg = false
     * .客户人员 里不可以有 New Agency [' + this.newAgencyCode + '] 的数据
     */
    @isTest static void test_newAgencyContact() {
        setupNormalData();
        //报错的数据
        Agency_Contact__c aContact = new Agency_Contact__c();
        aContact.Agency_Hospital__c = a2HPLink.Id;
        aContact.Type__c = '医生';
        aContact.Name = 'testAgency医生';
        aContact.OwnerId = user2.Id;
        insert aContact;
 
        // テストの開始
        Test.startTest();
        MergeAgencyActivityBatch mBatch = new MergeAgencyActivityBatch(oldAgencyCode, newAgencyCode, false);
        Database.executeBatch(mBatch);
        Test.stopTest();
 
        System.assertEquals(1, MergeAgencyActivityBatch.messagesForTest.size(), MergeAgencyActivityBatch.messagesForTest);
        System.assertEquals('.客户人员 里不可以有 New Agency [' + newAgencyCode + '] 的数据', MergeAgencyActivityBatch.messagesForTest[0]);
    }
 
    /**
     * @description start error test, forceFlg = false
     * 经销商询价 里不可以有 New Agency [' + this.newAgencyCode + '] 的数据
     */
    @isTest static void testStart_newAgencyOpportunity() {
        setupNormalData();
        Agency_Opportunity__c aOpp = new Agency_Opportunity__c();
        aOpp.RecordTypeId = Schema.SObjectType.Agency_Opportunity__c.getRecordTypeInfosByDeveloperName().get('Opportunity').getRecordTypeId();
        aOpp.Name = '引合1';
        aOpp.Hospital_Target__c = hospital.Id;
        aOpp.Agency__c = agency2.Id;
        aOpp.Agency_Hospital__c = a2HPLink.Id;
        aOpp.StageName__c = '还没申请预算';
        aOpp.NewOpportunity_Agency_Apply_Status__c = '批准';
        aOpp.OwnerId = user2.Id;
        insert aOpp;
 
        // テストの開始
        Test.startTest();
        MergeAgencyActivityBatch mBatch = new MergeAgencyActivityBatch(oldAgencyCode, newAgencyCode, false);
        Database.executeBatch(mBatch);
        Test.stopTest();
 
        System.assertEquals(1, MergeAgencyActivityBatch.messagesForTest.size(), MergeAgencyActivityBatch.messagesForTest);
        System.assertEquals('经销商询价 里不可以有 New Agency [' + newAgencyCode + '] 的数据', MergeAgencyActivityBatch.messagesForTest[0]);
    }
 
    /**
     * @description start error test, forceFlg = false
     * .周报一览 里不可以有 New Agency [' + this.newAgencyCode + '] 的数据
     */
    @isTest static void test_newAgencyReportHeader() {
        setupNormalData();
        //报错的数据
        String dtGTM = String.valueOf(monday);
        Agency_Report_Header__c arh = WeeklyReportCmp.makeReportHeader(user2.LastName
                , dtGTM, contact2.Id, contact2.Id + ':' + dtGTM.replaceAll('-', ''));
        arh.OwnerId = user2.Id;
        insert new List<Agency_Report_Header__c> {arh};
 
        // テストの開始
        Test.startTest();
        MergeAgencyActivityBatch mBatch = new MergeAgencyActivityBatch(oldAgencyCode, newAgencyCode, false);
        Database.executeBatch(mBatch);
        Test.stopTest();
 
        System.assertEquals(1, MergeAgencyActivityBatch.messagesForTest.size(), MergeAgencyActivityBatch.messagesForTest);
        System.assertEquals('.周报一览 里不可以有 New Agency [' + newAgencyCode + '] 的数据', MergeAgencyActivityBatch.messagesForTest[0]);
    }
 
    /**
     * @description start test, forceFlg = true
     * // 5. New Contact 件数, 补了一个Contact1
     * // 7. Agency_Report_Header の結果を確認
     * // 3. Agency_Contact の結果を確認
     * // 4. Agency_Opportunity の結果を確認
     * // 8. Agency_Report の結果を確認
     */
    @isTest static void test_newAgencyOpportunity_Force() {
        setupNormalData();
        //报错的数据, 但是 forceFlg = true, 不报错
        Agency_Opportunity__c aOpp = new Agency_Opportunity__c();
        aOpp.RecordTypeId = Schema.SObjectType.Agency_Opportunity__c.getRecordTypeInfosByDeveloperName().get('Opportunity').getRecordTypeId();
        aOpp.Name = '引合1';
        aOpp.Hospital_Target__c = hospital.Id;
        aOpp.Agency__c = agency2.Id;
        aOpp.Agency_Hospital__c = a2HPLink.Id;
        aOpp.StageName__c = '还没申请预算';
        aOpp.NewOpportunity_Agency_Apply_Status__c = '批准';
        aOpp.OwnerId = user2.Id;
        insert aOpp;
 
        String dtGTM = String.valueOf(monday);
        Agency_Report_Header__c arh = WeeklyReportCmp.makeReportHeader(user2.LastName
                , dtGTM, contact2.Id, contact2.Id + ':' + dtGTM.replaceAll('-', ''));
        arh.OwnerId = user2.Id;
        insert new List<Agency_Report_Header__c> {arh};
 
        // テストの開始
        Test.startTest();
        MergeAgencyActivityBatch mBatch = new MergeAgencyActivityBatch(oldAgencyCode, newAgencyCode, true);
        Database.executeBatch(mBatch);
        Test.stopTest();
 
        // バッチエラーなし
        System.assertEquals(0, MergeAgencyActivityBatch.messagesForTest.size(), MergeAgencyActivityBatch.messagesForTest);
        // 5. New Contact 件数, 补了一个Contact1
        List<Contact> a1ContactList = [SELECT Id, Name, LastName, FirstName, Agency_User__c, AccountId, RecordTypeId FROM Contact WHERE AccountId = :agency1.Id ORDER BY Name];
        List<Contact> a2ContactList = [SELECT Id, Name, LastName, FirstName, Agency_User__c, AccountId, RecordTypeId FROM Contact WHERE AccountId = :agency2.Id ORDER BY Name];
        System.assertEquals(2, a1ContactList.size());
        System.assertEquals(4, a2ContactList.size());
        Contact newContact1;            // batchで作った新Contact
        for (Contact nObj : a2ContactList) {
            if (nObj.LastName == contact1.LastName && nObj.FirstName == contact1.FirstName) {
                newContact1 = nObj;
            }
            System.assertEquals(agency2.Id, nObj.AccountId);
            System.assertEquals(true, nObj.Agency_User__c);
            System.assertEquals(Contact_Agency_RT, nObj.RecordTypeId);
        }
        System.assertNotEquals(null, newContact1, a2ContactList);
 
        // 7. Agency_Report_Header の結果を確認
        List<Agency_Report_Header__c> arhList = [SELECT Id, Name, OwnerId
                    , Agency__c, Old_Agency__c, Agency_Person2__c, Old_Agency_Person2__c, Old_OwnerId__c
                FROM Agency_Report_Header__c
                WHERE Agency__r.Management_Code__c = :newAgencyCode];
        System.assertEquals(3, arhList.size());
        Id grpId = MergeAgencyActivityBatch.accIdGrpIdMap(agency2.Id);
        for (Agency_Report_Header__c nObj : arhList) {
            if (nObj.Id == arh.Id) {
                System.assertEquals(agency2.Id, nObj.Agency__c);
                System.assertEquals(null, nObj.Old_Agency__c);
                System.assertEquals(contact2.Id, nObj.Agency_Person2__c);
                System.assertEquals(null, nObj.Old_Agency_Person2__c);
                System.assertEquals(null, nObj.Old_OwnerId__c);
                System.assertEquals(user2.Id, nObj.OwnerId);
            } else {
                System.assertEquals(agency2.Id, nObj.Agency__c);
                System.assertEquals(agency1.Id, nObj.Old_Agency__c);
                System.assertEquals(newContact1.Id, nObj.Agency_Person2__c);
                System.assertEquals(contact1.Id, nObj.Old_Agency_Person2__c);
                System.assertEquals(user1.Id, nObj.Old_OwnerId__c);
                System.assertEquals(user2.Id, nObj.OwnerId);
            }
            // List<Agency_Report_Header__share> tsList = [SELECT Id, UserOrGroupId
            //         FROM Agency_Report_Header__share WHERE ParentId = :nObj.Id AND RowCause = 'Manual'];
            // System.assertEquals(1, tsList.size());
            // System.assertEquals(grpId, tsList[0].UserOrGroupId);
        }
 
        // 3. Agency_Contact の結果を確認
        // List<Agency_Contact__c> acList = [SELECT Id, Name, OwnerId
        //             , Old_Agency_Hospital__c, Old_Agency_ID__c, Agency_ID__c, Agency_Hospital__c, Old_OwnerId__c
        //         FROM Agency_Contact__c
        //         WHERE Agency_Hospital__r.Agency__r.Management_Code__c = :newAgencyCode];
        // System.assertEquals(2, acList.size());
        // for (Agency_Contact__c nObj : acList) {
        //     System.assertEquals(agency2.Id, nObj.Agency_ID__c);
        //     System.assertEquals(agency1.Id, nObj.Old_Agency_ID__c);
        //     System.assertEquals(a2HPLink.Id, nObj.Agency_Hospital__c);
        //     System.assertEquals(a1HPLink.Id, nObj.Old_Agency_Hospital__c);
        //     System.assertEquals(user1.Id, nObj.Old_OwnerId__c);
        //     System.assertEquals(user2.Id, nObj.OwnerId);
        // }
 
        // 4. Agency_Opportunity の結果を確認
        // List<Agency_Opportunity__c> aoppList = [SELECT Id, Name
        //             , Old_OwnerId__c, Old_Agency__c, Old_Agency_Hospital__c, Old_Agency_Hospital_All__c, Old_Agency_Hospital_Target__c, Old_TargetInputKey__c
        //             , OwnerId, Agency__c, Agency_ID__c, Agency_Hospital__c, Agency_Hospital_All__c, Agency_Hospital_Target__c, TargetInputKey__c
        //         FROM Agency_Opportunity__c
        //         WHERE Agency_Hospital_All__r.Agency__r.Management_Code__c = :newAgencyCode];
        // System.assertEquals(3, aoppList.size());
        // for (Agency_Opportunity__c nObj : aoppList) {
        //     if (nObj.Id == aOpp.Id) {
        //         System.assertEquals(agency2.Id, nObj.Agency__c);
        //         System.assertEquals(null, nObj.Old_Agency__c);
        //         System.assertEquals(a2HPLink.Id, nObj.Agency_Hospital_All__c);
        //         System.assertEquals(null, nObj.Old_Agency_Hospital_All__c);
        //         System.assertEquals(null, nObj.Old_OwnerId__c);
        //         System.assertEquals(user2.Id, nObj.OwnerId);
        //    } else {
        //         System.assertEquals(agency2.Id, nObj.Agency__c);
        //         System.assertEquals(agency1.Id, nObj.Old_Agency__c);
        //         System.assertEquals(a2HPLink.Id, nObj.Agency_Hospital_All__c);
        //         System.assertEquals(a1HPLink.Id, nObj.Old_Agency_Hospital_All__c);
        //         System.assertEquals(user1.Id, nObj.Old_OwnerId__c);
        //         System.assertEquals(user2.Id, nObj.OwnerId);
        //     }
        // }
 
        // 8. Agency_Report の結果を確認
        // List<Agency_Report__c> arList = [SELECT Id, Name
        //             , Old_Agency__c, Old_Agency_Hospital__c, Old_Person_In_Charge2__c
        //             , Agency__c, Agency_ID__c, Agency_Hospital__c, Person_In_Charge2__c
        //         FROM Agency_Report__c WHERE Agency__c = :agency2.Id];
        // System.assertEquals(2, acList.size());
        // for (Agency_Report__c nObj : arList) {
        //     System.assertEquals(agency1.Id, nObj.Old_Agency__c);
        //     System.assertEquals(a2HPLink.Id, nObj.Agency_Hospital__c);
        //     System.assertEquals(a1HPLink.Id, nObj.Old_Agency_Hospital__c);
        //     System.assertEquals(newContact1.Id, nObj.Person_In_Charge2__c);
        //     System.assertEquals(contact1.Id, nObj.Old_Person_In_Charge2__c);
        // }
    }
}