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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
@isTest
private class OCMManagementProvinceBatchTest {
 
    @isTest static void test_method_one() {
        ControllerUtil.EscapeNFM001Trigger = true;
 
        Profile p = [select id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        String loginId = UserInfo.getUserId();
        User sys = [select id from User where Id = :loginId];
 
        User u1 = new User(Test_staff__c = true);
        u1.LastName = '_サンブリッジ';
        u1.Batch_User__c = true;
        u1.Alias = 'あ';
        u1.Email = 'olympusTest01@sunbridge.com';
        u1.Username = 'olympusTest10@sunbridge.com';
        u1.CommunityNickname = 'あ1';
        u1.IsActive = true;
        u1.EmailEncodingKey = 'ISO-2022-JP';
        u1.TimeZoneSidKey = 'Asia/Tokyo';
        u1.LocaleSidKey = 'ja_JP';
        u1.LanguageLocaleKey = 'ja';
        u1.ProfileId = p.id;
        u1.Job_Category__c = '销售服务';
        u1.Province__c = '東京';
 
        User u2 = new User(Test_staff__c = true);
        u2.LastName = '_サンブリッジ';
        u2.Batch_User__c = true;
        u2.Alias = 'い';
        u2.Email = 'olympusTest02@sunbridge.com';
        u2.Username = 'olympusTest02@sunbridge.com';
        u2.CommunityNickname = 'い';
        u2.IsActive = true;
        u2.EmailEncodingKey = 'ISO-2022-JP';
        u2.TimeZoneSidKey = 'Asia/Tokyo';
        u2.LocaleSidKey = 'ja_JP';
        u2.LanguageLocaleKey = 'ja';
        u2.ProfileId = p.id;
        u2.Job_Category__c = '销售推广';
        u2.Province__c = '東京';
 
        User u3 = new User(Test_staff__c = true);
        u3.LastName = '_サンブリッジ';
        u3.Batch_User__c = true;
        u3.Alias = 'う';
        u3.Email = 'olympusTest03@sunbridge.com';
        u3.Username = 'olympusTest03@sunbridge.com';
        u3.CommunityNickname = 'う';
        u3.IsActive = true;
        u3.EmailEncodingKey = 'ISO-2022-JP';
        u3.TimeZoneSidKey = 'Asia/Tokyo';
        u3.LocaleSidKey = 'ja_JP';
        u3.LanguageLocaleKey = 'ja';
        u3.ProfileId = p.id;
        u3.Job_Category__c = '支援';
        u3.Province__c = '東京';
        u3.Dept__c = '能量事业本部';
 
        insert new User[] {u1, u2, u3};
 
        System.runAs(sys) {
            OCM_Management_Province__c mp1 = new OCM_Management_Province__c();
            mp1.Name = '北京';
            mp1.GI_assistant__c = u1.Id;
            mp1.SP_assistant__c = u1.Id;
            mp1.Energy_assistant__c = u1.Id;
 
            OCM_Management_Province__c mp2 = new OCM_Management_Province__c();
            mp2.Name = '上海';
            mp2.GI_assistant__c = u2.Id;
            mp2.SP_assistant__c = u2.Id;
            mp2.Energy_assistant__c = u2.Id;
 
            OCM_Management_Province__c mp3 = new OCM_Management_Province__c();
            mp3.Name = '深圳';
            mp3.GI_assistant__c = u3.Id;
            mp3.SP_assistant__c = u3.Id;
            mp3.Energy_assistant__c = u3.Id;
 
            OCM_Management_Province__c mp4 = new OCM_Management_Province__c();
            mp4.Name = '特约经销商管理支援本部';
            mp4.GI_assistant__c = loginId;
            mp4.SP_assistant__c = loginId;
            mp4.Energy_assistant__c = loginId;
 
            insert new OCM_Management_Province__c[] {mp1, mp2, mp3, mp4};
 
            Address_Level__c al1 = new Address_Level__c();
            al1.Name = '北京市';
            al1.Level1_Code__c = 'CN-01';
            al1.Level1_Sys_No__c = '353341';
 
            Address_Level__c al2 = new Address_Level__c();
            al2.Name = '上海市';
            al2.Level1_Code__c = 'CN-16';
            al2.Level1_Sys_No__c = '353334';
 
            Address_Level__c al3 = new Address_Level__c();
            al3.Name = '广东省';
            al3.Level1_Code__c = 'CN-22';
            al3.Level1_Sys_No__c = '353343';
 
            insert new Address_Level__c[] {al1, al2, al3};
 
            Address_Level2__c al21 = new Address_Level2__c();
            al21.Level1_Code__c = 'CN-01';
            al21.Level1_Sys_No__c = '353341';
            al21.Level1_Name__c = '北京市';
            al21.Name = '海淀区';
            al21.Level2_Code__c = 'CN-0123';
            al21.Level2_Sys_No__c = '353664';
            al21.Address_Level__c = al1.id;
 
            Address_Level2__c al22 = new Address_Level2__c();
            al22.Level1_Code__c = 'CN-16';
            al22.Level1_Sys_No__c = '353334';
            al22.Level1_Name__c = '上海市';
            al22.Name = '徐汇区';
            al22.Level2_Code__c = 'CN-1631';
            al22.Level2_Sys_No__c = '353572';
            al22.Address_Level__c = al2.id;
 
            Address_Level2__c al23 = new Address_Level2__c();
            al23.Level1_Code__c = 'CN-22';
            al23.Level1_Sys_No__c = '353343';
            al23.Level1_Name__c = '广东省';
            al23.Name = '深圳市';
            al23.Level2_Code__c = 'CN-2259';
            al23.Level2_Sys_No__c = '353700';
            al23.Address_Level__c = al3.id;
 
            insert new Address_Level2__c[] {al21, al22, al23};
 
            RecordType rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Hp'];
            List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName IN ('Department_GI', 'Department_GS') order by DeveloperName];
 
            Account acc1 = new Account();
            acc1.RecordTypeId = rectCo.Id;
            acc1.Name = 'HP test1';
            //acc1.OCM_Management_Province__c = mp1.Id;
            acc1.State_Master__c = al1.Id;
            acc1.City_Master__c = al21.Id;
 
            Account acc2 = new Account();
            acc2.RecordTypeId = rectCo.Id;
            acc2.Name = 'HP test2';
            //acc2.OCM_Management_Province__c = mp2.Id;
            acc2.State_Master__c = al2.Id;
            acc2.City_Master__c = al22.Id;
 
            Account acc3 = new Account();
            acc3.RecordTypeId = rectCo.Id;
            acc3.Name = 'HP test3';
            //acc3.OCM_Management_Province__c = mp3.Id;
            acc3.State_Master__c = al3.Id;
            acc3.City_Master__c = al23.Id;
 
            insert new Account[] {acc1, acc2, acc3};
 
            List<Account> dept1 = [select Id, Name from Account where ParentId = :acc1.Id and Department_Class_Label__c = '消化科'];
            List<Account> dept2 = [select Id, Name from Account where ParentId = :acc2.Id and Department_Class_Label__c = '普外科'];
            List<Account> dept3 = [select Id, Name from Account where ParentId = :acc3.Id and Department_Class_Label__c = '消化科'];
 
            dept3[0].OwnerId = u3.Id;
            update dept3[0];
 
            Account depart1 = new Account();
            depart1.RecordTypeId = rectDpt[0].Id;
            depart1.Name         = '*';
            depart1.Department_Name__c  = '診療科1';
            depart1.ParentId            = dept1[0].Id;
            depart1.Department_Class__c = dept1[0].Id;
            depart1.Hospital__c         = acc1.Id;
 
            Account depart2 = new Account();
            depart2.RecordTypeId = rectDpt[1].Id;
            depart2.Name         = '*';
            depart2.Department_Name__c  = '診療科2';
            depart2.ParentId            = dept2[0].Id;
            depart2.Department_Class__c = dept2[0].Id;
            depart2.Hospital__c         = acc2.Id;
 
            Account depart3 = new Account();
            depart3.RecordTypeId = rectDpt[0].Id;
            depart3.Name         = '*';
            depart3.Department_Name__c  = '診療科3';
            depart3.ParentId            = dept3[0].Id;
            depart3.Department_Class__c = dept3[0].Id;
            depart3.Hospital__c         = acc3.Id;
 
            insert new Account[] {depart1, depart2, depart3};
 
            RecordType oppVND = [select id from RecordType where IsActive = true and SobjectType = 'Opportunity' and DeveloperName = 'Opportunity'];
 
            System.Test.startTest();
            Opportunity opp1 = new Opportunity(
                Name = 'test opp1',
                StageName = '引合',
                CurrencyIsoCode = 'USD',
                CloseDate = Date.today().addDays(5),
                AccountId = depart1.Id,
                RecordTypeId = oppVND.Id,
                Closing_Bid_Date__c = Date.today(),
                Hospital__c = acc1.Id,
                Competitor__c = 'A',
                SAP_Send_OK__c = false,
                Close_Forecasted_Date__c = date.today().addDays(1),
                Opportunity_Category__c = 'GI'
            );
            Opportunity opp2 = new Opportunity(
                Name = 'test opp2',
                StageName = '引合',
                CurrencyIsoCode = 'USD',
                CloseDate = Date.today().addDays(5),
                AccountId = depart2.Id,
                RecordTypeId = oppVND.Id,
                Closing_Bid_Date__c = Date.today(),
                Hospital__c = acc2.Id,
                Competitor__c = 'B',
                SAP_Send_OK__c = false,
                Close_Forecasted_Date__c = date.today().addDays(1),
                Opportunity_Category__c = 'SP'
            );
            Opportunity opp3 = new Opportunity(
                Name = 'test opp3',
                StageName = '引合',
                CurrencyIsoCode = 'USD',
                CloseDate = Date.today().addDays(5),
                AccountId = depart3.Id,
                RecordTypeId = oppVND.Id,
                Closing_Bid_Date__c = Date.today(),
                Hospital__c = acc3.Id,
                Competitor__c = 'B',
                Opportunity_Category__c = 'GI',
                OwnerId = u3.Id,
                SAP_Send_OK__c = false,
                Close_Forecasted_Date__c = date.today().addDays(2),
                owner_not_automatically_update__c = true
            );
            Opportunity opp4 = new Opportunity(
                Name = 'test opp4',
                StageName = '引合',
                CurrencyIsoCode = 'USD',
                CloseDate = Date.today().addDays(5),
                AccountId = depart3.Id,
                RecordTypeId = oppVND.Id,
                Closing_Bid_Date__c = Date.today(),
                Hospital__c = acc3.Id,
                Competitor__c = 'B',
                Opportunity_Category__c = 'GI',
                SAP_Send_OK__c = false,
                Close_Forecasted_Date__c = date.today().addDays(1),
                Distributor_InCharge_opp__c = true
            );
            insert new Opportunity[] {opp1, opp2, opp3, opp4};
 
            List<Opportunity> oppList = [select id,Sales_assistant_name_text__c from Opportunity order by Name];
            // System.assertEquals(u1.Id, oppList[0].Sales_assistant_name_text__c);
            // System.assertEquals(u2.Id, oppList[1].Sales_assistant_name_text__c);
            // System.assertEquals(u3.Id, oppList[2].Sales_assistant_name_text__c);
            // System.assertEquals(loginId, oppList[3].Sales_assistant_name_text__c);
 
            opp1.Sales_assistant_name_text__c = UserInfo.getUserId();
            opp2.Sales_assistant_name_text__c = null;
            opp3.Sales_assistant_name_text__c = null;
            opp4.Sales_assistant_name_text__c = null;
            update new Opportunity[] {opp1, opp2, opp3, opp4};
 
           
 
            OCMManagementProvinceWebService.updateOpportunity();
 
            System.Test.stopTest();
        }
    }
 
