@isTest(SeeAllData=false)
private class otherButtonMaintenanceContractCtlTest {
  
    @isTest
    public static void test_init() {
        Oly_TriggerHandler.bypass(Product2Handler.Class.getName());
            Oly_TriggerHandler.bypass(AssetHandler.Class.getName());
            Oly_TriggerHandler.bypass(FixtureSetHandler.Class.getName());
            Oly_TriggerHandler.bypass(RentalApplyTriggerHandler.Class.getName());
            Oly_TriggerHandler.bypass(RentalApplyEquipmentSetHandler.Class.getName());
            Oly_TriggerHandler.bypass(RentalApplyEquipmentSetDetailHandler.Class.getName());
            Oly_TriggerHandler.bypass(ContactTriggerHandler.Class.getName());
            Oly_TriggerHandler.bypass('AssetTrigger');
            Oly_TriggerHandler.bypass('MaintenanceContractTrigger');
            Oly_TriggerHandler.bypass('MaintenanceContractAfterUpdate');
            Oly_TriggerHandler.bypass('MaintenanceContractBeforeDelete');
            Oly_TriggerHandler.bypass('MaintenanceContractHpDeptUpd');
            Oly_TriggerHandler.bypass('NFM106Controller');
            Oly_TriggerHandler.bypass('NFM101Controller');
            Oly_TriggerHandler.bypass('NFM001AgencyContract');
            Oly_TriggerHandler.bypass('XinEventC2Event');
         // 病院を作る
        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;

        // 戦略科室を得る
        List<Account> strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_GI'];

        // 診療科を作る
        Account dep = new Account();
        dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_GI'].id;
        dep.Name = 'test dep';
        dep.ParentId = strategicDep[0].Id;
        dep.Department_Class__c = strategicDep[0].Id;
        dep.Hospital__c = hospital.Id;
        insert dep;

        // 维修合同を作成する
        Maintenance_Contract__c contract = new Maintenance_Contract__c();
        contract.Name = 'tect contract';
        contract.Hospital__c = hospital.Id;
        contract.Department_Class__c = strategicDep[0].Id;
        contract.Department__c = dep.Id;
        contract.Contract_Start_Date__c = Date.today();
        contract.RecordTypeId = [Select Id FROM RecordType WHERE Name = '维修合同'].Id;
        contract.Contract_End_Date__c = Date.today();
        insert contract;
        Test.startTest();
        otherButtonMaintenanceContractCtl.init(contract.Id);
         otherButtonMaintenanceContractCtl.selectRecords(contract.Id);
        otherButtonMaintenanceContractCtl.selectMaintenanceContractEstimate(contract.Id);
        otherButtonMaintenanceContractCtl.processResults(contract.Id);
        otherButtonMaintenanceContractCtl.updateFirstContract(contract.Id);

        Test.stopTest();
    }
    // @isTest
    // public static void test_updateFirstContract() {
    //     Oly_TriggerHandler.bypass(Product2Handler.Class.getName());
    //         Oly_TriggerHandler.bypass(AssetHandler.Class.getName());
    //         Oly_TriggerHandler.bypass(FixtureSetHandler.Class.getName());
    //         Oly_TriggerHandler.bypass(RentalApplyTriggerHandler.Class.getName());
    //         Oly_TriggerHandler.bypass(RentalApplyEquipmentSetHandler.Class.getName());
    //         Oly_TriggerHandler.bypass(RentalApplyEquipmentSetDetailHandler.Class.getName());
    //         Oly_TriggerHandler.bypass(ContactTriggerHandler.Class.getName());
    //         Oly_TriggerHandler.bypass('AssetTrigger');
    //         Oly_TriggerHandler.bypass('MaintenanceContractTrigger');
    //         Oly_TriggerHandler.bypass('MaintenanceContractAfterUpdate');
    //         Oly_TriggerHandler.bypass('MaintenanceContractBeforeDelete');
    //         Oly_TriggerHandler.bypass('MaintenanceContractHpDeptUpd');
    //         Oly_TriggerHandler.bypass('NFM106Controller');
    //         Oly_TriggerHandler.bypass('NFM101Controller');
    //         Oly_TriggerHandler.bypass('NFM001AgencyContract');
    //         Oly_TriggerHandler.bypass('XinEventC2Event');
    //     Test.startTest();
    //       // 省
    //     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 = '东京';
    //     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;

      
    //     Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
    //     User MacOwner = new User(Test_staff__c = true, LastName = 'hp1', FirstName = 'owner1', Alias = 'hp', CommunityNickname = 'hpOwner1', Email = 'olympus_hpowner@sunbridge.com',Job_Category__c = '销售服务', Username = 'olympus_hpowner@sunbridge.com1', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
    //     insert MacOwner;

