@isTest
private class AfterUpdateReportTriggerTest {
    static testMethod void testUpdateRental_Apply() {
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        // ユーザー作成
        User hpOwner = new User(Dept__c='医疗华北营业本部',Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner;
        // 取引先作成
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectHp.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            return;
        }
        List<RecordType> rectOPD = [select Id,name from RecordType where IsActive = true and SobjectType = 'Report__c' and Name = 'OPD・跟台'];
        if (rectOPD.size() == 0) {
            return;
        }
        
        Account hp1 = new Account(RecordTypeId = rectHp[0].Id, Name = 'TestHp1', OwnerId = hpOwner.Id);
        insert hp1;

        List<Account> dc1 = [Select Id, Name, Department_Class_Label__c from Account where Parent.Id = :hp1.Id and Department_Class_Label__c = '消化科'];

        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt[0].Id;
        depart1.Name         = '*';
        depart1.Department_Name__c  = '診療科1';
        depart1.ParentId            = dc1[0].Id;
        depart1.Department_Class__c = dc1[0].Id;
        depart1.Hospital__c         = hp1.Id;

        insert depart1;
        //AccountShare dcTS1 = new AccountShare(UserOrGroupId=hpOwner.Id,  AccountId=hp1.Id,AccountAccessLevel='Read', OpportunityAccessLevel = 'Read');
        //insert dcTS1;
        Daily_Report__c dr1 = new Daily_Report__c();
        dr1.Reporter__c = UserInfo.getUserId();
        dr1.Reported_Date__c = date.today();
        dr1.Status__c = '申請中';
        
        insert dr1;
        
        Report__c rep1 = new Report__c();
        rep1.CurrencyIsoCode         = 'CNY';
        rep1.RecordTypeId            = rectOPD[0].Id;
        rep1.Status__c               = '';
        rep1.Daily_Report__c         = dr1.Id;
        rep1.Purpose1__c             = 'OPD';
        rep1.Purpose2__c             = '引合追及活動';
        rep1.Reason__c               = '実施背景';
        rep1.Appeal_Point__c         = '実施目的・訴求点';
        rep1.Strategic_Department_Class__c = '1.消化科';
        rep1.Technical_Category1__c  = '消化道检查';
        rep1.Technical_Category2__c  = '普通胃镜检查';
        rep1.Patient_Status__c       = '患者状況';
        rep1.Operation_Process__c    = '手術プロセス';
        rep1.Operation_Task__c       = '手術上の課題';
        rep1.Product_Evaluation_Advantage__c = '製品評価利点';
        rep1.Product_Evaluation_Faults__c = '製品評価欠点';
        rep1.OPD_Summarize__c        = 'OPD実施総括';
        rep1.Activity_Plan__c        = '今後の活動計画';
        rep1.Opportunity_Situation__c = '引合無';
        rep1.OPD_ProductCategory1__c ='3D System';
        rep1.OPD_ProductCategory2__c ='OR Imaging Products';
        rep1.Hospital_Reference__c   = hp1.Id;
        rep1.Department_Class_Ref__c = dc1[0].Id;
        rep1.Hospital_Department__c  = depart1.Id;
        rep1.Status__c = '草案中';
        rep1.Date__c                 = Date.today().addDays(-1);
        
        insert rep1;
        //Rental_Apply__Share ras = new Rental_Apply__Share(
        //            RowCause = 'ApplyUserShare__c',
        //            UserOrGroupId = hpOwner.Id,
        //            ParentId = '11111111',
        //            AccessLevel = 'Edit'
        //        );
        //insert ras;
        Rental_Apply__c ra = new Rental_Apply__c();
        ra.Rental_Start_Date__c = Date.today().addDays(1);
        ra.Request_return_day__c=Date.today().addDays(5);
        ra.DataMigration_Flag__c = true;
        //ra.demo_purpose2__c = '其他';
        //ra.Person_In_Charge__c = hpOwner.Id;
        //System.runAs(hpOwner) {
            insert ra;
        //}
        Task t1 = new Task(
            Subject = 'test_task',
            Status = '未着手',
            Priority = '中',
            whatid = rep1.id
        );
        insert new Task[] {t1};

        rep1.Status__c = '申請中';
        update rep1;


        Rental_Apply__c rs1 = [select Id,OPD__c from Rental_Apply__c where Id = :ra.Id];
        System.assertEquals(null, rs1.OPD__c);
        
        rep1.Loaner_request_no1__c = ra.Id;
        update rep1;
        
        Rental_Apply__c rs2 = [select Id,OPD__c from Rental_Apply__c where Id = :ra.Id];
        System.assertEquals(rep1.Id, rs2.OPD__c);
    }
    
