package com.ruoyi.bst.vipUser.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;

import com.ruoyi.bst.vip.domain.enums.VipLimitUnit;
import com.ruoyi.bst.vip.domain.enums.VipType;
import com.ruoyi.bst.vipUser.domain.VipUser;
import com.ruoyi.bst.vipUser.domain.VipUserVO;
import com.ruoyi.common.utils.MathUtils;

public class VipUserUtil {

    public static LocalDateTime getNextResetTime(VipUser vip) {
        if (vip == null) {
            return null;
        }
        if (VipLimitUnit.DAY.getCode().equals(vip.getLimitUnit())) {
            return LocalDateTime.now().plusDays(vip.getLimitRound());
        }
        return null;
    }

    /**
     * 计算优惠金额
     * @param originalAmount 原价
     * @param vipUser 用户卡券
     * @return 优惠金额
     */
    public static BigDecimal calcDiscountAmount(BigDecimal originalAmount, VipUser vipUser) {
        if (vipUser == null || originalAmount == null) {
            return BigDecimal.ZERO;
        }

        if (VipType.DISCOUNT.getCode().equals(vipUser.getType())) {
            BigDecimal decimal10 = BigDecimal.valueOf(10);
            return MathUtils.mulDecimal(originalAmount, MathUtils.subtractDecimal(decimal10, vipUser.getDiscount()))
                    .divide(decimal10, 2, RoundingMode.DOWN);
        } else if (VipType.DEDUCT.getCode().equals(vipUser.getType())) {
            return MathUtils.min(originalAmount, vipUser.getDiscount());
        } else if (VipType.BALANCE.getCode().equals(vipUser.getType())) {
            return MathUtils.min(originalAmount, vipUser.getDiscount());
        }
        return BigDecimal.ZERO;
    }

    /**
     * 是否在可用期限内
     * @param vipUser
     * @return
     */
    public static boolean isValidTime(VipUserVO vipUser) {
        if (vipUser == null) {
            return false;
        }
        LocalDateTime now = LocalDateTime.now();
        return vipUser.getEndTime() != null && vipUser.getEndTime().isAfter(now)
        && vipUser.getStartTime() != null && vipUser.getStartTime().isBefore(now);
    }

}
