package org.wfc.user.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import org.wfc.user.domain.UAccount;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @description: 账户工具
 * @author: cyc
 * @since: 2024-12-31
 */
public class AccountUtil {

    /**
     * 检验账户有效
     *
     * @param account 账户
     * @param current 当前时间
     * @return boolean
     */
    public static boolean isValid(UAccount account, Date current) {
        // 过期/流量已用完/时长已用完应取消授权
        return ((DateUtil.compare(account.getStartTime(), current) <= 0 && DateUtil.compare(account.getEndTime(), current) > 0)
                && (!account.getTrafficEnable() || account.getTrafficUsed() <= account.getTraffic())
                && (!account.getDurationEnable() || account.getDurationUsed() <= account.getDuration()))
                || (ObjectUtil.isNotNull(account.getBalance()) && account.getBalance().compareTo(BigDecimal.ZERO) > 0);
    }

    public static boolean isPackageValid(UAccount account, Date current) {
        // 过期/流量已用完/时长已用完应取消授权
        return (DateUtil.compare(account.getStartTime(), current) <= 0 && DateUtil.compare(account.getEndTime(), current) > 0)
                && (!account.getTrafficEnable() || account.getTrafficUsed() <= account.getTraffic())
                && (!account.getDurationEnable() || account.getDurationUsed() <= account.getDuration());
    }

    public static boolean isBalanceValid(UAccount account) {
        // 过期/流量已用完/时长已用完应取消授权
        return ObjectUtil.isNotNull(account.getBalance()) && account.getBalance().compareTo(BigDecimal.ZERO) > 0;
    }
}