    //      Maintenance_Contract__c contract1 = new Maintenance_Contract__c();
    //     contract1.Name = 'tect contract1111';
    //     contract1.Hospital__c = hospital.Id;
    //     contract1.Department_Class__c = strategicDep[0].Id;
    //     contract1.Department__c = dep.Id;
    //     contract1.Service_Contract_Staff__c = MacOwner.Id;
    //     contract1.Payment_Plan_Sum_First__c = 1;
    //     contract1.Status__c = '契約';
    //     contract1.Maintenance_Contract_No__c = '111212123';
       
    //     contract1.Not_Upper_limit_reason__c = ' 1';
    //     contract1.Contract_Start_Date__c = Date.today().addDays( -10);   
    //     contract1.Contract_End_Date__c = Date.today().addDays( 5);       
    //     contract1.SalesOfficeCode_selection__c = '北京RC';
    //     insert contract1;

    //       // 产品
    //     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;

    //     Asset asset2 = new Asset();
    //     asset2.RecordTypeId = System.Label.Asset_RecordType;
    //     asset2.SerialNumber = 'ass01112';
    //     asset2.Name = 'ass01112';
    //     asset2.AccountId = dep.Id;
    //     asset2.Department_Class__c = strategicDep[0].Id;
    //     asset2.Hospital__c = hospital.Id;
    //     asset2.Product2Id = pro5.Id;
    //     asset2.Quantity = 1;
    //     asset2.Status = '有库存';
    //     asset2.Manage_type__c = '个体管理';
    //     asset2.Loaner_accsessary__c = false;
    //     asset2.Out_of_wh__c = 0;
    //     asset2.Salesdepartment__c = '1.华北营业本部';
    //     asset2.Internal_asset_location__c = '北京 备品中心';
    //     asset2.Product_category__c = 'GI';
    //     asset2.Equipment_Type__c = '产品试用';
    //     asset2.SalesProvince__c = '北京';
    //     asset2.CurrentContract__c = contract1.Id;
    //     asset2.CurrentContract_Asset_Price__c = 0;
    //     insert asset2;
    //     Maintenance_Contract_Asset__c contract1asset1 = new Maintenance_Contract_Asset__c();
    //     contract1asset1.Asset__c = asset2.Id;
    //     contract1asset1.Maintenance_Contract__c = contract1.Id;
    //     contract1asset1.Estimate_List_Price_All_Manual__c = 1000;
    //     insert contract1asset1;
    //     otherButtonMaintenanceContractCtl.selectRecords(contract1.Id);
    //     otherButtonMaintenanceContractCtl.selectMaintenanceContractEstimate(contract1.Id);
    //     otherButtonMaintenanceContractCtl.processResults(contract1.Id);
    //     Test.stopTest();
    // }
    // @isTest
    // public static void test_updateFirstContract02() {
    //     Oly_TriggerHandler.bypass(Product2Handler.Class.getName());
    //         Oly_TriggerHandler.bypass(AssetHandler.Class.getName());
    //         Oly_TriggerHandler.bypass(FixtureSetHandler.Class.getName());
    //         Oly_TriggerHandler.bypass(RentalApplyTriggerHandler.Class.getName());
    //         Oly_TriggerHandler.bypass(RentalApplyEquipmentSetHandler.Class.getName());
    //         Oly_TriggerHandler.bypass(RentalApplyEquipmentSetDetailHandler.Class.getName());
    //         Oly_TriggerHandler.bypass(ContactTriggerHandler.Class.getName());
    //         Oly_TriggerHandler.bypass('AssetTrigger');
    //         Oly_TriggerHandler.bypass('MaintenanceContractTrigger');
    //         Oly_TriggerHandler.bypass('MaintenanceContractAfterUpdate');
    //         Oly_TriggerHandler.bypass('MaintenanceContractBeforeDelete');
    //         Oly_TriggerHandler.bypass('MaintenanceContractHpDeptUpd');
    //         Oly_TriggerHandler.bypass('NFM106Controller');
    //         Oly_TriggerHandler.bypass('NFM101Controller');
    //         Oly_TriggerHandler.bypass('NFM001AgencyContract');
    //         Oly_TriggerHandler.bypass('XinEventC2Event');
    //       // 省
    //     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 = '东京';
    //     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;

      
    //     Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
    //     User MacOwner = new User(Test_staff__c = true, LastName = 'hp1', FirstName = 'owner1', Alias = 'hp', CommunityNickname = 'hpOwner1', Email = 'olympus_hpowner@sunbridge.com',Job_Category__c = '销售服务', Username = 'olympus_hpowner@sunbridge.com1', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
    //     insert MacOwner;

