@isTest
public class RentalApplySequenceTriggerHandlerTest{

	//伪造数据
	private static void setupTestData(){
		//前置数据
		System.runAs(new User(Id = Userinfo.getUserId())) {
			String flag='NG2';
			String d1='产品试用';
			String dept='医疗华北营业本部';
		    User user = new User(Test_staff__c = true);
	        user.LastName = '_サンブリッジ';
	        user.FirstName = 'う';
	        user.Alias = 'う';
	        user.Email = 'olympusTest03@sunbridge.com';
	        user.Username = 'olympusTest03@sunbridge.com';
	        user.CommunityNickname = 'う';
	        user.IsActive = true;
	        user.EmailEncodingKey = 'ISO-2022-JP';
	        user.TimeZoneSidKey = 'Asia/Tokyo';
	        user.LocaleSidKey = 'ja_JP';
	        user.LanguageLocaleKey = 'ja';
	        user.ProfileId = System.Label.ProfileId_SystemAdmin;
	        user.Province__c = '北京';
	        user.Dept__c = dept;
	        user.Use_Start_Date__c = Date.today().addMonths(-6);
	        insert user;
			 // 省
	         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 = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId();
	         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 = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Department_OTH').getRecordTypeId();
	         dep.Name = 'test dep1';
	         dep.AgentCode_Ext__c = '9999999';
	         dep.ParentId = strategicDep[0].Id;
	         dep.Department_Class__c = strategicDep[0].Id;
	         dep.Hospital__c = hospital.Id;
	         insert dep;

	         Contact contact2 = new Contact();
	         contact2.AccountId = dep.Id;
	         contact2.FirstName = '責任者';
	         contact2.LastName = 'test1经销商';
	         insert contact2;

	        Product2 pro1 = new Product2(Name='name01',IsActive=true,Family='GI',
	                Fixture_Model_No__c='n01',Serial_Lot_No__c='S/N tracing',
	                Fixture_Model_No_T__c = 'n01', Asset_Model_No__c = 'Pro1',
	                ProductCode_Ext__c='pc01',Manual_Entry__c=false);
	        Product2 pro2 = new Product2(Name='name02',IsActive=true,Family='GI',
	                Fixture_Model_No__c='n02',Serial_Lot_No__c='Lot tracing',
	                Fixture_Model_No_T__c = 'n02', Asset_Model_No__c = 'Pro2',
	                ProductCode_Ext__c='pc02',Manual_Entry__c=false);
	        Product2 pro3 = new Product2(Name='name03',IsActive=true,Family='GI',
	                Fixture_Model_No__c='n03',Serial_Lot_No__c='Lot tracing',
	                Fixture_Model_No_T__c = 'n03', Asset_Model_No__c = 'Pro3',
	                ProductCode_Ext__c='pc03',Manual_Entry__c=false);
	        Product2 pro4 = new Product2(Name='name04',IsActive=true,Family='GI',
	                Fixture_Model_No__c='n04',Serial_Lot_No__c='Lot tracing',
	                Fixture_Model_No_T__c = 'n04', Asset_Model_No__c = 'Pro4',
	                ProductCode_Ext__c='pc04',Manual_Entry__c=false);
	        insert new Product2[] {pro1, pro2, pro3, pro4};


			 //备品借出申请
			 Rental_Apply__c raObj = new Rental_Apply__c();
	         raObj.Name = 'testra';
	         raObj.OwnerId = user.Id;
	         raObj.Product_category__c = 'GI';
	         raObj.Demo_purpose1__c ='产品试用';
	         raObj.demo_purpose2__c = '试用(无询价)';
	         raObj.direct_send__c = '医疗机构';
	         raObj.Loaner_received_staff__c = '王五';
	         raObj.Loaner_received_staff_phone__c = '110';
	         raObj.direct_shippment_address__c = '北京市';
	         raObj.Hospital__c = hospital.Id;
	         raObj.Strategic_dept__c = strategicDep[0].Id;
	         raObj.Account__c = dep.Id;
	         raObj.Request_shipping_day__c = Date.toDay();
	         raObj.Hope_Lonaer_date_Num__c = 16;
	         //raObj.Request_return_day__c = Date.toDay();
	         raObj.Phone_number__c = '1234567890';
	         raObj.Request_approval_time__c = Datetime.newInstance(1970, 1, 1);
	         raObj.Loaner_medical_Staff__c = contact2.Id;
	         if ('协议借用' == d1) {
	             raObj.DB_loaner_request__c = '123';
	             raObj.AgreementBorrowingExtensionDate__c = Date.today().addDays(10);
	             if (false) {
	                 raObj.AgreementBorrowingExtensionDate__c = Date.today().addDays(-1);
	             }
	         }
	         insert raObj;

	    // 备品配套
	        Fixture_Set__c fsObj1 = new Fixture_Set__c();
	        fsObj1.Name = 'set1';
	        fsObj1.Fixture_Set_Body_Model_No__c = 'modelNo1';
	        fsObj1.Loaner_name__c = 'name1';
	        insert fsObj1;


	         Fixture_Set_Detail__c fsdObjA1 = new Fixture_Set_Detail__c();
	         // 备品配套明细
	         fsdObjA1.Name = '备品配套明细名1';
	         fsdObjA1.Name_CHN_Created__c = '中文名称1';
	         fsdObjA1.Product2__c = pro1.Id;
	         fsdObjA1.Fixture_Set__c = fsObj1.Id;
	         fsdObjA1.Is_Body__c = true;
	         fsdObjA1.Is_Optional__c = false;
	         fsdObjA1.UniqueKey__c = fsObj1.Id + ':' + pro1.Id;
	         fsdObjA1.SortInt__c = 1;
	         fsdObjA1.Quantity__c = 1;
	         insert fsdObjA1;

             Fixture_Set_Detail__c fsdObjA2 = new Fixture_Set_Detail__c();
             fsdObjA2.Name = '备品配套明细名2';
             fsdObjA2.Name_CHN_Created__c = '中文名称2';
             fsdObjA2.Product2__c = pro2.Id;
             fsdObjA2.Fixture_Set__c = fsObj1.Id;
             fsdObjA2.Is_Body__c = false;
             fsdObjA2.Is_Optional__c = true;
             fsdObjA2.UniqueKey__c = fsObj1.Id + ':' + pro2.Id;
             fsdObjA2.SortInt__c = 2;
             fsdObjA2.Quantity__c = 1;
             fsdObjA2.Is_OneToOne__c = true;
             insert fsdObjA2;

	         Rental_Apply_Equipment_Set__c raesObj = new Rental_Apply_Equipment_Set__c();
	         // 借出备品配套一览
	         raesObj.Rental_Apply__c = raObj.Id;
	         raesObj.Fixture_Set__c = fsObj1.Id;
	         raesObj.Cancel_Select__c = false;
	         raesObj.Rental_Start_Date__c = Date.toDay();
	         raesObj.Rental_End_Date__c = Date.toDay();
	         raesObj.IndexFromUniqueKey__c = 1;
	         raesObj.UniqueKey__c = '1:'+ fsObj1.Id + ':1';
	         insert raesObj;

			 Rental_Apply_Equipment_Set_Detail__c raesdObj1 = new Rental_Apply_Equipment_Set_Detail__c();
			 raesdObj1.Rental_Apply__c = raObj.Id;
	         raesdObj1.Fixture_Set_Detail__c = fsdObjA1.Id;
	         raesdObj1.Rental_Num__c = 1;
	         raesdObj1.Queue_Number__c = null;
	         raesdObj1.Is_Body__c = true;
	         raesdObj1.Rental_Apply_Equipment_Set__c = raesObj.Id;
	         raesdObj1.IndexFromUniqueKey__c = 1;
	         raesdObj1.UniqueKey__c = '1:'+ raesObj.Id + ':' + fsdObjA1.Id + ':1';
	         raesdObj1.FSD_OneToOneAccessory_Cnt__c = 2;
	         raesdObj1.FSD_Is_Optional__c = false;
	         raesdObj1.FSD_Is_OneToOne__c = false;
	         raesdObj1.ApplyPersonAppended__c = false;
	         raesdObj1.FSD_Fixture_Model_No__c = 'n01';
	         raesdObj1.Fixture_Model_No_text__c = 'n01';
	         raesdObj1.Salesdepartment_before__c = '1.华北营业本部';
	         raesdObj1.Internal_asset_location_before__c = '北京 备品中心';
	         raesdObj1.Product_category_text__c = 'GI';
	         raesdObj1.Equipment_Type_text__c = '产品试用';
	         insert raesdObj1;

             Rental_Apply_Equipment_Set_Detail__c raesdObj2 = new Rental_Apply_Equipment_Set_Detail__c();
             raesdObj2.Rental_Apply__c = raObj.Id;
             raesdObj2.Fixture_Set_Detail__c = fsdObjA2.Id;
             raesdObj2.Rental_Num__c = 1;
             raesdObj2.Queue_Number__c = null;
             raesdObj2.Is_Body__c = false;
             raesdObj2.Rental_Apply_Equipment_Set__c = raesObj.Id;
             raesdObj2.IndexFromUniqueKey__c = 2;
             raesdObj2.UniqueKey__c = '2:'+ raesObj.Id + ':' + fsdObjA2.Id + ':2';
             raesdObj2.FSD_OneToOneAccessory_Cnt__c = 2;
             raesdObj2.FSD_Is_Optional__c = false;
             raesdObj2.FSD_Is_OneToOne__c = true;
             raesdObj2.ApplyPersonAppended__c = false;
             raesdObj2.FSD_Fixture_Model_No__c = 'n02';
             raesdObj2.Fixture_Model_No_text__c = 'n02';
             raesdObj2.Salesdepartment_before__c = '1.华北营业本部';
             raesdObj2.Internal_asset_location_before__c = '北京 备品中心';
             raesdObj2.Product_category_text__c = 'GI';
             raesdObj2.Equipment_Type_text__c = '学会展会';
             insert raesdObj2;
		 
			Rental_Apply_Sequence__c itm1=new Rental_Apply_Sequence__c();
			itm1.ExternalKey__c='key1';
			itm1.Series_No__c=1;
			itm1.Invalid_Flag__c=false;
			itm1.Apply_Set_Detail__c=raesdObj1.Id;
			itm1.Name='it1';
			insert itm1;		

			Rental_Apply_Sequence__c itm2=new Rental_Apply_Sequence__c();
			itm2.Series_No__c=2;
			itm2.Invalid_Flag__c=false;
			itm2.ExternalKey__c='key2';
			itm2.Apply_Set_Detail__c=raesdObj1.Id;
			itm2.Name='it2';
			insert itm2;
		
			Rental_Apply_Sequence__c itm3=new Rental_Apply_Sequence__c();
			itm3.Series_No__c=3;
			itm3.Apply_Set_Detail__c=raesdObj1.Id;
			itm3.Invalid_Flag__c=false;
			itm3.ExternalKey__c='key3';
			itm3.Name='it3';
			insert itm3;
			
			Rental_Apply_Sequence__c itm4=new Rental_Apply_Sequence__c();
			itm4.Series_No__c=4;
			itm4.Apply_Set_Detail__c=raesdObj1.Id;
			itm4.Invalid_Flag__c=false;
			itm4.ExternalKey__c='key4';
			itm4.Name='it4';
			insert itm4;

			Rental_Apply_Sequence__c itm5=new Rental_Apply_Sequence__c();
			itm5.ExternalKey__c='key1';
			itm5.Series_No__c=1;
			itm5.Invalid_Flag__c=true;
			itm5.Apply_Set_Detail__c=raesdObj1.Id;
			itm5.Name='it1';
			insert itm5;		

			Rental_Apply_Sequence__c itm6=new Rental_Apply_Sequence__c();
			itm6.Series_No__c=2;
			itm6.Invalid_Flag__c=true;
			itm6.ExternalKey__c='key2';
			itm6.Apply_Set_Detail__c=raesdObj2.Id;
			itm6.Name='it2';
			insert itm6;
		}
	}

