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
@isTest
public class RentalAutomaticSortBeforeBatchTest {
 
    private static void setupTestData(){
        User user = new User();
        System.runAs(new User(Id = Userinfo.getUserId())) {
            String flag='NG2';
            String d1='产品试用';
            String dept='医疗华北营业本部';
            user = new User(Test_staff__c = true);
            user.LastName = 'う_サンブリッジ';
            user.FirstName = '';
            user.Alias = 'う';
            user.Email = 'olympusTest03@sunbridge.com';
            user.Username = 'olympusTest03@sunbridge.com';
            user.CommunityNickname = 'う';
            user.IsActive = true;
            user.EmailEncodingKey = 'ISO-2022-JP';
            user.TimeZoneSidKey = 'Asia/Tokyo';
            user.LocaleSidKey = 'ja_JP';
            user.LanguageLocaleKey = 'ja';
            user.ProfileId = System.Label.ProfileId_SystemAdmin;
            user.Province__c = '北京市';
            user.Dept__c = dept;
            user.Job_Category__c = '销售推广';
            user.Use_Start_Date__c = Date.today().addMonths(-6);
            insert user;
        }
        Boolean userProfileCheck = false;
        // 获取从次月14日至再次月13日的日期
        Date today = Date.today();
        // 获取从次月14日至再次月13日的日期
        Date startDate = today.addMonths(1).toStartOfMonth().addDays(13);
        Date endDate = today.addMonths(2).toStartOfMonth().addDays(12);
 
        opp2AuxiliarySort__c oas5 = new opp2AuxiliarySort__c();
        oas5.Name = '1.华北';
        oas5.type__c = 2;
        insert oas5;
        Opportunity opp = new Opportunity(
        Name='Opp',
        StageName='引合',
        CloseDate=Date.today().addDays(10),
        Close_Forecasted_Date__c=Date.today(),
            CurrencyIsoCode = 'CNY'
        );
        insert opp;
        Statu_Achievements__c Sac = new Statu_Achievements__c(
            name = 'zhucan_one',
            Opportunity__c = opp.id,
            DeliveryDate__c = Date.today(),
            ContractNO__c = 'ContractNO1',
            ContractAmount__c = 1000
        );
        insert Sac;
        
        RecordType rectCam =
        [SELECT Id FROM RecordType
         WHERE IsActive = true AND SobjectType = 'Campaign'
                          AND DeveloperName = 'ServiceEngineerTraining'];
        Campaign cam = new Campaign();
        cam.Name = 'cam';
        cam.Name2__c = '1234';
        cam.RecordTypeId = rectCam.Id;
        cam.StartDate = startDate.addDays(10);
        cam.EndDate = startDate.addDays(15);
        cam.Mailflg_after45__c = true;
        cam.Mailflg_cancel__c = true;
        cam.Mailflg_before15__c = true;
        cam.Mailflg_before7__c = true;
        cam.Mailflg_after3__c = true;
        insert cam;
 
 
        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;
 
 
        Account hospital = new Account();
        hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
        hospital.Name = 'test hospital';
        hospital.Is_Active__c = '有効';
        hospital.Attribute_Type__c = '卫生部';
        hospital.Speciality_Type__c = '综合医院';
        hospital.Grade__c = '一级';
        hospital.OCM_Category__c = 'SLTV';
        hospital.Is_Medical__c = '医疗机构';
        hospital.State_Master__c = al.id;
        hospital.City_Master__c = al2.id;
        hospital.Town__c = '东京';
        insert hospital;
        Account[] strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_OTH'];
        Account dep = new Account();
        dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_OTH'].id;
        dep.Name = 'test dep';
        dep.AgentCode_Ext__c = '9999998';
        dep.ParentId = strategicDep[0].Id;
        dep.Department_Class__c = strategicDep[0].Id;
        dep.Hospital__c = hospital.Id;
        insert dep;
 
        Contact contact2 = new Contact();
        contact2.AccountId = dep.Id;
        contact2.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
        contact2.FirstName = '責任者';
        contact2.LastName = 'test1经销商';
        contact2.Agency_User__c = true;
        insert contact2;
        Date nowday = Date.today();
        OPDPlan__c oPDPlan = new OPDPlan__c();
        oPDPlan.Status__c = '计划中';
        oPDPlan.OPDPlan_ImplementDate__c =  startDate;
        oPDPlan.OPDType__c = '询价';
        oPDPlan.Related_Opportunity1_ID__c = opp.Id;
        oPDPlan.NoOpp_Reason__c = null;
        oPDPlan.Campaign__c = cam.Id;
        oPDPlan.PlanProdDetail__c = '1000*1;';
        oPDPlan.OPDLendSort__c = 3;
        oPDPlan.OPDLendSortDraft__c = 3;
        oPDPlan.If_AutoSort__c = 0;
        oPDPlan.SortDate__c = Date.today();
        oPDPlan.SortOperator__c = userInfo.getUserId();
        insert oPDPlan;
 
        OPDPlan__c oPDPlan2 = new OPDPlan__c();
        oPDPlan2.Status__c = '计划中';
        oPDPlan2.OPDPlan_ImplementDate__c =  startDate.addDays(1);
 
        oPDPlan2.OPDType__c = '询价';
        oPDPlan2.Related_Opportunity1_ID__c = opp.Id;
        oPDPlan2.NoOpp_Reason__c = null;
        oPDPlan2.PlanProdDetail__c = '1000*1;';
        oPDPlan2.OPDLendSort__c = 3;
        oPDPlan2.OPDLendSortDraft__c = 3;
        oPDPlan2.If_AutoSort__c = 0;
        oPDPlan2.SortDate__c = Date.today();
        oPDPlan2.SortOperator__c = userInfo.getUserId();
        insert oPDPlan2;
 
        OPDPlan__c oPDPlan3 = new OPDPlan__c();
        oPDPlan3.Status__c = '计划中';
        oPDPlan3.OPDPlan_ImplementDate__c =  startDate;
        oPDPlan3.OPDType__c = '询价';
        oPDPlan3.Related_Opportunity1_ID__c = opp.Id;
        oPDPlan3.NoOpp_Reason__c = null;
        oPDPlan3.PlanProdDetail__c = '1000*1;';
        oPDPlan3.OPDLendSort__c = 3;
        oPDPlan3.OPDLendSortDraft__c = 3;
        oPDPlan3.If_AutoSort__c = 0;
        oPDPlan3.SortDate__c = Date.today();
        oPDPlan3.SortOperator__c = userInfo.getUserId();
 
        insert oPDPlan3;
        Test.StartTest();
        System.runAs(new User(Id = user.id)) {
            Rental_Apply__c raObj = new Rental_Apply__c();
            raObj.Name = 'testra';
            raObj.OPDPlan__c = oPDPlan.Id;
            raObj.Product_category__c = 'GI';
            raObj.Demo_purpose1__c = '产品试用';
            raObj.demo_purpose2__c = FixtureUtil.raDemo_purpose2MAP.get('shiyongwuxunjia');
            raObj.direct_send__c = '医疗机构';
            raObj.Loaner_received_staff__c = '王五';
            raObj.Loaner_received_staff_phone__c = '110';
            raObj.direct_shippment_address__c = '北京市';
            raObj.Hospital__c = hospital.Id;
            raObj.Strategic_dept__c = strategicDep[0].Id;
            raObj.Account__c = dep.Id;
            raObj.Request_shipping_day__c = startDate.addDays(-3);  // 希望到货日
            raObj.Hope_Lonaer_date_Num__c = 1;  // 希望借用天数
            raObj.applyUser__c = user.id;
            raObj.Person_In_Charge__c = user.id;
            raObj.Phone_number__c = '1234567890';
            raObj.Loaner_medical_Staff__c = contact2.Id;
            raObj.Add_Approval_Status__c = '';
            raObj.Request_approval_time__c = Date.toDay(); 
            raObj.Status__c = '草案中';
            raObj.OwnerId = user.id;
            insert raObj;
 
            Rental_Apply__c raObj1 = new Rental_Apply__c();
            raObj1.Name = 'testra';
            raObj1.OPDPlan__c = oPDPlan3.Id;
            raObj1.Product_category__c = 'GI';
            raObj1.Demo_purpose1__c = '产品试用';
            raObj1.demo_purpose2__c = FixtureUtil.raDemo_purpose2MAP.get('shiyongwuxunjia');
            raObj1.direct_send__c = '医疗机构';
            raObj1.Loaner_received_staff__c = '王五';
            raObj1.Loaner_received_staff_phone__c = '110';
            raObj1.direct_shippment_address__c = '北京市';
            raObj1.Hospital__c = hospital.Id;
            raObj1.Strategic_dept__c = strategicDep[0].Id;
            raObj1.Account__c = dep.Id;
            raObj1.Request_shipping_day__c = startDate.addDays(-3);  // 希望到货日
            raObj1.Hope_Lonaer_date_Num__c = 1;  // 希望借用天数
            raObj1.applyUser__c = user.id;
            raObj1.Person_In_Charge__c = user.id;
            raObj1.Phone_number__c = '1234567890';
            raObj1.Loaner_medical_Staff__c = contact2.Id;
            raObj1.Add_Approval_Status__c = '';
            raObj1.Request_approval_time__c = Date.toDay(); 
            raObj1.Status__c = '草案中';
            raObj1.OwnerId = user.id;
            insert raObj1;
 
            Rental_Apply__c raObj2 = new Rental_Apply__c();
            raObj2.Name = 'testra';
            raObj2.OPDPlan__c = oPDPlan2.Id;
            raObj2.Product_category__c = 'GI';
            raObj2.Demo_purpose1__c = '产品试用';
            raObj2.demo_purpose2__c = FixtureUtil.raDemo_purpose2MAP.get('shiyongwuxunjia');
            raObj2.direct_send__c = '医疗机构';
            raObj2.Loaner_received_staff__c = '王五';
            raObj2.Loaner_received_staff_phone__c = '110';
            raObj2.direct_shippment_address__c = '北京市';
            raObj2.Hospital__c = hospital.Id;
            raObj2.Strategic_dept__c = strategicDep[0].Id;
            raObj2.Account__c = dep.Id;
            raObj2.Request_shipping_day__c = startDate.addDays(-3);  // 希望到货日
            raObj2.Hope_Lonaer_date_Num__c = 1;  // 希望借用天数
            raObj2.applyUser__c = user.id;
            raObj2.Person_In_Charge__c = user.id;
            raObj2.Phone_number__c = '1234567890';
            raObj2.Loaner_medical_Staff__c = contact2.Id;
            raObj2.Add_Approval_Status__c = '';
            raObj2.Request_approval_time__c = Date.toDay(); 
            raObj2.Status__c = '草案中';
            raObj2.OwnerId = user.id;
            insert raObj2;
        }
    }
 
    static testMethod void test01() {
        setupTestData();
        // 2024-01-24 ssm 增加排序batch执行
        Database.executeBatch(new RentalAutomaticSortBeforeBatch(),1);
        Database.executeBatch(new RentalAutomaticSortBeforeBatch(true),1);
        Test.stopTest();
    }
}