package com.sckw.core.utils;

import com.sckw.core.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author xucaiqin
 * @date 2023-07-26 11:42:43
 */
@Slf4j
public class StringTimeUtil {
    private final static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private final static DateTimeFormatter dateTimeYMD = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    @SuppressWarnings("all")
    private static void formatCheck(String time) {
        try {
            LocalDate.parse(time, dateTimeYMD);
        } catch (Exception e) {
            log.error("时间格式化错误", e);
            throw new BusinessException("时间格式化错误!");
        }
    }

    /**
     * @param time yyyy-MM-dd
     * @return String yyyy-MM-dd HH:mm:ss
     * @throws RuntimeException
     */
    public static String fillStart(String time) {
        formatCheck(time);
        return time + " 00:00:00";
    }

    /**
     * @param time yyyy-MM-dd
     * @return String yyyy-MM-dd HH:mm:ss
     * @throws RuntimeException
     */
    public static String fillEnd(String time) {
        formatCheck(time);
        return time + " 23:59:59";
    }

    /**
     * @param time yyyy-MM-dd
     * @return LocalDateTime yyyy-MM-dd HH:mm:ss
     */
    public static LocalDateTime startDateTime(String time) {
        return LocalDateTime.parse(fillStart(time), dateTimeFormatter);
    }

    /**
     * @param time yyyy-MM-dd
     * @return LocalDateTime yyyy-MM-dd HH:mm:ss
     */
    public static LocalDateTime endDateTime(String time) {
        return LocalDateTime.parse(fillEnd(time), dateTimeFormatter);
    }
}