    //      Maintenance_Contract__c contract1 = new Maintenance_Contract__c();
    //     contract1.Name = 'tect contract1111';
    //     contract1.Hospital__c = hospital.Id;
    //     contract1.Department_Class__c = strategicDep[0].Id;
    //     contract1.Department__c = dep.Id;
    //     contract1.Service_Contract_Staff__c = MacOwner.Id;
    //     contract1.Payment_Plan_Sum_First__c = 1;
    //     contract1.Status__c = '契約';
    //     contract1.Maintenance_Contract_No__c = '111212123';
       
    //     contract1.Not_Upper_limit_reason__c = ' 1';
    //     contract1.Contract_Start_Date__c = Date.today().addDays( -10);   
    //     contract1.Contract_End_Date__c = Date.today().addDays( 5);       
    //     contract1.SalesOfficeCode_selection__c = '北京RC';
    //     insert contract1;

    //       // 产品
    //     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;

    //     Asset asset2 = new Asset();
    //     asset2.RecordTypeId = System.Label.Asset_RecordType;
    //     asset2.SerialNumber = 'ass01112';
    //     asset2.Name = 'ass01112';
    //     asset2.AccountId = dep.Id;
    //     asset2.Department_Class__c = strategicDep[0].Id;
    //     asset2.Hospital__c = hospital.Id;
    //     asset2.Product2Id = pro5.Id;
    //     asset2.Quantity = 1;
    //     asset2.Status = '有库存';
    //     asset2.Manage_type__c = '个体管理';
    //     asset2.Loaner_accsessary__c = false;
    //     asset2.Out_of_wh__c = 0;
    //     asset2.Salesdepartment__c = '1.华北营业本部';
    //     asset2.Internal_asset_location__c = '北京 备品中心';
    //     asset2.Product_category__c = 'GI';
    //     asset2.Equipment_Type__c = '产品试用';
    //     asset2.SalesProvince__c = '北京';
    //     asset2.CurrentContract__c = contract1.Id;
    //     asset2.CurrentContract_Asset_Price__c = 0;
    //     insert asset2;
    //     Maintenance_Contract_Asset__c contract1asset1 = new Maintenance_Contract_Asset__c();
    //     contract1asset1.Asset__c = asset2.Id;
    //     contract1asset1.Maintenance_Contract__c = contract1.Id;
    //     contract1asset1.Estimate_List_Price_All_Manual__c = 1000;
    //     insert contract1asset1;
    //     Test.startTest();
    //     otherButtonMaintenanceContractCtl.updateFirstContract(contract1.Id);
       
    //     Test.stopTest();
    // }
    
