feat(subscription): add support for wallet overflow and downgrade group functionality in subscription plans

This commit is contained in:
CaIon
2026-06-18 16:44:12 +08:00
parent 4e8b5e9b13
commit f6c260437c
17 changed files with 291 additions and 59 deletions
+21
View File
@@ -168,6 +168,9 @@ func AdminCreateSubscriptionPlan(c *gin.Context) {
if req.Plan.AllowBalancePay == nil {
req.Plan.AllowBalancePay = common.GetPointer(true)
}
if req.Plan.AllowWalletOverflow == nil {
req.Plan.AllowWalletOverflow = common.GetPointer(true)
}
if req.Plan.DurationUnit == "" {
req.Plan.DurationUnit = model.SubscriptionDurationMonth
}
@@ -189,6 +192,13 @@ func AdminCreateSubscriptionPlan(c *gin.Context) {
return
}
}
req.Plan.DowngradeGroup = strings.TrimSpace(req.Plan.DowngradeGroup)
if req.Plan.DowngradeGroup != "" {
if _, ok := ratio_setting.GetGroupRatioCopy()[req.Plan.DowngradeGroup]; !ok {
common.ApiErrorMsg(c, "降级分组不存在")
return
}
}
req.Plan.QuotaResetPeriod = model.NormalizeResetPeriod(req.Plan.QuotaResetPeriod)
if req.Plan.QuotaResetPeriod == model.SubscriptionResetCustom && req.Plan.QuotaResetCustomSeconds <= 0 {
common.ApiErrorMsg(c, "自定义重置周期需大于0秒")
@@ -256,6 +266,13 @@ func AdminUpdateSubscriptionPlan(c *gin.Context) {
return
}
}
req.Plan.DowngradeGroup = strings.TrimSpace(req.Plan.DowngradeGroup)
if req.Plan.DowngradeGroup != "" {
if _, ok := ratio_setting.GetGroupRatioCopy()[req.Plan.DowngradeGroup]; !ok {
common.ApiErrorMsg(c, "降级分组不存在")
return
}
}
req.Plan.QuotaResetPeriod = model.NormalizeResetPeriod(req.Plan.QuotaResetPeriod)
if req.Plan.QuotaResetPeriod == model.SubscriptionResetCustom && req.Plan.QuotaResetCustomSeconds <= 0 {
common.ApiErrorMsg(c, "自定义重置周期需大于0秒")
@@ -280,6 +297,7 @@ func AdminUpdateSubscriptionPlan(c *gin.Context) {
"max_purchase_per_user": req.Plan.MaxPurchasePerUser,
"total_amount": req.Plan.TotalAmount,
"upgrade_group": req.Plan.UpgradeGroup,
"downgrade_group": req.Plan.DowngradeGroup,
"quota_reset_period": req.Plan.QuotaResetPeriod,
"quota_reset_custom_seconds": req.Plan.QuotaResetCustomSeconds,
"updated_at": common.GetTimestamp(),
@@ -287,6 +305,9 @@ func AdminUpdateSubscriptionPlan(c *gin.Context) {
if req.Plan.AllowBalancePay != nil {
updateMap["allow_balance_pay"] = *req.Plan.AllowBalancePay
}
if req.Plan.AllowWalletOverflow != nil {
updateMap["allow_wallet_overflow"] = *req.Plan.AllowWalletOverflow
}
if err := tx.Model(&model.SubscriptionPlan{}).Where("id = ?", id).Updates(updateMap).Error; err != nil {
return err
}