@isTest
private class LoanerOrderStateTest {
	static void setupTestData() {
        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[] {oc1,oc2,oc3,oc4,oc5,oc6,oc7,oc8,oc9,oc10,oc11,oc12,oc13,oc14,oc15,oc16,oc17};
    }
	@isTest static void test_method_one() {
        setupTestData();
		// Implement test code
		List<RecordType> rectLo = [select Id from RecordType where IsActive = true and SobjectType = 'loaner_application__c' and Name = 'IE'];

        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer IE'];
        
        // 新建备品借出申请
        loaner_application__c loaner = new loaner_application__c();
        loaner.Name = 'TEST';
        loaner.RecordTypeId = rectLo[0].id;
        loaner.EC_CODE_1__C = 'DV2-CAMELEONS-CUBE';
        loaner.DEMO_PURPOSE__C = '演示';
        loaner.Request_shipping_Date__c = Date.today();
        loaner.Request_return_Date__c = Date.today();
        loaner.Status__c = '已批准';
        loaner.Return_Track_Company__c = '物流公司';
        //loaner.Return_Trake_Staff__c = '返品人';
        insert loaner;

        // 新建 客户
        Account acc = new Account();
        acc.RecordTypeId = rectCo[0].Id;
        acc.Name         = '客户テスト1';
        acc.DivisionName__c = 'DivisionName__c';
        acc.FacilityName__c = 'FacilityName__c';
        acc.PostCode__c='000000';
        insert acc;
         
        // 新建 资产
        List<Asset> In_asset = new List<Asset>();

        Asset asset = new Asset();
        asset.Name = 'テスト機器1';
        asset.AccountId = acc.Id;
        asset.SerialNumber = 'testserial';
        In_asset.add(asset);

        asset = new Asset();
        asset.Name = 'テスト機器2';
        asset.AccountId = acc.Id;
        asset.SerialNumber = 'testserial';
        In_asset.add(asset);

        asset = new Asset();
        asset.Name = 'テスト機器3';
        asset.AccountId = acc.Id;
        asset.SerialNumber = 'testserial';
        In_asset.add(asset);

        insert In_asset;

        // 新建 借出明细
        List<loaner_application_detail__c> In_detail = new List<loaner_application_detail__c>();
        loaner_application_detail__c detail = new loaner_application_detail__c();

        detail.loaner_application__c = loaner.Id;
        detail.loaner__c = In_asset[0].id;
        detail.name = '000';
        detail.RAESD_STATUS__C = '已批准';
        In_detail.add(detail);

        detail = new loaner_application_detail__c();
        detail.loaner_application__c = loaner.Id;
        detail.loaner__c = In_asset[1].id;
        detail.name = '111';
        detail.RAESD_STATUS__C = '已批准';
        In_detail.add(detail);

        detail = new loaner_application_detail__c();
        detail.loaner_application__c = loaner.Id;
        detail.loaner__c = In_asset[2].id;
        detail.name = '222';
        detail.RAESD_STATUS__C = '已批准';
        detail.Inspection_result_after__c = 'OK';
        In_detail.add(detail);

        insert In_detail;

        loaner.Status__c = LoanerOrderState.getOrderStater(loaner.Id);

        In_detail[0].RAESD_STATUS__C = '已出库指示';
        In_detail[1].RAESD_STATUS__C = '已出库指示';
        In_detail[2].RAESD_STATUS__C = '已出库指示';
        update In_detail;
        loaner.Status__c = LoanerOrderState.getOrderStater(loaner.Id);

        In_detail[0].RAESD_STATUS__C = '已下架';
        In_detail[1].RAESD_STATUS__C = '已下架';
        In_detail[2].RAESD_STATUS__C = '已下架';
        update In_detail;
		loaner.Status__c = LoanerOrderState.getOrderStater(loaner.Id);

		In_detail[0].RAESD_STATUS__C = '出库前已检测';
        In_detail[1].RAESD_STATUS__C = '出库前已检测';
        In_detail[2].RAESD_STATUS__C = '出库前已检测';
        update In_detail;
		loaner.Status__c = LoanerOrderState.getOrderStater(loaner.Id);
        
        
		In_detail[0].RAESD_STATUS__C = '已出库';
        In_detail[1].RAESD_STATUS__C = '已出库';
        In_detail[2].RAESD_STATUS__C = '已出库';
        update In_detail;
		loaner.Status__c = LoanerOrderState.getOrderStater(loaner.Id);

		In_detail[0].RAESD_STATUS__C = '申请者已收货';
        In_detail[1].RAESD_STATUS__C = '申请者已收货';
        In_detail[2].RAESD_STATUS__C = '申请者已收货';
        update In_detail;
		loaner.Status__c = LoanerOrderState.getOrderStater(loaner.Id);
		system.debug('213123'+loaner.Status__c);
        Test.startTest();

		In_detail[0].RAESD_STATUS__C = '申请者已装机确认';
        In_detail[1].RAESD_STATUS__C = '申请者已装机确认';
        In_detail[2].RAESD_STATUS__C = '申请者已装机确认';
        update In_detail;
		loaner.Status__c = LoanerOrderState.getOrderStater(loaner.Id);

		In_detail[0].RAESD_STATUS__C = '已回寄';
        In_detail[1].RAESD_STATUS__C = '已回寄';
        In_detail[2].RAESD_STATUS__C = '已回寄';
        update In_detail;
		loaner.Status__c = LoanerOrderState.getOrderStater(loaner.Id);

		In_detail[0].RAESD_STATUS__C = '欠品中';
        In_detail[1].RAESD_STATUS__C = '欠品中';
        In_detail[2].RAESD_STATUS__C = '欠品中';
        update In_detail;
        Test.stopTest();
		//loaner.Status__c = LoanerOrderState.getOrderStater(loaner.Id);

		//In_detail[0].RAESD_STATUS__C = '已回收';
  //      In_detail[1].RAESD_STATUS__C = '已回收';
  //      In_detail[2].RAESD_STATUS__C = '已回收';
  //      update In_detail;
		//loaner.Status__c = LoanerOrderState.getOrderStater(loaner.Id);





	}
	