    /*
    //伪造用户
	private static User getUser() {
        String timenow = Datetime.now().format('yyyyMMddHHmmss');
        // システム管理者
        User user = new User(Test_staff__c = true);
        user.LastName = '_サンブリッジ';
        user.FirstName = 'う';
        user.Alias = 'う';
        user.Email = 'olympusTest03@sunbridge.com';
        user.Username = 'olympusTest03@sunbridge.com';
        user.CommunityNickname = 'う';
        user.IsActive = true;
        user.EmailEncodingKey = 'ISO-2022-JP';
        user.TimeZoneSidKey = 'Asia/Tokyo';
        user.LocaleSidKey = 'ja_JP';
        user.LanguageLocaleKey = 'ja';
        user.ProfileId = System.Label.ProfileId_SystemAdmin;
        user.Job_Category__c = '销售推广';
        user.Province__c = '上海市';
        user.Use_Start_Date__c = Date.today().addMonths(-6);
        user.SalesManager__c = UserInfo.getUserId();
        user.BuchangApprovalManagerSales__c = UserInfo.getUserId();
        user.JingliApprovalManager__c = UserInfo.getUserId();
        user.BuchangApprovalManager__c = UserInfo.getUserId();
        user.ZongjianApprovalManager__c = UserInfo.getUserId();
        user.Dept__c = '医疗华北营业本部';
        insert user;
        return user;
    }
    */

