@isTest
private class TenderOpportunityLinkHandlerTest {
    static testMethod void testInsertAndDelete(){
        ControllerUtil.EscapeNFM001Trigger = true;

        Profile p = [select id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        String loginId = UserInfo.getUserId();
        User sys = [select id from User where Id = :loginId];
        User u1 = new User(Test_staff__c = true);
        u1.LastName = '123';
        u1.FirstName = '2';
        u1.Batch_User__c = true;
        u1.Alias = '2';
        u1.Email = 'shashiming@prec-tech.com';
        u1.Username = 'test_user1@olympus.com.cn.tenopplink';
        u1.CommunityNickname = 'あ1';
        u1.IsActive = true;
        u1.EmailEncodingKey = 'ISO-2022-JP';
        u1.TimeZoneSidKey = 'Asia/Tokyo';
        u1.LocaleSidKey = 'ja_JP';
        u1.LanguageLocaleKey = 'ja';
        u1.ProfileId = p.id;
        u1.Job_Category__c = '销售服务';
        u1.Province__c = '東京';
        insert u1;

        User u2 = new User(Test_staff__c = true);
        u2.LastName = '_サンブリッジ';
        u2.FirstName = 'い';
        u2.Batch_User__c = true;
        u2.Alias = 'い';
        u2.Email = 'shashiming@prec-tech.com';
        u2.Username = 'test_user2@olympus.com.cn.tenopplink';
        u2.CommunityNickname = 'い';
        u2.IsActive = true;
        u2.EmailEncodingKey = 'ISO-2022-JP';
        u2.TimeZoneSidKey = 'Asia/Tokyo';
        u2.LocaleSidKey = 'ja_JP';
        u2.LanguageLocaleKey = 'ja';
        u2.ProfileId = p.id;
        u2.Job_Category__c = '销售推广';
        u2.Province__c = '東京';
        u2.IsActive = true;
        insert u2;
/**
        User u3 = new User(Test_staff__c = true);
        u3.LastName = '1222';
        u3.FirstName = 'い';
        u3.Batch_User__c = true;
        u3.Alias = 'い';
        u3.Email = 'shashiming@prec-tech.com';
        u3.Username = 'test_user3@olympus.com.cn.tenopplink';
        u3.CommunityNickname = '33';
        u3.IsActive = true;
        u3.EmailEncodingKey = 'ISO-2022-JP';
        u3.TimeZoneSidKey = 'Asia/Tokyo';
        u3.LocaleSidKey = 'ja_JP';
        u3.LanguageLocaleKey = 'ja';
        u3.ProfileId = p.id;
        u3.Job_Category__c = '销售推广';
        u3.Province__c = '東京';
        u3.IsActive = true;
        insert u3;

        User u4 = new User(Test_staff__c = true);
        u4.LastName = '244';
        u4.FirstName = 'い';
        u4.Batch_User__c = true;
        u4.Alias = 'い';
        u4.Email = 'shashiming@prec-tech.com';
        u4.Username = 'test_user4@olympus.com.cn.tenopplink';
        u4.CommunityNickname = '444';
        u4.IsActive = true;
        u4.EmailEncodingKey = 'ISO-2022-JP';
        u4.TimeZoneSidKey = 'Asia/Tokyo';
        u4.LocaleSidKey = 'ja_JP';
        u4.LanguageLocaleKey = 'ja';
        u4.ProfileId = p.id;
        u4.Job_Category__c = '销售推广';
        u4.Province__c = '東京';
        u4.IsActive = true;
        insert u4;

        User u5 = new User(Test_staff__c = true);
        u5.LastName = 'test00001';
        u5.FirstName = 'qbcuy';
        u5.Batch_User__c = true;
        u5.Alias = '12cs';
        u5.Email = 'shashiming@prec-tech.com';
        u5.Username = 'test_user5@olympus.com.cn.tenopplink';
        u5.CommunityNickname = 'fewfa';
        u5.IsActive = true;
        u5.EmailEncodingKey = 'ISO-2022-JP';
        u5.TimeZoneSidKey = 'Asia/Tokyo';
        u5.LocaleSidKey = 'ja_JP';
        u5.LanguageLocaleKey = 'ja';
        u5.ProfileId = p.id;
        u5.Job_Category__c = '销售推广';
        u5.Province__c = '東京';
        u5.IsActive = true;
        insert u5;
**/
        OCM_Management_Province__c mp1 = new OCM_Management_Province__c();
        mp1.Name = '北京';
        mp1.GI_assistant__c = u1.Id;
        mp1.SP_assistant__c = u2.Id;
        //mp1.Window2__c = u3.Id;
        //mp1.Admin_assistant3__c = u4.Id;
        insert mp1;

        //创建一个医院
        // 省
        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 = '东京';
        hospital.GI_Main__c = u1.Id;
        hospital.BF_owner__c = u2.Id;
        hospital.ET_owner__c = u2.Id;
        //hospital.SP_Main__c = u3.Id;
        //hospital.URO_owner_ID__c = u4.Id;
        //hospital.GYN_owner__c = u1.Id;
        //hospital.ENT_owner_ID__c = u1.Id;
        insert hospital;

        // テストデータ
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (rectSct.size() == 0) {
            return;
        }
        Account section = new Account();
        section.RecordTypeId = rectSct[0].Id;
        section.Name         = '*';
        section.Department_Class_Label__c = '消化科';
        section.ParentId                  = hospital.Id;
        section.Hospital_Department_Class__c = hospital.Id;
        upsert section;

        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            return;
        }
        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = 'NFM007TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = hospital.Id;
        upsert depart;
        
        // 插入询价
        Opportunity opp1 = new Opportunity(
            Name = 'test opp1',
            StageName = '完了',
            CurrencyIsoCode = 'USD',
            CloseDate = Date.today(),
            AccountId = depart.Id,
            Closing_Bid_Date__c = Date.today().addDays(-5),
            Hospital__c = hospital.Id,
            Competitor__c = 'A'
        );
        insert opp1;
        
        
        // 插入招标项目1
        Tender_information__c info1 = new Tender_information__c();
        info1.Name = 'TEST001';
        info1.AreaProvince__c = '北京';
        // info1.Hospital__c = hospital.Id;
        info1.InfoType__c= '1：公告';
        info1.OpportunityNum__c = 0;
        info1.OwnerId = u1.Id;
        info1.InfoOwner__c = u2.Id;
        info1.relativeTime__c =System.now();
        insert info1;

        Test.startTest();
        
        System.runAs(u1){
            info1.IsBid__c = '否';
            info1.NotBidApprovalStatus__c = '申请中';
            update info1;

            // opp1.SAP_Send_OK__c = true;
            // opp1.Authorized_DB_No__c = '123';
            // opp1.Contract_DB_SalesDept_complite_day__c = Date.today();
            // opp1.NotesApprovedNo__c = '123';
            // update opp1;

            // 插入关联关系
            Tender_Opportunity_Link__c link1 = new Tender_Opportunity_Link__c();
            link1.Tender_information__c = info1.Id;
            link1.Opportunity__c = opp1.Id;
            // link1.Tender_Opportunity_Uniq__c = String.valueOf(info1.Id) + String.valueOf(opp1.Id);
            insert link1;

            // info1.InfoType__c= '3：结果';
            // update info1;
            StaticParameter.EscapeTOLinkTrigger=false;
            delete link1;
        }

        //System.runAs(u2){
            
        //    // 插入招标项目2
        //    Tender_information__c info2 = new Tender_information__c();
        //    info2.Name = 'TEST001';
        //    info2.AreaProvince__c = '北京';
        //    // info2.Hospital__c = hospital.Id;
        //    info2.InfoType__c= '1：公告';
        //    info2.OpportunityNum__c = 0;
        //    info2.OwnerId = u1.Id;
        //    info2.InfoOwner__c = u2.Id;
        //    info2.relativeTime__c =System.now();
        //    insert info2;
        //    info2.IsBid__c = '否';
        //    info2.NotBidApprovalStatus__c = '申请中';
        //    update info2;
            
        //    // 插入关联关系
        //    Tender_Opportunity_Link__c link2 = new Tender_Opportunity_Link__c();
        //    link2.Tender_information__c = info2.Id;
        //    link2.Opportunity__c = opp1.Id;
        //    insert link2;
            
        //    //// 插入关联关系
        //    //Tender_Opportunity_Link__c link3 = new Tender_Opportunity_Link__c();
        //    //link3.Tender_information__c = info1.Id;
        //    //link3.Opportunity__c = opp1.Id;
        //    //insert link3;
            
        //    delete link2;
        //}
        
        Test.stopTest();
    }
     static testMethod void testMethod1() {
        TenderOpportunityLinkHandler.testI();
    }

}