有效天数问题,充值单回调问题
This commit is contained in:
@@ -10,7 +10,7 @@ import (
|
||||
// calendarType: 套餐周期类型(natural_month=自然月,by_day=按天)
|
||||
// activatedAt: 激活时间
|
||||
// durationMonths: 套餐时长(月数,calendar_type=natural_month 时使用)
|
||||
// durationDays: 套餐天数(calendar_type=by_day 时使用)
|
||||
// durationDays: 套餐天数(calendar_type=by_day 时使用,兼容每月折算天数)
|
||||
// 返回:过期时间(当天 23:59:59)
|
||||
func CalculateExpiryTime(calendarType string, activatedAt time.Time, durationMonths, durationDays int) time.Time {
|
||||
var expiryDate time.Time
|
||||
@@ -31,13 +31,53 @@ func CalculateExpiryTime(calendarType string, activatedAt time.Time, durationMon
|
||||
expiryDate = time.Date(targetYear, targetMonth+1, 0, 23, 59, 59, 0, activatedAt.Location())
|
||||
} else {
|
||||
// 按天套餐:activated_at + N 天,23:59:59
|
||||
expiryDate = activatedAt.AddDate(0, 0, durationDays)
|
||||
effectiveDays := calculateByDayValidityDays(durationMonths, durationDays)
|
||||
expiryDate = activatedAt.AddDate(0, 0, effectiveDays)
|
||||
expiryDate = time.Date(expiryDate.Year(), expiryDate.Month(), expiryDate.Day(), 23, 59, 59, 0, expiryDate.Location())
|
||||
}
|
||||
|
||||
return expiryDate
|
||||
}
|
||||
|
||||
// CalculateValidityDays 计算套餐展示有效天数。
|
||||
// 按天套餐优先返回折算后的总天数;自然月套餐按 baseTime 推算到目标月月末。
|
||||
func CalculateValidityDays(calendarType string, baseTime time.Time, durationMonths, durationDays int) int {
|
||||
if calendarType == constants.PackageCalendarTypeNaturalMonth {
|
||||
return calculateNaturalMonthValidityDays(baseTime, durationMonths)
|
||||
}
|
||||
return calculateByDayValidityDays(durationMonths, durationDays)
|
||||
}
|
||||
|
||||
func calculateByDayValidityDays(durationMonths, durationDays int) int {
|
||||
if durationDays <= 0 {
|
||||
if durationMonths <= 0 {
|
||||
return 0
|
||||
}
|
||||
return durationMonths * 30
|
||||
}
|
||||
|
||||
if durationMonths > 1 && durationDays <= 31 {
|
||||
return durationMonths * durationDays
|
||||
}
|
||||
|
||||
return durationDays
|
||||
}
|
||||
|
||||
func calculateNaturalMonthValidityDays(baseTime time.Time, durationMonths int) int {
|
||||
if baseTime.IsZero() || durationMonths <= 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
expiresAt := CalculateExpiryTime(constants.PackageCalendarTypeNaturalMonth, baseTime, durationMonths, 0)
|
||||
startDate := time.Date(baseTime.Year(), baseTime.Month(), baseTime.Day(), 0, 0, 0, 0, baseTime.Location())
|
||||
endDate := time.Date(expiresAt.Year(), expiresAt.Month(), expiresAt.Day(), 0, 0, 0, 0, expiresAt.Location())
|
||||
days := int(endDate.Sub(startDate).Hours()/24) + 1
|
||||
if days < 0 {
|
||||
return 0
|
||||
}
|
||||
return days
|
||||
}
|
||||
|
||||
// CalculateNextResetTime 计算下次流量重置时间
|
||||
// dataResetCycle: 流量重置周期(daily/monthly/yearly/none)
|
||||
// calendarType: 套餐周期类型(natural_month/by_day),影响月重置逻辑
|
||||
|
||||
Reference in New Issue
Block a user