	@isTest static void test_method_two() {
        setupTestData();
		// Implement test code
		List<RecordType> rectLo = [select Id from RecordType where IsActive = true and SobjectType = 'loaner_application__c' and Name = 'IE'];

        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer IE'];
        
        // 新建备品借出申请
        loaner_application__c loaner = new loaner_application__c();
        loaner.Name = 'TEST';
        loaner.RecordTypeId = rectLo[0].id;
        loaner.EC_CODE_1__C = 'DV2-CAMELEONS-CUBE';
        loaner.DEMO_PURPOSE__C = '演示';
        loaner.Request_shipping_Date__c = Date.today();
        loaner.Request_return_Date__c = Date.today();
        loaner.Status__c = '已批准';
        loaner.Return_Track_Company__c = '物流公司';
        //loaner.Return_Trake_Staff__c = '返品人';
        insert loaner;

        // 新建 客户
        Account acc = new Account();
        acc.RecordTypeId = rectCo[0].Id;
        acc.Name         = '客户テスト1';
        acc.DivisionName__c = 'DivisionName__c';
        acc.FacilityName__c = 'FacilityName__c';
        acc.PostCode__c='000000';
        insert acc;
         
        // 新建 资产
        List<Asset> In_asset = new List<Asset>();

        Asset asset = new Asset();
        asset.Name = 'テスト機器1';
        asset.AccountId = acc.Id;
        asset.SerialNumber = 'testserial';
        In_asset.add(asset);

        asset = new Asset();
        asset.Name = 'テスト機器2';
        asset.AccountId = acc.Id;
        asset.SerialNumber = 'testserial';
        In_asset.add(asset);

        asset = new Asset();
        asset.Name = 'テスト機器3';
        asset.AccountId = acc.Id;
        asset.SerialNumber = 'testserial';
        In_asset.add(asset);

        insert In_asset;

        // 新建 借出明细
        List<loaner_application_detail__c> In_detail = new List<loaner_application_detail__c>();
        loaner_application_detail__c detail = new loaner_application_detail__c();

        detail.loaner_application__c = loaner.Id;
        detail.loaner__c = In_asset[0].id;
        detail.name = '000';
        detail.RAESD_STATUS__C = '已批准';
        In_detail.add(detail);

        detail = new loaner_application_detail__c();
        detail.loaner_application__c = loaner.Id;
        detail.loaner__c = In_asset[1].id;
        detail.name = '111';
        detail.RAESD_STATUS__C = '已批准';
        In_detail.add(detail);

        detail = new loaner_application_detail__c();
        detail.loaner_application__c = loaner.Id;
        detail.loaner__c = In_asset[2].id;
        detail.name = '222';
        detail.RAESD_STATUS__C = '已批准';
        detail.Inspection_result_after__c = 'OK';
        In_detail.add(detail);

        insert In_detail;

        List<String> loanerId = new List<String>();
        loanerId.add(loaner.Id);
        Map<String,String> statu =LoanerOrderState.getOrderStater1(loanerId);
        loaner.Status__c =statu.get(loaner.Id);

        In_detail[0].RAESD_STATUS__C = '已出库指示';
        In_detail[1].RAESD_STATUS__C = '已出库指示';
        In_detail[2].RAESD_STATUS__C = '已出库指示';
        update In_detail;
        statu =LoanerOrderState.getOrderStater1(loanerId);
        loaner.Status__c =statu.get(loaner.Id);
        system.debug(loaner.Status__c);

        In_detail[0].RAESD_STATUS__C = '已下架';
        In_detail[1].RAESD_STATUS__C = '已下架';
        In_detail[2].RAESD_STATUS__C = '已下架';
        update In_detail;
        statu =LoanerOrderState.getOrderStater1(loanerId);
        loaner.Status__c =statu.get(loaner.Id);
        system.debug(loaner.Status__c);

        In_detail[0].RAESD_STATUS__C = '出库前已检测';
        In_detail[1].RAESD_STATUS__C = '出库前已检测';
        In_detail[2].RAESD_STATUS__C = '出库前已检测';
        update In_detail;
        statu =LoanerOrderState.getOrderStater1(loanerId);
        loaner.Status__c =statu.get(loaner.Id);
        system.debug(loaner.Status__c);

        In_detail[0].RAESD_STATUS__C = '已出库';
        In_detail[1].RAESD_STATUS__C = '已出库';
        In_detail[2].RAESD_STATUS__C = '已出库';
        update In_detail;
        statu =LoanerOrderState.getOrderStater1(loanerId);
        loaner.Status__c =statu.get(loaner.Id);
        system.debug(loaner.Status__c);

        In_detail[0].RAESD_STATUS__C = '申请者已收货';
        In_detail[1].RAESD_STATUS__C = '申请者已收货';
        In_detail[2].RAESD_STATUS__C = '已回寄';
        In_detail[2].Asset_Return_Date__c = Date.today();
        In_detail[2].Receipt_Status__c  = 'NG';
        update In_detail;
        statu =LoanerOrderState.getOrderStater1(loanerId);
        loaner.Status__c =statu.get(loaner.Id);
        system.debug(loaner.Status__c);

        In_detail[0].RAESD_STATUS__C = '申请者已装机确认';
        In_detail[1].RAESD_STATUS__C = '申请者已装机确认';
        In_detail[2].RAESD_STATUS__C = '申请者已装机确认';
        update In_detail;
        statu =LoanerOrderState.getOrderStater1(loanerId);
        loaner.Status__c =statu.get(loaner.Id);
        system.debug('===='+loaner.Status__c);

        In_detail[0].RAESD_STATUS__C = '已回寄';
        In_detail[1].RAESD_STATUS__C = '已回寄';
        In_detail[2].RAESD_STATUS__C = '已回寄';
        update In_detail;
        statu =LoanerOrderState.getOrderStater1(loanerId);
        loaner.Status__c =statu.get(loaner.Id);
        system.debug(loaner.Status__c);
         In_detail[0].RAESD_STATUS__C = '欠品中';
        In_detail[1].RAESD_STATUS__C = '欠品中';
        In_detail[2].RAESD_STATUS__C = '欠品中';
        update In_detail;
        statu =LoanerOrderState.getOrderStater1(loanerId);
        loaner.Status__c =statu.get(loaner.Id);
        system.debug(loaner.Status__c);
         In_detail[0].RAESD_STATUS__C = '已回收';
        In_detail[1].RAESD_STATUS__C = '已回收';
        In_detail[2].RAESD_STATUS__C = '已回收';
        update In_detail;
        statu =LoanerOrderState.getOrderStater1(loanerId);
        loaner.Status__c =statu.get(loaner.Id);
        system.debug(loaner.Status__c);
	}
}