package com.ningdatech.kqapi.user.util;

import cn.hutool.core.util.StrUtil;
import com.ningdatech.basic.exception.BizException;
import com.ningdatech.kqapi.common.constant.BizConst;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.function.Consumer;
import java.util.regex.Pattern;


/**
 * <p>
 * BizUtils-特殊的业务方法
 * </p>
 *
 * @author WendyYang
 * @since 20:59 2022/11/29
 */
public class BizUtils {

    private BizUtils() {
    }


    public static <T> void notNull(T obj, Consumer<T> consumer) {
        if (obj != null) {
            consumer.accept(obj);
        }
    }

    public static <T extends Number> void notNullOrZero(T number, Consumer<T> consumer) {
        if (number != null && number.intValue() != 0) {
            consumer.accept(number);
        }
    }

    public static void notBlank(String str, Consumer<String> consumer) {
        if (StrUtil.isNotBlank(str)) {
            consumer.accept(str);
        }
    }

    public static void password(String password) {
        if (StrUtil.isBlank(password) || !Pattern.compile(BizConst.REGEX_PASS).matcher(password).matches()) {
            throw BizException.wrap("请输入6-10位大小写字母或数字");
        }
    }

    public static long getRestMillsToday() {
        LocalDate now = LocalDate.now();
        LocalDateTime currTime = now.atTime(LocalTime.now());
        LocalDateTime nextDay = now.plusDays(1).atStartOfDay();
        return ChronoUnit.MILLIS.between(currTime, nextDay);
    }

    /**
     * 判断当前小时数是否在某个范围内
     *
     * @param time  时间
     * @param start 开始小时数
     * @param end   结束小时数
     * @return boolean
     * @author WendyYang
     **/
    public static boolean betweenHours(LocalDateTime time, int start, int end) {
        if (time == null) {
            return false;
        }
        int hour = time.getHour();
        return hour >= start && end >= hour;
    }

    public static boolean isBetween(LocalDateTime targetTime, LocalDateTime startTime, LocalDateTime endTime) {
        return targetTime.isAfter(startTime) && targetTime.isBefore(endTime);
    }

}
