@isTest
private class OPDPlanMarkBatchTest {
	
	static Date thismonth_Lastday ;
	static Date lastmonth_Lastday ;
	
	static void  basicData() {
		// カスタム設定を定義
        SS_Batch_Column_Mapping__c opdMapping = new SS_Batch_Column_Mapping__c(Name = 'OPDPlan__c', SS_TableName__c='SS_OPDPlan__c');
        opdMapping.From_Column_001__c = 'Id';
        opdMapping.SS_Column_001__c = 'OPDPlan__c.Name';
        opdMapping.From_Column_002__c = 'Name';
        opdMapping.SS_Column_002__c = 'Name';
        insert new SS_Batch_Column_Mapping__c[] {opdMapping};

        SS_BatchColumnMapping__c opdMapping2 = new SS_BatchColumnMapping__c(Name = 'OPDPlan__c', SS_TableName__c='SS_OPDPlan__c');
        opdMapping2.From_Column_001__c = 'Id';
        opdMapping2.SS_Column_001__c = 'OPDPlan__c.Name';
        opdMapping2.From_Column_002__c = 'Name';
        opdMapping2.SS_Column_002__c = 'Name';
        insert new SS_BatchColumnMapping__c[] {opdMapping2};


		Date toDate = Date.today();
		//本月的第一天
		Date mon1stDate = Date.newInstance(toDate.year(), toDate.month(), 1);
		//本月的最后一天
		thismonth_Lastday = Date.toDay().addMonths(1).toStartOfMonth().addDays(-1);
		System.debug('thismonth_Lastday6666' + thismonth_Lastday);
		//上月的第一天
		Date lastmonth_firstday = Date.toDay().addMonths(-1).toStartOfMonth();
		//上月的最后一天
		lastmonth_Lastday = Date.newInstance(toDate.year(), toDate.month(), 1).addDays(-1);
		System.debug('lastmonth_Lastday1234' + lastmonth_Lastday);
		//下月的第一天
		Date nextmonth_firstday = Date.toDay().addMonths(1).toStartOfMonth();
		//下月的最后一天
		Date nextmonth_Lastday = Date.toDay().addMonths(2).toStartOfMonth().addDays(-1);

		OPDPlan__c oPDPlan0 = new OPDPlan__c();
		oPDPlan0.Status__c = '计划中';
		oPDPlan0.OPDPlan_ImplementDate__c = Date.toDay();//mon1stDate;
		oPDPlan0.NoOpp_Reason__c = 'HCP对应';   //2020-11-04  mzy  SWAG-BSC5WP   add
		oPDPlan0.OPDType__c = '学会'; //2020-11-19  mzy   SWAG-BSC5WP add 
		insert oPDPlan0;

		OPDPlan__c oPDPlan1 = new OPDPlan__c();
		oPDPlan1.Status__c = '计划中';
		oPDPlan1.OPDPlan_ImplementDate__c = thismonth_Lastday;
		oPDPlan1.NoOpp_Reason__c = 'HCP对应';   //2020-11-04  mzy  SWAG-BSC5WP   add
		oPDPlan1.OPDType__c = '学会'; //2020-11-19  mzy   SWAG-BSC5WP add 
		insert oPDPlan1;

		OPDPlan__c oPDPlan2 = new OPDPlan__c();
		oPDPlan2.Status__c = '计划中';
		oPDPlan2.OPDPlan_ImplementDate__c = nextmonth_firstday;
		oPDPlan2.NoOpp_Reason__c = 'HCP对应';   //2020-11-04  mzy  SWAG-BSC5WP   add
		oPDPlan2.OPDType__c = '学会'; //2020-11-19  mzy   SWAG-BSC5WP add 
		insert oPDPlan2;

		OPDPlan__c oPDPlan3 = new OPDPlan__c();
		oPDPlan3.Status__c = '计划中';
		oPDPlan3.OPDPlan_ImplementDate__c = nextmonth_Lastday;
		oPDPlan3.NoOpp_Reason__c = 'HCP对应';   //2020-11-04  mzy  SWAG-BSC5WP   add
		oPDPlan3.OPDType__c = '学会'; //2020-11-19  mzy   SWAG-BSC5WP add 
		insert oPDPlan3;

        OPDPlan__c oPDPlan4 = new OPDPlan__c();
		oPDPlan4.Status__c = '计划中';
		oPDPlan4.OPDPlan_ImplementDate__c = Date.today();//lastmonth_firstday;
		oPDPlan4.NoOpp_Reason__c = 'HCP对应';    //2020-11-04  mzy  SWAG-BSC5WP   add
		oPDPlan4.OPDType__c = '学会'; //2020-11-19  mzy   SWAG-BSC5WP add 
		insert oPDPlan4;
		
		OPDPlan__c oPDPlan5 = new OPDPlan__c();
		oPDPlan5.Status__c = '计划中';
		oPDPlan5.OPDPlan_ImplementDate__c = Date.today();//lastmonth_Lastday;
		oPDPlan5.NoOpp_Reason__c = 'HCP对应';    //2020-11-04  mzy  SWAG-BSC5WP   add
		oPDPlan5.OPDType__c = '学会'; //2020-11-19  mzy   SWAG-BSC5WP add 
		insert oPDPlan5;
	}
	//正常执行
	@isTest 
	static void test_method_one() {

		basicData();
        Id execBTId = null;
        System.Test.StartTest();
        List<OPDPlan__c> oPDPlans = [Select Id,Name,Status__c,lastMonth_Plan__c,ThisMonthPlan__c,If_ThisMonth_Plan__c 
        								from OPDPlan__c
        								 Where (( lastMonth_Plan__c != 0 and ThisMonthPlan__c = 0) or 
        								 	 ( lastMonth_Plan__c = 0 and ThisMonthPlan__c != 0) or 
        								 	 ( ThisMonthPlan__c = 0 and If_ThisMonth_Plan__c != 0) or 
        								 	 ( ThisMonthPlan__c != 0 and If_ThisMonth_Plan__c = 0))
        								 	 and OPDPlan_ImplementDate__c <= :thismonth_Lastday];
        System.assertEquals(4, oPDPlans.size());
        execBTId = Database.executeBatch(new OPDPlanMarkBatch());
        
        System.Test.StopTest();
		
	}
	////指定日期执行
	//@isTest 
	//static void test_method_two() {
	//	basicData();
 //       Id execBTId = null;
 //       List<OPDPlan__c> oPDPlans = [Select Id,Name,Status__c,lastMonth_Plan__c,ThisMonthPlan__c,If_ThisMonth_Plan__c 
 //       								from OPDPlan__c
 //       								 Where (( lastMonth_Plan__c != 0 and ThisMonthPlan__c = 0) or 
 //       								 	 ( lastMonth_Plan__c = 0 and ThisMonthPlan__c != 0) or 
 //       								 	 ( ThisMonthPlan__c = 0 and If_ThisMonth_Plan__c != 0) or 
 //       								 	 ( ThisMonthPlan__c != 0 and If_ThisMonth_Plan__c = 0) or 
 //       								 	 ( lastMonth_Execute__c = 0 and ThisMonthExecute__c != 0) or
 //       								 	 ( lastMonth_Execute__c != 0 and ThisMonthExecute__c = 0)) 
 //       								 	 and OPDPlan_ImplementDate__c <= :lastmonth_Lastday];
 //       System.assertEquals(2, oPDPlans.size());
 //       System.Test.StartTest();
 //       execBTId = Database.executeBatch(new OPDPlanMarkBatch(String.valueOf(lastmonth_Lastday)));
 //       System.Test.StopTest();
	//}
	//指定"OPD计划"的Id执行
	@isTest 
	static void test_method_three() {
		basicData();
		List<String> oPDPlanIds = new List<String>();
		List<OPDPlan__c> oDPlans = [ select Id,Name from OPDPlan__c];
		System.assertEquals(6, oDPlans.size());
		for (OPDPlan__c opdPlan : oDPlans) {
			oPDPlanIds.add(opdPlan.Id);
		}
        Id execBTId = null;
        System.Test.StartTest();
        execBTId = Database.executeBatch(new OPDPlanMarkBatch(oPDPlanIds));
        System.Test.StopTest();
	}
	
}