using System;

namespace JiaZhiQuan.Common.Utils
{
    public class AnalyticalFormulaUtils
    {
        /// <summary>
        /// 计算增长率, 保留两位小数
        /// </summary>
        /// <param name="currentData"></param>
        /// <param name="previousData"></param>
        /// <returns></returns>
        public static string GrowthRate(decimal currentData, decimal previousData)
        {
            // 防止除以零
            if (previousData == 0)
            {
                return "-"; // 上一次数据为零，无法计算增长率
            }
            if(currentData == previousData)
            {
                return "0.00%";
            }

            decimal growthRate = ((currentData - previousData) / previousData) * 100;
            decimal roundedGrowthRate = Math.Round(growthRate, 2); // 四舍五入保留两位小数
            return roundedGrowthRate.ToString("0.00") + "%"; // 格式化为百分比字符串
        }

        /// <summary>
        /// 计算，公式：a / b
        /// </summary>
        /// <param name="count"></param>
        /// <param name="sumCount"></param>
        /// <returns></returns>
        public static string RateStr(int count, int sumCount)
        {
            // 防止除以零
            if (sumCount == 0)
            {
                return "-"; // 上一次数据为零，无法计算
            }

            decimal rate = (decimal)count / sumCount * 100;
            decimal roundedGrowthRate = Math.Round(rate, 2); // 四舍五入保留两位小数
            return roundedGrowthRate.ToString("0.00") + "%"; // 格式化为百分比字符串
        }

        /// <summary>
        /// 计算，公式：a / b
        /// </summary>
        /// <param name="count"></param>
        /// <param name="sumCount"></param>
        /// <returns></returns>
        public static string RateStr(decimal count, decimal sumCount)
        {
            // 防止除以零
            if (sumCount == 0)
            {
                return "-"; // 上一次数据为零，无法计算
            }

            decimal rate = count / sumCount * 100;
            decimal roundedGrowthRate = Math.Round(rate, 2); // 四舍五入保留两位小数
            return roundedGrowthRate.ToString("0.00") + "%"; // 格式化为百分比字符串
        }

        /// <summary>
        /// 计算，公式：a / b
        /// </summary>
        /// <param name="count"></param>
        /// <param name="sumCount"></param>
        /// <returns></returns>
        public static string Rate(int count, int sumCount)
        {
            // 防止除以零
            if (sumCount == 0)
            {
                return "-"; // 上一次数据为零，无法计算
            }

            decimal rate = (decimal)count / sumCount * 100;
            decimal roundedGrowthRate = Math.Round(rate, 2); // 四舍五入保留两位小数
            return roundedGrowthRate.ToString("0.00"); // 格式化为百分比字符串
        }

        /// <summary>
        /// 四舍五入保留两位小数，如果结果是整数，则加上两位小数，否则直接返回格式化后的字符串
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public static string FormatNumber(decimal number)
        {
            // 使用字符串格式化将数字四舍五入并保留两位小数
            string formattedNumber = string.Format("{0:0.00}", Math.Round(number, 2));

            // 如果结果是整数，则加上两位小数，否则直接返回格式化后的字符串
            if (formattedNumber.EndsWith(".00"))
            {
                formattedNumber = formattedNumber.Substring(0, formattedNumber.Length - 3);
            }

            return formattedNumber;
        }
    }
}
