代码优化

This commit is contained in:
shangzhongyuan
2025-07-21 18:01:02 +08:00
parent 40e75a3774
commit 4e3a00523d
@@ -253,43 +253,22 @@ namespace YLErp.Modules.UnderlyingModule
/// <returns>返回3.12格式</returns>
public static double? getYearTerm(DateTime? calcDate, DateTime? sourceDate)
{
if (!calcDate.HasValue)
if (!calcDate.HasValue || !sourceDate.HasValue)
return null;
if (calcDate.Value > sourceDate.Value)
throw new ArgumentException("计算日期必须小于原日期", nameof(calcDate));
int totalMonths = (sourceDate.Value.Year - calcDate.Value.Year) * 12
+ (sourceDate.Value.Month - calcDate.Value.Month);
if (sourceDate.Value.Day < calcDate.Value.Day)
{
return null;//不限
totalMonths--;
}
if (sourceDate.HasValue)
{
if (calcDate > sourceDate)
{
throw new ArgumentException("计算日期必须小于原日期", nameof(calcDate));
}
//计算sourceDate与calcDate年限差
int yearDiff = sourceDate.Value.Year - calcDate.Value.Year;
int monthDiff = sourceDate.Value.Month - calcDate.Value.Month;
int dayDiff = sourceDate.Value.Day - calcDate.Value.Day;
// 如果月差为负,说明还没到整年,需要向前借一年
if (monthDiff < 0)
{
yearDiff -= 1;
monthDiff += 12;
}
// 如果天数为负,说明还没到整月,需要向前借一个月
if (dayDiff < 0)
{
monthDiff -= 1;
if (monthDiff < 0)
{
yearDiff -= 1;
monthDiff += 12;
}
}
double yearTerm = yearDiff + monthDiff / 12.0;
return yearTerm;
}
return null;//不限
double yearTerm = totalMonths / 12.0;
return yearTerm;
}
/// <summary>
/// 根据标的发行年限和剩余期限计算适用保证金率