高章伟
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
@isTest
private class UpdateAgencyOppProductTargetBatchTest {
 
    public static Account account1 = new Account();
    public static Account account2 = new Account();
    public static Contact contact1 = new Contact();
    public static Contact contact2 = new Contact();
    public static User user = new User();
    public static Agency_Hospital_Link__c agency_hospital_link = new Agency_Hospital_Link__c();
    public static Agency_Contact__c agency_contact = new Agency_Contact__c();
    public static ProductTypes__c product_types = new ProductTypes__c();
    public static ProductTypes__c product_types2 = new ProductTypes__c();
    public static ProductTypes__c product_types3 = new ProductTypes__c();
    public static OlympusCalendar__c olympus_calendar = new OlympusCalendar__c();
    
    public static String report_id;
    public static String report_header_id;
 
    private static void testInit() {
        // 取引先
        account1.Name = 'test1医院';
        account1.RecordTypeId = '01210000000QemG';
        insert account1;
        
        account2.Name = 'test1经销商';
        account2.RecordTypeId = '01210000000Qem1';
        insert account2;
        
        // 取引先責任者
        contact1.AccountId = account1.Id;
        contact1.FirstName = '責任者';
        contact1.LastName = 'test1医院';
        insert contact1;
        
        contact2.AccountId = account2.Id;
        contact2.FirstName = '責任者';
        contact2.LastName = 'test1经销商';
        insert contact2;
        
        // ユーザー
        Profile p = [select Id from Profile where Name = '901_经销商周报'];
        user.ProfileId = p.Id;
        user.ContactId = contact2.Id;
        user.FirstName = 'ユーザー';
        user.LastName = 'テスト';
        user.Email = 'test_user@example.com';
        user.emailencodingkey='UTF-8';
        user.languagelocalekey='zh_CN';
        user.localesidkey='ja_JP';
        user.timezonesidkey='Asia/Shanghai';
        user.Username = 'test_user@example.com';
        user.Alias = 'テユ';
        user.CommunityNickname = 'テストユーザー';
        user.Batch_User__c = true;
        insert user;
        
        // 代理店医院
        agency_hospital_link.Name = 'test1代理店医院';
        agency_hospital_link.Hospital__c = account1.Id;
        agency_hospital_link.Agency__c = account2.Id;
        agency_hospital_link.OwnerId = user.Id;
        insert agency_hospital_link;
        
        // 先生
        agency_contact.Name = 'test1医院先生';
        agency_contact.Doctor_Division1__c = '院长';
        agency_contact.Type__c = '医生';
        agency_contact.Agency_Hospital__c = agency_hospital_link.Id;
        agency_contact.OwnerId = user.Id;
        insert agency_contact;
        
        // 製品区分
        product_types.Name = '製品区分1';
        product_types.Name2__c = '製品区分1';
        product_types.OwnerId = user.Id;
        product_types.Department_Cateogy__c = 'GI;ET;BF;GS;URO;GYN;ENT;OTH';
        product_types.DeleteFlg__c = false;
        insert product_types;
 
        product_types2.Name = '製品区分2';
        product_types2.Name2__c = '製品区分2';
        product_types2.OwnerId = user.Id;
        product_types2.Department_Cateogy__c = 'GI;ET;BF;GS;URO;GYN;ENT;OTH';
        product_types2.DeleteFlg__c = false;
        insert product_types2;
 
        product_types3.Name = '製品区分3';
        product_types3.Name2__c = '製品区分3';
        product_types3.OwnerId = user.Id;
        product_types3.Department_Cateogy__c = 'GI;ET;BF;GS;URO;GYN;ENT;OTH';
        product_types3.DeleteFlg__c = false;
        insert product_types3;
 
        // 引合
        Agency_Opportunity__c agency_opportunity = new Agency_Opportunity__c();
        agency_opportunity.RecordTypeId = [select Id,DeveloperName from RecordType where IsActive = true and SobjectType = 'Agency_Opportunity__c' and DeveloperName = 'Opportunity'].Id;
        agency_opportunity.Name = '引合1';
        agency_opportunity.Hospital_Target__c = account1.Id;
        agency_opportunity.Agency__c = account2.Id;
        agency_opportunity.Agency_Hospital__c = agency_hospital_link.Id;
        agency_opportunity.StageName__c = '还没申请预算';
        agency_opportunity.OwnerId = user.Id;
        agency_opportunity.Department_Cateogy__c = 'GI';
        agency_opportunity.Product_Category1__c = product_types2.Id;
        agency_opportunity.Product_Category2__c = product_types.Id;
        agency_opportunity.Close_Forecasted_Date__c = Date.today();
        insert agency_opportunity;
 
        agency_opportunity = new Agency_Opportunity__c();
        agency_opportunity.RecordTypeId = [select Id,DeveloperName from RecordType where IsActive = true and SobjectType = 'Agency_Opportunity__c' and DeveloperName = 'Opportunity'].Id;
        agency_opportunity.Name = '引合2';
        agency_opportunity.Hospital_Target__c = account1.Id;
        agency_opportunity.Agency__c = account2.Id;
        agency_opportunity.Agency_Hospital__c = agency_hospital_link.Id;
        agency_opportunity.StageName__c = '还没申请预算';
        agency_opportunity.OwnerId = user.Id;
        agency_opportunity.Department_Cateogy__c = 'GI';
        agency_opportunity.Product_Category1__c = product_types.Id;
        agency_opportunity.Product_Category2__c = product_types2.Id;
        agency_opportunity.Product_Category3__c = product_types3.Id;
        agency_opportunity.Close_Forecasted_Date__c = Date.today();
        insert agency_opportunity;
 
        agency_opportunity = new Agency_Opportunity__c();
        agency_opportunity.RecordTypeId = [select Id,DeveloperName from RecordType where IsActive = true and SobjectType = 'Agency_Opportunity__c' and DeveloperName = 'Opportunity'].Id;
        agency_opportunity.Name = '引合3';
        agency_opportunity.Hospital_Target__c = account1.Id;
        agency_opportunity.Agency__c = account2.Id;
        agency_opportunity.Agency_Hospital__c = agency_hospital_link.Id;
        agency_opportunity.StageName__c = '还没申请预算';
        agency_opportunity.OwnerId = user.Id;
        agency_opportunity.Department_Cateogy__c = 'GI';
        agency_opportunity.Product_Category1__c = product_types.Id;
        agency_opportunity.Close_Forecasted_Date__c = Date.today();
        insert agency_opportunity;
        
        // オリンパスカレンダー
        olympus_calendar.Date__c = Date.valueOf('2017-04-10');
        olympus_calendar.OwnerId = user.Id;
        insert olympus_calendar;
    }
    
