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
                && (ObjectUtil.isNull(account.getBalanceUsed()) || account.getBalanceUsed().compareTo(BigDecimal.ZERO) == 0
                || (ObjectUtil.isNotNull(account.getBalanceUsed()) && account.getBalance().compareTo(BigDecimal.ZERO) > 0
                && account.getBalance().subtract(account.getBalanceUsed()).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;
    }

    public static boolean isPackageUnValidBalanceValid(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)
                && (ObjectUtil.isNull(account.getBalanceUsed()) || account.getBalanceUsed().compareTo(BigDecimal.ZERO) == 0);
    }

    public static boolean isPackageValidBalanceUnValid(UAccount account, Date current) {
        // 套餐有效，有已使用余额且余额扣去已使用余额大于0
        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.getBalanceUsed()) && account.getBalanceUsed().compareTo(BigDecimal.ZERO) > 0)
                && (ObjectUtil.isNotNull(account.getBalance()) && account.getBalance().subtract(account.getBalanceUsed()).compareTo(BigDecimal.ZERO) > 0);
    }

    public static boolean isPackageValidNoUsedBalance(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.isNull(account.getBalanceUsed()) || account.getBalanceUsed().compareTo(BigDecimal.ZERO) == 0);
    }
}
