package com.ruoyi.web.modules.architecture.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;

/**
 * 用药工具停药日期解析（yyyy-MM-dd）。
 */
public final class AppMedicationStopDateSupport
{
    private static final DateTimeFormatter DATE_FMT = DateTimeFormatter.ISO_LOCAL_DATE;

    private AppMedicationStopDateSupport()
    {
    }

    public static LocalDate parseStopDate(String stopDate)
    {
        if (StringUtils.isEmpty(stopDate))
        {
            throw new ServiceException("请选择停药日期");
        }
        String text = stopDate.trim();
        try
        {
            return LocalDate.parse(text, DATE_FMT);
        }
        catch (DateTimeParseException ex)
        {
            throw new ServiceException("停药日期格式须为 yyyy-MM-dd");
        }
    }

    public static String formatDate(LocalDate date)
    {
        return date.format(DATE_FMT);
    }
}
