172 lines
5.3 KiB
C#
Raw Permalink Normal View History

2025-06-17 13:50:37 +08:00
using Newtonsoft.Json;
using System;
namespace YD_AllHeartRates.Commons.Utils
{
/// <summary>
/// 工具类
/// </summary>
public static class Util
{
/// <summary>
/// 获取短信Code
/// </summary>
/// <returns></returns>
public static string GetSmsCode()
{
Random random = new Random();
return random.Next(1000, 9999).ToString();
}
public static string ReplaceWithSpecialChar(string value, int startLen = 4, int endLen = 4, char specialChar = '*')
{
try
{
int lenth = value.Length - startLen - endLen;
string replaceStr = value.Substring(startLen, lenth);
string specialStr = string.Empty;
for (int i = 0; i < replaceStr.Length; i++)
{
specialStr += specialChar;
}
value = value.Replace(replaceStr, specialStr);
}
catch (Exception)
{
throw;
}
return value;
}
public static string GetColorValue(string color)
{
switch (color)
{
case "黄色":
return "#FFFF00";
case "绿色":
return "#008000";
case "红色":
return "#FF0000";
case "蓝色":
return "#0000FF";
default:
return "#0000FF";
}
}
public static string Serialize(this object obj, JsonSerializerSettings formatDate = null)
{
if (obj == null) return null;
formatDate = formatDate ?? new JsonSerializerSettings
{
DateFormatString = "yyyy-MM-dd HH:mm:ss"
};
return JsonConvert.SerializeObject(obj, formatDate);
}
public static int GetInt(this object obj)
{
if (obj == null)
return 0;
int.TryParse(obj.ToString(), out int _number);
return _number;
}
private static DateTime dateStart = new DateTime(1970, 1, 1, 8, 0, 0);
private static long longTime = 621355968000000000;
private static int samllTime = 10000000;
/// <summary>
/// 获取时间戳
/// </summary>
/// <param name="dateTime"></param>
/// <returns></returns>
public static long GetTimeStamp(this DateTime dateTime)
{
return (dateTime.ToUniversalTime().Ticks - longTime) / samllTime;
}
/// <summary>
/// 时间戳转换成日期
/// </summary>
/// <param name="timeStamp"></param>
/// <returns></returns>
public static DateTime GetTimeSpmpToDate(this object timeStamp)
{
if (timeStamp == null) return dateStart;
DateTime dateTime = new DateTime(longTime + Convert.ToInt64(timeStamp) * samllTime, DateTimeKind.Utc).ToLocalTime();
return dateTime;
}
public static bool IsNullOrEmpty(this object str)
{
if (str == null)
return true;
return str.ToString() == "";
}
public static string ConvertSecondsToMinutes(this int? totalSeconds)
{
if (totalSeconds == null || totalSeconds < 0) return "0分0秒";
int minutes = totalSeconds.Value / 60;
int seconds = totalSeconds.Value % 60;
return $"{minutes}分{seconds}秒";
}
public static int GetRank<T>(this T score) where T : struct, IComparable
{
// 将 score 转换为 double 进行比较
double value = Convert.ToDouble(score);
return value switch
{
>= 90 => 1,
>= 80 => 2,
>= 60 => 3,
_ => 4
};
}
public static string GetRankStr<T>(this T score) where T : struct, IComparable
{
// 将 score 转换为 double 进行比较
double value = Convert.ToDouble(score);
return value switch
{
>= 90 => "优秀",
>= 80 => "良好",
>= 60 => "及格",
_ => "低分"
};
}
2025-06-26 17:36:12 +08:00
/// <summary>
2025-06-27 15:14:22 +08:00
/// 计算连续时间点的累计时长默认最大间隔为10秒
/// </summary>
/// <param name="times">时间集合,已满足条件</param>
/// <param name="maxGapSeconds">允许的最大时间间隔</param>
/// <returns>累计持续时间(秒)</returns>
public static int CalculateDuration(this List<DateTime> times, int maxGapSeconds = 10)
2025-06-26 17:36:12 +08:00
{
if (times == null || times.Count < 2)
return 0;
var ordered = times.OrderBy(t => t).ToList();
double totalSeconds = 0;
for (int i = 1; i < ordered.Count; i++)
{
var gap = (ordered[i] - ordered[i - 1]).TotalSeconds;
if (gap <= maxGapSeconds)
totalSeconds += gap;
}
2025-06-27 15:14:22 +08:00
return (int)Math.Round(totalSeconds); // 四舍五入
2025-06-26 17:36:12 +08:00
}
2025-06-27 15:14:22 +08:00
2025-06-17 13:50:37 +08:00
}
}