package com.cyksj.common.service;


import cn.hutool.core.collection.CollUtil;
import com.cyksj.model.dto.YhServiceDto;

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 项目名: netflix-job
 * 文件名: DateUtils
 * 创建者: JavaZou
 * 创建时间:2023/12/25 13:52
 */
public class CropDutyServiceDateService {

	public static DayOfWeek getNextWorkDay(DayOfWeek currentDay) {
		// 如果当前是周五，下一个工作日是周一；否则，是当前星期的下一天
		if (currentDay == DayOfWeek.FRIDAY) {
			return DayOfWeek.SATURDAY;
		}
		if (currentDay == DayOfWeek.SATURDAY) {
			return DayOfWeek.SUNDAY;
		}
		return DayOfWeek.MONDAY;
	}


	/**
	 * 返回明天星期几 用户获取对应周期工作客服
	 */
	public static List<YhServiceDto> getDutyService(DayOfWeek nextWorkDay, List<YhServiceDto> workday, List<YhServiceDto> saturday, List<YhServiceDto> sunday, Map<String, List<YhServiceDto>> holidyDateMap, LocalDateTime now) {
		List<YhServiceDto> holidayServiceIfHoliday = getHolidayServiceIfHoliday(now, holidyDateMap);
		if (holidayServiceIfHoliday != null) {
			return holidayServiceIfHoliday;
		}
		if (nextWorkDay == DayOfWeek.SATURDAY) {
			return Optional.ofNullable(saturday).orElse(Optional.ofNullable(sunday).orElse(workday));
		}
		if (nextWorkDay == DayOfWeek.SUNDAY) {
			return Optional.ofNullable(sunday).orElse(workday);
		}
		return workday;
	}


	public static List<YhServiceDto> getHolidayServiceIfHoliday(LocalDateTime now, Map<String, List<YhServiceDto>> holidyDateMap) {
		if (holidyDateMap == null) {
			return null;
		}
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d");
		String nowDate = now.format(formatter);

		List<YhServiceDto> holidayServices = holidyDateMap.get(nowDate);
		if (CollUtil.isNotEmpty(holidayServices)) {
			return holidayServices;
		}
		return null;
	}
}
