feat: 套餐系统升级 - Worker 重构、流量重置、文档与规范更新
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 6m54s

- 重构 Worker 启动流程,引入 bootstrap 模块统一管理依赖注入
- 实现套餐流量重置服务(日/月/年周期重置)
- 新增套餐激活排队、加油包绑定、囤货待实名激活逻辑
- 新增订单创建幂等性防重(Redis 业务键 + 分布式锁)
- 更新 AGENTS.md/CLAUDE.md:新增注释规范、幂等性规范,移除测试要求
- 添加套餐系统升级完整文档(API文档、使用指南、功能总结、运维指南)
- 归档 OpenSpec package-system-upgrade 变更,同步 specs 到主目录
- 新增 queue types 抽象和 Redis 常量定义
This commit is contained in:
2026-02-12 14:24:15 +08:00
parent 655c9ce7a6
commit c665f32976
51 changed files with 7289 additions and 424 deletions

View File

@@ -118,19 +118,7 @@ func (s *ActivationService) ActivateByRealname(ctx context.Context, carrierType
expiresAt := CalculateExpiryTime(pkg.CalendarType, activatedAt, pkg.DurationMonths, pkg.DurationDays)
// 计算下次重置时间
billingDay := 1 // 默认1号计费
if carrierType == "iot_card" {
var card model.IotCard
if err := tx.First(&card, carrierID).Error; err == nil {
var carrier model.Carrier
if err := tx.First(&carrier, card.CarrierID).Error; err == nil {
if carrier.CarrierType == "CUCC" {
billingDay = 27 // 联通27号计费
}
}
}
}
nextResetAt := CalculateNextResetTime(pkg.DataResetCycle, now, billingDay)
nextResetAt := CalculateNextResetTime(pkg.DataResetCycle, pkg.CalendarType, now, activatedAt)
// 更新套餐使用记录
updates := map[string]interface{}{
@@ -290,19 +278,7 @@ func (s *ActivationService) activateNextMainPackage(ctx context.Context, tx *gor
expiresAt := CalculateExpiryTime(pkg.CalendarType, activatedAt, pkg.DurationMonths, pkg.DurationDays)
// 计算下次重置时间
billingDay := 1
if carrierType == "iot_card" {
var card model.IotCard
if err := tx.First(&card, carrierID).Error; err == nil {
var carrier model.Carrier
if err := tx.First(&carrier, card.CarrierID).Error; err == nil {
if carrier.CarrierType == "CUCC" {
billingDay = 27
}
}
}
}
nextResetAt := CalculateNextResetTime(pkg.DataResetCycle, now, billingDay)
nextResetAt := CalculateNextResetTime(pkg.DataResetCycle, pkg.CalendarType, now, activatedAt)
// 更新套餐使用记录
updates := map[string]interface{}{

View File

@@ -117,43 +117,48 @@ func (s *ResetService) resetMonthlyUsageWithDB(ctx context.Context, db *gorm.DB)
return nil
}
// 按套餐分组处理(因为需要区分联通27号 vs 其他1号
for _, pkg := range packages {
// 查询运营商信息以确定计费日
// 只有单卡套餐才根据运营商判断设备级套餐统一使用1号计费
billingDay := 1
if pkg.IotCardID != 0 {
var card model.IotCard
if err := tx.First(&card, pkg.IotCardID).Error; err == nil {
var carrier model.Carrier
if err := tx.First(&carrier, card.CarrierID).Error; err == nil {
if carrier.CarrierType == "CUCC" {
billingDay = 27
}
}
}
// 按套餐分组处理(根据套餐周期类型计算下次重置时间
for _, usage := range packages {
// 查询套餐信息,获取 calendar_type
var pkg model.Package
if err := tx.First(&pkg, usage.PackageID).Error; err != nil {
s.logger.Error("查询套餐信息失败",
zap.Uint("usage_id", usage.ID),
zap.Uint("package_id", usage.PackageID),
zap.Error(err))
continue
}
// 设备级套餐默认使用1号计费已在 billingDay := 1 初始化)
// 计算下次重置时间
nextReset := calculateNextMonthlyResetTime(now, billingDay)
// 计算下次重置时间(基于套餐周期类型)
// 自然月套餐每月1号重置
// 按天套餐每30天重置
activatedAt := usage.ActivatedAt
if activatedAt.IsZero() {
activatedAt = now // 兜底处理
}
nextResetAt := CalculateNextResetTime(constants.PackageDataResetMonthly, pkg.CalendarType, now, activatedAt)
if nextResetAt == nil {
s.logger.Warn("计算下次重置时间失败",
zap.Uint("usage_id", usage.ID))
continue
}
// 更新套餐
updates := map[string]interface{}{
"data_usage_mb": 0,
"last_reset_at": now,
"next_reset_at": nextReset,
"next_reset_at": *nextResetAt,
"status": constants.PackageUsageStatusActive, // 重置后恢复为生效中
}
if err := tx.Model(pkg).Updates(updates).Error; err != nil {
if err := tx.Model(usage).Updates(updates).Error; err != nil {
return errors.Wrap(errors.CodeDatabaseError, err, "重置月流量失败")
}
s.logger.Info("月流量已重置",
zap.Uint("usage_id", pkg.ID),
zap.Int("billing_day", billingDay),
zap.Time("next_reset_at", nextReset))
zap.Uint("usage_id", usage.ID),
zap.String("calendar_type", pkg.CalendarType),
zap.Time("next_reset_at", *nextResetAt))
}
return nil
@@ -217,26 +222,3 @@ func (s *ResetService) resetYearlyUsageWithDB(ctx context.Context, db *gorm.DB)
})
}
// calculateNextMonthlyResetTime 计算下次月重置时间
func calculateNextMonthlyResetTime(now time.Time, billingDay int) time.Time {
currentDay := now.Day()
targetMonth := now.Month()
targetYear := now.Year()
// 如果当前日期 >= 计费日,下次重置是下月计费日
if currentDay >= billingDay {
targetMonth++
if targetMonth > 12 {
targetMonth = 1
targetYear++
}
}
// 处理月末天数不足的情况例如2月没有27日
maxDay := time.Date(targetYear, targetMonth+1, 0, 0, 0, 0, 0, now.Location()).Day()
if billingDay > maxDay {
billingDay = maxDay
}
return time.Date(targetYear, targetMonth, billingDay, 0, 0, 0, 0, now.Location())
}

View File

@@ -40,10 +40,11 @@ func CalculateExpiryTime(calendarType string, activatedAt time.Time, durationMon
// CalculateNextResetTime 计算下次流量重置时间
// dataResetCycle: 流量重置周期daily/monthly/yearly/none
// calendarType: 套餐周期类型natural_month/by_day影响月重置逻辑
// currentTime: 当前时间
// billingDay: 计费日(月重置时使用,联通=27其他=1
// activatedAt: 套餐激活时间(按天套餐月重置时使用
// 返回下次重置时间00:00:00
func CalculateNextResetTime(dataResetCycle string, currentTime time.Time, billingDay int) *time.Time {
func CalculateNextResetTime(dataResetCycle, calendarType string, currentTime, activatedAt time.Time) *time.Time {
if dataResetCycle == constants.PackageDataResetNone {
// 不重置
return nil
@@ -63,46 +64,49 @@ func CalculateNextResetTime(dataResetCycle string, currentTime time.Time, billin
)
case constants.PackageDataResetMonthly:
// 月重置:下月 billingDay 号 00:00:00
year := currentTime.Year()
month := currentTime.Month()
if calendarType == constants.PackageCalendarTypeNaturalMonth {
// 自然月套餐每月1号 00:00:00 重置
year := currentTime.Year()
month := currentTime.Month()
// 检查 billingDay 是否为当前月的最后一天(月末计费的特殊情况)
currentMonthLastDay := time.Date(year, month+1, 0, 0, 0, 0, 0, currentTime.Location()).Day()
isBillingDayMonthEnd := billingDay >= currentMonthLastDay
// 如果当前日期 >= billingDay则重置时间为下个月的 billingDay
// 否则,重置时间为本月的 billingDay
// 特殊情况:如果 billingDay 是月末,并且当前日期已接近月末,则跳到下个月
shouldUseNextMonth := currentTime.Day() >= billingDay || (isBillingDayMonthEnd && currentTime.Day() >= currentMonthLastDay-1)
if shouldUseNextMonth {
// 下个月
month++
if month > 12 {
month = 1
year++
// 如果当前日期 >= 1号即已过重置点则下次重置为下个月1号
if currentTime.Day() >= 1 {
month++
if month > 12 {
month = 1
year++
}
}
}
// 计算目标月份的最后一天(处理月末情况)
lastDayOfMonth := time.Date(year, month+1, 0, 0, 0, 0, 0, currentTime.Location()).Day()
resetDay := billingDay
if billingDay > lastDayOfMonth {
// 如果 billingDay 超过该月天数,使用月末
resetDay = lastDayOfMonth
}
nextResetTime = time.Date(year, month, 1, 0, 0, 0, 0, currentTime.Location())
} else {
// 按天套餐从激活日期开始每30天重置一次
// 计算从激活到现在经过了多少个30天周期
daysSinceActivation := int(currentTime.Sub(activatedAt).Hours() / 24)
cyclesPassed := daysSinceActivation / 30
nextResetTime = time.Date(year, month, resetDay, 0, 0, 0, 0, currentTime.Location())
// 下次重置时间 = 激活时间 + (已过周期数+1) * 30天
nextResetTime = activatedAt.AddDate(0, 0, (cyclesPassed+1)*30)
nextResetTime = time.Date(
nextResetTime.Year(),
nextResetTime.Month(),
nextResetTime.Day(),
0, 0, 0, 0,
nextResetTime.Location(),
)
}
case constants.PackageDataResetYearly:
// 年重置:明年 1 月 1 日 00:00:00
nextResetTime = time.Date(
currentTime.Year()+1,
1, 1,
0, 0, 0, 0,
currentTime.Location(),
)
// 年重置:每年1月1日 00:00:00
year := currentTime.Year()
// 如果当前日期已经过了1月1日则使用明年
jan1ThisYear := time.Date(year, 1, 1, 0, 0, 0, 0, currentTime.Location())
if currentTime.After(jan1ThisYear) || currentTime.Equal(jan1ThisYear) {
year++
}
nextResetTime = time.Date(year, 1, 1, 0, 0, 0, 0, currentTime.Location())
default:
return nil