@isTest
public class AgencyOpportunityHandlerTest {

    public static Account account1 = new Account();
    public static Account account2 = new Account();
    public static Account account3 = new Account();
    public static Contact contact1 = new Contact();
    public static Contact contact2 = new Contact();
    public static Contact contact3 = new Contact();
    public static User user = new User();
    public static User user2 = new User();
    public static Agency_Hospital_Link__c agency_hospital_link = new Agency_Hospital_Link__c();
    public static ProductTypes__c product_types = new ProductTypes__c();
    public static Agency_Opportunity__c agency_opportunity = new Agency_Opportunity__c();
    public static Agency_Opportunity__c agency_opportunity2 = new Agency_Opportunity__c();
    public static OlympusCalendar__c olympus_calendar = new OlympusCalendar__c();
    public static String report_id;
    public static String report_header_id;
    // 病院。コンタクト、ユーザー、カレンダーなどの初期データ作る
    public static Account hospital;
    public static Account agency1;
    public static Account agency2;
    public static Account agency3;
    public static String oldAgencyCode;
    public static String newAgencyCode;
    public static String oldAgencyId15;
    public static String newAgencyId15;

    public static Contact contact4;
    public static Contact contact5;
    public static User user1;
    public static User user3;
    public static Agency_Hospital_Link__c a1HPLink;
    public static Agency_Hospital_Link__c a2HPLink;
    public static Agency_Hospital_Link__c a3HPLink;
    
    private static void testInit() {
        // 取引先

        account1.Name = 'test1医院';
        account1.RecordTypeId = '01210000000QemG';
        insert account1;
        List<Account> acclist=[select RecordTypeId,Name 
                                from Account
                                ];
        

        
        account2.Name = 'test1经销商';
        account2.RecordTypeId = '01210000000Qem1';
        insert account2;

        List<Account> acc2list=[select RecordTypeId,Name 
                                from Account
                                ];

        System.assertEquals(10,acc2list.size());
        

        // 取引先責任者
        contact1.AccountId = account1.Id;
        contact1.FirstName = '責任者';
        contact1.LastName = 'test1医院';
        insert contact1;
        List<Contact> contactlist=[select Id,FirstName 
                                from Contact
                                ];

        System.assertEquals('責任者',contactlist[0].FirstName);

        contact2.AccountId = account2.Id;
        contact2.FirstName = '責任者';
        contact2.LastName = 'test1经销商';
        insert contact2;
        
        List<Contact> contact2list=[select Id,FirstName 
                                    from Contact
                                    ];

        System.assertEquals('責任者',contact2list[0].FirstName);


        // ユーザー
        //Profile p = [select Id from Profile where Name = '901_经销商周报'];
        Profile p = [select Id from Profile where Name = '901_经销商活动系统'];
        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 = 'tu' + System.now().millisecond();
        insert user;
        List<user> users = [select Id,Name,LastName, FirstName from User where LastName='テスト'];
        //System.assertEquals('テスト',users[0].LastName);
        System.assertEquals('ユーザー',users[0].FirstName );


        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;

        List<Agency_Hospital_Link__c> agencylist=[select Id,Name from Agency_Hospital_Link__c where Id =: agency_hospital_link.Id];
        //System.assertEquals(null, agencylist.size());
        System.assertEquals('test1医院',agencylist[0].Name);

        //Id rtId = [select Id,DeveloperName from RecordType where IsActive = true and SobjectType = 'Agency_Opportunity__c' and DeveloperName = 'Opportunity'].Id;
        Id rtId = Schema.SObjectType.Agency_Opportunity__c.getRecordTypeInfosByDeveloperName().get('Opportunity').getRecordTypeId();
        // Schema.SobjectType.Agency_Opportunity__c.getRecordTypeInfosByName().get('询价').getRecordTypeId();
        agency_opportunity.Agency_Person__c = contactlist[0].id;
        agency_opportunity.RecordTypeId = rtId;
        agency_opportunity.Name = '引合';
        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.Department_Cateogy__c = 'ET';

        agency_opportunity.Sales_Manager__c = user.Id;
        insert agency_opportunity;


        List<Agency_Opportunity__c> opportunitylist=[select Id,Name from Agency_Opportunity__c];

        System.assertEquals(1,opportunitylist.size());

        //rtId = [select Id,DeveloperName from RecordType where IsActive = true and SobjectType = 'Agency_Opportunity__c' and DeveloperName = 'Target'].Id;
        rtId = Schema.SObjectType.Agency_Opportunity__c.getRecordTypeInfosByDeveloperName().get('Opportunity').getRecordTypeId();
        //Schema.SobjectType.Agency_Opportunity__c.getRecordTypeInfosByName().get('目标').getRecordTypeId();
        agency_opportunity2.Agency_Person__c = contact2list[0].id;
        agency_opportunity2.RecordTypeId = rtId;
        agency_opportunity2.Name = '引合2';
        agency_opportunity2.Hospital_Target__c = account1.Id;
        agency_opportunity2.Agency__c = account2.Id;
        agency_opportunity2.OwnerId = user.Id;
        agency_opportunity2.NewOpportunity_Agency_Apply_Status__c = '草案中';
        agency_opportunity.Department_Cateogy__c = 'GS';

        insert agency_opportunity2;

        System.debug(agency_opportunity2.Name);

        List<Agency_Opportunity__c> opportunitylist2=[select Id,Name from Agency_Opportunity__c];

        System.assertEquals(2,opportunitylist2.size());

        olympus_calendar.Date__c = Date.valueOf('2017-04-10');
        olympus_calendar.OwnerId = user.Id;
        insert olympus_calendar;

        List<OlympusCalendar__c> olympusCalendarlist=[select Id,Name,Date__c from OlympusCalendar__c];
        System.assertEquals(Date.valueOf('2017-04-10'),olympusCalendarlist[0].Date__c);

    }
    