    @isTest static void test_method_one() {
        testInit();
        System.runAs(user) {
            // 病院リスト取得
            CreateTargetCmp.createSFTarget(agency_hospital_link.Id, false, new String[]{product_types.Id,product_types2.Id,product_types3.Id}, new String[]{product_types2.Id,'',''}, 
                    new String[]{product_types3.Id,'',''}, new String[]{product_types.Id,'',''}, new String[]{product_types.Id,'',''}, new String[]{product_types.Id,'',''}, new String[]{product_types.Id,'',''}, new String[]{product_types.Id,'',''}, '150P');
        
            System.Test.StartTest();
            UpdateAgencyOppProductTargetBatch b1 = new UpdateAgencyOppProductTargetBatch();
            Database.executeBatch(b1, 100);
            System.Test.StopTest();
        }
        
        String rid = [select Id,DeveloperName from RecordType where IsActive = true and SobjectType = 'Agency_Opportunity__c' and DeveloperName = 'Target'].Id;
        Agency_Opportunity__c target = [select Id,Product_Category_Opp_Count__c from Agency_Opportunity__c where RecordTypeId =:rid and Department_Cateogy__c = 'GI' and Product_Category__c =:product_types.Id];
        System.assertEquals(3, target.Product_Category_Opp_Count__c);
 
        target = [select Id,Product_Category_Opp_Count__c from Agency_Opportunity__c where RecordTypeId =:rid and Department_Cateogy__c = 'OTH'];
        System.assertEquals(null, target.Product_Category_Opp_Count__c);
 
        target = [select Id,Product_Category_Opp_Count__c,Product_Category__c from Agency_Opportunity__c where RecordTypeId =:rid and Department_Cateogy__c = 'GI' and Product_Category__c =:product_types2.Id];
        System.assertEquals(2, target.Product_Category_Opp_Count__c);
 
        target = [select Id,Product_Category_Opp_Count__c,Product_Category__c from Agency_Opportunity__c where RecordTypeId =:rid and Department_Cateogy__c = 'GI' and Product_Category__c =:product_types3.Id];
        System.assertEquals(1, target.Product_Category_Opp_Count__c);
 
        
    }
    
}