/**
 * 国庆或者春节，最新预定归还日的计算需要跳过长假
 * 计算字段如下：
 *     条件延期天数  7天内未生成回寄日/3周之内未同意报价 等日期是否跨长假
 *     受条件延期影响的延期
 *     日期延期天数
 * 
 * */
global class RentalApplyFinalReplyDayBatch implements Database.Batchable<sObject>, Database.Stateful {
	public static List<String> statusList = new List<String>{
			'已分配', '已出库指示', '已下架', '出库前已检测', '已出库', '申请者已收货', '申请者收货NG', '医院已装机确认', '已回寄', '欠品中', '已回收', '回收后已CDS', '回收后检测NG', '回收后已检测'
	};
    public static List<String> statusListForRaId = new List<String>{
            '已分配', '已出库指示', '已下架', '出库前已检测', '已出库', '申请者已收货', '申请者收货NG', '医院已装机确认', '已回寄', '欠品中', '已回收', '回收后已CDS', '回收后检测NG', '回收后已检测', '已回库'
    };

	public String idTemp = null;

	public RentalApplyFinalReplyDayBatch(String id) {
		this.idTemp = id;
	}

	public RentalApplyFinalReplyDayBatch() {
		Integer i = 0;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
        i ++;
	}

	global Database.QueryLocator start(Database.BatchableContext bc) {
		String soql = 'SELECT Id,Received_Confirm__c, Loaner_received_time__c, Asset_return_day2__c, Loaner_received_day2__c,';
		soql += ' Rental_Apply__r.NewRepair_Repair_Shipped_Date__c, Demo_purpose2_T__c, Rental_Apply__r.Repair__r.Agreed_Date__c,';
		soql += ' Rental_Apply__r.Repair__r.Repair_Quotation_date__c, Condition_Seven_Days__c, Condition_X21_Days__c, ';
		soql += ' Condition_Final_Reply_Days__c, Delay_Final_Reply_Days__c, Shippment_loaner_time2__c, Rental_End_Date__c,';
		soql += ' Rental_Apply__r.Follow_UP_Opp__r.Shipping_Finished_Day_Func__c, Final_reply_day__c,';
		soql += ' Rental_Apply__r.RC_Ordered_Date__c, Bollow_Date_Add_10_WD__c, Rental_Apply__r.Repair__r.Repair_Shipped_Date__c,';
		soql += ' Rental_Apply__r.QIS_Cancel_Submit_day__c, Rental_Apply__r.QIS_Repair_Shipped_Date__c, Rental_Apply__r.QIS_ReplaceDeliveryDate__c,';
		soql += ' Rental_Apply__r.QIS_Reply_day__c, Rental_Apply__r.QIS_cancel_date__c, Rental_Apply__r.next_action_Text__c,';
		soql += ' Rental_Apply__r.Repair__r.Repair_Ordered_Date__c, Rental_Apply__r.Request_demo_day__c, Bollow_Date__c, Bollow_Date_Add_7_WD__c,';
		soql += ' Return_to_office_Final_reply_day_U_RC__c, RC_return_to_office__c, Repair_Agreed_Quotation_Text__c, Repair_Agreed_Date__c, Request_demo_time__c';
		soql += ' FROM Rental_Apply_Equipment_Set__c';
		soql += ' WHERE Rental_Apply__r.DataMigration_Flag__c = FALSE';
		// soql += ' AND Final_reply_day__c != null';
		soql += ' AND Repair_Status_Text__c != \'0.删除\'';
		soql += ' AND Repair_Status_Text__c != \'0.取消\'';
		soql += ' AND Rental_Apply__r.QIS_Cancel_Submit_day__c = null';
		soql += ' AND Rental_Apply__r.QIS_cancel_date__c = null';

		if (!String.isBlank(idTemp)) {
			soql += ' AND Rental_Apply__c =: idTemp';
            soql += ' AND RAES_Status__c IN: statusListForRaId';   
		} else {
            soql += ' AND RAES_Status__c IN: statusList';
        }

		return Database.getQueryLocator(soql);
	}

	global void execute(Database.BatchableContext BC, List<Rental_Apply_Equipment_Set__c> scope) {
        // 只有1月、2月、8月、9月、10月、12月才执行长假延期的逻辑
        Integer month = Date.today().month();
        if (month == 3 || month == 4 || month == 5 || month == 6 || month == 7 || month == 11) {
            return;
        }

		Date today = Date.today();
		Datetime now = DateTime.now();
		List<Rental_Apply_Equipment_Set__c> updList = new List<Rental_Apply_Equipment_Set__c>();

		Date startDate = null;
		Date endDate = null;
		startDate = Date.newInstance(today.year(), 1, 1);
		endDate = Date.newInstance(today.year()+1, 12, 31);
		List<OlympusCalendar__c> holidayList = [
				select Id,Date__c,Holiday_Days__c 
				  from OlympusCalendar__c 
				 where Date__c >= :startDate
				   and Date__c <= :endDate  
				   and IsHoliday__c = true
				 order by Date__c];
		Map<Date, Decimal> holidayMap = new Map<Date, Decimal>();
		for (OlympusCalendar__c oc : holidayList) {
			holidayMap.put(oc.Date__c, oc.Holiday_Days__c);
		}

		for (Rental_Apply_Equipment_Set__c raes : scope) {
			// 7天如果跨长假，需要加上长假的天数做判断
			Datetime dtLoanerreceivedtime = null;
			Integer conditionX7 = 0;
			if (raes.Received_Confirm__c == 'NG' && raes.Loaner_received_time__c != null) {
				conditionX7 = isHolidayCheck(raes.Loaner_received_day2__c, 7, holidayMap);
			}
			raes.Condition_Seven_Days__c = conditionX7;
			if (raes.Received_Confirm__c == 'NG' && conditionX7 != 0) {
				dtLoanerreceivedtime = raes.Loaner_received_time__c.addDays(7).addDays(conditionX7);
			} else if (raes.Received_Confirm__c == 'NG') {
				dtLoanerreceivedtime = raes.Loaner_received_time__c.addDays(7);
			}

			// 21天如果跨长假，需要加上长假的天数做判断
			Integer X21Temp = 21;
			Integer conditionX21 = 0;
			if (raes.Rental_Apply__r.Repair__r.Repair_Quotation_date__c != null) {
				conditionX21 = isHolidayCheck(raes.Rental_Apply__r.Repair__r.Repair_Quotation_date__c, 21, holidayMap);
			}
			raes.Condition_X21_Days__c = conditionX21;
			if (conditionX21 != 0) {
				X21Temp = 21 + conditionX21;
			}

			// 备品到货NG and 7天内未生成回寄日
			if (raes.Received_Confirm__c == 'NG' && (
				(raes.Asset_return_day2__c == null && dtLoanerreceivedtime < now)
				|| (raes.Asset_return_day2__c != null && dtLoanerreceivedtime < raes.Asset_return_day2__c)
			)) {
				//raes.Condition_X21_Days__c = 0;
				//raes.Condition_Seven_Days__c = isHolidayCheck(raes.Loaner_received_day2__c, 7, holidayMap);
				raes.Condition_Final_Reply_Days__c = raes.Condition_Seven_Days__c;
				Date replayDate = raes.Shippment_loaner_time2__c.adddays(14).date();
				raes.Delay_Final_Reply_Days__c = isHolidayCheck(replayDate, holidayMap);
				updList.add(raes);
			} else if (raes.Demo_purpose2_T__c == '试用（无询价）' 
				|| raes.Demo_purpose2_T__c == '试用（有询价）' 
				|| raes.Demo_purpose2_T__c == '新产品评价' 
				|| raes.Demo_purpose2_T__c == '学会展会' 
				|| raes.Demo_purpose2_T__c == '协议借用' 
				|| raes.Demo_purpose2_T__c == '其他') {
				//raes.Condition_X21_Days__c = 0;
				//raes.Condition_Seven_Days__c = 0;
				raes.Condition_Final_Reply_Days__c = 0;
				raes.Delay_Final_Reply_Days__c = isHolidayCheck(raes.Rental_End_Date__c, holidayMap);
				updList.add(raes);
			} else if (raes.Demo_purpose2_T__c == '已购待货') {
				// 生成新品发货日
				if (raes.Rental_Apply__r.Follow_UP_Opp__r.Shipping_Finished_Day_Func__c != null) {
					//raes.Condition_X21_Days__c = 0;
					//raes.Condition_Seven_Days__c = 0;
					raes.Condition_Final_Reply_Days__c = 0;
					raes.Delay_Final_Reply_Days__c = isHolidayCheck(raes.Rental_Apply__r.Follow_UP_Opp__r.Shipping_Finished_Day_Func__c.addDays(14), holidayMap);
				} else if (raes.Final_reply_day__c != today) {
					//raes.Condition_X21_Days__c = 0;
					//raes.Condition_Seven_Days__c = 0;
					raes.Condition_Final_Reply_Days__c = 0;
					raes.Delay_Final_Reply_Days__c = isHolidayCheck(raes.Rental_End_Date__c, holidayMap);
				} else {
                    raes.Condition_X21_Days__c = 0;
                    raes.Condition_Seven_Days__c = 0;
                    raes.Condition_Final_Reply_Days__c = 0;
                    raes.Delay_Final_Reply_Days__c = isHolidayCheck(raes.Final_reply_day__c, holidayMap);
                }
                updList.add(raes);
			} else if (raes.Demo_purpose2_T__c == '一般用户') {
				System.debug('zheli:'+raes.Repair_Agreed_Date__c+raes.Rental_Apply__r.Request_demo_day__c+raes.Rental_Apply__r.NewRepair_Repair_Shipped_Date__c + raes.RC_return_to_office__c);
				if (raes.Repair_Agreed_Date__c <= raes.Rental_Apply__r.Request_demo_day__c && raes.Rental_Apply__r.NewRepair_Repair_Shipped_Date__c == null && raes.RC_return_to_office__c != null) {
					// 7.用户同意日≤申请时间and生成修理品返送日
					//raes.Condition_X21_Days__c = 0;
					//raes.Condition_Seven_Days__c = 0;
					System.debug('zheli:00');
					raes.Condition_Final_Reply_Days__c = 0;
					raes.Delay_Final_Reply_Days__c = isHolidayCheck(raes.RC_return_to_office__c.addDays(14), holidayMap);
				} else if ( raes.Repair_Agreed_Date__c > raes.Rental_Apply__r.Request_demo_day__c && raes.Rental_Apply__r.NewRepair_Repair_Shipped_Date__c == null && raes.RC_return_to_office__c != null && raes.Repair_Agreed_Quotation_Text__c <= X21Temp) {	
					// 申请时间＜7.用户同意日and3周之内同意报价、生成修理品返送日
					System.debug('zheli:01');
					// 计算3周是否跨长假
					//raes.Condition_X21_Days__c = isHolidayCheck(raes.Rental_Apply__r.Repair__r.Repair_Quotation_date__c, 21, holidayMap);
					raes.Delay_Final_Reply_Days__c = isHolidayCheck(raes.RC_return_to_office__c.addDays(14), holidayMap);
					//raes.Condition_Seven_Days__c = 0;
					raes.Condition_Final_Reply_Days__c = 0;
				} else if ((raes.Repair_Agreed_Quotation_Text__c > X21Temp && raes.Repair_Agreed_Date__c > raes.Rental_Apply__r.Request_demo_day__c && raes.Rental_Apply__r.NewRepair_Repair_Shipped_Date__c == null)
                            || (raes.Repair_Agreed_Quotation_Text__c > X21Temp && raes.Repair_Agreed_Date__c == null && raes.Rental_Apply__r.NewRepair_Repair_Shipped_Date__c == null)) {
					// 申请时间＜7.用户同意日and3周之内未同意报价：最新预定归还日=6.报价日+24天 
					System.debug('zheli:02');
					// 计算3周是否跨长假
					//raes.Condition_X21_Days__c = isHolidayCheck(raes.Rental_Apply__r.Repair__r.Repair_Quotation_date__c, 21, holidayMap);
					raes.Condition_Final_Reply_Days__c = raes.Condition_X21_Days__c;
					raes.Delay_Final_Reply_Days__c = isHolidayCheck(raes.Rental_Apply__r.Repair__r.Repair_Quotation_date__c.addDays(24), holidayMap);
					//raes.Condition_Seven_Days__c = 0;
				} else if ((raes.Repair_Agreed_Date__c == null 
								&& (raes.Repair_Agreed_Quotation_Text__c < X21Temp || raes.Repair_Agreed_Quotation_Text__c == null)
							)
							|| (raes.RC_return_to_office__c == null && raes.Repair_Agreed_Quotation_Text__c <= X21Temp)
							|| (raes.RC_return_to_office__c == null && raes.Repair_Agreed_Date__c <= raes.Rental_Apply__r.Request_demo_day__c)
						) {
					System.debug('zheli:03');
					//未到21天且无同意日）：最新预定归还日=备品预计回收日
					//OR 3周之内同意报价（未到21天且无同意日）and未生成修理品返送日
					//OR 7.用户同意日≤申请时间 and 未生成修理品返送日 
					raes.Delay_Final_Reply_Days__c = isHolidayCheck(raes.Rental_End_Date__c, holidayMap);
					//raes.Condition_X21_Days__c = 0;
					//raes.Condition_Seven_Days__c = 0;
					raes.Condition_Final_Reply_Days__c = 0;
				} else {
                    raes.Delay_Final_Reply_Days__c = isHolidayCheck(raes.Final_reply_day__c, holidayMap);
                }
				updList.add(raes);
			} else if (raes.Demo_purpose2_T__c == '再修理') {
				// 生成修理品返送日
				if (raes.Rental_Apply__r.NewRepair_Repair_Shipped_Date__c == null && raes.RC_return_to_office__c != null) {
					raes.Delay_Final_Reply_Days__c = isHolidayCheck(raes.RC_return_to_office__c.addDays(14), holidayMap);
					//raes.Condition_X21_Days__c = 0;
					//raes.Condition_Seven_Days__c = 0;
					raes.Condition_Final_Reply_Days__c = 0;
				} else if (raes.RC_return_to_office__c == null && raes.Final_reply_day__c != today) {
					// 未生成修理品返送日
					raes.Delay_Final_Reply_Days__c = isHolidayCheck(raes.Rental_End_Date__c, holidayMap);
					//raes.Condition_X21_Days__c = 0;
					//raes.Condition_Seven_Days__c = 0;
					raes.Condition_Final_Reply_Days__c = 0;
				} else {
                    raes.Condition_X21_Days__c = 0;
                    raes.Condition_Seven_Days__c = 0;
                    raes.Condition_Final_Reply_Days__c = 0;
                    raes.Delay_Final_Reply_Days__c = isHolidayCheck(raes.Final_reply_day__c, holidayMap);
                }
                updList.add(raes);
			} else if (raes.Demo_purpose2_T__c == '保修用户' || raes.Demo_purpose2_T__c == '市场多年保修') {
				if (
					(   !(
							(raes.Rental_Apply__r.Repair__r.Repair_Ordered_Date__c != null && raes.Rental_Apply__r.Repair__r.Repair_Ordered_Date__c <= raes.Bollow_Date_Add_7_WD__c)
							|| (raes.Bollow_Date_Add_7_WD__c == null && raes.Bollow_Date__c != null)
							|| (raes.Rental_Apply__r.Repair__r.Repair_Ordered_Date__c == null && raes.Bollow_Date_Add_7_WD__c >= today)
						)
					)
					&& raes.Shippment_loaner_time2__c != null) {
					raes.Delay_Final_Reply_Days__c = isHolidayCheck(raes.Shippment_loaner_time2__c.addDays(14).date(), holidayMap);
					//raes.Condition_X21_Days__c = 0;
					//raes.Condition_Seven_Days__c = 0;
					raes.Condition_Final_Reply_Days__c = 0;
				} else {
					if (raes.Rental_Apply__r.NewRepair_Repair_Shipped_Date__c == null && raes.RC_return_to_office__c != null) {
						raes.Delay_Final_Reply_Days__c = isHolidayCheck(raes.RC_return_to_office__c.addDays(14), holidayMap);
						//raes.Condition_X21_Days__c = 0;
						//raes.Condition_Seven_Days__c = 0;
						raes.Condition_Final_Reply_Days__c = 0;
					} else {
						raes.Delay_Final_Reply_Days__c = isHolidayCheck(raes.Rental_End_Date__c, holidayMap);
						//raes.Condition_X21_Days__c = 0;
						//raes.Condition_Seven_Days__c = 0;
						raes.Condition_Final_Reply_Days__c = 0;
					}
				}
				updList.add(raes);
			} else if (raes.Demo_purpose2_T__c == '索赔QIS') {
					// 生成修理品返送日
					if (raes.Rental_Apply__r.QIS_Repair_Shipped_Date__c != null 
						&& raes.Rental_Apply__r.NewRepair_Repair_Shipped_Date__c == null
						&& (raes.Rental_Apply__r.next_action_Text__c == '有偿维修'
								|| raes.Rental_Apply__r.next_action_Text__c == '无偿维修'
								|| raes.Rental_Apply__r.next_action_Text__c == '有偿维修+无偿维修'
						)) {
						raes.Delay_Final_Reply_Days__c = isHolidayCheck(raes.Rental_Apply__r.QIS_Repair_Shipped_Date__c.addDays(14), holidayMap);
						//raes.Condition_X21_Days__c = 0;
						//raes.Condition_Seven_Days__c = 0;
						raes.Condition_Final_Reply_Days__c = 0;
						updList.add(raes);
					} else if (raes.Rental_Apply__r.next_action_Text__c == '无偿更换' && raes.Rental_Apply__r.QIS_ReplaceDeliveryDate__c != null) {
						// QIS_新品发货日
						raes.Delay_Final_Reply_Days__c = isHolidayCheck(raes.Rental_Apply__r.QIS_ReplaceDeliveryDate__c.addDays(14), holidayMap);
						//raes.Condition_X21_Days__c = 0;
						//raes.Condition_Seven_Days__c = 0;
						raes.Condition_Final_Reply_Days__c = 0;
						updList.add(raes);
					} else if (raes.Rental_Apply__r.next_action_Text__c == '送回' && raes.Rental_Apply__r.QIS_Reply_day__c != null) {
						// QIS OCM服务本部回答日
						raes.Delay_Final_Reply_Days__c = isHolidayCheck(raes.Rental_Apply__r.QIS_Reply_day__c.addDays(14), holidayMap);
						//raes.Condition_X21_Days__c = 0;
						//raes.Condition_Seven_Days__c = 0;
						raes.Condition_Final_Reply_Days__c = 0;
						updList.add(raes);
					} else if (String.isBlank(raes.Rental_Apply__r.next_action_Text__c)
											|| (raes.Rental_Apply__r.QIS_Repair_Shipped_Date__c == null 
												&& (raes.Rental_Apply__r.next_action_Text__c == '有偿维修'
														|| raes.Rental_Apply__r.next_action_Text__c == '无偿维修'
														|| raes.Rental_Apply__r.next_action_Text__c == '有偿维修+无偿维修'))
											|| (raes.Rental_Apply__r.next_action_Text__c == '无偿更换' && raes.Rental_Apply__r.QIS_ReplaceDeliveryDate__c == null)
											|| (raes.Rental_Apply__r.next_action_Text__c == '送回' && raes.Rental_Apply__r.QIS_Reply_day__c == null)) {
						raes.Delay_Final_Reply_Days__c = isHolidayCheck(raes.Rental_End_Date__c, holidayMap);
						//raes.Condition_X21_Days__c = 0;
						//raes.Condition_Seven_Days__c = 0;
						raes.Condition_Final_Reply_Days__c = 0;
						updList.add(raes);
					} else {
                        raes.Condition_X21_Days__c = 0;
                        raes.Condition_Seven_Days__c = 0;
                        raes.Condition_Final_Reply_Days__c = 0;
                        raes.Delay_Final_Reply_Days__c = isHolidayCheck(raes.Final_reply_day__c, holidayMap);
                        updList.add(raes);
                    }
			} else if (raes.Demo_purpose2_T__c == '故障排查') {
				if (raes.Rental_Apply__r.RC_Ordered_Date__c != null && raes.Rental_Apply__r.RC_Ordered_Date__c <= raes.Bollow_Date_Add_10_WD__c) {
					if (raes.Rental_Apply__r.Repair__r.Agreed_Date__c != null) {
						if (raes.Rental_Apply__r.Repair__r.Repair_Quotation_date__c.addDays(X21Temp) >= raes.Rental_Apply__r.Repair__r.Agreed_Date__c) {
                            // RC修理品返送日不为空，RC返送+14；为空，备品预计回收日
                            if (raes.Rental_Apply__r.Repair__r.Repair_Shipped_Date__c != null) {
                                raes.Delay_Final_Reply_Days__c = isHolidayCheck(raes.Rental_Apply__r.Repair__r.Repair_Shipped_Date__c.addDays(14), holidayMap);
                            } else {
                                raes.Delay_Final_Reply_Days__c = isHolidayCheck(raes.Rental_End_Date__c, holidayMap);
                            }
							//raes.Condition_X21_Days__c = isHolidayCheck(raes.Rental_Apply__r.Repair__r.Repair_Quotation_date__c, 21, holidayMap);
							//raes.Condition_Seven_Days__c = 0;
							raes.Condition_Final_Reply_Days__c = 0;

							// 到货NG一周之内寄回
							if (raes.Received_Confirm__c == 'NG' && (
								(raes.Asset_return_day2__c == null && dtLoanerreceivedtime > now)
								|| (raes.Asset_return_day2__c != null && dtLoanerreceivedtime > raes.Asset_return_day2__c)
							)) {
								//Bollow_Date_Add_10_WD__c
								// 到货NG一周之内寄回：条件延期
								//raes.Condition_Seven_Days__c = isHolidayCheck(raes.Loaner_received_day2__c, 7, holidayMap);
							}
						} else {
							//raes.Condition_Seven_Days__c = 0;
							//raes.Condition_X21_Days__c = isHolidayCheck(raes.Rental_Apply__r.Repair__r.Repair_Quotation_date__c, 21, holidayMap);
							raes.Condition_Final_Reply_Days__c = raes.Condition_X21_Days__c;
							raes.Delay_Final_Reply_Days__c = isHolidayCheck(raes.Rental_Apply__r.Repair__r.Repair_Quotation_date__c.addDays(24), holidayMap);

							// 到货NG一周之内寄回
							if (raes.Received_Confirm__c == 'NG' && (
								(raes.Asset_return_day2__c == null && dtLoanerreceivedtime > now)
								|| (raes.Asset_return_day2__c != null && dtLoanerreceivedtime > raes.Asset_return_day2__c)
							)) {
								//Bollow_Date_Add_10_WD__c
								// 到货NG一周之内寄回：条件延期
								//raes.Condition_Seven_Days__c = isHolidayCheck(raes.Loaner_received_day2__c, 7, holidayMap);
							}
						}
					} else {
                        if (raes.Rental_Apply__r.Repair__r.Repair_Quotation_date__c != null && raes.Rental_Apply__r.Repair__r.Repair_Quotation_date__c.addDays(X21Temp) < today) {
                            //raes.Condition_X21_Days__c = isHolidayCheck(raes.Rental_Apply__r.Repair__r.Repair_Quotation_date__c, 21, holidayMap);
                            raes.Condition_Final_Reply_Days__c =  raes.Condition_X21_Days__c;
                            raes.Delay_Final_Reply_Days__c = isHolidayCheck(raes.Rental_Apply__r.Repair__r.Repair_Quotation_date__c.addDays(24), holidayMap);
                            //raes.Condition_Seven_Days__c = 0;

                            // 到货NG一周之内寄回
                            if (raes.Received_Confirm__c == 'NG' && (
                                (raes.Asset_return_day2__c == null && dtLoanerreceivedtime > now)
                                || (raes.Asset_return_day2__c != null && dtLoanerreceivedtime > raes.Asset_return_day2__c)
                            )) {
                                //Bollow_Date_Add_10_WD__c
                                // 到货NG一周之内寄回：条件延期
                                //raes.Condition_Seven_Days__c = isHolidayCheck(raes.Loaner_received_day2__c, 7, holidayMap);
                            }
                        } else {
                            //Rental_End_Date__c
                            // 未生成修理品返送日
                            raes.Delay_Final_Reply_Days__c = isHolidayCheck(raes.Rental_End_Date__c, holidayMap);
                            //raes.Condition_X21_Days__c = 0;
                            //raes.Condition_Seven_Days__c = 0;
                            raes.Condition_Final_Reply_Days__c = 0;
                        }
					}
				} else {
					if (raes.Rental_Apply__r.RC_Ordered_Date__c == null && raes.Bollow_Date_Add_10_WD__c >= today) {
						//Rental_End_Date__c
						// 未生成修理品返送日
						raes.Delay_Final_Reply_Days__c = isHolidayCheck(raes.Rental_End_Date__c, holidayMap);
						//raes.Condition_X21_Days__c = 0;
						//raes.Condition_Seven_Days__c = 0;
						raes.Condition_Final_Reply_Days__c = 0;
					} else {
						// 到货NG一周之内寄回
						if (raes.Received_Confirm__c == 'NG' && (
							(raes.Asset_return_day2__c == null && dtLoanerreceivedtime > now)
							|| (raes.Asset_return_day2__c != null && dtLoanerreceivedtime > raes.Asset_return_day2__c)
						)) {
							//Bollow_Date_Add_10_WD__c
							// 到货NG一周之内寄回：条件延期
							//raes.Condition_Seven_Days__c = isHolidayCheck(raes.Loaner_received_day2__c, 7, holidayMap);
							raes.Delay_Final_Reply_Days__c = 0;
							//raes.Condition_X21_Days__c = 0;
							raes.Condition_Final_Reply_Days__c = 0;
						}
					}
				}
				updList.add(raes);
			}
		}

		if (!updList.isEmpty()) {
				update updList;
		}
	}

	private Integer isHolidayCheck(Date dateStr, Integer passDay, Map<Date, Decimal> holidayMap) {
		Integer ret = 0;
		Date dateTemp = dateStr;
		for (Integer i = 1; i <= passDay; i++) {
			dateTemp = dateTemp.addDays(1);
			if (holidayMap.containsKey(dateTemp)) {
				ret = Integer.valueOf(holidayMap.get(dateTemp));
				break;
			}
		}
		return ret;
	}

	private Integer isHolidayCheck(Date dateStr, Map<Date, Decimal> holidayMap) {
		Integer ret = 0;
		if (holidayMap.containsKey(dateStr)) {
			ret = Integer.valueOf(holidayMap.get(dateStr));
		}
		return ret;
	}

	global void finish(Database.BatchableContext BC) {}

}