    static testMethod void testInsertRental_Apply() {
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        // ユーザー作成
        User hpOwner = new User(Dept__c='医疗华北营业本部',Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner;
        // 取引先作成
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectHp.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            return;
        }
        List<RecordType> rectOPD = [select Id,name from RecordType where IsActive = true and SobjectType = 'Report__c' and Name = 'OPD・跟台'];
        if (rectOPD.size() == 0) {
            return;
        }
        
        Account hp1 = new Account(RecordTypeId = rectHp[0].Id, Name = 'TestHp1', OwnerId = hpOwner.Id);
        insert hp1;

        List<Account> dc1 = [Select Id, Name, Department_Class_Label__c from Account where Parent.Id = :hp1.Id and Department_Class_Label__c = '消化科'];

        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt[0].Id;
        depart1.Name         = '*';
        depart1.Department_Name__c  = '診療科1';
        depart1.ParentId            = dc1[0].Id;
        depart1.Department_Class__c = dc1[0].Id;
        depart1.Hospital__c         = hp1.Id;

        insert depart1;
        //AccountShare dcTS1 = new AccountShare(UserOrGroupId=hpOwner.Id, AccountId=hp1.Id, AccountAccessLevel='Read', OpportunityAccessLevel = 'Read');
        //insert dcTS1;
        Daily_Report__c dr1 = new Daily_Report__c();
        dr1.Reporter__c = UserInfo.getUserId();
        dr1.Reported_Date__c = date.today();
        dr1.Status__c = '申請中';
        
        insert dr1;
        
        Rental_Apply__c ra = new Rental_Apply__c();
        ra.Rental_Start_Date__c = Date.today().addDays(1);
        //ra.Rental_End_Date__c = Date.today().addDays(3);
        ra.Request_return_day__c=Date.today().addDays(5);
        ra.DataMigration_Flag__c = true;
        //ra.demo_purpose2__c = '其他';
        //ra.Person_In_Charge__c = hpOwner.Id;
        
        insert ra;
        
        
        Rental_Apply__c rs1 = [select Id,OPD__c from Rental_Apply__c where Id = :ra.Id];
        System.assertEquals(null, rs1.OPD__c);
        
        Report__c rep1 = new Report__c();
        rep1.CurrencyIsoCode         = 'CNY';
        rep1.RecordTypeId            = rectOPD[0].Id;
        rep1.Status__c               = '草案中';
        rep1.Daily_Report__c         = dr1.Id;
        rep1.Purpose1__c             = 'OPD';
        rep1.Purpose2__c             = '引合追及活動';
        rep1.Reason__c               = '実施背景';
        rep1.Appeal_Point__c         = '実施目的・訴求点';
        rep1.Strategic_Department_Class__c = '1.消化科';
        rep1.Technical_Category1__c  = '消化道检查';
        rep1.Technical_Category2__c  = '普通胃镜检查';
        rep1.Patient_Status__c       = '患者状況';
        rep1.Operation_Process__c    = '手術プロセス';
        rep1.Operation_Task__c       = '手術上の課題';
        rep1.Product_Evaluation_Advantage__c = '製品評価利点';
        rep1.Product_Evaluation_Faults__c = '製品評価欠点';
        rep1.OPD_Summarize__c        = 'OPD実施総括';
        rep1.Activity_Plan__c        = '今後の活動計画';
        rep1.Opportunity_Situation__c = '引合無';
        rep1.OPD_ProductCategory1__c ='3D System';
        rep1.OPD_ProductCategory2__c ='OR Imaging Products';
        rep1.Hospital_Reference__c   = hp1.Id;
        rep1.Department_Class_Ref__c = dc1[0].Id;
        rep1.Hospital_Department__c  = depart1.Id;
        rep1.Date__c                 = Date.today().addDays(-1);
        rep1.Loaner_request_no1__c = ra.Id;
        
        insert rep1;
        
        Rental_Apply__c rs2 = [select Id,OPD__c from Rental_Apply__c where Id = :ra.Id];
        //System.assertEquals(rep1.Id, rs2.OPD__c);
    }

