高章伟
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
@IsTest
private class ET_Agency_Target_Result_Table_BatchTest {
 
    /*******************************************************************************************************
     * @description   testデータ作成        
     */
    private static void setup() {
        // OLY_OCM-644
        StaticParameter.EscapeConsumableOrderDetail2Trigger = true;
 
        //Account 经销商 recordtypeID
        Id AgencyRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
 
        //Account 医院 recordtypeID
        Id HPRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId();
 
        // 省
        Address_Level__c al = new Address_Level__c(Name = '東京', Level1_Code__c = 'CN-99', Level1_Sys_No__c = '999999');
        insert al;
        // 市
        Address_Level2__c al2 = new Address_Level2__c(Level1_Code__c = 'CN-99', Level1_Sys_No__c = '999999', Level1_Name__c = '東京',
                Name = '渋谷区', Level2_Code__c = 'CN-9999', Level2_Sys_No__c = '9999999', Address_Level__c = al.Id);
        insert al2;
 
        Account AccountHP = new Account(Name = 't_e_s_tHP001', AgentCode_Ext__c = '9999900', RecordTypeId = HPRecordTypeId,
                Is_Active__c = '有効', Attribute_Type__c = '卫生部', Speciality_Type__c = '综合医院',
                Grade__c = '一级', OCM_Category__c = 'SLTV', Is_Medical__c = '医疗机构',
                State_Master__c = al.Id, City_Master__c = al2.Id, Town__c = '东京');
        insert AccountHP;
 
        Account AccountAgent1 = new Account(Name = 't_e_s_tAgent001', RecordTypeId = AgencyRecordTypeId);
        insert AccountAgent1;
 
        Account AccountAgent2 = new Account(Name = 't_e_s_tAgent002', RecordTypeId = AgencyRecordTypeId, ParentId = AccountHP.Id);
        insert AccountAgent2;
 
        Product2 prod01 = new Product2(
                Name = 'Test01',
                ProductCode = 'Test01',
                Asset_Model_No__c = 'Test01',
                SFDA_Status__c = '有効',
                Dealer_special_Object__c = true,
                Family = 'ET',
                Category2__c = '耗材',
                Category3__c = 'ERCP',
                Category4__c = '导丝',
                Category5__c = 'Visiglide35'
        );
        Product2 prod02 = new Product2(
                Name = 'T_est002',
                ProductCode = 'T_estCode002',
                Category2__c = '耗材',
                Category3__c = 'ERCP',
                Category4__c = '导丝',
                Category5__c = 'Visiglide35',
                Family = 'ET',
                Packing_list_manual__c = 1,
                Asset_Model_No__c = 'T_est02',
                SFDA_Status__c = '有効',
                Dealer_special_Object__c = true
        );
        Product2 prod03 = new Product2(
                Name = 'T_est003',
                ProductCode = 'T_estCode003',
                Category2__c = '耗材',
                Category3__c = 'Capsule',
                Category4__c = 'CP打印机',
                Category5__c = 'CP打印机',
                Family = 'ET',
                Packing_list_manual__c = 1,
                Asset_Model_No__c = 'T_est03',
                SFDA_Status__c = '有効',
                Dealer_special_Object__c = true
        );
        Product2 prod04 = new Product2(
                Name = 'T_est004',
                ProductCode = 'T_estCode004',
                Category2__c = '耗材',
                Category3__c = '基幹',
                Category4__c = '清洗刷',
                Category5__c = 'DBW',
                Family = 'ET',
                Packing_list_manual__c = 1,
                Asset_Model_No__c = 'T_est04',
                SFDA_Status__c = '有効',
                Dealer_special_Object__c = true
        );
        Product2 prod05 = new Product2(
                Name = 'T_est005',
                ProductCode = 'T_estCode005',
                Category2__c = '耗材',
                Category3__c = '基幹',
                Category4__c = '清洗刷',
                Category5__c = 'DBW',
                Family = 'ET',
                Packing_list_manual__c = 6,
                Asset_Model_No__c = 'T_est05',
                SFDA_Status__c = '有効',
                Dealer_special_Object__c = true
        );
        Product2 prod06 = new Product2(
                Name = 'T_est006',
                ProductCode = 'T_estCode006',
                Category2__c = '耗材',
                Category3__c = '基幹',
                Category4__c = '清洗刷',
                Category5__c = 'DBW',
                Family = 'ET',
                Packing_list_manual__c = 5,
                Asset_Model_No__c = 'T_est06',
                SFDA_Status__c = '有効',
                Dealer_special_Object__c = true
        );
        Product2 [] pd2s = new List<Product2>{
                prod01, prod02, prod03, prod04, prod05, prod06
        };
        insert pd2s;
 
        List<Product2__c> prod2cs = new List<Product2__c>();
        for(Product2 p: pd2s) {
            Product2__c pdc = new Product2__c(
                    Name=p.Name,
                    OT_CODE_Text__c=p.ProductCode,
                    Product2__c = p.Id
            );
            prod2cs.add(pdc);
        }
        insert prod2cs;
 
        System.runAs(new User(Id = Userinfo.getUserId())) {
            // PCL
            Opportunity opp01 = new Opportunity(
                    AccountId = AccountAgent2.Id,
                    Opportunity_Category__c = 'GI',
                    Strategic_department_new__c = '消化科',
                    SAP_Province__c = '辽宁省',
                    Name = 't_e_s_t_opp',
                    Purchase_Reason__c = '新设',
                    Fund_Basis__c = '政府资金',
                    Purchase_Type__c = '一般询价',
                    Sales_Method__c = '院内招标',
                    Trade__c = '内贸',
                    Promise_Class__c = '内贸',
                    Sales_Root__c = '販売店',
                    Close_Forecasted_Date__c = Date.today().addDays(50),
                    CloseDate = Date.today(),
                    Hospital__c = AccountHP.Id,
                    StageName = '引合',
                    SAP_Send_OK__c = true
            );
            insert opp01;
 
            Consumable_order__c [] coList = new List<Consumable_order__c>();
            for(Integer i=0;i<pd2s.size();i++) {
                coList.add(
                        new Consumable_order__c(
                                Name='T_e_s_tOrder'+i,
                                Order_ForHospital__c = AccountHP.Id,
                                Dealer_Info__c = AccountAgent2.Id,
                                Opportunity__c = opp01.Id,
                                SummonsForDirction__c = '直接销售给医院'
                        )
                );
            }
 
            insert coList;
            Consumable_Orderdetails__c [] codList = new List<Consumable_Orderdetails__c>();
            for(Integer i=0;i<coList.size();i++) {
                codList.add(
                        new Consumable_Orderdetails__c(
                                Name='T_e_s_t'+i,
                                Consumable_order__c = coList[i].Id,
                                RecordTypeId = System.Label.RT_ConOrderDetail1_Order,
                                Consumable_count__c = 1,
                                Consumable_product__c = prod2cs[i].Id
                        )
                );
            }
 
            insert codList;
            Consumable_order_details2__c [] cod2List = new List<Consumable_order_details2__c>();
            for(Integer i=0;i<coList.size();i++) {
                cod2List.add(
                        new Consumable_order_details2__c(
                                Name = 'T_e_s_t_cod2_'+i,
                                Consumable_order_minor__c = coList[i].Id,
                                Consumable_Sale_order__c = coList[i].Id,
                                Send_Date__c =  Date.newInstance(Date.today().year(),Date.today().month(),1),
                                Consumable_product__c = prod2cs[i].Id,
                                Asset_Model_No__c = 'T_e_s_t01',
                                Intra_Trade_List_RMB__c = 100 * (i + 1),
                                Box_Piece__c = '盒'
                        )
                );
            }
 
            insert cod2List;
 
            //产品得分表
    //        ET_Product_ScoreTable_Batch bat = new ET_Product_ScoreTable_Batch();
    //        Database.executeBatch(bat);
            Product_Score_Table__c [] pstList = new List<Product_Score_Table__c>();
            Product_Score_Table__c pst01 = new Product_Score_Table__c(
            OCM_Term__c = '154P',
            Plan_Category3__c = 'ERCP',
            Category3__c = 'ERCP',
            Category4__c = '导丝');
            pstList.add(pst01);
            Product_Score_Table__c pst02 = new Product_Score_Table__c(
                    OCM_Term__c = '154P',
                    Plan_Category3__c = 'Capsule',
                    Category3__c = 'Capsule',
                    Category4__c = 'CP打印机'
                    );
            pstList.add(pst02);
 
            Product_Score_Table__c pst03 = new Product_Score_Table__c(
                    OCM_Term__c = '154P',
                    Plan_Category3__c = 'Capsule',
                    Category3__c = '基幹',
                    Category4__c = '清洗刷'
            );
            pstList.add(pst03);
 
            for(Product_Score_Table__c pst: pstList) {
                pst.UniqueKey__c = pst.OCM_Term__c+':'+pst.Category3__c+':'+pst.Category4__c;
            }
            insert pstList;
 
        }
 
    }
 
