145 lines
4.8 KiB
C#
145 lines
4.8 KiB
C#
|
|
using YD_Event.Core.Entities;
|
|||
|
|
|
|||
|
|
namespace YD_Event.Core.Extensions;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 项目时间处理扩展类
|
|||
|
|
/// 用于统一处理 Project 实体的时间计算逻辑
|
|||
|
|
/// </summary>
|
|||
|
|
public static class ProjectTimeExtensions
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取项目的总时长(秒)
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="project">项目实体</param>
|
|||
|
|
/// <returns>总时长(秒)</returns>
|
|||
|
|
/// <exception cref="ArgumentNullException">当 project 为 null 时抛出</exception>
|
|||
|
|
public static int GetTotalDurationInSeconds(this Project project)
|
|||
|
|
{
|
|||
|
|
if (project == null)
|
|||
|
|
throw new ArgumentNullException(nameof(project));
|
|||
|
|
|
|||
|
|
// 边界情况处理:确保值不小于 0
|
|||
|
|
int minutes = Math.Max(0, project.DurationMinutes);
|
|||
|
|
int seconds = Math.Max(0, project.DurationSeconds);
|
|||
|
|
|
|||
|
|
// 确保秒数不超过 59(一分钟内的秒数)
|
|||
|
|
seconds = Math.Min(seconds, 59);
|
|||
|
|
|
|||
|
|
return minutes * 60 + seconds;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 设置项目的总时长(秒)
|
|||
|
|
/// 自动计算并更新 DurationMinutes 和 DurationSeconds
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="project">项目实体</param>
|
|||
|
|
/// <param name="totalSeconds">总时长(秒)</param>
|
|||
|
|
/// <exception cref="ArgumentNullException">当 project 为 null 时抛出</exception>
|
|||
|
|
/// <exception cref="ArgumentOutOfRangeException">当 totalSeconds 小于 0 时抛出</exception>
|
|||
|
|
public static void SetTotalDurationInSeconds(this Project project, int totalSeconds)
|
|||
|
|
{
|
|||
|
|
if (project == null)
|
|||
|
|
throw new ArgumentNullException(nameof(project));
|
|||
|
|
|
|||
|
|
if (totalSeconds < 0)
|
|||
|
|
throw new ArgumentOutOfRangeException(nameof(totalSeconds), "总时长不能小于 0");
|
|||
|
|
|
|||
|
|
// 计算分钟和秒
|
|||
|
|
project.DurationMinutes = totalSeconds / 60;
|
|||
|
|
project.DurationSeconds = totalSeconds % 60;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 标准化项目的时间字段
|
|||
|
|
/// 确保 DurationSeconds 在 0-59 范围内,并正确处理负值
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="project">项目实体</param>
|
|||
|
|
/// <exception cref="ArgumentNullException">当 project 为 null 时抛出</exception>
|
|||
|
|
public static void NormalizeDuration(this Project project)
|
|||
|
|
{
|
|||
|
|
if (project == null)
|
|||
|
|
throw new ArgumentNullException(nameof(project));
|
|||
|
|
|
|||
|
|
// 先获取总秒数
|
|||
|
|
int totalSeconds = project.GetTotalDurationInSeconds();
|
|||
|
|
|
|||
|
|
// 重新设置,确保标准化
|
|||
|
|
project.SetTotalDurationInSeconds(totalSeconds);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取格式化的时长显示字符串(例如:"2分30秒" 或 "30秒")
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="project">项目实体</param>
|
|||
|
|
/// <returns>格式化的时长字符串</returns>
|
|||
|
|
/// <exception cref="ArgumentNullException">当 project 为 null 时抛出</exception>
|
|||
|
|
public static string GetFormattedDuration(this Project project)
|
|||
|
|
{
|
|||
|
|
if (project == null)
|
|||
|
|
throw new ArgumentNullException(nameof(project));
|
|||
|
|
|
|||
|
|
int totalSeconds = project.GetTotalDurationInSeconds();
|
|||
|
|
int minutes = totalSeconds / 60;
|
|||
|
|
int seconds = totalSeconds % 60;
|
|||
|
|
|
|||
|
|
if (minutes == 0)
|
|||
|
|
{
|
|||
|
|
return $"{seconds}秒";
|
|||
|
|
}
|
|||
|
|
else if (seconds == 0)
|
|||
|
|
{
|
|||
|
|
return $"{minutes}分";
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
return $"{minutes}分{seconds}秒";
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取符号格式的时长显示(例如:"2'30\"" 或 "30\"")
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="project">项目实体</param>
|
|||
|
|
/// <returns>符号格式的时长字符串</returns>
|
|||
|
|
/// <exception cref="ArgumentNullException">当 project 为 null 时抛出</exception>
|
|||
|
|
public static string GetSymbolFormattedDuration(this Project project)
|
|||
|
|
{
|
|||
|
|
if (project == null)
|
|||
|
|
throw new ArgumentNullException(nameof(project));
|
|||
|
|
|
|||
|
|
int totalSeconds = project.GetTotalDurationInSeconds();
|
|||
|
|
int hours = totalSeconds / 3600;
|
|||
|
|
int remainingSeconds = totalSeconds % 3600;
|
|||
|
|
int minutes = remainingSeconds / 60;
|
|||
|
|
int seconds = remainingSeconds % 60;
|
|||
|
|
|
|||
|
|
if (hours == 0)
|
|||
|
|
{
|
|||
|
|
if (seconds == 0)
|
|||
|
|
{
|
|||
|
|
return $"{minutes}'";
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
string paddedSeconds = seconds.ToString().PadLeft(2, '0');
|
|||
|
|
return $"{minutes}'{paddedSeconds}\"";
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
string result = $"{hours}h";
|
|||
|
|
if (minutes > 0 || seconds > 0)
|
|||
|
|
{
|
|||
|
|
result += $"{minutes}'";
|
|||
|
|
if (seconds > 0)
|
|||
|
|
{
|
|||
|
|
string paddedSeconds = seconds.ToString().PadLeft(2, '0');
|
|||
|
|
result += $"{paddedSeconds}\"";
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|