package com.agri.common.utils;

/**
 * @ClassName TimeRangeUtil
 * @Description TODO
 * @Author lld
 * @Date 2026/3/6 2:09
 * @Version 1.0
 */
import java.time.LocalDateTime;
import java.time.LocalTime;

public class TimeRangeUtil {

    /**
     * 判断dtu上报时间的时分秒是否在auto_term的时间区间内（支持跨天）
     */
    public static boolean isTimeInRange(LocalDateTime dtuTime, LocalDateTime startTime, LocalDateTime endTime) {
        if (dtuTime == null || startTime == null || endTime == null) {
            return false;
        }
        LocalTime currentTime = dtuTime.toLocalTime();
        LocalTime beforeTime = startTime.toLocalTime();
        LocalTime afterTime = endTime.toLocalTime();

        if (startTime.isBefore(endTime)) {
            // 正常时段：08:00:00 - 18:00:00
            return !currentTime.isBefore(beforeTime) && !currentTime.isAfter(afterTime);
        } else {
            // 跨天时段：22:00:00 - 06:00:00
            return currentTime.isAfter(beforeTime) || currentTime.isBefore(afterTime);
        }
    }

    /**
     * 从ts（毫秒时间戳）转换并判断
     */
    public static boolean isTsInRange(long ts, LocalDateTime startTime, LocalDateTime endTime) {
        LocalDateTime dtuTime = LocalDateTime.ofInstant(
                java.time.Instant.ofEpochMilli(ts),
                java.time.ZoneId.systemDefault()
        );
        return isTimeInRange(dtuTime, startTime, endTime);
    }

}
