package com.ruoyi.xq.util;

import cn.hutool.core.util.NumberUtil;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class CaiNumUtil {

    /**
     * 向下取整计算金额
     * @param value
     * @param rate
     * @return
     */
    public static BigDecimal coin(BigDecimal value, BigDecimal rate){
        if(value == null || rate == null){
            return BigDecimal.ZERO;
        }
        BigDecimal decimal = NumberUtil.mul(value, rate);
        return decimal.setScale(0, RoundingMode.DOWN);
    }

    public static String rateToStr(BigDecimal rate){
        BigDecimal mul = NumberUtil.mul(rate, 100);
        return mul.intValue()+"%";
    }

    public static BigDecimal memberDay(BigDecimal price, Integer days){
        BigDecimal div = NumberUtil.div(price, days, 2);
        if(div.compareTo(BigDecimal.ZERO) == 0){
            return new BigDecimal("0.01");
        }
        return div;
    }

    public static BigDecimal diffRate(Long onlineTodayNum, Long onlineLastNum) {
        Long diff = onlineTodayNum - onlineLastNum;
        if(onlineLastNum == 0){
            return BigDecimal.ZERO;
        }
        return NumberUtil.div(diff,onlineLastNum,3);
    }

    public static BigDecimal diffRate(BigDecimal onlineTodayNum, BigDecimal onlineLastNum) {
        BigDecimal diff = NumberUtil.sub(onlineTodayNum, onlineLastNum);
        if(onlineLastNum.compareTo(BigDecimal.ZERO) == 0){
            return BigDecimal.ZERO;
        }
        return NumberUtil.div(diff,onlineLastNum,3);
    }

    public static BigDecimal vipDays(BigDecimal bigDecimal, Long type){
        BigDecimal div = NumberUtil.div(bigDecimal, type, 2);
        if(div.equals(BigDecimal.ZERO)){
            return new BigDecimal("0.01");
        }
        return div;
    }

}