    // @isTest
    // public static void selectOpportunityByIdAndHospitalLinkIdTest() {
    //     Test.startTest();
    //    testInit();
    //     Test.stopTest();
    //    System.runAs(user) {
    //    List<Agency_Opportunity__c> opportunity_data = WeeklyReportCmp.selectOpportunityByIdAndHospitalLinkId(agency_opportunity.Id, agency_hospital_link.Id);
    
    //       List<Agency_Opportunity__c> agencyopportunity=[select Id,Name,Agency__c,Agency_ID__c,Owner_Agency_ID__c,
    //                                                     Agency_Hospital_All__c,Agency_Hospital__c,Agency_Hospital_Target__c
    //                                                     from Agency_Opportunity__c ];


    //      System.assertEquals('::'+agency_opportunity.Name, agencyopportunity[0].Name);

    //      System.assertEquals(agency_opportunity.Agency__c, agencyopportunity[0].Owner_Agency_ID__c);
        
    //      System.assertEquals(agency_opportunity.Agency_Hospital_All__c, agencyopportunity[0].Agency_Hospital_Target__c);

    //      //System.assertEquals(agency_opportunity2.Agency_Hospital_All__c, agencyopportunity[0].Agency_Hospital__c);

    //      //System.assertEquals(agency_opportunity2.Agency_ID__c, String.valueOf(agencyopportunity[0].Owner_Agency_ID__c).substring(0,15));

    //    }
    // }

    @isTest static void test_setAgency_Contact_Share_ToRole() {
        Oly_TriggerHandler.bypass('AgencyReportHeaderHandler');
        Oly_TriggerHandler.bypass('AgencyHospitalHandler');
        //MergeAgencyActivityBatchTest.makeNormalData(true);
        makeNormalData(true);

        // assert
        List<Agency_Opportunity__c> tList = [SELECT Name, NewOpportunity_Agency_Apply_Status__c, Agency__c
                FROM Agency_Opportunity__c ORDER BY Id LIMIT 2];
        System.assertEquals(2, tList.size());
        System.assertEquals(agency1.Id, tList[0].Agency__c);
        System.assertEquals(agency1.Id, tList[1].Agency__c);

        tList[0].NewOpportunity_Agency_Apply_Status__c = '填写完毕';
        tList[1].NewOpportunity_Agency_Apply_Status__c = '填写完毕';
        update tList;

        tList[0].NewOpportunity_Agency_Apply_Status__c = '批准';
        tList[1].NewOpportunity_Agency_Apply_Status__c = '批准';
        update tList;

        // List<Agency_Opportunity__Share> tsList = [SELECT Id
        //         FROM Agency_Opportunity__Share WHERE ParentId = :tList AND RowCause = 'Manual'];
        // System.assertEquals(2, tsList.size());

        Test.startTest();
        Delete tList[0];
        Test.stopTest();
        // tsList = [SELECT Id
        //         FROM Agency_Opportunity__Share WHERE ParentId = :tList AND RowCause = 'Manual'];
        // System.assertEquals(1, tsList.size());

        UnDelete tList[0];
        // tsList = [SELECT Id
        //         FROM Agency_Opportunity__Share WHERE ParentId = :tList AND RowCause = 'Manual'];
        // System.assertEquals(2, tsList.size());
    }