    //测试方法
     static testMethod void testMethod1Update(){
     	setupTestData();
         System.runAs(new User(Id = Userinfo.getUserId())) {
        	Rental_Apply_Sequence__c itm5=new Rental_Apply_Sequence__c();
        	List<Rental_Apply_Sequence__c> l1=[select Id,Name,Series_No__c,Invalid_Flag__c from Rental_Apply_Sequence__c];
        	List<Rental_Apply_Sequence__c> l2=new List<Rental_Apply_Sequence__c>();
        	for(Rental_Apply_Sequence__c r1:l1){
               r1.Name='only you';
               r1.Invalid_Flag__c=false;
               l2.add(r1);
        	}
        	test.startTest();
        	update l2;
        	test.stopTest();
        }
     }

     static testMethod void testMethod2Delete(){
     	setupTestData();
         System.runAs(new User(Id = Userinfo.getUserId())) {
         	List<Rental_Apply_Sequence__c> l1=[select Id,Name,Series_No__c,Invalid_Flag__c from Rental_Apply_Sequence__c];
         	Rental_Apply_Sequence__c it1;
         	for(Rental_Apply_Sequence__c r1:l1){
         		it1=r1;
        	}
        	test.startTest();
        	delete it1;
        	test.stopTest();
        }
     }
}