package com.ruoyi.web.modules.booking.support;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.diagnosis.support.ServiceAppointmentRules;

/**
 * 预约日期是否已预约 — 参数校验。
 */
public final class AppBookingDateCheckSupport
{
    private static final DateTimeFormatter DATE_FMT = DateTimeFormatter.ISO_LOCAL_DATE;

    private AppBookingDateCheckSupport()
    {
    }

    public static void validate(Integer providerType, Long providerId, String appointDate)
    {
        if (providerType == null
                || (providerType != ServiceAppointmentRules.PROVIDER_TYPE_VET
                        && providerType != ServiceAppointmentRules.PROVIDER_TYPE_ORG
                        && providerType != ServiceAppointmentRules.PROVIDER_TYPE_EXPERT))
        {
            throw new ServiceException("预约类型无效");
        }
        if (providerId == null)
        {
            throw new ServiceException("服务方资源ID不能为空");
        }
        parseAppointDate(appointDate);
    }

    public static String normalizeAppointDate(String appointDate)
    {
        return parseAppointDate(appointDate).toString();
    }

    private static LocalDate parseAppointDate(String appointDate)
    {
        if (StringUtils.isEmpty(appointDate))
        {
            throw new ServiceException("预约日期不能为空");
        }
        try
        {
            return LocalDate.parse(appointDate.trim(), DATE_FMT);
        }
        catch (DateTimeParseException ex)
        {
            throw new ServiceException("预约日期格式无效");
        }
    }
}