    @isTest
    public static void test_updateColunm() {
        Oly_TriggerHandler.bypass(Product2Handler.Class.getName());
            Oly_TriggerHandler.bypass(AssetHandler.Class.getName());
            Oly_TriggerHandler.bypass(FixtureSetHandler.Class.getName());
            Oly_TriggerHandler.bypass(RentalApplyTriggerHandler.Class.getName());
            Oly_TriggerHandler.bypass(RentalApplyEquipmentSetHandler.Class.getName());
            Oly_TriggerHandler.bypass(RentalApplyEquipmentSetDetailHandler.Class.getName());
            Oly_TriggerHandler.bypass(ContactTriggerHandler.Class.getName());
            Oly_TriggerHandler.bypass('AssetTrigger');
            Oly_TriggerHandler.bypass('MaintenanceContractTrigger');
            Oly_TriggerHandler.bypass('MaintenanceContractAfterUpdate');
            Oly_TriggerHandler.bypass('MaintenanceContractBeforeDelete');
            Oly_TriggerHandler.bypass('MaintenanceContractHpDeptUpd');
            Oly_TriggerHandler.bypass('NFM106Controller');
            Oly_TriggerHandler.bypass('NFM101Controller');
            Oly_TriggerHandler.bypass('NFM001AgencyContract');
            Oly_TriggerHandler.bypass('XinEventC2Event');
        Test.startTest();
        // 病院を作る
        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;

        // 戦略科室を得る
        List<Account> strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_GI'];

        // 診療科を作る
        Account dep = new Account();
        dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_GI'].id;
        dep.Name = 'test dep';
        dep.ParentId = strategicDep[0].Id;
        dep.Department_Class__c = strategicDep[0].Id;
        dep.Hospital__c = hospital.Id;
        insert dep;

        // 维修合同を作成する
        Maintenance_Contract__c contract = new Maintenance_Contract__c();
        contract.Name = 'tect contract';
        contract.Hospital__c = hospital.Id;
        contract.Department_Class__c = strategicDep[0].Id;
        contract.Department__c = dep.Id;
        contract.Contract_Start_Date__c = Date.today();
        contract.RecordTypeId = [Select Id FROM RecordType WHERE Name = '维修合同'].Id;
        contract.Contract_End_Date__c = Date.today();
        insert contract;

        // 维修合同を作成する
        Maintenance_Contract__c contract1 = new Maintenance_Contract__c();
        contract1.Name = 'tect contract';
        contract1.Hospital__c = hospital.Id;
        contract1.Department_Class__c = strategicDep[0].Id;
        contract1.Department__c = dep.Id;
        contract1.Maintenance_Contract_No__c = 'contact1';
        contract1.Contract_Start_Date__c = Date.today();
        contract1.Contract_Conclusion_Date__c = Date.today();
        contract1.RecordTypeId = [Select Id FROM RecordType WHERE Name = '服务合同'].Id;
        contract1.Contract_End_Date__c = Date.today();
        insert contract1;


        // 维修合同を作成する
        Maintenance_Contract__c contract2 = new Maintenance_Contract__c();
        contract2.Name = 'tect contract';
        contract2.Hospital__c = hospital.Id;
        contract2.Department_Class__c = strategicDep[0].Id;
        contract2.Department__c = dep.Id;
        contract2.Contract_Start_Date__c = Date.today();
        contract2.RecordTypeId = [Select Id FROM RecordType WHERE Name = '维修合同'].Id;
        contract2.Contract_End_Date__c = Date.today();
        insert contract2;

        // 维修合同を作成する
        Maintenance_Contract__c contract3 = new Maintenance_Contract__c();
        contract3.Name = 'tect contract';
        contract3.Hospital__c = hospital.Id;
        contract3.Department_Class__c = strategicDep[0].Id;
        contract3.Department__c = dep.Id;
        contract3.Contract_Start_Date__c = Date.today();
        contract3.Maintenance_Contract_No__c = 'contact1';
        contract3.Contract_Conclusion_Date__c = Date.today();
        contract3.RecordTypeId = [Select Id FROM RecordType WHERE Name = '服务合同'].Id;
        contract3.Contract_End_Date__c = Date.today();
        insert contract3;


        // 保有设备C (只有附属品 个体管理)
        Asset assetC1 = new Asset(Asset_Owner__c = 'Olympus');
        assetC1.RecordTypeId = System.Label.Asset_RecordType;
        assetC1.SerialNumber = 'assetC1';
        assetC1.Name = '测试';
        assetC1.AccountId = dep.Id;
   
        assetC1.Out_of_wh__c = 0;
        // 保有设备
        Asset asset = new Asset(Asset_Owner__c = 'Olympus');
        asset.RecordTypeId = System.Label.Asset_RecordType;
        asset.SerialNumber = 'asset';
        Asset.AccountId = dep.Id;
        asset.Freeze_sign__c = false;
        asset.Out_of_wh__c = 0;
        asset.Name = '测试';
        insert new Asset[] {assetC1, asset};

        //创建维修合同下的保有设备
        Maintenance_Contract_Asset__c mc1 = new Maintenance_Contract_Asset__c();
        mc1.Maintenance_Contract__c = contract2.Id;
        mc1.LastMContract__c = contract3.Id;
        mc1.Asset__c = assetC1.Id;
        insert mc1;

        //创建维修合同下的保有设备
        Maintenance_Contract_Asset__c mc2 = new Maintenance_Contract_Asset__c();
        mc2.Maintenance_Contract__c = contract1.Id;
        mc2.LastMContract__c = contract.Id;
        mc2.Asset__c = asset.Id;
        insert mc2;

        System.debug('上一期维修合同空白期:mc1 --- '+mc1.LastMContract_Gap__c);
        System.debug('上一期维修合同空白期:mc2 --- '+mc2.LastMContract_Gap__c);

        otherButtonMaintenanceContractCtl.updateColunm(contract.Id);
        otherButtonMaintenanceContractCtl.updateColunm(contract2.Id);
        
        otherButtonMaintenanceContractCtl.updateColunm(contract1.Id);
        otherButtonMaintenanceContractCtl.updateColunm(contract3.Id);
        Test.stopTest();
    }
    // @isTest
    // public static void test_selectMaintenanceContractEstimate() {
      
    //     Test.startTest();
    //     List<Maintenance_Contract__c> mcList = [select id from Maintenance_Contract__c limit 1];
    //     if(mcList.size()>0) {
    //         for(Maintenance_Contract__c mc:mcList) {
    //             LexOtherButtonMc.selectMaintenanceContractEstimate(mc.Id);
    //         LexOtherButtonMc.processResults(mc.Id);
    //         }
            
    //     }
       
    //     Test.stopTest();
    // }
}