    /*
     * @description   特約店实绩
     */
    private static testMethod void ET_Agency_Target_Result_Table_Batch01() {
        setup();
 
        Account AccountHP = [SELECT Id, Name, Management_Code__c FROM Account WHERE Name = 't_e_s_tHP001'];
        Account AccountAgent2 = [SELECT Id,Name FROM Account WHERE Name = 't_e_s_tAgent002'];
 
        Test.startTest();
        ET_Agency_Target_Result_Table_Batch bat = new ET_Agency_Target_Result_Table_Batch ();
        Id JobId = Database.executeBatch(bat);
        Test.stopTest();
//        Product_Score_Table_History__c psth = [SELECT Shipping_date__c,Qty__c,UniqueKey__c FROM Product_Score_Table_History__c WHERE UniqueKey__c='H:150P:T_e_s_t_HP001:ERCP:导丝'];
        Product_Score_Table_History__c [] psthList = [SELECT Id,Shipping_date__c,Sales_Amount__c,Qty__c,UniqueKey__c,Product_Score_Table__r.UniqueKey__c FROM Product_Score_Table_History__c];
        System.assertNotEquals('000000000000000',String.valueOf(JobId));
        String uniqueKey_1 = 'A:154P:' + Date.today().month() + ':' + AccountHP.Management_Code__c + ':' + AccountAgent2.Id + ':ERCP:导丝'; 
        String uniqueKey_2 = 'A:154P:' + Date.today().month() + ':' + AccountHP.Management_Code__c + ':' + AccountAgent2.Id + ':基幹:清洗刷';
        String uniqueKey_3 = 'A:154P:' + Date.today().month() + ':' + AccountHP.Management_Code__c + ':' + AccountAgent2.Id + ':Capsule:CP打印机';
        System.assertEquals(3, psthList.size());
        for(Product_Score_Table_History__c psth: psthList) {
            if (psth.UniqueKey__c == uniqueKey_1) {
                System.assertEquals(265.49,psth.Sales_Amount__c);
                System.assertEquals(2,psth.Qty__c);
                System.assertEquals('154P:ERCP:导丝',psth.Product_Score_Table__r.UniqueKey__c);
                System.assertEquals(psth.Shipping_date__c,Date.newInstance(Date.today().year(),Date.today().month(),1));
            } else if (psth.UniqueKey__c == uniqueKey_2) {
                System.assertEquals(1327.43,psth.Sales_Amount__c);
                System.assertEquals(3,psth.Qty__c);
                System.assertEquals('154P:基幹:清洗刷',psth.Product_Score_Table__r.UniqueKey__c);
                System.assertEquals(psth.Shipping_date__c,Date.newInstance(Date.today().year(),Date.today().month(),1));
            } else if (psth.UniqueKey__c == uniqueKey_3) {
                System.assertEquals(265.49,psth.Sales_Amount__c);
                System.assertEquals(1,psth.Qty__c);
                System.assertEquals('154P:Capsule:CP打印机',psth.Product_Score_Table__r.UniqueKey__c);
                System.assertEquals(psth.Shipping_date__c,Date.newInstance(Date.today().year(),Date.today().month(),1));
            }
            
        }
 
    }
    /*******************************************************************************************************
    *@description 添加额外数据数据
    */
    private static testMethod void ET_Agency_Target_Result_Table_Batch02() {
        setup();
 
        Test.startTest();
        Account AccountHP = [SELECT Id, Name, Management_Code__c FROM Account WHERE Name = 't_e_s_tHP001'];
        Account AccountAgent2 = [SELECT Id,Name FROM Account WHERE Name = 't_e_s_tAgent002'];
 
        Id PSTH_RecordTypeId = Schema.SObjectType.Product_Score_Table_History__c.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
 
        //历史资料
        Product2 p = [SELECT Plan_Term__c,Category3__c,Category4__c,CreatedDate FROM Product2 WHERE Name = 'Test01'];
        String month = String.valueOf(p.CreatedDate.month());
        String term = p.Plan_Term__c.left(4);
        String uniqueKey = 'A:' + term + ':' + month + ':' + AccountHP.Management_Code__c + ':' + AccountAgent2.Id + ':' + p.Category3__c + ':' + p.Category4__c;
        String pstKey = term + ':' + p.Category3__c + ':' + p.Category4__c;
        Product_Score_Table__c pst = [SELECT Id FROM Product_Score_Table__c WHERE UniqueKey__c = :pstKey];
        Product_Score_Table_History__c psth01 = new Product_Score_Table_History__c();
        //UniqueKey__c :  (151PA/151PB)计画财年:第三分类 :新4.5分类 :经销商 :医院
        psth01.UniqueKey__c = uniqueKey;
        psth01.Hospital__c = AccountHP.Id;
        psth01.Product_Score_Table__c = pst.Id;
        psth01.Sales_Amount__c = 67;
        psth01.Shipping_date__c = Date.newInstance(Date.today().year(), Date.today().month(), 1);
        psth01.Agency__c = AccountAgent2.Id;
        psth01.Qty__c = 2;
        psth01.RecordTypeId = PSTH_RecordTypeId;
        insert psth01;
        
 
        //call ET_Product_ScoreTableHistory_Batch
        ET_Agency_Target_Result_Table_Batch bat = new ET_Agency_Target_Result_Table_Batch ();
        Database.executeBatch(bat);
 
        Test.stopTest();
 
        System.runAs(new User(Id = Userinfo.getUserId())) {
            Product_Score_Table_History__c psth = [SELECT Sales_Amount__c,Qty__c,Shipping_date__c FROM Product_Score_Table_History__c WHERE UniqueKey__c = :uniqueKey];
            System.assertEquals(265.49, psth.Sales_Amount__c);
            System.assertEquals(2, psth.Qty__c);
            Date today = Date.today();
            System.assertEquals(Date.newInstance(today.year(), today.month(), 1), psth.Shipping_date__c);
 
        }
        
    }
}