@isTest
private class NFM301SendEmailTest {
	static String idX;
	static String idY;
	public static void setData(Integer num1, Integer num2, Integer num3, Integer num4, Boolean haveLoaner, Boolean haveBack, Boolean que){

        // 病院を作る
        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;
        // 产品
        Product2 pro1 = new Product2(Name='name01',IsActive=true,Family='GI',Fixture_Model_No__c='n01',Serial_Lot_No__c='S/N tracing',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',ProductCode_Ext__c='pc02',Manual_Entry__c=false);
        Product2 pro3 = new Product2(Name='name03',IsActive=true,Family='SP',Fixture_Model_No__c='n03',Serial_Lot_No__c='Lot tracing',ProductCode_Ext__c='pc03',Manual_Entry__c=false);
        Product2 pro4 = new Product2(Name='name04',IsActive=true,Family='SP',Fixture_Model_No__c='n04',Serial_Lot_No__c='S/N tracing',ProductCode_Ext__c='pc04',Manual_Entry__c=false);
        insert new Product2[] {pro1, pro2, pro3, pro4};


        Asset asset2 = new Asset(Asset_Owner__c = 'Olympus');
        asset2.RecordTypeId = System.Label.Asset_RecordType;
        asset2.SerialNumber = 'ass02';
        asset2.Loaner_accsessary__c = true;
        asset2.Name = 'ass02';
        asset2.AccountId = dep.Id;
        asset2.Department_Class__c = strategicDep[0].Id;
        asset2.Hospital__c = hospital.Id;
        asset2.Product2Id = pro2.Id;
        asset2.Quantity = 10;
        asset2.Status = '有库存';
        asset2.Out_of_wh__c = 3;
        asset2.Manage_type__c = '数量管理';

        Asset asset3 = new Asset(Asset_Owner__c = 'Olympus');
        asset3.RecordTypeId = System.Label.Asset_RecordType;
        asset3.SerialNumber = 'ass03';
        asset3.Loaner_accsessary__c = true;
        asset3.Name = 'ass03';
        asset3.AccountId = dep.Id;
        asset3.Department_Class__c = strategicDep[0].Id;
        asset3.Hospital__c = hospital.Id;
        asset3.Product2Id = pro3.Id;
        asset3.Quantity = 10;
        asset3.Status = '有库存';
        asset3.Out_of_wh__c = 3;
        asset3.Manage_type__c = '数量管理';

        Asset asset4 = new Asset(Asset_Owner__c = 'Olympus');
        asset4.RecordTypeId = System.Label.Asset_RecordType;
        asset4.SerialNumber = 'ass04';
        asset4.Loaner_accsessary__c = true;
        asset4.Name = 'ass04';
        asset4.AccountId = dep.Id;
        asset4.Department_Class__c = strategicDep[0].Id;
        asset4.Hospital__c = hospital.Id;
        asset4.Product2Id = pro4.Id;
        asset4.Quantity = 10;
        asset4.Status = '有库存';
        asset4.Out_of_wh__c = 3;
        asset4.Manage_type__c = '数量管理';

        insert new Asset[] {asset2, asset3, asset4};
        idx=asset2.id;
        idY=asset3.id;

        Fixture_OneToOne_Link__c foLink1 = new Fixture_OneToOne_Link__c();
        foLink1.Main_Asset__c = asset2.Id;
        foLink1.Accessory_Asset__c = asset3.Id;
        foLink1.Quantity__c = 2;
        foLink1.Select_Accessory_Asset_Cnt__c = 0;
        insert new Fixture_OneToOne_Link__c[] {foLink1};

        Fixture_Set__c fixtures1 = new Fixture_Set__c();
        fixtures1.Name = 'set1';
        fixtures1.Loaner_name__c = 'name1';

        Fixture_Set__c fixtures2 = new Fixture_Set__c();
        fixtures2.Name = 'set2';
        fixtures2.Loaner_name__c = 'name2';
        insert new Fixture_Set__c[] {fixtures1, fixtures2};

        Fixture_Set_Detail__c fsd1 = new Fixture_Set_Detail__c();
        fsd1.Product2__c = pro1.Id;
        fsd1.Fixture_Set__c = fixtures1.Id;
        fsd1.Is_Body__c = true;
        fsd1.Is_Optional__c = false;
        fsd1.UniqueKey__c = fixtures1.Id + ':' + pro1.Id;
        fsd1.SortInt__c = 1;

        Fixture_Set_Detail__c fsd2 = new Fixture_Set_Detail__c();
        fsd2.Product2__c = pro2.Id;
        fsd2.Fixture_Set__c = fixtures1.Id;
        fsd2.Is_Body__c = false;
        fsd2.Is_Optional__c = true;
        fsd2.UniqueKey__c = fixtures1.Id + ':' + pro2.Id;
        fsd2.SortInt__c = 2;

        Fixture_Set_Detail__c fsd3 = new Fixture_Set_Detail__c();
        fsd3.Product2__c = pro3.Id;
        fsd3.Fixture_Set__c = fixtures2.Id;
        fsd3.Is_Body__c = true;
        fsd3.Is_Optional__c = false;
        fsd3.UniqueKey__c = fixtures2.Id + ':' + pro3.Id;
        fsd3.SortInt__c = 1;

        Fixture_Set_Detail__c fsd4 = new Fixture_Set_Detail__c();
        fsd4.Product2__c = pro4.Id;
        fsd4.Fixture_Set__c = fixtures2.Id;
        fsd4.Is_Body__c = false;
        fsd4.Is_Optional__c = true;
        fsd4.UniqueKey__c = fixtures2.Id + ':' + pro4.Id;
        fsd4.SortInt__c = 2;

        insert new Fixture_Set_Detail__c[] {fsd1,fsd2,fsd3,fsd4};

        //创建借出申请
        Rental_Apply__c ra = new Rental_Apply__c();
        ra.Name = 'testra';
        ra.Rental_Start_Date__c = Date.today();
        ra.Request_return_day__c=Date.today();
        ra.DataMigration_Flag__c = true;
        ra.demo_purpose2__c = 'ceshi1';
        //创建借出申请
        Rental_Apply__c ra1 = new Rental_Apply__c();
        ra1.Name = 'testra1';
        ra1.Rental_Start_Date__c = Date.today();
        ra1.Request_return_day__c=Date.today();
        ra1.DataMigration_Flag__c = true;
        ra1.demo_purpose2__c = 'ceshi2';
        insert new List<Rental_Apply__c>{ra, ra1};


        Rental_Apply_Equipment_Set__c es1 = new Rental_Apply_Equipment_Set__c();
        es1.Rental_Apply__c = ra.Id;
        es1.Fixture_Set__c = fixtures1.Id;
        es1.IndexFromUniqueKey__c = 1;
        es1.UniqueKey__c = '1:'+ fixtures1.Id + ':1';

        Rental_Apply_Equipment_Set__c es2 = new Rental_Apply_Equipment_Set__c();
        es2.Rental_Apply__c = ra.Id;
        es2.Fixture_Set__c = fixtures2.Id;
        es2.IndexFromUniqueKey__c = 1;
        es2.UniqueKey__c = '1:'+ fixtures2.Id + ':1';

        insert new Rental_Apply_Equipment_Set__c[]{es1,es2};

        //创建借出配套明细
        Rental_Apply_Equipment_Set_Detail__c esd1 = new Rental_Apply_Equipment_Set_Detail__c();
        esd1.Rental_Apply__c = ra.Id;
        esd1.Cancel_Select__c = false;
        esd1.FSD_Is_OneToOne__c = true;
        esd1.Fixture_OneToOne_Link_Id__c = foLink1.Id;
        esd1.Fixture_Set_Detail__c = fsd1.Id;
        esd1.Rental_Num__c = num1;
        esd1.Queue_Number__c = num2;
        esd1.Rental_Apply_Equipment_Set__c = es1.Id;
        esd1.Salesdepartment_before__c = '1.华北营业本部';
        esd1.Internal_asset_location_before__c = '北京 备品中心';
        esd1.Product_category_text__c = 'GI';
        esd1.Equipment_Type_text__c = '产品试用';
        esd1.IndexFromUniqueKey__c = 1;
        esd1.UniqueKey__c = '1:'+ es1.Id + ':' + fsd1.Id + ':1';
        esd1.Is_Body__c = false;

        Rental_Apply_Equipment_Set_Detail__c esd2 = new Rental_Apply_Equipment_Set_Detail__c();
        esd2.Rental_Apply__c = ra.Id;
        esd2.Fixture_Set_Detail__c = fsd2.Id;
        esd2.Rental_Num__c = num1;
        esd2.Queue_Number__c = num2;
        esd2.Rental_Apply_Equipment_Set__c = es1.Id;
        esd2.Salesdepartment_before__c = '1.华北营业本部';
        esd2.Internal_asset_location_before__c = '北京 备品中心';
        esd2.Product_category_text__c = 'GI';
        esd2.Equipment_Type_text__c = '产品试用';
        esd2.IndexFromUniqueKey__c = 2;
        esd2.UniqueKey__c = '1:'+ es1.Id + ':' + fsd2.Id + ':1';
        esd2.Is_Body__c = false;

        Rental_Apply_Equipment_Set_Detail__c esd3 = new Rental_Apply_Equipment_Set_Detail__c();
        esd3.Rental_Apply__c = ra1.Id;
        esd3.Fixture_Set_Detail__c = fsd1.Id;
        esd3.Rental_Num__c = num3;
        esd3.Queue_Number__c = num4;
        esd3.Rental_Apply_Equipment_Set__c = es2.Id;
        esd3.Salesdepartment_before__c = '1.华北营业本部';
        esd3.Internal_asset_location_before__c = '北京 备品中心';
        esd3.Product_category_text__c = 'GI';
        esd3.Equipment_Type_text__c = '产品试用';
        esd3.IndexFromUniqueKey__c = 1;
        esd3.UniqueKey__c = '1:'+ es2.Id + ':' + fsd1.Id + ':1';
        esd3.Is_Body__c = true;

        Rental_Apply_Equipment_Set_Detail__c esd4 = new Rental_Apply_Equipment_Set_Detail__c();
        esd4.Rental_Apply__c = ra1.Id;
        esd4.Fixture_Set_Detail__c = fsd2.Id;
        esd4.Rental_Num__c = num3;
        esd4.Queue_Number__c = num4;
        esd4.Rental_Apply_Equipment_Set__c = es2.Id;
        esd4.Salesdepartment_before__c = '1.华北营业本部';
        esd4.Internal_asset_location_before__c = '北京 备品中心';
        esd4.Product_category_text__c = 'GI';
        esd4.Equipment_Type_text__c = '产品试用';
        esd4.IndexFromUniqueKey__c = 2;
        esd4.UniqueKey__c = '1:'+ es2.Id + ':' + fsd2.Id + ':1';
        esd4.Is_Body__c = false;

        if (haveLoaner == true) {
            esd2.Asset__c = asset2.Id;
            esd4.Asset__c = asset2.Id;
            es1.Shipment_request_time__c = Date.toDay();
            es2.Shipment_request_time__c = Date.toDay();
            update new Rental_Apply_Equipment_Set__c[]{es1,es2};
        }

        if (haveBack == true) {
            esd3.Arrival_in_wh__c = true;
            esd4.Arrival_in_wh__c = true;
        }

        if (que == false) {
            esd1.Queue_Number__c = 0;
            esd2.Queue_Number__c = 0;
            esd3.Queue_Number__c = 0;
            esd4.Queue_Number__c = 0;
        }

        insert new Rental_Apply_Equipment_Set_Detail__c[]{esd1,esd2,esd3,esd4};
        // 资产
        Asset asset1 = new Asset(Asset_Owner__c = 'Olympus');
        asset1.RecordTypeId = System.Label.Asset_RecordType;
        asset1.SerialNumber = 'ass01';
        asset1.Name = 'ass01';
        asset1.AccountId = dep.Id;
        asset1.Department_Class__c = strategicDep[0].Id;
        asset1.Hospital__c = hospital.Id;
        asset1.Product2Id = pro1.Id;
        asset1.Quantity = 10;
        asset1.Status = '有库存';
        asset1.Manage_type__c = '个体管理';
        insert asset1;
        asset1.Last_Reserve_RAES_Detail__c = esd1.Id;
        asset1.Out_of_wh__c = 3;
        update asset1;
        if (haveLoaner == true) {
            esd3.Asset__c = asset1.Id;
            esd1.Asset__c = asset1.Id;
        }
        update esd3;
        update esd1;
    }

    static testMethod void testdelRND() {
        setData(2, 1, 2, 2, false, false, true);
        //创建收货清单
    	Date td = Date.today();
        ReceivingNote__c rn = new ReceivingNote__c();
        rn.Internal_asset_location__c = '上海 张江';
        rn.Asset_loaner_category__c = '固定资产';
        rn.Name = 'ZZ-153PA-0000813';
        rn.Status__c = '待数据确认';
        rn.DateOfDelivery__c = td+7;
        rn.OrderCode__c = 'SHRC-SP-20044-02';
        rn.Sales_order_number__c = '1000109900';
        rn.ApprovalNumber__c = 'ME153P07-0301';
        rn.ContactPerson__c = 'username';
        insert rn;
        //收货清单一览
        ReceivingNoteSummary__c rns = new ReceivingNoteSummary__c();
        rns.ReceivingNote__c = rn.Id;
        insert rns;

        // 收货一览明细
        ReceivingNoteDetail__c rnd1 = new ReceivingNoteDetail__c();
        rnd1.ReceivingNoteSummary__c = rns.Id;
        rnd1.Fixture_Set_Detail__c = [select Id from Fixture_Set_Detail__c limit 1].Id;
        rnd1.Is_Body__c = true;
        rnd1.Delete_Flag__c = false;
        rnd1.SerialNumber__c = '12';

        insert new ReceivingNoteDetail__c[] {rnd1};
        List<ReceivingNoteSummary__c> rnsList = new List<ReceivingNoteSummary__c>();
        rnsList.add(rns); 
        Test.startTest();
        List<String> ids1=new List<String>();
        ids1.add(rn.id);
        List<String> ids2=new List<String>();
        ids1.add(rns.id);
        List<String> ids3=new List<String>();
        ids1.add(rnd1.id);
        List<String> ids4=new List<String>();
        ids1.add(idX);
        List<String> ids5=new List<String>();
        ids1.add(idY);
        NFM301SendEmail c=new NFM301SendEmail();
        NFM301SendEmail.sendEmailOnNFM301(ids1,ids2,ids3,ids4,ids5);
        NFM301SendEmail.sendTemperatureHumidityEmailOnNFM301('ttt');
        Test.stopTest();
    }
}