package org.wfc.user.util;

import org.wfc.user.domain.UBillRule;
import org.wfc.user.domain.constant.TratticUnitEnum;

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

/**
 * @description: 计费规则工具
 * @author: cyc
 * @since: 2025-01-16
 */
public class BillRuleUtil {

    public static BigDecimal calc(UBillRule billRule, Long currentTraffic) {
        long exchange = 1L;
        switch (TratticUnitEnum.getByCode(billRule.getUnit())) {
            case KB:
                exchange = 1024L;
                break;
            case MB:
                exchange = 1024*1024L;
                break;
            case GB:
                exchange = 1024*1024*1024L;
                break;
            case TB:
                exchange = 1024*1024*1024*1024L;
                break;
            default:
        }
        // 计算公式：金额 = 已用流量（B） * 价格 / 流量 / 换算值
        return BigDecimal.valueOf(currentTraffic).multiply(billRule.getPrice())
                .divide(BigDecimal.valueOf(billRule.getTraffic()), 8, RoundingMode.HALF_UP)
                .divide(BigDecimal.valueOf(exchange), 4, RoundingMode.HALF_UP);
    }
}