    @isTest static void test_method_two() {
        Id pricebookId = ControllerUtil.getStandardPricebook().Id;
 
        Product2 product = new Product2( Name='テスト商品');
        product.SFDA_Status__c = '有効';
        product.Intra_Trade_List_RMB_1__c = 100;
        product.Intra_Trade_List_RMB_Date1__c = date.today();
        product.Intra_Trade_Cost_RMB_1__c = 200;
        product.Intra_Trade_Cost_RMB_Date1__c = date.today();
        product.Manual_Entry__c = false;
        product.Asset_Model_No__c = '11111';
        product.ProductCode = 'OTV-SP1H-NA-12E';
        insert product;
 
        PricebookEntry entry = new PricebookEntry( Pricebook2Id=pricebookId, Product2Id=product.Id);
        entry.UnitPrice = 0;
        entry.IsActive = true;
        entry.UseStandardPrice = false;
        entry.CurrencyIsoCode = 'CNY';
        insert entry;
 
        Opportunity opp = new Opportunity();
        opp.Name='aiueo';
        opp.StageName='contact';
        opp.Trade__c = '内貿';
        opp.CloseDate=Date.today();
        opp.CurrencyIsoCode = 'CNY';
        opp.Estimation_List_Price__c = 100;
        opp.Wholesale_Price__c = 101;
        opp.Dealer_Final_Price__c = 102;
        opp.OCM_Agent1_Price__c = 103;
        opp.Stocking_Price__c = 104;
        opp.Estimation_No__c = '105';
        opp.Estimation_Name__c = '106';
        opp.Estimation_Id__c = '107';
        insert opp;
 
        OpportunityLineItem oppli = new OpportunityLineItem();
        oppli.OpportunityId = opp.Id;
        oppli.Id__c = '110';
        oppli.SFDA_Status__c = '有効';
        oppli.Name__c = '111';
        oppli.ListPrice__c = 112;
        oppli.Quantity = 113;
        oppli.UnitPrice = 114;
        oppli.UnitPrice__c = 115;
        oppli.Qty_Unit__c = '116';
        oppli.Cost__c = 117;
        oppli.BSS_Category__c = 'G&R';
        oppli.Item_Order__c = 1;
        oppli.PricebookEntryId = entry.Id;
        insert oppli;
 
        Quote quo = new Quote();
        quo.Name = '206';
        quo.OpportunityId = opp.Id;
        quo.Quote_No__c = '205';
        quo.Estimation_List_Price__c = 200;
        quo.Dealer_Final_Price__c = 202;
        quo.Stocking_Price__c = 204;
        quo.OCM_Sales_Forecast__c = 201;
        quo.OCM_Agent1_Price__c = 203;
        quo.Pricebook2Id = pricebookId;
        insert quo;
 
        QuoteLineItem qli = new QuoteLineItem();
        qli.QuoteId = quo.Id;
        qli.Id__c = '210';
        qli.SFDA_Status__c = '有効';
        qli.Name__c = '211';
        qli.ListPrice__c = 212;
        qli.Quantity = 213;
        qli.UnitPrice__c = 215;
        qli.UnitPrice = 0;
        qli.Qty_Unit__c = '216';
        qli.Cost__c = 217;
        qli.BSS_Category__c = 'ET';
        qli.Subtotal__c = 218;
        qli.PricebookEntryId = entry.Id;
        insert qli;
 
        opp.Estimation_Id__c = quo.Id;
        opp.Estimation_Decision__c = true;
        update opp;
 
        opp.Estimation_Decision__c = false;
        update opp;
    }
    @isTest static void test_method_three() {
         //医院
        //  List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Agency'];
        //  if (rectHp.size() == 0) {
        //      return;
        //  } 
         String rectHpid = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
         Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
         User hpOwner = new User(Test_staff__c = true, LastName = 'hp',  Alias = 'hp', Work_Location__c = '北京', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
         
         User thisUser = [select Id,Alias from User where Id = :UserInfo.getUserId() ];
         System.runAs ( thisUser ){
             insert hpOwner;
             // 省
             Address_Level__c al = new Address_Level__c();
             al.Name = '广东省';
             al.Level1_Code__c = 'CN-99';
             al.Level1_Sys_No__c = '999999';
             insert al;
             // 市
             Address_Level2__c al2 = new Address_Level2__c();
             al2.Level1_Code__c = 'CN-99';
             al2.Level1_Sys_No__c = '999999';
             al2.Level1_Name__c = '广东省';
             al2.Name = '深圳市';
             al2.Level2_Code__c = 'CN-9999';
             al2.Level2_Sys_No__c = '9999999';
             al2.Address_Level__c = al.id;
             insert al2;
            //  医院
            ControllerUtil.EscapeNFM001Trigger = true;
StaticParameter.EscapeNFM001Trigger = true;
StaticParameter.EscapeNFM001AgencyContractTrigger = true;
StaticParameter.EscapeNFM001AgencyContractTrigger2 = true;
             Account hp = new Account(RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId(), Name = 'hp', OwnerId = thisUser.Id);
             hp.Name = 'test hospital';
             hp.FSE_GI_Main_Leader__c = thisUser.Id;
             hp.FSE_SP_Main_Leader__c = thisUser.Id;
             hp.State_Master__c = al.id;
             hp.City_Master__c = al2.id;
             hp.ET_owner__c = thisUser.Id;
             hp.OCM_man_province_txt__c = '深圳';
             insert hp;
 
            // 战略科室
             Account section = new Account();
            section.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_Class_BF').getRecordTypeId();
            section.Name         = '*';
            section.Department_Class_Label__c = '消化科';
            section.ParentId                  = hp.Id;
            section.Hospital_Department_Class__c = hp.Id;
            upsert section;
            // 科室
            Account depart = new Account();
            depart.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_GI').getRecordTypeId();
            depart.Name         = '*';
            depart.Department_Name__c  = 'NFM007TestDepart';
            depart.ParentId            = section.Id;
            depart.Department_Class__c = section.Id;
            depart.Hospital__c         = hp.Id;
            upsert depart;
 
            // //  RecordType recByoin = [select id from RecordType where IsActive = true and SobjectType= 'Account' and Name='病院' limit 1];
            //  Account hospital = new Account(Name = 'テスト病院');
            //  hospital.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId();
            //  hospital.Name = 'test hospital1';
            //  hospital.ET_owner__c = UserInfo.getUserId();
            //  hospital.State_Master__c = al.id;
            //  hospital.City_Master__c = al2.id;
            //  hospital.OCM_man_province_txt__c = '深圳';
            //  insert hospital;
            //  医院
            String agencyid = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            Account agency = new Account(RecordTypeId =agencyid , Name = 'hp', OwnerId = thisUser.Id);
            agency.Name = 'test hospital';
            agency.FSE_GI_Main_Leader__c = thisUser.Id;
            agency.FSE_SP_Main_Leader__c = thisUser.Id;
            agency.State_Master__c = al.id;
            agency.City_Master__c = al2.id;
            agency.ET_owner__c = thisUser.Id;
            agency.OCM_man_province_txt__c = '深圳';
            insert agency;
             OCM_Management_Province__c mp1 = new OCM_Management_Province__c();
             mp1.Name = '深圳';
             mp1.Province__c = '广东省';
             mp1.Window1__c = hpOwner.Id;
             mp1.ET_DataWindow__c = hpOwner.Id;
             mp1.ET_BusinessWindow__c = hpOwner.Id;
             mp1.ET_Assistant1__c = hpOwner.Id;
             mp1.ET_Assistant2__c = hpOwner.Id;
             mp1.ENG_DataWindow__c = hpOwner.Id;
             mp1.ENG_BusinessWindow__c = hpOwner.Id;
             mp1.ETShareOpp__c = hpOwner.Id;
             mp1.ENGShareOpp__c = hpOwner.Id;
             mp1.ENG_Assistant1__c = hpOwner.Id;
             mp1.ENG_Assistant2__c = hpOwner.Id;
             insert mp1;
             System.Test.startTest();
             Opportunity opp = new Opportunity(
                Name = 'test opp1',
                StageName = '完了',
                CurrencyIsoCode = 'USD',
                CloseDate = Date.today().addDays(5),
                AccountId = depart.Id,
                Department_Class__c = section.Id,
                Hospital__c         = hp.Id,
                Closing_Bid_Date__c = Date.today(),
                //Hospital__c = hp.Id,
                Competitor__c = 'A',
                SAP_Send_OK__c = false,
                Close_Forecasted_Date__c = date.today().addDays(1),
                Opportunity_Category__c = 'GI',
                Opp_order_Type__c ='ET',
                SAP_Province__c = '广东省'
            );
            // insert opp;
            // Opportunity opp = new Opportunity();
            // opp.Name='aiueo';
            // opp.StageName='完了';
            // opp.Trade__c = '内貿';
            // opp.CloseDate=Date.today();
            // opp.CurrencyIsoCode = 'CNY';
            // opp.Estimation_List_Price__c = 100;
            // opp.Wholesale_Price__c = 101;
            // opp.Dealer_Final_Price__c = 102;
            // opp.OCM_Agent1_Price__c = 103;
            // opp.Stocking_Price__c = 104;
            // opp.Estimation_No__c = '105';
            // opp.Estimation_Name__c = '106';
            // opp.Estimation_Id__c = '107';
            // opp.Opp_order_Type__c ='ET';
            // opp.SAP_Province__c = '广东省';
            // insert opp;
            // Opportunity opp1 = new Opportunity();
            // opp1.Name='aiueo';
            // opp1.StageName='完了';
            // opp1.Trade__c = '内貿';
            // opp1.CloseDate=Date.today();
            // opp1.CurrencyIsoCode = 'CNY';
            // opp1.Estimation_List_Price__c = 100;
            // opp1.Wholesale_Price__c = 101;
            // opp1.Dealer_Final_Price__c = 102;
            // opp1.OCM_Agent1_Price__c = 103;
            // opp1.Stocking_Price__c = 104;
            // opp1.Estimation_No__c = '105';
            // opp1.Estimation_Name__c = '106';
            // opp1.Estimation_Id__c = '107';
            // opp1.Opp_order_Type__c ='ENG';
            // opp1.SAP_Province__c = '广东省';
            // opp1.Department_Class__c = section.Id;
            // opp1.Hospital__c         = hp.Id;
            // opp1.AccountId = depart.Id;
 
            // insert opp1;
            insert new Opportunity[] {opp};
            // 共享计划创建
            // opp = [select id,OCM_man_province_new__c from  Opportunity where id = :opp.id];
            // System.assertEquals('深圳', opp.OCM_man_province_new__c);
            OpportunityShare oppshare = new OpportunityShare();
            oppshare.RowCause= 'Manual';//手动
            oppshare.OpportunityId = opp.id;
            oppshare.UserOrGroupId = hpOwner.Id;
            oppshare.OpportunityAccessLevel = 'Read';//编辑
            // insert oppshare;
            // OpportunityShare oppshare1 = new OpportunityShare();
            // oppshare1.RowCause= 'Manual';//手动
            // oppshare1.OpportunityId = opp1.id;
            // oppshare1.UserOrGroupId = hpOwner.Id;
            // oppshare1.OpportunityAccessLevel = 'Read';//编辑
            // insert oppshare1;
            insert new OpportunityShare[] {oppshare};
            //TMS数据
            Dealer_ConsumableManage__c tms =new Dealer_ConsumableManage__c();
            tms.Dealer_Name__c =agency.id;
            tms.ET_ENG__c ='ET';
            tms.TMS_Key__c='1';
            // insert tms;
            
            // Dealer_ConsumableManage__c tms1 =new Dealer_ConsumableManage__c();
            // tms1.Dealer_Name__c =hp.id;
            // tms1.ET_ENG__c ='ENG';
            // tms1.TMS_Key__c='2';
            // insert tms1;
            insert new Dealer_ConsumableManage__c[] {tms};
            // 共享计划创建
            // tms = [select id,TMS_Province__c  from Dealer_ConsumableManage__c where id = :tms.id];
            // System.assertEquals('深圳', tms.TMS_Province__c);
            Dealer_ConsumableManage__Share tmsshare = new Dealer_ConsumableManage__Share();
            tmsshare.RowCause= 'OCSMToTMSUser__c';//手动
            tmsshare.ParentId = tms.id;
            tmsshare.UserOrGroupId = hpOwner.Id;
            tmsshare.AccessLevel = 'Read';
            insert tmsshare;
            //IMS数据
            Oly_TriggerHandler.bypass('ConsumableTargetManageHandler');
            ConsumableTargetManage__c ims =new ConsumableTargetManage__c();
            ims.Hospital__c =hp.id;
            ims.productType__c ='ET耗材';
            ims.ConsumableTargetManageKey__c ='001';
            // insert ims;
            // ConsumableTargetManage__c ims1 =new ConsumableTargetManage__c();
            // ims1.Hospital__c =agency.id;
            // ims1.productType__c ='外科耗材';
            // ims1.ConsumableTargetManageKey__c ='002';
            // insert ims1;
            insert new ConsumableTargetManage__c[] {ims};
 
        
             // 共享计划创建
            ConsumableTargetManage__Share imsshare = new ConsumableTargetManage__Share();
            imsshare.RowCause= 'OCSMToIMSUser__c';//手动
            imsshare.ParentId = ims.id;
            imsshare.UserOrGroupId = hpOwner.Id;
            imsshare.AccessLevel = 'Read';
            insert imsshare;
            
 
            OCMManagementProvinceWebService.updateOpportunity();
            System.Test.stopTest();
         }
    }
    @isTest static void test_method_4() {
        //医院
       //  List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Agency'];
       //  if (rectHp.size() == 0) {
       //      return;
       //  } 
        String rectHpid = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        User hpOwner = new User(Test_staff__c = true, LastName = 'hp',  Alias = 'hp', Work_Location__c = '北京', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        
        User thisUser = [select Id,Alias from User where Id = :UserInfo.getUserId() ];
        System.runAs ( thisUser ){
            insert hpOwner;
            // 省
            Address_Level__c al = new Address_Level__c();
            al.Name = '广东省';
            al.Level1_Code__c = 'CN-99';
            al.Level1_Sys_No__c = '999999';
            insert al;
            // 市
            Address_Level2__c al2 = new Address_Level2__c();
            al2.Level1_Code__c = 'CN-99';
            al2.Level1_Sys_No__c = '999999';
            al2.Level1_Name__c = '广东省';
            al2.Name = '深圳市';
            al2.Level2_Code__c = 'CN-9999';
            al2.Level2_Sys_No__c = '9999999';
            al2.Address_Level__c = al.id;
            insert al2;
           //  医院
           ControllerUtil.EscapeNFM001Trigger = true;
StaticParameter.EscapeNFM001Trigger = true;
StaticParameter.EscapeNFM001AgencyContractTrigger = true;
StaticParameter.EscapeNFM001AgencyContractTrigger2 = true;
            Account hp = new Account(RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId(), Name = 'hp', OwnerId = thisUser.Id);
            hp.Name = 'test hospital';
            hp.FSE_GI_Main_Leader__c = thisUser.Id;
            hp.FSE_SP_Main_Leader__c = thisUser.Id;
            hp.State_Master__c = al.id;
            hp.City_Master__c = al2.id;
            hp.ET_owner__c = thisUser.Id;
            hp.OCM_man_province_txt__c = '深圳';
            insert hp;
 
           // 战略科室
            Account section = new Account();
           section.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_Class_BF').getRecordTypeId();
           section.Name         = '*';
           section.Department_Class_Label__c = '消化科';
           section.ParentId                  = hp.Id;
           section.Hospital_Department_Class__c = hp.Id;
           upsert section;
           // 科室
           Account depart = new Account();
           depart.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_GI').getRecordTypeId();
           depart.Name         = '*';
           depart.Department_Name__c  = 'NFM007TestDepart';
           depart.ParentId            = section.Id;
           depart.Department_Class__c = section.Id;
           depart.Hospital__c         = hp.Id;
           upsert depart;
 
           // //  RecordType recByoin = [select id from RecordType where IsActive = true and SobjectType= 'Account' and Name='病院' limit 1];
           //  Account hospital = new Account(Name = 'テスト病院');
           //  hospital.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId();
           //  hospital.Name = 'test hospital1';
           //  hospital.ET_owner__c = UserInfo.getUserId();
           //  hospital.State_Master__c = al.id;
           //  hospital.City_Master__c = al2.id;
           //  hospital.OCM_man_province_txt__c = '深圳';
           //  insert hospital;
           //  医院
           String agencyid = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
           Account agency = new Account(RecordTypeId =agencyid , Name = 'hp', OwnerId = thisUser.Id);
           agency.Name = 'test hospital';
           agency.FSE_GI_Main_Leader__c = thisUser.Id;
           agency.FSE_SP_Main_Leader__c = thisUser.Id;
           agency.State_Master__c = al.id;
           agency.City_Master__c = al2.id;
           agency.ET_owner__c = thisUser.Id;
           agency.OCM_man_province_txt__c = '深圳';
           insert agency;
            OCM_Management_Province__c mp1 = new OCM_Management_Province__c();
            mp1.Name = '深圳';
            mp1.Province__c = '广东省';
            mp1.Window1__c = hpOwner.Id;
            mp1.ET_DataWindow__c = hpOwner.Id;
            mp1.ET_BusinessWindow__c = hpOwner.Id;
            mp1.ET_Assistant1__c = hpOwner.Id;
            mp1.ET_Assistant2__c = hpOwner.Id;
            mp1.ENG_DataWindow__c = hpOwner.Id;
            mp1.ENG_BusinessWindow__c = hpOwner.Id;
            mp1.ETShareOpp__c = hpOwner.Id;
            mp1.ENGShareOpp__c = hpOwner.Id;
            mp1.ENG_Assistant1__c = hpOwner.Id;
            mp1.ENG_Assistant2__c = hpOwner.Id;
            insert mp1;
            System.Test.startTest();
        //     Opportunity opp = new Opportunity(
        //        Name = 'test opp1',
        //        StageName = '完了',
        //        CurrencyIsoCode = 'USD',
        //        CloseDate = Date.today().addDays(5),
        //        AccountId = depart.Id,
        //        Department_Class__c = section.Id,
        //        Hospital__c         = hp.Id,
        //        Closing_Bid_Date__c = Date.today(),
        //        //Hospital__c = hp.Id,
        //        Competitor__c = 'A',
        //        SAP_Send_OK__c = false,
        //        Close_Forecasted_Date__c = date.today().addDays(1),
        //        Opportunity_Category__c = 'GI',
        //        Opp_order_Type__c ='ET',
        //        SAP_Province__c = '广东省'
        //    );
           // insert opp;
           // Opportunity opp = new Opportunity();
           // opp.Name='aiueo';
           // opp.StageName='完了';
           // opp.Trade__c = '内貿';
           // opp.CloseDate=Date.today();
           // opp.CurrencyIsoCode = 'CNY';
           // opp.Estimation_List_Price__c = 100;
           // opp.Wholesale_Price__c = 101;
           // opp.Dealer_Final_Price__c = 102;
           // opp.OCM_Agent1_Price__c = 103;
           // opp.Stocking_Price__c = 104;
           // opp.Estimation_No__c = '105';
           // opp.Estimation_Name__c = '106';
           // opp.Estimation_Id__c = '107';
           // opp.Opp_order_Type__c ='ET';
           // opp.SAP_Province__c = '广东省';
           // insert opp;
           Opportunity opp1 = new Opportunity();
           opp1.Name='aiueo';
           opp1.StageName='完了';
           opp1.Trade__c = '内貿';
           opp1.CloseDate=Date.today();
           opp1.CurrencyIsoCode = 'CNY';
           opp1.Estimation_List_Price__c = 100;
           opp1.Wholesale_Price__c = 101;
           opp1.Dealer_Final_Price__c = 102;
           opp1.OCM_Agent1_Price__c = 103;
           opp1.Stocking_Price__c = 104;
           opp1.Estimation_No__c = '105';
           opp1.Estimation_Name__c = '106';
           opp1.Estimation_Id__c = '107';
           opp1.Opp_order_Type__c ='ENG';
           opp1.SAP_Province__c = '广东省';
           opp1.Department_Class__c = section.Id;
           opp1.Hospital__c         = hp.Id;
           opp1.AccountId = depart.Id;
 
           // insert opp1;
           insert new Opportunity[] {opp1};
           // 共享计划创建
           // opp = [select id,OCM_man_province_new__c from  Opportunity where id = :opp.id];
           // System.assertEquals('深圳', opp.OCM_man_province_new__c);
        //    OpportunityShare oppshare = new OpportunityShare();
        //    oppshare.RowCause= 'Manual';//手动
        //    oppshare.OpportunityId = opp.id;
        //    oppshare.UserOrGroupId = hpOwner.Id;
        //    oppshare.OpportunityAccessLevel = 'Read';//编辑
           // insert oppshare;
           OpportunityShare oppshare1 = new OpportunityShare();
           oppshare1.RowCause= 'Manual';//手动
           oppshare1.OpportunityId = opp1.id;
           oppshare1.UserOrGroupId = hpOwner.Id;
           oppshare1.OpportunityAccessLevel = 'Read';//编辑
           // insert oppshare1;
           insert new OpportunityShare[] {oppshare1};
           //TMS数据
        //    Dealer_ConsumableManage__c tms =new Dealer_ConsumableManage__c();
        //    tms.Dealer_Name__c =hp.id;
        //    tms.ET_ENG__c ='ET';
        //    tms.TMS_Key__c='1';
           // insert tms;
       /**   
           Dealer_ConsumableManage__c tms1 =new Dealer_ConsumableManage__c();
           tms1.Dealer_Name__c =agency.id;
           tms1.ET_ENG__c ='ENG';
           tms1.TMS_Key__c='2';
           // insert tms1;
           insert new Dealer_ConsumableManage__c[] {tms1};
           // 共享计划创建
           // tms = [select id,TMS_Province__c  from Dealer_ConsumableManage__c where id = :tms.id];
           // System.assertEquals('深圳', tms.TMS_Province__c);
           Dealer_ConsumableManage__Share tmsshare = new Dealer_ConsumableManage__Share();
           tmsshare.RowCause= 'OCSMToTMSUser__c';//手动
           tmsshare.ParentId = tms1.id;
           tmsshare.UserOrGroupId = hpOwner.Id;
           tmsshare.AccessLevel = 'Read';
           insert tmsshare;
           //IMS数据
           Oly_TriggerHandler.bypass('ConsumableTargetManageHandler');
        //    ConsumableTargetManage__c ims =new ConsumableTargetManage__c();
        //    ims.Hospital__c =agency.id;
        //    ims.productType__c ='ET耗材';
        //    ims.ConsumableTargetManageKey__c ='001';
           // insert ims;
           ConsumableTargetManage__c ims1 =new ConsumableTargetManage__c();
           ims1.Hospital__c =hp.id;
           ims1.productType__c ='外科耗材';
           ims1.ConsumableTargetManageKey__c ='002';
           // insert ims1;
           insert new ConsumableTargetManage__c[] {ims1};
 
       
            // 共享计划创建
        //    ConsumableTargetManage__Share imsshare = new ConsumableTargetManage__Share();
        //    imsshare.RowCause= 'OCSMToIMSUser__c';//手动
        //    imsshare.ParentId = ims2.id;
        //    imsshare.UserOrGroupId = hpOwner.Id;
        //    imsshare.AccessLevel = 'Read';
        //    insert imsshare;
           
           */ 
           OCMManagementProvinceWebService.updateOpportunity();
 
           Dealer_ConsumableManage__c tms1 =new Dealer_ConsumableManage__c();
           tms1.Dealer_Name__c =agency.id;
           tms1.ET_ENG__c ='ENG';
           tms1.TMS_Key__c='1';
           Dealer_ConsumableManage__c tms2 =new Dealer_ConsumableManage__c();
           tms2.Dealer_Name__c =agency.id;
           tms2.ET_ENG__c ='ET';
           tms2.TMS_Key__c='2';
           // insert tms1;
           insert new Dealer_ConsumableManage__c[] {tms1,tms2};
 
           Dealer_ConsumableManage__Share tmsshare = new Dealer_ConsumableManage__Share();
           tmsshare.RowCause= 'OCSMToTMSUser__c';//手动
           tmsshare.ParentId = tms1.id;
           tmsshare.UserOrGroupId = hpOwner.Id;
           tmsshare.AccessLevel = 'Read';
           insert tmsshare;
 
           Database.executeBatch(new OCMTOTMSShareUserBatch(),200);
           Oly_TriggerHandler.bypass('ConsumableTargetManageHandler');
           ConsumableTargetManage__c ims =new ConsumableTargetManage__c();
           ims.Hospital__c =hp.id;
           ims.productType__c ='ET耗材';
           ims.ConsumableTargetManageKey__c ='001';
 
           ConsumableTargetManage__c ims1 =new ConsumableTargetManage__c();
           ims1.Hospital__c =hp.id;
           ims1.productType__c ='外科耗材';
           ims1.ConsumableTargetManageKey__c ='002';
 
           insert new ConsumableTargetManage__c[] {ims,ims1};
           Database.executeBatch(new OCMTOIMSShareUserBatch(),200);
           System.Test.stopTest();
        }
   }
    
}