高章伟
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
@isTest
private class TargetCustomerAssignmentBatchTest {
    //SWAG-BHP42V    精琢技术  20191209 START 
    static testMethod void UnitTest_01() {
        //医院 记录类型
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'];
        if (rectHp.size() == 0) {
            return;
        }
        //科室 记录类型
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_GI'];
        if (rectDpt.size() == 0) {
            return;
        }
 
        //简档
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
 
        //用户
        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', FirstName = 'owner', 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);
        insert hpOwner;
        User hpOwner2 = new User(Test_staff__c = true, LastName = 'hp2', FirstName = 'owner', Alias = 'hp2', Work_Location__c = '重庆', CommunityNickname = 'hpOwner2', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner2@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner2;
 
 
        //医院
        Account hp = new Account(RecordTypeId = rectHp[0].Id, Name = 'hp', OwnerId = hpOwner.Id);
        hp.FSE_GI_Main_Leader__c = hpOwner.Id;
        hp.FSE_SP_Main_Leader__c = hpOwner2.Id;
        insert hp;
 
        //战略科室
        Account dc = [select Id, Name, RecordType_DeveloperName__c, Account2__c from Account where ParentId = :hp.Id and RecordType_DeveloperName__c = 'Department_Class_GI'];
        //科室
        Account dpt = new Account(RecordTypeId = rectDpt[0].Id);
        dpt.Name         = '*';
        dpt.Department_Name__c  = 'TestDepart';
        dpt.ParentId            = dc.Id;
        dpt.Department_Class__c = dc.Id;
        dpt.Hospital__c         = hp.Id;
        insert dpt;
 
        //OPD计划 
        OPDPlan__c tempOPDPlan = new OPDPlan__c();
        tempOPDPlan.ThisMonthPlan__c = 1;
        tempOPDPlan.Status__c = '计划中';
        tempOPDPlan.Account_Laboratory__c =dpt.Id;
        tempOPDPlan.OPD_Customers_Target__c = null;
        //opd计划实施日期为今天
        tempOPDPlan.OPDPlan_ImplementDate__c = Date.today();
        tempOPDPlan.NoOpp_Reason__c = 'HCP对应';
        insert tempOPDPlan;
 
        //根据opd计划实施日期 算出本财年
        Date dateToday = Date.today();
        Integer year = dateToday.year();
        Integer month = dateToday.month();
        if (month < 4) {
            year -= 1;
        }
        String selectedTab = String.valueOf(year - 1867 - 1 + 'P'); 
 
        //客户-目标客户 默认让opd计划和客户-目标客户的财年相同
        Account_Number_of_target__c anot = new Account_Number_of_target__c();
        anot.Account__c = dc.Id;
        anot.Account_HP__c = hp.Id;
        anot.OCM_Period__c = selectedTab;
        anot.Target_amount_170_190__c =36597;
        insert anot;
        
        //测试
        System.Test.StartTest();
        Id execBTId = Database.executeBatch(new TargetCustomerAssignmentBatch(), 10);
        System.Test.StopTest();
    }
 
    //测试 传入一个id 指定更新某一条 batch
    static testMethod void UnitTest_02() {
        //医院 记录类型
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'];
        if (rectHp.size() == 0) {
            return;
        }
        // 科室 记录类型
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_GI'];
        if (rectDpt.size() == 0) {
            return;
        }
 
        //简档
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
 
        //用户
        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', FirstName = 'owner', 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);
        insert hpOwner;
        User hpOwner2 = new User(Test_staff__c = true, LastName = 'hp2', FirstName = 'owner', Alias = 'hp2', Work_Location__c = '重庆', CommunityNickname = 'hpOwner2', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner2@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner2;
 
 
        //医院
        Account hp = new Account(RecordTypeId = rectHp[0].Id, Name = 'hp', OwnerId = hpOwner.Id);
        hp.FSE_GI_Main_Leader__c = hpOwner.Id;
        hp.FSE_SP_Main_Leader__c = hpOwner2.Id;
        insert hp;
 
        //战略科室
        Account dc = [select Id, Name, RecordType_DeveloperName__c, Account2__c from Account where ParentId = :hp.Id and RecordType_DeveloperName__c = 'Department_Class_GI'];
 
        //科室
        Account dpt = new Account(RecordTypeId = rectDpt[0].Id);
        dpt.Name         = '*';
        dpt.Department_Name__c  = 'TestDepart';
        dpt.ParentId            = dc.Id;
        dpt.Department_Class__c = dc.Id;
        dpt.Hospital__c         = hp.Id;
        insert dpt;
 
        //opd计划
        OPDPlan__c tempOPDPlan = new OPDPlan__c();
        tempOPDPlan.ThisMonthPlan__c = 1;
        tempOPDPlan.Status__c = '计划中';
        tempOPDPlan.Account_Laboratory__c =dpt.Id;
        tempOPDPlan.OPD_Customers_Target__c = null;
        tempOPDPlan.OPDPlan_ImplementDate__c = Date.today();
        tempOPDPlan.NoOpp_Reason__c = 'HCP对应';
        insert tempOPDPlan;
 
        //opd计划实施日期为今天
        Date dateToday = Date.today();
        Integer year = dateToday.year();
        Integer month = dateToday.month();
        if (month < 4) {
            year -= 1;
        }
        String selectedTab = String.valueOf(year - 1867 - 1 + 'P'); 
 
        //客户-目标客户
        Account_Number_of_target__c anot = new Account_Number_of_target__c();
        anot.Account__c = dc.Id;
        anot.Account_HP__c = hp.Id;
        anot.OCM_Period__c = selectedTab;
        anot.Target_amount_170_190__c =36597;
        insert anot;
        
        //测试
        System.Test.StartTest();
        Id execBTId = Database.executeBatch(new TargetCustomerAssignmentBatch(tempOPDPlan.Id), 10);
        System.Test.StopTest();
    }
    //测试传入一批Id 批量更新batch
    static testMethod void UnitTest_03() {
        //医院 记录类型
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'];
        if (rectHp.size() == 0) {
            return;
        }
        //科室 记录类型
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_GI'];
        if (rectDpt.size() == 0) {
            return;
        }
 
        //简档
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
 
        //用户
        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', FirstName = 'owner', 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);
        insert hpOwner;
        User hpOwner2 = new User(Test_staff__c = true, LastName = 'hp2', FirstName = 'owner', Alias = 'hp2', Work_Location__c = '重庆', CommunityNickname = 'hpOwner2', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner2@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner2;
 
 
        //医院
        Account hp = new Account(RecordTypeId = rectHp[0].Id, Name = 'hp', OwnerId = hpOwner.Id);
        hp.FSE_GI_Main_Leader__c = hpOwner.Id;
        hp.FSE_SP_Main_Leader__c = hpOwner2.Id;
        insert hp;
 
        //战略科室
        Account dc = [select Id, Name, RecordType_DeveloperName__c, Account2__c from Account where ParentId = :hp.Id and RecordType_DeveloperName__c = 'Department_Class_GI'];
 
        //科室1
        Account dpt = new Account(RecordTypeId = rectDpt[0].Id);
        dpt.Name         = '*';
        dpt.Department_Name__c  = 'TestDepart';
        dpt.ParentId            = dc.Id;
        dpt.Department_Class__c = dc.Id;
        dpt.Hospital__c         = hp.Id;
        insert dpt;
 
        //科室2
        Account dpt1 = new Account(RecordTypeId = rectDpt[0].Id);
        dpt1.Name         = '*';
        dpt1.Department_Name__c  = 'TestDepart1';
        dpt1.ParentId            = dc.Id;
        dpt1.Department_Class__c = dc.Id;
        dpt1.Hospital__c         = hp.Id;
        insert dpt1;
 
        //opd计划1
        OPDPlan__c tempOPDPlan1 = new OPDPlan__c();
        tempOPDPlan1.ThisMonthPlan__c = 1;
        tempOPDPlan1.Status__c = '计划中';
        tempOPDPlan1.Account_Laboratory__c =dpt.Id;
        tempOPDPlan1.OPD_Customers_Target__c = null;
        tempOPDPlan1.OPDPlan_ImplementDate__c = Date.today();
        tempOPDPlan1.NoOpp_Reason__c = 'HCP对应';
        insert tempOPDPlan1;
 
        //opd计划2
        OPDPlan__c tempOPDPlan2 = new OPDPlan__c();
        tempOPDPlan2.ThisMonthPlan__c = 1;
        tempOPDPlan2.Status__c = '计划中';
        tempOPDPlan2.Account_Laboratory__c =dpt1.Id;
        tempOPDPlan2.OPD_Customers_Target__c = null;
        tempOPDPlan2.OPDPlan_ImplementDate__c = Date.today();
        tempOPDPlan2.NoOpp_Reason__c = 'HCP对应';
        insert tempOPDPlan2;
 
        //opd计划实施日期为今天
        Date dateToday = Date.today();
        Integer year = dateToday.year();
        Integer month = dateToday.month();
        if (month < 4) {
            year -= 1;
        }
        String selectedTab = String.valueOf(year - 1867 - 1 + 'P'); 
 
        //客户-目标客户1
        Account_Number_of_target__c anot = new Account_Number_of_target__c();
        anot.Account__c = dc.Id;
        anot.Account_HP__c = hp.Id;
        anot.OCM_Period__c = selectedTab;
        anot.Target_amount_170_190__c =36597;
        insert anot;
 
        //客户-目标客户2
        Account_Number_of_target__c anot1 = new Account_Number_of_target__c();
        anot1.Account__c = dc.Id;
        anot1.Account_HP__c = hp.Id;
        anot1.OCM_Period__c = selectedTab;
        anot1.Target_amount_170_190__c =36500;
        insert anot1;
 
        //批量传入id
        List<OPDPlan__c> opdList = [select Id from OPDPlan__c where OPD_Customers_Target__c = null and OCM_category_ID__c  != null];
        List<String> opdIdList = new List<String>();
        for( OPDPlan__c op : opdList){
            opdIdList.add(op.Id);
        }
 
        //测试
        System.Test.StartTest();
        Id execBTId = Database.executeBatch(new TargetCustomerAssignmentBatch(opdIdList), 10);
        System.Test.StopTest();
    }
    //SWAG-BHP42V    精琢技术  20191209 END
}