限制套餐具体到 档位
Build docker and publish / build (20.15.1) (push) Successful in 7m45s

This commit is contained in:
2026-03-08 21:25:07 -07:00
parent 69028898a4
commit 79a97ec569
7 changed files with 92 additions and 21 deletions
@@ -13,3 +13,19 @@ func getDiscount(discounts []types.SubscribeDiscount, inputMonths int64) float64
return finalDiscount / float64(100)
}
// isNewUserOnlyForQuantity checks whether the matched discount tier has new_user_only enabled.
func isNewUserOnlyForQuantity(discounts []types.SubscribeDiscount, inputQuantity int64) bool {
var finalDiscount float64 = 100
var newUserOnly bool
for _, discount := range discounts {
if inputQuantity >= discount.Quantity && discount.Discount < finalDiscount {
finalDiscount = discount.Discount
newUserOnly = discount.NewUserOnly
}
}
return newUserOnly
}
@@ -109,8 +109,14 @@ func (l *PreCreateOrderLogic) PreCreateOrder(req *types.PurchaseOrderRequest) (r
}
}
// check new user only restriction
if !isSingleModeRenewal && sub.NewUserOnly != nil && *sub.NewUserOnly {
// check new user only restriction (tier-level only)
var newUserOnly bool
if !isSingleModeRenewal && sub.Discount != "" {
var dis []types.SubscribeDiscount
_ = json.Unmarshal([]byte(sub.Discount), &dis)
newUserOnly = isNewUserOnlyForQuantity(dis, req.Quantity)
}
if newUserOnly {
if time.Since(u.CreatedAt) > 24*time.Hour {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SubscribeNewUserOnly), "not a new user")
}
+21 -13
View File
@@ -270,20 +270,28 @@ func (l *PurchaseLogic) Purchase(req *types.PurchaseOrderRequest) (resp *types.P
}
}
// check new user only restriction inside transaction to prevent race condition
if orderInfo.Type == 1 && sub.NewUserOnly != nil && *sub.NewUserOnly {
if time.Since(u.CreatedAt) > 24*time.Hour {
return errors.Wrapf(xerr.NewErrCode(xerr.SubscribeNewUserOnly), "not a new user")
// check new user only restriction inside transaction to prevent race condition (tier-level only)
if orderInfo.Type == 1 {
var txNewUserOnly bool
if sub.Discount != "" {
var dis []types.SubscribeDiscount
_ = json.Unmarshal([]byte(sub.Discount), &dis)
txNewUserOnly = isNewUserOnlyForQuantity(dis, orderInfo.Quantity)
}
var historyCount int64
if e := db.Model(&order.Order{}).
Where("user_id = ? AND subscribe_id = ? AND type = 1 AND status IN ?",
u.Id, targetSubscribeID, []int{2, 5}).
Count(&historyCount).Error; e != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "check new user purchase history error: %v", e.Error())
}
if historyCount >= 1 {
return errors.Wrapf(xerr.NewErrCode(xerr.SubscribeNewUserOnly), "already purchased new user plan")
if txNewUserOnly {
if time.Since(u.CreatedAt) > 24*time.Hour {
return errors.Wrapf(xerr.NewErrCode(xerr.SubscribeNewUserOnly), "not a new user")
}
var historyCount int64
if e := db.Model(&order.Order{}).
Where("user_id = ? AND subscribe_id = ? AND type = 1 AND status IN ?",
u.Id, targetSubscribeID, []int{2, 5}).
Count(&historyCount).Error; e != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "check new user purchase history error: %v", e.Error())
}
if historyCount >= 1 {
return errors.Wrapf(xerr.NewErrCode(xerr.SubscribeNewUserOnly), "already purchased new user plan")
}
}
}