package com.lanyuanxiaoyao.service.configuration.utils;

import cn.hutool.core.util.ObjectUtil;
import java.util.function.Supplier;

/**
 * 日期工具
 *
 * @author lanyuanxiaoyao
 * @date 2023-04-21
 */
public class DatetimeUtil {
    public static String fromNow(long now, Supplier<Long> supplier) {
        if (ObjectUtil.isNotNull(supplier.get()) && supplier.get() != 0) {
            return fromNow(now, supplier.get());
        }
        return "";
    }

    public static String fromNow(long now, long target) {
        long delta = now - target;
        if (delta == 0) {
            return "就是现在";
        }
        String suffix = delta > 0 ? "前" : "后";
        delta = Math.abs(delta);
        if (delta > 31536000000L) {
            return (int) (delta / 31536000000L) + " 年" + suffix;
        } else if (delta > 2592000000L) {
            return (int) (delta / 2592000000L) + " 个月" + suffix;
        } else if (delta > 604800000L) {
            return (int) (delta / 604800000L) + " 周" + suffix;
        } else if (delta > 86400000L) {
            return (int) (delta / 86400000L) + " 天" + suffix;
        } else if (delta > 3600000L) {
            return (int) (delta / 3600000L) + " 小时" + suffix;
        } else if (delta > 60000L) {
            return (int) (delta / 60000L) + " 分钟" + suffix;
        } else if (delta > 1000L) {
            return (delta / 1000L) + "秒" + suffix;
        } else {
            return "就是现在";
        }
    }
}
