高章伟
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
@isTest
public class AgencyShareUpdateBatchTest {
 
    public static Account account1;
    public static Account account2;
    public static Contact contact1;
    public static Contact contact2;
    public static Contact contact3;
    public static User user;
    public static User user2;
    public static Agency_Hospital_Link__c agency_hospital_link;
    public static Agency_Opportunity__c agency_opportunity;
    public static Agency_Opportunity__c agency_opportunity2;
    
    // Normal Data
    public static void setupData() {
        // 取引先
        account1 = new Account();
        account1.Name = 'test1医院';
        account1.RecordTypeId = '01210000000QemG';
        account2 = new Account();
        account2.Name = 'test1经销商';
        account2.RecordTypeId = '01210000000Qem1';
        Id AgencyRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
 
        insert new List<Account> {account1, account2};
 
        // 取引先責任者
        contact1 = new Contact();
        contact1.AccountId = account1.Id;
        contact1.FirstName = '責任者';
        contact1.LastName = 'test1医院';
        contact2 = new Contact();
        contact2.AccountId = account2.Id;
        contact2.FirstName = '責任者';
        contact2.LastName = 'test1经销商';
        contact3 = new Contact();
        contact3.AccountId = account2.Id;
        contact3.FirstName = '責任者';
        contact3.LastName = 'test2经销商';
        insert new List<contact> {contact1,contact2,contact3};
 
        // ユーザー
        Profile p = [select Id from Profile where Name = '901_经销商活动系统'];
        user = new User();
        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' + System.now().millisecond();
        user.Alias = 'テユ';
        user.CommunityNickname = 'tu1' + System.now().millisecond();
 
        user2 = new User();
        user2.ProfileId = p.Id;
        user2.ContactId = contact3.Id;
        user2.FirstName = 'ユーザー2';
        user2.LastName = 'テスト2';
        user2.Email = 'test_user2@example.com';
        user2.emailencodingkey='UTF-8';
        user2.languagelocalekey='zh_CN';
        user2.localesidkey='ja_JP';
        user2.timezonesidkey='Asia/Shanghai';
        user2.Username = 'test_user2@example.com' + System.now().millisecond();
        user2.Alias = 'テユ2';
        user2.CommunityNickname = 'tu2' + System.now().millisecond();
 
        insert new List<User> {user, user2};
 
        agency_hospital_link = new Agency_Hospital_Link__c();
        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;
        agency_hospital_link.Agency_Campaign_Obj__c = true;
        insert agency_hospital_link;
 
        ProductTypes__c productTypes = new ProductTypes__c();
        productTypes.Name ='290系统';
        productTypes.Name2__c ='290系统';
        productTypes.Department_Cateogy__c = 'GI;BF;GS';
        insert  productTypes;
 
        Id rtId = [select Id,DeveloperName from RecordType where IsActive = true and SobjectType = 'Agency_Opportunity__c' and DeveloperName = 'Opportunity'].Id;
        // Schema.SobjectType.Agency_Opportunity__c.getRecordTypeInfosByName().get('询价').getRecordTypeId();
 
        agency_opportunity = new Agency_Opportunity__c();
        agency_opportunity.RecordTypeId = rtId;
        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.NewOpportunity_Agency_Apply_Status__c = '批准';
        agency_opportunity.OCM_Change_To_Opportunity_Flg__c = true;
        agency_opportunity.Department_Cateogy__c = 'GI';
        agency_opportunity.Product_Category1_P__c ='290系统';
        insert agency_opportunity;
 
        Opportunity opp = new Opportunity();
        opp.Agency_Opportunity__c = agency_opportunity.Id;
        opp.Name = 'test询价';
        opp.StageName = '目標';
        //opp.AgencyOpp_FromOCM__c = true;
        opp.SAP_Send_OK__c = false;
        opp.CloseDate = Date.newInstance(2222, 7, 15);
        insert opp;
        opp.SAP_Send_OK__c = true;
 
        agency_opportunity2 = new Agency_Opportunity__c();
        agency_opportunity2.RecordTypeId = rtId;
        agency_opportunity2.Name = '引合2';
        agency_opportunity2.Hospital_Target__c = account1.Id;
        agency_opportunity2.Agency__c = account2.Id;
        agency_opportunity2.Agency_Hospital__c = agency_hospital_link.Id;
        //oObj.OCSM_Owner__c != nObj.OCSM_Owner__c
        agency_opportunity2.OwnerId = user2.Id;
        agency_opportunity2.Department_Cateogy__c = 'GI';
        agency_opportunity2.NewOpportunity_Agency_Apply_Status__c = '批准';
        agency_opportunity2.OCM_Change_To_Opportunity_Flg__c = false;
        insert agency_opportunity2;
    }
 
    @isTest
    public static void shareToOSCM_Test() {
        setupData();
        String rowCause = Schema.Agency_Opportunity__Share.RowCause.OCSM_Owner_c_User__c;
        List<Agency_Opportunity__Share> agencyopportunityShare=[SELECT
                    RowCause,ParentId,UserOrGroupId
                FROM Agency_Opportunity__Share
                WHERE ParentId=:agency_opportunity.Id
                  AND RowCause = :rowCause];
 
        System.assertEquals('OCSM_Owner_c_User__c', agencyopportunityShare[0].RowCause);
        System.assertEquals(UserInfo.getUserId(), agencyopportunityShare[0].UserOrGroupId);
 
        // テストの開始
        Test.startTest();
        List<RecordType> deptRId = [select Id, DeveloperName from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_Class_GI'];
        List<Account> GIDept = [select id, OwnerId from Account where Hospital_Department_Class__c = :account1.Id and RecordTypeId = :deptRId[0].Id];
        System.assertEquals(1, GIDept.size());
 
        Profile p2 = [select Id from Profile where Name = '系统管理员'];
        User user3 = new User();
        user3.ProfileId = p2.Id;
        user3.FirstName = 'ユーザー3';
        user3.LastName = 'テスト';
        user3.Email = 'test_user3@example.com';
        user3.emailencodingkey='UTF-8';
        user3.languagelocalekey='zh_CN';
        user3.localesidkey='ja_JP';
        user3.timezonesidkey='Asia/Shanghai';
        user3.Username = 'test_user3@example.com' + System.now().millisecond();
        user3.Alias = 'テユ';
        user3.CommunityNickname = 'tu1' + System.now().millisecond();
        insert user3;
 
        GIDept[0].OwnerId = user3.Id;
        update GIDept;
 
        AgencyShareUpdateBatch mBatch = new AgencyShareUpdateBatch();
        Database.executeBatch(mBatch);
 
        Test.stopTest();
 
        agencyopportunityShare=[SELECT
                    RowCause,ParentId,UserOrGroupId
                FROM Agency_Opportunity__Share
                WHERE ParentId=:agency_opportunity2.Id
                  AND RowCause = :rowCause];
 
        System.assertEquals('OCSM_Owner_c_User__c', agencyopportunityShare[0].RowCause);
        System.assertEquals(user3.Id, agencyopportunityShare[0].UserOrGroupId);
    }
}