高章伟
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
@isTest
private class SetPersonalTargetBatchTest {
    //20200618  为SetPersonalTargetBatch添加整个test class   byljh
    static private String currentPeriod(Integer i) {
        Date dateNow = Date.today();
        Integer year = dateNow.year();
        Integer month = dateNow.month();
        if (month < 4) {
            year -= 1;
        }
        return String.valueOf(year + i - 1867 + 'P');
    }
 
    static private Date oppCloseDate() {
        Date nowDate = Date.today();
        Integer year = nowDate.year();
        Integer month = nowDate.month();
        if (month < 4) {
            nowDate = nowDate.addYears(-1);
        }
        return nowDate;
    }
    // 主流程
    @isTest
    static void myUnitTest() {
        // システム管理者
        User u3 = new User(Test_staff__c = true);
        u3.LastName = '_サンブリッジ';
        u3.FirstName = 'う';
        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 = System.Label.ProfileId_SystemAdmin;
        u3.Job_Category__c = '销售推广';
        u3.Province__c = '上海市上海市';
        u3.Use_Start_Date__c = Date.today().addMonths(-6);
        insert u3;
 
        System.runAs(u3) {
            Oly_TriggerHandler.bypass('PowerBIBaseHandler');
            Product2 product = new Product2(
                Name = 'テスト商品',
                Important_Rroduct_1GI__c = true,
                Important_Rroduct_2GI__c = true,
                Important_Rroduct_3GI__c = true,
                Important_Rroduct_4GI__c = true,
                Important_Rroduct_5GI__c = true
            );
            insert product;
            Pricebook2 pricebook = ControllerUtil.getStandardPricebook();
            PricebookEntry entry = new PricebookEntry( Pricebook2Id = pricebook.Id, Product2Id = product.Id);
            entry.UnitPrice = 0;
            entry.IsActive = true;
            entry.UseStandardPrice = false;
            entry.CurrencyIsoCode = 'CNY';
            insert entry;
 
            RecordType[] rt = [select Id from RecordType where SobjectType = 'Opportunity' and IsActive = true and DeveloperName = 'Target'];
            Profile p = [select id from Profile where id = :System.Label.ProfileId_SystemAdmin];
 
            Map<String, List<Double>> proportion = new Map<String, List<Double>>();
            String strObjectiveProportionGI = System.Label.ObjectiveProportionGI;
            List<String> objectiveProportionGI = strObjectiveProportionGI.split(',');
            List<Double> doubleGI = new List<Double>();
            for (String strGI : objectiveProportionGI) {
                doubleGI.add(Double.valueOf(strGI));
            }
            proportion.put('GI', doubleGI);
 
            User u1 = new User(Test_staff__c = false);
            u1.LastName = '_サンブリッジ7';
            u1.FirstName = 'あ';
            u1.Alias = 'あ9';
            u1.Email = 'olympusTest01@sunbridge.com';
            u1.Username = 'olympusTest91@sunbridge.com';
            u1.CommunityNickname = 'あ9';
            u1.IsActive = true;
            u1.EmailEncodingKey = 'ISO-2022-JP';
            u1.TimeZoneSidKey = 'Asia/Tokyo';
            u1.LocaleSidKey = 'ja_JP';
            u1.LanguageLocaleKey = 'ja';
            u1.ProfileId = System.Label.ProfileId_SystemAdmin;
            u1.Job_Category__c = '销售推广';
            u1.Province__c = '上海市上海市';
            u1.Post__c = '一般';
            u1.Sales_Speciality__c = '产品担当';
            u1.Use_Start_Date__c = Date.today().addMonths(-24);
            insert u1;
 
            User u2 = new User(Test_staff__c = false);
            u2.LastName = '_サンブリッジ6';
            u2.FirstName = 'い';
            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 = System.Label.ProfileId_SystemAdmin;
            u2.Job_Category__c = '销售推广';
            u2.Province__c = '上海市上海市';
            u2.Post__c = '一般';
            u2.Sales_Speciality__c = '产品担当';
            u2.Use_Start_Date__c = Date.today().addMonths(-24);
            insert u2;
 
            // 插入 中间表 Num_Major_Product 的两个数据
            list<Amount_Major_Product__c> TestData = new list<Amount_Major_Product__c>();
            Date dateNow = Date.today();
            Integer year = dateNow.year();
 
            Amount_Major_Product__c testdatum1 = new Amount_Major_Product__c();
            string key = u1.Id + '_' + 'GI';
            testdatum1.key__c = key;
            testdatum1.Amount__c = 100.00;
            testdatum1.user_Alias__c = u1.Alias;
            testdatum1.Use_Start_Date__c = u1.Use_Start_Date__c;
            testdatum1.SAP_Province__c = u1.Province__c;
            testdatum1.iYear__c = year;
            testdatum1.Is_Processing__c = true;
            TestData.add(testdatum1);
            insert(TestData);
 
            TestData = [select id from Amount_Major_Product__c];
 
 
            System.Test.StartTest();
            Id execBTId = Database.executeBatch(new SetPersonalTargetBatch(), 10);
            System.Test.StopTest();
 
            // 设置既存opportunities数据
 
            Integer y = year;
            Integer m = 4 ;
            String syear = String.valueOf(y);
            String smonth = String.valueOf(m);
            if (m < 10) {
                smonth = '0' + smonth;
            }
            String sTagetDay = syear + '-' + smonth + '-01';
            Date tagetDay = Date.valueOf(sTagetDay);
            list<Opportunity> Opportunities = new list<Opportunity>();
            Opportunity newopp = new Opportunity();
            newopp.Name = u2.Alias + ' ' + '重点产品01' ;
            newopp.StageName = '目標';
            newopp.OwnerId = u2.Id;
            // トリガをスルーのため、ここでやります
            newopp.Owner_System__c = u2.Id;
            newopp.Opportunity_Category__c = 'GI';
            String tem_depName = ('GI' == null || 'GI' == '') ? 'GI' : 'GI';
            newopp.Proportion__c = proportion.get(tem_depName)[0];
            newopp.CloseDate = tagetDay;
            newopp.Amount = 100.00;
            newopp.Target_category__c = '担当重点产品目标';
            newopp.Important_Key_product_category__c = '重点产品01';
            newopp.SAP_Province__c = u2.Province__c;
            newopp.RecordTypeId = rt[0].ID;
            newopp.OCM_Target_period__c = currentPeriod(0);
            upsert newopp;
 
            Amount_Major_Product__c testdatum2 = new Amount_Major_Product__c();
            key = u2.Id + '_' +  'GI';
            testdatum2.key__c = key;
            testdatum2.Amount__c = 333.00;
            testdatum2.user_Alias__c = u2.Alias;
            testdatum2.Use_Start_Date__c = u2.Use_Start_Date__c;
            testdatum2.SAP_Province__c = u2.Province__c;
            testdatum2.iYear__c = year;
            testdatum2.Is_Processing__c = true;
            insert(testdatum2);
            execBTId = Database.executeBatch(new SetPersonalTargetBatch(), 10);
 
            Opportunities = [select Name, StageName, Owner_System__c, Opportunity_Category__c, Important_Key_product_category__c
                             from Opportunity];
            system.assertEquals(13, Opportunities.size());
 
        }
    }
}