package cn.iocoder.yudao.module.iot.controller.admin.deviceoperationrecord.utils;

import org.springframework.stereotype.Component;

@Component
public class TimeConverterUtil {

    private static final int SECONDS_PER_HOUR = 3600;

    /**
     * 秒转小时（保留2位小数）
     */
    public static double secondsToHours(double seconds) {
        if (seconds <= 0) {
            return 0.0;
        }
        double hours = seconds / SECONDS_PER_HOUR;
        return formatDouble(hours, 2);
    }

    /**
     * 秒转小时（自定义小数位数）
     */
    public static double secondsToHours(double seconds, int scale) {
        if (seconds <= 0) {
            return 0.0;
        }
        double hours = seconds / SECONDS_PER_HOUR;
        return formatDouble(hours, scale);
    }

    /**
     * 计算稼动率
     * 稼动率 = 运行时间 / (运行时间 + 待机时间 + 故障时间 + 警告时间) * 100%
     */
    public static double calculateUtilizationRate(double runningTime, double standbyTime,
                                                  double faultTime, double warningTime) {
        double totalTime = runningTime + standbyTime + faultTime + warningTime;
        if (totalTime <= 0) {
            return 0.0;
        }
        double rate = runningTime / totalTime * 100;
        return formatDouble(rate, 2);
    }

    /**
     * 格式化小数
     */
    public static double formatDouble(double value, int scale) {
        if (scale < 0) {
            scale = 2;
        }
        double factor = Math.pow(10, scale);
        return Math.round(value * factor) / factor;
    }

    /**
     * 获取百分比字符串
     */
    public static String getPercentString(double value) {
        return formatDouble(value, 2) + "%";
    }
}