    static testMethod void testMethod2() {
        RecordType rectOpp = [select id from RecordType where IsActive = true and SobjectType = 'Opportunity' and DeveloperName = 'Opportunity' ];
        // 病院を作る
            Account hospital = new Account();
            hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
            hospital.Name = 'test hospital';
            insert hospital;
        // 戦略科室を得る
        Account[] strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_OTH'];
            // 診療科を作る
        Account dep = new Account();
        dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_OTH'].id;
        dep.Name = 'test dep';
        dep.AgentCode_Ext__c = '9999998';
        dep.ParentId = strategicDep[0].Id;
        dep.Department_Class__c = strategicDep[0].Id;
        dep.Hospital__c = hospital.Id;
        insert dep;
        Opportunity opp1 = new Opportunity(
            Name='testOpp1',
            StageName='引合',
            CloseDate=Date.today(),
            AccountId=dep.Id,
            Competitor__c ='A',
            Click_Close_Date__c = null,
            RecordType = rectOpp
            );
        insert opp1;
        
        OPDPlan__c oPDPlan0 = new OPDPlan__c();
        oPDPlan0.Status__c = '草案中';
        oPDPlan0.OPDPlan_ImplementDate__c = Date.today().addDays(1);
        oPDPlan0.NoOpp_Reason__c = 'HCP对应';
        insert oPDPlan0;

        Rental_Apply__c raObj1 = new Rental_Apply__c();
        raObj1.Rental_Start_Date__c = Date.today().addDays(1);
        raObj1.Request_return_day__c=Date.today().addDays(5);
        raObj1.DataMigration_Flag__c = true;
        raObj1.demo_purpose2__c = 'ceshi';
        raObj1.Follow_UP_Opp__c = opp1.Id;
        insert raObj1;
        

        Rental_Apply__c raObj = new Rental_Apply__c();
        raObj.Rental_Start_Date__c = Date.today().addDays(1);
        raObj.Request_return_day__c=Date.today().addDays(5);
        raObj.DataMigration_Flag__c = true;
        raObj.demo_purpose2__c = '试用（有询价）';
        raObj.Old_Rental_Apply__c = raObj1.id;
        raObj.Follow_UP_Opp__c = opp1.Id;
        raObj.Status__c = '草案中';
        raObj.OPDPlan__c = oPDPlan0.id;
        insert raObj;
        // 借出备品配套一览
        Rental_Apply_Equipment_Set__c raesObj = new Rental_Apply_Equipment_Set__c();
        raesObj.Rental_Apply__c = raObj.Id;
        raesObj.Cancel_Select__c = false;
        raesObj.Shipment_request_time__c = Date.toDay();
        raesObj.Rental_Start_Date__c = Date.toDay();
        raesObj.Rental_End_Date__c = Date.toDay();
        raesObj.IndexFromUniqueKey__c = 1;
        raesObj.UniqueKey__c = '1::1';
        raesObj.DataMigration_Flag__c =true;
        insert raesObj;
         // 借出备品配套一览明细
        Rental_Apply_Equipment_Set_Detail__c raesdObj1 = new Rental_Apply_Equipment_Set_Detail__c();
        raesdObj1.Rental_Apply__c = raObj.Id;
       // raesdObj1.Rental_Num__c = 1;
        raesdObj1.Queue_Number__c = null;
        raesdObj1.Is_Body__c = true;
        raesdObj1.IndexFromUniqueKey__c = 1;
        raesdObj1.UniqueKey__c = '1:'+ raesObj.Id +  ':1';
        raesdObj1.Salesdepartment_before__c = '1.华北营业本部';
        raesdObj1.Internal_asset_location_before__c = '北京 备品中心';
        raesdObj1.Product_category_text__c = 'GI';
        raesdObj1.Equipment_Type_text__c = '产品试用';
        raesdObj1.Rental_Apply_Equipment_Set__c = raesObj.Id;
        raesdObj1.Fixture_Model_No_text__c ='n05';
        insert raesdObj1;

        raesObj.First_RAESD__c = raesdObj1.Id;
        update raesObj;
 
        // 产品
        Product2 pro5 = new Product2(Name='name05',IsActive=true,Family='SP',
                Fixture_Model_No__c='n05',Serial_Lot_No__c='S/N tracing',
                Fixture_Model_No_T__c = 'n05',
                ProductCode_Ext__c='pc05',Manual_Entry__c=false);
        insert pro5; 

        System.Test.startTest();
        Report__c rep1 = new Report__c();
        rep1.CurrencyIsoCode         = 'CNY';
       // rep1.RecordTypeId            = rectOPD[0].Id;
        rep1.Report_OPDPlan__c       = oPDPlan0.id;
        rep1.Status__c               = '';
        rep1.Loaner_request_no1__c         = raObj.Id;
        rep1.Purpose1__c             = 'OPD';
        rep1.Purpose2__c             = '引合追及活動';        
        rep1.Reason__c               = '実施背景';
        rep1.Appeal_Point__c         = '実施目的・訴求点';
        rep1.Strategic_Department_Class__c = '1.消化科';
        rep1.Technical_Category1__c  = '消化道检查';
        rep1.Technical_Category2__c  = '普通胃镜检查';
        rep1.Patient_Status__c       = '患者状況';
        rep1.Operation_Process__c    = '手術プロセス';
        rep1.Operation_Task__c       = '手術上の課題';
        rep1.Product_Evaluation_Advantage__c = '製品評価利点';
        rep1.Product_Evaluation_Faults__c = '製品評価欠点';
        rep1.OPD_Summarize__c        = 'OPD実施総括';
        rep1.Activity_Plan__c        = '今後の活動計画';
        rep1.Opportunity_Situation__c = '引合無';
        rep1.OPD_ProductCategory1__c ='3D System';
        rep1.OPD_ProductCategory2__c ='OR Imaging Products';
        rep1.Hospital_Reference__c   = hospital.Id;
        rep1.Department_Class_Ref__c = strategicDep[0].Id;
        rep1.Hospital_Department__c  = dep.Id;
        
        rep1.Status__c = '草案中';
        rep1.Date__c= Date.today().addDays(-1);
        
        insert rep1;


        rep1.Status__c = '申請中'; 
        rep1.Report_OPDPlan__c       = oPDPlan0.id;
        update rep1;
        System.Test.stopTest();

    }
}