/**
 * Created by T on 2020/5/19.
 */

@IsTest
private class MonthEndAutoJudgeBatchTest {

	/**
	 * 创建出当月所有日期
	 */
	@TestSetup
	static void makeData(){

		// 获取当前月份的第一天
		Date firstDayOfMonth = Date.today().toStartOfMonth();
		// 将月份推到下一个月
		Date nextMonth = Date.newInstance(firstDayOfMonth.year(), firstDayOfMonth.month() + 1, 1);
		// 获取当前月份最后一天
		System.debug('lastDay:' + nextMonth.addDays(-1));
		Date lastDayOfMonth = nextMonth.addDays(-1);

		List<OlympusCalendar__c> currentMonthDays = new List<OlympusCalendar__c>();

		for(Integer i = firstDayOfMonth.day(); i <= lastDayOfMonth.day(); i++){
			Date OlympusCalendarDate1 = Date.newInstance(firstDayOfMonth.year(),firstDayOfMonth.month(),i);
			OlympusCalendar__c oc1 = new OlympusCalendar__c(Date__c = OlympusCalendarDate1);
			currentMonthDays.add(oc1);
		}

		insert currentMonthDays;

	}

	static testMethod void test() {
		// 预测发货日  
		Date forecastDate = Date.today().addMonths(1);
		// 发货日
		Date delivery = forecastDate.addDays(-5);
		// 18.付全款日期
		Date depositInFullDay = Date.today().addMonths(-5);
		// 该数据测试的是: 有本月预测标记,但是当月未发货,需要删除本月发货预测标记,并且符合次月预测添加次月预测标记的情况
		Statu_Achievements__c sa1 = new Statu_Achievements__c(Name='测试数据1',Shipping_Scheduled_Date__c=forecastDate,
			DeliveryStatus__c='未交付',DeliveryDate__c=null,PaymentStatus__c='尾款支付',
			Monthly_forecast_shipping__c='本月发货预测',NM_forecast_shipping__c=null,
			UnshippedAmount__c=42529.0,ContractAmount__c=42529.0,Opportunity__c='00610000016seNJAAY',End_User_price__c=51000.0,
			InstallStatus__c='已提交',Deposit_In_Full_Day__c=depositInFullDay);


		// 预测发货日  
		forecastDate = Date.today();
		// 发货日
		delivery = forecastDate;
		// 18.付全款日期
		depositInFullDay = Date.today().addMonths(-5);

		// 该数据测试的是: 不是当月预测,但是当月已经发货,需要添加本月发货预测标记
		//				  并且有次月发货预测的标记但在当月全部发货,需要删除次月发货预测的情况
		Statu_Achievements__c sa2 = new Statu_Achievements__c(Name='测试数据2',Shipping_Scheduled_Date__c=forecastDate,
			DeliveryStatus__c='已完全交付',DeliveryDate__c=delivery,PaymentStatus__c='尾款支付',
			Monthly_forecast_shipping__c=null,NM_forecast_shipping__c='次月发货预测',
			UnshippedAmount__c=42529.0,ContractAmount__c=42529.0,Opportunity__c='00610000016seNJAAY',End_User_price__c=51000.0,
			InstallStatus__c='已提交',Deposit_In_Full_Day__c=depositInFullDay);
		insert new Statu_Achievements__c[]{sa1,sa2};


		System.Test.startTest();
		List<Id> ids = new List<Id>();
		ids.add(sa1.Id);
		ids.add(sa2.Id);
		Database.executeBatch(new MonthEndAutoJudgeBatch(ids));
		System.Test.stopTest();

		sa1 = [SELECT Id,Name,Monthly_forecast_shipping__c,NM_forecast_shipping__c,Shipping_Scheduled_Date__c FROM Statu_Achievements__c WHERE Id = :sa1.Id];
		sa2 = [SELECT Id,Name,Monthly_forecast_shipping__c,NM_forecast_shipping__c,Shipping_Scheduled_Date__c FROM Statu_Achievements__c WHERE Id = :sa2.Id];
		System.debug('sa1.Monthly_forecast_shipping__c'+sa1.Monthly_forecast_shipping__c);
		System.debug('sa1.NM_forecast_shipping__c'+sa1.NM_forecast_shipping__c);
		System.debug('sa2.Monthly_forecast_shipping__c'+sa2.Monthly_forecast_shipping__c);
		System.debug('sa2.NM_forecast_shipping__c'+sa2.NM_forecast_shipping__c);
		List<OpportunitySpecialApply__c> opps = [select id,name,Apply_Content__c,Achievements__c,
													NMApply_Content_c__c ,Status__c,Approval_Date__c
													from OpportunitySpecialApply__c
													 where Achievements__c = :sa1.Id 
													 or Achievements__c = :sa2.Id];
		System.debug('+++opps'+opps);
		// System.assertEquals(null, sa1.Monthly_forecast_shipping__c);
		// System.assertEquals('次月发货预测', sa1.NM_forecast_shipping__c);
		// System.assertEquals('本月发货预测', sa2.Monthly_forecast_shipping__c);
		// System.assertEquals(null, sa2.NM_forecast_shipping__c);
	}
}