package com.m2pool.lease.utils;

import com.m2pool.common.core.utils.StringUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

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

/**
 * @Description 算力转换
 * @Date 2024/6/13 10:54
 * @Author dy
 */
public class PowerUnitUtils {

    /**
     * 根据unit 对value 进行转换
     * @param unit
     * @param value
     * @return
     */
    public static BigDecimal getPower(String unit,BigDecimal value) {


        if (StringUtils.isEmpty(unit)) {
            unit = "gh/s";
        }
        unit = unit.toLowerCase();
        // 根据输入单位计算对应的转换因子
        BigDecimal conversionFactor;
        switch (unit) {
            case "h/s":
                conversionFactor = BigDecimal.ONE;
                break;
            case "kh/s":
                conversionFactor = BigDecimal.valueOf(1000L);
                break;
            case "mh/s":
                conversionFactor = BigDecimal.valueOf(1000L).pow(2);
                break;
            case "gh/s":
                conversionFactor = BigDecimal.valueOf(1000L).pow(3);
                break;
            case "th/s":
                conversionFactor = BigDecimal.valueOf(1000L).pow(4);
                break;
            case "ph/s":
                conversionFactor = BigDecimal.valueOf(1000L).pow(5);
                break;
            case "eh/s":
                conversionFactor = BigDecimal.valueOf(1000L).pow(6);
                break;
            default:
                throw new IllegalArgumentException("不支持的单位: " + unit);
        }

        // 进行单位转换并保留小数
        return value.divide(conversionFactor,6, RoundingMode.HALF_UP);
    }

}