    //agency_opportunityのinsert
    @isTest
    public static void tst_shareToOSCM() {
        // 取引先
        
        account1.Name = 'test1医院';
        account1.RecordTypeId = '01210000000QemG';
        account2.Name = 'test1经销商';
        account2.RecordTypeId = '01210000000Qem1';
        Id AgencyRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
        account3.Name = 'test2经销商';
        account3.RecordTypeId = AgencyRecordTypeId;

        insert new List<Account> {account1, account2, account3};

        // 取引先責任者
        contact1.AccountId = account1.Id;
        contact1.FirstName = '責任者';
        contact1.LastName = 'test1医院';
        contact2.AccountId = account2.Id;
        contact2.FirstName = '責任者';
        contact2.LastName = 'test1经销商';
        contact3.AccountId = account2.Id;
        contact3.FirstName = '責任者';
        contact3.LastName = 'test2经销商';
        insert new List<contact> {contact1,contact2,contact3};

        Test.startTest();
        // ユーザー
        //Profile p = [select Id from Profile where Name = '901_经销商周报'];
        Profile p = [select Id from Profile where Name = '901_经销商活动系统'];
        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.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.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;

        List<Contact> contactlist=[select Id,FirstName from Contact];

        //Id rtId = [select Id,DeveloperName from RecordType where IsActive = true and SobjectType = 'Agency_Opportunity__c' and DeveloperName = 'Opportunity'].Id;
        Id rtId = Schema.SObjectType.Agency_Opportunity__c.getRecordTypeInfosByDeveloperName().get('Opportunity').getRecordTypeId();
        // Schema.SobjectType.Agency_Opportunity__c.getRecordTypeInfosByName().get('询价').getRecordTypeId();
        agency_opportunity.Agency_Person__c = contactlist[0].id;
        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系统';
        //agency_opportunity.Product_Category2_P__c ='170系统';
        //agency_opportunity.Product_Category3_P__c ='170系统';

        agency_opportunity.Sales_Manager__c = user.Id;
        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.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.NewOpportunity_Agency_Apply_Status__c = '批准';
        agency_opportunity2.OCM_Change_To_Opportunity_Flg__c = false;
        insert agency_opportunity2;

        Test.stopTest();
        Opportunity opp2 = new Opportunity();
        opp2.Agency_Opportunity__c = agency_opportunity2.Id;
        opp2.Name = 'test询价2';
        opp2.StageName = '目標2';
        opp2.SAP_Send_OK__c = false;
        opp2.CloseDate = Date.newInstance(2222, 7, 15);
        opp2.SAP_Send_OK__c = true;
        
        // Test.startTest();
        insert opp2;
        // Test.stopTest();

        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);

