修复加油包独立有效期和流量重置
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 12m22s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 12m22s
This commit is contained in:
@@ -2690,15 +2690,15 @@ func (s *Service) activateAddonPackage(ctx context.Context, tx *gorm.DB, order *
|
||||
status := constants.PackageUsageStatusActive
|
||||
activatedAt := now
|
||||
|
||||
// 计算过期时间(根据 has_independent_expiry)
|
||||
var expiresAt time.Time
|
||||
// 注意:has_independent_expiry 字段在 Package 模型中,暂时使用默认行为
|
||||
// 默认加油包跟随主套餐过期
|
||||
if mainPackage.ExpiresAt != nil {
|
||||
expiresAt = *mainPackage.ExpiresAt
|
||||
} else {
|
||||
expiresAt = now
|
||||
}
|
||||
// 加油包按自身有效期计算;仅当主套餐更早到期时截断到主套餐到期时间。
|
||||
expiresAt, hasIndependentExpiry := packagepkg.CalculateAddonExpiryTime(
|
||||
activatedAt,
|
||||
terms.CalendarType,
|
||||
terms.DurationMonths,
|
||||
terms.DurationDays,
|
||||
mainPackage.ExpiresAt,
|
||||
)
|
||||
nextResetAt := packagepkg.CalculateNextResetTime(pkg.DataResetCycle, terms.CalendarType, now, activatedAt)
|
||||
|
||||
virtualTotalMBSnapshot, displayGainRatioSnapshot, enableVirtualDataSnapshot := model.BuildPackageUsageSnapshotValues(pkg)
|
||||
addonRetailAmount := order.TotalAmount
|
||||
@@ -2719,8 +2719,10 @@ func (s *Service) activateAddonPackage(ctx context.Context, tx *gorm.DB, order *
|
||||
Status: status,
|
||||
Priority: priority,
|
||||
MasterUsageID: &mainPackage.ID,
|
||||
HasIndependentExpiry: hasIndependentExpiry,
|
||||
ActivatedAt: &activatedAt,
|
||||
ExpiresAt: &expiresAt,
|
||||
NextResetAt: nextResetAt,
|
||||
DataResetCycle: pkg.DataResetCycle,
|
||||
PaidAmount: &order.SellerCostPrice,
|
||||
RetailAmount: &addonRetailAmount,
|
||||
|
||||
@@ -62,6 +62,18 @@ func (s *ActivationService) AppendExpirationAudit(ctx context.Context, tx *gorm.
|
||||
return s.appendExpirationAudit(ctx, tx, &mainAfter, packageUsageStateData(main), addonChanges)
|
||||
}
|
||||
|
||||
// AppendAddonExpirationAudit 记录独立有效期加油包到期事实。
|
||||
func (s *ActivationService) AppendAddonExpirationAudit(ctx context.Context, tx *gorm.DB, addon *model.PackageUsage) error {
|
||||
if addon == nil {
|
||||
return errors.New(errors.CodeInvalidParam, "加油包权益资源不完整")
|
||||
}
|
||||
after := *addon
|
||||
after.Status = constants.PackageUsageStatusExpired
|
||||
return appendPackageUsageAudit(ctx, tx, s.auditWriter, constants.AuditActionPackageUsageExpired, "加油包权益到期", []packageUsageAuditChange{{
|
||||
Usage: &after, BeforeData: packageUsageStateData(addon), AfterData: packageUsageStateData(&after),
|
||||
}}, nil, map[string]any{"addon_usage_id": addon.ID})
|
||||
}
|
||||
|
||||
// RecordUsageFailure 在权益已定位且业务事务回滚后记录失败事实。
|
||||
func (s *ActivationService) RecordUsageFailure(ctx context.Context, actionCode, summary string, usage *model.PackageUsage, businessErr error) {
|
||||
recordPackageUsageFailure(ctx, s.db, s.auditWriter, actionCode, summary, usage, businessErr)
|
||||
|
||||
@@ -39,6 +39,16 @@ func CalculateExpiryTime(calendarType string, activatedAt time.Time, durationMon
|
||||
return expiryDate
|
||||
}
|
||||
|
||||
// CalculateAddonExpiryTime 计算加油包到期时间,并在主套餐更早到期时截断。
|
||||
// 返回值中的 independent 表示加油包自身有效期未晚于主套餐,因而按自身规则到期。
|
||||
func CalculateAddonExpiryTime(activatedAt time.Time, calendarType string, durationMonths, durationDays int, mainExpiresAt *time.Time) (expiresAt time.Time, independent bool) {
|
||||
addonExpiresAt := CalculateExpiryTime(calendarType, activatedAt, durationMonths, durationDays)
|
||||
if mainExpiresAt != nil && mainExpiresAt.Before(addonExpiresAt) {
|
||||
return *mainExpiresAt, false
|
||||
}
|
||||
return addonExpiresAt, true
|
||||
}
|
||||
|
||||
// CalculateValidityDays 计算套餐展示有效天数。
|
||||
// 按天套餐优先返回折算后的总天数;自然月套餐按 baseTime 推算到目标月月末。
|
||||
func CalculateValidityDays(calendarType string, baseTime time.Time, durationMonths, durationDays int) int {
|
||||
|
||||
Reference in New Issue
Block a user