@isTest
private class EquipmentLoanerApplyControllerTest
{
    static Product2 prd1;
    static Product2 prd2;
    static Account acc;
    static Account dealer;
    static Contact core;
    static User testUser;
    static loaner_Set__c loanerSet;

    //转借元代理商
    static Account leaddealer;
    static void setupTestData01() {
        OlympusCalendar__c oc0 = new OlympusCalendar__c(Date__c = Date.today(), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc1 = new OlympusCalendar__c(Date__c = Date.today().addDays(1), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc2 = new OlympusCalendar__c(Date__c = Date.today().addDays(2), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc3 = new OlympusCalendar__c(Date__c = Date.today().addDays(3), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc4 = new OlympusCalendar__c(Date__c = Date.today().addDays(4), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc5 = new OlympusCalendar__c(Date__c = Date.today().addDays(5), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc6 = new OlympusCalendar__c(Date__c = Date.today().addDays(6), ChangeToHoliday__c=true, ChangeToWorkday__c=false);
        OlympusCalendar__c oc7 = new OlympusCalendar__c(Date__c = Date.today().addDays(7), ChangeToHoliday__c=true, ChangeToWorkday__c=false);
        OlympusCalendar__c oc8 = new OlympusCalendar__c(Date__c = Date.today().addDays(8), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc9 = new OlympusCalendar__c(Date__c = Date.today().addDays(9), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc10 = new OlympusCalendar__c(Date__c = Date.today().addDays(10), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc11 = new OlympusCalendar__c(Date__c = Date.today().addDays(11), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc12 = new OlympusCalendar__c(Date__c = Date.today().addDays(12), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc13 = new OlympusCalendar__c(Date__c = Date.today().addDays(-1), ChangeToHoliday__c=true, ChangeToWorkday__c=false);
        OlympusCalendar__c oc14 = new OlympusCalendar__c(Date__c = Date.today().addDays(-2), ChangeToHoliday__c=true, ChangeToWorkday__c=false);
        OlympusCalendar__c oc15 = new OlympusCalendar__c(Date__c = Date.today().addDays(-3), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc16 = new OlympusCalendar__c(Date__c = Date.today().addDays(-4), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc17 = new OlympusCalendar__c(Date__c = Date.today().addDays(-5), ChangeToHoliday__c=false, ChangeToWorkday__c=true);

        insert new OlympusCalendar__c[] {oc0,oc1,oc2,oc3,oc4,oc5,oc6,oc7,oc8,oc9,oc10,oc11,oc12,oc13,oc14,oc15,oc16,oc17};
    }
	static void setupTestData() {
        setupTestData01();
		List<RecordType> dealerIE = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Dealer'];
        if (dealerIE.size() == 0) {
            return;
        }
        List<RecordType> olyCompany = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'OlympusCompany'];
        if (olyCompany.size() == 0) {
            return;
        }

        List<RecordType> loa_individual = [select Id from RecordType where IsActive = true and SobjectType = 'Asset' and Name = '样机_个体管理'];
        if (loa_individual.size() == 0) {
            return;
        }
        List<RecordType> loa_number = [select Id from RecordType where IsActive = true and SobjectType = 'Asset' and Name = '样机_数量管理'];
        if (loa_number.size() == 0) {
            return;
        }

        //代理商用户
        List<RecordType> rectDealer = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Dealer'];
        Profile prof = [select Id from Profile where Name ='SSBG DEALER IE'];
        dealer = new Account();
        dealer.Name = 'test dealer';
        dealer.RecordTypeId = rectDealer[0].Id;
        dealer.ProductSegment__c = 'IE';
        dealer.PostCode__c='000000';
        insert dealer;
        core = new Contact(email='jplumber@salesforce.com', firstname='Joe',lastname='Plumber',accountid=dealer.id);
        insert core;
        testUser = New User(ContactId = core.id,Alias = 'newUser',Email='newuser@testorg.com',EmailEncodingKey='UTF-8', LastName='TestUser', LanguageLocaleKey='zh_CN',LocaleSidKey='zh_CN', ProfileId = prof.Id,TimeZoneSidKey='Asia/Shanghai', UserName='testUser01@prectech.com');
        insert testUser;

        //样机客户
        acc = new Account();
        acc.Name = 'test dealer';
        acc.RecordTypeId = olyCompany[0].Id;
        acc.ProductSegment__c = 'IE';
        acc.PostCode__c='000000';
        insert acc;

        loanerSet = new loaner_Set__c();
        loanerSet.Equipment_Type__c = 'IE';
        loanerSet.Set_No__c = 'IETEST0001';
        loanerSet.Name = 'IETEST0001';

        insert loanerSet;

		// 产品
        prd1 = new Product2();
        prd1.Product_ECCode__c      = 'testSBG001';
        prd1.ProductCode            = 'testSBG001';
        prd1.Name                   = 'testSBG001';
        prd1.IsActive               = true;

        prd2 = new Product2();
        prd2.Product_ECCode__c      = 'testSBG002';
        prd2.ProductCode            = 'testSBG002';
        prd2.Name                   = 'testSBG002';
        prd2.IsActive               = true;
        insert new Product2[] {prd1, prd2};

        // 保有设备 (数量管理)
        Asset asset = new Asset();
        asset.RecordTypeId = loa_number[0].Id;
        asset.SerialNumber = 'asset';
        asset.Name = 'asset';
        asset.AccountId = acc.Id;
        asset.Product2Id = prd2.Id;
        asset.Quantity = 1;
        asset.Status = '在库';
        asset.Equipment_Type__c = 'IE';
        asset.loaner_place__c = '广州办';
        asset.total_number__c = 20;
        asset.Remarks2__c = 'asset';

        // 保有设备 (个体管理)
        Asset assetC1 = new Asset();
        assetC1.RecordTypeId = loa_individual[0].Id;
        assetC1.SerialNumber = 'assetC1';
        assetC1.Name = 'assetC1';
        assetC1.AccountId = acc.Id;
        assetC1.Product2Id = prd1.Id;
        assetC1.Quantity = 1;
        assetC1.Status = '在库';
        assetC1.Equipment_Type__c = 'IE';
        assetC1.loaner_place__c = '北京酒仙桥';
        assetC1.Remarks2__c = 'assetC1';
        
        // 保有设备 (个体管理)
        Asset assetC2 = new Asset();
        assetC2.RecordTypeId = loa_individual[0].Id;
        assetC2.SerialNumber = 'assetC2';
        assetC2.Name = 'assetC2';
        assetC2.AccountId = acc.Id;
        assetC2.Product2Id = prd1.Id;
        assetC2.Quantity = 1;
        assetC2.Status = '在库';
        assetC2.Equipment_Type__c = 'IE';
        assetC2.loaner_place__c = '北京酒仙桥';
        insert new Asset[] {asset,assetC1};

        Loaner_Set_Detail__c lSD1 = new Loaner_Set_Detail__c();
        lSD1.Loaner_Set__c = loanerSet.Id;
        lSD1.Asset__c = asset.Id;
        Loaner_Set_Detail__c lSD2 = new Loaner_Set_Detail__c();
        lSD2.Loaner_Set__c = loanerSet.Id;
        lSD2.Asset__c = assetC1.Id;
        insert new Loaner_Set_Detail__c[] {lSD1,lSD2};


        //转借代理商信息
        List<RecordType> rectLapp = [select Id from RecordType where IsActive = true and SobjectType = 'loaner_application__c' and Name = 'IE'];
        if (rectLapp.size() == 0) {
            return;
        }
        leaddealer = new Account();
        leaddealer.Name = 'lead dealer';
        leaddealer.RecordTypeId = rectDealer[0].Id;
        leaddealer.ProductSegment__c = 'IE';
        leaddealer.PostCode__c='000000';
        insert leaddealer;
        Contact leadcore = new Contact(email='jplumber@salesforce.com', firstname='Joe',lastname='Plumber',accountid=leaddealer.id);
        insert leadcore;
        User leadUser = New User(ContactId = leadcore.id,Alias = 'newUser',Email='newuser@testorg.com',EmailEncodingKey='UTF-8', LastName='TestUser', LanguageLocaleKey='zh_CN',LocaleSidKey='zh_CN', ProfileId = prof.Id,TimeZoneSidKey='Asia/Shanghai', UserName='testUser02@prectech.com');
        insert leadUser;

        loaner_application__c loanerApp = new loaner_application__c();
        loanerApp.Name = 'TESTYJ0002';
        loanerApp.Approval_Step__c = 2;
        loanerApp.Status__c = '申请者已收货';
        loanerApp.Loaner_Ser__c = 'test';
        loanerApp.Equipment_Type__c = 'IE';
        loanerApp.RecordTypeId = rectLapp[0].Id;
        loanerApp.Demo_purpose__c = '演示';
        loanerApp.Request_shipping_Date__c = Date.today().addDays(1);
        loanerApp.Request_return_Date__c = Date.today().addDays(5);
        loanerApp.ownerId = leadUser.Id;
        insert loanerApp;

        loaner_application_detail__c loanerAppDet = new loaner_application_detail__c();
        loanerAppDet.RAESD_Status__c = '申请者已收货';
        loanerAppDet.Name = 'TESTYJ0002-01';
        loanerAppDet.loaner_application__c = loanerApp.Id;
        loanerAppDet.LOANER__c = asset.Id;
        loanerAppDet.Allocate_Date__c = Date.today();
        insert loanerAppDet;

	}

    //IE 样机分配
    static testMethod void leadWithIE(){
        setupTestData();

        List<RecordType> rectLapp = [select Id from RecordType where IsActive = true and SobjectType = 'loaner_application__c' and Name = 'IE'];
        if (rectLapp.size() == 0) {
            return;
        }
        loaner_application__c loanerApp = new loaner_application__c();
        loanerApp.Name = 'TESTYJ0001';
        loanerApp.Approval_Step__c = 1;
        loanerApp.Status__c = '申请中';
        loanerApp.Loaner_Ser__c = 'test';
        loanerApp.Equipment_Type__c = 'IE';
        loanerApp.RecordTypeId = rectLapp[0].Id;
        loanerApp.Demo_purpose__c = '演示';
        loanerApp.Request_shipping_Date__c = Date.today().addDays(1);
        loanerApp.Request_return_Date__c = Date.today().addDays(5);
        loanerApp.ownerId = testUser.Id;
       loanerApp.pickup_time__c = Date.today();
        insert loanerApp;

        List<loaner_application__c> loanerAppList = [select Id from loaner_application__c where Id = :loanerApp.Id];
        System.assertEquals(1, loanerAppList.size());
        PageReference page = new PageReference('/apex/EquipmentLoanerApply?loid=' + loanerApp.Id);
        page.setRedirect(true);
        System.Test.setCurrentPage(page);
        EquipmentLoanerApplyController loanerApplyCon = new EquipmentLoanerApplyController();
        loanerApplyCon.init();
        loanerApplyCon.rec.Equipment_Type__c = 'IE';
        loanerApplyCon.rec.Status = '在库';
        loanerApplyCon.rec.SerialNumber = 'asset';
        loanerApplyCon.rec.Name = 'asset';
        loanerApplyCon.searchLoanerApp();
        System.assertEquals(2, loanerApplyCon.equipmentSetRecords.size());
        System.assertEquals('asset', loanerApplyCon.equipmentSetRecords[0].aset.Name);
        loanerApplyCon.equipmentSetRecords[0].check = true;
        loanerApplyCon.equipmentSetRecords[0].deatilNo = 2;
        loanerApplyCon.equipmentSetRecords[1].check = true;
        //loanerApplyCon.equipmentSetRecords[1].deatilNo = 2;
        loanerApplyCon.save();

        List<loaner_application_detail__c> loanerAppDetList = [select Id from loaner_application_detail__c
                            where loaner_application__c = :loanerApp.Id];
        System.assertEquals(3, loanerAppDetList.size());
        loanerApplyCon.sortTable();




        // When


        // Then

    }

    //IE 样机分配
    static testMethod void IEdeatilNoTest (){
        setupTestData();

        List<RecordType> rectLapp = [select Id from RecordType where IsActive = true and SobjectType = 'loaner_application__c' and Name = 'IE'];
        if (rectLapp.size() == 0) {
            return;
        }
        loaner_application__c loanerApp = new loaner_application__c();
        loanerApp.Name = 'TESTYJ0001';
        loanerApp.Approval_Step__c = 1;
        loanerApp.Status__c = '申请中';
        loanerApp.Loaner_Ser__c = 'test';
        loanerApp.Equipment_Type__c = 'IE';
        loanerApp.RecordTypeId = rectLapp[0].Id;
        loanerApp.Demo_purpose__c = '演示';
        loanerApp.Request_shipping_Date__c = Date.today().addDays(1);
        loanerApp.Request_return_Date__c = Date.today().addDays(5);
        loanerApp.ownerId = testUser.Id;
        loanerApp.pickup_time__c = Date.today();
        insert loanerApp;

        List<loaner_application__c> loanerAppList = [select Id from loaner_application__c where Id = :loanerApp.Id];
        System.assertEquals(1, loanerAppList.size());
        PageReference page = new PageReference('/apex/EquipmentLoanerApply?loid=' + loanerApp.Id);
        page.setRedirect(true);
        System.Test.setCurrentPage(page);
        EquipmentLoanerApplyController loanerApplyCon = new EquipmentLoanerApplyController();
        loanerApplyCon.init();
        loanerApplyCon.rec.Equipment_Type__c = 'IE';
        loanerApplyCon.rec.Status = '在库';
        loanerApplyCon.rec.SerialNumber = 'asset';
        loanerApplyCon.rec.Name = 'asset';
        loanerApplyCon.searchLoanerApp();
        System.assertEquals(2, loanerApplyCon.equipmentSetRecords.size());
        System.assertEquals('asset', loanerApplyCon.equipmentSetRecords[0].aset.Name);
        loanerApplyCon.equipmentSetRecords[0].check = true;
        loanerApplyCon.equipmentSetRecords[1].check = true;
        loanerApplyCon.save();

        List<loaner_application_detail__c> loanerAppDetList = [select Id from loaner_application_detail__c
                            where loaner_application__c = :loanerApp.Id];
        System.assertEquals(2, loanerAppDetList.size());
        loanerApplyCon.sortTable();
        loanerApplyCon.sortTable();

    }
    //IE 样机套装
    static testMethod void loanerSetTest(){
        setupTestData();

        List<RecordType> rectLapp = [select Id from RecordType where IsActive = true and SobjectType = 'loaner_application__c' and Name = 'IE'];
        if (rectLapp.size() == 0) {
            return;
        }
        List<RecordType> loa_individual = [select Id from RecordType where IsActive = true and SobjectType = 'Asset' and Name = '样机_个体管理'];
        if (loa_individual.size() == 0) {
            return;
        }
        // 保有设备 (个体管理)
        Asset assetC2 = new Asset();
        assetC2.RecordTypeId = loa_individual[0].Id;
        assetC2.SerialNumber = 'assetC2';
        assetC2.Name = 'assetC2';
        assetC2.AccountId = acc.Id;
        assetC2.Product2Id = prd1.Id;
        assetC2.Quantity = 1;
        assetC2.Status = '在库';
        assetC2.Equipment_Type__c = 'IE';
        assetC2.loaner_place__c = '北京酒仙桥';
        assetC2.Remarks2__c = 'assetC2';
        insert assetC2;

        loaner_application__c loanerApp = new loaner_application__c();
        loanerApp.Name = 'TESTYJ0001';
        loanerApp.Approval_Step__c = 1;
        loanerApp.Status__c = '申请中';
        loanerApp.Loaner_Ser__c = 'test';
        loanerApp.Equipment_Type__c = 'IE';
        loanerApp.RecordTypeId = rectLapp[0].Id;
        loanerApp.Demo_purpose__c = '演示';
        loanerApp.Request_shipping_Date__c = Date.today().addDays(1);
        loanerApp.Request_return_Date__c = Date.today().addDays(5);
        loanerApp.ownerId = testUser.Id;
        loanerApp.pickup_time__c = Date.today();
        insert loanerApp;

        loaner_application_detail__c loanerAppDet = new loaner_application_detail__c();
        loanerAppDet.RAESD_Status__c = '已分配';
        loanerAppDet.Name = 'TESTYJ0001-01';
        loanerAppDet.loaner_application__c = loanerApp.Id;
        loanerAppDet.LOANER__c = assetC2.Id;
        loanerAppDet.Allocate_Date__c = Date.today();
        insert loanerAppDet;
        List<loaner_application__c> loanerAppList = [select Id from loaner_application__c where Id = :loanerApp.Id];
        System.assertEquals(1, loanerAppList.size());
        PageReference page = new PageReference('/apex/EquipmentLoanerApply?loid=' + loanerApp.Id);
        page.setRedirect(true);
        System.Test.setCurrentPage(page);
        EquipmentLoanerApplyController loanerApplyCon = new EquipmentLoanerApplyController();
        loanerApplyCon.init();
        loanerApplyCon.rec.Equipment_Type__c = 'IE';
        loanerApplyCon.rec.Status = '在库';
        loanerApplyCon.rec.SerialNumber = 'asset';
        loanerApplyCon.rec.Name = 'asset';
        loanerApplyCon.searchLoanerApp();
        System.assertEquals(3, loanerApplyCon.equipmentSetRecords.size());

        loanerApplyCon.setId = loanerSet.Id;
        loanerApplyCon.getLoanerSetdet();
        System.assertEquals(3, loanerApplyCon.equipmentSetRecords.size());
        loanerApplyCon.sortTable();
        loanerApplyCon.getcanSave();
        loanerApplyCon.getcanSearch();
        loanerApplyCon.cancel();

        loanerApplyCon.rec.Equipment_Type__c = 'IE';
        loanerApplyCon.rec.Status = '在库';
        loanerApplyCon.rec.SerialNumber = 'asset';
        loanerApplyCon.rec.Name = 'asset';
        loanerApplyCon.assECcode = 'test';
        loanerApplyCon.rec.loaner_place__c = 'test';
        loanerApplyCon.assName = 'test';
        loanerApplyCon.searchLoanerApp();
    }

    //IE 转借申请
    static testMethod void leadOrderTest(){
        setupTestData();

        List<RecordType> rectLapp = [select Id from RecordType where IsActive = true and SobjectType = 'loaner_application__c' and Name = 'IE'];
        if (rectLapp.size() == 0) {
            return;
        }
        List<RecordType> loa_individual = [select Id from RecordType where IsActive = true and SobjectType = 'Asset' and Name = '样机_个体管理'];
        if (loa_individual.size() == 0) {
            return;
        }
        // 保有设备 (个体管理)
        Asset assetC2 = new Asset();
        assetC2.RecordTypeId = loa_individual[0].Id;
        assetC2.SerialNumber = 'assetC2';
        assetC2.Name = 'assetC2';
        assetC2.AccountId = acc.Id;
        assetC2.Product2Id = prd1.Id;
        assetC2.Quantity = 1;
        assetC2.Status = '在库';
        assetC2.Equipment_Type__c = 'IE';
        assetC2.loaner_place__c = '北京酒仙桥';
        assetC2.Remarks2__c = 'assetC2';
        insert assetC2;

        loaner_application__c loanerApp = new loaner_application__c();
        loanerApp.Name = 'TESTYJ0001';
        loanerApp.Approval_Step__c = 1;
        loanerApp.Status__c = '申请中';
        loanerApp.Loaner_Ser__c = 'test';
        loanerApp.Equipment_Type__c = 'IE';
        loanerApp.RecordTypeId = rectLapp[0].Id;
        loanerApp.Demo_purpose__c = '演示';
        loanerApp.Request_shipping_Date__c = Date.today().addDays(1);
        loanerApp.Request_return_Date__c = Date.today().addDays(5);
        loanerApp.ownerId = testUser.Id;
        loanerApp.Loaner_LendOrder__c = true;
        loanerApp.Dealer_From_txt__c = 'test';
        loanerApp.DealerOrder_From__c = leaddealer.Id;
        loanerApp.ProductNameNum1__c = prd2.Id;
        loanerApp.pickup_time__c = Date.today();
        insert loanerApp;
        List<loaner_application__c> loanerAppList = [select Id from loaner_application__c where Id = :loanerApp.Id];
        System.assertEquals(1, loanerAppList.size());
        PageReference page = new PageReference('/apex/EquipmentLoanerApply?loid=' + loanerApp.Id);
        page.setRedirect(true);
        System.Test.setCurrentPage(page);
        EquipmentLoanerApplyController loanerApplyCon = new EquipmentLoanerApplyController();
        loanerApplyCon.init();
        System.assertEquals(1, loanerApplyCon.equipmentSetRecords.size());
        loanerApplyCon.equipmentSetRecords[0].check = true;
        loanerApplyCon.save();
    }

    //IE 转借申请
    static testMethod void bSOrderTest(){
        setupTestData01();
        List<RecordType> dealerIE = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Dealer'];
        if (dealerIE.size() == 0) {
            return;
        }
        List<RecordType> olyCompany = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'OlympusCompany'];
        if (olyCompany.size() == 0) {
            return;
        }

        List<RecordType> loa_individual = [select Id from RecordType where IsActive = true and SobjectType = 'Asset' and Name = '样机_个体管理'];
        if (loa_individual.size() == 0) {
            return;
        }
        List<RecordType> loa_number = [select Id from RecordType where IsActive = true and SobjectType = 'Asset' and Name = '样机_数量管理'];
        if (loa_number.size() == 0) {
            return;
        }
        List<RecordType> rectLapp = [select Id from RecordType where IsActive = true and SobjectType = 'loaner_application__c' and Name = 'BS'];
        if (rectLapp.size() == 0) {
            return;
        }

        //代理商用户
        List<RecordType> rectDealer = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Dealer'];
        Profile prof = [select Id from Profile where Name ='SSBG DEALER BS(only loanerEmail)'];
        dealer = new Account();
        dealer.Name = 'test dealer';
        dealer.RecordTypeId = rectDealer[0].Id;
        dealer.ProductSegment__c = 'BS';
        dealer.PostCode__c='000000';
        insert dealer;
        core = new Contact(email='jplumber@salesforce.com', firstname='Joe',lastname='Plumber',accountid=dealer.id);
        insert core;
        testUser = New User(ContactId = core.id,Alias = 'newUser',Email='newuser@testorg.com',EmailEncodingKey='UTF-8', LastName='TestUser', LanguageLocaleKey='zh_CN',LocaleSidKey='zh_CN', ProfileId = prof.Id,TimeZoneSidKey='Asia/Shanghai', UserName='testUser01@prectech.com');
        insert testUser;

        //样机客户
        acc = new Account();
        acc.Name = 'test dealer';
        acc.RecordTypeId = olyCompany[0].Id;
        acc.ProductSegment__c = 'BS';
        acc.PostCode__c='000000';
        insert acc;

        loanerSet = new loaner_Set__c();
        loanerSet.Equipment_Type__c = 'BS';
        loanerSet.Set_No__c = 'IETEST0001';
        loanerSet.Name = 'IETEST0001';

        insert loanerSet;

        // 产品
        prd1 = new Product2();
        prd1.Product_ECCode__c      = 'testSBG001';
        prd1.ProductCode            = 'testSBG001';
        prd1.Name                   = 'testSBG001';
        prd1.IsActive               = true;

        prd2 = new Product2();
        prd2.Product_ECCode__c      = 'testSBG002';
        prd2.ProductCode            = 'testSBG002';
        prd2.Name                   = 'testSBG002';
        prd2.IsActive               = true;
        insert new Product2[] {prd1, prd2};

        // 保有设备 (数量管理)
        Asset asset = new Asset();
        asset.RecordTypeId = loa_number[0].Id;
        asset.SerialNumber = 'asset';
        asset.Name = 'asset';
        asset.AccountId = acc.Id;
        asset.Product2Id = prd2.Id;
        asset.Quantity = 1;
        asset.Status = '在库';
        asset.Equipment_Type__c = 'BS';
        asset.loaner_place__c = '广州办';
        asset.total_number__c = 20;
        asset.Remarks2__c = 'asset';

        // 保有设备 (个体管理)
        Asset assetC1 = new Asset();
        assetC1.RecordTypeId = loa_individual[0].Id;
        assetC1.SerialNumber = 'assetC1';
        assetC1.Name = 'assetC1';
        assetC1.AccountId = acc.Id;
        assetC1.Product2Id = prd1.Id;
        assetC1.Quantity = 1;
        assetC1.Status = '在库';
        assetC1.Equipment_Type__c = 'BS';
        assetC1.loaner_place__c = '广州办';
        assetC1.Remarks2__c = 'assetC1';
        
        // 保有设备 (数量管理)
        Asset assetC2 = new Asset();
        assetC2.RecordTypeId = loa_number[0].Id;
        assetC2.SerialNumber = 'assetC2';
        assetC2.Name = 'assetC2';
        assetC2.AccountId = acc.Id;
        assetC2.Product2Id = prd2.Id;
        assetC2.Quantity = 1;
        assetC2.Status = '在库';
        assetC2.Equipment_Type__c = 'BS';
        assetC2.loaner_place__c = '广州办';
        assetC2.total_number__c = 20;
        assetC2.Remarks2__c = 'asset';
        insert new Asset[] {asset,assetC1,assetC2};

        Loaner_Set_Detail__c lSD1 = new Loaner_Set_Detail__c();
        lSD1.Loaner_Set__c = loanerSet.Id;
        lSD1.Asset__c = asset.Id;
        Loaner_Set_Detail__c lSD2 = new Loaner_Set_Detail__c();
        lSD2.Loaner_Set__c = loanerSet.Id;
        lSD2.Asset__c = assetC1.Id;
        insert new Loaner_Set_Detail__c[] {lSD1,lSD2};

        loaner_application__c loanerApp = new loaner_application__c();
        loanerApp.Name = 'TESTYJ0001';
        loanerApp.Approval_Step__c = 1;
        loanerApp.Status__c = '申请中';
        loanerApp.Loaner_Ser__c = 'test';
        loanerApp.Equipment_Type__c = 'BS';
        loanerApp.RecordTypeId = rectLapp[0].Id;
        loanerApp.Demo_purpose__c = '全国性展会';
        loanerApp.Request_shipping_Date__c = Date.today().addDays(1);
        loanerApp.Request_return_Date__c = Date.today().addDays(5);
        loanerApp.ownerId = testUser.Id;
        loanerApp.ProductNameNum1__c = prd2.Id;
       loanerApp.pickup_time__c = Date.today();
        insert loanerApp;
        List<loaner_application__c> loanerAppList = [select Id from loaner_application__c where Id = :loanerApp.Id];
        System.assertEquals(1, loanerAppList.size());
        system.runAs(testUser){
            PageReference page = new PageReference('/apex/EquipmentLoanerApply?loid=' + loanerApp.Id);
            page.setRedirect(true);
            System.Test.setCurrentPage(page);
            EquipmentLoanerApplyController loanerApplyCon = new EquipmentLoanerApplyController();
            loanerApplyCon.init();
            System.assertEquals(false, loanerApplyCon.isManager);
            loanerApplyCon.rec.Equipment_Type__c = 'BS';
            loanerApplyCon.rec.Status = '在库';
            loanerApplyCon.searchLoanerApp();
            System.assertEquals(3, loanerApplyCon.equipmentSetRecords.size());
            loanerApplyCon.equipmentSetRecords[0].check = true;
            if(loanerApplyCon.equipmentSetRecords[0].aset.RecordTypeId == System.label.Asset_Record_Type1)loanerApplyCon.equipmentSetRecords[0].deatilNo = 2;
            loanerApplyCon.equipmentSetRecords[1].check = true;
            if(loanerApplyCon.equipmentSetRecords[1].aset.RecordTypeId == System.label.Asset_Record_Type1)loanerApplyCon.equipmentSetRecords[0].deatilNo = 2;
            loanerApplyCon.equipmentSetRecords[2].check = true;
            loanerApplyCon.save();
        }
    }
}