        agencyopportunityShare=[SELECT
                    RowCause,ParentId,UserOrGroupId
                FROM Agency_Opportunity__Share
                WHERE ParentId=:agency_opportunity.Id and RowCause <>:rowCause];
        System.assertEquals(user.Id, agencyopportunityShare[0].UserOrGroupId);
    }

    // 经销商所有者テスト
    @isTest
    public static void tst_Owner1() {

        // 取引先
        // 
        StaticParameter.EscapeAccountTrigger = true;
        StaticParameter.EscapeNFM007Trigger = true;
        StaticParameter.EscapeOpportunityBefUpdTrigger = true;
        StaticParameter.EscapeOpportunityHpDeptUpdTrigger = true;
        StaticParameter.EscapeSyncOpportunityTrigger = true;

        account1.Name = 'test1医院';
        account1.RecordTypeId = '01210000000QemG';
        account2.Name = 'test1经销商';
        account2.RecordTypeId = '01210000000Qem1';
        Id AgencyRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
        account3.Name = 'test2经销商';
        account3.RecordTypeId = AgencyRecordTypeId;

        insert new List<Account> {account1, account2, account3};

        // 取引先責任者
        contact1.AccountId = account1.Id;
        contact1.FirstName = '責任者';
        contact1.LastName = 'test1医院';
        contact2.AccountId = account2.Id;
        contact2.FirstName = '責任者';
        contact2.Agency_User__c = true;
        contact2.LastName = 'test1经销商';
        contact3.AccountId = account2.Id;
        contact3.FirstName = '責任者';
        contact3.LastName = 'test2经销商';
        insert new List<contact> {contact1,contact2,contact3};
        //insert new List<contact> {contact1,contact2};


        // ユーザー
        //Profile p = [select Id from Profile where Name = '901_经销商周报'];
        Profile p = [select Id from Profile where Name = '901_经销商活动系统'];
        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.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.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;

        List<Contact> contactlist=[select Id,FirstName from Contact];

        //Id rtId = [select Id,DeveloperName from RecordType where IsActive = true and SobjectType = 'Agency_Opportunity__c' and DeveloperName = 'Opportunity'].Id;
        Id rtId = Schema.SObjectType.Agency_Opportunity__c.getRecordTypeInfosByDeveloperName().get('Opportunity').getRecordTypeId();
        // Schema.SobjectType.Agency_Opportunity__c.getRecordTypeInfosByName().get('询价').getRecordTypeId();
        agency_opportunity.Agency_Person__c = contactlist[0].id;
        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系统';
        //agency_opportunity.Product_Category2_P__c ='170系统';
        //agency_opportunity.Product_Category3_P__c ='170系统';

        agency_opportunity.Sales_Manager__c = user.Id;
        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);
        opp.OwnerId = user.Id;
        insert opp;

        List<Opportunity> oppList = [SELECT Id,
                                            OwnerId
                                       FROM Opportunity];
        System.assertEquals(1, oppList.size());
        System.assertEquals(user.Id, oppList[0].OwnerId);

        List<User> owners = [select Id from User where AccountId =:account2.Id and isActive = true and Contact.Agency_User__c = true order by CreatedDate limit 1];
        System.assertEquals(1, owners.size());

        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;
        agency_opportunity2.NewOpportunity_Agency_Apply_Status__c = '批准';
        agency_opportunity2.OCM_Change_To_Opportunity_Flg__c = false;
        insert agency_opportunity2;


        List<Agency_Opportunity__c> aoList = [SELECT Id,
                                                     OwnerId,
                                                     Sales_Manager_F__c,
                                                     Sales_Manager__c
                                                FROM Agency_Opportunity__c
                                               WHERE Id = :agency_opportunity2.Id];
        List<User> userList = [SELECT Id,
                                      SalesManager__c
                                 FROM User
                                WHERE Id = :UserInfo.getUserId()];
        System.assertEquals(1, aoList.size());
        System.assertEquals(UserInfo.getUserId(), aoList[0].OwnerId);
        System.assertEquals(userList[0].SalesManager__c, aoList[0].Sales_Manager_F__c);
        //System.assertEquals(aoList[0].Sales_Manager_F__c, aoList[0].Sales_Manager__c);
    }

   // 普通所有者テスト
    @isTest
    public static void tst_Owner2() {

        // 取引先

        account1.Name = 'test1医院';
        account1.RecordTypeId = '01210000000QemG';
        account2.Name = 'test1经销商';
        account2.RecordTypeId = '01210000000Qem1';
        Id AgencyRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
        account3.Name = 'test2经销商';
        account3.RecordTypeId = AgencyRecordTypeId;

        insert new List<Account> {account1, account2, account3};

        // 取引先責任者
        contact1.AccountId = account1.Id;
        contact1.FirstName = '責任者';
        contact1.LastName = 'test1医院';
        contact2.AccountId = account2.Id;
        contact2.FirstName = '責任者';
        contact2.LastName = 'test1经销商';
        contact2.Agency_User__c = true;
        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_经销商周报'];
        Profile p = [select Id from Profile where Name = '901_经销商活动系统'];
        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.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();

        // ユーザー
        User user3 = new User();
        user3.ProfileId = UserInfo.getProfileId();
        user3.FirstName = 'ユーザー3';
        user3.LastName = 'テスト3';
        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 = 'テユ4';
        user3.CommunityNickname = 'tu3' + System.now().millisecond();

        User user4 = new User();
        user4.ProfileId = UserInfo.getProfileId();
        user4.FirstName = 'ユーザー4';
        user4.LastName = 'テスト4';
        user4.Email = 'test_user4@example.com';
        user4.emailencodingkey='UTF-8';
        user4.languagelocalekey='zh_CN';
        user4.localesidkey='ja_JP';
        user4.timezonesidkey='Asia/Shanghai';
        user4.Username = 'test_user4@example.com' + System.now().millisecond();
        user4.Alias = 'テユ4';
        user4.CommunityNickname = 'tu4' + System.now().millisecond();

        insert new List<User> {user, user2, user3, user4};
        user3.SalesManager__c = user4.Id;
        update user3;

        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;
        Id rtId = Schema.SObjectType.Agency_Opportunity__c.getRecordTypeInfosByDeveloperName().get('Opportunity').getRecordTypeId();
        // Schema.SobjectType.Agency_Opportunity__c.getRecordTypeInfosByName().get('询价').getRecordTypeId();

        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);
        opp.OwnerId = user3.Id;
        insert opp;

        List<Opportunity> oppList = [SELECT Id,
                                            OwnerId
                                       FROM Opportunity];

        List<Contact> contactlist=[select Id,FirstName from Contact];
        System.assertEquals(1, oppList.size());
        System.assertEquals(user3.Id, oppList[0].OwnerId);
        agency_opportunity.Agency_Person__c = contactlist[0].id;
        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 = user4.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系统';
        agency_opportunity.Change_To_Opportunity__c = opp.Id;
        //agency_opportunity.Product_Category2_P__c ='170系统';
        //agency_opportunity.Product_Category3_P__c ='170系统';
        agency_opportunity.Agency_ID__c = '0011000000V9RIs';
        //agency_opportunity.Agency_Hospital_Target__c = agency_hospital_link.Id;

        agency_opportunity.Sales_Manager__c = user.Id;
        insert agency_opportunity;

        List<Agency_Opportunity__c> aoList = [SELECT Id,
                                                     OwnerId,
                                                     Sales_Manager_F__c,
                                                     Sales_Manager__c
                                                FROM Agency_Opportunity__c];
        List<User> userList = [SELECT Id,
                                      SalesManager__c
                                 FROM User
                                WHERE Id = :user3.Id];
        System.assertEquals(1, aoList.size());
        System.assertEquals(userList[0].Id, aoList[0].OwnerId);
        System.assertEquals(userList[0].SalesManager__c, aoList[0].Sales_Manager_F__c);
        //System.assertEquals(aoList[0].Sales_Manager_F__c, aoList[0].Sales_Manager__c);
    }

    public static void makeNormalData(boolean withAgencyDetail) {
        ControllerUtil.EscapeNFM001Trigger = true;
        ControllerUtil.EscapeMaintenanceContractAfterUpdateTrigger = true;
        StaticParameter.EscapeNFM001Trigger = true;
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        StaticParameter.EscapeNFM001AgencyContractTrigger2 = true;
        StaticParameter.EscapeMaintenanceContractAfterUpdateTrigger = true;
        StaticParameter.EscapeAccountTrigger = true;
        StaticParameter.EscapeNFM007Trigger = true;
        StaticParameter.EscapeOpportunityBefUpdTrigger = true;
        StaticParameter.EscapeOpportunityHpDeptUpdTrigger = true;
        StaticParameter.EscapeSyncOpportunityTrigger = true;
        System.runAs(new User(Id = UserInfo.getUserId())) {
            // create 病院
            hospital = new Account();
            hospital.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId();
            hospital.Name = 'test hospital_1';
            // create 经销商
            agency1 = new Account();
            agency1.Name = 'test1经销商';
            agency1.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            agency2 = new Account();
            agency2.Name = 'test2经销商';
            agency2.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            agency3 = new Account();
            agency3.Name = 'test3经销商';
            agency3.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            insert new List<Account> {hospital, agency1, agency2,agency3};
            List<Account> agencyList = [SELECT Name, Management_Code__c
                    FROM Account WHERE (Id = :agency1.Id OR Id = :agency2.Id)
                    ORDER BY ID];
            oldAgencyCode = agencyList[0].Management_Code__c;
            oldAgencyId15 = ('' + agencyList[0].Id).mid(0, 15);
            newAgencyCode = agencyList[1].Management_Code__c;
            newAgencyId15 = ('' + agencyList[1].Id).mid(0, 15);

            // 取引先責任者
            contact4 = new Contact();
            contact4.AccountId = agency1.Id;
            contact4.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            contact4.FirstName = '責任者';
            contact4.LastName = 'test1经销商';
            contact4.Agency_User__c = true;
            Contact contact12 = new Contact();
            contact12.AccountId = agency1.Id;
            contact12.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            contact12.FirstName = '社員2';
            contact12.LastName = 'test1经销商';
            contact12.Agency_User__c = true;
            contact5 = new Contact();
            contact5.AccountId = agency2.Id;
            contact5.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            contact5.FirstName = '責任者';
            contact5.LastName = 'test2经销商';
            contact5.Agency_User__c = true;
            Contact contact22 = new Contact();
            contact22.AccountId = agency2.Id;
            contact22.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            contact22.FirstName = '社員2';
            contact22.LastName = 'test1经销商';
            contact22.Agency_User__c = true;
            Contact contact23 = new Contact();
            contact23.AccountId = agency2.Id;
            contact23.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            contact23.FirstName = '社員3';
            contact23.LastName = 'test2经销商';
            contact23.Agency_User__c = true;

            Contact contact3 = new Contact();
            contact3.AccountId = agency3.Id;
            contact3.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            contact3.FirstName = '社員3';
            contact3.LastName = 'test2经销商';
            contact3.Agency_User__c = true;
            insert new List<Contact> {contact4, contact12, contact5, contact22, contact23,contact3};
            // User (link to ContactId)
            Profile p = [SELECT Id FROM Profile WHERE Name = '901_经销商活动系统'];
            user1 = new User();
            user1.ProfileId = p.Id;
            user1.ContactId = contact4.Id;
            user1.FirstName = 'ユーザー';
            user1.LastName = 'テスト';
            user1.Email = 'test_user1@example.com';
            user1.Username = 'test_user1@sb1.example.com' + System.now().millisecond();
            user1.Alias = 'テユa1';
            user1.CommunityNickname = 'tu1' + System.now().millisecond();
            user1.PortalRole = 'Worker';
            user1.emailencodingkey='UTF-8';
            user1.languagelocalekey='zh_CN';
            user1.localesidkey='ja_JP';
            user1.timezonesidkey='Asia/Shanghai';
            user2 = new User();
            user2.ProfileId = p.Id;
            user2.ContactId = contact5.Id;
            user2.FirstName = 'ユーザー';
            user2.LastName = 'テスト';
            user2.Email = 'test_user2@example.com';
            user2.Username = 'test_user2@sb1.example.com' + System.now().millisecond();
            user2.Alias = 'テユa2';
            user2.CommunityNickname = 'tu2' + System.now().millisecond();
            user2.PortalRole = 'Worker';
            user2.emailencodingkey='UTF-8';
            user2.languagelocalekey='zh_CN';
            user2.localesidkey='ja_JP';
            user2.timezonesidkey='Asia/Shanghai';

            user3 = new User();
            user3.ProfileId = p.Id;
            user3.ContactId = contact3.Id;
            user3.FirstName = 'ユーザー';
            user3.LastName = 'テスト';
            user3.Email = 'test_user3@example.com';
            user3.Username = 'test_user3@sb1.example.com' + System.now().millisecond();
            user3.Alias = 'テユa3';
            user3.CommunityNickname = 'tu3' + System.now().millisecond();
            user3.PortalRole = 'Worker';
            user3.emailencodingkey='UTF-8';
            user3.languagelocalekey='zh_CN';
            user3.localesidkey='ja_JP';
            user3.timezonesidkey='Asia/Shanghai';
            insert new List<User> {user1, user2,user3};
            user2 = [SELECT FirstName, LastName, ContactId, Contact.AccountId, UserRoleId FROM User WHERE Id = :user2.Id];
            System.assertEquals(agency2.Id, user2.Contact.AccountId);
            UserRole rolesAcc = [SELECT Id, Name, PortalAccountId, PortalRole
                    FROM UserRole
                    WHERE PortalAccountId = :agency2.Id
                    AND PortalRole = 'Worker'];
            UserRole rolesUser = [SELECT Id, Name, PortalAccountId, PortalRole
                    FROM UserRole
                    WHERE Id = :user2.UserRoleId];
            System.assertEquals(rolesAcc.PortalAccountId, rolesUser.PortalAccountId);
            System.assertEquals(rolesAcc.PortalRole, rolesUser.PortalRole);
            System.assertEquals(rolesAcc.Name, rolesUser.Name);
            System.assertEquals(rolesAcc.Id, rolesUser.Id);
        }
        System.runAs(new User(Id = UserInfo.getUserId())) {
            // UserにPermissionSetを設定
            PermissionSet ps = [SELECT ID FROM PermissionSet WHERE Name = 'P002_Partner'];
            insert new List<PermissionSetAssignment> {
                new PermissionSetAssignment(AssigneeId = user1.id, PermissionSetId = ps.Id),
                new PermissionSetAssignment(AssigneeId = user2.id, PermissionSetId = ps.Id)
            };
        }

        System.runAs(new User(Id = UserInfo.getUserId())) {
            // 营销关系
            a1HPLink = new Agency_Hospital_Link__c();
            a1HPLink.Name = 'test1经销商医院';
            a1HPLink.Hospital__c = hospital.Id;
            a1HPLink.Agency__c = agency1.Id;
            a1HPLink.Agency_Campaign_Obj__c = true;
            insert a1HPLink;
            a2HPLink = new Agency_Hospital_Link__c();
            a2HPLink.Name = 'test2经销商医院';
            a2HPLink.Hospital__c = hospital.Id;
            a2HPLink.Agency__c = agency2.Id;
            a2HPLink.Agency_Campaign_Obj__c = true;
            insert a2HPLink;

            a3HPLink = new Agency_Hospital_Link__c();
            a3HPLink.Name = 'test2经销商医院01';
            a3HPLink.Hospital__c = hospital.Id;
            a3HPLink.Agency__c = agency3.Id;
            a3HPLink.Agency_Campaign_Obj__c = true;
            insert a3HPLink;

            a3HPLink.Agency_Campaign_Obj__c = false;
            update a3HPLink; 

            if (withAgencyDetail) {
                //.客户人员 (agency1)
                Agency_Contact__c aContact1 = new Agency_Contact__c();
                aContact1.Agency_Hospital__c = a1HPLink.Id;
                aContact1.Type__c = '医生';
                aContact1.Name = 'testAgency医生';
                aContact1.OwnerId = user1.Id;
                Agency_Contact__c aContact2 = new Agency_Contact__c();
                aContact2.Agency_Hospital__c = a1HPLink.Id;
                aContact2.Type__c = '护士';
                aContact2.Name = 'testAgency护士';
                aContact2.OwnerId = user1.Id;
                insert new List<Agency_Contact__c> {aContact1, aContact2};

                // 经销商询价 (agency1)
                Agency_Opportunity__c agency_opportunity1 = new Agency_Opportunity__c();
                agency_opportunity1.RecordTypeId = Schema.SObjectType.Agency_Opportunity__c.getRecordTypeInfosByDeveloperName().get('Opportunity').getRecordTypeId();
                agency_opportunity1.Name = '引合1';
                agency_opportunity1.Hospital_Target__c = hospital.Id;
                agency_opportunity1.Agency__c = agency1.Id;
                agency_opportunity1.Agency_Hospital__c = a1HPLink.Id;
                agency_opportunity1.StageName__c = '还没申请预算';
                agency_opportunity1.NewOpportunity_Agency_Apply_Status__c = '批准';
                agency_opportunity1.OwnerId = user1.Id;
                Agency_Opportunity__c agency_opportunity2 = new Agency_Opportunity__c();
                agency_opportunity2.RecordTypeId = Schema.SObjectType.Agency_Opportunity__c.getRecordTypeInfosByDeveloperName().get('Opportunity').getRecordTypeId();
                agency_opportunity2.Name = '引合2';
                agency_opportunity2.Hospital_Target__c = hospital.Id;
                agency_opportunity2.Agency__c = agency1.Id;
                agency_opportunity2.Agency_Hospital__c = a1HPLink.Id;
                agency_opportunity2.StageName__c = '还没申请预算';
                agency_opportunity2.NewOpportunity_Agency_Apply_Status__c = '批准';
                agency_opportunity2.OwnerId = user1.Id;
                insert new List<Agency_Opportunity__c> {agency_opportunity1, agency_opportunity2};
            }
        }
    }
}